forked from hyperandroid/Automata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFSM.ts
137 lines (105 loc) · 3.25 KB
/
FSM.ts
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
import State from "./State";
import Transition from "./Transition";
import Session from "./Session";
import Registry from "./Registry";
export interface TransitionJson {
from: string;
to: string;
event: string;
}
export interface FSMJson {
name: string;
state: string[];
initial: string;
transition: TransitionJson[];
}
export type FSMCollectionJson = FSMJson[];
export default class FSM extends State {
states_: State[] = [];
transitions_: Transition[] = [];
initialState_: State = null;
constructor(json: FSMJson) {
super(json.name, null);
this.__createStates(json.state, json.initial);
this.__createTransitions(json.transition);
}
__createStates(states: string[], initial: string) {
for (let name of states) {
let st: State = null;
["@", "FSM:"].forEach(prefix => {
if (name.lastIndexOf(prefix) !== -1) {
const fsmname = name.substring(prefix.length);
st = Registry.Get(fsmname);
}
});
if (st === null) {
st = new State(name, this);
if (name === initial) {
this.initialState_ = st;
}
}
this.states_.push(st);
}
if (this.initialState_ === null) {
throw new Error("No initial state defined.");
}
}
__findStateByName(n: string): State {
for (let s of this.states_) {
if (s.name === n) {
return s;
}
}
if ( n===this.name) {
return this;
}
return null;
}
__createTransitions(transitions: TransitionJson[]) {
transitions.forEach(v => {
const f: State = this.__findStateByName(v.from);
const t: State = this.__findStateByName(v.to);
const e: string = v.event;
if (!f || !t) {
throw new Error(
`Wrongly defined Automata '${this.name}'. Transition '${v.event}' refers unknown state:'${(!f ? v.from : v.to)}'`);
}
this.transitions_.push(new Transition(f, t, e).setup());
});
}
get isFinal() {
return this.states_.length === 0;
}
onEnter<T>(session: Session<T>, tr: Transition) {
if (!this.isFinal) {
session.pushContext(this.initialState_, tr);
}
}
toJSON() : any {
return {
name: this.name_,
state: this.states_.map(st => st instanceof FSM ? "@"+st.name_ : st.name_),
initial: this.initialState_.name_,
transition: this.transitions_.map(tr => {
return {
event: tr.event_,
from: tr.from.name_,
to: tr.to.name_
}
})
}
}
serialize() : FSMJson[] {
const jfsm: FSMJson[] = [];
jfsm.push(this.toJSON() as FSMJson);
this.__fsmToJSONImpl(jfsm, this);
return jfsm;
}
__fsmToJSONImpl( jfsm: FSMJson[], fsm: FSM ) {
fsm.states_.forEach(s => {
if (s instanceof FSM) {
jfsm.push( ...(s as FSM).serialize() );
}
})
}
}