Search in sources :

Example 61 with InternalFactHandle

use of org.drools.core.common.InternalFactHandle in project drools by kiegroup.

the class CepEspTest method testEventAssertion.

@Test(timeout = 10000)
public void testEventAssertion() throws Exception {
    KieBase kbase = loadKnowledgeBase("test_CEP_SimpleEventAssertion.drl");
    KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    conf.setOption(ClockTypeOption.get("pseudo"));
    KieSession session = createKnowledgeSession(kbase, conf);
    SessionPseudoClock clock = (SessionPseudoClock) session.<SessionClock>getSessionClock();
    final List results = new ArrayList();
    session.setGlobal("results", results);
    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);
    clock.advanceTime(10, TimeUnit.SECONDS);
    InternalFactHandle handle2 = (InternalFactHandle) session.insert(tick2);
    clock.advanceTime(30, TimeUnit.SECONDS);
    InternalFactHandle handle3 = (InternalFactHandle) session.insert(tick3);
    clock.advanceTime(20, TimeUnit.SECONDS);
    InternalFactHandle handle4 = (InternalFactHandle) session.insert(tick4);
    clock.advanceTime(10, TimeUnit.SECONDS);
    assertNotNull(handle1);
    assertNotNull(handle2);
    assertNotNull(handle3);
    assertNotNull(handle4);
    assertTrue(handle1.isEvent());
    assertTrue(handle2.isEvent());
    assertTrue(handle3.isEvent());
    assertTrue(handle4.isEvent());
    session.fireAllRules();
    assertEquals(2, ((List) session.getGlobal("results")).size());
}
Also used : StockTickInterface(org.drools.compiler.StockTickInterface) StockTick(org.drools.compiler.StockTick) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) 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) Test(org.junit.Test)

Example 62 with InternalFactHandle

use of org.drools.core.common.InternalFactHandle in project drools by kiegroup.

the class CepEspTest method testPackageSerializationWithEvents.

@SuppressWarnings("unchecked")
@Test(timeout = 10000)
public void testPackageSerializationWithEvents() throws Exception {
    // read in the source
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newInputStreamResource(getClass().getResourceAsStream("test_CEP_SimpleEventAssertion.drl")), ResourceType.DRL);
    // get the package
    Collection<KiePackage> pkgs = kbuilder.getKnowledgePackages();
    assertEquals(2, pkgs.size());
    // serialize the package
    byte[] serializedPkg = DroolsStreamUtils.streamOut(pkgs);
    pkgs = (Collection<KiePackage>) DroolsStreamUtils.streamIn(serializedPkg);
    // create the kbase
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(pkgs);
    // create the session
    KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    KieSession session = createKnowledgeSession(kbase, conf);
    final List<StockTick> results = new ArrayList<StockTick>();
    session.setGlobal("results", results);
    StockTickInterface tick1 = new StockTick(1, "DROO", 50, 10000);
    StockTickInterface tick2 = new StockTick(2, "ACME", 10, 10010);
    InternalFactHandle handle1 = (InternalFactHandle) session.insert(tick1);
    InternalFactHandle handle2 = (InternalFactHandle) session.insert(tick2);
    assertNotNull(handle1);
    assertNotNull(handle2);
    assertTrue(handle1.isEvent());
    assertTrue(handle2.isEvent());
    session.fireAllRules();
    assertEquals(1, results.size());
    assertEquals(tick2, results.get(0));
}
Also used : StockTickInterface(org.drools.compiler.StockTickInterface) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) StockTick(org.drools.compiler.StockTick) KiePackage(org.kie.api.definition.KiePackage) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) InternalFactHandle(org.drools.core.common.InternalFactHandle) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Test(org.junit.Test)

Example 63 with InternalFactHandle

use of org.drools.core.common.InternalFactHandle in project drools by kiegroup.

the class CepEspTest method testIdleTime.

