Recommend this page to a friend! |
Download |
Info | Documentation | Files | Download | Reputation | Support forum | Blog | Links |
Ratings | Unique User Downloads | Download Rankings | ||||
Not enough user ratings | Total: 39 | All time: 514 This week: 3 |
Version | License | JavaScript version | Categories | |||
jamrules 1.0 | MIT/X Consortium ... | 5 | jQuery, Testing |
Description | Author | |||
This package can test object values against a set of rules. Innovation Award
|
Javascript/jQuery filtering tool that helps to filter objects among a set of objects according to rules.
Let's say you have a set of objects with properties and you'd like to filter them according to a user configuration of criteria and specific rules of selection... then JamRules is for you!
JamRules is a Javascript/jQuery library.
With it, you configure: * a set of parameters/criteria of selection (filters configurator) that can be driven by checkboxes and input * a set of rules to find the objects according to the filters configuration * a set of objects to play with
Once configured, you can start the filtering process so that JamRules selects the objects that match your criteria and calls a 'selected' function on each of them, and call a 'not selected' function for the others...
For example, connected to a dialog box of criteria managed with checkboxes, JamRules can be activated each time a criteria changes and so to alert the selected/unselected object of their new selection status, for instance to be displayed or not...
As an object filter library, Jamrules is your best friend! Ideal for product configurators, objects selection on criteria, ...
See JamRules in action (source code in test/filterDocsExclusive.html)
To run jamrules, you will have to: * create a jamrules object, * define the filter configuration * create rules, * add objects to test, * run the filtering process,
//Create a jamrules object
var rulesEngine = jamrules.build();
The properties are data that define your object. They are used to identify the objects that answer your filtering rules.
Eg, if your objects to filter are animals, properties of an animal may be "Type" ('mammal', 'bird', 'insect', ...), "Name" ('cat', 'dog', ...), "Number of limbs" (0,2,4,8, ...), "Color" ('brown', 'green', ...), ...
Some of the properties may be used in the filtering configuration to select the objects.
Your objects needs to have a json definition, eg. :
let myobjects = [
{
"type":"mammal",
"name":"cat",
"color":"black"
},
{
"type":["mammal","carnivora"],
"name":"dog",
"color":"white"
},
//and so on
]
A same property may have several values.
Each object may have its own set of properties that may be different from the other object's sets... Up to you to define in your rules how to select or not your objects...
Use the function addPropertyObjects of your rules engine to add your objects and the behavior of the selected and not selected objects by jamRules:
rulesEngine.addPropertyObjects(
myobjects,
function(){console.log('I am selected:'+this.name)},
function(){console.log('I am NOT selected'+this.name)}
);
Generally, the filtering configuration is driven by the status of checkboxes, radio buttons, input... that the user can click to select a configuration value. These input set the status of the value of a property as chosen or not.
For example, for the property "color", you could set several checkboxes, each allowing to select a color as "red", "blue", "green", ...
To configure the property values of the configurator, we use the function selectConfigurationPropertyValue.
<label for="check_green" onclick="rulesEngine.selectConfigurationPropertyValue("color","green",$(this).children('input').value());">
<input type="checkbox">
Green
</label>
<label for="check_red" onclick="rulesEngine.selectConfigurationPropertyValue("color","red",$(this).children('input').value());">
<input type="checkbox">
Red
</label>
A rule is a boolean test on your configuration and objects.
Rules are defined within a rule set. You can defined as rule sets as you need.
If all the rules in a rule set are valid ('and'), then the object is selected.
Hence, to be selected ("matched"), an object should match ONE rule set. If none of the rule sets are validated by the object, it is considered as "not matched"...
The rules will be based on pre-defined test functions as "is property xxx of object equal this value?" (ObjectPropertySet), "is property value is selected in the filtering configuration?" (MatchProperty), ...
So, you first define your rule set, then add rules in it, then define a second rule set, and so on...
// rules setting
rulesEngine.createRulesSet("HasGreenColor");
rulesEngine.addRule("HasGreenColor","lightgreen",'ObjectPropertySet("color","light green")');
rulesEngine.addRule("HasGreenColor","darkgreen",'ObjectPropertySet("color","dark green")');
rulesEngine.addRule("HasGreenColor","green",'ObjectPropertySet("color","green")');
Once done, we will be able to run our jamrules engine with runRulesEngine:
rulesEngine.runRulesEngine();
Any object that matches the rules will have their "Matched" function called. Any object that is not selected with the rules will have their "NotMatched" function called....
//initialisation of jamrules and its configurator
var rulesEngine = jamrules.build({
"debug": "<boolean>", //default: false
"matched": "<a function to call when the rule find a match>", // default: null
"notmatched": "<a function to call when the rule did not find a match>",// default: null
"matchedFunctionName": "<property name for the 'matched' function in objects>",// default: matched
"notmatchedFunctionName": "<property name for the 'notmatched' function in objects>"// default: notmatched
"startProcessing": "<a function to call when rule engine starts to process rules>"// default: null
"stopProcessing": "<a function to call when rule engine finished to process rules>"// default: null
});
if true, the rule engine will send debug message on the console
The "matched" and "notmatched" functions are called whenever the rule engine matches an object profile.
Functions have the following parameters: * aListOfMatchedObjects: the list of objects that matched the rule * this refers to the rule engine object
Remarks: These functions are not to be confused with the ones defined on the object level...
These options allows to change the default property names of the object that define the 'matched' and 'notmatched' functions of it. May be used if by any chance, these property names are used for other things...
In order to test objects with jamrules, you have to give it objects to test against the rules defined in the rule engine.
These objects may be any with properties...
{
"color": "red",
"size": "xl",
...
}
Internally, the objects are formatted in order to process the matching functions and rules, the internal format of your data in jamrules will be :
{
propertiesSet:{
<propertyName1>:{<propertyValue1:<0|1>,<propertyValue2:<0|1>, ...},
<propertyName2>:{<propertyValue1:<0|1>,<propertyValue2:<0|1>, ...},
...
},
matched: <a function to call when it matches>,
notmatched: <a function to call when it does not match>,
}
eg:
{
propertiesSet:{
color: {red:1},
size: {xl:1}
...
},
matched: function(ruleEngine){console.log("object matched!")},
notmatched: null,
}
Remark: The properties of the objects should be "static". The use of functions to define dynamic properties within objects is not possible.
The JamRules filtering configurator is a special object that can be used in a rule to test a configuration of properties against the properties of the objects to filter.
For example, let's say we have white and black trousers. If you'd like to get only the white trousers, you can set a configurator property "color" with a "white" property value set to 1. Then you'll be able to test this configurator property against your objects.
The selectConfigurationPropertyValue function allows to create and edit such entry in the configurator.
rulesEngine.selectConfigurationPropertyValue("color","white",1);
When "run", Jamrules tests each objects against the defined sets of rules in their order of declaration.
It declares an object as "matched" as soon as a set of rules is compliant with the object and its properties.
Rules are defined within a "rules set" declation. A rules set is validated when all its rules are validated to true.
When a rule set is not ok, Jamrules tries the next rules set.
If none of the rules sets are validated, then the object is declared "unmatched".
We use the createRulesSet function to create a rules set, and the addRule function to add a rule in a rule set.
A rule declares a test to try.
The test can use information on the object properties, the configurator or any other information you'd like...
JamRules has several matching functions ready to use as: * ObjectPropertySet: tests the value of the property of the object currently tested * ObjectPropertiesSameValue: tests the value of one property against another property... * ...
There are several filtering functions that may help to test a configuration in the filtering configurator against the properties of objects: * MatchProperty * MatchPropertyValue * MatchProperties * MatchPropertiesSameValue * MatchPropertiesSameValues * MatchPropertySearch * ConfigurationPropertySet * ConfigurationPropertiesSameValue * ConfigurationPropertiesSameValues * MatchExternalRule
rulesEngine.createRulesSet("SameTrousers");
rulesEngine.addRule("SameTrousers","O1Trouser",'ObjectPropertySet("object1","trouser")');
rulesEngine.addRule("SameTrousers","O2Trouser",'ObjectPropertiesSameValue("object1","object2")');
rulesEngine.createRulesSet("SameShirts",["object1","object2"]);
rulesEngine.addRule("SameShirts","O1Shirt",'ObjectPropertySet("object1","shirt")');
rulesEngine.addRule("SameShirts","O2Shirt",'ObjectPropertiesSameValue("object1","object2")');
Add objects to the list of objects to test against rules.
Objects*: array of objects with their properties plus these optional ones:
matched* (option): function to call when a rule will match for the object
notmatched* (option): function to call when rules will be tested but no rules match for the object
aMatchingFunction* (option): a matching function, same as to define a "matched" property in the object aNotMatchingFunction* (option): a 'not' matching function, same as to define a "notmatched" property in the object
ruleEngine = jamrules.build();
var anObject = {
object1Color : "white"
};
myMatchFunction = function(){alert("Hello:"+this.object1Color)};
rulesEngine.addPropertyObject(onObject,myMatchFunction);
Add an object to the list of objects to test against rules.
anObject* with its properties plus these optional ones
matched* (option): function to call when a rule will match for the object
notmatched* (option): function to call when rules will be tested but no rules match for the object
aMatchingFunction* (option): a matching function, same as to define a "matched" property in the object aNotMatchingFunction* (option): a 'not' matching function, same as to define a "notmatched" property in the object
ruleEngine = jamrules.build();
var anObject = {
object1Color : "white"
};
myMatchFunction = function(){alert("Hello:"+this.object1Color)};
rulesEngine.addPropertyObject(onObject,myMatchFunction);
Add an object to the list of objects to test against rules.
ruleEngine = jamrules.build();
var anObject = {
propertiesSet : {
object1Color : {
white : 1
},
},
matched : myMatchFunction,
notmatched : null
};
rulesEngine.addObject(onObject);
Remark: to be called with jamrules variables.
Add an object to the list of objects to test against rules. This function differs from addObject in the way that all the jamrules engines will share the objects added this way. So, you include once your objects in the first jamrules object and then they will be processed by all the other rules.
var anObject = {
propertiesSet : {
object1Color : {
white : 1
},
},
matched : myMatchFunction,
notmatched : null
};
jamrules._addObject(onObject);
Creates a rule set.
rulesEngine.createRulesSet("SameTrousers");
...
rulesEngine.createRulesSet("SameTrousers",["aProperty1","aProperty2"]);
Add a new "and" rule in aRulesGroup.
// colortop should have the same color name than colorbottom but different from colormiddle
rulesEngine.addRule("SameColorTrousersPack","Test1",'ObjectPropertiesSameValue("colortop","colorbottom")');
rulesEngine.addRule("SameColorTrousersPack","TestNot2",'!ObjectPropertiesSameValue("colortop","colormiddle")');
Initialize the rule engine - to do before action and after adding new rules
// prepare the rule engine
rulesEngine.compileRules();
Run the rules engine.
rulesEngine.runRulesEngine();
Select a value in the filtering configurator as a radio would do: unselecting other values of aPropertyName.
If "doTest" is set, the rules engine will run and process -only- the rules sets that have configured the "aPropertyName" in the "ruleEvents" parameter in createRulesSet function.
aPropertyValue may be set to "*" to match any value of aPropertyName.
rulesEngine.createRulesSet("SameTrousers",["object1"]);
rulesEngine.addRule("SameTrousers","Trouser",'MatchProperty("object1")');
....
//as 'object1' is defined in the "SameTrousers" rules set, the following line will configure the "object1" property and see the rule set "SameTrousers" processed
rulesEngine.selectConfigurationPropertyValue("object1","trouser");
...
//no rule set to process... just configure the property in the configurator
rulesEngine.selectConfigurationPropertyValue("object1","trouser",false);
...
//will process all the rules sets
rulesEngine.runRulesEngine();
set a property/property value status in the rules configurator. It is designed for checkboxes/multiple select as it set a value as a checkbox would do.
If "doTest" is set, the rules engine will run and process -only- the rules sets that have configured the "aPropertyName" in the "ruleEvents" parameter in createRulesSet function.
aPropertyValue may be set to "*" to match any value of aPropertyName.
rulesEngine.createRulesSet("SameTrousers",["object1"]);
rulesEngine.addRule("SameTrousers","Trouser",'MatchProperty("object1")');
....
//as 'object1' is defined in the "SameTrousers" rules set, the following line will configure the "object1" property and see the rule set "SameTrousers" processed
rulesEngine.checkConfigurationPropertyValue("object1","trouser",1);
...
//no rule set to process... just configure the property in the configurator
rulesEngine.checkConfigurationPropertyValue("object1","trouser",1,false);
...
//will process all the rules sets
rulesEngine.runRulesEngine();
reset a property by setting all its property values to a false status in the rules configurator
reset a property completely
Tests if at least a property value of a property is shared between the configuration and the object
Returns true if any property value for a given aPropertyName is set in the profile object and in the configuration property set
Test if a string aPropertyName is found as a property value of objects. Generally used for text input as search input.
returns true if the pattern string(s) defined in the configurator are found in property values of object
Tests if a given property value is set for configuration and the object
Returns true if the configuration for the aPropertyName.aPropertyValue == the one defined for the current objectProfile being tested
Tests if a property value of a property is set for the configurator and the object
Returns true if aPropertyValue in aConfigurationPropertyName and in anObjectPropertyName are both set.
tests the property values set for the configurator's property and the object's property and if they are the same between the two
Returns true if all properties values of aConfigurationPropertyName and of anObjectPropertyName are both set
Tests if at least a property value exists and is set between the configurator property and the object property
returns true if it exists a value of aConfigurationPropertyName that is the same that in anObjectPropertyName
Tests if the value of a configuration property string is found in the values of object's properties Generally used for a text input in the configuration, as search input...
returns true if the pattern string(s) defined in the configurator are found in property values of object
tests if the property in theObjectPropertySett has its value set
Returns true if the configuration for the aPropertyName.aPropertyValue == valueSet
tests if the property in the configurator has its value set
Returns true if the configuration for the aPropertyName.aPropertyValue == valueSet
Tests if the property in the element has the same value as an other element property
Returns true if the configuration for the aPropertyName.aPropertyValue == valueSet
Tests if the property in the element has the same values as an other element property
Returns boolean
tests if the property in the configuration has the same value as an other configuration property
Returns true if the configuration for the aPropertyName.aPropertyValue == valueSet
Tests if the property in the element has the same values as an other element property
Returns boolean
Tests the given rule and return true/false according to the test.
aRule: a statement to evaluate during the rule test
you can use these variables to access to the properties of the configurator or of the object * propertiesObjectProfile : properties of the current object being tested * propertiesConfiguration : properties set in the configurator
you can use the other matching functions prefixing them with "this.<matchingFunction>" ex: this.MatchPropertiesSameValue('strawberry','priority','priority1')
Returns boolean
* download JamRules from github where you'd like in your project * include the following javascript libraries (provided in the 'extlib' directory)
<script type="text/javascript" src="../extlib/jQuery/jquery-2.2.4.js"></script>
<script type="text/javascript" src="../extlib/iFSM/extlib/jquery.dorequesttimeout.js"></script>
<script type="text/javascript" src="../extlib/iFSM/extlib/jquery.attrchange.js"></script>
<script type="text/javascript" src="../extlib/iFSM/iFSM.js"></script>
<script type="text/javascript" src="../extlib/jQuery-MD5/jquery.md5.js"></script>
<script type="text/javascript" src="../jamrules.js"></script>
* include JamRules
<script type="text/javascript" src="../jamrules.js"></script>
You're done!
JamRules needs to include the following javascript libraries and here's what they do:
* jQuery (>= 1.10) <script type="text/javascript" src="extlib/jQuery/jquery-3.1.1.js"></script>
* iFSM by intersel.
* This library manages finite state machines and needs these libraries:
* doTimeout by "Cowboy" Ben Alman
* this library brings some very usefull feature on the usual javascript setTimeout function like Debouncing, Delays & Polling Loops, Hover Intent...
* `<script type="text/javascript" src="extlib/iFSM/extlib/jquery.dorequesttimeout.js"></script>`
* attrchange by Selvakumar Arumugam](http://meetselva.github.io/attrchange/)
* a simple jQuery function to bind a listener function to any HTML object on attribute change
* `<script type="text/javascript" src="../extlib/iFSM/extlib/jquery.attrchange.js"></script>`
You can get the reasons why the engine did not match by accessing to the following reason property :
// notmatched function of an element
var notmatched=function(aJamRules){
var reason = aJamRules.myRulesEngine.opts.reason; //array of strings with the rules that did not match
}
// notmatched function of engine rule
var notmatched=function(aListOfObjects){
var reason = this.myRulesEngine.opts.reason; //array of strings with the rules that did not match
}
Yes.
To do that, define a "matched" function like in this example:
var myObject1 ={property1:20}
myObject1.matched=function(){
alert("it matches this object 1"+this.property1);
}
var myObject2 ={property2:40}
myObject2.matched=function(){
alert("it matches this object 2:"+this.property2);
}
rulesEngine.addPropertyObjects([myObject1,myObject2]);
If you have any ideas, feedback, requests or bug reports, you can reach me at github@intersel.org, or via my website: http://www.intersel.fr
Files (74) |
File | Role | Description | ||
---|---|---|---|---|
extlib (4 directories) | ||||
tests (8 files, 3 directories) | ||||
jamrules.js | Class | Class source | ||
LICENSE | Example | Example script | ||
README.md | Doc. | Documentation |
Files (74) | / | extlib |
File | Role | Description | ||
---|---|---|---|---|
Blapy (4 files, 1 directory) | ||||
iFSM (3 files, 1 directory) | ||||
jQuery-MD5 (3 files) | ||||
jQuery (1 file) |
Files (74) | / | extlib | / | Blapy |
File | Role | Description | ||
---|---|---|---|---|
extlib (2 files, 6 directories) | ||||
Blapy.js | Class | Class source | ||
Blapy_AnimationPlugins.js | Class | Class source | ||
LICENSE | Example | Example script | ||
README.md | Class | Class source |
Files (74) | / | extlib | / | Blapy | / | extlib |
File | Role | Description | ||
---|---|---|---|---|
iFSM (6 files, 1 directory) | ||||
jquery.appear (7 files) | ||||
json2html (4 files) | ||||
json5 (1 file) | ||||
mustache (1 file) | ||||
sammy (1 directory) | ||||
jquery-3.4.1.js | Class | Class source | ||
jquery-3.4.1.min.js | Class | Class source |
Files (74) | / | extlib | / | Blapy | / | extlib | / | iFSM |
File | Role | Description | ||
---|---|---|---|---|
extlib (8 files) | ||||
iFSM.compressed.js | Class | Class source | ||
iFSM.jquery.json | Data | Auxiliary data | ||
iFSM.js | Class | Class source | ||
LICENSE | Example | Example script | ||
package.json | Data | Auxiliary data | ||
README.md | Class | Class source |
Files (74) | / | extlib | / | Blapy | / | extlib | / | iFSM | / | extlib |
File | Role | Description |
---|---|---|
jcanvas.min.js | Class | Class source |
jquery-3.2.0.min.js | Class | Class source |
jquery.attrchange.js | Class | Class source |
jquery.dorequesttimeout.js | Example | Example script |
jquery.dotimeout.js | Aux. | Auxiliary script |
jquery.mousewheel.js | Class | Class source |
jquery.touchSwipe.js | Class | Class source |
Vector2.js | Class | Class source |
Files (74) | / | extlib | / | Blapy | / | extlib | / | jquery.appear |
File | Role | Description |
---|---|---|
AUTHORS.txt | Doc. | Documentation |
bower.json | Data | Auxiliary data |
CHANGELOG | Data | Auxiliary data |
jquery.appear.js | Class | Class source |
LICENSE | Lic. | License text |
package.json | Data | Auxiliary data |
README.md | Doc. | Documentation |
Files (74) | / | extlib | / | Blapy | / | extlib | / | json2html |
File | Role | Description |
---|---|---|
jquery.json2html.js | Class | Class source |
json2html.js | Class | Class source |
package.json | Data | Auxiliary data |
README.md | Doc. | Documentation |
Files (74) | / | extlib | / | Blapy | / | extlib | / | mustache |
File | Role | Description |
---|---|---|
mustache.js | Class | Class source |
Files (74) | / | extlib | / | Blapy | / | extlib | / | sammy | / | lib |
File | Role | Description |
---|---|---|
sammy.js | Class | Class source |
Files (74) | / | extlib | / | iFSM |
Files (74) | / | extlib | / | iFSM | / | extlib |
File | Role | Description |
---|---|---|
jcanvas.min.js | Class | Class source |
jquery-3.2.0.min.js | Class | Class source |
jquery.attrchange.js | Class | Class source |
jquery.dorequesttimeout.js | Example | Example script |
jquery.dotimeout.js | Aux. | Auxiliary script |
jquery.mousewheel.js | Class | Class source |
jquery.touchSwipe.js | Class | Class source |
Vector2.js | Class | Class source |
Files (74) | / | extlib | / | jQuery-MD5 |
File | Role | Description |
---|---|---|
jquery.md5.js | Aux. | Auxiliary script |
README.md | Doc. | Documentation |
README.txt | Doc. | Documentation |
Files (74) | / | tests |
File | Role | Description | ||
---|---|---|---|---|
css (9 files) | ||||
img (3 files) | ||||
includes (2 files) | ||||
exampleReadMe.html | Doc. | Documentation | ||
filterDocsExclusive.html | Doc. | Documentation | ||
filterDocsInclusive.html | Doc. | Documentation | ||
JamRulesExample1.html | Example | Example | ||
JamRulesPackExample.html | Example | Example | ||
JamRulesPackExemple2.html | Example | Example | ||
mastermind.html | Example | Example | ||
testJamRules.html | Example | Example |
Files (74) | / | tests | / | css |
File | Role | Description |
---|---|---|
footer.css | Data | Auxiliary data |
header.css | Data | Auxiliary data |
layout.css | Data | Auxiliary data |
login.css | Data | Auxiliary data |
mobile.css | Data | Auxiliary data |
normalize.css | Data | Auxiliary data |
page-documents.css | Data | Auxiliary data |
style.css | Data | Auxiliary data |
text.css | Data | Auxiliary data |
Files (74) | / | tests | / | img |
File | Role | Description |
---|---|---|
DL.svg | Data | Auxiliary data |
downArrow.svg | Data | Auxiliary data |
X.svg | Data | Auxiliary data |
Files (74) | / | tests | / | includes |
File | Role | Description |
---|---|---|
exampleRM.json | Data | Auxiliary data |
sectionDate.json | Data | Auxiliary data |
Version Control | Unique User Downloads | Download Rankings | |||||||||||||||
100% |
|
|
Applications that use this package |
If you know an application of this package, send a message to the author to add a link here.