Search in sources :

Example 21 with EventFactHandle

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

the class StrictAnnotationTest method testJavaSqlTimestamp.

@Test
public void testJavaSqlTimestamp() {
    String str = "package " + Message.class.getPackage().getName() + "\n" + "@Role( Role.Type.EVENT ) @Timestamp( \"startTime\" ) @Duration( \"duration\" )\n" + "declare " + Message.class.getCanonicalName() + "\n" + "end\n";
    Map<String, String> kieModuleConfigurationProperties = new HashMap<>();
    kieModuleConfigurationProperties.put(LanguageLevelOption.PROPERTY_NAME, LanguageLevelOption.DRL6_STRICT.toString());
    KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, kieModuleConfigurationProperties, str);
    KieSession ksession = kbase.newKieSession();
    try {
        Message msg = new Message();
        msg.setStartTime(new Timestamp(10000));
        msg.setDuration(1000l);
        EventFactHandle efh = (EventFactHandle) ksession.insert(msg);
        assertEquals(10000, efh.getStartTimestamp());
        assertEquals(1000, efh.getDuration());
    } finally {
        ksession.dispose();
    }
}
Also used : HashMap(java.util.HashMap) 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 22 with EventFactHandle

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

the class WindowNode method assertObject.

public void assertObject(final InternalFactHandle factHandle, final PropagationContext pctx, final ReteEvaluator reteEvaluator) {
    EventFactHandle evFh = (EventFactHandle) factHandle;
    for (AlphaNodeFieldConstraint constraint : constraints) {
        if (!constraint.isAllowed(evFh, reteEvaluator)) {
            return;
        }
    }
    RightTuple rightTuple = new RightTupleImpl(evFh, this);
    rightTuple.setPropagationContext(pctx);
    // this is cloned, as we need to separate the child RightTuple references
    InternalFactHandle clonedFh = evFh.cloneAndLink();
    rightTuple.setContextObject(clonedFh);
    // process the behavior
    final WindowMemory memory = reteEvaluator.getNodeMemory(this);
    if (!behavior.assertFact(memory.behaviorContext, clonedFh, pctx, reteEvaluator)) {
        return;
    }
    this.sink.propagateAssertObject(clonedFh, pctx, reteEvaluator);
}
Also used : AlphaNodeFieldConstraint(org.drools.core.spi.AlphaNodeFieldConstraint) WindowMemory(org.drools.core.reteoo.WindowNode.WindowMemory) EventFactHandle(org.drools.core.common.EventFactHandle) InternalFactHandle(org.drools.core.common.InternalFactHandle)

Example 23 with EventFactHandle

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

the class CepTest method testComplexTimestamp.

@Test
public void testComplexTimestamp() {
    final String str = "import " + Message.class.getCanonicalName() + "\n" + "declare " + Message.class.getCanonicalName() + "\n" + "   @role( event ) \n" + "   @timestamp( getProperties().get( 'timestamp' ) - 1 ) \n" + "   @duration( getProperties().get( 'duration' ) + 1 ) \n" + "end\n";
    KieSession ksession = getKieSession(getCepKieModuleModel(), str);
    try {
        final Message msg = new Message();
        final Properties props = new Properties();
        props.put("timestamp", 99);
        props.put("duration", 52);
        msg.setProperties(props);
        final EventFactHandle efh = (EventFactHandle) ksession.insert(msg);
        assertEquals(98, efh.getStartTimestamp());
        assertEquals(53, efh.getDuration());
    } finally {
        ksession.dispose();
    }
}
Also used : EventFactHandle(org.drools.core.common.EventFactHandle) KieSession(org.kie.api.runtime.KieSession) Properties(java.util.Properties) Test(org.junit.Test)

Example 24 with EventFactHandle

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

the class ProtobufInputMarshaller method readFactHandle.

public static InternalFactHandle readFactHandle(ProtobufMarshallerReaderContext context, EntryPoint entryPoint, FactHandle _handle) throws IOException, ClassNotFoundException {
    Object object = null;
    ObjectMarshallingStrategy strategy = null;
    if (_handle.hasStrategyIndex()) {
        strategy = context.getUsedStrategies().get(_handle.getStrategyIndex());
        object = strategy.unmarshal(context.getStrategyContexts().get(strategy), context, _handle.getObject().toByteArray(), (context.getKnowledgeBase() == null) ? null : context.getKnowledgeBase().getRootClassLoader());
    }
    EntryPointId confEP;
    if (entryPoint != null) {
        confEP = ((NamedEntryPoint) entryPoint).getEntryPoint();
    } else {
        confEP = context.getWorkingMemory().getEntryPoint();
    }
    ObjectTypeConf typeConf = context.getWorkingMemory().getObjectTypeConfigurationRegistry().getOrCreateObjectTypeConf(confEP, object);
    InternalFactHandle handle = null;
    switch(_handle.getType()) {
        case FACT:
            {
                handle = new DefaultFactHandle(_handle.getId(), object, _handle.getRecency(), (WorkingMemoryEntryPoint) entryPoint, typeConf != null && typeConf.isTrait());
                break;
            }
        case QUERY:
            {
                handle = new QueryElementFactHandle(object, _handle.getId(), _handle.getRecency());
                break;
            }
        case EVENT:
            {
                handle = new EventFactHandle(_handle.getId(), object, _handle.getRecency(), _handle.getTimestamp(), _handle.getDuration(), (WorkingMemoryEntryPoint) entryPoint, typeConf != null && typeConf.isTrait());
                ((EventFactHandle) handle).setExpired(_handle.getIsExpired());
                ((EventFactHandle) handle).setOtnCount(_handle.getOtnCount());
                // ((EventFactHandle) handle).setActivationsCount( _handle.getActivationsCount() );
                break;
            }
        default:
            {
                throw new IllegalStateException("Unable to marshal FactHandle, as type does not exist:" + _handle.getType());
            }
    }
    return handle;
}
Also used : DefaultFactHandle(org.drools.core.common.DefaultFactHandle) ObjectTypeConf(org.drools.core.reteoo.ObjectTypeConf) EntryPointId(org.drools.core.rule.EntryPointId) ObjectMarshallingStrategy(org.kie.api.marshalling.ObjectMarshallingStrategy) QueryElementFactHandle(org.drools.core.common.QueryElementFactHandle) EventFactHandle(org.drools.core.common.EventFactHandle) InternalFactHandle(org.drools.core.common.InternalFactHandle)

Example 25 with EventFactHandle

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

the class CepEspTest method testSubclassWithLongerExpirationThanSuperWithSerialization.

@Test
public void testSubclassWithLongerExpirationThanSuperWithSerialization() {
    // 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);
    KieSession ksession = kbase.newKieSession(KieSessionTestConfiguration.STATEFUL_PSEUDO.getKieSessionConfiguration(), null);
    try {
        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());
        try {
            ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
        } catch (final Exception e) {
            e.printStackTrace();
            fail(e.getMessage());
        }
        clock = ksession.getSessionClock();
        clock.advanceTime(10, TimeUnit.SECONDS);
        ksession.fireAllRules();
        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) ParseException(java.text.ParseException) IOException(java.io.IOException) 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