@ dedgerules/ web( 网络规则)
边缘规则 浏览器二号舰:
| 导出 | 说明 |
|---|---|
@edgerules/web | 生产建设——只执行(DecisionService(中文(简体) ) |
@edgerules/web/mutable | 发展建设——执行+CRUDMutableDecisionService(中文(简体) ) |
浏览器软件包是 API- 相同到 @edgerules/node;仅限
基础 WASM 加载器不同( 浏览器) fetch 对节点.js fs) (中文(简体) ). 选择此软件包用于 Vite 、 Web package 和其他
浏览器捆绑器设置 .
安装
npm install @edgerules/web @edgerules/portable
仅限无害环境管理。 WASM 二进制在第一次使用时同步加载.
生产用途
从边框规则 DSL 字符串装入
import {DecisionService} from '@edgerules/web';
const service = new DecisionService(`{
applicant: {
age: <number, required: true>
income: <number, default: 0>
}
isEligible: applicant.age >= 18 and applicant.income >= 1000
}`);
// Execute the whole model against a request
const result = service.execute('*', {applicant: {age: 25, income: 2500}});
// { applicant: { age: 25, income: 2500 }, isEligible: true }
从便携式JSON型号装入
import {DecisionService} from '@edgerules/web';
import type {PortableContext} from '@edgerules/portable';
const model: PortableContext = {
'@kind': 'context',
fields: {
minAge: 18,
minIncome: 1000,
applicant: {
'@kind': 'context',
fields: {age: {'@kind': 'placeholder', type: 'number'}, income: {'@kind': 'placeholder', type: 'number'}},
},
isEligible: 'applicant.age >= minAge and applicant.income >= minIncome',
},
};
const service = new DecisionService(model);
const result = service.execute('isEligible', {applicant: {age: 30, income: 3000}});
// true
处理错误
import type {PortableError} from '@edgerules/portable';
try {
service.execute('nonExistentField');
} catch (err) {
const error = err as PortableError;
console.error(error.type); // "EntryNotFound"
console.error(error.message); // human-readable, safe to surface in UI
}
可变/ 开发用法
使用 ./mutable 用于规则编辑器、视觉构建器和测试工具的子路径. 不要在其中使用此构建
生产.
import {Kinds, MutableDecisionService} from '@edgerules/web/mutable';
const service = new MutableDecisionService(initialModel);
// Inspect a field with inferred type information
const node = service.get('applicant.age');
// { "@kind": "placeholder", "type": "number", "required": true, "writeOnly": true, "linked": true }
// Hot-update a rule
service.set('minAge', 21);
// Add a new typed input
service.set('applicant.creditScore', {'@kind': 'placeholder', type: 'number', default: 0});
// Remove a field
service.remove('applicant.creditScore');
// Read the full model snapshot
const snapshot = service.get('*');
// Read the method signatures only
const signatures = service.get('*', Kinds.METHOD_INTERFACES);
// Read/write a node as DSL source text instead of Portable JSON
const code = service.getCode('minAge'); // '21'
service.setCode('minAge', '18'); // returns a PortableNode, not code text
API 参考
DecisionService
| 方法 | 说明 |
|---|---|
new DecisionService(model) | 从 DSL 字符串或根创建 PortableContext |
execute(method, args?) | 评价 method (字段路径、功能或 "*")反对选择性要求的约束 |
execute 细节
method: "*"——评价每个顶级场并返回完整的模型结果.method: "person.isAdult"评估并返回这个单一领域.method: "validateApplicant"调用一个命名函数;通过args。 。 。 .args——一个模型形状的对象,其条目被绑定为外部输入. 嵌入式打字孔遵循模型形状 (例如:.{ applicant: { age: 30 } }页:1applicant: { age: <number> }) (中文(简体) ).
MutableDecisionService (德文建设)
继承 execute 并添加:
| 方法 | 说明 |
|---|---|
get(path, kinds?) | 读取节点, 通过过滤 @kind (见 Kinds(三); linked 告诉模型是否链接 |
set(path, value) | 写入表达式、占位符、上下文、方法或类型 |
getCode(path) | 将节点读作 Edge Rules DSL 源文本 |
setCode(path, code) | 从 Edge Rules DSL 源文本中写入节点; 返回 a PortableNode代码文本 |
remove(path) | 删除给定路径上的条目 |
rename(oldPath, newPath) | 在上下文中重命名一个条目 |
相关软件包
@edgerules/node- 节点的API.js相同@edgerules/portable- 便携式JSON类型定义