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
|
package org.uscxml;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable;
public class ECMAScriptDataModel extends JavaDataModel {
protected Context ctx;
protected Scriptable scope;
@Override
public JavaDataModel create(Interpreter interpreter) {
/**
* Called when an SCXML interpreter wants an instance of this datamodel
* Be careful to instantiate attributes of instance returned and not *this*
*/
System.out.println("create");
ECMAScriptDataModel newDM = new ECMAScriptDataModel();
newDM.ctx = Context.enter();
try {
newDM.scope = newDM.ctx.initStandardObjects();
} catch(Exception e) {
System.err.println(e);
}
return newDM;
}
@Override
public StringSet getNames() {
/**
* Register with the following names for the datamodel attribute
* at the scxml element. <scxml datamodel="one of these">
*/
System.out.println("getNames");
StringSet ss = new StringSet();
ss.insert("ecmascript");
return ss;
}
@Override
public boolean validate(String location, String schema) {
/**
* Validate the datamodel. This make more sense for XML datamodels
* and is pretty much unused but required as per draft.
*/
System.out.println("validate " + location + " " + schema);
return true;
}
@Override
public void setEvent(Event event) {
/**
* Make the current event available as the variable _event
* in the datamodel.
*/
System.out.println("setEvent " + event);
}
@Override
public DataNative getStringAsData(String content) {
/**
* Evaluate the string as a value expression and
* transform it into a JSON-like Data structure
*/
System.out.println("getStringAsData " + content);
Data data = new Data();
// TODO: populate data object
return Data.toNative(data);
}
@Override
public long getLength(String expr) {
/**
* Return the length of the expression if it were an
* array, used by foreach element.
*/
System.out.println("getLength " + expr);
Object result = ctx.evaluateString(scope, expr, "uscxml", 1, null);
return (long) Context.toNumber(result);
}
@Override
public void setForeach(String item, String array, String index, long iteration) {
/**
* Prepare an iteration of the foreach element, by setting the variable in index
* to the current iteration and setting the variable in item to the current item
* from array.
*/
System.out.println("setForeach " + item + " " + index + " " + iteration);
}
@Override
public void eval(String scriptElem, String expr) {
/**
* Evaluate the given expression in the datamodel.
* This is used foremost with script elements.
*/
System.out.println("eval " + scriptElem + " " + expr);
ctx.evaluateString(scope, expr, "uscxml", 1, null);
}
@Override
public String evalAsString(String expr) {
/**
* Evaluate the expression as a string e.g. for the log element.
*/
System.out.println("evalAsString " + expr);
Object result = ctx.evaluateString(scope, expr, "uscxml", 1, null);
return Context.toString(result);
}
@Override
public boolean evalAsBool(String expr) {
/**
* Evaluate the expression as a boolean for cond attributes in
* if and transition elements.
*/
System.out.println("evalAsBool " + expr);
Object result = ctx.evaluateString(scope, expr, "uscxml", 1, null);
return Context.toBoolean(result);
}
@Override
public boolean isDeclared(String expr) {
/**
* The interpreter is supposed to raise an error if we assign
* to an undeclared variable. This method is used to check whether
* a location from assign is declared.
*/
System.out.println("isDeclared " + expr);
Object x = scope.get(expr, scope);
return x != Scriptable.NOT_FOUND;
}
@Override
public void init(String dataElem, String location, String content) {
/**
* Called when we pass data elements.
*/
System.out.println("init " + dataElem + " " + location + " " + content);
String expr = location + "=" + content;
ctx.evaluateString(scope, expr, "uscxml", 1, null);
}
@Override
public void assign(String assignElem, String location, String content) {
/**
* Called when we evaluate assign elements
*/
System.out.println("assign " + assignElem + " " + location + " " + content);
String expr = location + "=" + content;
ctx.evaluateString(scope, expr, "uscxml", 1, null);
}
}
|