// @Test(timeout=10000) @Ignore
// public void testTransactionCorrelation() throws Exception {
// // read in the source
// final Reader reader = new InputStreamReader( getClass().getResourceAsStream( "test_TransactionCorrelation.drl" ) );
// final RuleBase ruleBase = loadRuleBase( reader );
// 
// final WorkingMemory wm = ruleBase.newStatefulSession();
// final List results = new ArrayList();
// 
// wm.setGlobal( "results",
// results );
// 
// 
// }
@Test(timeout = 10000)
public void testIdleTime() throws Exception {
    // read in the source
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newInputStreamResource(getClass().getResourceAsStream("test_CEP_SimpleEventAssertion.drl")), ResourceType.DRL);
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    conf.setOption(ClockTypeOption.get("pseudo"));
    StatefulKnowledgeSessionImpl session = (StatefulKnowledgeSessionImpl) createKnowledgeSession(kbase, conf);
    SessionPseudoClock clock = (SessionPseudoClock) session.getSessionClock();
    final List results = new ArrayList();
    session.setGlobal("results", results);
    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);
    assertEquals(0, session.getIdleTime());
    InternalFactHandle handle1 = (InternalFactHandle) session.insert(tick1);
    clock.advanceTime(10, TimeUnit.SECONDS);
    assertEquals(10000, session.getIdleTime());
    InternalFactHandle handle2 = (InternalFactHandle) session.insert(tick2);
    assertEquals(0, session.getIdleTime());
    clock.advanceTime(15, TimeUnit.SECONDS);
    assertEquals(15000, session.getIdleTime());
    clock.advanceTime(15, TimeUnit.SECONDS);
    assertEquals(30000, session.getIdleTime());
    InternalFactHandle handle3 = (InternalFactHandle) session.insert(tick3);
    assertEquals(0, session.getIdleTime());
    clock.advanceTime(20, TimeUnit.SECONDS);
    InternalFactHandle handle4 = (InternalFactHandle) session.insert(tick4);
    clock.advanceTime(10, TimeUnit.SECONDS);
    assertNotNull(handle1);
    assertNotNull(handle2);
    assertNotNull(handle3);
    assertNotNull(handle4);
    assertTrue(handle1.isEvent());
    assertTrue(handle2.isEvent());
    assertTrue(handle3.isEvent());
    assertTrue(handle4.isEvent());
    assertEquals(10000, session.getIdleTime());
    session.fireAllRules();
    assertEquals(0, session.getIdleTime());
    assertEquals(2, ((List) session.getGlobal("results")).size());
}
Also used : StockTickInterface(org.drools.compiler.StockTickInterface) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) StockTick(org.drools.compiler.StockTick) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) InternalFactHandle(org.drools.core.common.InternalFactHandle) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Test(org.junit.Test)

Example 64 with InternalFactHandle

use of org.drools.core.common.InternalFactHandle in project drools by kiegroup.

the class CepEspTest method testAnnotatedEventAssertion.

@Test(timeout = 10000)
public void testAnnotatedEventAssertion() throws Exception {
    KieBase kbase = loadKnowledgeBase("test_CEP_SimpleAnnotatedEventAssertion.drl");
    KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    conf.setOption(ClockTypeOption.get("pseudo"));
    KieSession session = createKnowledgeSession(kbase, conf);
    SessionPseudoClock clock = (SessionPseudoClock) session.<SessionClock>getSessionClock();
    final List results = new ArrayList();
    session.setGlobal("results", results);
    StockTickInterface tick1 = new StockTickEvent(1, "DROO", 50, 10000);
    StockTickInterface tick2 = new StockTickEvent(2, "ACME", 10, 10010);
    StockTickInterface tick3 = new StockTickEvent(3, "ACME", 10, 10100);
    StockTickInterface tick4 = new StockTickEvent(4, "DROO", 50, 11000);
    InternalFactHandle handle1 = (InternalFactHandle) session.insert(tick1);
    clock.advanceTime(10, TimeUnit.SECONDS);
    InternalFactHandle handle2 = (InternalFactHandle) session.insert(tick2);
    clock.advanceTime(30, TimeUnit.SECONDS);
    InternalFactHandle handle3 = (InternalFactHandle) session.insert(tick3);
    clock.advanceTime(20, TimeUnit.SECONDS);
    InternalFactHandle handle4 = (InternalFactHandle) session.insert(tick4);
    clock.advanceTime(10, TimeUnit.SECONDS);
    assertNotNull(handle1);
    assertNotNull(handle2);
    assertNotNull(handle3);
    assertNotNull(handle4);
    assertTrue(handle1.isEvent());
    assertTrue(handle2.isEvent());
    assertTrue(handle3.isEvent());
    assertTrue(handle4.isEvent());
    session.fireAllRules();
    assertEquals(2, ((List) session.getGlobal("results")).size());
}
Also used : StockTickInterface(org.drools.compiler.StockTickInterface) StockTickEvent(org.drools.compiler.StockTickEvent) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) 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) Test(org.junit.Test)

