-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmodel.js
112 lines (87 loc) · 2.73 KB
/
model.js
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
/*
model.js
Model Class
read a from model.xml
store model in string etc
model.xml example:
<modelleertaal>
<startwaarden>
Fmotor = 500 'N
m = 800 'kg
dt = 1e-2 's
v = 0'm/s
s = 0 'm/s
t = 0 's
</startwaarden>
<modelregels>
Fres= Fmotor
a = Fres/m
dv = a * dt
v = v + dv
ds = v * dt
s = s + ds
t = t + dt
als (0)
dan
Stop
EindAls
</modelregels>
</modelleertaal>
*/
//jshint es3:true
var fs = require('fs');
function Model() {
this.modelregels = '';
this.startwaarden = '';
}
Model.prototype.readBogusXMLFile = function(filename) {
// This read a "bogus" XML file that still includes < instead of <
var buf = fs.readFileSync(filename, "utf8");
this.parseBogusXMLString(buf);
};
Model.prototype.parseBogusXMLString = function(xmlString) {
var action = 0; // 0 = do nothing, 1 = modelregels, 2 = startwaarden
var equationRe = /([A-Za-z])[ ]*\=[ ]*(\d*)/g; // Match N = 1000
this.startwaarden = '';
this.modelregels = '';
var lines = xmlString.split('\n');
for(var line = 1; line < lines.length; line++) {
//console.log(action, lines[line]);
// try to extra N = ... from model
if (action === 0) {
var matches = equationRe.exec(lines[line]);
if (matches !== null) {
if (matches[1] == 'N')
this.N = parseInt(matches[2], 10);
if (this.debug) console.log('Found N = '+this.N+' in model.xml');
}
}
switch(lines[line].replace('\r','')) {
// < and > mess things up in the browser
case '<modelregels>': { action = 1; continue; }
case '</modelregels>': { action = 0; continue; }
case '<startwaarden>': { action = 2; continue; }
case '</startwaarden>': { action = 0; continue; }
}
if (action==1) this.modelregels += lines[line]+'\n';
if (action==2) this.startwaarden += lines[line]+'\n';
}
//console.log('DEBUG: in model.js parseBogusXMLString endresult this.modelregels:');
//console.log(this.modelregels);
//console.log('DEBUG: in model.js parseBogusXMLString endresult this.startwaarden:');
//console.log(this.startwaarden);
};
Model.prototype.createBogusXMLString = function() {
// TODO replace this with a function that exports all options.
var Nstring = '';
if (this.N !== undefined)
Nstring = 'N = ' + this.N + '\n';
return '<modelleertaal>\n' +
Nstring +
'<startwaarden>\n' +
this.startwaarden +
'\n</startwaarden>\n<modelregels>\n' +
this.modelregels +
'\n</modelregels>\n</modelleertaal>\n';
};
exports.Model = Model;