Search in sources :

Example 1 with EventFactHandle

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

the class ProtobufOutputMarshaller method writeFactHandle.

private static ProtobufMessages.FactHandle writeFactHandle(MarshallerWriteContext context, ObjectMarshallingStrategyStore objectMarshallingStrategyStore, InternalFactHandle handle) throws IOException {
    ProtobufMessages.FactHandle.Builder _handle = ProtobufMessages.FactHandle.newBuilder();
    _handle.setType(getHandleType(handle));
    _handle.setId(handle.getId());
    _handle.setRecency(handle.getRecency());
    if (_handle.getType() == ProtobufMessages.FactHandle.HandleType.EVENT) {
        // is event
        EventFactHandle efh = (EventFactHandle) handle;
        _handle.setTimestamp(efh.getStartTimestamp());
        _handle.setDuration(efh.getDuration());
        _handle.setIsExpired(efh.isExpired());
        _handle.setActivationsCount(efh.getActivationsCount());
        _handle.setOtnCount(efh.getOtnCount());
    }
    if (handle.getEqualityKey() != null && handle.getEqualityKey().getStatus() == EqualityKey.JUSTIFIED) {
        _handle.setIsJustified(true);
    } else {
        _handle.setIsJustified(false);
    }
    Object object = handle.getObject();
    if (object != null) {
        ObjectMarshallingStrategy strategy = objectMarshallingStrategyStore.getStrategyObject(object);
        Integer index = context.getStrategyIndex(strategy);
        _handle.setStrategyIndex(index);
        _handle.setObject(ByteString.copyFrom(strategy.marshal(context.strategyContext.get(strategy), context, object)));
    }
    return _handle.build();
}
Also used : InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.drools.core.marshalling.impl.ProtobufMessages.FactHandle) QueryElementFactHandle(org.drools.core.common.QueryElementFactHandle) EventFactHandle(org.drools.core.common.EventFactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) ObjectMarshallingStrategy(org.kie.api.marshalling.ObjectMarshallingStrategy) EventFactHandle(org.drools.core.common.EventFactHandle)

Example 2 with EventFactHandle

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

the class InputMarshaller method readFactHandle.

public static InternalFactHandle readFactHandle(MarshallerReaderContext context) throws IOException, ClassNotFoundException {
    int type = context.stream.readInt();
    int id = context.stream.readInt();
    long recency = context.stream.readLong();
    long startTimeStamp = 0;
    long duration = 0;
    boolean expired = false;
    long activationsCount = 0;
    if (type == 2) {
        startTimeStamp = context.stream.readLong();
        duration = context.stream.readLong();
        expired = context.stream.readBoolean();
        activationsCount = context.stream.readLong();
    }
    int strategyIndex = context.stream.readInt();
    Object object = null;
    ObjectMarshallingStrategy strategy = null;
    // This is the old way of de/serializing strategy objects
    if (strategyIndex >= 0) {
        strategy = context.resolverStrategyFactory.getStrategy(strategyIndex);
    } else // This is the new way
    if (strategyIndex == -2) {
        String strategyClassName = context.stream.readUTF();
        if (!StringUtils.isEmpty(strategyClassName)) {
            strategy = context.resolverStrategyFactory.getStrategyObject(strategyClassName);
            if (strategy == null) {
                throw new IllegalStateException("No strategy of type " + strategyClassName + " available.");
            }
        }
    }
    // If either way retrieves a strategy, use it
    if (strategy != null) {
        object = strategy.read(context.stream);
    }
    EntryPoint entryPoint = null;
    if (context.readBoolean()) {
        String entryPointId = context.readUTF();
        if (entryPointId != null && !entryPointId.equals("")) {
            entryPoint = ((RuleRuntime) context.wm).getEntryPoint(entryPointId);
        }
    }
    EntryPointId confEP;
    if (entryPoint != null) {
        confEP = ((NamedEntryPoint) entryPoint).getEntryPoint();
    } else {
        confEP = context.wm.getEntryPoint();
    }
    ObjectTypeConf typeConf = context.wm.getObjectTypeConfigurationRegistry().getObjectTypeConf(confEP, object);
    InternalFactHandle handle = null;
    switch(type) {
        case 0:
            {
                handle = new DefaultFactHandle(id, object, recency, (WorkingMemoryEntryPoint) entryPoint, typeConf != null && typeConf.isTrait());
                break;
            }
        case 1:
            {
                handle = new QueryElementFactHandle(object, id, recency);
                break;
            }
        case 2:
            {
                handle = new EventFactHandle(id, object, recency, startTimeStamp, duration, (WorkingMemoryEntryPoint) entryPoint, typeConf != null && typeConf.isTrait());
                ((EventFactHandle) handle).setExpired(expired);
                ((EventFactHandle) handle).setActivationsCount(activationsCount);
                break;
            }
        default:
            {
                throw new IllegalStateException("Unable to marshal FactHandle, as type does not exist:" + type);
            }
    }
    return handle;
}
Also used : ObjectMarshallingStrategy(org.kie.api.marshalling.ObjectMarshallingStrategy) WorkingMemoryEntryPoint(org.drools.core.WorkingMemoryEntryPoint) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) EntryPoint(org.kie.api.runtime.rule.EntryPoint) WorkingMemoryEntryPoint(org.drools.core.WorkingMemoryEntryPoint) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) EntryPoint(org.kie.api.runtime.rule.EntryPoint) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) ObjectTypeConf(org.drools.core.reteoo.ObjectTypeConf) EntryPointId(org.drools.core.rule.EntryPointId) QueryElementFactHandle(org.drools.core.common.QueryElementFactHandle) EventFactHandle(org.drools.core.common.EventFactHandle) InternalFactHandle(org.drools.core.common.InternalFactHandle)

