|
| 1 | +# Rules Engine |
| 2 | +--- |
| 3 | +#### Author : Digi-Corp (Ashish & Chandrakant) |
| 4 | +##### Created on : 2th Nov 2015 |
| 5 | +<!-- ##### Update Date : 24th Nov 2015 --> |
| 6 | +--- |
| 7 | +This Rules Engine is used to bind the rules and gets the result based on rules. It have 'RulesEngine()' function which returns 'RE' Object. You require to import 'assertHelper.js' file for use this rules engine library.It is also require user to set **defaultResult** before using getResult() method. |
| 8 | +- Create Instance Of RulesEngine |
| 9 | +- Useful Functions |
| 10 | + - addRule() |
| 11 | + - removeRule() |
| 12 | + - getResult() |
| 13 | + - print() |
| 14 | + - getRule() |
| 15 | + - getAllRules() |
| 16 | +- variable |
| 17 | + - defaultResult |
| 18 | + |
| 19 | +## Create Instance Of RulesEngine |
| 20 | +`To use RulesEngine Library you require to create RulesEngine instance through that you can call it's functions.` |
| 21 | + |
| 22 | +- Example |
| 23 | + |
| 24 | + ```javascript |
| 25 | + var RulesEngine = new RulesEngine(); |
| 26 | + ``` |
| 27 | +`Here 'RulesEngine' is instance through this you can Call RulesEngine functions.` |
| 28 | + |
| 29 | +## addRule(rule,result) |
| 30 | + `This method is used to add rules into 'RulesEngine' Object.It returns index of current rule into 'RulesEngine' object` |
| 31 | + |
| 32 | +Parameter Name | Descriptions |
| 33 | +:------------- | :------------------------------------------------------------------------ |
| 34 | +rule | This contains array of JSON objects that defines rule |
| 35 | +result | This contains JSON objects that defines result with respect to given rule |
| 36 | + |
| 37 | +- Attributes for rule and result Object |
| 38 | + |
| 39 | +| Parameter | Attributes | DataType | Possible Values| |
| 40 | +| :------------- | :------------- |:------------- |:------------- | |
| 41 | +| rule | field | string | Any | |
| 42 | +| | filterType | string | between, in, notin, lt, lteq, gt, gteq, eq, nteq | |
| 43 | +| | value | array | Any | |
| 44 | +| result | Any | Any | Any | |
| 45 | + |
| 46 | +- Example |
| 47 | + |
| 48 | + ```javascript |
| 49 | + RulesEngine.addRule([{field:"growth",filterType:"between",value:[26, 35]}, |
| 50 | + {field:"slope",filterType:"between",value:[26,35]}], |
| 51 | + {'Color': '#1A9850','Comment': 'Between'}); |
| 52 | + ``` |
| 53 | + |
| 54 | + Here,rule is binded for <br/> |
| 55 | + |
| 56 | + 1> growth is Between 26 to 35. <br/> |
| 57 | + |
| 58 | + 2> slope is Between 26 to 35. |
| 59 | + |
| 60 | + with result 'Color'='#1A9850' and 'Comment'='Between' |
| 61 | + |
| 62 | +-------------------------------------------------------------------------------- |
| 63 | + |
| 64 | +## removeRule(index) |
| 65 | +`This method is used to remove the rule from specific index of 'RulesEngine' object.` |
| 66 | + |
| 67 | +Parameter Name | Descriptions |
| 68 | +:------------- | :----------------------------------------------------- |
| 69 | +index | This defines index of rules into 'RulesEngine' object |
| 70 | + |
| 71 | +- Example |
| 72 | + |
| 73 | +```javascript |
| 74 | + RulesEngine.removeRule(0); |
| 75 | +``` |
| 76 | + Here, It will remove first rule from index 0 of 'RulesEngine' object. |
| 77 | + |
| 78 | +-------------------------------------------------------------------------------- |
| 79 | + |
| 80 | +## getResult(values) |
| 81 | +`This method compare the given value with predefined rules and gives result object based on comaprision` |
| 82 | + |
| 83 | +Parameter Name | Descriptions |
| 84 | +:------------- | :-------------------------------------------------------------------------- |
| 85 | +values | This defines JSON object which contains values to be compare against rules. |
| 86 | + |
| 87 | +- Example |
| 88 | + |
| 89 | +```javascript |
| 90 | +RulesEngine.addRule([{field:"growth",filterType:"between",value:[26, 35]}, |
| 91 | + {field:"slope",filterType:"between",value:[26,35]}], |
| 92 | + {'Color': '#1A9850','Comment': 'Between'}); |
| 93 | + |
| 94 | +retRestult=RulesEngine.getResult({growth:26,slope:29}); |
| 95 | + |
| 96 | +``` |
| 97 | + |
| 98 | +Here,<br/> |
| 99 | + |
| 100 | +retRestult.Color contains '#1A9850' and retRestult.Comment contains 'Between' |
| 101 | + |
| 102 | +--- |
| 103 | + |
| 104 | +## print() |
| 105 | +`This method print Entire rules with result and also defaultResult.` |
| 106 | + |
| 107 | +- Example |
| 108 | + |
| 109 | +```javascript |
| 110 | +RulesEngine.print() |
| 111 | +``` |
| 112 | +--- |
| 113 | +### defaultResult |
| 114 | +`defaultResult variable defines default result when no rules mathced.You must require to set default result before use getResult method.` |
| 115 | + |
| 116 | +- Example |
| 117 | +```javascript |
| 118 | +RulesEngine.defaultResult={'Color': '#D73027','Comment': 'Default Message'}; |
| 119 | +``` |
| 120 | + |
| 121 | +--- |
| 122 | + |
| 123 | +## getRule(index) |
| 124 | +`This method is used to get rule from specific index.` |
| 125 | + |
| 126 | +Parameter Name | Descriptions |
| 127 | +:------------- | :----------------------------------------------------- |
| 128 | +index | This defines index of rules into 'RulesEngine' object |
| 129 | + |
| 130 | +- Example |
| 131 | + |
| 132 | +```javascript |
| 133 | +RulesEngine.getRule(0) |
| 134 | +``` |
| 135 | + Here, It will return first rule from index 0 of 'RulesEngine' object. |
| 136 | + |
| 137 | +--- |
| 138 | + |
| 139 | +## getAllRules() |
| 140 | +`This method is used to get all rules of 'RulesEngine' object.` |
| 141 | + |
| 142 | +- Example |
| 143 | + |
| 144 | +```javascript |
| 145 | +RulesEngine.getAllRules() |
| 146 | +``` |
| 147 | + Here, It will return all rules of 'RulesEngine' object. |
0 commit comments