-
Notifications
You must be signed in to change notification settings - Fork 181
/
nools.js
17690 lines (15427 loc) · 569 KB
/
nools.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
(function () {
"use strict";
var nools = require("../");
if (typeof Object.getPrototypeOf !== "function") {
Object.getPrototypeOf = "".__proto__ === String.prototype
? function (object) {
return object.__proto__;
}
: function (object) {
// May break if the constructor has been tampered with
return object.constructor.prototype;
};
}
if ("function" === typeof this.define && this.define.amd) {
define([], function () {
return nools;
});
} else {
this.nools = nools;
}
}).call(typeof window !== "undefined" ? window : this);
},{"../":2}],2:[function(require,module,exports){
module.exports = exports = require("./lib");
},{"./lib":15}],3:[function(require,module,exports){
"use strict";
var extd = require("./extended"),
declare = extd.declare,
AVLTree = extd.AVLTree,
LinkedList = extd.LinkedList,
isPromise = extd.isPromiseLike,
EventEmitter = require("events").EventEmitter;
var FactHash = declare({
instance: {
constructor: function () {
this.memory = {};
this.memoryValues = new LinkedList();
},
clear: function () {
this.memoryValues.clear();
this.memory = {};
},
remove: function (v) {
var hashCode = v.hashCode,
memory = this.memory,
ret = memory[hashCode];
if (ret) {
this.memoryValues.remove(ret);
delete memory[hashCode];
}
return ret;
},
insert: function (insert) {
var hashCode = insert.hashCode;
if (hashCode in this.memory) {
throw new Error("Activation already in agenda " + insert.rule.name + " agenda");
}
this.memoryValues.push((this.memory[hashCode] = insert));
}
}
});
var DEFAULT_AGENDA_GROUP = "main";
module.exports = declare(EventEmitter, {
instance: {
constructor: function (flow, conflictResolution) {
this.agendaGroups = {};
this.agendaGroupStack = [DEFAULT_AGENDA_GROUP];
this.rules = {};
this.flow = flow;
this.comparator = conflictResolution;
this.setFocus(DEFAULT_AGENDA_GROUP).addAgendaGroup(DEFAULT_AGENDA_GROUP);
},
addAgendaGroup: function (groupName) {
if (!extd.has(this.agendaGroups, groupName)) {
this.agendaGroups[groupName] = new AVLTree({compare: this.comparator});
}
},
getAgendaGroup: function (groupName) {
return this.agendaGroups[groupName || DEFAULT_AGENDA_GROUP];
},
setFocus: function (agendaGroup) {
if (agendaGroup !== this.getFocused() && this.agendaGroups[agendaGroup]) {
this.agendaGroupStack.push(agendaGroup);
this.emit("focused", agendaGroup);
}
return this;
},
getFocused: function () {
var ags = this.agendaGroupStack;
return ags[ags.length - 1];
},
getFocusedAgenda: function () {
return this.agendaGroups[this.getFocused()];
},
register: function (node) {
var agendaGroup = node.rule.agendaGroup;
this.rules[node.name] = {tree: new AVLTree({compare: this.comparator}), factTable: new FactHash()};
if (agendaGroup) {
this.addAgendaGroup(agendaGroup);
}
},
isEmpty: function () {
var agendaGroupStack = this.agendaGroupStack, changed = false;
while (this.getFocusedAgenda().isEmpty() && this.getFocused() !== DEFAULT_AGENDA_GROUP) {
agendaGroupStack.pop();
changed = true;
}
if (changed) {
this.emit("focused", this.getFocused());
}
return this.getFocusedAgenda().isEmpty();
},
fireNext: function () {
var agendaGroupStack = this.agendaGroupStack, ret = false;
while (this.getFocusedAgenda().isEmpty() && this.getFocused() !== DEFAULT_AGENDA_GROUP) {
agendaGroupStack.pop();
}
if (!this.getFocusedAgenda().isEmpty()) {
var activation = this.pop();
this.emit("fire", activation.rule.name, activation.match.factHash);
var fired = activation.rule.fire(this.flow, activation.match);
if (isPromise(fired)) {
ret = fired.then(function () {
//return true if an activation fired
return true;
});
} else {
ret = true;
}
}
//return false if activation not fired
return ret;
},
pop: function () {
var tree = this.getFocusedAgenda(), root = tree.__root;
while (root.right) {
root = root.right;
}
var v = root.data;
tree.remove(v);
var rule = this.rules[v.name];
rule.tree.remove(v);
rule.factTable.remove(v);
return v;
},
peek: function () {
var tree = this.getFocusedAgenda(), root = tree.__root;
while (root.right) {
root = root.right;
}
return root.data;
},
modify: function (node, context) {
this.retract(node, context);
this.insert(node, context);
},
retract: function (node, retract) {
var rule = this.rules[node.name];
retract.rule = node;
var activation = rule.factTable.remove(retract);
if (activation) {
this.getAgendaGroup(node.rule.agendaGroup).remove(activation);
rule.tree.remove(activation);
}
},
insert: function (node, insert) {
var rule = this.rules[node.name], nodeRule = node.rule, agendaGroup = nodeRule.agendaGroup;
rule.tree.insert(insert);
this.getAgendaGroup(agendaGroup).insert(insert);
if (nodeRule.autoFocus) {
this.setFocus(agendaGroup);
}
rule.factTable.insert(insert);
},
dispose: function () {
for (var i in this.agendaGroups) {
this.agendaGroups[i].clear();
}
var rules = this.rules;
for (i in rules) {
if (i in rules) {
rules[i].tree.clear();
rules[i].factTable.clear();
}
}
this.rules = {};
}
}
});
},{"./extended":12,"events":60}],4:[function(require,module,exports){
/*jshint evil:true*/
"use strict";
var extd = require("../extended"),
forEach = extd.forEach,
isString = extd.isString;
exports.modifiers = ["assert", "modify", "retract", "emit", "halt", "focus", "getFacts"];
var createFunction = function (body, defined, scope, scopeNames, definedNames) {
var declares = [];
forEach(definedNames, function (i) {
if (body.indexOf(i) !== -1) {
declares.push("var " + i + "= defined." + i + ";");
}
});
forEach(scopeNames, function (i) {
if (body.indexOf(i) !== -1) {
declares.push("var " + i + "= scope." + i + ";");
}
});
body = ["((function(){", declares.join(""), "\n\treturn ", body, "\n})())"].join("");
try {
return eval(body);
} catch (e) {
throw new Error("Invalid action : " + body + "\n" + e.message);
}
};
var createDefined = (function () {
var _createDefined = function (action, defined, scope) {
if (isString(action)) {
var declares = [];
extd(defined).keys().forEach(function (i) {
if (action.indexOf(i) !== -1) {
declares.push("var " + i + "= defined." + i + ";");
}
});
extd(scope).keys().forEach(function (i) {
if (action.indexOf(i) !== -1) {
declares.push("var " + i + "= function(){var prop = scope." + i + "; return __objToStr__.call(prop) === '[object Function]' ? prop.apply(void 0, arguments) : prop;};");
}
});
if (declares.length) {
declares.unshift("var __objToStr__ = Object.prototype.toString;");
}
action = [declares.join(""), "return ", action, ";"].join("");
action = new Function("defined", "scope", action)(defined, scope);
}
var ret = action.hasOwnProperty("constructor") && "function" === typeof action.constructor ? action.constructor : function (opts) {
opts = opts || {};
for (var i in opts) {
if (i in action) {
this[i] = opts[i];
}
}
};
var proto = ret.prototype;
for (var i in action) {
proto[i] = action[i];
}
return ret;
};
return function (options, defined, scope) {
return _createDefined(options.properties, defined, scope);
};
})();
exports.createFunction = createFunction;
exports.createDefined = createDefined;
},{"../extended":12}],5:[function(require,module,exports){
var Buffer=require("__browserify_Buffer").Buffer;/*jshint evil:true*/
"use strict";
var extd = require("../extended"),
parser = require("../parser"),
constraintMatcher = require("../constraintMatcher.js"),
indexOf = extd.indexOf,
forEach = extd.forEach,
removeDuplicates = extd.removeDuplicates,
map = extd.map,
obj = extd.hash,
keys = obj.keys,
merge = extd.merge,
rules = require("../rule"),
common = require("./common"),
modifiers = common.modifiers,
createDefined = common.createDefined,
createFunction = common.createFunction;
/**
* @private
* Parses an action from a rule definition
* @param {String} action the body of the action to execute
* @param {Array} identifiers array of identifiers collected
* @param {Object} defined an object of defined
* @param scope
* @return {Object}
*/
var parseAction = function (action, identifiers, defined, scope) {
var declares = [];
forEach(identifiers, function (i) {
if (action.indexOf(i) !== -1) {
declares.push("var " + i + "= facts." + i + ";");
}
});
extd(defined).keys().forEach(function (i) {
if (action.indexOf(i) !== -1) {
declares.push("var " + i + "= defined." + i + ";");
}
});
extd(scope).keys().forEach(function (i) {
if (action.indexOf(i) !== -1) {
declares.push("var " + i + "= scope." + i + ";");
}
});
extd(modifiers).forEach(function (i) {
if (action.indexOf(i) !== -1) {
declares.push("if(!" + i + "){ var " + i + "= flow." + i + ";}");
}
});
var params = ["facts", 'flow'];
if (/next\(.*\)/.test(action)) {
params.push("next");
}
action = declares.join("") + action;
try {
return new Function("defined, scope", "return " + new Function(params.join(","), action).toString())(defined, scope);
} catch (e) {
throw new Error("Invalid action : " + action + "\n" + e.message);
}
};
var createRuleFromObject = (function () {
var __resolveRule = function (rule, identifiers, conditions, defined, name) {
var condition = [], definedClass = rule[0], alias = rule[1], constraint = rule[2], refs = rule[3];
if (extd.isHash(constraint)) {
refs = constraint;
constraint = null;
}
if (definedClass && !!(definedClass = defined[definedClass])) {
condition.push(definedClass);
} else {
throw new Error("Invalid class " + rule[0] + " for rule " + name);
}
condition.push(alias, constraint, refs);
conditions.push(condition);
identifiers.push(alias);
if (constraint) {
forEach(constraintMatcher.getIdentifiers(parser.parseConstraint(constraint)), function (i) {
identifiers.push(i);
});
}
if (extd.isObject(refs)) {
for (var j in refs) {
var ident = refs[j];
if (indexOf(identifiers, ident) === -1) {
identifiers.push(ident);
}
}
}
};
function parseRule(rule, conditions, identifiers, defined, name) {
if (rule.length) {
var r0 = rule[0];
if (r0 === "not" || r0 === "exists") {
var temp = [];
rule.shift();
__resolveRule(rule, identifiers, temp, defined, name);
var cond = temp[0];
cond.unshift(r0);
conditions.push(cond);
} else if (r0 === "or") {
var conds = [r0];
rule.shift();
forEach(rule, function (cond) {
parseRule(cond, conds, identifiers, defined, name);
});
conditions.push(conds);
} else {
__resolveRule(rule, identifiers, conditions, defined, name);
identifiers = removeDuplicates(identifiers);
}
}
}
return function (obj, defined, scope) {
var name = obj.name;
if (extd.isEmpty(obj)) {
throw new Error("Rule is empty");
}
var options = obj.options || {};
options.scope = scope;
var constraints = obj.constraints || [], l = constraints.length;
if (!l) {
constraints = ["true"];
}
var action = obj.action;
if (extd.isUndefined(action)) {
throw new Error("No action was defined for rule " + name);
}
var conditions = [], identifiers = [];
forEach(constraints, function (rule) {
parseRule(rule, conditions, identifiers, defined, name);
});
return rules.createRule(name, options, conditions, parseAction(action, identifiers, defined, scope));
};
})();
exports.parse = function (src, file) {
//parse flow from file
return parser.parseRuleSet(src, file);
};
exports.compile = function (flowObj, options, cb, Container) {
if (extd.isFunction(options)) {
cb = options;
options = {};
} else {
options = options || {};
}
var name = flowObj.name || options.name;
//if !name throw an error
if (!name) {
throw new Error("Name must be present in JSON or options");
}
var flow = new Container(name);
var defined = merge({Array: Array, String: String, Number: Number, Boolean: Boolean, RegExp: RegExp, Date: Date, Object: Object}, options.define || {});
if (typeof Buffer !== "undefined") {
defined.Buffer = Buffer;
}
var scope = merge({console: console}, options.scope);
//add the anything added to the scope as a property
forEach(flowObj.scope, function (s) {
scope[s.name] = true;
});
//add any defined classes in the parsed flowObj to defined
forEach(flowObj.define, function (d) {
defined[d.name] = createDefined(d, defined, scope);
});
//expose any defined classes to the flow.
extd(defined).forEach(function (cls, name) {
flow.addDefined(name, cls);
});
var scopeNames = extd(flowObj.scope).pluck("name").union(extd(scope).keys().value()).value();
var definedNames = map(keys(defined), function (s) {
return s;
});
forEach(flowObj.scope, function (s) {
scope[s.name] = createFunction(s.body, defined, scope, scopeNames, definedNames);
});
var rules = flowObj.rules;
if (rules.length) {
forEach(rules, function (rule) {
flow.__rules = flow.__rules.concat(createRuleFromObject(rule, defined, scope));
});
}
if (cb) {
cb.call(flow, flow);
}
return flow;
};
exports.transpile = require("./transpile").transpile;
},{"../constraintMatcher.js":9,"../extended":12,"../parser":44,"../rule":49,"./common":4,"./transpile":6,"__browserify_Buffer":63}],6:[function(require,module,exports){
var Buffer=require("__browserify_Buffer").Buffer;var extd = require("../extended"),
forEach = extd.forEach,
indexOf = extd.indexOf,
merge = extd.merge,
isString = extd.isString,
modifiers = require("./common").modifiers,
constraintMatcher = require("../constraintMatcher"),
parser = require("../parser");
function definedToJs(options) {
/*jshint evil:true*/
options = isString(options) ? new Function("return " + options + ";")() : options;
var ret = ["(function(){"], value;
if (options.hasOwnProperty("constructor") && "function" === typeof options.constructor) {
ret.push("var Defined = " + options.constructor.toString() + ";");
} else {
ret.push("var Defined = function(opts){ for(var i in opts){if(opts.hasOwnProperty(i)){this[i] = opts[i];}}};");
}
ret.push("var proto = Defined.prototype;");
for (var key in options) {
if (options.hasOwnProperty(key)) {
value = options[key];
ret.push("proto." + key + " = " + (extd.isFunction(value) ? value.toString() : extd.format("%j", value)) + ";");
}
}
ret.push("return Defined;");
ret.push("}())");
return ret.join("");
}
function actionToJs(action, identifiers, defined, scope) {
var declares = [], usedVars = {};
forEach(identifiers, function (i) {
if (action.indexOf(i) !== -1) {
usedVars[i] = true;
declares.push("var " + i + "= facts." + i + ";");
}
});
extd(defined).keys().forEach(function (i) {
if (action.indexOf(i) !== -1 && !usedVars[i]) {
usedVars[i] = true;
declares.push("var " + i + "= defined." + i + ";");
}
});
extd(scope).keys().forEach(function (i) {
if (action.indexOf(i) !== -1 && !usedVars[i]) {
usedVars[i] = true;
declares.push("var " + i + "= scope." + i + ";");
}
});
extd(modifiers).forEach(function (i) {
if (action.indexOf(i) !== -1 && !usedVars[i]) {
declares.push("var " + i + "= flow." + i + ";");
}
});
var params = ["facts", 'flow'];
if (/next\(.*\)/.test(action)) {
params.push("next");
}
action = declares.join("") + action;
try {
return ["function(", params.join(","), "){", action, "}"].join("");
} catch (e) {
throw new Error("Invalid action : " + action + "\n" + e.message);
}
}
function parseConstraintModifier(constraint, ret) {
if (constraint.length && extd.isString(constraint[0])) {
var modifier = constraint[0].match(" *(from)");
if (modifier) {
modifier = modifier[0];
switch (modifier) {
case "from":
ret.push(', "', constraint.shift(), '"');
break;
default:
throw new Error("Unrecognized modifier " + modifier);
}
}
}
}
function parseConstraintHash(constraint, ret, identifiers) {
if (constraint.length && extd.isHash(constraint[0])) {
//ret of options
var refs = constraint.shift();
extd(refs).values().forEach(function (ident) {
if (indexOf(identifiers, ident) === -1) {
identifiers.push(ident);
}
});
ret.push(',' + extd.format('%j', [refs]));
}
}
function constraintsToJs(constraint, identifiers) {
constraint = constraint.slice(0);
var ret = [];
if (constraint[0] === "or") {
ret.push('["' + constraint.shift() + '"');
ret.push(extd.map(constraint,function (c) {
return constraintsToJs(c, identifiers);
}).join(",") + "]");
return ret;
} else if (constraint[0] === "not" || constraint[0] === "exists") {
ret.push('"', constraint.shift(), '", ');
}
identifiers.push(constraint[1]);
ret.push(constraint[0], ', "' + constraint[1].replace(/\\/g, "\\\\").replace(/"/g, "\\\"") + '"');
constraint.splice(0, 2);
if (constraint.length) {
//constraint
var c = constraint.shift();
if (extd.isString(c) && c) {
ret.push(',"' + c.replace(/\\/g, "\\\\").replace(/"/g, "\\\""), '"');
forEach(constraintMatcher.getIdentifiers(parser.parseConstraint(c)), function (i) {
identifiers.push(i);
});
} else {
ret.push(',"true"');
constraint.unshift(c);
}
}
parseConstraintModifier(constraint, ret);
parseConstraintHash(constraint, ret, identifiers);
return '[' + ret.join("") + ']';
}
exports.transpile = function (flowObj, options) {
options = options || {};
var ret = [];
ret.push("(function(){");
ret.push("return function(options){");
ret.push("options = options || {};");
ret.push("var bind = function(scope, fn){return function(){return fn.apply(scope, arguments);};}, defined = {Array: Array, String: String, Number: Number, Boolean: Boolean, RegExp: RegExp, Date: Date, Object: Object}, scope = options.scope || {};");
ret.push("var optDefined = options.defined || {}; for(var i in optDefined){defined[i] = optDefined[i];}");
var defined = merge({Array: Array, String: String, Number: Number, Boolean: Boolean, RegExp: RegExp, Date: Date, Object: Object}, options.define || {});
if (typeof Buffer !== "undefined") {
defined.Buffer = Buffer;
}
var scope = merge({console: console}, options.scope);
ret.push(["return nools.flow('", options.name, "', function(){"].join(""));
//add any defined classes in the parsed flowObj to defined
ret.push(extd(flowObj.define || []).map(function (defined) {
var name = defined.name;
defined[name] = {};
return ["var", name, "= defined." + name, "= this.addDefined('" + name + "',", definedToJs(defined.properties) + ");"].join(" ");
}).value().join("\n"));
ret.push(extd(flowObj.scope || []).map(function (s) {
var name = s.name;
scope[name] = {};
return ["var", name, "= scope." + name, "= ", s.body, ";"].join(" ");
}).value().join("\n"));
ret.push("scope.console = console;\n");
ret.push(extd(flowObj.rules || []).map(function (rule) {
var identifiers = [], ret = ["this.rule('", rule.name.replace(/'/g, "\\'"), "'"], options = extd.merge(rule.options || {}, {scope: "scope"});
ret.push(",", extd.format("%j", [options]).replace(/(:"scope")/, ":scope"));
if (rule.constraints && !extd.isEmpty(rule.constraints)) {
ret.push(", [");
ret.push(extd(rule.constraints).map(function (c) {
return constraintsToJs(c, identifiers);
}).value().join(","));
ret.push("]");
}
ret.push(",", actionToJs(rule.action, identifiers, defined, scope));
ret.push(");");
return ret.join("");
}).value().join(""));
ret.push("});");
ret.push("};");
ret.push("}());");
return ret.join("");
};
},{"../constraintMatcher":9,"../extended":12,"../parser":44,"./common":4,"__browserify_Buffer":63}],7:[function(require,module,exports){
var map = require("./extended").map;
function salience(a, b) {
return a.rule.priority - b.rule.priority;
}
function bucketCounter(a, b) {
return a.counter - b.counter;
}
function factRecency(a, b) {
/*jshint noempty: false*/
var i = 0;
var aMatchRecency = a.match.recency,
bMatchRecency = b.match.recency, aLength = aMatchRecency.length - 1, bLength = bMatchRecency.length - 1;
while (aMatchRecency[i] === bMatchRecency[i] && i < aLength && i < bLength && i++) {
}
var ret = aMatchRecency[i] - bMatchRecency[i];
if (!ret) {
ret = aLength - bLength;
}
return ret;
}
function activationRecency(a, b) {
return a.recency - b.recency;
}
var strategies = {
salience: salience,
bucketCounter: bucketCounter,
factRecency: factRecency,
activationRecency: activationRecency
};
exports.strategies = strategies;
exports.strategy = function (strats) {
strats = map(strats, function (s) {
return strategies[s];
});
var stratsLength = strats.length;
return function (a, b) {
var i = -1, ret = 0;
var equal = (a === b) || (a.name === b.name && a.hashCode === b.hashCode);
if (!equal) {
while (++i < stratsLength && !ret) {
ret = strats[i](a, b);
}
ret = ret > 0 ? 1 : -1;
}
return ret;
};
};
},{"./extended":12}],8:[function(require,module,exports){
"use strict";
var extd = require("./extended"),
deepEqual = extd.deepEqual,
merge = extd.merge,
instanceOf = extd.instanceOf,
filter = extd.filter,
declare = extd.declare,
constraintMatcher;
var id = 0;
var Constraint = declare({
type: null,
instance: {
constructor: function (constraint) {
if (!constraintMatcher) {
constraintMatcher = require("./constraintMatcher");
}
this.id = id++;
this.constraint = constraint;
extd.bindAll(this, ["assert"]);
},
"assert": function () {
throw new Error("not implemented");
},
getIndexableProperties: function () {
return [];
},
equal: function (constraint) {
return instanceOf(constraint, this._static) && this.get("alias") === constraint.get("alias") && extd.deepEqual(this.constraint, constraint.constraint);
},
getters: {
variables: function () {
return [this.get("alias")];
}
}
}
});
Constraint.extend({
instance: {
type: "object",
constructor: function (type) {
this._super([type]);
},
"assert": function (param) {
return param instanceof this.constraint || param.constructor === this.constraint;
},
equal: function (constraint) {
return instanceOf(constraint, this._static) && this.constraint === constraint.constraint;
}
}
}).as(exports, "ObjectConstraint");
var EqualityConstraint = Constraint.extend({
instance: {
type: "equality",
constructor: function (constraint, options) {
this._super([constraint]);
options = options || {};
this.pattern = options.pattern;
this._matcher = constraintMatcher.getMatcher(constraint, options, true);
},
"assert": function (values) {
return this._matcher(values);
}
}
}).as(exports, "EqualityConstraint");
EqualityConstraint.extend({instance: {type: "inequality"}}).as(exports, "InequalityConstraint");
EqualityConstraint.extend({instance: {type: "comparison"}}).as(exports, "ComparisonConstraint");
Constraint.extend({
instance: {
type: "equality",
constructor: function () {
this._super([
[true]
]);
},
equal: function (constraint) {
return instanceOf(constraint, this._static) && this.get("alias") === constraint.get("alias");
},
"assert": function () {
return true;
}
}
}).as(exports, "TrueConstraint");
var ReferenceConstraint = Constraint.extend({
instance: {
type: "reference",
constructor: function (constraint, options) {
this.cache = {};
this._super([constraint]);
options = options || {};
this.values = [];
this.pattern = options.pattern;
this._options = options;
this._matcher = constraintMatcher.getMatcher(constraint, options, false);
},
"assert": function (fact, fh) {
try {
return this._matcher(fact, fh);
} catch (e) {
throw new Error("Error with evaluating pattern " + this.pattern + " " + e.message);
}
},
merge: function (that) {
var ret = this;
if (that instanceof ReferenceConstraint) {
ret = new this._static([this.constraint, that.constraint, "and"], merge({}, this._options, this._options));
ret._alias = this._alias || that._alias;
ret.vars = this.vars.concat(that.vars);
}
return ret;
},
equal: function (constraint) {
return instanceOf(constraint, this._static) && extd.deepEqual(this.constraint, constraint.constraint);
},
getters: {
variables: function () {
return this.vars;
},
alias: function () {
return this._alias;
}
},
setters: {
alias: function (alias) {
this._alias = alias;
this.vars = filter(constraintMatcher.getIdentifiers(this.constraint), function (v) {
return v !== alias;
});
}
}
}
}).as(exports, "ReferenceConstraint");
ReferenceConstraint.extend({
instance: {
type: "reference_equality",
op: "eq",
getIndexableProperties: function () {
return constraintMatcher.getIndexableProperties(this.constraint);
}
}
}).as(exports, "ReferenceEqualityConstraint")
.extend({instance: {type: "reference_inequality", op: "neq"}}).as(exports, "ReferenceInequalityConstraint")
.extend({instance: {type: "reference_gt", op: "gt"}}).as(exports, "ReferenceGTConstraint")
.extend({instance: {type: "reference_gte", op: "gte"}}).as(exports, "ReferenceGTEConstraint")
.extend({instance: {type: "reference_lt", op: "lt"}}).as(exports, "ReferenceLTConstraint")
.extend({instance: {type: "reference_lte", op: "lte"}}).as(exports, "ReferenceLTEConstraint");
Constraint.extend({
instance: {
type: "hash",
constructor: function (hash) {
this._super([hash]);
},
equal: function (constraint) {
return extd.instanceOf(constraint, this._static) && this.get("alias") === constraint.get("alias") && extd.deepEqual(this.constraint, constraint.constraint);
},
"assert": function () {
return true;
},
getters: {
variables: function () {
return this.constraint;
}
}
}
}).as(exports, "HashConstraint");
Constraint.extend({
instance: {
constructor: function (constraints, options) {
this.type = "from";
this.constraints = constraintMatcher.getSourceMatcher(constraints, (options || {}), true);
extd.bindAll(this, ["assert"]);
},
equal: function (constraint) {
return instanceOf(constraint, this._static) && this.get("alias") === constraint.get("alias") && deepEqual(this.constraints, constraint.constraints);
},
"assert": function (fact, fh) {
return this.constraints(fact, fh);
},
getters: {
variables: function () {
return this.constraint;
}
}
}
}).as(exports, "FromConstraint");
Constraint.extend({
instance: {
constructor: function (func, options) {
this.type = "custom";
this.fn = func;
this.options = options;
extd.bindAll(this, ["assert"]);
},
equal: function (constraint) {
return instanceOf(constraint, this._static) && this.fn === constraint.constraint;
},
"assert": function (fact, fh) {
return this.fn(fact, fh);
}
}
}).as(exports, "CustomConstraint");
},{"./constraintMatcher":9,"./extended":12}],9:[function(require,module,exports){
"use strict";