Example 65 with InternalFactHandle

use of org.drools.core.common.InternalFactHandle in project drools by kiegroup.

the class BackwardChainingTest method testInsertionOrderTwo.

@Test(timeout = 10000)
public void testInsertionOrderTwo() throws Exception {
    String str = "" + "package org.drools.compiler.test \n" + "import java.util.List \n" + "global List list \n" + "declare Thing \n" + "    thing : String @key \n" + "end \n" + "declare Edible extends Thing \n" + "end \n" + "declare Location extends Thing \n" + "    location : String  @key \n" + "end \n" + "declare Here \n" + "    place : String \n" + "end \n" + "rule kickOff \n" + "when \n" + "    Integer( $i: intValue ) \n" + "then \n" + "    switch( $i ){ \n";
    String[] facts = new String[] { "new Edible( 'peach' )", "new Location( 'peach', 'table' )", "new Here( 'table' )" };
    int f = 0;
    for (int i = 0; i < facts.length; i++) {
        for (int j = 0; j < facts.length; j++) {
            for (int k = 0; k < facts.length; k++) {
                // use a Set to make sure we only include 3 unique values
                Set<String> set = new HashSet<String>();
                set.add(facts[i]);
                set.add(facts[j]);
                set.add(facts[k]);
                if (set.size() == 3) {
                    str += "    case " + f++ + ": \n" + // "        System.out.println( \"s) \"+" + (f-1) + ");\n" +
                    "        insert( " + facts[i] + " ); \n" + "        insert( " + facts[j] + " ); \n" + "        insert( " + facts[k] + " ); \n" + "        break; \n";
                }
            }
        }
    }
    facts = new String[] { "new Edible( 'peach' )", "new Location( 'table', 'office' )", "new Location( 'peach', 'table' )", "new Here( 'office' )" };
    int h = f;
    for (int i = 0; i < facts.length; i++) {
        for (int j = 0; j < facts.length; j++) {
            for (int k = 0; k < facts.length; k++) {
                for (int l = 0; l < facts.length; l++) {
                    // use a Set to make sure we only include 3 unique values
                    Set<String> set = new HashSet<String>();
                    set.add(facts[i]);
                    set.add(facts[j]);
                    set.add(facts[k]);
                    set.add(facts[l]);
                    if (set.size() == 4) {
                        str += "    case " + h++ + ": \n" + // "        System.out.println( \"s) \"+" + (h-1) + ");\n" +
                        "        insert( " + facts[i] + " ); \n" + "        insert( " + facts[j] + " ); \n" + "        insert( " + facts[k] + " ); \n" + "        insert( " + facts[l] + " ); \n" + "        break; \n";
                    }
                }
            }
        }
    }
    str += "    } \n" + "end \n" + "\n" + "query whereFood( String x, String y ) \n" + "    ( Location(x, y;) and \n" + "    Edible(x;) ) \n " + "    or  \n" + "    ( Location(z, y;) and whereFood(x, z;) ) \n" + "end " + "query look(String place, List things, List food)  \n" + "    Here(place;) \n" + "    things := List() from accumulate( Location(thing, place;), \n" + "                                      collectList( thing ) ) \n" + "    food := List() from accumulate( whereFood(thing, place;), \n" + "                                    collectList( thing ) ) \n" + "end \n" + "rule reactiveLook \n" + "when \n" + "    Here( $place : place)  \n" + "    look($place, $things; $food := food) \n" + "then \n" + "    list.addAll( $things ); \n" + "    list.addAll( $food   ); \n" + // "    System.out.println( $things + \":\" + $food ); \n" +
    "end \n" + "";
    // System.out.println( str );
    KieBase kbase = SerializationHelper.serializeObject(loadKnowledgeBaseFromString(str));
    for (int i = 0; i < f; i++) {
        KieSession ksession = createKnowledgeSession(kbase);
        List<String> list = new ArrayList<String>();
        ksession.setGlobal("list", list);
        // ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
        ksession.fireAllRules();
        list.clear();
        InternalFactHandle fh = (InternalFactHandle) ksession.insert(Integer.valueOf(i));
        // ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
        ksession.fireAllRules();
        // ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true, false);
        assertEquals(2, list.size());
        assertEquals("peach", list.get(0));
        assertEquals("peach", list.get(1));
        list.clear();
        InternalFactHandle[] handles = ksession.getFactHandles().toArray(new InternalFactHandle[0]);
        for (int j = 0; j < handles.length; j++) {
            if (handles[j].getObject() instanceof InitialFact || handles[j].getObject() instanceof Integer) {
                continue;
            }
            // ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true, false);
            handles[j] = getFactHandle(handles[j], ksession);
            Object o = handles[j].getObject();
            // first retract + assert
            ksession.retract(handles[j]);
            // ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true, false);
            handles[j] = (InternalFactHandle) ksession.insert(o);
            // ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true, false);
            ksession.fireAllRules();
            assertEquals(2, list.size());
            assertEquals("peach", list.get(0));
            assertEquals("peach", list.get(1));
            list.clear();
            // now try update
            // session was serialised so need to get factHandle
            handles[j] = getFactHandle(handles[j], ksession);
            ksession.update(handles[j], handles[j].getObject());
            // ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true, false);
            ksession.fireAllRules();
            assertEquals(2, list.size());
            assertEquals("peach", list.get(0));
            assertEquals("peach", list.get(1));
            list.clear();
        }
        fh = getFactHandle(fh, ksession);
        ksession.retract(fh);
        ksession.dispose();
    }
    for (int i = f; i < h; i++) {
        // h; i++) {
        KieSession ksession = createKnowledgeSession(kbase);
        List<String> list = new ArrayList<String>();
        ksession.setGlobal("list", list);
        ksession.fireAllRules();
        list.clear();
        // ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
        InternalFactHandle fh = (InternalFactHandle) ksession.insert(Integer.valueOf(i));
        ksession.fireAllRules();
        assertEquals(2, list.size());
        assertEquals("table", list.get(0));
        assertEquals("peach", list.get(1));
        list.clear();
        // ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true, false);
        InternalFactHandle[] handles = ksession.getFactHandles().toArray(new InternalFactHandle[0]);
        for (int j = 0; j < handles.length; j++) {
            if (handles[j].getObject() instanceof InitialFact || handles[j].getObject() instanceof Integer) {
                continue;
            }
            // if ( !handles[j].getObject().toString().equals( "Location( thing=peach, location=table )" )) {
            // continue;
            // }
            // ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true, false);
            handles[j] = getFactHandle(handles[j], ksession);
            Object o = handles[j].getObject();
            // first retract + assert
            ksession.retract(handles[j]);
            // ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true, false);
            handles[j] = (InternalFactHandle) ksession.insert(o);
            // ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true, false);
            ksession.fireAllRules();
            assertEquals(2, list.size());
            assertEquals("table", list.get(0));
            assertEquals("peach", list.get(1));
            list.clear();
            // ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true, false);
            // now try update
            handles[j] = getFactHandle(handles[j], ksession);
            ksession.update(handles[j], handles[j].getObject());
            // ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true, false);
            ksession.fireAllRules();
            assertEquals(2, list.size());
            assertEquals("table", list.get(0));
            assertEquals("peach", list.get(1));
            list.clear();
        }
        // ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true, false);
        fh = getFactHandle(fh, ksession);
        ksession.retract(fh);
        ksession.dispose();
    }
}
Also used : ArrayList(java.util.ArrayList) KieBase(org.kie.api.KieBase) InitialFact(org.drools.core.InitialFact) KieSession(org.kie.api.runtime.KieSession) InternalFactHandle(org.drools.core.common.InternalFactHandle) HashSet(java.util.HashSet) 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