Search in sources :

Example 6 with RuleBase

use of org.drools.core.impl.RuleBase in project drools by kiegroup.

the class DynamicEvalTest method testDynamicAdd2.

@Test
public void testDynamicAdd2() {
    String test = "rule id3\n" + "when\n" + "eval(0 == 0)\n" + // this eval works
    "String( this == \"go\" )\n" + "then\n" + "insertLogical( \"done\" );\n" + "end\n" + "rule id5\n" + "when\n" + "eval(0 == 0)\n" + // this eval works
    "Integer( this == 7 )\n" + "then\n" + "insertLogical( \"done3\" );\n" + "end\n";
    loadPackages(ResourceFactory.newByteArrayResource(test.getBytes()), ResourceType.DRL);
    ((RuleBase) session.getKieBase()).addPackages(kbuilder.getKnowledgePackages());
    session.addEventListener(new DebugRuleRuntimeEventListener());
    session.insert("go");
    session.insert(5);
    session.insert(7);
    // 1
    int fired = session.fireAllRules();
    System.out.println(fired);
    effects = session.getObjects();
    assertTrue("fired", effects.contains("done"));
    // so the above works, let's try it again
    String test2 = "\nrule id4" + "\nwhen" + // this eval doesn't
    "\neval(0 == 0 )" + "\nInteger( this == 5 )" + "\nthen" + "\ninsertLogical( \"done2\" );" + "\nend";
    loadPackages(ResourceFactory.newByteArrayResource(test2.getBytes()), ResourceType.DRL);
    ((RuleBase) session.getKieBase()).addPackages(kbuilder.getKnowledgePackages());
    // 0
    fired = session.fireAllRules();
    System.out.println(fired);
    effects = session.getObjects();
    // fails
    assertTrue("fired", effects.contains("done2"));
    for (Object o : session.getObjects()) {
        System.out.println(o);
    }
}
Also used : DebugRuleRuntimeEventListener(org.kie.api.event.rule.DebugRuleRuntimeEventListener) RuleBase(org.drools.core.impl.RuleBase) Test(org.junit.Test)

Example 7 with RuleBase

use of org.drools.core.impl.RuleBase in project drools by kiegroup.

the class DynamicEvalTest method testDynamicAdd.

@Test
public void testDynamicAdd() {
    String test = "\nrule id3" + "\nwhen" + // this eval works
    "\neval(0 < 1)" + "\nthen" + "\ninsertLogical( \"done\" );" + "\nend";
    loadPackages(ResourceFactory.newByteArrayResource(test.getBytes()), ResourceType.DRL);
    ((RuleBase) session.getKieBase()).addPackages(kbuilder.getKnowledgePackages());
    session.addEventListener(new DebugRuleRuntimeEventListener());
    // 1
    int fired = session.fireAllRules();
    System.out.println(fired);
    effects = session.getObjects();
    assertTrue("fired", effects.contains("done"));
    // so the above works, let's try it again
    String test2 = "\nrule id4" + "\nwhen" + // this eval doesn't
    "\neval(0 == 0 )" + "\nthen" + "\ninsertLogical( \"done2\" );" + "\nend";
    loadPackages(ResourceFactory.newByteArrayResource(test2.getBytes()), ResourceType.DRL);
    ((RuleBase) session.getKieBase()).addPackages(kbuilder.getKnowledgePackages());
    // 0
    fired = session.fireAllRules();
    System.out.println(fired);
    effects = session.getObjects();
    // fails
    assertTrue("fired", effects.contains("done2"));
}
Also used : DebugRuleRuntimeEventListener(org.kie.api.event.rule.DebugRuleRuntimeEventListener) RuleBase(org.drools.core.impl.RuleBase) Test(org.junit.Test)

Example 8 with RuleBase

use of org.drools.core.impl.RuleBase in project drools by kiegroup.

the class KnowledgeBuilderImpl method newKnowledgeBase.

