我想从简单的java文件中调用guvnor规则。但我正面临着一些问题。 我正在使用jboss7进行部署。任何人都告诉我,如果我错过任何事情。
我的drl规则
package com.rule;
import com.rule.Rule1Bean;
rule "r1"
when
$s : Rule1Bean(true)
then
$s.setB("b");
System.out.println("finished" );
end
我的bean文件
package com.rule;
public class Rule1Bean {
public Rule1Bean()
{
}
public Rule1Bean(String a) {
this.a = a;
}
private String a;
private String b;
public String getA() {
return a;
}
public void setA(String a) {
this.a = a;
}
public String getB() {
return b;
}
public void setB(String b) {
this.b = b;
System.out.println("b="+b);
}
}
changeset.xml
?xml version="1.0" encoding="UTF-8"?>
<change-set xmlns='http://drools.org/drools-5.0/change-set'
xmlns:xs='http://www.w3.org/2001/XMLSchema-instance'
xs:schemaLocation='http://drools.org/drools-5.0/change-set
http://anonsvn.jboss.org/repos/labs/labs/jbossrules/trunk/drools-api/src/main/resources/change-set-1.0.0.xsd' >
<add>
<resource
source='http://localhost:8080/drools-guvnor/org.drools.guvnor.Guvnor/package/com.rule/TestRule.drl'
type='DRL' basicAuthentication="enabled" username="admin" password="admin" />
</add>
</change-set>
TestGuvnor.java文件
package com.rule;
import junit.framework.TestCase;
import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseFactory;
import org.drools.agent.KnowledgeAgent;
import org.drools.agent.KnowledgeAgentConfiguration;
import org.drools.agent.KnowledgeAgentFactory;
import org.drools.core.util.FileManager;
import org.drools.io.ResourceChangeScannerConfiguration;
import org.drools.io.ResourceFactory;
import org.drools.io.impl.ResourceChangeScannerImpl;
import org.drools.runtime.StatefulKnowledgeSession;
import org.drools.RuleBase;
import org.drools.WorkingMemory;
import org.drools.agent.RuleAgent;
import java.util.Iterator;
public class TestGuvnor extends TestCase{
public static void main(String[] args) throws Exception {
(new TestGuvnor()).testDroolsWithGuvnor();
/*RuleAgent agent = RuleAgent.newRuleAgent("/Guvnor.properties");
RuleBase ruleBase = agent.getRuleBase();
WorkingMemory workingMemory = ruleBase.newStatefulSession();
Rule1Bean d = new Rule1Bean("Jarek");
workingMemory.insert(d);
workingMemory.fireAllRules();
for (Iterator i = workingMemory.iterateObjects(); i.hasNext();) {
System.out.println(i.next().getClass().getCanonicalName());
}*/
}
public void testDroolsWithGuvnor() throws Exception {
KnowledgeBase knowledgeBase = createKnowledgeBase();
StatefulKnowledgeSession session = knowledgeBase.newStatefulKnowledgeSession();
try {
Rule1Bean rule = new Rule1Bean();
rule.setA("aaa");
System.out.println("creating rule");
session.insert(rule);
session.fireAllRules();
System.out.println("b="+rule.getB());
}
finally {
session.dispose();
}
}
private static KnowledgeBase createKnowledgeBase() {
KnowledgeAgentConfiguration kaconf = KnowledgeAgentFactory.newKnowledgeAgentConfiguration();
kaconf.setProperty( "drools.agent.scanDirectories", "false" );
KnowledgeAgent kagent = KnowledgeAgentFactory.newKnowledgeAgent( "test agent", kaconf );
kagent.applyChangeSet( ResourceFactory.newClassPathResource("changeset.xml"));
return kagent.getKnowledgeBase();
}
}