use of org.drools.core.common.InternalFactHandle in project drools by kiegroup.
the class StreamsTest method testEntryPointReference.
// (timeout=10000)
@Test
public void testEntryPointReference() throws Exception {
// read in the source
KieBase kbase = loadKnowledgeBase("test_EntryPointReference.drl");
KieSession session = kbase.newKieSession();
final List<StockTick> results = new ArrayList<StockTick>();
session.setGlobal("results", results);
StockTickInterface tick5 = new StockTick(5, "DROO", 50, System.currentTimeMillis());
StockTickInterface tick6 = new StockTick(6, "ACME", 10, System.currentTimeMillis());
StockTickInterface tick7 = new StockTick(7, "ACME", 30, System.currentTimeMillis());
StockTickInterface tick8 = new StockTick(8, "DROO", 50, System.currentTimeMillis());
EntryPoint entry = session.getEntryPoint("stream1");
InternalFactHandle handle5 = (InternalFactHandle) entry.insert(tick5);
InternalFactHandle handle6 = (InternalFactHandle) entry.insert(tick6);
InternalFactHandle handle7 = (InternalFactHandle) entry.insert(tick7);
InternalFactHandle handle8 = (InternalFactHandle) entry.insert(tick8);
assertNotNull(handle5);
assertNotNull(handle6);
assertNotNull(handle7);
assertNotNull(handle8);
assertTrue(handle5.isEvent());
assertTrue(handle6.isEvent());
assertTrue(handle7.isEvent());
assertTrue(handle8.isEvent());
session.fireAllRules();
assertEquals(1, results.size());
assertSame(tick7, results.get(0));
}
use of org.drools.core.common.InternalFactHandle in project drools by kiegroup.
the class TruthMaintenanceTest method testLogicalWithDeleteException.
@Test(timeout = 10000)
public void testLogicalWithDeleteException() {
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" + "";
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);
session.insert("go1");
session.fireAllRules();
TruthMaintenanceSystem tms = ((StatefulKnowledgeSessionImpl) session).getTruthMaintenanceSystem();
InternalFactHandle jfh1 = tms.get("f1").getLogicalFactHandle();
assertSame(jfh1, session.getFactHandle("f1"));
}
use of org.drools.core.common.InternalFactHandle in project drools by kiegroup.
the class TruthMaintenanceTest method testJustificationStateOverridingBySuperClass.
@Test(timeout = 10000)
public void testJustificationStateOverridingBySuperClass() {
// DROOLS-352
String droolsSource = "package org.drools.tms.test; \n" + "" + "declare Foo end \n" + "declare Bar extends Foo end \n" + "" + "rule Justify_Sub \n" + "when \n" + "then \n" + " insertLogical( new Bar() ); \n" + " insertLogical( 42.0 ); \n" + "end \n" + "rule Justify_Sup \n" + "when \n" + " $d : Double() \n" + "then \n" + " insertLogical( new Foo() ); \n" + "end \n" + "" + "" + "rule Fuu when Foo() then end \n" + "rule Bor when Bar() then end \n";
// ///////////////////////////////////
KieBaseConfiguration kieConf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
kieConf.setOption(EqualityBehaviorOption.EQUALITY);
KieBase kbase = loadKnowledgeBaseFromString(kieConf, droolsSource);
KieSession session = kbase.newKieSession();
session.fireAllRules();
for (FactHandle fh : session.getFactHandles()) {
InternalFactHandle ifh = (InternalFactHandle) fh;
assertEquals(EqualityKey.JUSTIFIED, ifh.getEqualityKey().getStatus());
}
}
use of org.drools.core.common.InternalFactHandle in project drools by kiegroup.
the class TruthMaintenanceTest method testLogicalInsertionsSelfreferencing.
// (timeout=10000)
@Test
public // @Ignore
void testLogicalInsertionsSelfreferencing() throws Exception {
final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newClassPathResource("test_LogicalInsertionsSelfreferencing.drl", getClass()), ResourceType.DRL);
Collection<KiePackage> kpkgs = kbuilder.getKnowledgePackages();
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(kpkgs);
kbase = SerializationHelper.serializeObject(kbase);
final KieSession session = createKnowledgeSession(kbase);
final Person b = new Person("b");
final Person a = new Person("a");
session.setGlobal("b", b);
FactHandle h1 = session.insert(a);
session.fireAllRules();
Collection<?> list = session.getObjects(new ClassObjectFilter(a.getClass()));
assertEquals(2, list.size());
assertTrue(list.contains(a));
assertTrue(list.contains(b));
session.retract(h1);
session.fireAllRules();
list = session.getObjects(new ClassObjectFilter(a.getClass()));
assertEquals("b was deleted, but it should not have. Is backed by b => b being true.", 1, list.size());
assertEquals("b was deleted, but it should not have. Is backed by b => b being true.", b, list.iterator().next());
h1 = session.getFactHandle(b);
assertSame(((InternalFactHandle) h1).getEqualityKey().getLogicalFactHandle(), h1);
((StatefulKnowledgeSessionImpl) session).getTruthMaintenanceSystem().delete(h1);
session.fireAllRules();
list = session.getObjects(new ClassObjectFilter(a.getClass()));
assertEquals(0, list.size());
}
use of org.drools.core.common.InternalFactHandle in project drools by kiegroup.
the class TruthMaintenanceTest method testLogicalThenUpdateAsStatedShadowSingleOccurance.
@Test(timeout = 10000)
public void testLogicalThenUpdateAsStatedShadowSingleOccurance() {
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);
session.insert("go1");
session.fireAllRules();
TruthMaintenanceSystem tms = ((StatefulKnowledgeSessionImpl) session).getTruthMaintenanceSystem();
InternalFactHandle jfh1 = tms.get("f1").getLogicalFactHandle();
assertEquals(EqualityKey.JUSTIFIED, jfh1.getEqualityKey().getStatus());
InternalFactHandle fh1 = (InternalFactHandle) session.insert("f1");
InternalFactHandle fh2 = (InternalFactHandle) session.insert("f2");
session.insert("go2");
session.fireAllRules();
assertEquals(EqualityKey.STATED, fh1.getEqualityKey().getStatus());
assertSame(fh1.getEqualityKey(), jfh1.getEqualityKey());
assertNotSame(fh1, jfh1);
// Make sure f1 only occurs once
assertEquals(1, list.size());
assertEquals("f1", list.get(0));
}
Aggregations