Minsky
schemaHelper.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 
20 #ifndef SCHEMA_HELPER
21 #define SCHEMA_HELPER
22 
23 #include "operation.h"
24 #include "variable.h"
25 #include "godleyTable.h"
26 #include "godleyIcon.h"
27 #include "ravelWrap.h"
28 #include "variableValue.h"
29 #include <xml_unpack_base.h>
30 
31 template <class T>
32 ecolab::array<T> toArray(const std::vector<T>& v)
33 {
34  ecolab::array<T> a(v.size());
35  for (std::size_t i=0; i<v.size(); ++i) a[i]=v[i];
36  return a;
37 }
38 
39 template <class T>
40 std::vector<T> toVector(const ecolab::array<T>& a)
41 {
42  std::vector<T> v(a.size());
43  for (std::size_t i=0; i<v.size(); ++i) v[i]=a[i];
44  return v;
45 }
46 
47 template <class T>
48 std::vector<double> toDoubleVector(const ecolab::array<T>& a)
49 {
50  std::vector<double> v(a.size());
51  for (std::size_t i=0; i<v.size(); ++i) v[i]=a[i];
52  return v;
53 }
54 
55 namespace minsky
56 {
64  struct SchemaHelper
65  {
66  static void setPrivates
67  (minsky::GodleyTable& g, const vector<vector<string> >& data,
68  const vector<GodleyTable::AssetClass>& assetClass)
69  {
70  g.data=data;
71  g.m_assetClass=assetClass;
72  }
73  static void setPrivates
74  (minsky::GodleyIcon& g, const vector<vector<string> >& data,
75  const vector<GodleyTable::AssetClass>& assetClass)
76  {
77  setPrivates(g.table, data, assetClass);
78  }
79 
80  static void setVariableDisplay(minsky::GodleyIcon& g, bool variableDisplay)
81  {
82  g.m_variableDisplay=variableDisplay;
83  }
84 
86  const minsky::GodleyIcon::Variables& flowVars,
87  const minsky::GodleyIcon::Variables& stockVars)
88  {
89  g.m_flowVars=flowVars;
90  g.m_stockVars=stockVars;
91  }
92 
93  static void initHandleState(minsky::Ravel& r, const ravel::RavelState& s)
94  {r.initState=s;}
95 
96  };
97 
98  template <class PreviousSchema, class CurrentSchema>
99  void loadSchema(CurrentSchema& currentSchema,
100  classdesc::xml_unpack_t& data, const std::string& rootElement)
101  {
102  xml_unpack(data, rootElement, currentSchema);
103  if (currentSchema.schemaVersion < currentSchema.version)
104  {
105  PreviousSchema prevSchema(data);
106  currentSchema=prevSchema;
107  }
108  else if (currentSchema.schemaVersion > currentSchema.version)
109  throw error("Minsky schema version %d not supported",currentSchema.schemaVersion);
110  }
111 
113  classdesc::pack_t decode(const classdesc::CDATA&);
115  classdesc::CDATA encode(const classdesc::pack_t&);
116 
117 
118 }
119 
120 #endif
void xml_unpack(classdesc::xml_unpack_t &, const classdesc::string &, classdesc::ref< ecolab::urand > &)
ecolab::array< T > toArray(const std::vector< T > &v)
Definition: schemaHelper.h:32
Variables m_flowVars
Definition: godleyIcon.h:176
classdesc::pack_t decode(const classdesc::CDATA &data)
decode ascii-encoded representation to binary data
Definition: schemaHelper.cc:34
static void initHandleState(minsky::Ravel &r, const ravel::RavelState &s)
Definition: schemaHelper.h:93
vector< AssetClass > m_assetClass
class of each column (used in DE compliant mode)
Definition: godleyTable.h:45
ravel::RavelState initState
used entirely to defer persisted state data until after first load from a variable ...
Definition: ravelWrap.h:73
GodleyTable table
table data. Must be declared before editor
Definition: godleyIcon.h:80
std::vector< double > toDoubleVector(const ecolab::array< T > &a)
Definition: schemaHelper.h:48
Creation and access to the minskyTCL_obj object, which has code to record whenever Minsky&#39;s state cha...
Definition: constMap.h:22
CDATA encode(const pack_t &buf)
Definition: schemaHelper.cc:51
static void setStockAndFlow(minsky::GodleyIcon &g, const minsky::GodleyIcon::Variables &flowVars, const minsky::GodleyIcon::Variables &stockVars)
Definition: schemaHelper.h:85
void loadSchema(CurrentSchema &currentSchema, classdesc::xml_unpack_t &data, const std::string &rootElement)
Definition: schemaHelper.h:99
Variables m_stockVars
Definition: godleyIcon.h:176
std::vector< VariablePtr > Variables
Definition: godleyIcon.h:111
static void setVariableDisplay(minsky::GodleyIcon &g, bool variableDisplay)
Definition: schemaHelper.h:80
static void setPrivates(minsky::GodleyTable &g, const vector< vector< string > > &data, const vector< GodleyTable::AssetClass > &assetClass)
Definition: schemaHelper.h:67
std::vector< T > toVector(const ecolab::array< T > &a)
Definition: schemaHelper.h:40