Search in sources :

Example 41 with InternalKnowledgeBase

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

the class PhreakLiaNodeTest method test.

@Test
public void test() {
    String str = "package org.drools.compiler.test\n" + "\n" + "import " + A.class.getCanonicalName() + "\n" + "import " + B.class.getCanonicalName() + "\n" + "\n" + "rule r1 \n" + "    when \n" + "        $a : A( object == 1 )\n" + "    then \n" + "        System.out.println( $a ); \n" + "end \n" + "rule r2 \n" + "    when \n" + "        $a : A( object == 2 )\n" + "    then \n" + "        System.out.println( $a ); \n" + "end \n " + "rule r3 \n" + "    when \n" + "        $a : A( object == 2 )\n" + "        $b : B( )\n" + "    then \n" + "        System.out.println( $a ); \n" + "end \n " + "rule r4 \n" + "    when \n" + "        $a : A( object == 3 )\n" + "    then \n" + "        System.out.println( $a ); \n" + "end \n";
    KnowledgeBuilder builder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    builder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
    if (builder.hasErrors()) {
        throw new RuntimeException(builder.getErrors().toString());
    }
    InternalKnowledgeBase knowledgeBase = KnowledgeBaseFactory.newKnowledgeBase();
    knowledgeBase.addPackages(builder.getKnowledgePackages());
    KieSession ksession = knowledgeBase.newKieSession();
    InternalFactHandle fhB = (InternalFactHandle) ksession.insert(B.b(1));
    InternalFactHandle fhA = (InternalFactHandle) ksession.insert(A.a(1));
    ksession.fireAllRules();
    System.out.println("---1---");
    // ksession.update( fhA, a(1) );
    // ksession.fireAllRules();
    // System.out.println( "---2---" );
    // 
    ksession.update(fhA, A.a(2));
    ksession.fireAllRules();
    System.out.println("---3---");
    ksession.update(fhA, A.a(2));
    ksession.fireAllRules();
    System.out.println("---4---");
    ksession.update(fhA, A.a(3));
    ksession.fireAllRules();
    System.out.println("---5---");
    ksession.update(fhB, B.b(1));
    ksession.update(fhA, A.a(3));
    ksession.fireAllRules();
    // ksession.update( fhA, a(1) );
    // ksession.fireAllRules();
    // 
    // ksession.update( fhA, a(1) );
    // ksession.fireAllRules();
    ksession.dispose();
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KieSession(org.kie.api.runtime.KieSession) InternalFactHandle(org.drools.core.common.InternalFactHandle) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 42 with InternalKnowledgeBase

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

the class RemoveRuleTest method testPopulatedMultipleSharesRemoveFirst.

@Test
public void testPopulatedMultipleSharesRemoveFirst() throws Exception {
    InternalKnowledgeBase kbase1 = buildKnowledgeBase("r1", "   A(1;)  A(2;) B(1;) B(2;) C(1;) D() E()\n");
    InternalWorkingMemory wm = ((InternalWorkingMemory) kbase1.newKieSession());
    List list = new ArrayList();
    wm.setGlobal("list", list);
    InternalFactHandle fh1 = (InternalFactHandle) wm.insert(new A(1));
    InternalFactHandle fh2 = (InternalFactHandle) wm.insert(new A(2));
    InternalFactHandle fh3 = (InternalFactHandle) wm.insert(new A(2));
    InternalFactHandle fh4 = (InternalFactHandle) wm.insert(new A(3));
    InternalFactHandle fh5 = (InternalFactHandle) wm.insert(new B(1));
    InternalFactHandle fh6 = (InternalFactHandle) wm.insert(new B(2));
    InternalFactHandle fh7 = (InternalFactHandle) wm.insert(new C(1));
    InternalFactHandle fh8 = (InternalFactHandle) wm.insert(new C(2));
    InternalFactHandle fh9 = (InternalFactHandle) wm.insert(new D(1));
    InternalFactHandle fh10 = (InternalFactHandle) wm.insert(new E(1));
    wm.fireAllRules();
    assertEquals(2, list.size());
    kbase1.addPackages(buildKnowledgePackage("r2", "   A(1;)  A(2;) B(1;) B(2;) C(2;) D() E()\n"));
    kbase1.addPackages(buildKnowledgePackage("r3", "   A(1;)  A(3;) B(1;) B(2;) C(2;) D() E()\n"));
    wm.fireAllRules();
    assertEquals(5, list.size());
    kbase1.removeRule("org.kie", "r1");
    list.clear();
    wm.update(fh1, fh1.getObject());
    wm.update(fh2, fh2.getObject());
    wm.update(fh3, fh3.getObject());
    wm.update(fh4, fh4.getObject());
    wm.update(fh5, fh5.getObject());
    wm.update(fh6, fh6.getObject());
    wm.update(fh7, fh7.getObject());
    wm.update(fh8, fh8.getObject());
    wm.update(fh9, fh9.getObject());
    wm.update(fh10, fh10.getObject());
    wm.fireAllRules();
    assertEquals(3, list.size());
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) InternalFactHandle(org.drools.core.common.InternalFactHandle) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 43 with InternalKnowledgeBase

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

the class RemoveRuleTest method testPathMemorySizeAfterSegmentMergeNonInitialized.

@Test
public void testPathMemorySizeAfterSegmentMergeNonInitialized() throws Exception {
    InternalKnowledgeBase kbase1 = buildKnowledgeBase("r1", "   A(1;) B(1;)\n");
    kbase1.addPackages(buildKnowledgePackage("r2", "   A(1;)\n"));
    InternalWorkingMemory wm = ((InternalWorkingMemory) kbase1.newKieSession());
    RuleTerminalNode rtn1 = getRtn("org.kie.r1", kbase1);
    RuleTerminalNode rtn2 = getRtn("org.kie.r2", kbase1);
    assertEquals(2, wm.getNodeMemory(rtn1).getSegmentMemories().length);
    assertEquals(2, wm.getNodeMemory(rtn2).getSegmentMemories().length);
    kbase1.removeRule("org.kie", "r2");
    assertEquals(1, wm.getNodeMemory(rtn1).getSegmentMemories().length);
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) Test(org.junit.Test)

Example 44 with InternalKnowledgeBase

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

the class RemoveRuleTest method testPopulatedSharedLiaNodeNoBeta.

@Test
public void testPopulatedSharedLiaNodeNoBeta() throws Exception {
    InternalKnowledgeBase kbase1 = buildKnowledgeBase("r1", "   A()\n");
    InternalWorkingMemory wm = ((InternalWorkingMemory) kbase1.newKieSession());
    List list = new ArrayList();
    wm.setGlobal("list", list);
    A a1 = new A(1);
    InternalFactHandle fh1 = (InternalFactHandle) wm.insert(a1);
    A a2 = new A(2);
    InternalFactHandle fh2 = (InternalFactHandle) wm.insert(a2);
    wm.fireAllRules();
    assertEquals(2, list.size());
    kbase1.addPackages(buildKnowledgePackage("r2", "   a : A()\n"));
    wm.fireAllRules();
    assertEquals(4, list.size());
    kbase1.removeRule("org.kie", "r1");
    kbase1.removeRule("org.kie", "r2");
    list.clear();
    assertNull(fh1.getFirstLeftTuple());
    assertNull(fh2.getFirstLeftTuple());
    wm.update(fh1, a1);
    wm.update(fh2, a2);
    wm.fireAllRules();
    assertEquals(0, list.size());
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) InternalFactHandle(org.drools.core.common.InternalFactHandle) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 45 with InternalKnowledgeBase

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

the class RemoveRuleTest method testSplitTwoBeforeCreatedSegment.

@Test
public void testSplitTwoBeforeCreatedSegment() throws Exception {
    InternalKnowledgeBase kbase1 = buildKnowledgeBase("r1", "   A(1;)  A(2;) B(1;) B(2;) C(1;) C(2;) D(1;) D(2;) E(1;) E(2;)\n");
    kbase1.addPackages(buildKnowledgePackage("r2", "   A(1;)  A(2;) B(1;) B(2;) C(1;) C(2;) D(1;) D(2;) E(1;) E(2;)\n"));
    kbase1.addPackages(buildKnowledgePackage("r3", "   A(1;)  A(2;) B(1;) B(2;) C(1;) C(2;) D(1;) D(2;)\n"));
    kbase1.addPackages(buildKnowledgePackage("r4", "   A(1;)  A(2;) B(1;) B(2;) C(1;) C(2;) \n"));
    InternalWorkingMemory wm = ((InternalWorkingMemory) kbase1.newKieSession());
    List list = new ArrayList();
    wm.setGlobal("list", list);
    wm.insert(new E(1));
    wm.insert(new E(2));
    wm.flushPropagations();
    RuleTerminalNode rtn1 = getRtn("org.kie.r1", kbase1);
    RuleTerminalNode rtn2 = getRtn("org.kie.r2", kbase1);
    RuleTerminalNode rtn3 = getRtn("org.kie.r3", kbase1);
    RuleTerminalNode rtn4 = getRtn("org.kie.r4", kbase1);
    PathMemory pm1 = (PathMemory) wm.getNodeMemory(rtn1);
    SegmentMemory[] smems = pm1.getSegmentMemories();
    assertEquals(4, smems.length);
    assertNull(smems[0]);
    assertNull(smems[1]);
    assertNull(smems[3]);
    SegmentMemory sm = smems[2];
    assertEquals(2, sm.getPos());
    assertEquals(4, sm.getSegmentPosMaskBit());
    assertEquals(4, pm1.getLinkedSegmentMask());
    kbase1.addPackages(buildKnowledgePackage("r5", "   A(1;)  A(2;) B(1;) B(2;) \n"));
    smems = pm1.getSegmentMemories();
    assertEquals(5, smems.length);
    assertNull(smems[0]);
    assertNull(smems[1]);
    assertNull(smems[2]);
    sm = smems[3];
    assertEquals(3, sm.getPos());
    assertEquals(8, sm.getSegmentPosMaskBit());
    assertEquals(8, pm1.getLinkedSegmentMask());
    RuleTerminalNode rtn5 = getRtn("org.kie.r5", kbase1);
    PathMemory pm5 = (PathMemory) wm.getNodeMemory(rtn5);
    smems = pm5.getSegmentMemories();
    assertEquals(2, smems.length);
    assertNull(smems[0]);
    assertNull(smems[1]);
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) SegmentMemory(org.drools.core.reteoo.SegmentMemory) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) PathMemory(org.drools.core.reteoo.PathMemory) Test(org.junit.Test)

Aggregations

InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)368 Test (org.junit.Test)307 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)203 KieSession (org.kie.api.runtime.KieSession)193 ArrayList (java.util.ArrayList)163 List (java.util.List)99 KiePackage (org.kie.api.definition.KiePackage)79 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)45 FactHandle (org.kie.api.runtime.rule.FactHandle)39 ByteArrayResource (org.drools.core.io.impl.ByteArrayResource)38 InternalFactHandle (org.drools.core.common.InternalFactHandle)37 StatefulKnowledgeSessionImpl (org.drools.core.impl.StatefulKnowledgeSessionImpl)37 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)37 AbstractBaseTest (org.jbpm.test.util.AbstractBaseTest)37 Cheese (org.drools.compiler.Cheese)36 Person (org.drools.compiler.Person)36 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)34 FactType (org.kie.api.definition.type.FactType)26 LeftInputAdapterNode (org.drools.core.reteoo.LeftInputAdapterNode)24 HashMap (java.util.HashMap)23