File: tests/JamRulesPackExample.html

Recommend this page to a friend!
  Classes of Emmanuel Podvin   Jam Rules   tests/JamRulesPackExample.html   Download  
File: tests/JamRulesPackExample.html
Role: Example script
Content type: text/plain
Description: Example
Class: Jam Rules
Match objects against a list of rules
Author: By
Last change: add new example filterDocs
Date: 2 years ago
Size: 5,434 bytes
 

Contents

Class file image Download
<html> <head> <title>JamRules Pack Example</title> <meta charset="utf-8" /> <script type="text/javascript" src="../extlib/jQuery/jquery-3.2.0.min.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> </head> <body id="jamrules"> <h1>Raw test of jamrules</h1> <div id="msg"></div> <script> //initialisation of jamrules and its configurator /* var jamrules2 = function(aJqueryObj,options){ jamrules.call(this, aJqueryObj,options); }; jamrules2.prototype = new jamrules(); var rulesEngine = new jamrules2($('body'),{debug:true}); */ //add objects that we want to test with jamrules var matched=function(){ var msg; msg="<p style='color:green;font-weight:bold;'>This pack has a promo coupon</p>"; $("#msg").append("<h3>configuration of this pack:</h3><pre>"+JSON.stringify(this)+"</pre>"+msg); }; var notmatched=function(aJamRules){ var msg; var reason = aJamRules.myRulesEngine.opts.reason; msg="<p style='color:red'>This pack is has no coupon</p>"; $("#msg").append("<h3>configuration of this pack:</h3><pre>"+JSON.stringify(this)+"</pre>"+msg); $.each(reason,function(index,value) { if (value.indexOf("DefaultState")==-1) $("#msg").append('reason:'+index+' '+reason[index]); }); }; var rulesEngine = jamrules.build({jqueryObj:$('#jamrules'),debug:true,name:"engine1"}); var pack1 = { propertiesSet : { object1 : { trouser : 1 }, object2 : { shirt : 1 }, object1Color : { white : 1 }, object2Color : { blue : 1 } }, matched : matched, notmatched : notmatched }; var pack2 = { propertiesSet : { object1 : { trouser : 1 }, object2 : { trouser : 1 }, object1Color : { white : 1 }, object2Color : { blue : 1 } }, matched : matched, notmatched : notmatched }; var pack3 = { propertiesSet : { object1 : { trouser : 1 }, object2 : { trouser : 1 }, object1Color : { white : 1 }, object2Color : { white : 1 } }, matched : matched, notmatched : notmatched }; jamrules._addObject(pack1); rulesEngine.addObject(pack2); rulesEngine.addObject(pack3); // rules setting rulesEngine.createRulesSet("SameColorTrousersPack"); // we'd like to test a pack for giving it a promo coupon because it has 2 trousers of same color // so, does our current pack being tested have a trouser for object1 property? rulesEngine.addRule("SameColorTrousersPack","O1Trouser",'ObjectPropertySet("object1","trouser")'); // yes? ok... do we have a trouser for object2 property too in our pack? rulesEngine.addRule("SameColorTrousersPack","O2Trouser",'ObjectPropertiesSameValue("object1","object2")'); // yes? ok... is the color of the trouser is of same color? rulesEngine.addRule("SameColorTrousersPack","O1O2SameColor",'ObjectPropertiesSameValue("object1Color","object2Color")'); // if gone up here implies that the pack has two trousers of same color... // then jamrules will call the match function for this pack // prepare the rule engine rulesEngine.compileRules(); // $("#msg").append("<h2>run the test to get the packs that match the rules... </h2>"); rulesEngine.runRulesEngine(); var pack21 = { object1 : 'trouser', object2 : 'trouser', object1Color : 'blue', object2Color : 'blue' }; var pack22 = { object1 : 'shirt', object2 : 'shirt', object1Color : 'blue', object2Color : 'blue' }; var pack23 = { object1 : 'trouser', object2 : 'trouser', object1Color : 'red', object2Color : 'red' }; var rulematched=function(aListOfMatchedObjects){ var msg; msg="<p style='color:green;font-weight:bold;'>The Rule matched for an element profile of "+aListOfMatchedObjects.length+" element(s)!</p>"; $("#msg").append("<h3>"+msg+"</h3><pre>"); }; var rulenotmatched=function(aListOfMatchedObjects){ var msg; msg="<p style='color:green;font-weight:bold;'>The Rule did NOT matched for an element profile of "+aListOfMatchedObjects.length+" element(s)!</p>"; $("#msg").append("<h3>"+msg+"</h3><pre>"); }; var rulesEngine2 = jamrules.build({debug:true,name:"engine2",matched:rulematched,notmatched:rulenotmatched}); rulesEngine2.addObject({propertiesSet:jamrules._translateToJamrulesProperties(pack21),matched : matched,notmatched : notmatched}); rulesEngine2.addPropertyObject(pack22); rulesEngine2.addPropertyObject(pack23,matched,notmatched); rulesEngine2.createRulesSet("SameColorTrousersPack"); rulesEngine2.addRule("SameColorTrousersPack","O1Trouser",'ObjectPropertySet("object1","trouser")'); rulesEngine2.addRule("SameColorTrousersPack","O2Trouser",'ObjectPropertiesSameValue("object1","object2")'); rulesEngine2.addRule("SameColorTrousersPack","O1O2SameColor",'ObjectPropertiesSameValue("object1Color","object2Color")'); rulesEngine2.compileRules(); $("#msg").append("<h2>run the test to get the packs that match the rules... </h2>"); rulesEngine2.runRulesEngine(); </script> </body> </html>