|
Project Information
Links
|
INTRODUCTIONCheetah is a workflow suite writting in Java, including engine and eclipse plug-in designer. Cheetah is made for simplifying complex business logic and improving efficiency. Cheetah是一个用Java编写的工作流开发套件,其中包含了工作流引擎和可视化设计器(基于Eclipse插件开发)。Cheetah可用于系统中简化复杂的业务逻辑以及提高开发效率。 What kind of project is Cheetah suitable for? Complex logic (too much if-else, deep nested classes or methods...) ones, e.g., fuzzy search engine. 什么样的项目可以考虑使用Cheetah?逻辑复杂(if-else众多,类、方法嵌套层次较深……)的项目,如模糊搜索。 GETTING STARTEDWe make a very simple example to show how to use Cheetah. 我们将用一个极简单的例子来说明如何使用Cheetah。 Input a positive integer, print the Collatz sequence from this integer. Collatz Conjecture 输入一个正整数,打印出从这个数开始的考拉兹数列。考拉兹猜想
Define the following workflow:
定义流程如下:
Install the plug-in of Cheetah designer in Eclipse, create a new file named "colltaz.workflow". Drag the left components to the right canvas, add the arrows to indicate the workflow. 在Eclipse中安装Cheetah设计器插件,新建文件“collatz.workflow”。拖动左侧的控件到右侧画布,并使用箭头指示流程。
In the above chart, "Start" means the start of the workflow, "End" means the end. The orange rectangle means a handling process, cyan hexagon means a switch node. Letter "Y" and "N" are the comments written to help people understand the workflow chart. 上图中,“Start”表示工作流的开始节点,“End”表示结束节点。橙色的矩形节点表示一个流程步骤,青色的六边形表示一个分支步骤。字母“Y”“N”是帮助理解流程图的注释。
Then, add the corresponding Java classes for handling process nodes (the orange rectangles). Click the menu "Window" -> "Show View" -> "Other..." -> "General" -> "Properties" -> "OK" to open the Properties View. Enter the corresponding name of Java class in the class field. This class must implement the interface of org.cheetahworkflow.engine.WorkflowNode. 然后,为流程步骤节点(橙色矩形)添加对应的Java类。点击Eclipse菜单的“Window”->“Show View”->“Other...”->“General”->“Properties”->“OK”打开属性视图。在Class属性中填入对应的类名。这个类必须实现org.cheetahworkflow.engine.WorkflowNode接口。
After that, add the corresponding condition test Java classes for each switch node (cyan hexagon). Choose the outgoing arrow of switch node, enter the corresponding class name in class field. This class must implement the interface of org.cheetahworkflow.engine.SwitchCondition. 接着,为分支节点(青色六边形)的每个分支添加对应的条件测试Java类。选中分支节点向外的箭头,在属性视图中填入对应的条件测试类名。这个类必须实现org.cheetahworkflow.engine.SwitchCondition接口。 After the above jobs, we can get the following XML file, which describes the workflow just designed. 填写完毕后,得到如下的XML文件,这个文件描述了刚刚设计好的工作流流程。 <?xml version="1.0" encoding="UTF-8"?> <workflow xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="workflow.xsd"> <start location="271,30">node4</start> <node type="switch" name="node1" location="201,274" size="180,60"> <caption><![CDATA[x is odd ?]]></caption> <description><![CDATA[]]></description> <switch> <condition class="org.cheetahworkflow.test.TestOddCondition"></condition> <next-node>node2</next-node> </switch> <switch> <default-node>node3</default-node> </switch> </node> <node type="sequence" name="node2" location="17,281" size="150,50"> <caption><![CDATA[x = x * 3 + 1]]></caption> <description><![CDATA[]]></description> <class name="org.cheetahworkflow.test.OddNumberHandler"></class> <next-node>node4</next-node> </node> <node type="sequence" name="node3" location="413,283" size="150,50"> <caption><![CDATA[x = x / 2]]></caption> <description><![CDATA[]]></description> <class name="org.cheetahworkflow.test.EvenNumberHandler"></class> <next-node>node4</next-node> </node> <node type="switch" name="node4" location="202,99" size="180,60"> <caption><![CDATA[x == 1 ?]]></caption> <description><![CDATA[]]></description> <switch> <condition class="org.cheetahworkflow.test.EqualOneCondition"></condition> <next-node>node5</next-node> </switch> <switch> <default-node>node1</default-node> </switch> </node> <end name="node5" location="521,108"></end> <comment location="419,102" size="56,23"><![CDATA[Y]]></comment> <comment location="296,228" size="29,24"><![CDATA[N]]></comment> <comment location="177,282" size="22,20"><![CDATA[Y]]></comment> <comment location="383,284" size="20,18"><![CDATA[N]]></comment> </workflow> Copy file "collatz.workflow" to the classpath. Create 6 Java files, program the concrete business logic, as the following content: 将collatz.workflow复制到classpath下面。创建6个Java文件,编写具体的业务逻辑,内容如下: CollatzSequencePrinter.java package org.cheetahworkflow.example.collatz;
import java.io.InputStream;
import org.cheetahworkflow.engine.WorkflowExecutor;
public class CollatzSequencePrinter {
public static void main(String[] args) {
// load the config file
InputStream config = CollatzSequencePrinter.class.getResourceAsStream("/collatz.workflow");
// construct the workflow executor
WorkflowExecutor<Context> executor = new WorkflowExecutor<Context>(config);
// initial value
int x = 27;
// construct the context variable
Context context = new Context(x);
// print the initial value
System.out.println(x);
// execute the workflow
executor.exec(context);
}
}Context.java package org.cheetahworkflow.example.collatz;
public class Context {
private int x;
public Context(int x) {
this.x = x;
}
public int get() {
return x;
}
public void set(int x) {
this.x = x;
}
}EqualOneCondition.java package org.cheetahworkflow.example.collatz;
import org.cheetahworkflow.engine.SwitchCondition;
public class EqualOneCondition implements SwitchCondition<Context> {
@Override
public boolean test(Context context) {
return context.get() == 1;
}
}TestOddCondition.java package org.cheetahworkflow.example.collatz;
import org.cheetahworkflow.engine.SwitchCondition;
public class TestOddCondition implements SwitchCondition<Context> {
@Override
public boolean test(Context context) {
return (context.get() & 1) == 1;
}
}OddNumberHandler.java package org.cheetahworkflow.example.collatz;
import org.cheetahworkflow.engine.WorkflowNode;
public class OddNumberHandler implements WorkflowNode<Context> {
@Override
public void exec(Context context) {
int x = context.get();
x = x * 3 + 1;
context.set(x);
System.out.println(x);
}
}EvenNumberHandler.java package org.cheetahworkflow.example.collatz;
import org.cheetahworkflow.engine.WorkflowNode;
public class EvenNumberHandler implements WorkflowNode<Context> {
@Override
public void exec(Context context) {
int x = context.get();
x = x / 2;
context.set(x);
System.out.println(x);
}
}Run CollatzSequencePrinter.java, output: 运行CollatzSequencePrinter.java,输出结果: 27 82 41 124 62 31 94 47 142 71 214 107 322 161 484 242 121 364 182 91 274 137 412 206 103 310 155 466 233 700 350 175 526 263 790 395 1186 593 1780 890 445 1336 668 334 167 502 251 754 377 1132 566 283 850 425 1276 638 319 958 479 1438 719 2158 1079 3238 1619 4858 2429 7288 3644 1822 911 2734 1367 4102 2051 6154 3077 9232 4616 2308 1154 577 1732 866 433 1300 650 325 976 488 244 122 61 184 92 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1 |