This repository has been archived by the owner on Sep 25, 2018. It is now read-only.
/
annotate-scxml-json.coffee
476 lines (380 loc) · 13.8 KB
/
annotate-scxml-json.coffee
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
###
Copyright 2011-2012 Jacob Beard, INFICON, and other SCION contributors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
###
###
This file transforms an SCXML document converted to JsonML so that it is easier for a JavaScript-based SCXML interpreter to parse and interpret.
###
###
Example SCXML document basic1.scxml stripped of whitespace and converted to JsonML.
[
"scxml",
{
"id": "root",
"profile": "ecmascript",
"version": "1.0"
},
[
"initial",
{
"id": "intitial1"
},
[
"transition",
{
"target": "a"
}
]
],
[
"state",
{
"id": "a"
},
[
"transition",
{
"event": "t",
"target": "b"
}
]
],
[
"state",
{
"id": "b"
}
]
]
###
#constants
define ["scxml/state-kinds-enum"],(stateKinds) ->
STATES_THAT_CAN_BE_CHILDREN = ["state", "parallel", "history", "final", "initial"]
STATE_TAGS = STATES_THAT_CAN_BE_CHILDREN.concat "scxml"
#TODO: read from stdin, file, or allow transorm via API
#variable declarations outside of scope of transform
states = basicStates = uniqueEvents = transitions = idToStateMap = onFoundStateIdCallbacks = datamodel = undefined
transformAndSerialize = (root,genDepth,genAncestors,genDescendants,genLCA) ->
JSON.stringify transform(root,genDepth,genAncestors,genDescendants,genLCA)
#we expect depth to actually only be at most 100, so we can use regular recursion
transform = (root,genDepth,genAncestors,genDescendants,genLCA) ->
#initialize variables we want to track
states = []
basicStates = []
uniqueEvents = {}
transitions = []
idToStateMap = {}
onFoundStateIdCallbacks = []
datamodel = {}
rootState = transformStateNode root,[],genDepth,genAncestors,genDescendants,genLCA
[tagName,attributes,children] = deconstructNode root
#reverse ancestors (needs to be in reverse document order)
if genAncestors or genLCA then state.ancestors.reverse() for state in states
#reverse descendants (needs to be in reverse document order)
if genDescendants or genLCA then state.descendants.reverse() for state in states
#generate LCAs on transitions
if genLCA
for transition in transitions when transition.targets
source = idToStateMap[transition.source]
targets = (idToStateMap[target] for target in transition.targets)
if not source
#console.debug transition
new Error "source missing"
else if not targets.length
#console.debug transition
new Error "target missing"
transition.lca = getLCA(source,targets[0])
states : states
transitions : transitions
root : rootState.id
events : genEventsEnum uniqueEvents
scripts : genRootScripts children
profile : attributes.profile
version : attributes.version
datamodel : datamodel
genRootScripts = (rootChildren) ->
toReturn = []
for child in rootChildren
[tagName,attributes,grandChildren] = deconstructNode child
if tagName is "script"
for scriptNode in grandChildren when typeof scriptNode is "string"
toReturn.push scriptNode
return toReturn
genEventsEnum = (uniqueEvents) ->
eventDocumentOrder = 0
toReturn = {}
for event of uniqueEvents
toReturn[event] =
name : event
documentOrder : eventDocumentOrder++
return toReturn
deconstructNode = (node,filterText) ->
tagName = node[0]
n1 = node[1]
if n1 and typeof n1 is "object" and not (isArray n1 or typeof n1 is "string")
#process his attributes? do we care?
attributes = n1
children = node[2..]
else
attributes = {}
children = node[1..]
if filterText
children = (child for child in children when typeof child isnt "string")
return [tagName,attributes,children]
stripStarFromEventNameRe = /^([a-zA-Z0-9.]+)(\.\*)?$/
transformTransitionNode = (transitionNode,parentState,genDepth,genAncestors,genDescendants,genLCA) ->
[tagName,attributes,children] = deconstructNode transitionNode,true
#transform event attribute
#TODO: split up space-delimited events
#if attributes.event [all,attributes.event] = attributes.event.match stripStar
if attributes.event and attributes.event isnt "*" then uniqueEvents[attributes.event] = true #track unique events
transition =
documentOrder : transitions.length
id : transitions.length
source : parentState.id
cond : attributes.cond
event : attributes.event
actions : (transformActionNode child for child in children)
targets : attributes?.target?.trim().split(/\s+/) #this will either be a list, or undefined
transitions.push transition
#set up LCA later
return transition
processParam = (param) ->
[tagName,attributes,children] = deconstructNode param
{ name : attributes.name, expr : attributes.expr, location: attributes.location }
transformActionNode = (node) ->
[tagName,attributes,children] = deconstructNode node
switch tagName
when "if"
"type" : "if"
"cond" : attributes.cond
"actions" : (transformActionNode child for child in children)
when "elseif"
"type" : "elseif"
"cond" : attributes.cond
"actions" : (transformActionNode child for child in children)
when "else"
"type" : "else"
"actions" : (transformActionNode child for child in children)
when "log"
"type" : "log"
"expr" : attributes.expr
"label" : attributes.label
when "script"
"type" : "script",
"script" : children.join "\n"
when "send"
"type" : "send",
"sendType" : attributes.type
"delay" : attributes.delay
"id" : attributes.id
"event" : attributes.event
"target" : attributes.target
"idlocation" : attributes.idlocation
#data
"namelist" : attributes?.namelist?.trim().split(new RegExp " +")
"params" : (processParam child for child in children when child[0] is "param")
"content" : (deconstructNode(child)[2][0] for child in children when child[0] is "content")[0]
#exprs
"eventexpr" : attributes.eventexpr
"targetexpr" : attributes.targetexpr
"typeexpr" : attributes.typeexpr
"delayexpr" : attributes.delayexpr
when "cancel"
"type" : "cancel",
"sendid" : attributes.sendid
when "assign"
"type" : "assign",
"location" : attributes.location
"expr" : attributes.expr
when "raise"
"type" : "raise"
"event" : attributes.event
when "invoke"
#TODO
throw new Error("Element #{tagName} not yet supported")
when "finalize"
#TODO
throw new Error("Element #{tagName} not yet supported")
transformDatamodel = (node,ancestors,genDepth,genAncestors,genDescendants,genLCA) ->
[tagName,attributes,children] = deconstructNode node,true
for child in children when child[0] is "data"
[childTagName,childAttributes,childChildren] = deconstructNode child,true
if childAttributes.id
datamodel[childAttributes.id] = childAttributes.expr or null
transformStateNode = (node,ancestors,genDepth,genAncestors,genDescendants,genLCA) ->
[tagName,attributes,children] = deconstructNode node,true
#generate id if necessary
id = attributes?.id or genId tagName
#console.warn "Processing #{tagName} node with id '#{id}'"
#console.warn "ancestors",ancestors
kind = switch tagName
when "state"
if (child for child in children when child[0] in STATE_TAGS).length then stateKinds.COMPOSITE
else stateKinds.BASIC
when "scxml" then stateKinds.COMPOSITE
when "initial" then stateKinds.INITIAL
when "parallel" then stateKinds.PARALLEL
when "final" then stateKinds.FINAL
when "history" then stateKinds.HISTORY
#stub out the state
state =
id : id
kind : kind
descendants : [] #descendants gets populated later
idToStateMap[id] = state
if ancestors.length
state.parent = ancestors[ancestors.length - 1]
if kind is stateKinds.HISTORY
state.isDeep = if attributes.type is "deep" then true else false
state.documentOrder = states.length
states.push state
if kind is stateKinds.BASIC or kind is stateKinds.INITIAL or kind is stateKinds.HISTORY
state.basicDocumentOrder = basicStates.length
basicStates.push state
#special stuff
if genDepth
state.depth = ancestors.length
if genAncestors or genLCA
state.ancestors = ancestors.slice()
if genDescendants or genLCA
#walk back up ancestors and add this state to lists of descendants
(idToStateMap[anc].descendants.push state.id for anc in ancestors)
#need to do some work on his children
onExitChildren = []
onEntryChildren = []
transitionChildren = []
stateChildren = []
nextAncestors = ancestors.concat state.id #we are copying ancestors twice. may be more efficient way?
processedInitial = false
firstStateChild = null
processInitialState = (initialState) ->
child = transformStateNode initialState,nextAncestors,genDepth,genAncestors,genDescendants,genLCA
state.initial = child.id
stateChildren.push child
processedInitial = true
for child in children when isArray child #they should all be tuples. too bad this is expensive.
[childTagName,childAttributes,childChildren] = deconstructNode child,true
switch childTagName
#apply recursively
when "transition"
transitionChildren.push transformTransitionNode child,state
when "onentry"
(onEntryChildren.push transformActionNode actionNode for actionNode in childChildren)
when "onexit"
(onExitChildren.push transformActionNode actionNode for actionNode in childChildren)
when "initial"
if not processedInitial
processInitialState child
else
throw new Error("Encountered duplicate initial states in state #{state.id}")
when "history"
child = transformStateNode child,nextAncestors,genDepth,genAncestors,genDescendants,genLCA
state.history = child.id
stateChildren.push child
when "datamodel"
transformDatamodel child,nextAncestors,genDepth,genAncestors,genDescendants,genLCA
else
if childTagName in STATES_THAT_CAN_BE_CHILDREN #another filter
transformedStateNode = transformStateNode child,nextAncestors,genDepth,genAncestors,genDescendants,genLCA
if firstStateChild is null
#this is used to set default initial state, if initial state is not specified
firstStateChild = transformedStateNode
stateChildren.push transformedStateNode
#console.error state.id,firstStateChild,processedInitial
#handle initial
#FIXME: handle initial state for parallel states
if not processedInitial and tagName isnt "parallel"
hasInitialAttribute = attributes?.initial
generateFakeInitialState = (targetId) ->
#console.log "generating fake initial node"
#create a fake initial state and process him
fakeInitialState = [
"initial",
[
"transition",
{
target : targetId
}
]
]
processInitialState fakeInitialState
if hasInitialAttribute
generateFakeInitialState attributes.initial
else
if firstStateChild #if this exists, he is composite
generateFakeInitialState firstStateChild.id
#set up these properties
state.onexit = onExitChildren
state.onentry = onEntryChildren
state.transitions = (transition.documentOrder for transition in transitionChildren)
state.children = (child.id for child in stateChildren)
#lazy-initialize transitions that need it
#if onFoundStateIdCallbacks[id] then (cb() for cb in onFoundStateIdCallbacks[id])
#return the state object. depending on specified optinos, some of these properties may be undefined, which is fine
return state
#http://stackoverflow.com/questions/4775722/javascript-check-if-object-is-array
isArray = (o) -> Object.prototype.toString.call(o) is '[object Array]'
#TODO: make this safer
idRoot = "$generated"
idCounter = {}
genId = (tagName) ->
idCounter[tagName] ?= 0
"#{idRoot}-#{tagName}-#{idCounter[tagName]++}"
#inspect = require('util').inspect
getLCA = (s1,s2) ->
#FIXME: ancestors will be complete at this point, but will descendants?
#yah, i think descandants may not be complete, but it will be complete enough
#lots of string comparison... expensive
#console.debug "getLCA"
#console.debug "s1",s1
#console.debug "s2",s2
###
process.stdout.setEncoding 'utf-8'
process.stdout.write "\ngetLCA\n"
process.stdout.write "\ns1\n"
process.stdout.write inspect(s1)
process.stdout.write "\ns2\n"
process.stdout.write inspect(s2)
###
commonAncestors = []
for a in s1.ancestors
anc = idToStateMap[a]
#console.debug "anc",anc
if s2.id in anc.descendants
commonAncestors.push a
if not commonAncestors.length
throw new Error("Could not find LCA for states.")
return commonAncestors[0]
#FIXME: work on this API
return (inFileOrObject,outFile) ->
if typeof inFileOrObject is "object" and isArray inFileOrObject
return transform inFileOrObject
else
go = (jsonStr) ->
scxmlJson = JSON.parse jsonStr
s = transformAndSerialize scxmlJson,true,true,true,true
if outFile is "-"
process.stdout.write s
else
fs = require 'fs'
fs.writeFileSync outFile,s,'utf-8'
if not inFileOrObject or inFileOrObject is "-"
process.stdin.resume()
process.stdin.setEncoding "utf-8"
#read from stdin
json = ""
process.stdin.on "data",(data) -> json += data
process.stdin.on "end", -> go json
else
fs = require 'fs'
str = fs.readFileSync inFileOrObject,'utf-8'
go str