Search in sources :

Example 81 with EventFactHandle

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

the class TypeDeclarationTest method testMultipleAnnotationDeclarations.

@Test
public void testMultipleAnnotationDeclarations() {
    String str1 = "";
    str1 += "package org.kie1 " + "" + "declare Foo \n" + "   @role(event) " + "    name : String " + "    age : int " + "end ";
    String str2 = "" + "package org.kie3 " + "" + "declare org.kie1.Foo " + "    @propertyReactive " + "end ";
    String str3 = "" + "package org.kie4; " + "import org.kie1.Foo; " + "" + "rule Check " + "when " + " $f : Foo( name == 'bar' ) " + "then " + " modify( $f ) { setAge( 99 ); } " + "end ";
    KieBase kieBase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, str1, str2, str3);
    FactType type = kieBase.getFactType("org.kie1", "Foo");
    assertEquals(2, type.getFields().size());
    Object foo = null;
    try {
        foo = type.newInstance();
        type.set(foo, "name", "bar");
        assertEquals("bar", type.get(foo, "name"));
    } catch (InstantiationException e) {
        fail(e.getMessage());
    } catch (IllegalAccessException e) {
        fail(e.getMessage());
    }
    KieSession session = kieBase.newKieSession();
    FactHandle handle = session.insert(foo);
    int n = session.fireAllRules(5);
    assertTrue(handle instanceof EventFactHandle);
    assertEquals(1, n);
    assertEquals(99, type.get(foo, "age"));
}
Also used : EventFactHandle(org.drools.core.common.EventFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) KieBase(org.kie.api.KieBase) EventFactHandle(org.drools.core.common.EventFactHandle) KieSession(org.kie.api.runtime.KieSession) FactType(org.kie.api.definition.type.FactType) Test(org.junit.Test)

Example 82 with EventFactHandle

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

the class OldOutputMarshallerMethods method writeFactHandle_v1.

// Changed with JBRULES-3257
public static void writeFactHandle_v1(MarshallerWriteContext context, ObjectOutputStream stream, ObjectMarshallingStrategyStore objectMarshallingStrategyStore, int type, InternalFactHandle handle) throws IOException {
    stream.writeInt(type);
    stream.writeLong(handle.getId());
    stream.writeLong(handle.getRecency());
    if (type == 2) {
        // is event
        EventFactHandle efh = (EventFactHandle) handle;
        stream.writeLong(efh.getStartTimestamp());
        stream.writeLong(efh.getDuration());
        stream.writeBoolean(efh.isExpired());
        stream.writeLong(efh.getActivationsCount());
    }
    // context.out.println( "Object : int:" + handle.getId() + " long:" + handle.getRecency() );
    // context.out.println( handle.getObject() );
    Object object = handle.getObject();
    if (object != null) {
        int index = objectMarshallingStrategyStore.getStrategy(object);
        ObjectMarshallingStrategy strategy = objectMarshallingStrategyStore.getStrategy(index);
        stream.writeInt(index);
        strategy.write(stream, object);
    } else {
        stream.writeInt(-1);
    }
    String entryPoint = handle.getEntryPointId() != null ? handle.getEntryPointId().getEntryPointId() : null;
    if (entryPoint != null && !entryPoint.equals("")) {
        stream.writeBoolean(true);
        stream.writeUTF(entryPoint);
    } else {
        stream.writeBoolean(false);
    }
}
Also used : ObjectMarshallingStrategy(org.kie.api.marshalling.ObjectMarshallingStrategy) EventFactHandle(org.drools.core.common.EventFactHandle)

Example 83 with EventFactHandle

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

the class CepEspTest method testJavaSqlTimestamp.

@Test(timeout = 10000)
public void testJavaSqlTimestamp() {
    final String drl = "package " + Message.class.getPackage().getName() + "\n" + "declare " + Message.class.getCanonicalName() + "\n" + "   @role( event ) \n" + "   @timestamp( startTime ) \n" + "   @duration( duration )\n" + "end\n";
    final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("cep-esp-test", kieBaseTestConfiguration, drl);
    final KieSession ksession = kbase.newKieSession();
    try {
        final Message msg = new Message();
        msg.setStartTime(new Timestamp(10000));
        msg.setDuration(1000L);
        final EventFactHandle efh = (EventFactHandle) ksession.insert(msg);
        assertEquals(10000, efh.getStartTimestamp());
        assertEquals(1000, efh.getDuration());
    } finally {
        ksession.dispose();
    }
}
Also used : KieBase(org.kie.api.KieBase) EventFactHandle(org.drools.core.common.EventFactHandle) KieSession(org.kie.api.runtime.KieSession) Timestamp(java.sql.Timestamp) Test(org.junit.Test)

Example 84 with EventFactHandle

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

the class CepEspTest method testEventAssertionWithDateTimestamp.

