Minsky
godleyExport.cc
Go to the documentation of this file.
1 /*
2  @copyright Steve Keen 2016
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 
20 #include "pango.h"
21 #include "cairoItems.h"
22 #include "godleyExport.h"
23 #include "flowCoef.h"
24 #include "latexMarkup.h"
25 #include "group.h"
26 #include "selection.h"
27 #include "lasso.h"
28 #include "variableValue.h"
29 #include "minsky_epilogue.h"
30 
31 using namespace std;
32 using classdesc::enum_keysData;
33 
34 namespace minsky
35 {
36  namespace
37  {
38  string fcStr(const FlowCoef& fc)
39  {
40  auto nm=uqName(fc.name);
41  if (fc.coef==1)
42  return nm;
43  if (fc.coef==-1)
44  return "-"+nm;
45  if (fc.coef==0)
46  return "";
47  return str(fc.coef)+nm;
48  }
49 
50  // trim enclosing <i> tags
51  string trim(string x)
52  {
53  if (x.substr(0,3)=="<i>")
54  x=x.substr(3);
55  if (x.length()>=4 && x.substr(x.length()-4)=="</i>")
56  return x.substr(0,x.length()-4);
57  return x;
58  }
59 }
60 
61  void exportToCSV(std::ostream& s, const GodleyTable& g)
62  {
63  s<<'"'<<g.getCell(0,0)<<'"';
64  for (unsigned i=1; i<g.cols(); ++i)
65  s<<",\""<<trim(latexToPango(uqName(g.getCell(0,i))))<<'"';
66  s<<'\n';
68  {
69  s<<"Asset Class";
70  for (unsigned i=1; i<g.cols(); ++i)
71  s<<","<<enum_keysData<GodleyAssetClass::AssetClass>::
72  keysData[g._assetClass(i)].name;
73  s<<"\n";
74  }
75  for (unsigned r=1; r<g.rows(); ++r)
76  {
77  s<<g.getCell(r,0);
78  for (unsigned c=1; c<g.cols(); ++c)
79  s<<",\""<<trim(latexToPango(fcStr(FlowCoef(g.getCell(r,c)))))<<'"';
80  s<<'\n';
81  }
82  }
83 
84  void exportToLaTeX(std::ostream& f, const GodleyTable& g)
85  {
86  f<<"\\documentclass{article}\n\\begin{document}\n";
87  f<<"\\begin{tabular}{|c|";
88  for (unsigned i=1; i<g.cols(); ++i)
89  f<<'c';
90  f<<"|}\n\\hline\n";
91  f<<"Flows $\\downarrow$ / Stock Variables $\\rightarrow$";
92  for (unsigned i=1; i<g.cols(); ++i)
93  f<<"&\\multicolumn{1}{|c|}{$"<<uqName(g.getCell(0,i))<<"$}";
94 
95  // asset class descriptors
97  {
98  f<<"\\\\\\cline{2-"<<g.cols()<<"}Asset Class";
99  unsigned repeat=0;
100  GodleyAssetClass::AssetClass asset=GodleyAssetClass::noAssetClass;
101 
102  auto outputAC=[&]() {
103  if (repeat>0)
104  f<<"&\\multicolumn{"<<repeat<<"}{|c|}{"<<
105  enum_keysData<GodleyAssetClass::AssetClass>::
106  keysData[asset].name<<'}';
107  };
108 
109  for (unsigned i=1; i<g.cols(); ++i)
110  if (g._assetClass(i)!=asset)
111  {
112  outputAC();
113  asset=g._assetClass(i);
114  repeat=1;
115  }
116  else
117  repeat++;
118 
119  // now output last column
120  outputAC();
121  }
122 
123  f<<"\\\\\\hline\n";
124  for (unsigned r=1; r<g.rows(); ++r)
125  {
126  f<<g.getCell(r,0);
127  for (unsigned c=1; c<g.cols(); ++c)
128  f<<"&$"<<fcStr(FlowCoef(g.getCell(r,c)))<<'$';
129  f<<"\\\\\n";
130  }
131  f<<"\\hline\n\\end{tabular}\n";
132  f<<"\\end{document}\n";
133  }
134 }
std::size_t cols() const
Definition: godleyTable.h:115
function f
Definition: canvas.m:1
std::string latexToPango(const char *s)
Definition: latexMarkup.h:30
void exportToLaTeX(std::ostream &f, const GodleyTable &g)
Definition: godleyExport.cc:84
std::string name
Definition: flowCoef.h:30
STL namespace.
std::string uqName(const std::string &name)
extract unqualified portion of name
Definition: valueId.cc:135
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
std::string str(T x)
utility function to create a string representation of a numeric type
Definition: str.h:33
represents a numerical coefficient times a variable (a "flow")
Definition: flowCoef.h:27
const vector< AssetClass > & _assetClass() const
class of each column (used in DE compliant mode)
Definition: godleyTable.h:78
void exportToCSV(std::ostream &s, const GodleyTable &g)
Definition: godleyExport.cc:61