Minsky
schema1.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 
25 #ifndef SCHEMA_1_H
26 #define SCHEMA_1_H
27 
28 #include "model/minsky.h"
29 #include "schema/schema0.h"
30 #include "schemaHelper.h"
31 #include "classdesc.h"
32 #include "polyXMLBase.h"
33 #include "polyJsonBase.h"
34 #include "plot.xcd"
35 #include "simulation.h"
36 
37 #include <xsd_generate_base.h>
38 #include <vector>
39 #include <string>
40 
41 // Note the explicit declaration of default assignment operators is
42 // required to disable warnings about virtual move assigns. See ticket
43 // #461.
44 
45 namespace schema1
46 {
47 
49  using namespace std;
50  using namespace classdesc;
51  using classdesc::shared_ptr;
52 
53  // refine poly templates for current usage
54  struct SPolyBase:
55  virtual public PolyBase<string>,
56  virtual public PolyJsonBase,
57  virtual public PolyXMLBase
58  {
59  };
60 
61  template <class T, class B1, class B2=PolyBase<string> >
62  struct SPoly: virtual public B1, virtual public B2
63  {
64  SPoly& operator=(const SPoly&)=default;
65  // clone() has to return an SPoly* to satisfy covariance
66  SPoly* clone() const override {return new T(static_cast<const T&>(*this));}
67  string type() const override {return classdesc::typeName<T>();}
68 
69  void xml_pack(xml_pack_t& x, const string& d) const override
70  {::xml_pack(x,d,static_cast<const T&>(*this));}
71 
72  void xml_unpack(xml_unpack_t& x, const string& d) override
73  {::xml_unpack(x,d,static_cast<T&>(*this));}
74 
75  void json_pack(json_pack_t& x, const string& d) const override
76  {::json_pack(x,d,static_cast<const T&>(*this));}
77 
78  void json_unpack(json_unpack_t& x, const string& d) override
79  {::json_unpack(x,d,static_cast<T&>(*this));}
80  };
81 
82  template <class T, class U>
83  struct Join: virtual public T, virtual public U
84  {
85  Join& operator=(const Join&)=default;
86  Join* clone() const override {return new Join(*this);}
87  string type() const override {return "";}
88  void xml_pack(xml_pack_t& x, const string& d) const override {}
89  void xml_unpack(xml_unpack_t& x, const string& d) override {}
90  void json_pack(json_pack_t& x, const string& d) const override {}
91  void json_unpack(json_unpack_t& x, const string& d) override {}
92  };
93 
94  struct Item: public SPoly<Item, SPolyBase>
95  {
96  int id;
98  string detailedText, tooltip;
99  Item(int id=-1): id(id) {}
100  template <class T>
101  Item(int id, const T& it):
102  id(id), detailedText(it.detailedText), tooltip(it.tooltip) {}
103  // just because Operation and Variable overload "type"
104  string typeName() const {return type();}
105  };
106 
107 
108  struct Port: public SPoly<Port,Item>
109  {
110  bool input;
111  Port(): input(false) {}
112  };
113 
114  struct Wire: public SPoly<Wire,Item>
115  {
116  int from, to;
117  Wire(): from(-1), to(-1) {}
118  Wire(int id, int from, int to): Item(id), from(from), to(to) {}
119  };
120 
121  struct Operation: public SPoly<Operation,Item>
122  {
124  double value;
125  vector<int> ports;
126  map<double,double> data; //for data operations
127  string name;
128  int intVar;
129  Operation(): type(minsky::OperationType::numOps), value(0) {}
130  Operation(int id, const schema0::Operation& op):
131  Item(id), type(op.m_type), value(op.value),
132  ports(op.m_ports),
133  name(op.m_description.empty()? op.description: op.m_description),
134  intVar(op.intVar) {}
135  };
136 
137  struct Variable: public SPoly<Variable,Item>
138  {
140  string init;
141  vector<int> ports;
142  string name;
143  Variable(): type(minsky::VariableType::undefined), init("0") {}
144  Variable(int id, const schema0::VariablePtr& v):
145  Item(id), type(v.m_type), init(v.init), ports{v.m_outPort,v.m_inPort},
146  name(v.name) {}
147  };
148 
149  // why is the schema1 qualifier needed here?
150  struct Plot: public SPoly<schema1::Plot,Item>
151  {
152  typedef ecolab::Plot::Side Side;
153  vector<int> ports;
154  shared_ptr<Side> legend;
155  bool logx{0}, logy{0};
156  string title, xlabel, ylabel, y1label;
157  Plot() {}
158  Plot(int id, const schema0::PlotWidget& p):
159  Item(id), ports(p.ports.begin(),p.ports.end()), logx(p.logx), logy(p.logy) {}
160  };
161 
162  struct Group: public SPoly<Group,Item>
163  {
164  //the following field is left commented out here to indicate this
165  //deprecated field is part of the version 1 spec
166  //vector<int> ports;
167  vector<int> items;
168  vector<int> ports;
169  vector<int> createdVars;
170  string name;
171  Group() {}
173  Group(int id, const schema0::GroupIcon& g):
174  Item(id), items(g.operations), ports(g.m_ports.begin(),g.m_ports.end()) {
175  items.insert(items.end(), g.variables.begin(), g.variables.end());
176  }
177  };
178 
179  struct Switch: public SPoly<Switch,Item>
180  {
181  vector<int> ports;
182  Switch() {}
183  };
184 
185  struct Godley: public SPoly<Godley,Item>
186  {
187  vector<int> ports;
188  bool doubleEntryCompliant=true;
189  string name;
190  vector<vector<string> > data;
191  vector<minsky::GodleyTable::AssetClass> assetClasses;
192  double zoomFactor=1;
193  Godley() {}
194  Godley(int id, const schema0::GodleyIcon& g):
195  Item(id),
196  doubleEntryCompliant(g.table.doubleEntryCompliant),
197  name(g.table.title), data(g.table.data),
198  assetClasses(g.table.m_assetClass)
199  {
200  for (auto& i: g.flowVars) ports.push_back(i.m_inPort);
201  for (auto& i: g.stockVars) ports.push_back(i.m_outPort);
202  }
203  };
204 
205  struct Layout: public SPoly<Layout, SPolyBase>
206  {
207  int id;
208  Layout(int id=-1): id(id) {}
209  virtual ~Layout() {}
210  };
211 
214  struct PositionLayout: public SPoly<PositionLayout, Layout>
215  {
216  double x=0, y=0;
217 
219  PositionLayout(int id, double x, double y): Layout(id), x(x), y(y) {}
220  template <class T> PositionLayout(int id, const T& item):
221  Layout(id), x(item.m_x), y(item.m_y) {}
223  Layout(id), x(o.x), y(o.y) {}
225  Layout(id), x(v.x), y(v.y) {}
227  Layout(id), x(g.x), y(g.y) {}
229  Layout(id), x(p.x), y(p.y) {}
230  };
231 
234  {
235  bool visible;
236  VisibilityLayout(bool visible=true): visible(visible) {}
237  template <class T> VisibilityLayout(const T& item):
238  visible(item.visible()) {}
240  visible(item.visible) {}
242  visible(item.visible) {}
244  visible(true) {}
245  };
246 
247  struct SizeLayout
248  {
249  double width=-1, height=-1; // -ve indicates schema file does not supply sizelayout info
251  template <class T>
252  SizeLayout(const T& x): width(x.width), height(x.height) {}
254  };
255 
257  struct WireLayout: public SPoly<WireLayout, Layout, VisibilityLayout>
258  {
259  vector<float> coords;
260 
262  WireLayout(int id, const schema0::Wire& wire):
263  Layout(id), VisibilityLayout(wire.visible),
264  coords(wire.coords.begin(),wire.coords.end()) {}
265  };
266 
268  struct ItemLayout: public SPoly<ItemLayout, Layout,
269  Join<PositionLayout, VisibilityLayout> >
270  {
271  double rotation=0;
272 
274  template <class T> ItemLayout(int id, const T& item):
275  Layout(id), PositionLayout(id, item), VisibilityLayout(item),
276  rotation(item.rotation) {}
277  };
278 
279 
281  struct GroupLayout: public SPoly<GroupLayout, ItemLayout, SizeLayout>
282  {
283  double displayZoom=1;
284  GroupLayout(): displayZoom(1) {}
285  GroupLayout(int id, const schema0::GroupIcon& g):
286  Layout(id), PositionLayout(id, g.x, g.y), ItemLayout(id, g), SizeLayout(g) {}
287  };
288 
289  struct PlotLayout: public SPoly<PlotLayout, PositionLayout, SizeLayout>
290  {
292  PlotLayout(int id, const schema0::PlotWidget& p):
293  Layout(id), PositionLayout(id, p), SizeLayout(p) {}
294  };
295 
297  struct SliderLayout: public SPoly<SliderLayout, ItemLayout>
298  {
299  bool sliderVisible=false, sliderBoundsSet=false, sliderStepRel=false;
300  double sliderMin=0, sliderMax=0, sliderStep=0;
302  template <class T>
303  SliderLayout(int id, const T& item):
304  Layout(id), PositionLayout(id, item), VisibilityLayout(item),
305  ItemLayout(id, item), sliderVisible(item.sliderVisible()),
306  sliderBoundsSet(item.sliderBoundsSet), sliderStepRel(item.sliderStepRel),
307  sliderMin(item.sliderMin), sliderMax(item.sliderMax),
308  sliderStep(item.sliderStep) {}
309  };
310 
313  struct UnionLayout: public SPoly<UnionLayout,
314  Join<SliderLayout, GroupLayout>,
315  Join<PlotLayout, WireLayout> >
316  {
318  UnionLayout(const Layout&);
319  };
320 
321  struct MinskyModel
322  {
323  //the following field is left commented out here to indicate this
324  //deprecated field is part of the version 1 spec
325  // vector<Port> ports;
326  vector<Wire> wires;
327  vector<Item> notes;
328  vector<Operation> operations;
329  vector<Variable> variables;
330  vector<Plot> plots;
331  vector<Group> groups;
332  vector<Switch> switches;
333  vector<Godley> godleys;
335  };
336 
337  struct Minsky
338  {
339  static const int version=1;
340  int schemaVersion=Minsky::version;
342  vector<shared_ptr<Layout> > layout;
343  double zoomFactor=1;
344  Minsky(): schemaVersion(-1) {} // schemaVersion defined on read in
345  Minsky(const schema0::Minsky& m);
346 
348  Minsky(classdesc::xml_unpack_t& data): schemaVersion(0)
349  {
350  minsky::loadSchema<schema0::Minsky>(*this,data,"Minsky");
351  removeIntVarOrphans();
352  }
353 
359  void removeIntVarOrphans();
360 
361 
362  };
363 
364  // Item and Layout factory
365  template <class T> std::unique_ptr<T> factory(const std::string&);
366 
367 }
368 
369 namespace classdesc
370 {
371  // we provide a specialisation here, to ensure our intended schema
372  // is as a "vector of Layouts"
373  template <> inline std::string typeName<shared_ptr<schema1::Layout> >()
374  {return "schema1::Layout";}
375 }
376 
377 namespace classdesc_access
378 {
379  namespace cd=classdesc;
380  template <> struct access_xml_pack<std::shared_ptr<schema1::Layout> >
381  {
382  template <class U>
383  void operator()(cd::xml_pack_t& x, const cd::string& d, U& a)
384  {a->xml_pack(x,d);}
385  };
386 
388  template <>struct access_xml_unpack<std::shared_ptr<schema1::Layout> >
389  {
390  template <class U>
391  void operator()(cd::xml_unpack_t& x, const cd::string& d, U& a)
392  {
393  a.reset(new schema1::UnionLayout);
394  ::xml_unpack(x, d, dynamic_cast<schema1::UnionLayout&>(*a));
395  }
396  };
397 
398 }
399 
401 
402 
403 #ifdef _CLASSDESC
404 #pragma omit xsd_generate schema1::SPolyBase
405 #pragma omit xsd_generate schema1::SPoly
406 #endif
407 
408 inline void xsd_generate(classdesc::xsd_generate_t&,const std::string&,const schema1::SPolyBase&) {}
409 template <class T, class B1, class B2>
410 void xsd_generate(classdesc::xsd_generate_t& x,const std::string& d,
411  const schema1::SPoly<T,B1,B2>& a)
412 {xsd_generate(x,d,static_cast<const B1&>(a));}
413 
414 // Layout is end of the line, no need to process further
415 inline void xsd_generate(classdesc::xsd_generate_t& x,const std::string& d,
417 {}
418 
419 
420 #include "schema1.cd"
421 #include "schema1.xcd"
422 #include "enumerateSchema1.h"
423 
424 
425 #endif
represent objects whose layouts just have a position (ports, plots, godleyIcons)
Definition: schema1.h:214
vector< int > ports
Definition: schema1.h:153
string typeName() const
Definition: schema1.h:104
void json_unpack(json_unpack_t &x, const string &d) override
Definition: schema1.h:78
void operator()(cd::xml_pack_t &x, const cd::string &d, U &a)
Definition: schema1.h:383
void xml_unpack(classdesc::xml_unpack_t &, const classdesc::string &, classdesc::ref< ecolab::urand > &)
vector< int > ports
Definition: schema1.h:168
Definition: input.py:1
describes item with sliders - currently just constants
Definition: schema1.h:297
vector< minsky::GodleyTable::AssetClass > assetClasses
Definition: schema1.h:191
vector< Plot > plots
Definition: schema1.h:330
shared_ptr< Side > legend
Definition: schema1.h:154
Operation(int id, const schema0::Operation &op)
Definition: schema1.h:130
ItemLayout(int id, const T &item)
Definition: schema1.h:274
vector< int > items
Definition: schema1.h:167
VisibilityLayout(const schema0::Operation &item)
Definition: schema1.h:239
vector< shared_ptr< Layout > > layout
Definition: schema1.h:342
Item(int id, const T &it)
Definition: schema1.h:101
VisibilityLayout(const schema0::GroupIcon &item)
Definition: schema1.h:243
Item(int id=-1)
Definition: schema1.h:99
STL namespace.
void xsd_generate(xsd_generate_t &g, const string &d, const minsky::Optional< T > &a)
Definition: optional.h:77
ecolab::Plot::Side Side
Definition: schema1.h:152
Wire(int id, int from, int to)
Definition: schema1.h:118
minsky::Simulation rungeKutta
Definition: schema1.h:334
PositionLayout(int id, const T &item)
Definition: schema1.h:220
void xml_unpack(xml_unpack_t &x, const string &d) override
Definition: schema1.h:89
WireLayout(int id, const schema0::Wire &wire)
Definition: schema1.h:262
void json_unpack(json_unpack_t &x, const string &d) override
Definition: schema1.h:91
represents layouts of objects like variables and operators
Definition: schema1.h:268
minsky::OperationType::Type type
Definition: schema1.h:123
PositionLayout(int id, const schema0::Operation &o)
Definition: schema1.h:222
map< double, double > data
Definition: schema1.h:126
represents layouts of wires
Definition: schema1.h:257
string type() const override
Definition: schema1.h:67
SPoly * clone() const override
Definition: schema1.h:66
vector< Godley > godleys
Definition: schema1.h:333
void xml_pack(xml_pack_t &x, const string &d) const override
Definition: schema1.h:88
group layouts also have a width & height
Definition: schema1.h:281
Variable(int id, const schema0::VariablePtr &v)
Definition: schema1.h:144
MinskyModel model
Definition: schema1.h:341
struct TCLcmd::trap::init_t init
structure representing a union of all of the above Layout classes, for xml_unpack ...
Definition: schema1.h:313
void operator()(cd::xml_unpack_t &x, const cd::string &d, U &a)
Definition: schema1.h:391
Creation and access to the minskyTCL_obj object, which has code to record whenever Minsky&#39;s state cha...
Definition: constMap.h:22
string type() const override
Definition: schema1.h:87
PositionLayout(int id, const schema0::Variable &v)
Definition: schema1.h:224
static const int version
Definition: schema1.h:339
Group(int id, const schema0::GroupIcon &g)
note this assumes that ids have been uniquified prior to this call
Definition: schema1.h:173
Layout(int id=-1)
Definition: schema1.h:208
virtual ~Layout()
Definition: schema1.h:209
string ylabel
Definition: schema1.h:156
PositionLayout(int id, double x, double y)
Definition: schema1.h:219
vector< Group > groups
Definition: schema1.h:331
vector< Operation > operations
Definition: schema1.h:328
VisibilityLayout(const T &item)
Definition: schema1.h:237
SizeLayout(const schema0::PlotWidget &)
Definition: schema1.h:253
bool input
Definition: schema1.h:110
PlotLayout(int id, const schema0::PlotWidget &p)
Definition: schema1.h:292
PositionLayout(int id, const schema0::PlotWidget &p)
Definition: schema1.h:228
std::unique_ptr< T > factory(const std::string &)
Godley(int id, const schema0::GodleyIcon &g)
Definition: schema1.h:194
int m_inPort
where wires connect to
Definition: schema0.h:92
Plot(int id, const schema0::PlotWidget &p)
Definition: schema1.h:158
PositionLayout(int id, const schema0::GroupIcon &g)
Definition: schema1.h:226
string tooltip
Definition: schema1.h:98
SizeLayout(const T &x)
Definition: schema1.h:252
vector< int > ports
Definition: schema1.h:187
void xsd_generate(classdesc::xsd_generate_t &, const std::string &, const schema1::SPolyBase &)
Definition: schema1.h:408
vector< int > ports
Definition: schema1.h:181
vector< Variable > variables
Definition: schema1.h:329
void xml_pack(xml_pack_t &t, const string &d, minsky::Optional< T > &a)
Definition: optional.h:84
void xml_unpack(xml_unpack_t &x, const string &d) override
Definition: schema1.h:72
std::vector< VariablePtr > stockVars
Definition: schema0.h:142
Minsky(classdesc::xml_unpack_t &data)
populate schema from XML data
Definition: schema1.h:348
string name
Definition: schema1.h:170
vector< int > createdVars
Definition: schema1.h:169
std::vector< VariablePtr > flowVars
Definition: schema0.h:142
vector< vector< string > > data
Definition: schema1.h:190
GroupLayout(int id, const schema0::GroupIcon &g)
Definition: schema1.h:285
represents items with a visibility attribute
Definition: schema1.h:233
vector< Item > notes
descriptive notes
Definition: schema1.h:327
VisibilityLayout(const schema0::Variable &item)
Definition: schema1.h:241
vector< Wire > wires
Definition: schema1.h:326
vector< Switch > switches
Definition: schema1.h:332
vector< int > ports
Definition: schema1.h:141
string name
Definition: schema1.h:189
void json_pack(json_pack_t &x, const string &d) const override
Definition: schema1.h:75
void json_pack(json_pack_t &x, const string &d) const override
Definition: schema1.h:90
SliderLayout(int id, const T &item)
Definition: schema1.h:303
VisibilityLayout(bool visible=true)
Definition: schema1.h:236
vector< float > coords
Definition: schema1.h:259
minsky::VariableType::Type type
Definition: schema1.h:139
Join * clone() const override
Definition: schema1.h:86
void xml_pack(xml_pack_t &x, const string &d) const override
Definition: schema1.h:69
std::vector< int > variables
Definition: schema0.h:152
vector< int > ports
Definition: schema1.h:125