From 6b3b92d7dddf8ce7435467f60e4d0331c3cc8767 Mon Sep 17 00:00:00 2001
From: Houtan Bastani <houtan@dynare.org>
Date: Thu, 28 Mar 2013 16:46:00 +0100
Subject: [PATCH] reporting: @graph, @series, support syntax simplification

---
 matlab/reporting/@graph/addSeries.m           | 22 +++++++++
 matlab/reporting/@graph/createGraph.m         | 38 +++++++--------
 matlab/reporting/@graph/display.m             | 15 ++----
 matlab/reporting/@graph/graph.m               | 21 +++++++-
 matlab/reporting/@graph/subsasgn.m            |  4 ++
 matlab/reporting/@graph/subsref.m             |  4 +-
 matlab/reporting/@section/element.m           | 23 +++++++++
 matlab/reporting/@section/subsasgn.m          | 11 ++++-
 matlab/reporting/@series/getName.m            | 23 +++++++++
 matlab/reporting/@seriesElements/addSeries.m  | 22 +++++++++
 matlab/reporting/@seriesElements/display.m    | 37 ++++++++++++++
 matlab/reporting/@seriesElements/getNames.m   | 26 ++++++++++
 .../@seriesElements/getSeriesElements.m       | 22 +++++++++
 matlab/reporting/@seriesElements/numel.m      | 22 +++++++++
 .../@seriesElements/seriesElements.m          | 42 ++++++++++++++++
 matlab/reporting/@seriesElements/subsasgn.m   | 39 +++++++++++++++
 matlab/reporting/@seriesElements/subsref.m    | 48 +++++++++++++++++++
 17 files changed, 383 insertions(+), 36 deletions(-)
 create mode 100644 matlab/reporting/@graph/addSeries.m
 create mode 100644 matlab/reporting/@section/element.m
 create mode 100644 matlab/reporting/@series/getName.m
 create mode 100644 matlab/reporting/@seriesElements/addSeries.m
 create mode 100644 matlab/reporting/@seriesElements/display.m
 create mode 100644 matlab/reporting/@seriesElements/getNames.m
 create mode 100644 matlab/reporting/@seriesElements/getSeriesElements.m
 create mode 100644 matlab/reporting/@seriesElements/numel.m
 create mode 100644 matlab/reporting/@seriesElements/seriesElements.m
 create mode 100644 matlab/reporting/@seriesElements/subsasgn.m
 create mode 100644 matlab/reporting/@seriesElements/subsref.m

diff --git a/matlab/reporting/@graph/addSeries.m b/matlab/reporting/@graph/addSeries.m
new file mode 100644
index 0000000000..ddfcb41325
--- /dev/null
+++ b/matlab/reporting/@graph/addSeries.m
@@ -0,0 +1,22 @@
+function o = addSeries(o, varargin)
+% function o = addSeries(o, varargin)
+
+% Copyright (C) 2013 Dynare Team
+%
+% This file is part of Dynare.
+%
+% Dynare is free software: you can redistribute it and/or modify
+% it under the terms of the GNU General Public License as published by
+% the Free Software Foundation, either version 3 of the License, or
+% (at your option) any later version.
+%
+% Dynare is distributed in the hope that it will be useful,
+% but WITHOUT ANY WARRANTY; without even the implied warranty of
+% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+% GNU General Public License for more details.
+%
+% You should have received a copy of the GNU General Public License
+% along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
+
+o.seriesElements = o.seriesElements.addSeries(varargin{:});
+end
\ No newline at end of file
diff --git a/matlab/reporting/@graph/createGraph.m b/matlab/reporting/@graph/createGraph.m
index 98035a73a2..0daf941b87 100644
--- a/matlab/reporting/@graph/createGraph.m
+++ b/matlab/reporting/@graph/createGraph.m
@@ -28,15 +28,11 @@ function o = createGraph(o)
 % You should have received a copy of the GNU General Public License
 % along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
 
