use of org.drools.core.common.InternalFactHandle in project drools by kiegroup.
the class CepEspTest method testMetByOperator.
@Test(timeout = 10000)
public void testMetByOperator() throws Exception {
// read in the source
KieBaseConfiguration conf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
conf.setOption(EventProcessingOption.STREAM);
final KieBase kbase = loadKnowledgeBase(conf, "test_CEP_MetByOperator.drl");
KieSessionConfiguration sconf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
sconf.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
KieSession ksession = createKnowledgeSession(kbase, sconf);
final PseudoClockScheduler clock = (PseudoClockScheduler) ksession.<PseudoClockScheduler>getSessionClock();
clock.setStartupTime(1000);
List list = new ArrayList();
ksession.setGlobal("list", list);
StockTickInterface tick1 = new StockTick(1, "DROO", 50, System.currentTimeMillis(), 3);
StockTickInterface tick2 = new StockTick(2, "ACME", 10, System.currentTimeMillis(), 3);
StockTickInterface tick3 = new StockTick(3, "ACME", 10, System.currentTimeMillis(), 3);
StockTickInterface tick4 = new StockTick(4, "DROO", 50, System.currentTimeMillis(), 5);
StockTickInterface tick5 = new StockTick(5, "ACME", 10, System.currentTimeMillis(), 5);
StockTickInterface tick6 = new StockTick(6, "ACME", 10, System.currentTimeMillis(), 3);
StockTickInterface tick7 = new StockTick(7, "ACME", 10, System.currentTimeMillis(), 5);
StockTickInterface tick8 = new StockTick(8, "ACME", 10, System.currentTimeMillis(), 3);
InternalFactHandle fh1 = (InternalFactHandle) ksession.insert(tick1);
clock.advanceTime(4, TimeUnit.MILLISECONDS);
InternalFactHandle fh2 = (InternalFactHandle) ksession.insert(tick2);
clock.advanceTime(4, TimeUnit.MILLISECONDS);
ksession.insert(tick3);
clock.advanceTime(4, TimeUnit.MILLISECONDS);
ksession.insert(tick4);
ksession.insert(tick5);
clock.advanceTime(1, TimeUnit.MILLISECONDS);
ksession.insert(tick6);
ksession.insert(tick7);
clock.advanceTime(2, TimeUnit.MILLISECONDS);
ksession.insert(tick8);
ksession.fireAllRules();
assertEquals(1, list.size());
StockTick[] stocks = (StockTick[]) list.get(0);
assertSame(tick1, stocks[0]);
assertSame(tick2, stocks[1]);
}
use of org.drools.core.common.InternalFactHandle in project drools by kiegroup.
the class CepEspTest method testAfterOnArbitraryDates.
@Test(timeout = 10000)
public void testAfterOnArbitraryDates() throws Exception {
// read in the source
KieBaseConfiguration conf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
conf.setOption(EventProcessingOption.STREAM);
final KieBase kbase = loadKnowledgeBase(conf, "test_CEP_AfterOperatorDates.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, // 4 seconds after DROO
104000, 3);
InternalFactHandle handle2 = (InternalFactHandle) wm.insert(tick2);
InternalFactHandle handle1 = (InternalFactHandle) wm.insert(tick1);
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 testEventAssertionWithDateTimestamp.
@Test(timeout = 10000)
public void testEventAssertionWithDateTimestamp() throws Exception {
KieBase kbase = loadKnowledgeBase("test_CEP_SimpleEventAssertionWithDateTimestamp.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());
session.fireAllRules();
assertEquals(2, results.size());
}
use of org.drools.core.common.InternalFactHandle in project drools by kiegroup.
the class BackwardChainingTest method testOpenBackwardChain.
// (timeout = 10000)
@Test
public void testOpenBackwardChain() throws Exception {
// http://www.amzi.com/AdventureInProlog/advtop.php
String str = "" + "package org.drools.compiler.test \n" + "import java.util.List\n" + "import java.util.ArrayList\n" + "import org.drools.compiler.Person\n" + "global List list\n" + "dialect \"mvel\"\n" + "declare Location\n" + " thing : String \n" + " location : String \n" + "end" + "\n" + "query isContainedIn( String x, String y ) \n" + " Location(x, y;)\n" + " or \n" + " ( Location(z, y;) and isContainedIn(x, z;) )\n" + "end\n" + "\n" + "rule look when \n" + " Person( $l : likes ) \n" + " isContainedIn( $l, 'office'; )\n" + "then\n" + " insertLogical( 'blah' );" + "end\n" + "rule existsBlah when \n" + " exists String( this == 'blah') \n" + "then\n" + " list.add( 'exists blah' );" + "end\n" + "\n" + "rule notBlah when \n" + " not String( this == 'blah') \n" + "then\n" + " list.add( 'not blah' );" + "end\n" + "\n" + "rule init when\n" + "then\n" + // " insert( new Location(\"apple\", \"kitchen\") );\n" +
" insert( new Location(\"desk\", \"office\") );\n" + // " insert( new Location(\"flashlight\", \"desk\") );\n" +
" insert( new Location(\"envelope\", \"desk\") );\n" + " insert( new Location(\"key\", \"envelope\") );\n" + // " insert( new Location(\"computer\", \"office\") );\n" +
"end\n" + "\n" + "rule go1 when \n" + " String( this == 'go1') \n" + "then\n" + " list.add( rule.getName() ); \n" + " insert( new Location('lamp', 'desk') );\n" + "end\n" + "\n" + "rule go2 when \n" + " String( this == 'go2') \n" + " $l : Location('lamp', 'desk'; )\n" + "then\n" + " list.add( rule.getName() ); \n" + " retract( $l );\n" + "end\n" + "\n" + "rule go3 when \n" + " String( this == 'go3') \n" + "then\n" + " list.add( rule.getName() ); \n" + " insert( new Location('lamp', 'desk') );\n" + "end\n" + "\n" + "rule go4 when \n" + " String( this == 'go4') \n" + " $l : Location('lamp', 'desk'; )\n" + "then\n" + " list.add( rule.getName() ); \n" + " modify( $l ) { thing = 'book' };\n" + "end\n" + "\n" + "rule go5 when \n" + " String( this == 'go5') \n" + " $l : Location('book', 'desk'; )\n" + "then\n" + " list.add( rule.getName() ); \n" + " modify( $l ) { thing = 'lamp' };\n" + "end\n" + "\n" + "rule go6 when \n" + " String( this == 'go6') \n" + " $l : Location( 'lamp', 'desk'; )\n" + "then\n" + " list.add( rule.getName() ); \n" + " modify( $l ) { thing = 'book' };\n" + "end\n" + "\n" + "rule go7 when \n" + " String( this == 'go7') \n" + " $p : Person( likes == 'lamp' ) \n" + "then\n" + " list.add( rule.getName() ); \n" + " modify( $p ) { likes = 'key' };\n" + "end\n" + "\n";
KieBase kbase = SerializationHelper.serializeObject(loadKnowledgeBaseFromString(str));
KieSession ksession = createKnowledgeSession(kbase);
List<String> list = new ArrayList<String>();
ksession.setGlobal("list", list);
QueryResults results = null;
Person p = new Person();
p.setLikes("lamp");
FactHandle handle = ksession.insert(p);
// ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
ksession.fireAllRules();
assertEquals("not blah", list.get(0));
list.clear();
InternalFactHandle fh = (InternalFactHandle) ksession.insert("go1");
// ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
ksession.fireAllRules();
fh = getFactHandle(fh, ksession);
ksession.retract(fh);
assertEquals("go1", list.get(0));
assertEquals("exists blah", list.get(1));
fh = (InternalFactHandle) ksession.insert("go2");
// ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
ksession.fireAllRules();
fh = getFactHandle(fh, ksession);
ksession.retract(fh);
assertEquals("go2", list.get(2));
assertEquals("not blah", list.get(3));
fh = (InternalFactHandle) ksession.insert("go3");
logger.trace("--------------");
ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
ksession.fireAllRules();
fh = getFactHandle(fh, ksession);
ksession.retract(fh);
assertEquals("go3", list.get(4));
assertEquals("exists blah", list.get(5));
fh = (InternalFactHandle) ksession.insert("go4");
ksession.fireAllRules();
fh = getFactHandle(fh, ksession);
ksession.retract(fh);
ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
assertEquals("go4", list.get(6));
assertEquals("not blah", list.get(7));
fh = (InternalFactHandle) ksession.insert("go5");
ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
ksession.fireAllRules();
fh = getFactHandle(fh, ksession);
ksession.retract(fh);
ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
assertEquals("go5", list.get(8));
assertEquals("exists blah", list.get(9));
// This simulates a modify of the root DroolsQuery object, but first we break it
fh = (InternalFactHandle) ksession.insert("go6");
ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
ksession.fireAllRules();
fh = getFactHandle(fh, ksession);
ksession.retract(fh);
ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
assertEquals("go6", list.get(10));
assertEquals("not blah", list.get(11));
// now fix it
fh = (InternalFactHandle) ksession.insert("go7");
ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
ksession.fireAllRules();
fh = getFactHandle(fh, ksession);
ksession.retract(fh);
ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
assertEquals("go7", list.get(12));
assertEquals("exists blah", list.get(13));
}
use of org.drools.core.common.InternalFactHandle in project drools by kiegroup.
the class FactHandleMarshallingTest method backwardsCompatibleEventFactHandleTest.
@Test
public void backwardsCompatibleEventFactHandleTest() throws IOException, ClassNotFoundException {
InternalKnowledgeBase kBase = createKnowledgeBase();
StatefulKnowledgeSessionImpl wm = createWorkingMemory(kBase);
InternalFactHandle factHandle = createEventFactHandle(wm, kBase);
// marshall/serialize workItem
byte[] byteArray;
{
ObjectMarshallingStrategy[] strats = new ObjectMarshallingStrategy[] { MarshallerFactory.newSerializeMarshallingStrategy(), new MarshallerProviderImpl().newIdentityMarshallingStrategy() };
ByteArrayOutputStream baos = new ByteArrayOutputStream();
MarshallerWriteContext outContext = new MarshallerWriteContext(baos, null, null, null, new ObjectMarshallingStrategyStoreImpl(strats), true, true, null);
OldOutputMarshallerMethods.writeFactHandle_v1(outContext, (ObjectOutputStream) outContext, outContext.objectMarshallingStrategyStore, 2, factHandle);
outContext.close();
byteArray = baos.toByteArray();
}
// unmarshall/deserialize workItem
InternalFactHandle newFactHandle;
{
// Only put serialization strategy in
ObjectMarshallingStrategy[] newStrats = new ObjectMarshallingStrategy[] { MarshallerFactory.newSerializeMarshallingStrategy() };
ByteArrayInputStream bais = new ByteArrayInputStream(byteArray);
MarshallerReaderContext inContext = new MarshallerReaderContext(bais, null, null, new ObjectMarshallingStrategyStoreImpl(newStrats), Collections.EMPTY_MAP, true, true, null);
inContext.wm = wm;
newFactHandle = InputMarshaller.readFactHandle(inContext);
inContext.close();
}
assertTrue("Serialized FactHandle not the same as the original.", compareInstances(factHandle, newFactHandle));
}
Aggregations