Search in sources :

Example 41 with InternalFactHandle

use of org.drools.core.common.InternalFactHandle in project drools by kiegroup.

the class BaseLeftTuplesBuilder method insert.

public T insert(Object... objects) {
    this.testStagedInsert = true;
    if (objects == null) {
        objects = new Object[0];
    }
    for (int i = 0; i < objects.length; i++) {
        if (!(objects[i] instanceof Pair)) {
            Object o1 = objects[i];
            InternalFactHandle fh1 = (InternalFactHandle) wm.getFactHandle(o1);
            LeftTuple leftTuple = new LeftTupleImpl(fh1, sink, true);
            leftTuples.addInsert(leftTuple);
        } else {
            Pair p = (Pair) objects[i];
            InternalFactHandle fh1 = (InternalFactHandle) wm.getFactHandle(p.getO1());
            LeftTuple leftTuple1 = new LeftTupleImpl(fh1, sink, true);
            InternalFactHandle fh2 = (InternalFactHandle) wm.getFactHandle(p.getO2());
            LeftTuple leftTuple2 = sink.createLeftTuple(leftTuple1, new RightTupleImpl(fh2), sink);
            leftTuples.addInsert(leftTuple2);
        }
    }
    return (T) this;
}
Also used : LeftTupleImpl(org.drools.core.reteoo.LeftTupleImpl) RightTupleImpl(org.drools.core.reteoo.RightTupleImpl) InternalFactHandle(org.drools.core.common.InternalFactHandle) LeftTuple(org.drools.core.reteoo.LeftTuple)

Example 42 with InternalFactHandle

use of org.drools.core.common.InternalFactHandle in project drools by kiegroup.

the class BaseLeftTuplesBuilder method delete.

public T delete(Object... objects) {
    this.testStagedDelete = true;
    if (objects == null) {
        objects = new Object[0];
    }
    for (int i = 0; i < objects.length; i++) {
        if (!(objects[i] instanceof Pair)) {
            Object o1 = objects[i];
            InternalFactHandle fh1 = (InternalFactHandle) wm.getFactHandle(o1);
            LeftTuple leftTuple = new LeftTupleImpl(fh1, sink, true);
            leftTuples.addDelete(leftTuple);
        } else {
            Pair p = (Pair) objects[i];
            InternalFactHandle fh1 = (InternalFactHandle) wm.getFactHandle(p.getO1());
            LeftTuple leftTuple1 = new LeftTupleImpl(fh1, sink, true);
            InternalFactHandle fh2 = (InternalFactHandle) wm.getFactHandle(p.getO2());
            LeftTuple leftTuple2 = sink.createLeftTuple(leftTuple1, new RightTupleImpl(fh2), sink);
            leftTuples.addDelete(leftTuple2);
        }
    }
    return (T) this;
}
Also used : LeftTupleImpl(org.drools.core.reteoo.LeftTupleImpl) RightTupleImpl(org.drools.core.reteoo.RightTupleImpl) InternalFactHandle(org.drools.core.common.InternalFactHandle) LeftTuple(org.drools.core.reteoo.LeftTuple)

Example 43 with InternalFactHandle

use of org.drools.core.common.InternalFactHandle 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 44 with InternalFactHandle

use of org.drools.core.common.InternalFactHandle 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 45 with InternalFactHandle

use of org.drools.core.common.InternalFactHandle 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)

Aggregations

InternalFactHandle (org.drools.core.common.InternalFactHandle)182 Test (org.junit.Test)89 KieSession (org.kie.api.runtime.KieSession)66 ArrayList (java.util.ArrayList)63 List (java.util.List)41 KieBase (org.kie.api.KieBase)31 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)29 LeftTuple (org.drools.core.reteoo.LeftTuple)27 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)21 StatefulKnowledgeSessionImpl (org.drools.core.impl.StatefulKnowledgeSessionImpl)21 FactHandle (org.kie.api.runtime.rule.FactHandle)21 RightTuple (org.drools.core.reteoo.RightTuple)20 Declaration (org.drools.core.rule.Declaration)20 LeftTupleImpl (org.drools.core.reteoo.LeftTupleImpl)16 StockTickInterface (org.drools.compiler.StockTickInterface)15 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)15 RightTupleImpl (org.drools.core.reteoo.RightTupleImpl)15 StockTick (org.drools.compiler.StockTick)14 Cheese (org.drools.core.test.model.Cheese)14 KieBaseConfiguration (org.kie.api.KieBaseConfiguration)14