Search in sources :

Example 16 with StatefulKnowledgeSessionImpl

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

the class ClearRuleFlowGroupCommand method execute.

public Void execute(Context context) {
    KieSession ksession = ((RegistryContext) context).lookup(KieSession.class);
    ((StatefulKnowledgeSessionImpl) ksession).getAgenda().getRuleFlowGroup(this.name).clear();
    return null;
}
Also used : StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) KieSession(org.kie.api.runtime.KieSession) RegistryContext(org.drools.core.command.impl.RegistryContext)

Example 17 with StatefulKnowledgeSessionImpl

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

the class ParallelEvaluationTest method getMultipleParallelKieSessionsFireUntilHaltCallable.

private Callable<Void> getMultipleParallelKieSessionsFireUntilHaltCallable(KieBase kBase, boolean asyncInsert) {
    return () -> {
        KieSession ksession = kBase.newKieSession();
        assertThat(((InternalWorkingMemory) ksession).getAgenda().isParallelAgenda()).isTrue();
        CountDownLatch done = new CountDownLatch(1);
        DebugList<Integer> list = new DebugList<Integer>();
        list.onItemAdded = (l -> {
            if (l.size() == 10) {
                ksession.halt();
                done.countDown();
            }
        });
        ksession.setGlobal("list", list);
        new Thread(ksession::fireUntilHalt).start();
        if (asyncInsert) {
            StatefulKnowledgeSessionImpl session = (StatefulKnowledgeSessionImpl) ksession;
            for (int i = 0; i < 10; i++) {
                session.insertAsync(i);
                session.insertAsync("" + String.valueOf(i));
            }
        } else {
            insertFacts(ksession, 10);
        }
        try {
            done.await();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        assertThat(list.size()).isEqualTo(10);
        return null;
    };
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) KieSession(org.kie.api.runtime.KieSession) CountDownLatch(java.util.concurrent.CountDownLatch) DebugList(org.drools.compiler.util.debug.DebugList)

Example 18 with StatefulKnowledgeSessionImpl

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

the class PropertyReactivityTest method testPropReactiveUpdate.

@Test
public void testPropReactiveUpdate() {
    // DROOLS-1275
    final String str1 = "import " + Klass.class.getCanonicalName() + "\n" + "global java.util.List list;\n" + "rule R when\n" + "  Klass( b == 2 )\n" + "then\n" + "  list.add(\"fired\");\n" + "end\n";
    final KieSession ksession = new KieHelper().addContent(str1, ResourceType.DRL).build().newKieSession();
    final List<String> list = new ArrayList<String>();
    ksession.setGlobal("list", list);
    final Klass bean = new Klass(1, 2, 3, 4, 5, 6);
    final FactHandle fh = ksession.insert(bean);
    ksession.fireAllRules();
    assertEquals(1, list.size());
    ((StatefulKnowledgeSessionImpl) ksession).update(fh, bean, "a", "d");
    ksession.fireAllRules();
    assertEquals(1, list.size());
    ((StatefulKnowledgeSessionImpl) ksession).update(fh, bean, "c", "b");
    ksession.fireAllRules();
    assertEquals(2, list.size());
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) ArrayList(java.util.ArrayList) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 19 with StatefulKnowledgeSessionImpl

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

the class IndexingTest method testFullFastIteratorResume.

@Test(timeout = 10000)
public void testFullFastIteratorResume() throws Exception {
    String str = "";
    str += "package org.drools.compiler.test  \n";
    str += "import org.drools.compiler.Person \n";
    str += "query peeps( String $name, int $age ) \n";
    str += "    not $p2 : Person( $name := name, age > $age ) \n";
    str += "end\n";
    KieBase kbase = loadKnowledgeBaseFromString(str);
    List<ObjectTypeNode> nodes = ((KnowledgeBaseImpl) kbase).getRete().getObjectTypeNodes();
    ObjectTypeNode node = null;
    for (ObjectTypeNode n : nodes) {
        if (((ClassObjectType) n.getObjectType()).getClassType() == DroolsQuery.class) {
            node = n;
            break;
        }
    }
    StatefulKnowledgeSessionImpl wm = ((StatefulKnowledgeSessionImpl) kbase.newKieSession());
    AlphaNode alphanode = (AlphaNode) node.getObjectSinkPropagator().getSinks()[0];
    LeftInputAdapterNode liaNode = (LeftInputAdapterNode) alphanode.getObjectSinkPropagator().getSinks()[0];
    NotNode n = (NotNode) liaNode.getSinkPropagator().getSinks()[0];
    DoubleNonIndexSkipBetaConstraints c = (DoubleNonIndexSkipBetaConstraints) n.getRawConstraints();
    // assertEquals( "$name", ((VariableConstraint)c.getConstraint()).getRequiredDeclarations()[0].getIdentifier() );
    assertTrue(c.isIndexed());
    BetaMemory bm = (BetaMemory) wm.getNodeMemory(n);
    System.out.println(bm.getLeftTupleMemory().getClass());
    System.out.println(bm.getRightTupleMemory().getClass());
    assertTrue(bm.getLeftTupleMemory() instanceof TupleIndexHashTable);
    assertTrue(bm.getRightTupleMemory() instanceof TupleIndexHashTable);
    final Map<String, Integer> map = new HashMap<String, Integer>();
    map.put("inserted", new Integer(0));
    map.put("deleted", new Integer(0));
    map.put("updated", new Integer(0));
    wm.openLiveQuery("peeps", new Object[] { Variable.v, 99 }, new ViewChangedEventListener() {

        @Override
        public void rowInserted(Row row) {
        }

        @Override
        public void rowDeleted(Row row) {
        }

        @Override
        public void rowUpdated(Row row) {
        }
    });
    Map<String, InternalFactHandle> peeps = new HashMap<String, InternalFactHandle>();
    Person p = new Person("x0", 100);
    InternalFactHandle fh = (InternalFactHandle) wm.insert(p);
    peeps.put(p.getName(), fh);
    for (int i = 1; i < 100; i++) {
        p = new Person("x" + i, 101);
        fh = (InternalFactHandle) wm.insert(p);
        wm.fireAllRules();
        peeps.put(p.getName(), fh);
    }
    List<RightTuple> list = new ArrayList<RightTuple>(100);
    FastIterator it = n.getRightIterator(bm.getRightTupleMemory());
    for (RightTuple rt = n.getFirstRightTuple(null, bm.getRightTupleMemory(), null, it); rt != null; rt = (RightTuple) it.next(rt)) {
        list.add(rt);
    }
    assertEquals(100, list.size());
    // check we can resume from each entry in the list above.
    for (int i = 0; i < 100; i++) {
        RightTuple rightTuple = list.get(i);
        // resumes from the current rightTuple
        it = n.getRightIterator(bm.getRightTupleMemory(), rightTuple);
        int j = i + 1;
        for (RightTuple rt = (RightTuple) it.next(rightTuple); rt != null; rt = (RightTuple) it.next(rt)) {
            assertSame(list.get(j), rt);
            j++;
        }
    }
}
Also used : NotNode(org.drools.core.reteoo.NotNode) HashMap(java.util.HashMap) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) ArrayList(java.util.ArrayList) DoubleNonIndexSkipBetaConstraints(org.drools.core.common.DoubleNonIndexSkipBetaConstraints) KieBase(org.kie.api.KieBase) FastIterator(org.drools.core.util.FastIterator) InternalFactHandle(org.drools.core.common.InternalFactHandle) BetaMemory(org.drools.core.reteoo.BetaMemory) TupleIndexHashTable(org.drools.core.util.index.TupleIndexHashTable) AlphaNode(org.drools.core.reteoo.AlphaNode) RightTuple(org.drools.core.reteoo.RightTuple) IndexableConstraint(org.drools.core.rule.IndexableConstraint) ViewChangedEventListener(org.kie.api.runtime.rule.ViewChangedEventListener) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) Row(org.kie.api.runtime.rule.Row) Person(org.drools.compiler.Person) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) Test(org.junit.Test)

Example 20 with StatefulKnowledgeSessionImpl

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

the class LinkingTest method testForallNodes.

@Test
public void testForallNodes() throws Exception {
    String str = "";
    str += "package org.kie \n";
    str += "import " + A.class.getCanonicalName() + "\n";
    str += "import " + B.class.getCanonicalName() + "\n";
    str += "import " + C.class.getCanonicalName() + "\n";
    str += "import " + D.class.getCanonicalName() + "\n";
    str += "import " + E.class.getCanonicalName() + "\n";
    str += "import " + F.class.getCanonicalName() + "\n";
    str += "import " + G.class.getCanonicalName() + "\n";
    str += "global java.util.List list \n";
    str += "rule rule1 when \n";
    str += "   $a : A() \n";
    str += "   forall( B() )\n";
    str += "   $c : C() \n";
    str += "then \n";
    str += "  list.add( 'x' ); \n";
    str += "end \n";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
    assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors());
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    ObjectTypeNode aotn = getObjectTypeNode(kbase, A.class);
    ObjectTypeNode botn = getObjectTypeNode(kbase, A.class);
    ObjectTypeNode cotn = getObjectTypeNode(kbase, A.class);
    KieSession wm = kbase.newKieSession();
    List list = new ArrayList();
    // wm.setGlobal( "list", list );
    // 
    // for ( int i = 0; i < 3; i++ ) {
    // wm.insert(  new A(i) );
    // }
    // 
    // wm.insert(  new B(2) );
    // 
    // for ( int i = 0; i < 3; i++ ) {
    // wm.insert(  new C(i) );
    // }
    // 
    // wm.fireAllRules();
    // assertEquals( 0, list.size() );
    wm = ((StatefulKnowledgeSessionImpl) kbase.newKieSession());
    list = new ArrayList();
    wm.setGlobal("list", list);
    for (int i = 0; i < 2; i++) {
        wm.insert(new A(i));
    }
    for (int i = 0; i < 27; i++) {
        wm.insert(new B(1));
    }
    for (int i = 0; i < 2; i++) {
        wm.insert(new C(i));
    }
    wm.fireAllRules();
    assertEquals(4, list.size());
// wm.retract( fh );
// wm.fireAllRules();
// assertEquals( 9, list.size() );
}
Also used : ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) ArrayList(java.util.ArrayList) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Aggregations

StatefulKnowledgeSessionImpl (org.drools.core.impl.StatefulKnowledgeSessionImpl)79 Test (org.junit.Test)63 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)41 ArrayList (java.util.ArrayList)22 InternalFactHandle (org.drools.core.common.InternalFactHandle)22 KieSession (org.kie.api.runtime.KieSession)22 List (java.util.List)20 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)17 KieBaseConfiguration (org.kie.api.KieBaseConfiguration)17 ClassObjectType (org.drools.core.base.ClassObjectType)14 FactHandle (org.kie.api.runtime.rule.FactHandle)14 KieBase (org.kie.api.KieBase)13 HashMap (java.util.HashMap)9 LeftTupleImpl (org.drools.core.reteoo.LeftTupleImpl)8 BuildContext (org.drools.core.reteoo.builder.BuildContext)8 Cheese (org.drools.core.test.model.Cheese)8 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)7 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)6 TruthMaintenanceSystem (org.drools.core.common.TruthMaintenanceSystem)6 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)6