-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRun.ts
181 lines (179 loc) · 5.95 KB
/
Run.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
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
interface IStep<W extends IWorld<W>> {
getRobot(): IRobot<W>;
getWorld(): W;
getSuccessor(): IStep<W>;
getPredecessor(): IStep<W>;
isError(): boolean;
hasPredecessor(): boolean;
hasSuccessor(): boolean;
enter(): void;
exit(): void;
}
abstract class AStep<W extends IWorld<W>> implements IStep<W> {
robot: IRobot<W>;
world: W;
successor: IStep<W> = null;
predecessor: IStep<W> = null;
getRobot(): IRobot<W> {
return this.robot;
}
getWorld(): W {
return this.world;
}
getSuccessor(): IStep<W> {
//if (this.successor == null) {
this.successor = this.computeSuccessor();
//}
return this.successor;
}
getPredecessor(): IStep<W> { return this.predecessor; }
hasPredecessor(): boolean { return this.predecessor != null; }
abstract computeSuccessor(): IStep<W>;
abstract hasSuccessor(): boolean;
abstract isError(): boolean;
constructor(robot: IRobot<W>, world: W, predecessor: IStep<W>) {
this.robot = robot; this.world = world; this.predecessor = predecessor;
}
abstract enter(): void;
abstract exit(): void;
}
class InitStep<W extends IWorld<W>> extends AStep<W> {
constructor(robot: IRobot<W>, world: W) {
super(robot, world, null);
}
computeSuccessor(): IStep<W> {
return new RuleMatchStep<W>(this.robot, this.world, this, 0);
}
hasSuccessor(): boolean { return !this.world.isFinal(); }
isError(): boolean { return false; }
enter(): void { }
exit(): void { }
}
class SimpleInitStep<W extends IWorld<W>> extends AStep<W> {
constructor(robot: IRobot<W>, world: W) {
super(robot, world, null);
}
computeSuccessor(): IStep<W> {
return new RuleActionStep<W>(this.robot, this.world, this, this.robot.getRules()[0], 0);
}
hasSuccessor(): boolean { return !this.world.isFinal(); }
isError(): boolean { return false; }
enter(): void { }
exit(): void { }
}
class RuleMatchStep<W extends IWorld<W>> extends AStep<W> {
ruleIndex: number = 0;
failures: Array<IConditionFailure>;
rule: IRule<W>;
condition: ICondition<W>;
totalFailure: boolean = false;
constructor(robot: IRobot<W>, world: W, predecessor: IStep<W>, ruleIndex: number) {
super(robot, world, predecessor);
this.ruleIndex = ruleIndex;
if (this.ruleIndex < this.robot.getRules().length) {
var rule = this.robot.getRules()[this.ruleIndex];
this.rule = rule;
this.condition = rule.getCondition();
this.failures = this.condition.check(world);
} else {
this.totalFailure = true;
}
}
hasSuccessor(): boolean { return (!this.world.isFinal()); }
computeSuccessor(): IStep<W> {
if (!this.totalFailure) {
if (this.failures.length == 0) {
return new RuleActionStep<W>(this.robot, this.world.clearMemory(), this, this.rule, 0);
}
return new RuleMatchStep<W>(this.robot, this.world, this, this.ruleIndex + 1);
}
return new ErrorStep<W>(this.robot, this.world, this);
}
isError(): boolean { return false; }
enter(): void {
if (this.condition) {
this.condition.enter(this.failures);
}
}
exit(): void {
if (this.condition) {
this.condition.exit(this.failures);
}
}
}
class RuleActionStep<W extends IWorld<W>> extends AStep<W> {
actionIndex: number;
rule: IRule<W>;
action: IAction<W>;
totalFailure: boolean = false;
runnable: boolean;
constructor(robot: IRobot<W>, world: W, predecessor: IStep<W>, rule: IRule<W>, actionIndex: number) {
super(robot, world, predecessor);
this.rule = rule;
this.actionIndex = actionIndex;
var actions = this.rule.getActions();
if (this.actionIndex < actions.length) {
this.action = actions[this.actionIndex];
this.runnable = this.action.runnable(this.world);
} else {
this.totalFailure = true;
}
}
hasSuccessor(): boolean { return !this.world.isFinal(); }
computeSuccessor(): IStep<W> {
var actions = this.rule.getActions();
if (!this.totalFailure) {
var action = this.action;
if (!this.runnable) {
return new ErrorStep<W>(this.robot, this.world, this);
}
if (this.actionIndex + 1 < actions.length) {
return new RuleActionStep<W>(this.robot, action.run(this.world), this, this.rule, this.actionIndex + 1);
}
var newworld = action.run(this.world);
if (newworld.isFinal()) {
return new FinalStep<W>(this.robot, newworld, this);
} else {
return new RuleMatchStep<W>(this.robot, action.run(this.world), this, 0);
}
} else {
return new RuleMatchStep<W>(this.robot, this.world, this, 0);
}
}
enter(): void {
if (this.action) {
this.action.enter(this.runnable);
}
}
exit(): void {
if (this.action) {
this.action.exit();
}
}
isError(): boolean { return false; }
}
class ErrorStep<W extends IWorld<W>> extends AStep<W> {
constructor(robot: IRobot<W>, world: W, predecessor: IStep<W>) {
super(robot, world, predecessor);
}
computeSuccessor(): IStep<W> {
return this;
}
hasSuccessor(): boolean { return false; }
isError(): boolean { return true; }
enter(): void { this.predecessor.enter(); }
exit(): void { this.predecessor.exit(); }
}
class FinalStep<W extends IWorld<W>> extends AStep<W> {
constructor(robot: IRobot<W>, world: W, predecessor: IStep<W>) {
super(robot, world, predecessor);
}
computeSuccessor(): IStep<W> {
return this;
}
hasSuccessor(): boolean { return false; }
isError(): boolean { return false; }
enter(): void { }
exit(): void { }
}