Recommend this page to a friend! |
Classes of Emmanuel Podvin | Jam Rules | README.md | Download |
|
|
DownloadJamRulesJavascript/jQuery filtering tool that helps to filter objects among a set of objects according to rules. What is JamRules?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) How it works...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
Identify the properties used to filterThe 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. :
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... Add objectsUse the function addPropertyObjects of your rules engine to add your objects and the behavior of the selected and not selected objects by jamRules:
Filtering configurationGenerally, 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.
Define the rules to select objectsA 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...
Test your objects against the rules...Once done, we will be able to run our jamrules engine with 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.... Demos
Create the JamRules object: jamrules.build(options)
the options parameterdebugif true, the rule engine will send debug message on the console the "Matched" and "NotMatched" functionsThe "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... "matchedFunctionName" and "notmatchedFunctionName" optionsThese 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... The JamRules ObjectsIn 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...
Internally, the objects are formatted in order to process the matching functions and rules, the internal format of your data in jamrules will be :
eg:
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 ConfiguratorThe 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.
The JamRules rulesRules setWhen "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. RulesA 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 Example
Adding Objects to test by JamRulesaddPropertyObjects(Objects <, aMatchingFunction, aNotMatchingFunction>)Add objects to the list of objects to test against rules. Objects*: array of objects with their properties plus these optional ones:
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 Example
addPropertyObject(anObject<, aMatchingFunction, aNotMatchingFunction>)Add an object to the list of objects to test against rules. anObject* with its properties plus these optional ones
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 Example
addObject(anObject)Add an object to the list of objects to test against rules. parameters
Example
_addObject(anObject) - static functionRemark: 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. parameters
Example
Creating rules set and rulescreateRulesSet(aRulesGroup, ruleEvents)Creates a rule set. parameters
Example
addRule(aRulesGroup, aRuleName, aRuleTest)Add a new "and" rule in aRulesGroup. parameters
Example
Run JamRulescompileRulesInitialize the rule engine - to do before action and after adding new rules Example
runRulesEngineRun the rules engine. Example
Filtering configuratorselectConfigurationPropertyValue(aPropertyName, aPropertyValue, doTest)Select a value in the filtering configurator as a radio would do: unselecting other values of aPropertyName. parameters
RemarksIf "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. Example
checkConfigurationPropertyValue(aPropertyName,aPropertyValue,aStatus, doTest)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. parameters
RemarksIf "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. Example
resetConfigurationPropertyValues(aPropertyName)reset a property by setting all its property values to a false status in the rules configurator parameters
RemarksExample
resetConfigurationProperty(aPropertyName)reset a property completely parameters
RemarksExample
The Available filtering functions for "addRule"MatchProperty(aPropertyName)Tests if at least a property value of a property is shared between the configuration and the object parameters
returnsReturns true if any property value for a given aPropertyName is set in the profile object and in the configuration property set Example
MatchPropertySearch(aPropertyName)Test if a string aPropertyName is found as a property value of objects. Generally used for text input as search input. parameters
returnsreturns true if the pattern string(s) defined in the configurator are found in property values of object Example
MatchPropertyValue(aPropertyName,aPropertyValue)Tests if a given property value is set for configuration and the object parameters
returnsReturns true if the configuration for the aPropertyName.aPropertyValue == the one defined for the current objectProfile being tested Example
MatchPropertiesSameValue(aConfigurationPropertyName,anObjectPropertyName,aPropertyValue)Tests if a property value of a property is set for the configurator and the object parameters
returnsReturns true if aPropertyValue in aConfigurationPropertyName and in anObjectPropertyName are both set. Example
MatchPropertiesSameValues(aConfigurationPropertyName,anObjectPropertyName)tests the property values set for the configurator's property and the object's property and if they are the same between the two parameters
returnsReturns true if all properties values of aConfigurationPropertyName and of anObjectPropertyName are both set Example
MatchProperties(aConfigurationPropertyName,anObjectPropertyName)Tests if at least a property value exists and is set between the configurator property and the object property parameters
returnsreturns true if it exists a value of aConfigurationPropertyName that is the same that in anObjectPropertyName Example
MatchObjectSearch(aConfigurationPropertyName,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... parameters
returnsreturns true if the pattern string(s) defined in the configurator are found in property values of object Example
ObjectPropertySet(aPropertyName,aPropertyValue,valueSet)tests if the property in theObjectPropertySett has its value set parameters
returnsReturns true if the configuration for the aPropertyName.aPropertyValue == valueSet ExampleConfigurationPropertySet(aPropertyName,aPropertyValue,valueSet)tests if the property in the configurator has its value set parameters
returnsReturns true if the configuration for the aPropertyName.aPropertyValue == valueSet ExampleObjectPropertiesSameValue(aPropertyName1,aPropertyName2,aPropertyValue)Tests if the property in the element has the same value as an other element property parameters
returnsReturns true if the configuration for the aPropertyName.aPropertyValue == valueSet ExampleObjectPropertiesSameValues(aPropertyName1,aPropertyName2)Tests if the property in the element has the same values as an other element property parameters
returnsReturns boolean ExampleConfigurationPropertiesSameValue(aPropertyName1,aPropertyName2,aPropertyValue)tests if the property in the configuration has the same value as an other configuration property parameters
returnsReturns true if the configuration for the aPropertyName.aPropertyValue == valueSet ExampleConfigurationPropertiesSameValues(aPropertyName1,aPropertyName2)Tests if the property in the element has the same values as an other element property parameters
returnsReturns boolean ExampleMatchExternalRule(aRule)Tests the given rule and return true/false according to the test. parametersaRule: 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') returnsReturns boolean Example
Install JamRules* download JamRules from github where you'd like in your project * include the following javascript libraries (provided in the 'extlib' directory)
* include JamRules
You're done! Library DependenciesJamRules needs to include the following javascript libraries and here's what they do:
* jQuery (>= 1.10)
Official websiteFAQHow to get why the rule did not matchYou can get the reasons why the engine did not match by accessing to the following reason property :
Can I define a match function different for each object?Yes. To do that, define a "matched" function like in this example:
ContactIf 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 |