forked from thundergolfer/the-general-problem-solver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgps.ts
98 lines (73 loc) · 2.49 KB
/
gps.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
export function greeter(person: string) {
return "Hello, " + person;
}
let user = "Jane Doe";
export function gps(initialStates: string[], goalStates: string[], operators: Operator[]) {
var prefix = 'Executing ';
operators.forEach(function(operator: Operator) {
operator.add.push(prefix + operator.action);
});
var finalStates = achieveAll(initialStates, operators, goalStates, []);
if (!finalStates.length) return false;
var actions = finalStates.filter(function(elem: string) {
return elem.startsWith(prefix);
});
return actions;
}
function achieveAll(states: string[], ops: Operator[], goals: string[], goalStack: string[]) {
goals.forEach(function (goal: string) {
states = achieve(states, ops, goal, goalStack);
if (!states.length) return <string[]>[];
});
goals.forEach(function(goal: string) {
if (states && states.indexOf(goal) < 0) return [];
});
return states;
}
export class Operator {
add: string[];
delete: string[];
preconds: string[];
action: string;
constructor(add: string[],
del: string[],
preconds: string[],
action: string) {
this.add = add;
this.delete = del;
this.preconds = preconds;
this.action = action;
}
}
function achieve(states: string[], operators: Operator[], goal: string, goalStack: string[]) {
// Let's check to see if the state already holds before we do anything.
if (states && states.indexOf(goal) >= 0) {
return states;
}
if (goalStack.indexOf(goal) >= 0) { // is this a bug? What if the index of goal is 0 AND 4?
return <string[]>[];
}
var result = <string[]>[];
for(var op of operators) {
if (op.add.indexOf(goal) < 0) {
continue
}
var result: string[] = applyOperator(op, states, operators, goal, goalStack);
if (!!result.length) return result;
}
return <string[]>[];
}
export function applyOperator(operator: Operator,
states: string[],
operators: Operator[],
goal: string,
goalStack: string[]) {
var result = achieveAll(states, operators, operator.preconds, [goal].concat(goalStack));
if (!result.length) return [];
let addList = operator.add;
let delList = operator.delete;
var newState = result.filter(function(elem: string) {
return this.indexOf(elem) < 0;
}, delList);
return newState.concat(addList);
}