Example 3 with EventFactHandle

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

the class ProtobufInputMarshaller method readFactHandle.

public static InternalFactHandle readFactHandle(MarshallerReaderContext context, EntryPoint entryPoint, FactHandle _handle) throws IOException, ClassNotFoundException {
    Object object = null;
    ObjectMarshallingStrategy strategy = null;
    if (_handle.hasStrategyIndex()) {
        strategy = context.usedStrategies.get(_handle.getStrategyIndex());
        object = strategy.unmarshal(context.strategyContexts.get(strategy), context, _handle.getObject().toByteArray(), (context.kBase == null) ? null : context.kBase.getRootClassLoader());
    }
    EntryPointId confEP;
    if (entryPoint != null) {
        confEP = ((NamedEntryPoint) entryPoint).getEntryPoint();
    } else {
        confEP = context.wm.getEntryPoint();
    }
    ObjectTypeConf typeConf = context.wm.getObjectTypeConfigurationRegistry().getObjectTypeConf(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 4 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";
    KieSession ksession = new KieHelper().setKieModuleModel(KieServices.Factory.get().newKieModuleModel().setConfigurationProperty(LanguageLevelOption.PROPERTY_NAME, LanguageLevelOption.DRL6_STRICT.toString())).addContent(str, ResourceType.DRL).build().newKieSession();
    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());
}
Also used : KieHelper(org.kie.internal.utils.KieHelper) EventFactHandle(org.drools.core.common.EventFactHandle) KieSession(org.kie.api.runtime.KieSession) Timestamp(java.sql.Timestamp) Test(org.junit.Test)

Example 5 with EventFactHandle

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

the class ExtendsTest method testInheritAnnotationsInOtherPackage.

@Test
public void testInheritAnnotationsInOtherPackage() throws Exception {
    String s1 = "package org.drools.compiler.test.pack1;\n" + "global java.util.List list;" + "\n" + "declare Event\n" + "@role(event)" + "  id    : int\n" + "end\n" + "\n" + "rule \"X\"\n" + "when\n" + "  $s1 : Event()\n" + "then\n" + "  System.out.println( $s1 );\n" + "  list.add( $s1.getId() );\n " + "end";
    String s2 = "package org.drools.compiler.test.pack2;\n" + "\n" + "import org.drools.compiler.test.pack1.Event;\n" + "global java.util.List list;" + "\n" + "declare Event end\n" + "\n" + "declare SubEvent extends Event\n" + "end\n" + "\n" + "rule \"Init\"\n" + "when\n" + "then\n" + "  list.add( 0 );\n" + "  insert( new SubEvent( 1 ) );\n" + "  insert( new SubEvent( 2 ) );\n" + "end\n" + "\n" + "rule \"Seq\"\n" + "when\n" + "  $s1 : SubEvent( id == 1 )\n" + "  $s2 : SubEvent( id == 2, this after[0,10s] $s1 )\n" + "then\n" + "  list.add( 3 );\n" + "  System.out.println( $s1 + \" after \" + $s1 );\n" + "end \n" + "\n" + "rule \"Seq 2 \"\n" + "when\n" + "  $s1 : Event( id == 1 )\n" + "  $s2 : Event( id == 2, this after[0,10s] $s1 )\n" + "then\n" + "  list.add( 4 );\n" + "  System.out.println( $s1 + \" after II \" + $s1 );\n" + "end";
    KieSession kSession = new KieHelper().addContent(s1, ResourceType.DRL).addContent(s2, ResourceType.DRL).build().newKieSession();
    List list = new ArrayList();
    kSession.setGlobal("list", list);
    kSession.fireAllRules();
    for (Object o : kSession.getObjects()) {
        FactHandle h = kSession.getFactHandle(o);
        assertTrue(h instanceof EventFactHandle);
    }
    System.out.println(list);
    assertEquals(5, list.size());
    assertTrue(list.contains(0));
    assertTrue(list.contains(1));
    assertTrue(list.contains(2));
    assertTrue(list.contains(3));
    assertTrue(list.contains(4));
}
Also used : InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) EventFactHandle(org.drools.core.common.EventFactHandle) ArrayList(java.util.ArrayList) KieHelper(org.kie.internal.utils.KieHelper) EventFactHandle(org.drools.core.common.EventFactHandle) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) 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