-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
325 lines (306 loc) · 11.4 KB
/
index.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
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
// TODO : conversion format to gml -> yed : NO, ugly
// gml :
// Creator "yFiles"
// Version "2.14"
// graph
// [
// hierarchic 1
// label ""
// directed 1
// node
// [
// id 9
// label "ControlState"
// gid 8
// ]
// ...
// edge
// [
// source 2
// target 0
// label "Fetch result [ step Teams] / Display screen"
// ]
// TODO :respect indentation...
// for yed, I will have to do something to indicate H states (not really), but INIT state for sure (for now just put
// I)
import { arrayTreeLenses, breadthFirstTraverseTree } from "fp-rosetree"
import dagre from 'cytoscape-dagre';
const INIT = "init";
const HISTORY = 'history';
const TO_SELF = 'to_self';
const STANDARD = 'standard';
const INITIAL_STATE_NAME = 'nok';
const style = `
node[label = '${INIT}'] {
background-color: black;
height: 5px;
width: 5px;
}
node[label != '${INIT}'] {
content: data(label);
text-valign: center;
text-halign: center;
shape: roundrectangle;
width: label;
height: label;
padding-left: 5px;
padding-right: 5px;
padding-top: 5px;
padding-bottom: 5px;
background-color: white;
border-width: 1px;
border-color: black;
font-size: 10px;
font-family: Helvetica Neue;
}
node:active {
overlay-color: black;
overlay-padding: 0;
overlay-opacity: 0.1;
}
$node > node {
padding-top: 1px;
padding-left: 10px;
padding-bottom: 10px;
padding-right: 10px;
text-valign: top;
text-halign: center;
border-width: 1px;
border-color: black;
background-color: white;
}
edge {
width: 1px;
target-arrow-shape: triangle;
label: data(label);
font-size: 5px;
font-weight: bold;
text-background-color: #fff;
text-background-padding: 3px;
line-color: black;
target-arrow-color: black;
z-index: 100;
text-wrap: wrap;
text-background-color: white;
text-background-opacity: 1;
target-distance-from-node: 1px;
}
edge[type = '${TO_SELF}'] {
curve-style: bezier;
loop-direction: -20deg;
loop-sweep: -70deg;
}
edge[type = '${STANDARD}'] {
curve-style: bezier;
width: 1px;
target-distance-from-node: 5px;
}
edge[type = '${HISTORY}'] {
curve-style: bezier;
}
edge[type = '${INIT}'] {
curve-style: segments;
edge-distances: node-position;
segment-weights: 0.15 0.85;
}
`;
let graphSpecs;
window.onload = function () {
const generateGraphSel = document.querySelector('#visualize');
const textAreaSel = document.querySelector('textarea');
graphSpecs = `
{
"states": ["${INITIAL_STATE_NAME}", [
["no_cd_loaded", [
"cd_drawer_closed",
"cd_drawer_open",
"closing_cd_drawer"
]],
["cd_loaded", [
["cd_loaded_group", [
["cd_paused_group", [
"time_and_track_fields_not_blank",
"time_and_track_fields_blank"
]],
"cd_playing",
"cd_stopped"
]],
"stepping_forwards",
"stepping_backwards"
]]
]],
"transitions": [
{ "from": "${INITIAL_STATE_NAME}", "to": "no_cd_loaded", "event": "${INIT}", "action": "fsm_initialize_model" },
{ "from": "no_cd_loaded", "to": "cd_drawer_closed", "event": "${INIT}", "action": "identity" },
{ "from": "cd_drawer_closed", "to": "cd_drawer_open", "event": "EJECT", "action": "open_drawer" },
{ "from": "cd_drawer_open", "to": "closing_cd_drawer", "event": "EJECT", "action": "close_drawer" },
{
"from": "closing_cd_drawer", "guards": [
{ "predicate": "is_not_cd_in_drawer", "to": "cd_drawer_closed", "action": "identity" },
{ "predicate": "is_cd_in_drawer", "to": "cd_loaded", "action": "identity" }
]
},
{ "from": "cd_loaded", "to": "cd_loaded_group", "event": "${INIT}", "action": "identity" },
{ "from": "cd_playing", "to": "cd_paused_group", "event": "PAUSE", "action": "pause_playing_cd" },
{ "from": "cd_paused_group", "to": "cd_playing", "event": "PAUSE", "action": "resume_paused_cd" },
{ "from": "cd_paused_group", "to": "cd_playing", "event": "PLAY", "action": "resume_paused_cd" },
{ "from": "cd_paused_group", "to": "time_and_track_fields_not_blank", "event": "${INIT}", "action": "identity" },
{
"from": "time_and_track_fields_not_blank",
"to": "time_and_track_fields_blank",
"event": "TIMER_EXPIRED",
"action": "create_pause_timer"
},
{
"from": "time_and_track_fields_blank",
"to": "time_and_track_fields_not_blank",
"event": "TIMER_EXPIRED",
"action": "create_pause_timer"
},
{ "from": "cd_paused_group", "to": "cd_stopped", "event": "STOP", "action": "stop" },
{ "from": "cd_stopped", "to": "cd_playing", "event": "PLAY", "action": "play" },
{ "from": "cd_playing", "to": "cd_stopped", "event": "STOP", "action": "stop" },
{ "from": "cd_loaded_group", "to": "cd_stopped", "event": "${INIT}", "action": "stop" },
{
"from": "cd_loaded_group", "event": "NEXT_TRACK", "guards": [
{ "predicate": "is_last_track", "to": "cd_stopped", "action": "stop" },
{ "predicate": "is_not_last_track", "to": "history.cd_loaded_group", "action": "go_next_track" }
]
},
{
"from": "cd_loaded_group", "event": "PREVIOUS_TRACK", "guards": [
{ "predicate": "is_track_gt_1", "to": "history.cd_loaded_group", "action": "go_previous_track" },
{ "predicate": "is_track_eq_1", "to": "history.cd_loaded_group", "action": "go_track_1" }
]
},
{ "from": "cd_loaded", "to": "cd_drawer_open", "event": "EJECT", "action": "eject" },
{
"from": "stepping_forwards", "event": "TIMER_EXPIRED", "guards": [
{ "predicate": "is_not_end_of_cd", "to": "stepping_forwards", "action": "go_forward_1_s" },
{ "predicate": "is_end_of_cd", "to": "cd_stopped", "action": "stop" }
]
},
{ "from": "stepping_forwards", "to": "history.cd_loaded_group", "event": "FORWARD_UP", "action": "stop_forward_timer" },
{ "from": "cd_loaded_group", "to": "stepping_forwards", "event": "FORWARD_DOWN", "action": "go_forward_1_s" },
{ "from": "stepping_backwards", "to": "stepping_backwards", "event": "TIMER_EXPIRED", "action": "go_backward_1_s" },
{ "from": "stepping_backwards", "to": "history.cd_loaded_group", "event": "REVERSE_UP", "action": "stop_backward_timer" },
{ "from": "cd_loaded_group", "to": "stepping_backwards", "event": "REVERSE_DOWN", "action": "go_backward_1_s" }
]
}
`;
// listeners
textAreaSel.value = graphSpecs;
textAreaSel.addEventListener('change', graphChangeHandler, false);
generateGraphSel.addEventListener('click', generateGraphHandler, false);
};
function graphChangeHandler(ev) {
graphSpecs = ev.target.value;
}
function generateGraphHandler(ev) {
// Get all nodes declared in the states property of the graph specification
// We need a unique id for the node, as per cytoscape contract
// `label` will be displayed in the node rectangle area
// `parent` serves to identify compound groups
const { states, transitions } = eval(`var g=${graphSpecs};g`);
const { getLabel, getChildren } = arrayTreeLenses;
const traverse = {
seed: [],
visit: (result, traversalState, tree) => {
const parentLabel = getLabel(tree);
const children = getChildren(tree);
const graphNodes = children.map(child => {
return {
data: {
id: getLabel(child),
label: getLabel(child),
parent: parentLabel
}
}
});
return result.concat(graphNodes)
}
}
// Per our state transducer specs, all automata start in an initial NOK state, so we create that first
const firstNode = [{ data: { id: `${INITIAL_STATE_NAME}.${INIT}`, label: INIT, parent: undefined } }];
// Then we add the rest of the nodes passed in the graph specification
const nodes = firstNode.concat(breadthFirstTraverseTree(arrayTreeLenses, traverse, states));
// We display it in the console for now in case we want to export it to another visualizer or format (yed...)
console.debug(`nodes`, JSON.stringify(nodes));
// Next, we traverse the `transitions` rows to get the list of edges to display
const edgesList = transitions.map(transition => {
const { guards, from, to, event, action } = transition;
// If we have an array of guards in a row, we unwrap that array into a regular array of transition
const transitions = guards
? guards.map(({ predicate, to, action }) => ({ from, to, event, action, guard: predicate }))
: [{ from, to, event, action, guard: null }];
// Then we get all the edges from that array of transitions for that row
const edgeList = transitions.map(transition => {
const { from, to, event, action, guard } = transition;
const actionStr = action ? `/ ${action}` : '';
const guardStr = guard ? `[ ${guard} ]` : '';
const eventStr = event ? `${event}` : '';
let edge;
// Case init transition (event is INIT)
// { from: "cd_loaded", to: "cd_loaded_group", event: "${INIT}", action: "identity" },
if (event === INIT) {
const initEvent = [from, event].join('.');
nodes.push({ data: { id: `${initEvent}`, label: INIT, parent: from } });
edge = {
data: {
id: `${initEvent} - ${to} : ${guard ? guard : 'T'}? ${actionStr}`,
source: initEvent,
target: to,
type: INIT,
label: ""
}
}
}
else if (to.startsWith(HISTORY)) {
// Case history transition
// { from: "stepping_backwards", to: "history.cd_loaded_group", event: "REVERSE_UP", action:
// "stop_backward_timer" },
nodes.push({ data: { id: [HISTORY, from, to.split('.')[1]].join('.'), label: 'H', parent: to.split('.')[1] } });
// NOTE : exact same as normal condition
edge = {
data: {
id: `${from} - ${to} : ${guard ? guard : 'T'}? ${actionStr}`,
source: from,
target: [HISTORY, from, to.split('.')[1]].join('.'),
type: HISTORY,
label: [eventStr, guardStr, actionStr].join(' ')
}
}
}
else {
// Case normal transition
edge = {
data: {
id: `${from} - ${to} : ${guard ? guard : 'T'}? ${actionStr}`,
source: from,
target: to,
type: from === to ? TO_SELF : STANDARD,
label: [eventStr, guardStr, actionStr].join(' ')
}
}
}
return edge
});
return edgeList;
});
const edges = [].concat(...edgesList);
console.debug(`edges`, JSON.stringify(edges));
cytoscape({
container: document.getElementById('cy'),
boxSelectionEnabled: true,
autounselectify: true,
style,
elements: nodes.concat(edges),
layout: {
name: 'dagre',
randomize: false,
idealEdgeLength: 70,
animate: false,
nodeDimensionsIncludeLabels: true
}
});
}