@Test(timeout = 10000)
public void testEventAssertionWithDateTimestamp() {
    final String drl = "package org.drools.compiler\n" + "\n" + "import " + StockTick.class.getCanonicalName() + ";\n" + "\n" + "global java.util.List results;\n" + "\n" + "declare StockTick \n" + "    @role( event )\n" + "    @timestamp( dateTimestamp )\n" + "end\n" + "\n" + "rule \"Check event\"\n" + "when\n" + "    $st : StockTick( company == \"ACME\" )\n" + "then\n" + "    results.add( $st );\n" + "end\n";
    final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("cep-esp-test", kieBaseTestConfiguration, drl);
    final KieSession session = kbase.newKieSession(KieSessionTestConfiguration.STATEFUL_PSEUDO.getKieSessionConfiguration(), null);
    try {
        final List results = new ArrayList();
        session.setGlobal("results", results);
        final StockTick tick1 = new StockTick(1, "DROO", 50, 10000, 5);
        final StockTick tick2 = new StockTick(2, "ACME", 10, 11000, 10);
        final StockTick tick3 = new StockTick(3, "ACME", 10, 12000, 8);
        final StockTick tick4 = new StockTick(4, "DROO", 50, 13000, 7);
        final InternalFactHandle handle1 = (InternalFactHandle) session.insert(tick1);
        final InternalFactHandle handle2 = (InternalFactHandle) session.insert(tick2);
        final InternalFactHandle handle3 = (InternalFactHandle) session.insert(tick3);
        final 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());
        final EventFactHandle eh1 = (EventFactHandle) handle1;
        final EventFactHandle eh2 = (EventFactHandle) handle2;
        final EventFactHandle eh3 = (EventFactHandle) handle3;
        final 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());
    } finally {
        session.dispose();
    }
}
Also used : StockTick(org.drools.testcoverage.common.model.StockTick) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) EventFactHandle(org.drools.core.common.EventFactHandle) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) InternalFactHandle(org.drools.core.common.InternalFactHandle) Test(org.junit.Test)

Example 85 with EventFactHandle

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

the class CepEspTest method testSubclassWithLongerExpirationThanSuper.

@Test
public void testSubclassWithLongerExpirationThanSuper() {
    // DROOLS-983
    final String drl = "import " + SuperClass.class.getCanonicalName() + "\n" + "import " + SubClass.class.getCanonicalName() + "\n" + "\n" + "rule R1 when\n" + "    $e : SuperClass()\n" + "then\n" + "end\n" + "rule R2 when\n" + "    $e : SubClass()\n" + "    not SubClass(this != $e)\n" + "then\n" + "end";
    final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("cep-esp-test", kieBaseTestConfiguration, drl);
    final KieSession ksession = kbase.newKieSession(KieSessionTestConfiguration.STATEFUL_PSEUDO.getKieSessionConfiguration(), null);
    try {
        final PseudoClockScheduler clock = ksession.getSessionClock();
        final EventFactHandle handle1 = (EventFactHandle) ksession.insert(new SubClass());
        ksession.fireAllRules();
        clock.advanceTime(15, TimeUnit.SECONDS);
        ksession.fireAllRules();
        assertFalse(handle1.isExpired());
        assertEquals(1, ksession.getObjects().size());
        clock.advanceTime(10, TimeUnit.SECONDS);
        ksession.fireAllRules();
        assertTrue(handle1.isExpired());
        assertEquals(0, ksession.getObjects().size());
    } finally {
        ksession.dispose();
    }
}
Also used : KieBase(org.kie.api.KieBase) EventFactHandle(org.drools.core.common.EventFactHandle) KieSession(org.kie.api.runtime.KieSession) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) Test(org.junit.Test)

Aggregations

EventFactHandle (org.drools.core.common.EventFactHandle)86 Test (org.junit.Test)51 KieSession (org.kie.api.runtime.KieSession)23 KieBase (org.kie.api.KieBase)18 InternalFactHandle (org.drools.core.common.InternalFactHandle)16 DisconnectedWorkingMemoryEntryPoint (org.drools.core.common.DisconnectedWorkingMemoryEntryPoint)13 DisconnectedWorkingMemoryEntryPoint (org.drools.kiesession.entrypoints.DisconnectedWorkingMemoryEntryPoint)13 FactHandle (org.kie.api.runtime.rule.FactHandle)11 DuringEvaluatorDefinition (org.drools.core.base.evaluators.DuringEvaluatorDefinition)10 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)10 ObjectMarshallingStrategy (org.kie.api.marshalling.ObjectMarshallingStrategy)8 ArrayList (java.util.ArrayList)6 List (java.util.List)6 QueryElementFactHandle (org.drools.core.common.QueryElementFactHandle)6 WorkingMemoryEntryPoint (org.drools.core.WorkingMemoryEntryPoint)5 ObjectTypeConf (org.drools.core.reteoo.ObjectTypeConf)5 PropagationContext (org.drools.core.spi.PropagationContext)5 StockTick (org.drools.testcoverage.common.model.StockTick)5 EntryPointId (org.drools.core.rule.EntryPointId)4 AlphaNodeFieldConstraint (org.drools.core.spi.AlphaNodeFieldConstraint)4