Skip to content

Commit 7534eb8

Browse files
author
Emmanuel
committed
add addPropertyObjects function
1 parent 35b4f79 commit 7534eb8

4 files changed

Lines changed: 221 additions & 8 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ Any object that matches the rules will have their "Matched" function called. Any
129129

130130

131131
# Demos
132+
* [filtering of animals according to filters](https://demo.intersel.fr/jamrules/tests/exampleReadMe.html) (source code in test/exampleReadMe.html)
132133
* [filtering of documents according to filters](https://demo.intersel.fr/jamrules/tests/filterDocs.html) (source code in test/filterDocs.html)
133134

134135
# Create the JamRules object: jamrules.build(options)

jamrules.js

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
*
99
* -----------------------------------------------------------------------------------------
1010
* Modifications :
11+
* - 20210418 - E.Podvin - V2.2.0 - add addPropertyObjects
1112
* - 20170402 - E.Podvin - V2.1.0 - adding new objects simplified + possibility to call matching rule functions
1213
* - 20170331 - E.Podvin - V2.0.0 - Refactoring
1314
* - 20170227 - E.Podvin - V1.0.0 - Creation
@@ -36,6 +37,7 @@ var jamrules = (function() {
3637
* @param ObjectProfiles
3738
* @access private
3839
* @abstract list of possible profiles available to all rules
40+
* used as optimization to process filtering on object profiles rather than on each objects...
3941
* a profile is defined by a list of entries [objectKey]:{propertiesSet:<apropertiesSet>,objectsList:[]}
4042
* {
4143
* <objectKey1>:{
@@ -790,8 +792,8 @@ var jamrules = (function() {
790792
for (aPropertyValue in propertiesObjectProfile[aPropertyName]) {
791793
if (
792794
(propertiesConfiguration[aPropertyName][aPropertyValue]) &&
793-
(propertiesObjectProfile[aPropertyName][aPropertyValue])
794-
// && (propertiesObjectProfile[aPropertyName][aPropertyValue] == propertiesConfiguration[aPropertyName][aPropertyValue])
795+
(propertiesObjectProfile[aPropertyName][aPropertyValue]) &&
796+
(propertiesObjectProfile[aPropertyName][aPropertyValue] == propertiesConfiguration[aPropertyName][aPropertyValue])
795797
)
796798
return true;
797799
}
@@ -818,9 +820,11 @@ var jamrules = (function() {
818820
var MatchPropertyValue = function(aPropertyName, aPropertyValue) {
819821
var propertiesObjectProfile = this.myRulesEngine.opts.objectProfile.propertiesSet;
820822
if (
821-
(propertiesConfiguration[aPropertyName] && propertiesObjectProfile[aPropertyName]) &&
822-
(propertiesConfiguration[aPropertyName][aPropertyValue] && propertiesObjectProfile[aPropertyName][aPropertyValue])
823-
// && (propertiesConfiguration[aPropertyName][aPropertyValue] == propertiesObjectProfile[aPropertyName][aPropertyValue])
823+
(propertiesConfiguration[aPropertyName]
824+
&& propertiesObjectProfile[aPropertyName])
825+
&& (propertiesConfiguration[aPropertyName][aPropertyValue]
826+
&& propertiesObjectProfile[aPropertyName][aPropertyValue])
827+
&& (propertiesConfiguration[aPropertyName][aPropertyValue] == propertiesObjectProfile[aPropertyName][aPropertyValue])
824828
)
825829
return true;
826830
else return false;
@@ -988,7 +992,7 @@ var jamrules = (function() {
988992
/**
989993
* @function ObjectPropertySet
990994
* @access public
991-
* @abstract matching rule function, tests if the property in theObjectPropertySett has its value set
995+
* @abstract matching rule function, tests if the property in theObjectPropertySet has its value set
992996
* @param aPropertyName: an element property name
993997
* @param aPropertyValue: a value of aPropertyName
994998
* @param valueSet: [0|1(default)]
@@ -1021,7 +1025,7 @@ var jamrules = (function() {
10211025
var ObjectPropertiesSameValue = function(aPropertyName1, aPropertyName2, aPropertyValue) {
10221026
propertiesObjectProfile = this.myRulesEngine.opts.objectProfile.propertiesSet;
10231027

1024-
//if undefined, means that we want that one value of the property 1 and property 2 of object are set
1028+
//if undefined, means that we want that one value of the property 1 and property 2 of object are set
10251029
if (aPropertyValue == undefined) {
10261030
if (propertiesObjectProfile[aPropertyName1] !== undefined)
10271031
for (aPropertyValue in propertiesObjectProfile[aPropertyName1]) {
@@ -1308,7 +1312,7 @@ var jamrules = (function() {
13081312
* @example
13091313
*/
13101314
var addPropertyObject = function(anObject, aMatchingFunction, aNotMatchingFunction) {
1311-
this.log("addObject");
1315+
this.log("addPropertyObject");
13121316
if (!aMatchingFunction && anObject.matched)
13131317
aMatchingFunction = anObject.matched;
13141318

@@ -1321,6 +1325,35 @@ var jamrules = (function() {
13211325
notmatched: aNotMatchingFunction
13221326
}, this.getObjectProfiles());
13231327
};
1328+
/**
1329+
* @function public addPropertyObjects
1330+
* @abstract add objects to the list of objects to test against rules
1331+
* @param objects: array of property objects. Each object may have these properties set:
1332+
* matched (otion):<function name to call when a rule will match for the object>
1333+
* notmatched (option):<function name to call when there is a change but object does not match any rules>
1334+
* @param aMatchingFunction (option): the matching function, same as to define the "matched" property in the object
1335+
* @param aNotMatchingFunction (option): the matching function, same as to define the "notmatched" property in the object
1336+
* @example
1337+
*/
1338+
var addPropertyObjects = function(objects, aMatchingFunction, aNotMatchingFunction) {
1339+
this.log("addPropertyObjects");
1340+
let that=this;
1341+
objects.forEach(function(anObject) {
1342+
if (!aMatchingFunction && anObject.matched)
1343+
aMatchingFunction = anObject.matched;
1344+
1345+
if (!aNotMatchingFunction && anObject.notmatched)
1346+
aNotMatchingFunction = anObject.notmatched;
1347+
1348+
let theObject = anObject;
1349+
_addObject({
1350+
propertiesSet: jamrules._translateToJamrulesProperties(anObject),
1351+
matched: aMatchingFunction,
1352+
notmatched: aNotMatchingFunction,
1353+
object:theObject
1354+
}, that.getObjectProfiles());
1355+
});
1356+
};
13241357
/**
13251358
* @access public
13261359
* @abstract log a message on the console for debug
@@ -1342,6 +1375,7 @@ var jamrules = (function() {
13421375
compileRules: compileRules,
13431376
addObject: addObject,
13441377
addPropertyObject: addPropertyObject,
1378+
addPropertyObjects: addPropertyObjects,
13451379
log: log
13461380
/** Public variables **/
13471381
,

tests/exampleReadMe.html

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
<html>
2+
3+
<head>
4+
<title>Readme Example</title>
5+
<meta charset="utf-8" />
6+
<script type="text/javascript" src="../extlib/jQuery/jquery-3.2.0.min.js"></script>
7+
<script type="text/javascript" src="../extlib/iFSM/extlib/jquery.dorequesttimeout.js"></script>
8+
<script type="text/javascript" src="../extlib/iFSM/extlib/jquery.attrchange.js"></script>
9+
<script type="text/javascript" src="../extlib/iFSM/iFSM.js"></script>
10+
<script type="text/javascript" src="../extlib/Blapy/extlib/mustache/mustache.js"></script>
11+
<script type="text/javascript" src="../extlib/Blapy/Blapy.js"></script>
12+
<script type="text/javascript" src="../extlib/jQuery-MD5/jquery.md5.js"></script>
13+
<script type="text/javascript" src="../jamrules.js"></script>
14+
</head>
15+
16+
<body id="myBlapy">
17+
<section id="filterbox">
18+
<h1>Filter box</h1>
19+
<p>Select some filters to see the selection of some animals</p>
20+
<section id='type' onclick="
21+
let radioVal = $('input[name=type]:checked').val();
22+
rulesEngine.selectConfigurationPropertyValue('type', 'mammal', radioVal=='mammal'?'1':'0');
23+
rulesEngine.selectConfigurationPropertyValue('type', 'other' , radioVal=='other' ?'1':'0');
24+
">
25+
<h3>Type</h3>
26+
<label>
27+
<input name="type" type="radio" value="mammal">
28+
Mammal
29+
</label>
30+
<label>
31+
<input name="type" type="radio" value="other">
32+
Other
33+
</label>
34+
35+
</section>
36+
<section id='name'>
37+
<label>
38+
<input id="nameCat" type="checkbox" onclick="rulesEngine.selectConfigurationPropertyValue('name', 'cat' , $(this).is(':checked') ? '1' : '0');">
39+
Cat
40+
</label>
41+
<label>
42+
<input id="nameDog" type="checkbox" onclick="rulesEngine.selectConfigurationPropertyValue('name', 'dog' , $(this).is(':checked') ? '1' : '0');">
43+
Dog
44+
</label>
45+
46+
</section>
47+
<section id='color'>
48+
<label>
49+
<input class="checkbox" id="colorBlack" type="checkbox" onclick="rulesEngine.selectConfigurationPropertyValue('color', 'black' , $(this).is(':checked') ? '1' : '0');">
50+
Black
51+
</label>
52+
<label>
53+
<input class="checkbox" id="colorWhite" type="checkbox" onclick="rulesEngine.selectConfigurationPropertyValue('color', 'white' , $(this).is(':checked') ? '1' : '0');">
54+
White
55+
</label>
56+
<label>
57+
<input class="checkbox" id="colorGrey" type="checkbox" onclick="rulesEngine.selectConfigurationPropertyValue('color', 'grey' , $(this).is(':checked') ? '1' : '0');">
58+
Grey
59+
</label>
60+
</section>
61+
</section>
62+
<section id="result">
63+
<h1>Result Matching</h1>
64+
<div id="debug">
65+
</div>
66+
<div id="animalSection" data-blapy-container="true" data-blapy-container-name="animalSection" data-blapy-container-content="animalSection" data-blapy-update="json">
67+
<xmp style="display:none;">
68+
<section style="display:none;" id="{{type}}_{{name}}_{{color}}">
69+
<div>
70+
{{type}} - {{name}} - {{color}}
71+
</div>
72+
</section>
73+
</xmp>
74+
</section>
75+
76+
<script>
77+
// ---------------------------------
78+
// Example of jamrules usage
79+
// ---------------------------------
80+
81+
var rulesEngine = jamrules.build({
82+
debug: true
83+
});
84+
// rules setting
85+
// Select animals that have the same properties than the configurator
86+
rulesEngine.createRulesSet("selectAnimal", ['type', 'name', 'color']);
87+
rulesEngine.addRule("selectAnimal", "typeMatch", 'MatchProperty("type")');
88+
rulesEngine.addRule("selectAnimal", "nameMatch", 'MatchProperty("name")');
89+
rulesEngine.addRule("selectAnimal", "colorMatch", 'MatchProperty("color")');
90+
91+
// if not found...
92+
// Try if it is not a mammal and configuration is on 'other', and the color is ok, we don't mind about the name...
93+
rulesEngine.createRulesSet("selectOtherAnimal", ['type', 'color']);
94+
rulesEngine.addRule("selectOtherAnimal", "typeNotMammal", '!ObjectPropertySet("type","mammal","1")');
95+
rulesEngine.addRule("selectOtherAnimal", "configurationOther", 'ConfigurationPropertySet("type","other","1")');
96+
rulesEngine.addRule("selectOtherAnimal", "colorMatch", 'MatchProperty("color")');
97+
98+
// initialize the engine
99+
rulesEngine.runRulesEngine();
100+
101+
var animalsData = null;
102+
103+
$(document).ready(function() {
104+
105+
$('#myBlapy').Blapy();
106+
107+
$.getJSON("includes/exampleRM.json", function(data) {
108+
109+
animalsData = data;
110+
111+
rulesEngine.addPropertyObjects(
112+
animalsData,
113+
function() {
114+
console.log('I am selected: ' + this.object.type + '_' + this.object.name + '_' + this.object.color);
115+
$('#'+this.object.type + '_' + this.object.name + '_' + this.object.color).show();
116+
},
117+
function() {
118+
console.log('I am NOT selected: ' + this.object.type + '_' + this.object.name + '_' + this.object.color);
119+
$('#'+this.object.type + '_' + this.object.name + '_' + this.object.color).hide();
120+
}
121+
);
122+
123+
$('#myBlapy').trigger('updateBlock', {
124+
html: animalsData,
125+
params: {
126+
embeddingBlockId: "animalSection"
127+
}
128+
});
129+
130+
});
131+
132+
});
133+
</script>
134+
</body>
135+
136+
</html>

tests/includes/exampleRM.json

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
[
2+
{
3+
"type":"mammal",
4+
"name":"cat",
5+
"color":"black"
6+
},
7+
{
8+
"type":"mammal",
9+
"name":"dog",
10+
"color":"green"
11+
},
12+
{
13+
"type":"mammal",
14+
"name":"dog",
15+
"color":"white"
16+
},
17+
{
18+
"type":"mammal",
19+
"name":"cat",
20+
"color":"orange"
21+
},
22+
{
23+
"type":"mammal",
24+
"name":"dog",
25+
"color":"grey"
26+
},
27+
{
28+
"type":"mammal",
29+
"name":"cat",
30+
"color":"white"
31+
},
32+
{
33+
"type":"bird",
34+
"name":"seagull",
35+
"color":"white"
36+
},
37+
{
38+
"type":"insect",
39+
"name":"spider",
40+
"color":"black"
41+
}
42+
]

0 commit comments

Comments
 (0)