public KieBase newKnowledgeBase(KieBaseConfiguration conf) {
    KnowledgeBuilderErrors errors = getErrors();
    if (!errors.isEmpty()) {
        for (KnowledgeBuilderError error : errors) {
            logger.error(error.toString());
        }
        throw new IllegalArgumentException("Could not parse knowledge. See the logs for details.");
    }
    RuleBase kbase = RuleBaseFactory.newRuleBase(conf);
    kbase.addPackages(Arrays.asList(getPackages()));
    return KnowledgeBaseFactory.newKnowledgeBase(kbase);
}
Also used : KnowledgeBuilderError(org.kie.internal.builder.KnowledgeBuilderError) KnowledgeBuilderErrors(org.kie.internal.builder.KnowledgeBuilderErrors) RuleBase(org.drools.core.impl.RuleBase)

Example 9 with RuleBase

use of org.drools.core.impl.RuleBase in project drools by kiegroup.

the class AbstractKieModule method createKieBase.

public InternalKnowledgeBase createKieBase(KieBaseModelImpl kBaseModel, KieProject kieProject, BuildContext buildContext, KieBaseConfiguration conf) {
    KnowledgePackagesBuildResult knowledgePackagesBuildResult = buildKnowledgePackages(kBaseModel, kieProject, buildContext);
    if (knowledgePackagesBuildResult.hasErrors()) {
        return null;
    }
    Collection<KiePackage> pkgs = knowledgePackagesBuildResult.getPkgs();
    checkStreamMode(kBaseModel, conf, pkgs);
    ClassLoader cl = kieProject.getClassLoader();
    if (conf == null) {
        conf = getKnowledgeBaseConfiguration(kBaseModel, cl);
    } else if (conf instanceof RuleBaseConfiguration) {
        ((RuleBaseConfiguration) conf).setClassLoader(cl);
    }
    RuleBase kBase = RuleBaseFactory.newRuleBase(kBaseModel.getName(), conf);
    kBase.addPackages(pkgs);
    return KnowledgeBaseFactory.newKnowledgeBase(kBase);
}
Also used : RuleBaseConfiguration(org.drools.core.RuleBaseConfiguration) KiePackage(org.kie.api.definition.KiePackage) RuleBase(org.drools.core.impl.RuleBase)

Example 10 with RuleBase

use of org.drools.core.impl.RuleBase in project drools by kiegroup.

the class ConcurrentNodeMemories method resetAllMemories.

public void resetAllMemories(StatefulKnowledgeSession session) {
    RuleBase kBase = (RuleBase) session.getKieBase();
    Set<SegmentMemory> smemSet = new HashSet<>();
    for (int i = 0; i < memories.length(); i++) {
        Memory memory = memories.get(i);
        if (memory != null) {
            memory.reset();
            smemSet.add(memory.getSegmentMemory());
        }
    }
    smemSet.forEach(smem -> resetSegmentMemory(session, kBase, smem));
}
Also used : SegmentMemory(org.drools.core.reteoo.SegmentMemory) SegmentMemory(org.drools.core.reteoo.SegmentMemory) RuleBase(org.drools.core.impl.RuleBase) HashSet(java.util.HashSet)

Aggregations

RuleBase (org.drools.core.impl.RuleBase)21 Test (org.junit.Test)16 KieBase (org.kie.api.KieBase)9 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)6 KieSession (org.kie.api.runtime.KieSession)6 Rete (org.drools.core.reteoo.Rete)5 LeftInputAdapterNode (org.drools.core.reteoo.LeftInputAdapterNode)4 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)3 BuildContext (org.drools.core.reteoo.builder.BuildContext)3 FactHandle (org.kie.api.runtime.rule.FactHandle)3 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 ClassObjectType (org.drools.core.base.ClassObjectType)2 AlphaNode (org.drools.core.reteoo.AlphaNode)2 LeftTupleSink (org.drools.core.reteoo.LeftTupleSink)2 RuleTerminalNode (org.drools.core.reteoo.RuleTerminalNode)2 IndexableConstraint (org.drools.core.rule.IndexableConstraint)2 KieBaseConfiguration (org.kie.api.KieBaseConfiguration)2 ReleaseId (org.kie.api.builder.ReleaseId)2 KiePackage (org.kie.api.definition.KiePackage)2