0

単純な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

私の豆ファイル

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();
    }



}
4

0 に答える 0