我对规则这方面的编码都不会,刚开始做,一点都摸不着门,请各位前辈帮帮忙。谢谢了。IDE是eclipse3.0
我的代码特别简单,就是想试试先使规则引擎转起来,下面是我的一个类
package test.mis.action;
public class PeopleValid {
private int peopleId;
private String msg;
public int getId () {
return peopleId;
}
public void setId (int Id) {
this.peopleId = Id;
}
public String getMsg () {
return msg;
}
public void setMsg (String m) {
this.msg = m;
System.out.println(m);
}
}
下面是我的drl文件:
package TestRules
#list any import classes here.
import Test.mis.action.*
#declare any global variables here
rule "First Rule"
when
#conditions
$p:PeopleValid()
eval($p.getId==5)
then
#actions
System.out.println("12345")
end
下面是我的测试代码:
package test.rules.engine.test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Reader;
import java.util.List;
import org.drools.RuleBase;
import org.drools.RuleBaseFactory;
import org.drools.WorkingMemory;
import org.drools.compiler.PackageBuilder;
import org.drools.rule.Package;
import test.mis.action.*;
public class RuleEngineTest
{
private String ruleName;
private RuleBase ruleBase;
private WorkingMemory memory;
private static String msg = "Initiated!";
// 用来测试规则路径的正确性和执行情况。
public static void main(String[] args) throws Exception
{
PeopleValid people = new PeopleValid();
System.err.println("已创建对象!");
people.setId(5);
System.err.println(people.getId());
System.err.println("已赋值!");
RuleEngineTest testEngine = new RuleEngineTest();
testEngine.setRuleName("/test\\rules\\drl\\TestRules.drl");
testEngine.init(people);
System.err.println("------------------------------");
testEngine.getMemory().fireAllRules();
System.out.println(people.getMsg());
System.out.println(msg);
System.err.println("规则成功执行!!!");
}
public void init(PeopleValid p) throws Exception
{
long start1, finish1;
long start2, finish2;// 测试响应时间用的变量
start1 = System.currentTimeMillis();
readRule();
finish1 = System.currentTimeMillis();
System.err.println("readRule需要的时间: " + (finish1 - start1));
start1 = System.currentTimeMillis();
ObjectInputStream oIn = new ObjectInputStream(new FileInputStream("src\\test\\rules\\pkg\\TestRules.pkg"));
Package pkg = (Package)oIn.readObject();
finish1 = System.currentTimeMillis();
System.err.println("解析Package文件流需要的时间: " + (finish1 - start1));
// add the package to a rulebase (deploy the rule package).
start1 = System.currentTimeMillis();
// 生成一个RuleBase实例并bore一个workingMemory
ruleBase = RuleBaseFactory.newRuleBase();
memory = ruleBase.newWorkingMemory();
ruleBase.addPackage(pkg);
// 并且初始化一个WorkingMemory
finish1 = System.currentTimeMillis();
System.err.println("从pkg加载规则文件需要的时间: " + (finish1 - start1));
memory.assertObject(p);
System.err.println("调用规则!");
}
public void tearDown()
{
// 销毁工作内存
memory.clearAgenda();
memory.dispose();
}
public void readRule() throws Exception
{
// read in the source
Reader input = new InputStreamReader(RuleEngineTest.class
.getResourceAsStream(ruleName));
PackageBuilder builder = new PackageBuilder();
long start = System.currentTimeMillis();