-assert(~isempty(o.data));
-
 if ~isempty(o.figname)
     warning('@graph.createGraph: will overwrite %s with new graph\n', ...
             o.figname);
 end
 
-%o = readConfig(o);
-
 disp('creating plot..........');
 h = figure('visible','off');
 hold on;
@@ -45,25 +41,25 @@ if o.grid
     grid on;
     set(gca, 'GridLineStyle', '--');
 end
-%set(0, 'CurrentFigure',h);
-%set(h, 'PaperPositionMode', 'auto');
-%set(h, 'units', 'normalized', 'outerposition', [0 0 1 1]);
 
-if isempty(o.seriestouse)
-    ds = o.data;
-else
-    ds = o.data{o.seriestouse{:}};
+ne = o.seriesElements.numel();
+dd = dynDates();
+for i=1:ne
+    ddt = o.seriesElements(i).getLine(o.xrange);
+    if isempty(dd)
+        dd = ddt;
+        continue
+    end
+    if ddt(1) < dd(1)
+        dd = union(ddt(1):dd(1), dd);
+    end
+    if ddt(ddt.ndat) > dd(dd.ndat)
+        dd = union(dd, dd(dd.ndat):ddt(ddt.ndat));
+    end
 end
 
-if ~isempty(o.xrange)
-    ds  = ds(o.xrange);
-end
-data = ds.data;
-
-x = 1:1:ds.nobs;
-xlabels = getDatesCellStringArray(ds.time);
-
-plot(x, data);
+x = 1:1:dd.ndat;
+xlabels = getDatesCellStringArray(dd);
 
 if ~isempty(o.yrange)
     ylim(o.yrange);
@@ -87,7 +83,7 @@ set(gca,'XTick', x);
 set(gca,'XTickLabel', xlabels);
 
 if o.legend
-    lh = legend(ds.name);
+    lh = legend(o.seriesElements.getNames());
     set(lh, 'orientation', o.legend_orientation);
     set(lh, 'Location', o.legend_location);
     set(lh, 'FontSize', o.legend_font_size);
