UNPKG

mutants-appfx

Version:

appfx module

64 lines (55 loc) 1.3 kB
import nools from 'nools'; import { api } from 'mutants-microfx'; import DataProviderManager from '../DataProviderManager'; let gid = 0; // 分配全局id const assignId = () => { // 每次加一 gid = gid + 1; return gid; } const toArray = (val) => { return Array.isArray(val) ? val : [val]; } export default class RuleSet { flow; name; constructor(noolsSource = [], define, scope) { this.name = 'RuleSet' + assignId(); this.flow = nools.compile(noolsSource, { define, scope, name: this.name }); } execute(facts) { const session = this.flow.getSession(...facts); return session.match(() => { session.dispose(); }); } dispose() { nools.deleteFlow(this.name); this.flow = null; } static async load({ sysid, mid }, define, scope) { const rules = (await DataProviderManager.getRule({sysid,mid})).data.data; const src = rules.reduce((ret, it) => { if (typeof it === 'string') { ret.push(it); } else { ret.push("rule '" + it.name + "' {" + "when {" + toArray(it.when).join(';') + "}" + "then {" + toArray(it.then).join(';') + "}" + "}"); } return ret; }, []); return new RuleSet(src.join('\n'), define, scope); } }