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();
}
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;
}
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;
}
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());
}
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));
}
Aggregations