Skip to content

Allow Named Conditions #332

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 1 commit into from
Jun 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions examples/02-nested-boolean-logic.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ async function start () {
fact: 'personalFoulCount',
operator: 'greaterThanInclusive',
value: 5
}]
}],
name: "short foul limit"
}, {
all: [{
fact: 'gameDuration',
Expand All @@ -43,7 +44,8 @@ async function start () {
operator: 'lessThan',
value: 6
}
}]
}],
name: "long foul limit"
}]
},
event: { // define the event to fire when the conditions evaluate truthy
Expand Down
3 changes: 3 additions & 0 deletions src/condition.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ export default class Condition {
if (this.priority) {
props.priority = this.priority
}
if (this.name) {
props.name = this.name
}
const oper = Condition.booleanOperator(this)
if (oper) {
if (Array.isArray(this[oper])) {
Expand Down
2 changes: 2 additions & 0 deletions test/condition.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ function condition () {
return {
all: [{
id: '6ed20017-375f-40c9-a1d2-6d7e0f4733c5',
name: "team participation in form",
fact: 'team_participation',
operator: 'equal',
value: 50,
Expand All @@ -29,6 +30,7 @@ describe('Condition', () => {
expect(subject).to.have.property('operator')
expect(subject).to.have.property('value')
expect(subject).to.have.property('path')
expect(subject).to.have.property('name');
})

it('boolean conditions have properties', () => {
Expand Down
92 changes: 56 additions & 36 deletions test/engine-event.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,19 @@ describe('Engine: event', () => {
*/
function simpleSetup () {
const conditions = {
any: [{
fact: 'age',
operator: 'greaterThanInclusive',
value: 21
}, {
fact: 'qualified',
operator: 'equal',
value: true
}]
any: [
{
name: 'over 21',
fact: 'age',
operator: 'greaterThanInclusive',
value: 21
},
{
fact: 'qualified',
operator: 'equal',
value: true
}
]
}
engine = engineFactory()
const ruleOptions = { conditions, event, priority: 100 }
Expand All @@ -50,25 +54,32 @@ describe('Engine: event', () => {
*/
function advancedSetup () {
const conditions = {
any: [{
fact: 'age',
operator: 'greaterThanInclusive',
value: 21
}, {
fact: 'qualified',
operator: 'equal',
value: true
}, {
all: [{
fact: 'zipCode',
operator: 'in',
value: [80211, 80403]
}, {
fact: 'gender',
operator: 'notEqual',
value: 'female'
}]
}]
any: [
{
fact: 'age',
operator: 'greaterThanInclusive',
value: 21
},
{
fact: 'qualified',
operator: 'equal',
value: true
},
{
all: [
{
fact: 'zipCode',
operator: 'in',
value: [80211, 80403]
},
{
fact: 'gender',
operator: 'notEqual',
value: 'female'
}
]
}
]
}
engine = engineFactory()
const ruleOptions = { conditions, event, priority: 100 }
Expand All @@ -91,6 +102,7 @@ describe('Engine: event', () => {
expect(ruleResult.result).to.be.true()
expect(ruleResult.conditions.any[0].result).to.be.true()
expect(ruleResult.conditions.any[0].factResult).to.equal(21)
expect(ruleResult.conditions.any[0].name).to.equal('over 21')
expect(ruleResult.conditions.any[1].result).to.be.false()
expect(ruleResult.conditions.any[1].factResult).to.equal(false)
}
Expand Down Expand Up @@ -177,11 +189,13 @@ describe('Engine: event', () => {
params: drinkOrderParams
}
const drinkOrderConditions = {
any: [{
fact: 'canOrderDrinks',
operator: 'equal',
value: true
}]
any: [
{
fact: 'canOrderDrinks',
operator: 'equal',
value: true
}
]
}
const drinkOrderRule = factories.rule({
conditions: drinkOrderConditions,
Expand All @@ -193,7 +207,10 @@ describe('Engine: event', () => {
engine.on('success', function (event, almanac, ruleResult) {
switch (event.type) {
case 'setDrinkingFlag':
almanac.addRuntimeFact('canOrderDrinks', event.params.canOrderDrinks)
almanac.addRuntimeFact(
'canOrderDrinks',
event.params.canOrderDrinks
)
break
case 'offerDrink':
expect(event.params).to.eql(drinkOrderParams)
Expand Down Expand Up @@ -257,7 +274,9 @@ describe('Engine: event', () => {
expect(ruleResult.conditions.any[1].factResult).to.equal(false)
expect(ruleResult.conditions.any[2].result).to.be.false()
expect(ruleResult.conditions.any[2].all[0].result).to.be.false()
expect(ruleResult.conditions.any[2].all[0].factResult).to.equal(ZIP_CODE)
expect(ruleResult.conditions.any[2].all[0].factResult).to.equal(
ZIP_CODE
)
expect(ruleResult.conditions.any[2].all[1].result).to.be.false()
expect(ruleResult.conditions.any[2].all[1].factResult).to.equal(GENDER)
}
Expand Down Expand Up @@ -375,7 +394,8 @@ describe('Engine: event', () => {
rule.on('success', successSpy)
await engine.run()
const ruleResult = successSpy.getCall(0).args[2]
const expected = '{"conditions":{"priority":1,"any":[{"operator":"greaterThanInclusive","value":21,"fact":"age","factResult":21,"result":true},{"operator":"equal","value":true,"fact":"qualified","factResult":false,"result":false}]},"event":{"type":"setDrinkingFlag","params":{"canOrderDrinks":true}},"priority":100,"result":true}'
const expected =
'{"conditions":{"priority":1,"any":[{"name":"over 21","operator":"greaterThanInclusive","value":21,"fact":"age","factResult":21,"result":true},{"operator":"equal","value":true,"fact":"qualified","factResult":false,"result":false}]},"event":{"type":"setDrinkingFlag","params":{"canOrderDrinks":true}},"priority":100,"result":true}'
expect(JSON.stringify(ruleResult)).to.equal(expected)
})
})
Expand Down
7 changes: 4 additions & 3 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,11 @@ interface ConditionProperties {
path?: string;
priority?: number;
params?: Record<string, any>;
name?: string;
}

type NestedCondition = ConditionProperties | TopLevelCondition;
type AllConditions = { all: NestedCondition[] };
type AnyConditions = { any: NestedCondition[] };
type NotConditions = { not: NestedCondition };
type AllConditions = { all: NestedCondition[]; name?: string; priority?: number; };
type AnyConditions = { any: NestedCondition[]; name?: string; priority?: number; };
type NotConditions = { not: NestedCondition; name?: string; priority?: number; };
export type TopLevelCondition = AllConditions | AnyConditions | NotConditions;