-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjamrules.js
1729 lines (1573 loc) · 65.3 KB
/
jamrules.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
/**
* -----------------------------------------------------------------------------------------
* INTERSEL - 4 cité d'Hauteville - 75010 PARIS - France
* RCS PARIS 488 379 660 - NAF 721Z
*
* File : jamrules.js
*
*
* -----------------------------------------------------------------------------------------
* Modifications :
* - 20250224 - E.Podvin - V2.5.3 - use 'let' instead of 'var' to declare variables
* - 20250220 - E.Podvin - V2.5.2 - fix search in MatchPropertySearch
* - 20210509 - E.Podvin - V2.5.1 - fix on start/stopProcessing
* - 20210508 - E.Podvin - V2.5.0 - add startProcessing and stopProcessing
* - 20210507 - E.Podvin - V2.4.0
* - !!selectConfigurationPropertyValue is renamed checkConfigurationPropertyValue !!
* - selectConfigurationPropertyValue: select a value as a radio would do (unselecting other values)
* - checkConfigurationPropertyValue: set a value as a checkbox would do
* - resetConfigurationPropertyValues: reset all values of a property
* - 20210504 - E.Podvin - V2.3.0 -
* - add MatchPropertySearch test with wildcard
* - add resetConfigurationProperty
* - add '*' as a property value to match any value of a property of the configurator
* - 20210418 - E.Podvin - V2.2.0 - add addPropertyObjects
* - 20170402 - E.Podvin - V2.1.0 - adding new objects simplified + possibility to call matching rule functions
* - 20170331 - E.Podvin - V2.0.0 - Refactoring
* - 20170227 - E.Podvin - V1.0.0 - Creation
*
* -----------------------------------------------------------------------------------------
*
* @copyright Intersel 2017-2025
* @fileoverview :
* @see {@link https://github.com/intersel/jamrules}
* @author : Emmanuel Podvin - [email protected]
* @version : 2.5.3
* -----------------------------------------------------------------------------------------
*/
/**
* How to use it :
* ===============
*
* see README.md content or consult it on https://github.com/intersel/jamrules
*/
var jamrules = (function() {
/**
* @param ObjectProfiles
* @access private
* @abstract list of possible profiles available to all rules
* used as optimization to process filtering on object profiles rather than on each objects...
* a profile is defined by a list of entries [objectKey]:{propertiesSet:<apropertiesSet>,objectsList:[]}
* {
* <objectKey1>:{
* propertiesSet:
* {
* <propertyName1>:{
* type:<discreteValuesList>,
* <propertyValue1>:1|0,
* <propertyValue2>:1|0,
* ...
* },
* ... same as propertiesConfiguration definition
* <propertyName1>.<propertyValue1>:1|0,
* <propertyName2>.<propertyValue2>:1|0
* ....
* ],
* elementList:[ //element objects that share the same properties set
* ]
* },
* <objectKey2>:{
* ....
* }
*/
let ObjectProfiles = {};
/**
* private
* private jQuery object for iFSM when not given
*/
let aDefaultJqueryObj;
let randomSessionId = $.md5(Math.random());
if (!$('html').attr("id")) $('html').attr("id", "jamrules" + randomSessionId)
aDefaultJqueryObj = $("html");
/**
* @access private
* @abstract get the key to access to the object profile of an object
* @return a md5 key for a json object
*
*/
let getObjectProfileKey = function(anObject) {
return $.md5(JSON.stringify(anObject.propertiesSet));
}
/**
* @access private
* @abstract add a new object profile if the one defines by anObject does not exist
*
*/
let addObjectProfile = function(objectKey, anObject, anObjectProfiles) {
if (!anObjectProfiles) anObjectProfiles = ObjectProfiles;
if (!anObjectProfiles[objectKey]) anObjectProfiles[objectKey] = {
propertiesSet: anObject.propertiesSet,
objectsList: []
};
}
/**
* @access private
* @abstract add a new element to the element profiles array
*
*/
let addObjectToObjectProfilesArray = function(objectKey, anObject, anObjectProfiles) {
if (!anObjectProfiles) anObjectProfiles = ObjectProfiles;
anObjectProfiles[objectKey]['objectsList'].push(anObject);
}
/**
* @function public static _addObject
* @abstract add an object to the list of objects to test against rules
* the new object will be tested against all the called rules
* @param anObject: object
* {
* propertiesSet:
* [
* <propertyName1>.<propertyValue1>:true|false,
* <propertyName2>.<propertyValue2>:true|false
* ....
* ],
* matched:<function name to call when a rule will match for the object>,
* notmatched:<function name to call when there is a change but object does not match any rules>
* }
* @example
*/
let _addObject = function(anObject, anObjectProfiles) {
if (!anObject) {
alert("object is void...?");
return;
}
if ((!anObject.propertiesSet) || (Object.keys(anObject.propertiesSet).length === 0)) {
alert("object has no properties...?");
return;
}
let objectKey = getObjectProfileKey(anObject);
addObjectProfile(objectKey, anObject, anObjectProfiles);
addObjectToObjectProfilesArray(objectKey, anObject, anObjectProfiles);
};
/**
* @function public static _translateToJamrulesProperties
* @abstract translate an object with properties to a propertiessSet compliant with jamrules..
* @return anObject: object
* {
* propertiesSet:
* [
* <propertyName1>.<propertyValue1>:true|false,
* <propertyName2>.<propertyValue2>:true|false
* ....
* ]
* matched:<function name to call when a rule will match for the object>
* notmatched:<function name to call when there is a change but object does not match any rules>
* }
* remarks:
* - the property value of an object may be a numeric, string or an array of values (multiple values)
* @example
*/
let _translateToJamrulesProperties = function(anObject) {
let aPropertiesSet = {};
for (let aProperty in anObject) {
aPropertiesSet[aProperty] = {};
if (Array.isArray(anObject[aProperty]))
{
anObject[aProperty].map(aPropertyValue => aPropertiesSet[aProperty][aPropertyValue] = 1);
}
else
{
aPropertiesSet[aProperty][anObject[aProperty]] = 1;
}
aPropertiesSet[aProperty]['*'] = 1;
};
return aPropertiesSet;
};
let jamrulesClass = function(options) {
/**
* @param propertiesConfiguration
* @access private
* @abstract a configuration set of the properties defines by an array of [aPropertyName][aPropertyValue] = status (1|0)
* {
* <propertyName1>:{
* type:<discreteValuesList>,
* <propertyValue1>:1|0,
* <propertyValue2>:1|0,
* ...
* },
* <propertyName2>:{
* type:<intervalValues>, //not implemented
* min:<aValue>,
* max:<aValue>,
* interval:'<external|internal>'
* ...
* },
* <propertyName3>:{
* type:<supIntervalValues>, //not implemented
* min:<aValue>,
* ...
* },
* <propertyName4>:{
* type:<infIntervalValues>, //not implemented
* max:<aValue>,
* ...
* },
* ....
* }
* it is used to test against element profiles to identify the element profiles that match or not to this configuration
*/
let propertiesConfiguration = {};
/**
* @param matchRuleTemplate
* @abstract template to create new rule in the state definition of the rules engine
*/
let matchRuleTemplate = {
submachine: {
startTesting: {
enterState: {
next_state: 'ruleMatch', //default state that will end the process
},
},
ruleMatch: {
enterState: {
propagate_event: 'testRuleDone'
},
},
ruleDontMatch: {
enterState: {
propagate_event: 'testRuleDone'
}
},
DefaultState: {
catchEvent: 'startTestingRules',
startTestingRules: {
next_state: 'startTesting',
},
ruleDontMatch: {
next_state: 'ruleDontMatch',
prevent_bubble: true
},
testRuleDone: {
//dummy to catch the event without being caught by catchevent!
}
}
},
};
/**
* @param stateRuleTemplate
* @access private
* @abstract template of a rule described in a subengine
*/
let stateRuleTemplate = {
enterState: {
process_event_if: 'this.opts.jamrules.aMatchingTest()',
propagate_event_on_refused: 'ruleDontMatch',
propagate_event_on_localmachine: true,
next_state: '',
}
};
/**
* @param myRulesEngineStates
* @access private
* @abstract states definition of the rule engine that handles the rule testing process
*/
let myRulesEngineStates = {
TestRules: {
enterState: {
init_function: function() {
if (!this.opts.TestRules) this.opts.TestRules = {};
this.opts.TestRules.nbRulesTested = 0;
this.opts.TestRules.nbRulesToTest = Object.keys(this._stateDefinition.TestRules
.testRuleDone
.next_state_on_target
.submachines).length;
this.opts.jamrules.log('nb rules to test:' + this.opts.TestRules.nbRulesToTest,3);
},
},
delegate_machines: {
/***
* submachine for testing
/**/
/* for test - close this comment to activate the test
PriorityTestMatch1:
{
submachine:
{
startTesting:
{
enterState:
{
init_function: function(data,aEvent,aPropertyConfiguration){
this.opts.jamrules.log("-->"+this.currentState+' '+this.receivedEvent);
},
next_state:'testDisplayAll',
},
},
testDisplayAll:
{
enterState:
{
init_function: function(data,aEvent,aPropertyConfiguration){
this.opts.jamrules.log("-->"+this.currentState+' '+this.receivedEvent);
},
process_event_if: 'this.opts.jamrules.ConfigurationPropertySet("activities","all")',
propagate_event_on_refused:'ruleDontMatch',
propagate_event_on_localmachine:true,
next_state:'testPriorityExist',
},
},
testPriorityExist:
{
enterState:
{
init_function: function(data,aEvent,aPropertyConfiguration){
this.opts.jamrules.log("-->"+this.currentState+' '+this.receivedEvent);
},
process_event_if: 'this.opts.jamrules.MatchProperty("priority")',
propagate_event_on_refused:'ruleDontMatch',
propagate_event_on_localmachine:true,
next_state:'ruleMatch',
},
},
ruleMatch:
{
enterState:
{
init_function: function(data,aEvent,aPropertyConfiguration){
this.opts.jamrules.log("-->"+this.currentState+' '+this.receivedEvent);
},
propagate_event:'testRuleDone'
},
},
ruleDontMatch:
{
enterState:
{
init_function: function(data,aEvent,aPropertyConfiguration){
this.opts.jamrules.log("-->"+this.currentState+' '+this.receivedEvent);
},
propagate_event:'testRuleDone'
}
},
DefaultState:
{
catchEvent:'startTestingRules',
startTestingRules:
{
init_function: function(data,aEvent,aPropertyConfiguration){
this.opts.jamrules.log("-->"+this.currentState+' '+this.receivedEvent);
},
next_state:'startTesting',
},
ruleDontMatch:
{
init_function: function(data,aEvent,aPropertyConfiguration){
this.opts.jamrules.log("-->"+this.currentState+' '+this.receivedEvent);
},
next_state:'ruleDontMatch',
prevent_bubble:true
},
testRuleDone:
{
}
}
},
},
/* for test - close this comment line to activate the test
PriorityTestMatch2:
{
submachine:
{
startTesting:
{
enterState:
{
//next_state:'ruleMatch', //default state that will end the process
init_function: function(data,aEvent,aPropertyConfiguration){
this.opts.jamrules.log("-->"+this.currentState+' '+this.receivedEvent);
},
next_state:'testDisplayAll',
},
},
testDisplayAll:
{
enterState:
{
init_function: function(data,aEvent,aPropertyConfiguration){
this.opts.jamrules.log("-->"+this.currentState+' '+this.receivedEvent);
},
process_event_if: 'this.opts.jamrules.ConfigurationPropertySet("activities","compliant")',
propagate_event_on_refused:'ruleDontMatch',
propagate_event_on_localmachine:true,
next_state:'testPriorityExist',
},
},
testPriorityExist:
{
enterState:
{
init_function: function(data,aEvent,aPropertyConfiguration){
this.opts.jamrules.log("-->"+this.currentState+' '+this.receivedEvent);
},
process_event_if: 'this.opts.jamrules.MatchProperty("priority")',
propagate_event_on_refused:'ruleDontMatch',
propagate_event_on_localmachine:true,
next_state:'technicianCompliant',
},
},
technicianCompliant:
{
enterState:
{
init_function: function(data,aEvent,aPropertyConfiguration){
this.opts.jamrules.log("-->"+this.currentState+' '+this.receivedEvent);
},
process_event_if: 'this.opts.jamrules.MatchProperties("compliantTechnician","technician")',
propagate_event_on_refused:'ruleDontMatch',
propagate_event_on_localmachine:true,
next_state:'ruleMatch',
},
},
ruleMatch:
{
enterState:
{
init_function: function(data,aEvent,aPropertyConfiguration){
this.opts.jamrules.log("-->"+this.currentState+' '+this.receivedEvent);
},
propagate_event:'testRuleDone'
},
},
ruleDontMatch:
{
enterState:
{
init_function: function(data,aEvent,aPropertyConfiguration){
this.opts.jamrules.log("-->"+this.currentState+' '+this.receivedEvent);
},
propagate_event:'testRuleDone'
}
},
DefaultState:
{
catchEvent:'startTestingRules',
startTestingRules:
{
init_function: function(data,aEvent,aPropertyConfiguration){
this.opts.jamrules.log("-->"+this.currentState+' '+this.receivedEvent);
},
next_state:'startTesting',
},
ruleDontMatch:
{
next_state:'ruleDontMatch',
init_function: function(data,aEvent,aPropertyConfiguration){
this.opts.jamrules.log("-->"+this.currentState+' '+this.receivedEvent);
},
},
testRuleDone:
{
}
}
},
},
/**/
},
/**
* Events of TestRules
*/
testRuleDone: {
init_function: function() {
this.opts.jamrules.log("-->" + this.currentState + ' ' + this.receivedEvent,3);
this.opts.TestRules.nbRulesTested++;
this.opts.reason = {};
},
next_state_on_target: {
condition: '||',
submachines: {
/*for test - close this comment line to activate the test
PriorityTestMatch1:
{
target_list: ['ruleMatch'],
},
PriorityTestMatch2:
{
target_list: ['ruleMatch'],
},
/**/
}
},
next_state: 'ruleMatch',
propagate_event: 'giveMatchResult',
propagate_event_on_localmachine: true,
},
giveMatchResult: {
init_function: function(data, aEvent, aPropertyConfiguration) {
this.opts.jamrules.log("-->" + this.currentState + ' ' + this.receivedEvent,3);
},
process_event_if: 'this.opts.TestRules.nbRulesTested >= this.opts.TestRules.nbRulesToTest',
propagate_event: 'giveMatchResult',
next_state: 'ruleDontMatch',
propagate_event_on_localmachine: true,
},
},
/**
* State ruleMatch
*/
ruleMatch: {
giveMatchResult: {
init_function: function(data, aEvent, aPropertyConfiguration) {
this._log('Element profile matched',3);
//alert("match");
},
propagate_event: 'updateObjectsMatch',
next_state: 'updateObjects',
},
},
/**
* State ruleDontMatch
*/
ruleDontMatch: {
giveMatchResult: {
init_function: function(data, aEvent, aPropertyConfiguration) {
this._log('Object profile did not match',3);
//alert("don't match");
},
propagate_event: 'updateObjectsDontMatch',
next_state: 'updateObjects',
out_function: function() {},
},
},
/**
* State updateObjects
*/
updateObjects: {
updateObjectsMatch: {
init_function: function() {
for (aSubMachine in this._stateDefinition.TestRules.delegate_machines) {
this.opts.jamrules.log("-->" + aSubMachine,3);
this.opts.reason[aSubMachine] = this._stateDefinition.TestRules.delegate_machines[aSubMachine].myFSM.currentState;
//not needed this.opts.reason[aSubMachine]+=':'+this._stateDefinition.TestRules.delegate_machines[aSubMachine].myFSM.lastState;
}
let thisme = this
$.each(this.opts.reason, function(index, value) {
if (value.indexOf("DefaultState") == -1)
thisme.opts.jamrules.log("Match reason: State " + index + " --> " + value,3);
});
if (this.opts.jamrules.options.matched) {
this.opts.jamrules.options.matched.apply(thisme.opts.jamrules, [this.opts.objectProfile.objectsList]);
}
if (this.opts.objectProfile.objectsList[0].matched)
for (anObject in this.opts.objectProfile.objectsList) {
this.opts.objectProfile.objectsList[anObject].matched(thisme.opts.jamrules);
}
},
propagate_event: 'testRules',
next_state: 'waitTestRules',
out_function: function() {},
},
updateObjectsDontMatch: {
init_function: function() {
this.opts.reason = {};
for (aSubMachine in this._stateDefinition.TestRules.delegate_machines) {
this.opts.jamrules.log("-->" + aSubMachine,3);
this.opts.reason[aSubMachine] = this._stateDefinition.TestRules.delegate_machines[aSubMachine].myFSM.currentState;
this.opts.reason[aSubMachine] += ':' + this._stateDefinition.TestRules.delegate_machines[aSubMachine].myFSM.lastState;
}
let thisme = this
$.each(this.opts.reason, function(index, value) {
if (value.indexOf("DefaultState") == -1)
thisme.opts.jamrules.log("Don't Match reason: State " + index + " --> " + value,3);
});
if (this.opts.jamrules.options.notmatched) {
this.opts.jamrules.options.notmatched.apply(thisme.opts.jamrules, [this.opts.objectProfile.objectsList]);
}
if (this.opts.objectProfile.objectsList[0].notmatched)
for (anObject in this.opts.objectProfile.objectsList) {
this.opts.objectProfile.objectsList[anObject].notmatched(thisme.opts.jamrules);
}
},
propagate_event: 'testRules',
next_state: 'waitTestRules',
}
},
/**
* State waitTestRules
*/
waitTestRules: {
/*
* event to emit when a property changed in the configuration
* Initializes the rule engine for testing the rules against the current configuration and the different element profiles
*
* event should send a 'aPropertyConfiguration' data object as:
* {propertyName:<aPropertyName>,propertyValue:<aPropertyValue>,status:<aStatus>}
*/
propertyChange: {
init_function: function(data, aEvent, aPropertyConfiguration) {
//initialize the element profiles to process
this.opts.objectProfileId = -1;
this.opts.aPropertyConfiguration = aPropertyConfiguration;
this.opts.ObjectProfiles = this.opts.jamrules.getAllObjectProfiles();
this.opts.maxObjectProfiles = Object.keys(this.opts.ObjectProfiles).length;
// start processing rules on the element profiles list
if (this.opts.maxObjectProfiles > 0)
{
this.trigger('startProcessing');
let propertyNameEvent = this.opts.aPropertyConfiguration.propertyName;
let that=this;
$.doTimeout('testRules'+that.FSMName,100,function(){
that.trigger(propertyNameEvent);
});
}
},
},
/*
* event to emit to run a test without a change on the configurator
*
*/
runEngine: {
init_function: function() {
//initialize the element profiles to process
this.opts.objectProfileId = -1;
this.opts.aPropertyConfiguration = {
propertyName: 'dummyEvent'
};
this.opts.ObjectProfiles = this.opts.jamrules.getAllObjectProfiles();
this.opts.maxObjectProfiles = Object.keys(this.opts.ObjectProfiles).length;
// start processing rules on the element profiles list
if (this.opts.maxObjectProfiles > 0)
{
this.trigger('startProcessing');
let that=this;
$.doTimeout('testRules'+that.FSMName,10,function(){
that.trigger('testRules')
});
}
},
},
/*
* internal event - starts the process to test the rules against the current configuration and the different element profiles
* event should send a 'aPropertyConfiguration' data object as:
* {propertyName:<aPropertyName>,propertyValue:<aPropertyValue>,status:<aStatus>}
*/
/*for test
priority:'testRules',
activities:'testRules',
compliantTechnician:'testRules',
/**/
testRules: {
next_state_when: "this.opts.objectProfileId < this.opts.maxObjectProfiles",
next_state: 'TestRules',
init_function: function(data, aEvent) {
this.opts.objectProfileId++;
if (this.opts.objectProfileId < this.opts.maxObjectProfiles) {
this.opts.objectProfile = this.opts.ObjectProfiles[Object.keys(this.opts.ObjectProfiles)[this.opts.objectProfileId]];
this.trigger(this.opts.aPropertyConfiguration.propertyName);
}
else{
this.trigger('stopProcessing');
}
},
},
},
DefaultState: {
start: //a default start event received at the FSM start
{
next_state: "waitTestRules"
},
testRuleDone: {
//dummy to catch it
},
startProcessing: {
init_function: function() {
if (this.opts.jamrules.options.startProcessing)
this.opts.jamrules.options.startProcessing(this.opts.jamrules);
}
},
stopProcessing: {
init_function: function() {
if (this.opts.jamrules.options.stopProcessing)
this.opts.jamrules.options.stopProcessing(this.opts.jamrules);
}
},
}
};
/**
* jamrulesConstructor
*
* constructor of a jamRules object
*
*/
let jamrulesConstructor = function(options) {
// variables and functions private unless attached to API below
// 'this' refers to global window
let defaults = {
debug: false,
LogLevel: 1,
matchedFunctionName: "matched",
notmatchedFunctionName: "notmatched"
};
/**
* @param options - options given to jamrules
* @access public
*/
if (this.options == undefined) this.options = {
jqueryObj: aDefaultJqueryObj
};
this.options = jQuery.extend({}, defaults, options || {});
/**
* @param myRulesEngine - the FSM engine bound to the jamrules
* @access public
*/
this.myRulesEngine = null;
/**
* @param myJqueryObj - the jquery object on which jamrules is bound
* @access public
*/
this.myJqueryObj = options.jqueryObj;
/**
* @param _ObjectProfiles
* @access private
* @abstract list of possible profiles available local to this rule engine
* a profile is defined by a list of entries [objectKey]:{propertiesSet:<apropertiesSet>,objectsList:[]}
* {
* <objectKey1>:{
* propertiesSet:
* {
* <propertyName1>:{
* type:<discreteValuesList>,
* <propertyValue1>:1|0,
* <propertyValue2>:1|0,
* ...
* },
* ... same as propertiesConfiguration definition
* <propertyName1>.<propertyValue1>:1|0,
* <propertyName2>.<propertyValue2>:1|0
* ....
* ],
* elementList:[ //element objects that share the same properties set
* ]
* },
* <objectKey2>:{
* ....
* }
*/
let _ObjectProfiles = {};
/**
* getObjectProfiles - get the current object profiles bound to this rule engine
* @access public
*
*/
this.getObjectProfiles = function() {
return _ObjectProfiles;
}
/**
* getAllObjectProfiles - get all the object profiles (shared and of the current instance) bound to this rule engine
* @access public
*
*/
this.getAllObjectProfiles = function() {
return $.extend({}, ObjectProfiles, this.getObjectProfiles());
}
/**
* wildcardSearch - look for a string with wildcards in an other string
* @access public
* @param {string} str - string to test
* @param {string} wildcardStr
* checks if a string match to wildcardStr
* Wildcards are: * as zero to unlimited numbers - ? as zero to one character
* @param {[type]} regexOptions default:gmi, regex options
* @returns {boolean}
*
* "a*b" => everything that starts with "a" and ends with "b"
* "a*" => everything that starts with "a"
* "*b" => everything that ends with "b"
* "*a*" => everything that has an "a" in it
* "*a*b*"=> everything that has an "a" in it, followed by anything, followed by a "b", followed by anything
*
*/
this.wildcardSearch = function(str, wildcardStr,regexOptions) {
regexOptions = regexOptions || 'gim';
const escapeRegex = (str) => str.replace(/([.*+?^=!:${}()|\[\]\/\\])/gi, "\\$1");
return new RegExp("^" + regexReplaceHelper(wildcardStr, {"*": ".*", "?": ".?"}, escapeRegex) + "$",regexOptions).test(str);
}
/**
* translate a string with wildcards in a regex
* @access private
* @param {string} input a string with wildcards
* eg "a*b"
* @param {object} replace_dico object giving the how to translate a wildcard to a regex
* eg: {"*": ".*", "?": ".?"}
* @param {function} last_map a function to apply to string parts with no wildcard
* @return {string} a regex expression
*/
let regexReplaceHelper = function(input, replace_dico, last_map) {
let replace_dict = Object.assign({}, replace_dico);
if (Object.keys(replace_dict).length === 0) {
return last_map(input);
}
const split_by = Object.keys(replace_dict)[0];
const replace_with = replace_dict[split_by];
delete replace_dict[split_by];
return input.split(split_by).map((next_input) => regexReplaceHelper(next_input, replace_dict, last_map)).join(replace_with);
}
/**
* wildcardSearchInPropertyObject Search if a string is contained in one of the proporty values of the object
* @access public
* @param {string} aString a string (possible wildcards: '*' or '?') to find in one of the property values
* @param {object} jsonObj an object to test
* @param {boolean} searchDeep default:false - if true, search all the levels in the object
* @return {boolean} true if found
*/
this.wildcardSearchInPropertyObject = function(aString,jsonObj,searchDeep)
{
let found = false;
searchDeep = searchDeep?true:false;
for (let i in jsonObj) {
if (searchDeep && (typeof jsonObj[i] == 'object') && wildcardSearchInPropertyObject(aString, jsonObj[i])) {
found = true
break;
} else
//if key matches and value matches or if key matches and value is not passed (eliminating the case where key matches but passed value does not)
if (wildcardSearch(jsonObj[i], aString)) {
found = true
break;
}
}//end for
return found;
}
};
/**
* -----------------------------------------
* Testing functions available for the rules
* -----------------------------------------
*/
/**
* @function MatchProperty
* @access public
* @abstract matching rule function, tests if at least a property value of a property is shared between the configuration and the object
* @param aPropertyName: a property name
*
* @return returns true if any property value for a given aPropertyName is set in the profile object and in the configuration property set
* @example
* object.priority.priority1=1
* object.technician.technician1=1
* configuration.priority.priority1=1
* configuration.priority.priority2=0
* configuration.technician.technician1=0
* configuration.technician.technician2=1
* MatchProperty('priority') -> match
* MatchProperty('technician') -> no match
*/
let MatchProperty = function(aPropertyName) {
let propertiesObjectProfile = this.myRulesEngine.opts.objectProfile.propertiesSet;
if (propertiesConfiguration[aPropertyName] && propertiesObjectProfile[aPropertyName]) {
for (aPropertyValue in propertiesObjectProfile[aPropertyName]) {
if (
(propertiesConfiguration[aPropertyName][aPropertyValue]) &&
(propertiesObjectProfile[aPropertyName][aPropertyValue]) &&
(propertiesObjectProfile[aPropertyName][aPropertyValue] == propertiesConfiguration[aPropertyName][aPropertyValue])
)
return true;
}
}
return false;
}
/**
* @function MatchPropertyValue
* @access public
* @abstract matching rule function, tests if a given property value is set for configuration and the object
* @param aPropertyName: a property name
* @param aPropertyValue: a value of aPropertyName
*
* @return returns true if the configuration for the aPropertyName.aPropertyValue == the one defined for the current objectProfile being tested
* @example
* object.priority.priority1=1
* object.technician.technician1=1
* configuration.priority.priority1=1
* configuration.technician.technician1=0
* MatchPropertyValue('priority','priority1') -> match
* MatchPropertyValue('technician','technician1') -> no match
*/
let MatchPropertyValue = function(aPropertyName, aPropertyValue) {
let propertiesObjectProfile = this.myRulesEngine.opts.objectProfile.propertiesSet;
if (
(propertiesConfiguration[aPropertyName]
&& propertiesObjectProfile[aPropertyName])
&& (propertiesConfiguration[aPropertyName][aPropertyValue]
&& propertiesObjectProfile[aPropertyName][aPropertyValue])
&& (propertiesConfiguration[aPropertyName][aPropertyValue] == propertiesObjectProfile[aPropertyName][aPropertyValue])
)
return true;
else return false;
}
/**
* @function MatchPropertySearch
**access public
* @abstract matching rule function, tests if a string aPropertyName is found as a property value of objects =
* @param aPropertyName: a string to search in the property values of objects.
* wildcards are possible: '*' (0 or more char), '?' (0 or 1 char)
* eg: 'my*propert?' will match 'myproperty','mygivenpropert','myREDproperts'
* won't match 'property', 'myREDproperties'
*
* @param searchMode: default:'or'
* - or: blank are considered as 'or' operator between keywords to find
* - and: blank are considered as 'and' operator with all keywords to be found in any property values
* @return returns true if the pattern string(s) defined in the configurator are found in property values of object
* @remark generally used for text input as search input
* @example
* object.priority.priority1=1
* object.technician.technician1=1
* configuration.priority['prio*']=1
* configuration.technician['technician2']=1
* MatchPropertySearch('priority') -> match
* MatchPropertySearch('technician') -> no match
*/
let MatchPropertySearch = function(aPropertyName, searchMode) {
searchMode = searchMode || 'or';
let propertiesObjectProfile = this.myRulesEngine.opts.objectProfile.propertiesSet;
let nbfound = 0;
let nbtofind = 0;
let found = false;
let that = this;
for (searchString in propertiesConfiguration[aPropertyName]) {
if (!propertiesConfiguration[aPropertyName][searchString]) continue;// not active
found = false;
searchString.split(" ").some(function (searchSubString) {
nbtofind++;
for (aObjPropertyValue in propertiesObjectProfile[aPropertyName]) {
if (aObjPropertyValue != '*'