A configurable rule engine written by Typescript. This project is aim to solve simple rule engine problem. Do comment on what you think of this project so that I could improve it in the future. Of course, you are welcome to pull and make a version of yourself. Thanks!
- Condition - used to evaluate data provided by
Context
- Outcome - object that return when Rule is full filled.
- Rule - set Conditions and Outcome. You can have a combination of
when().orWhen()
orwhen().andWhen()
..contextIdentifier()
is important, it used to determine when this rule will be applied in the RuleEngine process method. -
Context - store data that is going to evaluate.
id
anddata
is mandatory here, it used by Rule Engine to process with rule defined. There is a pre and post trigger can be setup as well asonEvaluate()
. The sequence for these function is preTrigger, onEvaluate, postTrigger. - ContextTrigger - function that is trigger by specific context is being evaluate.
-
Rule Engine - setup multiple context and data, then set
currentContext
and callprocess()
.
npm i && npm run ts
const rule1 = new Rule()
.id('test1')
.describe('country delivery rule')
.contextIdentifier('deliveryInMalaysia')
.when(new Condition('country.name == Malaysia'))
.andWhen(new Condition('deliveryWeightInKg <= 2'))
.thenReturn(new Outcome('delivery with POS Laju in Malaysia'));
const rule2 = new Rule()
.id('test2')
.describe('country delivery rule')
.contextIdentifier('deliveryInMalaysia')
.when(new Condition('logisticVendor == DHL'))
.andWhen(new Condition('country.name == Malaysia'))
.andWhen(new Condition('deliveryWeightInKg > 2'))
.thenReturn(new Outcome('delivery with DHL in Malaysia'));
const data = {
country: {
name: 'Malaysia',
logisticOption: 'lorry',
},
logisticVendor: null,
deliveryWeightInKg: 2
};
const ruleEngine: RuleEngine = new RuleEngine();
ruleEngine.addRule(rule1);
ruleEngine.addRule(rule2);
const trigger: EngineContextTrigger = new EngineContextTrigger('deliveryInMalaysia', PrePost.POST, (self: any) => {
console.log(self);
setTimeout(() => {
console.log('evaluate rule of delivery in Malaysia');
}, 1000);
});
const trigger2: EngineContextTrigger = new EngineContextTrigger('deliveryInMalaysia', PrePost.POST, () => {
// second trigger function
console.log('trigger some function');
});
const trigger3: EngineContextTrigger = new EngineContextTrigger('deliveryInMalaysia', PrePost.PRE, (self: any) => {
// console.log(self);
console.log('trigger a function before evaluate rule of delivery in Malaysia');
});
const appContext: EngineContext = new EngineContext('deliveryInMalaysia');
appContext.setTrigger(trigger);
appContext.setTrigger(trigger2);
appContext.setTrigger(trigger3);
ruleEngine.setContext(appContext);
ruleEngine.currentContext = 'deliveryInMalaysia';
ruleEngine.getContext('deliveryInMalaysia').data = data;
const outcome = ruleEngine.process();
console.log(outcome);
{outcome: 'delivery witht POS Laju in Malaysia', passed: true}
simulate async function
evaluate rule of dellivery Malaysia
- setup rules with json file
- complete test case
- scenario & examples
- relase to npm