forked from highperformancecoder/minsky
-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema2.h
225 lines (195 loc) · 6.85 KB
/
schema2.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
@copyright Steve Keen 2017
@author Russell Standish
This file is part of Minsky.
Minsky 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.
Minsky 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 Minsky. If not, see <http://www.gnu.org/licenses/>.
*/
/**
@file schema 2 is a defined and published Minsky schema. It is
expected to grow over time, deprecated attributes are also allowed,
but any renamed attributes require bumping the schema number.
*/
#ifndef SCHEMA_2_H
#define SCHEMA_2_H
#include "model/minsky.h"
#include "model/ravelWrap.h"
#include "model/sheet.h"
#include "schema/schema1.h"
#include "schemaHelper.h"
#include "classdesc.h"
#include "plotOptions.h"
#include "polyXMLBase.h"
#include "polyJsonBase.h"
#include "simulation.h"
#include "ravelState-schema2.h"
#include "optional.h"
#include <xsd_generate_base.h>
#include <vector>
#include <string>
namespace schema2
{
using minsky::SchemaHelper;
using namespace std;
using namespace classdesc;
using classdesc::shared_ptr;
using minsky::Optional;
/// unpack a TensorVal from a pack_t buffer. Schema2 encoding.
void unpack(classdesc::pack_t&, civita::TensorVal&);
struct Note
{
Optional<std::string> detailedText, tooltip;
Note() {}
Note(const minsky::NoteBase& x): detailedText(x.detailedText()), tooltip(x.tooltip()) {}
Note(const schema1::Item& x): detailedText(x.detailedText), tooltip(x.tooltip) {}
};
// attribute common to all items
struct ItemBase: public minsky::PlotOptions<Note>
{
int id=-1;
std::string type;
float x=0, y=0; ///< position in canvas, or within group
float zoomFactor=1;
double rotation=0; ///< rotation of icon, in degrees
std::vector<int> ports;
ItemBase() {}
ItemBase(int id, const minsky::Item& it, const std::vector<int>& ports):
minsky::PlotOptions<Note>(Note(it)), id(id), type(it.classType()),
x(it.m_x), y(it.m_y), zoomFactor(it.zoomFactor()), rotation(it.rotation()),
ports(ports) {}
ItemBase(const schema1::Item& it, const std::string& type="Item"):
PlotOptions<Note>(it), id(it.id), type(type) {}
};
struct Slider
{
bool visible=true, stepRel=false;
double min, max, step;
Slider() {}
Slider(bool v, bool stepRel, double min, double max, double step):
visible(v), stepRel(stepRel), min(min), max(max), step(step) {}
};
struct Item: public ItemBase
{
Optional<float> width, height;
Optional<std::string> name; //name, description or title
Optional<std::string> init;
Optional<std::string> units;
Optional<Slider> slider;
Optional<int> intVar;
Optional<std::map<double,double>> dataOpData;
Optional<std::string> filename;
Optional<RavelState> ravelState;
Optional<int> lockGroup;
Optional<minsky::Dimensions> dimensions;
// Operation tensor parameters
Optional<std::string> axis;
Optional<double> arg;
// Godley Icon specific fields
Optional<std::vector<std::vector<std::string>>> data;
Optional<std::vector<minsky::GodleyAssetClass::AssetClass>> assetClasses;
Optional<float> iconScale; // for handling legacy schemas
// group specific fields
Optional<std::vector<minsky::Bookmark>> bookmarks;
Optional<classdesc::CDATA> tensorData; // used for saving tensor data attached to parameters
Optional<std::vector<ecolab::Plot::LineStyle>> palette;
Item() {}
// minsky object importers
Item(const schema1::Item& it): ItemBase(it) {}
// schema1 importers
Item(const schema1::Operation& it):
ItemBase(it, "Operation:"+minsky::OperationType::typeName(it.type)),
name(it.name), dataOpData(it.data) {
if (it.intVar>-1) intVar=it.intVar;
ports=it.ports;
// rewrite deprecated constants as variables
if (it.type==minsky::OperationType::constant)
{
type="Variable:constant";
init.reset(new std::string(std::to_string(it.value)));
}
}
Item(const schema1::Variable& it):
ItemBase(it, "Variable:"+minsky::VariableType::typeName(it.type)),
name(it.name), init(it.init) {ports=it.ports;}
Item(const schema1::Godley& it);
Item(const schema1::Plot& it):
ItemBase(it, "PlotWidget"), name(it.title)
{
logx=it.logx;
logy=it.logy;
xlabel=it.xlabel;
ylabel=it.ylabel;
y1label=it.y1label;
if (it.legend) legend=*it.legend;
ports=it.ports;
}
Item(const schema1::Group& it): ItemBase(it,"Group"), name(it.name) {}
Item(const schema1::Switch& it): ItemBase(it,"SwitchIcon") {ports=it.ports;}
void addLayout(const schema1::UnionLayout& layout) {
x=layout.x;
y=layout.y;
rotation=layout.rotation;
if (layout.width>=0)
width.reset(new float(layout.width));
else if (type=="PlotWidget")
width.reset(new float(150));
if (layout.height>=0)
height.reset(new float(layout.height));
else if (type=="PlotWidget")
height.reset(new float(150));
if (layout.sliderBoundsSet)
slider.reset(new Slider(layout.sliderVisible,layout.sliderStepRel,
layout.sliderMin,layout.sliderMax,layout.sliderStep));
}
};
struct Wire: public Note
{
int id=-1;
int from=-1, to=-1;
Optional<std::vector<float>> coords;
Wire() {}
Wire(const schema1::Wire& w): Note(w), id(w.id), from(w.from), to(w.to) {}
void addLayout(const schema1::UnionLayout& layout) {
if (layout.coords.size()>4)
coords.reset(new std::vector<float>(layout.coords));
}
};
struct Group: public Item
{
vector<int> items;
Optional<vector<int>> inVariables, outVariables;
Group() {}
/// note this assumes that ids have been uniquified prior to this call
Group(const schema1::Group& g):
Item(g), items(g.items) {}
};
struct Minsky
{
static const int version=2;
int schemaVersion=Minsky::version;
vector<Wire> wires;
vector<Item> items;
vector<Group> groups;
minsky::Simulation rungeKutta;
double zoomFactor=1;
vector<minsky::Bookmark> bookmarks;
minsky::Dimensions dimensions;
minsky::ConversionsMap conversions;
Minsky(): schemaVersion(0) {} // schemaVersion defined on read in
Minsky(const schema1::Minsky& m);
/// populate schema from XML data
Minsky(classdesc::xml_unpack_t& data): schemaVersion(0)
{minsky::loadSchema<schema1::Minsky>(*this,data,"Minsky");}
};
}
#include "schema2.cd"
#include "schema2.xcd"
#endif