forked from jruizgit/rules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestsamples.js
1173 lines (958 loc) · 39.2 KB
/
testsamples.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
'use strict';
var d = require('../libjs/durable');
d.ruleset('match1', function() {
whenAll: m.subject.matches('a+(bc|de?)*')
run: console.log('match 1 ' + m.subject)
});
d.post('match1', {id: 1, sid: 1, subject: 'abcbc'});
d.post('match1', {id: 2, sid: 1, subject: 'addd'});
d.post('match1', {id: 3, sid: 1, subject: 'ddd'}, function(err, state){ console.log('match 1 ' + err.message)});
d.post('match1', {id: 4, sid: 1, subject: 'adee'}, function(err, state){ console.log('match 1 ' + err.message)});
d.ruleset('match2', function() {
whenAll: m.subject.matches('[0-9]+-[a-z]+')
run: console.log('match 2 ' + m.subject)
});
d.post('match2', {id: 1, sid: 1, subject: '0-a'});
d.post('match2', {id: 2, sid: 1, subject: '43-bcd'});
d.post('match2', {id: 3, sid: 1, subject: 'a-3'}, function(err, state){ console.log('match 2 ' + err.message)});
d.post('match2', {id: 4, sid: 1, subject: '0a'}, function(err, state){ console.log('match 2 ' + err.message)});
d.ruleset('match3', function() {
whenAll: m.subject.matches('[adfzx-]+[5678]+')
run: console.log('match 3 ' + m.subject)
});
d.post('match3', {id: 1, sid: 1, subject: 'ad-567'});
d.post('match3', {id: 2, sid: 1, subject: 'ac-78'}, function(err, state){ console.log('match 3 ' + err.message)});
d.post('match3', {id: 3, sid: 1, subject: 'zx12'}, function(err, state){ console.log('match 3 ' + err.message)});
d.post('match3', {id: 4, sid: 1, subject: 'xxxx-8888'});
d.ruleset('match4', function() {
whenAll: m.subject.matches('[z-a]+-[9-0]+')
run: console.log('match 4 ' + m.subject)
});
d.post('match4', {id: 1, sid: 1, subject: 'ed-56'});
d.post('match4', {id: 2, sid: 1, subject: 'ac-ac'}, function(err, state){ console.log('match 4 ' + err.message)});
d.post('match4', {id: 3, sid: 1, subject: 'az-90'});
d.post('match4', {id: 4, sid: 1, subject: 'AZ-90'}, function(err, state){ console.log('match 4 ' + err.message)});
d.ruleset('match5', function() {
whenAll: m.subject.matches('.+b')
run: console.log('match 5 ' + m.subject)
});
d.post('match5', {id: 1, sid: 1, subject: 'ab'});
d.post('match5', {id: 2, sid: 1, subject: 'adfb0'}, function(err, state){ console.log('match 5 ' + err.message)});
d.post('match5', {id: 3, sid: 1, subject: 'abbbcd'}, function(err, state){ console.log('match 5 ' + err.message)});
d.post('match5', {id: 4, sid: 1, subject: 'xybz.b'});
d.ruleset('match6', function() {
whenAll: m.subject.matches('[a-z]{3}')
run: console.log('match 6 ' + m.subject)
});
d.post('match6', {id: 1, sid: 1, subject: 'ab'}, function(err, state){ console.log('match 6 ' + err.message)});
d.post('match6', {id: 2, sid: 1, subject: 'bbb'});
d.post('match6', {id: 3, sid: 1, subject: 'azc'});
d.post('match6', {id: 4, sid: 1, subject: 'abbbcd'}, function(err, state){ console.log('match 6 ' + err.message)});
d.ruleset('match7', function() {
whenAll: m.subject.matches('[a-z]{2,4}')
run: console.log('match 7 ' + m.subject)
});
d.post('match7', {id: 1, sid: 1, subject: 'ar'});
d.post('match7', {id: 2, sid: 1, subject: 'bxbc'});
d.post('match7', {id: 3, sid: 1, subject: 'a'}, function(err, state){ console.log('match 7 ' + err.message)});
d.post('match7', {id: 4, sid: 1, subject: 'abbbc'}, function(err, state){ console.log('match 7 ' + err.message)});
d.ruleset('match8', function() {
whenAll: m.subject.matches('(a|bc){2,}')
run: console.log('match 8 ' + m.subject)
});
d.post('match8', {id: 1, sid: 1, subject: 'aa'});
d.post('match8', {id: 2, sid: 1, subject: 'abcbc'});
d.post('match8', {id: 3, sid: 1, subject: 'bc'}, function(err, state){ console.log('match 8 ' + err.message)});
d.post('match8', {id: 4, sid: 1, subject: 'a'}, function(err, state){ console.log('match 8 ' + err.message)});
d.ruleset('match9', function() {
whenAll: m.subject.matches('a{1,2}')
run: console.log('match 9 ' + m.subject)
});
d.post('match9', {id: 1, sid: 1, subject: 'a'});
d.post('match9', {id: 2, sid: 1, subject: 'b'}, function(err, state){ console.log('match 9 ' + err.message)});
d.post('match9', {id: 3, sid: 1, subject: 'aa'});
d.post('match9', {id: 4, sid: 1, subject: 'aaa'}, function(err, state){ console.log('match 9 ' + err.message)});
d.ruleset('match10', function() {
whenAll: m.subject.matches('cba{1,}')
run: console.log('match 10 ' + m.subject)
});
d.post('match10', {id: 1, sid: 1, subject: 'cba'});
d.post('match10', {id: 2, sid: 1, subject: 'b'}, function(err, state){ console.log('match 10 ' + err.message)});
d.post('match10', {id: 3, sid: 1, subject: 'cb'}, function(err, state){ console.log('match 10 ' + err.message)});
d.post('match10', {id: 4, sid: 1, subject: 'cbaaa'});
d.ruleset('match11', function() {
whenAll: m.subject.matches('cba{0,}')
run: console.log('match 11 ' + m.subject)
});
d.post('match11', {id: 1, sid: 1, subject: 'cb'});
d.post('match11', {id: 2, sid: 1, subject: 'b'}, function(err, state){ console.log('match 11 ' + err.message)});
d.post('match11', {id: 3, sid: 1, subject: 'cbab'}, function(err, state){ console.log('match 11 ' + err.message)});
d.post('match11', {id: 4, sid: 1, subject: 'cbaaa'});
d.ruleset('match12', function() {
whenAll: m.user.matches('[a-z0-9_-]{3,16}')
run: console.log('match 12 user ' + m.user)
});
d.post('match12', {id: 1, sid: 1, user: 'git-9'});
d.post('match12', {id: 2, sid: 1, user: 'git_9'});
d.post('match12', {id: 3, sid: 1, user: 'GIT-9'}, function(err, state){ console.log('match 12 ' + err.message)});
d.post('match12', {id: 4, sid: 1, user: '01234567890123456789'}, function(err, state){ console.log('match 12 ' + err.message)});
d.post('match12', {id: 5, sid: 1, user: 'gi'}, function(err, state){ console.log('match 12 ' + err.message)});
d.ruleset('match13', function() {
whenAll: m.number.matches('#?([a-f0-9]{6}|[a-f0-9]{3})')
run: console.log('match 13 hex ' + m.number)
});
d.post('match13', {id: 1, sid: 1, number: 'fff'});
d.post('match13', {id: 2, sid: 1, number: '#45'}, function(err, state){ console.log('match 13 ' + err.message)});
d.post('match13', {id: 3, sid: 1, number: 'abcdex'}, function(err, state){ console.log('match 13 ' + err.message)});
d.post('match13', {id: 4, sid: 1, number: '#54ba45'});
d.post('match13', {id: 5, sid: 1, number: '#9999'}, function(err, state){ console.log('match 13 ' + err.message)});
d.ruleset('match14', function() {
whenAll: m.alias.matches('([a-z0-9_.-]+)@([0-9a-z.-]+)%.([a-z]{2,6})')
run: console.log('match 14 email ' + m.alias)
});
d.post('match14', {id: 1, sid: 1, alias: '[email protected]'});
d.post('match14', {id: 2, sid: 1, alias: 'j#[email protected]'}, function(err, state){ console.log('match 14 ' + err.message)});
d.post('match14', {id: 3, sid: 1, alias: '[email protected]'});
d.post('match14', {id: 4, sid: 1, alias: 'dd@dd.'}, function(err, state){ console.log('match 14 ' + err.message)});
d.post('match14', {id: 5, sid: 1, alias: '[email protected]'}, function(err, state){ console.log('match 14 ' + err.message)});
d.ruleset('match15', function() {
whenAll: m.url.matches('(https?://)?([%da-z.-]+)%.[a-z]{2,6}(/[%w_.-]+/?)*')
run: console.log('match 15 url ' + m.url)
});
d.post('match15', {id: 1, sid: 1, url: 'https://github.com'});
d.post('match15', {id: 2, sid: 1, url: 'http://github.com/jruizgit/rul!es'}, function(err, state){ console.log('match 15 ' + err.message)});
d.post('match15', {id: 3, sid: 1, url: 'https://github.com/jruizgit/rules/blob/master/docs/rb/reference.md'});
d.post('match15', {id: 4, sid: 1, url: '//rules'}, function(err, state){ console.log('match 15 ' + err.message)});
d.post('match15', {id: 5, sid: 1, url: 'https://github.c/jruizgit/rules'}, function(err, state){ console.log('match 15 ' + err.message)});
d.ruleset('match16', function() {
whenAll: m.ip.matches('((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)%.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)')
run: console.log('match 16 ip ' + m.ip)
});
d.post('match16', {id: 1, sid: 1, ip: '73.60.124.136'});
d.post('match16', {id: 2, sid: 1, ip: '256.60.124.136'}, function(err, state){ console.log('match 16 ' + err.message)});
d.post('match16', {id: 3, sid: 1, ip: '250.60.124.256'}, function(err, state){ console.log('match 16 ' + err.message)});
d.post('match16', {id: 4, sid: 1, ip: '73.60.124'}, function(err, state){ console.log('match 16 ' + err.message)});
d.post('match16', {id: 5, sid: 1, ip: '127.0.0.1'});
d.ruleset('match17', function() {
whenAll: m.subject.matches('([ab]?){30}%w{30}')
run: console.log('match 17 ' + m.subject)
});
d.post('match17', {id: 1, sid: 1, subject: 'aaaaaaaaab'}, function(err, state){ console.log('match 17 ' + err.message)});
d.post('match17', {id: 2, sid: 1, subject: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaab'});
d.post('match17', {id: 3, sid: 1, subject: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab'});
d.post('match17', {id: 4, sid: 1, subject: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab'}, function(err, state){ console.log('match 17 ' + err.message)});
d.ruleset('match18', function() {
whenAll: m.subject.matches('%w+')
run: console.log('match 18 ' + m.subject)
});
d.post('match18', {id: 1, sid: 1, subject: 'aAzZ5678'});
d.post('match18', {id: 2, sid: 1, subject: '789Az'});
d.post('match18', {id: 3, sid: 1, subject: 'aA`89'}, function(err, state){ console.log('match 18 ' + err.message)});
d.ruleset('match19', function() {
whenAll: m.subject.matches('%u+')
run: console.log('match 19 upper ' + m.subject)
whenAll: m.subject.matches('%l+')
run: console.log('match 19 lower ' + m.subject)
});
d.post('match19', {id: 1, sid: 1, subject: 'ABCDZ'});
d.post('match19', {id: 2, sid: 1, subject: 'W\u00D2\u00D3'});
d.post('match19', {id: 3, sid: 1, subject: 'abcz'});
d.post('match19', {id: 4, sid: 1, subject: 'wßÿ'});
d.post('match19', {id: 4, sid: 1, subject: 'AbcZ'}, function(err, state){ console.log('match 19 ' + err.message)});
d.ruleset('match20', function() {
whenAll: m.subject.matches('[^ABab]+')
run: console.log('match 20 ' + m.subject)
});
d.post('match20', {id: 1, sid: 1, subject: 'ABCDZ'}, function(err, state){ console.log('match 20 ' + err.message)});
d.post('match20', {id: 2, sid: 1, subject: '.;:<>'});
d.post('match20', {id: 3, sid: 1, subject: '^&%()'});
d.post('match20', {id: 4, sid: 1, subject: 'abcz'}, function(err, state){ console.log('match 20 ' + err.message)});
d.ruleset('match21', function() {
whenAll: m.subject.matches('hello.*')
run: console.log('match 21 starts with hello: ' + m.subject)
whenAll: m.subject.matches('.*hello')
run: console.log('match 21 ends with hello: ' + m.subject)
whenAll: m.subject.matches('.*hello.*')
run: console.log('match 21 contains hello: ' + m.subject)
whenAll: m.subject.matches('.*(error while abc).*')
run: console.log('match 21 error while: ' + m.subject)
whenAll: m.subject.matches('.*error error.*')
run: console.log('match 21 error: ' + m.subject)
});
d.assert('match21', {subject: 'hello world'});
d.assert('match21', {subject: 'world hello'});
d.assert('match21', {subject: 'world hello hello'});
d.assert('match21', {subject: 'has hello string'});
d.assert('match21', {subject: 'error while abc'});
d.assert('match21', {subject: 'error error error'});
d.assert('match21', {subject: 'does not match'}, function(err, state){ console.log('match 21 ' + err.message)});
d.assert('match21', {subject: 5}, function(err, state){ console.log('match 21 ' + err.message)});
d.ruleset('coerce', function() {
whenAll: m.subject.matches('10.500000')
run: console.log('coerce-> matches: ' + m.subject)
whenAll: m.subject.matches('5')
run: console.log('coerce-> matches: ' + m.subject)
whenAll: m.subject.matches('false')
run: console.log('coerce-> matches: ' + m.subject)
whenAll: m.subject.matches('nil')
run: console.log('coerce-> matches: nil')
});
d.assert('coerce', {subject: 10.5 });
d.assert('coerce', {subject: 5 });
d.assert('coerce', {subject: false });
d.assert('coerce', {subject: null });
try {
d.ruleset('invalid1', function() {
whenAll: m.subject.matches('*')
run: console.log('should not work: ' + m.subject)
});
d.assert('invalid1', {subject: 'hello world' });
} catch (err) {
console.log('invalid1 expected: ' + err.message);
}
try {
d.ruleset('invalid2', function() {
whenAll: m.subject.matches('.**')
run: console.log('should not work: ' + m.subject)
});
d.assert('invalid2', {subject: 'hello world' });
} catch (err) {
console.log('invalid2 expected: ' + err.message);
}
try {
d.ruleset('invalid3', function() {
whenAll: m.subject.matches('.?*')
run: console.log('should not work: ' + m.subject)
});
d.assert('invalid3', {subject: 'hello world' });
} catch (err) {
console.log('invalid3 expected: ' + err.message);
}
d.ruleset('match22', function() {
whenAll: m.subject.imatches('hello.*')
run: console.log('match 22 starts with case insensitive hello: ' + m.subject)
whenAll: m.subject.imatches('.*hello')
run: console.log('match 22 ends with case insensitive hello: ' + m.subject)
whenAll: m.subject.imatches('.*Hello.*')
run: console.log('match 22 contains case insensitive hello: ' + m.subject)
});
d.assert('match22', {id: 1, sid: 1, subject: 'HELLO world'});
d.assert('match22', {id: 2, sid: 1, subject: 'world hello'});
d.assert('match22', {id: 3, sid: 1, subject: 'world hello hi'});
d.assert('match22', {id: 4, sid: 1, subject: 'has Hello string'});
d.assert('match22', {id: 5, sid: 1, subject: 'does not match'}, function(err, state){ console.log('match 22 ' + err.message)});
d.ruleset('test', function() {
// antecedent
whenAll: m.subject == 'World'
// consequent
run: console.log('Hello ' + m.subject)
});
d.post('test', {subject: 'World'});
d.ruleset('mixedNested', function() {
whenAll: m['field1'].field2['field3'] == 8
run: console.log("mixedNested ok")
});
d.post('mixedNested',{field1 : {field2 : {field3 : 8}}});
d.ruleset('null', function() {
whenAll: m.subject == null
run: console.log('null passed')
});
d.post('null', {subject: null});
d.post('null', {subject: 'something'}, function(err, state) {console.log('null expected error: ' + err.message)});
d.ruleset('risk0', function() {
whenAll: {
first = m.t == 'purchase'
second = m.location != first.location
}
run: console.log('risk0 fraud detected ->' + first.location + ', ' + second.location)
});
d.post('risk0', {t: 'purchase', location: 'US'});
console.log('risk0 events');
console.log(d.getPendingEvents('risk0'));
d.post('risk0', {t: 'purchase', location: 'CA'});
d.post('risk0', {t: 'purchase', location: 'US'});
d.ruleset('counter_or', function() {
whenAll: {
first = +m.counter
second = m.countersValid == true && (m.primeCount == first.counter || m.secondCount == first.counter)
}
run: console.log('counter_or match')
});
d.assert('counter_or', { counter: 1})
d.assert('counter_or', { primeCount: 2, secondCount: 1, countersValid: true})
d.assert('counter_or', { primeCount: 2, secondCount: 2, countersValid: true})
d.ruleset('indistinct', function() {
whenAll: {
first = m.amount > 10
second = m.amount > first.amount * 2
third = m.amount > (first.amount + second.amount) / 2
}
distinct: false
run: {
console.log('indistinct detected -> ' + first.amount);
console.log(' -> ' + second.amount);
console.log(' -> ' + third.amount);
}
});
d.post('indistinct', {amount: 200});
d.post('indistinct', {amount: 500});
d.post('indistinct', {amount: 1000});
d.ruleset('distinct', function() {
whenAll: {
first = m.amount > 10
second = m.amount > first.amount * 2
third = m.amount > (first.amount + second.amount) / 2
}
run: {
console.log('distinct detected -> ' + first.amount);
console.log(' -> ' + second.amount);
console.log(' -> ' + third.amount);
}
});
d.post('distinct', {amount: 50});
d.post('distinct', {amount: 200});
d.post('distinct', {amount: 251});
d.ruleset('bracket', function() {
whenAll: {
first = m['amount'] > 10
second = m['amount'] > first['amount'] * 2
third = m['amount'] > (first['amount'] + second['amount']) / 2
}
run: {
console.log('bracket detected -> ' + first['amount']);
console.log(' -> ' + second['amount']);
console.log(' -> ' + third['amount']);
}
});
d.post('bracket', {amount: 50});
d.post('bracket', {amount: 200});
d.post('bracket', {amount: 251});
d.ruleset('expense0', function() {
whenAll: m.subject == 'approve' || m.subject == 'ok'
run: console.log('expense0 Approved');
});
d.post('expense0', { subject: 'approve' }, function(err, state){});
d.ruleset('match', function() {
whenAll: m.url.matches('(https?://)?([%da-z.-]+)%.[a-z]{2,6}(/[%w_.-]+/?)*')
run: console.log('match url ' + m.url)
});
d.post('match', {url: 'https://github.com'});
d.post('match', {url: 'http://github.com/jruizgit/rul!es'}, function(err, state){ console.log('match: ' + err.message)});
d.post('match', {url: 'https://github.com/jruizgit/rules/reference.md'});
d.post('match', {url: '//rules'}, function(err, state){ console.log('match: ' + err.message)});
d.post('match', {url: 'https://github.c/jruizgit/rules'}, function(err, state){ console.log('match: ' + err.message)});
d.ruleset('strings', function() {
whenAll: m.subject.matches('hello.*')
run: console.log('string starts with hello: ' + m.subject)
whenAll: m.subject.imatches('.*hello')
run: console.log('string ends with hello: ' + m.subject)
whenAll: m.subject.imatches('.*hello.*')
run: console.log('string contains hello (case insensitive): ' + m.subject)
});
d.assert('strings', { subject: 'HELLO world' });
d.assert('strings', { subject: 'world hello' });
d.assert('strings', { subject: 'hello hi' });
d.assert('strings', { subject: 'has Hello string' });
try {
d.assert('strings', { subject: 'does not match' });
} catch (err) {
console.log('strings: ' + err.message);
}
d.ruleset('risk2_0', function() {
whenAll: {
first = m.t == 'deposit'
none(m.t == 'balance')
third = m.t == 'withdrawal'
fourth = m.t == 'chargeback'
}
run: console.log('risk2_0 fraud detected sid: ' + s.sid + ' result: ' + first.t + ' ' + third.t + ' ' + fourth.t);
});
d.assert('risk2_0', {t: 'deposit'});
d.assert('risk2_0', {t: 'withdrawal'});
d.assert('risk2_0', {t: 'chargeback'});
d.assert('risk2_0', {sid: 1, t: 'balance'});
d.assert('risk2_0', {sid: 1, t: 'deposit'});
d.assert('risk2_0', {sid: 1, t: 'withdrawal'});
d.assert('risk2_0', {sid: 1, t: 'chargeback'});
d.retract('risk2_0', {sid: 1, t: 'balance'});
d.assert('risk2_0', {sid: 2, t: 'deposit'});
d.assert('risk2_0', {sid: 2, t: 'withdrawal'});
d.assert('risk2_0', {sid: 2, t: 'chargeback'});
d.assert('risk2_0', {sid: 2, t: 'balance'});
d.ruleset('expense2', function() {
whenAll: m.amount < 100
count: 3
run: console.log('expense2 approved ' + JSON.stringify(m));
whenAll: {
expense = m.amount >= 100
approval = m.review == true
}
cap: 2
run: console.log('expense2 rejected ' + JSON.stringify(m));
});
d.postEvents('expense2', [{ amount: 10 },
{ amount: 20 },
{ amount: 100 },
{ amount: 30 },
{ amount: 200 },
{ amount: 400 }]);
d.assert('expense2', { review: true })
d.ruleset('flow0', function() {
whenAll: s.state == 'start'
run: {
s.state = 'next';
console.log('flow0 start');
}
whenAll: s.state == 'next'
run: {
s.state = 'last';
console.log('flow0 next');
}
whenAll: s.state == 'last'
run: {
s.state = 'end';
console.log('flow0 last');
deleteState();
}
});
d.updateState('flow0', {state: 'start'});
d.ruleset('flow', function() {
whenAll: m.action == 'start'
run: throw 'Unhandled Exception!'
whenAll: +s.exception
run: {
console.log('flow expected ' + s.exception);
delete(s.exception);
}
whenStart: {
}
});
d.post('flow', { action: 'start' });
d.statechart('expense3', function() {
input: {
to: 'denied'
whenAll: m.subject == 'approve' && m.amount > 1000
run: s.status = 'expense3: Denied amount: ' + m.amount
to: 'pending'
whenAll: m.subject == 'approve' && m.amount <= 1000
run: s.status = 'expense3: sid ' + m.sid + ' requesting approve amount: ' + m.amount
}
pending: {
to: 'approved'
whenAll: m.subject == 'approved'
run: s.status = 'expense3: Expense approved for sid ' + m.sid
to: 'denied'
whenAll: m.subject == 'denied'
run: s.status = 'expense3: Expense denied for sid ' + m.sid
}
denied: {}
approved: {}
});
console.log(d.post('expense3', { subject: 'approve', amount: 100 }).status);
console.log(d.post('expense3', { subject: 'approved' }).status);
console.log(d.post('expense3', { sid: 1, subject: 'approve', amount: 100 }).status);
console.log(d.post('expense3', { sid: 1, subject: 'denied' }).status);
console.log(d.post('expense3', { sid: 2, subject: 'approve', amount: 10000 }).status);
d.statechart('worker', function() {
work: {
enter: {
to: 'process'
whenAll: m.subject == 'enter'
run: {
console.log('worker start process');
console.log(getPendingEvents());
}
}
process: {
to: 'process'
whenAll: m.subject == 'continue'
run: console.log('worker continue processing')
}
to: 'canceled'
pri: 1
whenAll: m.subject == 'cancel'
run: console.log('worker cancel process')
}
canceled: {}
});
d.post('worker', { subject: 'enter' });
d.post('worker', { id: 1, subject: 'continue' });
d.post('worker', { id: 2, subject: 'continue' });
d.post('worker', { subject: 'cancel' });
d.flowchart('expense4', function() {
input: {
request: m.subject == 'approve' && m.amount <= 1000
deny: m.subject == 'approve' && m.amount > 1000
}
request: {
run: console.log('expense4: Requesting approve for ' + m.sid + ' for ' + m.amount)
approve: m.subject == 'approved'
deny: m.subject == 'denied'
self: m.subject == 'retry'
}
approve: {
run: console.log('expense4: Expense approved for ' + m.sid)
}
deny: {
run: console.log('expense4: Expense denied for ' + m.sid)
}
});
d.post('expense4', { subject: 'approve', amount: 100 });
d.post('expense4', { subject: 'retry' });
d.post('expense4', { subject: 'approved' });
d.post('expense4', {sid: 1, subject: 'approve', amount: 100});
d.post('expense4', {sid: 1, subject: 'denied'});
d.post('expense4', {sid: 2, subject: 'approve', amount: 10000});
d.ruleset('bookstore', function() {
// this rule will trigger for events with status
whenAll: +m.status
run: console.log('bookstore reference ' + m.reference + ' status ' + m.status)
whenAll: +m.name
run: {
console.log('bookstore added: ' + m.name);
}
// this rule will be triggered when the fact is retracted
whenAll: none(+m.name)
run: console.log('bookstore no books');
});
// will not throw because the fact assert was successful
d.assert('bookstore', {
name: 'The new book',
seller: 'bookstore',
reference: '75323',
price: 500
});
// will throw MessageObservedError because the fact has already been asserted
try {
d.assert('bookstore', {
reference: '75323',
name: 'The new book',
price: 500,
seller: 'bookstore'
});
} catch (err) {
console.log('bookstore: ' + err.message);
}
// will not throw because a new event is being posted
d.post('bookstore', {
reference: '75323',
status: 'Active'
});
// will not throw because a new event is being posted
d.post('bookstore', {
reference: '75323',
status: 'Active'
});
d.ruleset('attributes', function() {
whenAll: m.amount < 300
pri: 3
run: console.log('attributes P3 ->' + m.amount);
whenAll: m.amount < 200
pri: 2
run: console.log('attributes P2 ->' + m.amount);
whenAll: m.amount < 100
pri: 1
run: {
console.log('attributes P1 ->' + m.amount);
console.log(getFacts());
}
});
d.assert('attributes', {amount: 50});
d.assert('attributes', {amount: 150});
d.assert('attributes', {amount: 250});
d.ruleset('expense5', function() {
// use the '.' notation to match properties in nested objects
whenAll: {
bill = m.t == 'bill' && m.invoice.amount > 50
account = m.t == 'account' && m.payment.invoice.amount == bill.invoice.amount
}
run: {
console.log('expense5 bill amount ->' + bill.invoice.amount);
console.log('expense5 account payment amount ->' + account.payment.invoice.amount);
}
});
// one level of nesting
d.post('expense5', {t: 'bill', invoice: {amount: 100}});
// two levels of nesting
d.post('expense5', {t: 'account', payment: {invoice: {amount: 100}}});
d.ruleset('animal', function() {
whenAll: {
first = m.predicate == 'eats' && m.object == 'flies'
m.predicate == 'lives' && m.object == 'water' && m.subject == first.subject
}
run: assert({ subject: first.subject, predicate: 'is', object: 'frog' })
whenAll: {
first = m.predicate == 'eats' && m.object == 'flies'
m.predicate == 'lives' && m.object == 'land' && m.subject == first.subject
}
run: assert({ subject: first.subject, predicate: 'is', object: 'chameleon' })
whenAll: m.predicate == 'eats' && m.object == 'worms'
run: assert({ subject: m.subject, predicate: 'is', object: 'bird' })
whenAll: m.predicate == 'is' && m.object == 'frog'
run: assert({ subject: m.subject, predicate: 'is', object: 'green'})
whenAll: m.predicate == 'is' && m.object == 'chameleon'
run: assert({ subject: m.subject, predicate: 'is', object: 'green'})
whenAll: m.predicate == 'is' && m.object == 'bird'
run: assert({ subject: m.subject, predicate: 'is', object: 'black'})
whenAll: +m.subject
run: console.log('animal fact: ' + m.subject + ' ' + m.predicate + ' ' + m.object)
});
d.assert('animal', { subject: 'Kermit', predicate: 'eats', object: 'flies'});
d.assert('animal', { subject: 'Kermit', predicate: 'lives', object: 'water'});
d.assert('animal', { subject: 'Greedy', predicate: 'eats', object: 'flies'});
d.assert('animal', { subject: 'Greedy', predicate: 'lives', object: 'land'});
d.assert('animal', { subject: 'Tweety', predicate: 'eats', object: 'worms'});
console.log(d.getFacts('animal'));
d.ruleset('risk5', function() {
// compares properties in the same event, evaluated in alpha tree (node.js)
whenAll: {
m.debit > 2 * m.credit
}
run: { console.log('risk5 debit ' + m.debit + ' more than twice the credit ' + m.credit) }
// correlates two events, evaluated in the beta tree (redis)
whenAll: {
first = m.amount > 100
second = m.amount > first.amount + m.amount / 2
}
run: {
console.log('risk5 fraud detected -> ' + first.amount);
console.log('risk5 fraud detected -> ' + second.amount);
}
});
d.post('risk5', { debit: 220, credit: 100 });
d.post('risk5', { debit: 150, credit: 100 }, function(err, state) {console.log('risk5: ' + err.message)});
d.post('risk5', { amount: 200 });
d.post('risk5', { amount: 500 });
d.ruleset('risk6', function() {
// matching primitive array
whenAll: {
m.payments.allItems(item > 2000)
}
run: console.log('risk6 should not match ' + m.payments)
// matching primitive array
whenAll: {
m.payments.allItems(item > 1000)
}
run: console.log('risk6 fraud 1 detected ' + m.payments)
// matching object array
whenAll: {
m.payments.allItems(item.amount < 250 || item.amount >= 300)
}
run: console.log('risk6 fraud 2 detected ' + JSON.stringify(m.payments))
// pattern matching string array
whenAll: {
m.cards.anyItem(item.matches('three.*'))
}
run: console.log('risk6 fraud 3 detected ' + m.cards)
// matching nested arrays
whenAll: {
m.payments.anyItem(item.allItems(item < 100))
}
pri: 1
run: console.log('risk6 fraud 4 detected ' + JSON.stringify(m.payments))
whenAll: {
m.payments.anyItem(item > 100) && ~m.cash
}
run: console.log('risk6 fraud 5 detected ' + JSON.stringify(m))
whenAll: {
m.payments.anyItem(item.amount > 100 && ~item.cash)
}
run: console.log('risk6 fraud 6 detected ' + JSON.stringify(m))
whenAll: {
m.field == 1 && m.payments.allItems(item.allItems(item > 100 && item < 1000))
}
run: console.log('risk6 fraud 7 detected ' + JSON.stringify(m.payments))
whenAll: {
m.field == 1 && m.payments.allItems(item.anyItem(item > 100 || item < 50))
}
run: console.log('risk6 fraud 8 detected ' + JSON.stringify(m.payments))
whenAll: {
m.array.anyItem(+item.malformed)
}
run: console.log('risk6 fraud 9 detected ' + JSON.stringify(m));
whenAll: {
m.array1.anyItem(item.array2.anyItem(item.field21 == 1) && item.field == 8)
}
run: console.log('risk6 fraud 10 detected ' + JSON.stringify(m));
whenAll: {
m.array1.anyItem(item.field == 8 && item.array2.anyItem(item.field21 == 1))
}
run: console.log('risk6 fraud 11 detected ' + JSON.stringify(m));
whenAll: {
m.payments.anyItem((~item.field1 || ~item.field2) && item.field3 == 2)
}
run: console.log('risk6 fraud 12 detected ' + JSON.stringify(m.payments));
whenAll: {
m.a1.anyItem(~item.field1 && item.a2.anyItem(~item.field2))
}
run: console.log('risk6 fraud 13 detected ' + JSON.stringify(m));
whenAll: {
m.a4.anyItem(~item.field1 || item.field2 == 3)
}
run: console.log('risk6 fraud 14 detected ' + JSON.stringify(m))
whenAll: {
m.a5.anyItem(item.a6.anyItem(item.field21 == 1) && ~item.field)
}
run: console.log('risk6 fraud 15 detected ' + JSON.stringify(m))
whenAll: {
m.array.anyItem(item.field2 == m.field1)
}
run: console.log('risk6 fraud 16 detected ' + JSON.stringify(m))
whenAll: {
m.features.feature1.anyItem(item == 'a') || (m.features.feature2 == 'c')
}
run: console.log('risk6 fraud 17 detected ' + JSON.stringify(m.features))
});
d.post('risk6', { payments: [ 2500, 150, 450 ], cash: true }, function(err, state) {console.log('risk6: ' + err.message)});
d.post('risk6', { payments: [ 1500, 3500, 4500 ]});
d.post('risk6', { payments: [{ amount: 200 }, { amount: 300 }, { amount: 400 }]});
d.post('risk6', { cards: ['one card', 'two cards', 'three cards']});
d.post('risk6', { payments: [[ 10, 20, 30 ], [ 30, 40, 50 ], [ 10, 20 ]]});
d.post('risk6', { payments: [ 150, 50] });
d.post('risk6', { payments: [ 150, 50 ], cash: true }, function(err, state) {console.log('risk6: ' + err.message)});
d.post('risk6', { payments: [ { amount: 260 } ]});
d.post('risk6', { payments: [ { amount: 260, cash: true } ] }, function(err, state) {console.log('risk6: ' + err.message)});
d.post('risk6', { field: 1, payments: [ [ 200, 300 ], [ 150, 200 ] ]});
d.post('risk6', { field: 1, payments: [ [ 20, 80 ], [ 90, 180 ] ]});
d.post('risk6', { array:[{ tc: 0 }]}, function(err, state) {console.log('risk6: ' + err.message)});
d.post('risk6', { array:[{ malformed: 0 }]});
d.assert('risk6', { array1: [{ field: 8, array2: [{field21: 1}]}]})
d.assert('risk6', { array1: [{ field: 7, array2: [{field21: 1}]}]}, function(err, state) {console.log('risk6: ' + err.message)})
d.assert('risk6', { array1: [{ field: 8, array2: [{field21: 2}]}]}, function(err, state) {console.log('risk6: ' + err.message)})
d.post('risk6', { payments: [{field3: 2}]});
d.post('risk6', { payments: [{field2: 1, field3: 2}]});
d.post('risk6', { payments: [{field1: 1, field2: 2}]}, function(err, state) {console.log('risk6: ' + err.message)});
d.post('risk6', { payments: [{field1: 1, field2: 1}]}, function(err, state) {console.log('risk6: ' + err.message)});
d.post('risk6', { a1: [{ field: 8, a2: [{field: 1}]}]});
d.post('risk6', { a1: [{ field1: 8, a2: [{field2: 1}]}]}, function(err, state) {console.log('risk6: ' + err.message)});
d.post('risk6', { a1: [{ field1: 8, a2: [{field: 1}]}]}, function(err, state) {console.log('risk6: ' + err.message)});
d.post('risk6', { a1: [{ field: 8, a2: [{field2: 1}]}]}, function(err, state) {console.log('risk6: ' + err.message)});
d.post('risk6', { a4: [{field2: 2}]});
d.post('risk6', { a4: [{field2: 3, field1: true}]});
d.post('risk6', { a4: [{field2: 2, field1: true}]}, function(err, state) {console.log('risk6: ' + err.message)});
d.post('risk6', { a5: [{field: 7, a6: [{field21 : 1}]}]}, function(err, state) {console.log('risk6: ' + err.message)});
d.post('risk6', { a5: [{a6: [{field21 : 1}]}]});
d.post('risk6', { field1: 8, array: [{field2 : 9}]}, function(err, state) {console.log('risk6: ' + err.message)});
d.post('risk6', { field1: 8, array: [{field2 : 8}]});
d.post('risk6', { features: {feature1: ['a'], feature2: 'c'}})
d.post('risk6', { features: {feature1: [], feature2: 'c'}})
d.ruleset('expense1', function() {
whenAny: {
whenAll: {
first = m.subject == 'approve'
second = m.amount == 1000
}
whenAll: {
third = m.subject == 'jumbo'
fourth = m.amount == 10000
}
}
run: {
if (first) {
console.log('expense1 Approved ' + first.subject + ' ' + second.amount);
} else {
console.log('expense1 Approved ' + third.subject + ' ' + fourth.amount);
}
}
});
d.post('expense1', {subject: 'approve'});
d.post('expense1', {amount: 1000});
d.post('expense1', {subject: 'jumbo'});
d.post('expense1', {amount: 10000});
d.ruleset('expense6', function() {
whenAny: {
first = m.subject == 'approve' || m.subject == 'jumbo'
second = m.amount <= 1000
}
run: {
console.log('expense 6 Approved')
if (first) {
console.log('expense6 ' + first.subject);
}
if (second) {
console.log('expense6 ' + second.amount);
}
}
});
d.post('expense6', {subject: 'approve'});
d.assert('expense6', {amount: 1000});
d.assert('expense6', {subject: 'jumbo'});
d.post('expense6', {amount: 100});
d.ruleset('expense7', function() {
whenAll: {
whenAny: {
first = m.subject == 'approve'
second = m.amount == 1000
}
whenAny: {
third = m.subject == 'jumbo'
fourth = m.amount == 10000
}
}
run: {
console.log('expense 7 Approved')
if (first) {
console.log('expense7 ' + first.subject);
}
if (second) {
console.log('expense7 ' + second.amount);
}
if (third) {
console.log('expense7 ' + third.subject);
}
if (fourth) {
console.log('expense7 ' + fourth.amount);
}
}
});
d.post('expense7', {subject: 'approve'});
d.assert('expense7', {amount: 1000});
d.assert('expense7', {subject: 'jumbo'});
d.post('expense7', {amount: 10000});
d.ruleset('expense8', function() {
whenAny: {