@ edgerules/ 便携式
脚本类型定义 边缘规则可移植 JSON 格式 - 犬科序列化计划 边缘规则模型、函数、类型和错误.
此软件包具有 TypeScript 类型和一个运行时常数, Kinds: 常见 @kind 组别
service.get(path, kinds?)。在浏览器和Node.js项目中都可以安全使用.
安装
npm install @edgerules/portable
什么是便携式JSON?
每个边缘规则模型都是平坦的JSON对象. 便携式格式是您如何在数据库中存储一个模型, 发送到
HTTP,并传递给 DecisionService ——没有专门的系列化库.
- 每个对象节点都有一个
@kind。仅限@kind, (中文(简体) ),@node,以及@name拥有@前缀. - 样板根是
PortableContext数字 :{ '@kind': 'context', fields: { ... } }。 。 。 . - 机构声明有一个紧凑的接口节点(
*-interface没有尸体.
@kind | 类型脚本 | 说明 |
|---|---|---|
context, (中文(简体) ), workbook | PortableContext | 记录 fields页:1 workbook 仅为根 |
placeholder | PortablePlaceholderValue | 输入槽——主机供应值 |
expression, (中文(简体) ), expression-interface | PortableExpression, (中文(简体) ), PortableExpressionInterface | 以其 variables |
invocation | PortableInvocation | 调用模型中的用户方法 |
type | PortableType | 用户定义类型 : fields 或者说 alias |
facts | PortableFacts | 结构类型的衍生字段 |
method, (中文(简体) ), method-interface | PortableMethod, (中文(简体) ), PortableMethodInterface | func数字 : parameters, (中文(简体) ), return, (中文(简体) ), body |
ruleset, (中文(简体) ), ruleset-interface | PortableRuleset, (中文(简体) ), PortableRulesetInterface | 决定表: hitPolicy, (中文(简体) ), rules, (中文(简体) ), default |
loop, (中文(简体) ), loop-interface | PortableLoop, (中文(简体) ), PortableLoopInterface | 国家机器: state, (中文(简体) ), do, (中文(简体) ), output |
optimise, (中文(简体) ), optimise-interface | PortableOptimise, (中文(简体) ), PortableOptimiseInterface | LP/MILP : (英语) variables目标, constraints |
external-method | PortableExternalMethod | external func数字 : parameters页:1 return |
decision, (中文(简体) ), decision-interface | PortableDecision, (中文(简体) ), PortableDecisionInterface | decision数字 : context, (中文(简体) ), body链接 requires |
service, (中文(简体) ), service-interface | PortableService, (中文(简体) ), PortableServiceInterface | service数字 : parameters, (中文(简体) ), body链接 requires |
error | PortableError | 结构错过主机边界 |
使用量
型号建筑
import type { PortableContext, PortablePlaceholderValue } from '@edgerules/portable';
const ageInput: PortablePlaceholderValue = {
'@kind': 'placeholder',
type: 'number',
required: true,
};
const model: PortableContext = {
'@kind': 'context',
fields: {
minAge: 18,
applicant: {
'@kind': 'context',
fields: {
age: ageInput,
income: { '@kind': 'placeholder', type: 'number', default: 0 },
},
},
isEligible: 'applicant.age >= minAge and applicant.income >= 1000',
},
};
定义方法
import type { PortableMethod } from '@edgerules/portable';
const isEligible: PortableMethod = {
'@kind': 'method',
parameters: { age: 'number', income: 'number' },
body: {
'@kind': 'context',
fields: { result: 'age >= 18 and income >= 1000' },
},
};
定义类型
import type { PortableType } from '@edgerules/portable';
const CustomerType: PortableType = {
'@kind': 'type',
fields: {
name: { '@kind': 'placeholder', type: 'string', required: true },
age: 'number',
tier: { '@kind': 'placeholder', type: 'string', default: 'STANDARD', enum: ['GOLD', 'SILVER', 'STANDARD'] },
},
};
要求规则(援引)
import type { PortableInvocation } from '@edgerules/portable';
const call: PortableInvocation = {
'@kind': 'invocation',
method: 'risk',
arguments: { age: 'applicant.age', income: 'applicant.income' },
};
用类过滤器读取
import { Kinds } from '@edgerules/portable';
service.get('*', Kinds.METHODS); // methods, rulesets, loops, optimise declarations
service.get('*', Kinds.EXTERNALS); // the resolver-wiring catalog
service.get('*', [...Kinds.EXPRESSION_INTERFACES, ...Kinds.TYPES, ...Kinds.INVOCABLE_INTERFACES]); // project explorer
处理错误
import type { PortableError, PortableErrorType } from '@edgerules/portable';
function handleError(err: unknown): void {
const error = err as PortableError;
switch (error.type as PortableErrorType) {
case 'EntryNotFound':
console.error('Path not found:', error.path);
break;
case 'WrongFilter':
console.error('Unknown @kind in kinds:', error.message);
break;
default:
console.error(error.message);
}
}
密钥类型
// The root model object, a nested record, or a root workbook
PortableContext
// Any node returned by `service.get(path, kinds?)`
PortableNode
// Any value `service.set(path, value)` accepts
PortableSetValue
// A filterable @kind, and the common groups of them
Kind
Kinds
// The base of each method-like interface: { parameters, return? }
MethodDefinition
// A structured error from the engine, and its discriminant
PortableError
PortableErrorType // "EntryNotFound" | "WrongFieldPath" | "DuplicateName" | "Parse" | "Execution" | "WrongFilter"
相关软件包
@edgerules/node——节点js决策服务@edgerules/web——浏览器决定服务