use of org.drools.core.common.InternalFactHandle in project drools by kiegroup.
the class TraitTest method testTupleIntegrityOnModification.
@Test
public void testTupleIntegrityOnModification() {
String drl = "package test " + "import " + Entity.class.getName() + ";" + "global java.util.List list; " + "declare trait A @propertyReactive value : int end " + "rule Trait when " + " $core: Entity( ) " + "then " + " A o = don( $core, A.class ); " + " System.out.println( 'Found ! ' + o ); " + "end " + "rule Test when " + " $x: A( value == 0 ) " + "then " + " list.add( 0 ); " + "end " + "rule Check when " + " $x: A( value == 42 ) " + "then " + " list.add( 42 ); " + "end " + "rule Mood when " + " $x : A( value != 42 ) " + "then " + " modify ( $x ) { setValue( 42 ); } " + "end ";
KieBase kbase = getKieBaseFromString(drl);
TraitFactory.setMode(mode, kbase);
KieSession ksession = kbase.newKieSession();
List list = new ArrayList();
ksession.setGlobal("list", list);
ksession.insert(new Entity());
ksession.fireAllRules();
for (final Object o : ksession.getObjects(new ObjectFilter() {
@Override
public boolean accept(Object object) {
return object.getClass().getName().contains("test.A");
}
})) {
InternalFactHandle handle = (InternalFactHandle) ksession.getFactHandle(o);
LeftTuple first = handle.getFirstLeftTuple();
assertTrue(first instanceof RuleTerminalNodeLeftTuple);
assertEquals("Check", ((RuleTerminalNodeLeftTuple) first).getRule().getName());
}
assertEquals(Arrays.asList(0, 42), list);
}
use of org.drools.core.common.InternalFactHandle in project drools by kiegroup.
the class CepEspTest method testEventAssertionWithDuration.
@Test(timeout = 10000)
public void testEventAssertionWithDuration() throws Exception {
KieBase kbase = loadKnowledgeBase("test_CEP_SimpleEventAssertionWithDuration.drl");
KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
conf.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
KieSession session = createKnowledgeSession(kbase, conf);
final List results = new ArrayList();
session.setGlobal("results", results);
StockTickInterface tick1 = new StockTick(1, "DROO", 50, 10000, 5);
StockTickInterface tick2 = new StockTick(2, "ACME", 10, 11000, 10);
StockTickInterface tick3 = new StockTick(3, "ACME", 10, 12000, 8);
StockTickInterface tick4 = new StockTick(4, "DROO", 50, 13000, 7);
InternalFactHandle handle1 = (InternalFactHandle) session.insert(tick1);
InternalFactHandle handle2 = (InternalFactHandle) session.insert(tick2);
InternalFactHandle handle3 = (InternalFactHandle) session.insert(tick3);
InternalFactHandle handle4 = (InternalFactHandle) session.insert(tick4);
assertNotNull(handle1);
assertNotNull(handle2);
assertNotNull(handle3);
assertNotNull(handle4);
assertTrue(handle1.isEvent());
assertTrue(handle2.isEvent());
assertTrue(handle3.isEvent());
assertTrue(handle4.isEvent());
EventFactHandle eh1 = (EventFactHandle) handle1;
EventFactHandle eh2 = (EventFactHandle) handle2;
EventFactHandle eh3 = (EventFactHandle) handle3;
EventFactHandle eh4 = (EventFactHandle) handle4;
assertEquals(tick1.getTime(), eh1.getStartTimestamp());
assertEquals(tick2.getTime(), eh2.getStartTimestamp());
assertEquals(tick3.getTime(), eh3.getStartTimestamp());
assertEquals(tick4.getTime(), eh4.getStartTimestamp());
assertEquals(tick1.getDuration(), eh1.getDuration());
assertEquals(tick2.getDuration(), eh2.getDuration());
assertEquals(tick3.getDuration(), eh3.getDuration());
assertEquals(tick4.getDuration(), eh4.getDuration());
session.fireAllRules();
assertEquals(2, results.size());
}
use of org.drools.core.common.InternalFactHandle in project drools by kiegroup.
the class CepEspTest method testCoincidesOnArbitraryDates.
@Test(timeout = 10000)
public void testCoincidesOnArbitraryDates() throws Exception {
// read in the source
KieBaseConfiguration conf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
conf.setOption(EventProcessingOption.STREAM);
final KieBase kbase = loadKnowledgeBase(conf, "test_CEP_CoincidesOperatorDates.drl");
KieSessionConfiguration sconf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
sconf.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
KieSession wm = createKnowledgeSession(kbase, sconf);
final List<?> results = new ArrayList<Object>();
wm.setGlobal("results", results);
StockTickInterface tick1 = new StockTick(1, "DROO", 50, // arbitrary timestamp
100000, 3);
StockTickInterface tick2 = new StockTick(2, "ACME", 10, // 50 milliseconds after DROO
100050, 3);
InternalFactHandle handle1 = (InternalFactHandle) wm.insert(tick1);
InternalFactHandle handle2 = (InternalFactHandle) wm.insert(tick2);
assertNotNull(handle1);
assertNotNull(handle2);
assertTrue(handle1.isEvent());
assertTrue(handle2.isEvent());
// wm = SerializationHelper.serializeObject(wm);
wm.fireAllRules();
assertEquals(4, results.size());
assertEquals(tick1, results.get(0));
assertEquals(tick2, results.get(1));
assertEquals(tick1, results.get(2));
assertEquals(tick2, results.get(3));
}
use of org.drools.core.common.InternalFactHandle in project drools by kiegroup.
the class CepEspTest method testEventDeclarationForInterfaces.
@Test(timeout = 10000)
public void testEventDeclarationForInterfaces() throws Exception {
// read in the source
final KieBase kbase = loadKnowledgeBase("test_CEP_EventInterfaces.drl");
KieSession session = createKnowledgeSession(kbase);
StockTickInterface tick1 = new StockTick(1, "DROO", 50, 10000);
StockTickInterface tick2 = new StockTick(2, "ACME", 10, 10010);
StockTickInterface tick3 = new StockTick(3, "ACME", 10, 10100);
StockTickInterface tick4 = new StockTick(4, "DROO", 50, 11000);
InternalFactHandle handle1 = (InternalFactHandle) session.insert(tick1);
InternalFactHandle handle2 = (InternalFactHandle) session.insert(tick2);
InternalFactHandle handle3 = (InternalFactHandle) session.insert(tick3);
InternalFactHandle handle4 = (InternalFactHandle) session.insert(tick4);
assertTrue(handle1.isEvent());
assertTrue(handle2.isEvent());
assertTrue(handle3.isEvent());
assertTrue(handle4.isEvent());
}
use of org.drools.core.common.InternalFactHandle in project drools by kiegroup.
the class CepEspTest method testBeforeOnArbitraryDates.
@Test(timeout = 10000)
public void testBeforeOnArbitraryDates() throws Exception {
// read in the source
KieBaseConfiguration conf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
conf.setOption(EventProcessingOption.STREAM);
final KieBase kbase = loadKnowledgeBase(conf, "test_CEP_BeforeOperatorDates.drl");
KieSessionConfiguration sconf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
sconf.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
KieSession wm = createKnowledgeSession(kbase, sconf);
final List<?> results = new ArrayList<Object>();
wm.setGlobal("results", results);
StockTickInterface tick1 = new StockTick(1, "DROO", 50, // arbitrary timestamp
104000, 3);
StockTickInterface tick2 = new StockTick(2, "ACME", 10, // 4 seconds after DROO
100000, 3);
InternalFactHandle handle1 = (InternalFactHandle) wm.insert(tick1);
InternalFactHandle handle2 = (InternalFactHandle) wm.insert(tick2);
assertNotNull(handle1);
assertNotNull(handle2);
assertTrue(handle1.isEvent());
assertTrue(handle2.isEvent());
// wm = SerializationHelper.serializeObject(wm);
wm.fireAllRules();
assertEquals(4, results.size());
assertEquals(tick1, results.get(0));
assertEquals(tick2, results.get(1));
assertEquals(tick1, results.get(2));
assertEquals(tick2, results.get(3));
}
Aggregations