Minsky
godleyTable.h
Go to the documentation of this file.
1 /*
2  @copyright Steve Keen 2012
3  @author Russell Standish
4  This file is part of Minsky.
5 
6  Minsky is free software: you can redistribute it and/or modify it
7  under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  Minsky is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with Minsky. If not, see <http://www.gnu.org/licenses/>.
18 */
19 #ifndef GODLEYTABLE_H
20 #define GODLEYTABLE_H
21 
22 #include <set>
23 #include <vector>
24 
25 #include <ecolab.h>
26 
27 #include "variable.h"
28 #include "assetClass.h"
29 
30 namespace minsky
31 {
32  using namespace std;
33  using classdesc::shared_ptr;
34 
36  {
37  public:
38 
39  friend struct SchemaHelper;
40  friend class GodleyIcon;
41  typedef std::vector<std::vector<string>> Data;
42  private:
45  vector<AssetClass> m_assetClass{noAssetClass, asset, liability, equity};
47 
48  static void markEdited();
49  void _resize(unsigned rows, unsigned cols) {
50  // resize existing
51  for (std::size_t i=0; i<data.size(); ++i) data[i].resize(cols);
52  data.resize(rows, vector<string>(cols));
53  m_assetClass.resize(cols, noAssetClass);
54  }
55  public:
57 
59 
60  std::string title;
61 
62  static const char* initialConditions;
63  GodleyTable(): doubleEntryCompliant(true)
64  {
65  _resize(2,4);
66  cell(1,0)=initialConditions;
67 
68  }
69 
70  // Perform deep comparison of Godley tables in history to avoid spurious noAssetClass columns from arising during undo. For ticket 1118.
71  bool operator==(const GodleyTable& other) const
72  {
73  return (data==other.data && m_assetClass==other.m_assetClass &&
74  doubleEntryCompliant==other.doubleEntryCompliant && title==other.title);
75  }
76 
78  const vector<AssetClass>& _assetClass() const {return m_assetClass;}
79  AssetClass _assetClass(std::size_t col) const;
80  AssetClass _assetClass(std::size_t col, AssetClass cls);
81 
84  bool singleEquity() const;
85 
89  void nameUnique();
90 
94  bool signConventionReversed(int col) const
95  {
96  return doubleEntryCompliant &&
97  (_assetClass(col)==liability || _assetClass(col)==equity);
98  }
106  string assetClass(ecolab::TCL_args args);
107 
109  bool initialConditionRow(unsigned row) const;
110 
112  bool singularRow(unsigned row, unsigned col);
113 
114  std::size_t rows() const {return data.size();}
115  std::size_t cols() const {return data.empty()? 0: data[0].size();}
116 
117  void clear() {data.clear(); m_assetClass.clear(); markEdited();}
118  void resize(unsigned rows, unsigned cols){_resize(rows,cols); markEdited();}
119 
128  void insertRow(unsigned row);
131  void deleteRow(unsigned row) {data.erase(data.begin()+row);}
133  void insertCol(unsigned col);
135  void deleteCol(unsigned col);
138  void moveRow(int row, int n);
140  void moveCol(int col, int n);
141 
142  void dimension(unsigned rows, unsigned cols) {clear(); resize(rows,cols);}
143 
144  string& cell(unsigned row, unsigned col) {
145  if (row>=rows() || col>=cols())
146  _resize(row+1, col+1);
147  if (data[row].size()<=col) data[row].resize(cols());
148  return data[row][col];
149  }
150  const string& cell(unsigned row, unsigned col) const {
151  if (row>=data.size() || col>=data[row].size())
152  throw std::out_of_range("Godley table index error");
153  return data[row][col];
154  }
155  bool cellInTable(int row, int col) const
156  {return row>=0 && std::size_t(row)<rows() && col>=0 && std::size_t(col)<cols();}
157  string getCell(unsigned row, unsigned col) const {
158  if (row<rows() && col<cols())
159  return cell(row,col);
160  else
161  return "";
162  }
163  void setCell(unsigned row, unsigned col, const string& data) {
164  cell(row,col)=data;
165  }
166 
168  void balanceEquity(int col);
169 
171  std::vector<std::string> getColumnVariables() const;
174  std::vector<std::string> getVariables() const;
177  std::string savedText;
178 
180  std::vector<std::string> getColumn(unsigned col) const;
181 
183  void setDEmode(bool doubleEntryCompliant);
184 
185  std::map<std::string,double> rowSumAsMap(int row) const;
186  static std::string stringify(const std::map<std::string,double>&);
187 
189  std::string rowSum(int row) const {return stringify(rowSumAsMap(row));}
190 
192  const Data& getData() const {return data;}
193 
194  void exportToLaTeX(const std::string& filename) const;
195  void exportToCSV(const std::string& filename) const;
196 
198  void orderAssetClasses();
199 
201  void rename(const std::string& from, const std::string& to);
203  void renameFlows(const std::string& from, const std::string& to);
205  void renameStock(const std::string& from, const std::string& to);
206  };
207 
208 }
209 
210 #include "godleyTable.cd"
211 #include "godleyTable.xcd"
212 #endif
std::size_t cols() const
Definition: godleyTable.h:115
const Data & getData() const
accessor for schema access
Definition: godleyTable.h:192
bool cellInTable(int row, int col) const
Definition: godleyTable.h:155
void exportToLaTeX(std::ostream &f, const GodleyTable &g)
Definition: godleyExport.cc:84
vector< AssetClass > m_assetClass
class of each column (used in DE compliant mode)
Definition: godleyTable.h:45
void deleteRow(unsigned row)
delete row at row
Definition: godleyTable.h:131
STL namespace.
std::string rowSum(int row) const
return the symbolic sum across a row
Definition: godleyTable.h:189
string & cell(unsigned row, unsigned col)
Definition: godleyTable.h:144
string getCell(unsigned row, unsigned col) const
Definition: godleyTable.h:157
std::size_t rows() const
Definition: godleyTable.h:114
Creation and access to the minskyTCL_obj object, which has code to record whenever Minsky&#39;s state cha...
Definition: constMap.h:22
void resize(unsigned rows, unsigned cols)
Definition: godleyTable.h:118
void setCell(unsigned row, unsigned col, const string &data)
Definition: godleyTable.h:163
#define CLASSDESC_ACCESS(type)
const string & cell(unsigned row, unsigned col) const
Definition: godleyTable.h:150
GodleyAssetClass::AssetClass AssetClass
Definition: godleyTable.h:56
std::string title
Definition: godleyTable.h:60
const vector< AssetClass > & _assetClass() const
class of each column (used in DE compliant mode)
Definition: godleyTable.h:78
void dimension(unsigned rows, unsigned cols)
Definition: godleyTable.h:142
void _resize(unsigned rows, unsigned cols)
Definition: godleyTable.h:49
std::string savedText
save text in currently highlighted column heading for renaming all variable instances and to enable u...
Definition: godleyTable.h:177
void exportToCSV(std::ostream &s, const GodleyTable &g)
Definition: godleyExport.cc:61
static const char * initialConditions
Definition: godleyTable.h:62
bool signConventionReversed(int col) const
The usual mathematical sign convention is reversed in double entry book keeping conventions if the as...
Definition: godleyTable.h:94
std::vector< std::vector< string > > Data
Definition: godleyTable.h:41
bool operator==(const GodleyTable &other) const
Definition: godleyTable.h:71