diff --git a/matlab/reporting/@graph/display.m b/matlab/reporting/@graph/display.m
index 7938abb45b..b05877a950 100644
--- a/matlab/reporting/@graph/display.m
+++ b/matlab/reporting/@graph/display.m
@@ -54,16 +54,6 @@ disp([name '.figname = ']);
 disp(' ');
 disp(['     ''' o.figname '''']);
 
-disp(' ');
-disp([name '.data = ']);
-disp(' ');
-display(o.data);
-
-disp(' ');
-disp([name '.seriestoplot = ']);
-disp(' ');
-disp(o.seriestoplot);
-
 disp(' ');
 disp([name '.config = ']);
 disp(' ');
@@ -78,4 +68,9 @@ disp(' ');
 disp([name '.shade = ']);
 disp(' ');
 disp(o.shade);
+
+disp(' ');
+disp([name '.seriesElements = ']);
+disp(' ');
+o.seriesElements.getSeriesElements()
 end
\ No newline at end of file
diff --git a/matlab/reporting/@graph/graph.m b/matlab/reporting/@graph/graph.m
index 56fd9518e9..c1a16a0301 100644
--- a/matlab/reporting/@graph/graph.m
+++ b/matlab/reporting/@graph/graph.m
@@ -35,6 +35,8 @@ o = struct;
 
 o.config = '';
 
+o.seriesElements = seriesElements();
+
 o.title = '';
 o.ylabel = '';
 o.xlabel = '';
@@ -112,11 +114,26 @@ assert(isempty(o.yrange) || (isfloat(o.yrange) && length(o.yrange) == 2 && ...
                              o.yrange(1) < o.yrange(2)), ...
        ['@graph.graph: yrange is specified an array with two float entries, ' ...
         'the lower bound and upper bound.']);
-assert(~isempty(o.data) && isa(o.data, 'dynSeries'), ['@graph.graph: must ' ...
-                    'provide data as a dynSeries']);
+assert(isempty(o.data) || isa(o.data, 'dynSeries'), ['@graph.graph: data must ' ...
+                    'be a dynSeries']);
 assert(isempty(o.seriestouse) || iscellstr(o.seriestouse), ['@graph.graph: ' ...
                     'series to use must be a cell array of string(s)']);
 
+% using o.seriestouse, create series objects and put them in o.seriesElements
+if ~isempty(o.data)
+    if isempty(o.seriestouse)
+        for i=1:o.data.vobs
+            o.seriesElements = o.seriesElements.addSeries('data', o.data{o.name{i}});
+        end
+    else
+        for i=1:length(o.seriestouse)
+            o.seriesElements = o.seriesElements.addSeries('data', o.data{o.seriestouse{i}});
+        end
+    end
+end
+o = rmfield(o, 'seriestouse');
+o = rmfield(o, 'data');
+
 % Create graph object
 o = class(o, 'graph');
 end
\ No newline at end of file
diff --git a/matlab/reporting/@graph/subsasgn.m b/matlab/reporting/@graph/subsasgn.m
index 8ab064094a..fb645de5b6 100644
--- a/matlab/reporting/@graph/subsasgn.m
+++ b/matlab/reporting/@graph/subsasgn.m
@@ -29,6 +29,10 @@ if length(S) > 1
 end
 
 switch S.type
+    case '()'
+        index = S.subs{:};
+        assert(isnumeric(index));
+        B.seriesElements(index) = V;
     case '.'
         switch S.subs
             case fieldnames(A)
diff --git a/matlab/reporting/@graph/subsref.m b/matlab/reporting/@graph/subsref.m
index 1610116f77..d25f47fa3b 100644
--- a/matlab/reporting/@graph/subsref.m
+++ b/matlab/reporting/@graph/subsref.m
@@ -33,7 +33,9 @@ switch S(1).type
             otherwise
                 error(['@graph.subsref: unknown field or method: ' S(1).subs]);
         end
-    case {'()', '{}'}
+    case '()'
+        A = A.seriesElements.getSeriesElements(S(1).subs{:});
+    case '{}'
         error(['@graph.subsref: ' S(1).type ' indexing not supported.']);
     otherwise
         error('@graph.subsref: impossible case')
diff --git a/matlab/reporting/@section/element.m b/matlab/reporting/@section/element.m
new file mode 100644
index 0000000000..7aab80960f
--- /dev/null
+++ b/matlab/reporting/@section/element.m
@@ -0,0 +1,23 @@
+function o = element(o, index)
+% function o = element(o, index)
+
+% Copyright (C) 2013 Dynare Team
+%
+% This file is part of Dynare.
+%
+% Dynare is free software: you can redistribute it and/or modify
+% it under the terms of the GNU General Public License as published by
+% the Free Software Foundation, either version 3 of the License, or
+% (at your option) any later version.
+%
+% Dynare is distributed in the hope that it will be useful,
+% but WITHOUT ANY WARRANTY; without even the implied warranty of
+% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+% GNU General Public License for more details.
+%
+% You should have received a copy of the GNU General Public License
+% along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
+
+assert(isint(index));
+o = o.elements(index);
+end
\ No newline at end of file
diff --git a/matlab/reporting/@section/subsasgn.m b/matlab/reporting/@section/subsasgn.m
index 79f3af53a1..a0a7a1164e 100644
--- a/matlab/reporting/@section/subsasgn.m
+++ b/matlab/reporting/@section/subsasgn.m
@@ -32,8 +32,15 @@ switch S.type
     case '()'
         index = S.subs{:};
         assert(isnumeric(index));
-        B(index) = V;
+        B.elements(index) = V;
+    case '.'
+        switch S.subs
+            case fieldnames(A)
+                B.(S.subs) = V;
+            otherwise
+                error(['@section.subsasgn: field ' S.subs 'does not exist']);
+        end
     otherwise
-        error('@section.subsasgn: syntax error')
+        error('@section.subsasgn: syntax error');
 end
 end
\ No newline at end of file
diff --git a/matlab/reporting/@series/getName.m b/matlab/reporting/@series/getName.m
new file mode 100644
index 0000000000..4296e9bc65
--- /dev/null
+++ b/matlab/reporting/@series/getName.m
@@ -0,0 +1,23 @@
+function s = getName(o)
+%function s = getName(o)
+
+% Copyright (C) 2013 Dynare Team
+%
+% This file is part of Dynare.
+%
+% Dynare is free software: you can redistribute it and/or modify
+% it under the terms of the GNU General Public License as published by
+% the Free Software Foundation, either version 3 of the License, or
+% (at your option) any later version.
+%
+% Dynare is distributed in the hope that it will be useful,
+% but WITHOUT ANY WARRANTY; without even the implied warranty of
+% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+% GNU General Public License for more details.
+%
+% You should have received a copy of the GNU General Public License
+% along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
+
+assert(~isempty(o.data) && numel(o.data) == 1);
+s = o.data(1).name();
+end
\ No newline at end of file
diff --git a/matlab/reporting/@seriesElements/addSeries.m b/matlab/reporting/@seriesElements/addSeries.m
new file mode 100644
index 0000000000..3c683576ce
--- /dev/null
+++ b/matlab/reporting/@seriesElements/addSeries.m
@@ -0,0 +1,22 @@
+function o = addSeries(o, varargin)
+% function o = addSeries(o, varargin)
+
+% Copyright (C) 2013 Dynare Team
+%
+% This file is part of Dynare.
+%
+% Dynare is free software: you can redistribute it and/or modify
+% it under the terms of the GNU General Public License as published by
+% the Free Software Foundation, either version 3 of the License, or
+% (at your option) any later version.
+%
+% Dynare is distributed in the hope that it will be useful,
+% but WITHOUT ANY WARRANTY; without even the implied warranty of
+% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+% GNU General Public License for more details.
+%
+% You should have received a copy of the GNU General Public License
+% along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
+
+o.objArray = o.objArray.addObj(series(varargin{:}));
+end
\ No newline at end of file
diff --git a/matlab/reporting/@seriesElements/display.m b/matlab/reporting/@seriesElements/display.m
new file mode 100644
index 0000000000..38061b3e97
--- /dev/null
+++ b/matlab/reporting/@seriesElements/display.m
@@ -0,0 +1,37 @@
+function display(o)
+%function display(o)
+% Display a Elements object
+%
+% INPUTS
+%   none
+%
+% OUTPUTS
+%   none
+%
+% SPECIAL REQUIREMENTS
+%   none
+
+% Copyright (C) 2013 Dynare Team
+%
+% This file is part of Dynare.
+%
+% Dynare is free software: you can redistribute it and/or modify
+% it under the terms of the GNU General Public License as published by
+% the Free Software Foundation, either version 3 of the License, or
+% (at your option) any later version.
+%
+% Dynare is distributed in the hope that it will be useful,
+% but WITHOUT ANY WARRANTY; without even the implied warranty of
+% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+% GNU General Public License for more details.
+%
+% You should have received a copy of the GNU General Public License
+% along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
+
+name = 'report.page.section.elements.seriesElement';
+
+disp(' ');
+disp([name ' = ']);
+disp(' ');
+disp(getSeriesElements(o));
+end
\ No newline at end of file
diff --git a/matlab/reporting/@seriesElements/getNames.m b/matlab/reporting/@seriesElements/getNames.m
new file mode 100644
index 0000000000..cbb29b3edb
--- /dev/null
+++ b/matlab/reporting/@seriesElements/getNames.m
@@ -0,0 +1,26 @@
+function names = getNames(o, varargin)
+% function names = getNames(o, varargin)
+
+% Copyright (C) 2013 Dynare Team
+%
+% This file is part of Dynare.
+%
+% Dynare is free software: you can redistribute it and/or modify
+% it under the terms of the GNU General Public License as published by
+% the Free Software Foundation, either version 3 of the License, or
+% (at your option) any later version.
+%
+% Dynare is distributed in the hope that it will be useful,
+% but WITHOUT ANY WARRANTY; without even the implied warranty of
+% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+% GNU General Public License for more details.
+%
+% You should have received a copy of the GNU General Public License
+% along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
+
+se = o.objArray.getObjs(varargin{:});
+names = {};
+for i=1:length(se)
+    names(i) = se{i}.getName();
+end
+end
\ No newline at end of file
diff --git a/matlab/reporting/@seriesElements/getSeriesElements.m b/matlab/reporting/@seriesElements/getSeriesElements.m
new file mode 100644
index 0000000000..1a9dcd2ab8
--- /dev/null
+++ b/matlab/reporting/@seriesElements/getSeriesElements.m
@@ -0,0 +1,22 @@
+function o = getSeriesElements(o, varargin)
+% function o = getSeriesElements(o, varargin)
+
+% Copyright (C) 2013 Dynare Team
+%
+% This file is part of Dynare.
+%
+% Dynare is free software: you can redistribute it and/or modify
+% it under the terms of the GNU General Public License as published by
+% the Free Software Foundation, either version 3 of the License, or
+% (at your option) any later version.
+%
+% Dynare is distributed in the hope that it will be useful,
+% but WITHOUT ANY WARRANTY; without even the implied warranty of
+% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+% GNU General Public License for more details.
+%
+% You should have received a copy of the GNU General Public License
+% along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
+
+o = o.objArray.getObjs(varargin{:});
+end
\ No newline at end of file
diff --git a/matlab/reporting/@seriesElements/numel.m b/matlab/reporting/@seriesElements/numel.m
new file mode 100644
index 0000000000..e52bd80314
--- /dev/null
+++ b/matlab/reporting/@seriesElements/numel.m
@@ -0,0 +1,22 @@
+function n = numel(o)
+% function n = numel(o)
+
+% Copyright (C) 2013 Dynare Team
+%
+% This file is part of Dynare.
+%
+% Dynare is free software: you can redistribute it and/or modify
+% it under the terms of the GNU General Public License as published by
+% the Free Software Foundation, either version 3 of the License, or
+% (at your option) any later version.
+%
+% Dynare is distributed in the hope that it will be useful,
+% but WITHOUT ANY WARRANTY; without even the implied warranty of
+% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+% GNU General Public License for more details.
+%
+% You should have received a copy of the GNU General Public License
+% along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
+
+n = o.objArray.numObjs();
+end
\ No newline at end of file
diff --git a/matlab/reporting/@seriesElements/seriesElements.m b/matlab/reporting/@seriesElements/seriesElements.m
new file mode 100644
index 0000000000..a6bd3ebced
--- /dev/null
+++ b/matlab/reporting/@seriesElements/seriesElements.m
@@ -0,0 +1,42 @@
+function o = seriesElements(varargin)
+%function o = seriesElements(varargin)
+% SeriesElements Class Constructor
+%
+% INPUTS
+%   Optional seriesElements object
+%
+% OUTPUTS
+%   seriesElements object
+%
+% SPECIAL REQUIREMENTS
+%   none
+
+% Copyright (C) 2013 Dynare Team
+%
+% This file is part of Dynare.
+%
+% Dynare is free software: you can redistribute it and/or modify
+% it under the terms of the GNU General Public License as published by
+% the Free Software Foundation, either version 3 of the License, or
+% (at your option) any later version.
+%
+% Dynare is distributed in the hope that it will be useful,
+% but WITHOUT ANY WARRANTY; without even the implied warranty of
+% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+% GNU General Public License for more details.
+%
+% You should have received a copy of the GNU General Public License
+% along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
+
+switch nargin
+    case 0
+        o = class(struct, 'seriesElements', objArray());
+    case 1
+        assert(isa(varargin{1}, 'seriesElements'), ...
+            ['@seriesElements.seriesElements: with one arg, you must pass an seriesElements ' ...
+             'object or a char.']);
+        o = varargin{1};
+    otherwise
+        error('@seriesElements.seriesElements: invalid number of arguments');
+end
+end
\ No newline at end of file
diff --git a/matlab/reporting/@seriesElements/subsasgn.m b/matlab/reporting/@seriesElements/subsasgn.m
new file mode 100644
index 0000000000..fab53a73c4
--- /dev/null
+++ b/matlab/reporting/@seriesElements/subsasgn.m
@@ -0,0 +1,39 @@
+function B = subsasgn(A, S, V)
+% function B = subsasgn(A, S, V)
+
+% Copyright (C) 2013 Dynare Team
+%
+% This file is part of Dynare.
+%
+% Dynare is free software: you can redistribute it and/or modify
+% it under the terms of the GNU General Public License as published by
+% the Free Software Foundation, either version 3 of the License, or
+% (at your option) any later version.
+%
+% Dynare is distributed in the hope that it will be useful,
+% but WITHOUT ANY WARRANTY; without even the implied warranty of
+% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+% GNU General Public License for more details.
+%
+% You should have received a copy of the GNU General Public License
+% along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
+
+B = A;
+if length(S) > 1
+    for i=1:(length(S)-1)
+        B = subsref(B, S(i));
+    end
+    B = subsasgn(B, S(end), V);
+    B = subsasgn(A, S(1:(end-1)), B);
+    return
+end
+
+switch S.type
+    case '()'
+        index = S.subs{:};
+        assert(isnumeric(index));
+        B.objArray(index) = V;
+    otherwise
+        error('@seriesElements.subsasign: syntax error');
+end
+end
\ No newline at end of file
diff --git a/matlab/reporting/@seriesElements/subsref.m b/matlab/reporting/@seriesElements/subsref.m
new file mode 100644
index 0000000000..f4c171abdc
--- /dev/null
+++ b/matlab/reporting/@seriesElements/subsref.m
@@ -0,0 +1,48 @@
+function A = subsref(A, S)
+%function A = subsref(A, S)
+
+% Copyright (C) 2013 Dynare Team
+%
+% This file is part of Dynare.
+%
+% Dynare is free software: you can redistribute it and/or modify
+% it under the terms of the GNU General Public License as published by
+% the Free Software Foundation, either version 3 of the License, or
+% (at your option) any later version.
+%
+% Dynare is distributed in the hope that it will be useful,
+% but WITHOUT ANY WARRANTY; without even the implied warranty of
+% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+% GNU General Public License for more details.
+%
+% You should have received a copy of the GNU General Public License
+% along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
+
+switch S(1).type
+    case '.'
+        switch S(1).subs
+            case fieldnames(A)
+                A = A.(S(1).subs);
+            case methods(A)
+                if areParensNext(S)
+                    A = feval(S(1).subs, A, S(2).subs{:});
+                    S = shiftS(S);
+                else
+                    A = feval(S(1).subs, A);
+                end
+            otherwise
+                error(['@seriesElements.subsref: unknown field or method: ' S(1).subs]);
+        end
+    case '()'
+        A = getSeriesElements(A, S(1).subs{:});
+    case '{}'
+        error(['@seriesElements.subsref: ' S(1).type ' indexing not supported.']);
+    otherwise
+        error('@seriesElements.subsref: impossible case');
+end
+
+S = shiftS(S);
+if length(S) >= 1
+    A = subsref(A, S);
+end
+end
-- 
GitLab