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());
}
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());
}
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));
}
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());
}
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();
}
Aggregations