-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathstructures.js
157 lines (137 loc) · 3.52 KB
/
structures.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
/* Set :
a simple set object that counts its members, unlike regular objects
*/
function Set()
{
this.items={}
this.length=0
if(arguments.length) this.add.apply(this, arguments)
}
Set.prototype.add=function ()
{
for(var i=0; i<arguments.length; i++)
{
var s=arguments[i]
if(s in this.items) continue
this.items[s]=true
this.length++
}
return this.length
}
Set.prototype.remove=function ()
{
for(var i=0; i<arguments.length; i++)
{
var s=arguments[i]
if(!(s in this.items)) continue
delete this.items[s]
this.length--
}
return this.length
}
/* CompiledPath :
is a maximal path (each state has exactly one child)
[State - State - … State - Parallel]
or
[State - State - … State - AtomicState]
*/
function CompiledPath(path)
{
this.parent = path[0].parentNode
this.path = path
this.end = path[path.length-1]
this.atomic = this.end.tagName!="parallel"
}
CompiledPath.prototype.toString=function()
{
return this.path.map(function(s){return s.getAttribute("id")}).join(" → ")
+ (this.atomic?"":" ⇉ ")
}
/* CompiledTree:
+-Path (atomic)
|
Path-+-Path (atomic)
|
| +-Path (atomic)
+-Path-+
+-Path …
where each branching occurs at the Parallel state at the end of a Path */
function CompiledTree(root, children)
{
this.parallels={}
if(!root.atomic) this.parallels[root.end.getAttribute("id")]=this
this.root=root
this.children=[]
}
CompiledTree.prototype.toString=function()
{
return this.root +
(this.root.atomic?"" : ("(" + this.children.join(" ∥ ") + ")"))
}
CompiledTree.prototype.appendChild=function(subTree)
{
for(var id in subTree.parallels)
this.parallels[id] = subTree.parallels[id]
this.children.push(subTree)
}
CompiledTree.prototype.attach=function(subTree)
{
var id
if(!((id=subTree.root.parent.getAttribute("id")) in this.parallels))
return false
var p=this.parallels[id].children
for(var i=0, c; c=p[i]; i++) if(c.root.path[0] == subTree.root.path[0])
{
for(id in c.parallels)
delete this.parallels[id]
break
}
for(id in subTree.parallels)
this.parallels[id] = subTree.parallels[id]
p[i]=subTree
return true
}
CompiledTree.prototype.inEntryOrder=function()
{
return this.root.path.concat(this.root.atomic ? [] : this.children.map(function(c){ return c.inEntryOrder() }).reduce(function(a,b){return a.concat(b)})).filter(function(c){return !c.CA})
}
CompiledTree.prototype.atoms=function()
{
return this.root.atomic ? [this.root.end] : this.children.map(function(c){ return c.atoms() }).reduce(function(a,b){return a.concat(b)})
}
// returns the list of enabled transitions and whether they are all targetless
// 'test' is a function that takes a state as input
// and returns the first matching transition in the state, if any
CompiledTree.prototype.select=function(test)
{
var enabled=[]
var allChildrenEnabled=true
var enabledTargetedTransition=false
if(!this.root.atomic){
var childSelection
for(var i=0; i<this.children.length; i++){
childSelection=this.children[i].select(test)
if(!childSelection.enabled.length){
allChildrenEnabled=false
continue
}
enabled=enabled.concat(childSelection.enabled)
enabledTargetedTransition |= childSelection.hasTargets
}
}
else allChildrenEnabled=false
if(!allChildrenEnabled){
var t
for(var p=this.root.path, i=p.length-1; i>=0; i--) if(t=test(p[i])){
if(!t.targets)
enabled.push(t)
else{
if(enabledTargetedTransition) break
enabled.push(t)
enabledTargetedTransition=true
}
break
}
}
return {hasTargets:enabledTargetedTransition, enabled:enabled}
}