Search in sources :

Example 86 with InternalFactHandle

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]);
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) StockTickInterface(org.drools.compiler.StockTickInterface) StockTick(org.drools.compiler.StockTick) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) InternalFactHandle(org.drools.core.common.InternalFactHandle) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) Test(org.junit.Test)

Example 87 with InternalFactHandle

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));
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) StockTickInterface(org.drools.compiler.StockTickInterface) StockTick(org.drools.compiler.StockTick) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) InternalFactHandle(org.drools.core.common.InternalFactHandle) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Test(org.junit.Test)

Example 88 with InternalFactHandle

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());
}
Also used : StockTickInterface(org.drools.compiler.StockTickInterface) StockTick(org.drools.compiler.StockTick) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) EventFactHandle(org.drools.core.common.EventFactHandle) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) InternalFactHandle(org.drools.core.common.InternalFactHandle) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Test(org.junit.Test)

Example 89 with InternalFactHandle

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));
}
Also used : InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) InternalFactHandle(org.drools.core.common.InternalFactHandle) Person(org.drools.compiler.Person) QueryResults(org.kie.api.runtime.rule.QueryResults) Test(org.junit.Test)

Example 90 with InternalFactHandle

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));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) MarshallerProviderImpl(org.drools.core.marshalling.impl.MarshallerProviderImpl) MarshallerWriteContext(org.drools.core.marshalling.impl.MarshallerWriteContext) MarshallerReaderContext(org.drools.core.marshalling.impl.MarshallerReaderContext) ByteArrayOutputStream(java.io.ByteArrayOutputStream) InternalFactHandle(org.drools.core.common.InternalFactHandle) ObjectOutputStream(java.io.ObjectOutputStream) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) ObjectMarshallingStrategyStoreImpl(org.drools.core.marshalling.impl.ObjectMarshallingStrategyStoreImpl) Test(org.junit.Test)

Aggregations

InternalFactHandle (org.drools.core.common.InternalFactHandle)182 Test (org.junit.Test)89 KieSession (org.kie.api.runtime.KieSession)66 ArrayList (java.util.ArrayList)63 List (java.util.List)41 KieBase (org.kie.api.KieBase)31 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)29 LeftTuple (org.drools.core.reteoo.LeftTuple)27 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)21 StatefulKnowledgeSessionImpl (org.drools.core.impl.StatefulKnowledgeSessionImpl)21 FactHandle (org.kie.api.runtime.rule.FactHandle)21 RightTuple (org.drools.core.reteoo.RightTuple)20 Declaration (org.drools.core.rule.Declaration)20 LeftTupleImpl (org.drools.core.reteoo.LeftTupleImpl)16 StockTickInterface (org.drools.compiler.StockTickInterface)15 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)15 RightTupleImpl (org.drools.core.reteoo.RightTupleImpl)15 StockTick (org.drools.compiler.StockTick)14 Cheese (org.drools.core.test.model.Cheese)14 KieBaseConfiguration (org.kie.api.KieBaseConfiguration)14