Skip to content

Commit

Permalink
Merge pull request #1250 from towersxu/master
Browse files Browse the repository at this point in the history
Add LogicFlow Engine
  • Loading branch information
wumail authored Jul 28, 2023
2 parents 34c414b + dfc7f17 commit 2829668
Show file tree
Hide file tree
Showing 38 changed files with 2,174 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ module.exports = {
pragma: 'h',
},
},
ignorePatterns: ["**/*.mjs"],
ignorePatterns: ["**/*.mjs", "**/*.test.js"],
rules: {
'indent': ['error', 2, { SwitchCase: 1 }],
'linebreak-style': ['error', 'unix'],
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"sideEffects": true,
"jsdelivr": "dist/logic-flow.min.js",
"license": "Apache-2.0",
"homepage": "https://docs.logic-flow.cn",
"homepage": "https://site.logic-flow.cn",
"types": "types/index.d.ts",
"repository": {
"type": "git",
Expand Down
10 changes: 10 additions & 0 deletions packages/engine/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# engine

一个可以在JavaScript环境执行的流程引擎

## 使用方式

```shell
npm test
```

37 changes: 37 additions & 0 deletions packages/engine/__test__/01_index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import Engine from '../src/index';

describe('@logicflow/engine', () => {
test('Execution Process Completed, Returning Data Containing executionId', async () => {
const engine = new Engine();
const flowData = {
graphData: {
nodes: [
{
id: 'node1',
type: 'StartNode',
},
{
id: 'node2',
type: 'TaskNode',
}
],
edges: [
{
id: 'edge1',
sourceNodeId: 'node1',
targetNodeId: 'node2',
}
]
},
context: {},
globalData: {},
}
const flowModel = engine.load(flowData);
const result = await engine.execute();
expect(engine).toBeInstanceOf(Engine);
expect(flowModel.nodeConfigMap.size).toBe(flowData.graphData.nodes.length);
expect(result).toHaveProperty('executionId');
expect(result.status).toBe('completed');
expect(result.nodeId).toEqual('node2');
});
});
56 changes: 56 additions & 0 deletions packages/engine/__test__/02_recorder.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import Engine from '../src/index';

describe('@logicflow/engine Recorder', () => {
test('Using the getExecutionRecord API, receive the complete execution record of the process.', async () => {
const engine = new Engine();
const flowData = {
graphData: {
nodes: [
{
id: 'node1',
type: 'StartNode',
properties: {}
},
{
id: 'node2',
type: 'TaskNode',
properties: {}
}
],
edges: [
{
id: 'edge1',
sourceNodeId: 'node1',
targetNodeId: 'node2',
}
]
},
global: {},
}
engine.load(flowData);
const result = await engine.execute();
const executionId = result.executionId;
/**
* [
* {
* taskId: '',
* nodeId: '',
* instanceId: '',
* nodeType: '',
* timestamp: '',
* properties: {},
* }
* ]
*/
const execution = await engine.getExecutionRecord(executionId);
expect(execution.length).toBe(2);
expect(execution[1]).toHaveProperty('taskId');
expect(execution[1]).toHaveProperty('nodeId');
expect(execution[1]).toHaveProperty('executionId');
expect(execution[1]).toHaveProperty('nodeType');
expect(execution[1]).toHaveProperty('timestamp');
expect(execution[1]).toHaveProperty('properties');
expect(execution[1].nodeId).toBe('node2');
expect(execution[1].nodeType).toBe('TaskNode');
});
});
56 changes: 56 additions & 0 deletions packages/engine/__test__/03_condition.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import Engine from '../src/index';

describe('@logicflow/engine condition', () => {
test('The process will not continue its execution if the condition expression evaluates to false.', async () => {
const engine = new Engine();
const flowData = {
graphData: {
nodes: [
{
id: 'node1',
type: 'StartNode',
properties: {
}
},
{
id: 'node2',
type: 'TaskNode',
properties: {}
},
{
id: 'node3',
type: 'TaskNode',
properties: {}
}
],
edges: [
{
id: 'edge1',
sourceNodeId: 'node1',
targetNodeId: 'node2',
properties: {
conditionExpression: 'a === 1'
}
},
{
id: 'edge2',
sourceNodeId: 'node1',
targetNodeId: 'node3',
properties: {
conditionExpression: 'a === 2'
}
}
]
},
globalData: {
a: 2
},
}
engine.load(flowData);
const result = await engine.execute();
const execution = await engine.getExecutionRecord(result.executionId);
expect(execution.length).toBe(2);
expect(execution[1].nodeId).toBe('node3');
expect(execution[1].nodeType).toBe('TaskNode');
});
});
Loading

0 comments on commit 2829668

Please sign in to comment.