Created
August 9, 2020 20:24
-
-
Save onepointconsulting/9f64447eead7d564a5daeadfd889512f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { AzureFunction, Context, HttpRequest } from '@azure/functions' | |
import { Engine } from 'json-rules-engine' | |
/** | |
* Setup a new engine | |
*/ | |
const engine = new Engine() | |
// define a rule for detecting the player has exceeded foul limits. Foul out any player who: | |
// (has committed 5 fouls AND game is less 40 minutes) OR (has committed 6 fouls AND game is less 48 minutes) | |
engine.addRule({ | |
conditions: { | |
any: [ | |
{ | |
all: [ | |
{ | |
fact: 'gameDuration', | |
operator: 'lessThanInclusive', | |
value: 40 | |
}, | |
{ | |
fact: 'personalFoulCount', | |
operator: 'greaterThanInclusive', | |
value: 5 | |
} | |
] | |
}, | |
{ | |
all: [ | |
{ | |
fact: 'gameDuration', | |
operator: 'lessThanInclusive', | |
value: 48 | |
}, | |
{ | |
fact: 'personalFoulCount', | |
operator: 'greaterThanInclusive', | |
value: 6 | |
} | |
] | |
} | |
] | |
}, | |
event: { | |
// define the event to fire when the conditions evaluate truthy | |
type: 'fouledOut', | |
params: { | |
message: 'Player has fouled out!' | |
} | |
} | |
}); | |
engine.addRule({ | |
conditions: { | |
any: [{ | |
all: [{ | |
fact: 'gameDuration', | |
operator: 'lessThanInclusive', | |
value: 35 | |
}, { | |
fact: 'personalFoulCount', | |
operator: 'greaterThanInclusive', | |
value: 4 | |
}] | |
}] | |
}, | |
event: { // define the event to fire when the conditions evaluate truthy | |
type: 'warned', | |
params: { | |
message: 'Player got a warning!' | |
} | |
} | |
}); | |
engine.addRule({ | |
conditions: { | |
any: [{ | |
all: [{ | |
fact: 'gameDuration', | |
operator: 'greaterThan', | |
value: 48 | |
}] | |
}] | |
}, | |
event: { // define the event to fire when the conditions evaluate truthy | |
type: 'userError', | |
params: { | |
message: 'A basketball game only has 48 minutes' | |
} | |
} | |
}); | |
/** | |
* Some example facts, the first one is used by default when the rule is not defined in the request | |
*/ | |
const factCollection = [ | |
{ | |
personalFoulCount: 6, | |
gameDuration: 40 | |
}, | |
{ | |
personalFoulCount: 4, | |
gameDuration: 30 | |
} | |
] | |
const httpTrigger: AzureFunction = async function ( | |
context: Context, | |
req: HttpRequest | |
): Promise<void> { | |
context.log('Rule experiment processed a request.'); | |
const body = req.body || factCollection[0]; | |
// Run the engine to evaluate | |
const results = await engine.run(body); | |
context.res = { | |
// status: 200, /* Defaults to 200 */ | |
body: results, | |
headers: { | |
'Content-Type': 'application/json' | |
} | |
} | |
} | |
export default httpTrigger |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment