Search in sources :

Example 46 with KieSession

use of org.kie.api.runtime.KieSession in project drools by kiegroup.

the class TruthMaintenanceTest method testLogicalInsertions.

@Test(timeout = 10000)
public void testLogicalInsertions() throws Exception {
    final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newClassPathResource("test_LogicalInsertions.drl", getClass()), ResourceType.DRL);
    Collection<KiePackage> kpkgs = kbuilder.getKnowledgePackages();
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kpkgs);
    kbase = SerializationHelper.serializeObject(kbase);
    KieSession session = createKnowledgeSession(kbase);
    final List list = new ArrayList();
    session.setGlobal("list", list);
    final Cheese brie = new Cheese("brie", 12);
    FactHandle brieHandle = session.insert(brie);
    final Cheese provolone = new Cheese("provolone", 12);
    FactHandle provoloneHandle = session.insert(provolone);
    session.fireAllRules();
    session = getSerialisedStatefulKnowledgeSession(session, true);
    System.out.println(list);
    assertEquals(3, list.size());
    assertEquals(3, session.getObjects().size());
    brieHandle = getFactHandle(brieHandle, session);
    session.retract(brieHandle);
    session = getSerialisedStatefulKnowledgeSession(session, true);
    assertEquals(2, session.getObjects().size());
    provoloneHandle = getFactHandle(provoloneHandle, session);
    session.retract(provoloneHandle);
    session.fireAllRules();
    assertEquals(0, session.getObjects().size());
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KiePackage(org.kie.api.definition.KiePackage) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) Cheese(org.drools.compiler.Cheese) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 47 with KieSession

use of org.kie.api.runtime.KieSession in project drools by kiegroup.

the class TruthMaintenanceTest method testLogicalInsertionsNot.

@Test(timeout = 10000)
public // @Ignore("in Java 8, the byte[] generated by serialization are not the same and requires investigation")
void testLogicalInsertionsNot() throws Exception {
    KieBase kbase = loadKnowledgeBase("test_LogicalInsertionsNot.drl");
    KieSession ksession = kbase.newKieSession();
    final Person a = new Person("a");
    final Cheese cheese = new Cheese("brie", 1);
    ksession.setGlobal("cheese", cheese);
    ksession.fireAllRules();
    ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
    Collection list = ksession.getObjects();
    assertEquals("i was not asserted by not a => i.", 1, list.size());
    assertEquals("i was not asserted by not a => i.", cheese, list.iterator().next());
    FactHandle h = ksession.insert(a);
    ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
    // no need to fire rules, assertion alone removes justification for i,
    // so it should be deleted.
    // workingMemory.fireAllRules();
    ksession.fireAllRules();
    list = ksession.getObjects();
    assertEquals("a was not asserted or i not deleted.", 1, list.size());
    assertEquals("a was asserted.", a, list.iterator().next());
    assertFalse("i was not rectracted.", list.contains(cheese));
    // no rules should fire, but nevertheless...
    // workingMemory.fireAllRules();
    assertEquals("agenda should be empty.", 0, ((InternalAgenda) ((StatefulKnowledgeSessionImpl) ksession).getAgenda()).agendaSize());
    h = getFactHandle(h, ksession);
    ksession.retract(h);
    ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
    ksession.fireAllRules();
    ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
    list = ksession.getObjects();
    assertEquals("i was not asserted by not a => i.", 1, list.size());
    assertEquals("i was not asserted by not a => i.", cheese, list.iterator().next());
}
Also used : InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) KieBase(org.kie.api.KieBase) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) Collection(java.util.Collection) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.compiler.Cheese) Person(org.drools.compiler.Person) Test(org.junit.Test)

Example 48 with KieSession

use of org.kie.api.runtime.KieSession in project drools by kiegroup.

the class TruthMaintenanceTest method testStatedShadowLogicalWithSingleOccurance.

@Test(timeout = 10000)
public void testStatedShadowLogicalWithSingleOccurance() {
    String droolsSource = "package org.drools.tms.test; \n" + "global java.util.List list; \n" + "rule Justify \n" + "when \n" + "    String( this == 'go1' ) " + "then \n" + "    insertLogical( 'f1' ); \n" + "end \n" + "rule StillHere \n" + "when \n" + "    String( this == 'go2' ) " + "    s : String( this == 'f1' ) " + "then \n" + "    list.add( s ); \n" + "end \n" + "";
    KieBaseConfiguration kieConf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kieConf.setOption(EqualityBehaviorOption.IDENTITY);
    KieBase kbase = loadKnowledgeBaseFromString(kieConf, droolsSource);
    KieSession session = kbase.newKieSession();
    List list = new ArrayList();
    session.setGlobal("list", list);
    InternalFactHandle fh1 = (InternalFactHandle) session.insert("f1");
    InternalFactHandle fh2 = (InternalFactHandle) session.insert("f2");
    session.insert("go1");
    session.fireAllRules();
    session.insert("go2");
    session.fireAllRules();
    // Make sure f1 only occurs once
    assertEquals(1, list.size());
    assertEquals("f1", list.get(0));
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) InternalFactHandle(org.drools.core.common.InternalFactHandle) Test(org.junit.Test)

Example 49 with KieSession

use of org.kie.api.runtime.KieSession in project drools by kiegroup.

the class TruthMaintenanceTest method testLogicalInsertionsWithExists.

@Test(timeout = 10000)
public // @Ignore("in Java 8, the byte[] generated by serialization are not the same and requires investigation")
void testLogicalInsertionsWithExists() throws Exception {
    KieBase kbase = loadKnowledgeBase("test_LogicalInsertionWithExists.drl");
    KieSession ksession = kbase.newKieSession();
    final Person p1 = new Person("p1", "stilton", 20);
    p1.setStatus("europe");
    FactHandle c1FactHandle = ksession.insert(p1);
    final Person p2 = new Person("p2", "stilton", 30);
    p2.setStatus("europe");
    FactHandle c2FactHandle = ksession.insert(p2);
    final Person p3 = new Person("p3", "stilton", 40);
    p3.setStatus("europe");
    FactHandle c3FactHandle = ksession.insert(p3);
    ksession.fireAllRules();
    ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
    // all 3 in europe, so, 2 cheese
    Collection cheeseList = ksession.getObjects(new ClassObjectFilter(Cheese.class));
    assertEquals(2, cheeseList.size());
    // europe=[ 1, 2 ], america=[ 3 ]
    p3.setStatus("america");
    c3FactHandle = getFactHandle(c3FactHandle, ksession);
    ksession.update(c3FactHandle, p3);
    ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
    ksession.fireAllRules();
    ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
    cheeseList = ksession.getObjects(new ClassObjectFilter(Cheese.class));
    assertEquals(1, cheeseList.size());
    // europe=[ 1 ], america=[ 2, 3 ]
    p2.setStatus("america");
    c2FactHandle = getFactHandle(c2FactHandle, ksession);
    ksession.update(c2FactHandle, p2);
    ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
    ksession.fireAllRules();
    ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
    cheeseList = ksession.getObjects(new ClassObjectFilter(Cheese.class));
    assertEquals(1, cheeseList.size());
    // europe=[ ], america=[ 1, 2, 3 ]
    p1.setStatus("america");
    c1FactHandle = getFactHandle(c1FactHandle, ksession);
    ksession.update(c1FactHandle, p1);
    ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
    ksession.fireAllRules();
    ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
    cheeseList = ksession.getObjects(new ClassObjectFilter(Cheese.class));
    assertEquals(2, cheeseList.size());
    // europe=[ 2 ], america=[ 1, 3 ]
    p2.setStatus("europe");
    c2FactHandle = getFactHandle(c2FactHandle, ksession);
    ksession.update(c2FactHandle, p2);
    ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
    ksession.fireAllRules();
    ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
    cheeseList = ksession.getObjects(new ClassObjectFilter(Cheese.class));
    assertEquals(1, cheeseList.size());
    // europe=[ 1, 2 ], america=[ 3 ]
    p1.setStatus("europe");
    c1FactHandle = getFactHandle(c1FactHandle, ksession);
    ksession.update(c1FactHandle, p1);
    ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
    ksession.fireAllRules();
    ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
    cheeseList = ksession.getObjects(new ClassObjectFilter(Cheese.class));
    assertEquals(1, cheeseList.size());
    // europe=[ 1, 2, 3 ], america=[ ]
    p3.setStatus("europe");
    c3FactHandle = getFactHandle(c3FactHandle, ksession);
    ksession.update(c3FactHandle, p3);
    ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
    ksession.fireAllRules();
    ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
    cheeseList = ksession.getObjects(new ClassObjectFilter(Cheese.class));
    assertEquals(2, cheeseList.size());
}
Also used : ClassObjectFilter(org.drools.core.ClassObjectFilter) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) KieBase(org.kie.api.KieBase) Collection(java.util.Collection) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.compiler.Cheese) Person(org.drools.compiler.Person) Test(org.junit.Test)

Example 50 with KieSession

use of org.kie.api.runtime.KieSession in project drools by kiegroup.

the class TruthMaintenanceTest method testRestateJustified.

@Test(timeout = 10000)
public void testRestateJustified() {
    String droolsSource = "package org.drools.tms.test; \n" + "declare Foo id : int @key end \n\n" + "rule Zero \n" + "when \n" + " $s : String( this == \"go\" ) \n" + "then \n" + " insertLogical( new Foo(1) ); \n" + "end \n" + "rule Restate \n" + "when \n" + " $s : String( this == \"go2\" ) \n" + " $f : Foo( id == 1 ) \n" + "then \n" + " insert( $f ); \n" + "end \n";
    // ///////////////////////////////////
    KieSession session = loadKnowledgeBaseFromString(droolsSource).newKieSession();
    session.fireAllRules();
    FactHandle handle = session.insert("go");
    session.fireAllRules();
    FactHandle handle2 = session.insert("go2");
    session.fireAllRules();
    session.delete(handle);
    session.delete(handle2);
    session.fireAllRules();
    assertEquals(1, session.getObjects().size());
    session.dispose();
}
Also used : InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Aggregations

KieSession (org.kie.api.runtime.KieSession)5328 Test (org.junit.Test)4824 KieBase (org.kie.api.KieBase)2414 ArrayList (java.util.ArrayList)2317 List (java.util.List)1105 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)769 FactHandle (org.kie.api.runtime.rule.FactHandle)598 Person (org.drools.modelcompiler.domain.Person)519 HashMap (java.util.HashMap)416 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)415 KieServices (org.kie.api.KieServices)382 KieHelper (org.kie.internal.utils.KieHelper)355 KieContainer (org.kie.api.runtime.KieContainer)298 RuntimeEngine (org.kie.api.runtime.manager.RuntimeEngine)265 InternalFactHandle (org.drools.core.common.InternalFactHandle)259 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)234 AbstractBaseTest (org.jbpm.test.util.AbstractBaseTest)234 ReleaseId (org.kie.api.builder.ReleaseId)232 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)229 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)207