-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.js
214 lines (189 loc) · 7.21 KB
/
core.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
/* Convert a proposition to regex for pattern matching
* The pattern is identified by a <tag>
* link the variables position in the compiled pattern to a
* variable name in the <link> dictionary
*/
function compileAndLinkPattern(pattern_string, link, tag) {
var trimmed_pattern = trim(pattern_string);
var divided_pattern = trimmed_pattern.split("");
var is_compound = /[&v=>]/.test(trimmed_pattern);
var matched_variable_count = 0;
var variable_ids = {};
for (var i = 0, len = divided_pattern.length, test_pat = /[A-Za-uw-z]/; i < len; i++) {
if (test_pat.test(divided_pattern[i])) {
if (variable_ids[divided_pattern[i]]) {
divided_pattern[i] = "\\" + variable_ids[divided_pattern[i]];
} else {
variable_ids[divided_pattern[i]] = ++matched_variable_count;
link[divided_pattern[i]] = link[divided_pattern[i]] || {};
link[divided_pattern[i]][tag] = matched_variable_count;
if (!is_compound) {
divided_pattern[i] = "([\\w\\(\\)v&~=>\|-]+)";
} else {
divided_pattern[i] = "([~\\w]+|~*\\([\\w\\(\\)v&~=>\|-]+\\))";
}
}
} else if (divided_pattern[i] === '(' || divided_pattern[i] === ')' ||
divided_pattern[i] === '|'){
divided_pattern[i] = '\\' + divided_pattern[i];
}
}
return new RegExp("^" + divided_pattern.join("") + "$");
}
function parse(s, depth) {
var out = '';
var lowest = "none";
while (depth < s.length) {
var c = s[depth];
if (c == '(') {
var p = parse(s, depth + 1);
var leftmost = depth && /[&v~]|(=)|(>)|(-)/.exec(s[depth-1]);
var rightmost = p[1] && (depth + p[1].length + 2) < (s.length - 1) &&
/[&v~]|(=)|(>)|(-)/.exec(s[depth + p[1].length + 2]);
if (p[1].length > 1 && p[0] != "~" &&
((leftmost && comparePrecedence(p[0], leftmost[0]) <= 0) ||
(rightmost && comparePrecedence(p[0], rightmost[0]) <= 0))) {
depth += p[1].length + 2;
p[1] = '(' + p[1] + ')';
} else {
depth += p[1].length + 2;
}
out += p[1];
} else if (c == ')') {
if (out.length > 0) {
return [lowest, out];
} else {
depth++;
}
} else {
if (precedence[c] && comparePrecedence(c, lowest) < 0) {
lowest = c;
}
out += c;
depth++;
}
}
return out;
}
/* Rule function constructor
* params: name: String; premises: String Array; conclusion: String
*/
function Rule(name, premises, conclusion) {
this.name = name;
this.uncompiled_premises = premises;
this.premises = premises.slice(0);
this.conclusion = trim(conclusion);
this.sub_index = []; //index of substition slots in conclusion
this.link = {}; //link structure: {variable_name : {premise_tag: var_position_in premise,...},...}
this.compile_and_link();
}
Rule.prototype.compile_and_link = function () {
//compile regex for each premise (identified by a unique tag)
//and link variable's name in each premise with corresponding matched position
for (var i = 0, len = this.premises.length; i < len; i++) {
this.premises[i] = compileAndLinkPattern(this.premises[i], this.link, i);
this.premises[i].tag = i;
}
//find index for substitution slot in conclusion
var a_conclusion = this.conclusion.split("");
for (var i = 0, len = a_conclusion.length, test_pat = /[A-Za-uw-z]/; i < len; i++) {
if (test_pat.test(a_conclusion[i])) {
this.sub_index.push(i);
}
}
};
Rule.prototype.substitute = function (var_map) {
var a_conclusion = this.conclusion.split("");
for (var i = 0, len = this.sub_index.length; i < len; i++) {
var cur_pos = this.sub_index[i];
if (var_map[a_conclusion[cur_pos]]) {
a_conclusion[cur_pos] = var_map[a_conclusion[cur_pos]];
if (/[v&]|(=>)(|-)/.test(a_conclusion[cur_pos])
&& a_conclusion[cur_pos][0] != '(') {
a_conclusion[cur_pos] = "(" + a_conclusion[cur_pos] + ")";
}
} else {
a_conclusion[this.sub_index[i]] = "<VAR>";
}
}
return parse(a_conclusion.join("").split(""), 0);
};
/* Generate a variable_name -> variable value map
* from a dictionary of tagged matches' variable values
* using a rule's link dictionary
*/
function generateVarMap(taggedMatches, rule) {
var var_map = {};
for (var var_name in rule.link) {
var premises_set = rule.link[var_name];
var variable_value = "";
for (var tag in premises_set) {
var value_pos = premises_set[tag];
if (variable_value == "") {
variable_value = taggedMatches[tag][value_pos];
} else if (taggedMatches[tag][value_pos] != variable_value) {
return null;
}
}
var_map[var_name] = variable_value;
}
return var_map;
}
/* Match an array of expressions with a Rule object's premises
* return a variables map for conclusion substituion or null if cannot match
*/
function matchWithRule(expressions, rule) {
if (expressions.length < rule.premises.length) {
return null;
}
var matched = arrayInit(false, expressions.length)
var taggedMatches = {}; //{premise_tag: [match, matched_variables]}
for (var i = 0; i < expressions.length; i++) {
var expr = trim(expressions[i]);
var j = 0;
for (;j < rule.premises.length; j++) {
if (!matched[j]) {
var match = rule.premises[j].exec(expr);
if (match) {
for (var k = 1; k < match.length; k++) {
match[k] = parse(match[k].split(""), 0);
}
taggedMatches[rule.premises[j].tag] = match;
matched[j] = true;
break;
}
}
}
if (j >= rule.premises.length) {
return null;
}
}
return generateVarMap(taggedMatches, rule);
}
/* Expression object constructor
* params: identifier: string;
*/
function Expression(identifier, content, type, rule_name) {
this.identifier = identifier;
this.content = content;
this.rule_name = rule_name;
this.type = type;
this.scope = identifier.split(".");
if (this.type != "Assumption") {
this.scope.pop();
}
}
//check if an expression can be seen under a scope
//params: scope: number array
Expression.prototype.canBeSeen = function (scope) {
if (this.scope.length > scope.length) {
return false;
} else {
for (var i = 0, len = this.scope.length; i < len; i++) {
if (this.scope[i] != scope[i]) {
return false;
}
}
}
return true;
};