/
rule.js
183 lines (164 loc) · 5.67 KB
/
rule.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
(function () {
"use strict";
var comb = require("comb"),
assert = require("assert"),
hitch = comb.hitch,
isString = comb.isString,
parser = require("./parser"),
pattern = require("./pattern"),
ObjectPattern = pattern.ObjectPattern,
NotPattern = pattern.NotPattern,
CompositePattern = pattern.CompositePattern,
array = comb.array,
flatten = comb.array.flatten,
removeDuplicates = array.removeDuplicates,
constraintMatcher = require("./constraintMatcher");
var InitialRule = comb.define(null, {
instance:{
constructor:function () {
}
}
}).as(exports, "InitialRule");
var Rule = comb.define(InitialRule, {
instance:{
constructor:function (name, options, pattern, cb) {
this.name = name;
this.pattern = pattern;
this.cb = cb;
this.priority = options.priority || options.salience || 0;
},
fire:function (flow, match) {
var ret = new comb.Promise(), cb = this.cb;
if (cb.length === 3) {
this.cb.call(flow, match.factHash, flow, ret.classic.bind(ret));
} else {
return comb.when(this.cb.call(flow, match.factHash, flow));
}
return ret;
}
}
}).as(exports, "Rule");
var createRule = exports.createRule = function (name, options, conditions, cb) {
if (comb.isArray(options)) {
cb = conditions;
conditions = options;
} else {
options = options || {};
}
var isRules = conditions.every(function (cond) {
return Array.isArray(cond);
});
if (isRules && conditions.length === 1) {
conditions = conditions[0];
isRules = false;
}
var rules = [];
if (isRules) {
var _mergePatterns = function (patt, i) {
if (!patterns[i]) {
patterns[i] = i === 0 ? [] : patterns[i - 1].slice();
//remove dup
if (i !== 0) {
patterns[i].pop();
}
patterns[i].push(patt);
} else {
patterns.forEach(function (p) {
p.push(patt);
});
}
};
var l = conditions.length, patterns = [];
for (var i = 0; i < l; i++) {
var patts = parsePattern(conditions[i]);
patts.forEach(_mergePatterns);
}
rules = patterns.map(function (patterns) {
var compPat = null;
for (var i = 0; i < patterns.length; i++) {
if (compPat === null) {
compPat = new CompositePattern(patterns[i++], patterns[i]);
} else {
compPat = new CompositePattern(compPat, patterns[i]);
}
}
return new Rule(name, options, compPat, cb);
});
} else {
rules = parsePattern(conditions).map(function (cond) {
return new Rule(name, options, cond, cb);
});
}
return rules;
};
var parsePattern = function (condition) {
if (condition[0] === "or") {
condition.shift();
return flatten(condition.map(function (cond) {
return parsePattern(cond);
}));
} else if (condition[0] === "not") {
condition.shift();
return [new NotPattern(getParamType(condition[0]), condition[1] || "m", parser.parseConstraint(condition[2] || "true"),
condition[3] || {})];
} else {
return [new ObjectPattern(getParamType(condition[0]), condition[1] || "m", parser.parseConstraint(condition[2] || "true"),
condition[3] || {})];
}
};
var getParamTypeFromString = function (param) {
var ret;
switch (param.toLowerCase()) {
case "string":
ret = String;
break;
case "date":
ret = Date;
break;
case "array":
ret = Array;
break;
case "boolean":
ret = Boolean;
break;
case "regexp":
ret = RegExp;
break;
case "number":
ret = Number;
break;
case "object":
case "hash":
ret = Object;
break;
default:
throw "invalid param type " + param;
}
return ret;
};
var getParamType = function (param) {
var ret;
if (isString(param)) {
ret = getParamTypeFromString(param);
} else if (comb.isDefined(param)) {
if (param === String) {
ret = String;
} else if (param === Array || (comb.isArray(param) && param.length === 0)) {
ret = Array;
} else if (param === Date) {
ret = Date;
} else if (param === Boolean) {
ret = Boolean;
} else if (param === RegExp) {
ret = RegExp;
} else if (param === Number) {
ret = Number;
} else if ("function" === typeof param) {
ret = param;
} else {
throw "invalid param type " + param;
}
}
return ret;
};
})();