use of org.kie.api.marshalling.ObjectMarshallingStrategy in project drools by kiegroup.
the class MarshallingTest method testDisposeAfterMarshall.
@Test(timeout = 10_000L)
public void testDisposeAfterMarshall() throws InterruptedException, IOException {
// DROOLS-4413
String str = "package com.sample\n" + "rule R when\n" + " $s : String()\n" + "then\n" + " System.out.println($s);\n" + "end\n";
KieBase kbase = new KieHelper().addContent(str, ResourceType.DRL).build();
KieSession ksession = kbase.newKieSession();
CountDownLatch latch = new CountDownLatch(1);
Thread t = new Thread(() -> {
System.out.println("Firing.");
latch.countDown();
ksession.fireUntilHalt();
System.out.println("Halted.");
});
t.start();
// wait fireUntilHalt to be invoked
latch.await();
Thread.sleep(100L);
// Halt the session without adding any facts
ksession.halt();
KieMarshallers kMarshallers = KieServices.Factory.get().getMarshallers();
ObjectMarshallingStrategy oms = kMarshallers.newSerializeMarshallingStrategy();
Marshaller marshaller = kMarshallers.newMarshaller(kbase, new ObjectMarshallingStrategy[] { oms });
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
marshaller.marshall(baos, ksession);
}
// Destroy
ksession.dispose();
ksession.destroy();
// Wait for our thread to exit
// ** The thread exits if we call t.interrupt();
t.join();
}
use of org.kie.api.marshalling.ObjectMarshallingStrategy 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);
}
}
use of org.kie.api.marshalling.ObjectMarshallingStrategy in project drools by kiegroup.
the class ObjectMarshallingStrategyStoreTest method testMultipleObjectMarshallingStrategiesOfTheSameClassWithDifferentNames.
@Test
public void testMultipleObjectMarshallingStrategiesOfTheSameClassWithDifferentNames() throws IOException, ClassNotFoundException {
Environment env = EnvironmentFactory.newEnvironment();
final Thing entityOne = new Thing(1, "Object 1");
final Thing entityTwo = new Thing(2, "Object 2");
Collection srcItems = new ArrayList();
srcItems.add(entityOne);
srcItems.add(entityTwo);
ObjectMarshallingStrategy[] strats = new ObjectMarshallingStrategy[] { new IdentityPlaceholderResolverStrategy("entityOne", new ObjectMarshallingStrategyAcceptor() {
@Override
public boolean accept(Object object) {
return entityOne.equals(object);
}
}, Collections.singletonMap(entityOne.id, (Object) entityOne)), new IdentityPlaceholderResolverStrategy("entityTwo", new ObjectMarshallingStrategyAcceptor() {
@Override
public boolean accept(Object object) {
return entityTwo.equals(object);
}
}, Collections.singletonMap(entityTwo.id, (Object) entityTwo)) };
env.set(EnvironmentName.OBJECT_MARSHALLING_STRATEGIES, strats);
KieSessionConfiguration ksc = SessionConfiguration.newInstance();
final KieBaseConfiguration kbconf = RuleBaseFactory.newKnowledgeBaseConfiguration();
kbconf.setOption(EventProcessingOption.STREAM);
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(RuleBaseFactory.newRuleBase(kbconf));
KieSession ks = kbase.newKieSession(ksc, env);
ks.insert(entityOne);
ks.insert(entityTwo);
ProtobufMarshaller marshaller = (ProtobufMarshaller) MarshallerFactory.newMarshaller(kbase, strats);
// Serialize object
final byte[] b1;
{
ByteArrayOutputStream bos = new ByteArrayOutputStream();
marshaller.marshall(bos, ks, System.currentTimeMillis());
b1 = bos.toByteArray();
bos.close();
}
// Deserialize object
StatefulKnowledgeSession ksession2;
{
ByteArrayInputStream bais = new ByteArrayInputStream(b1);
try {
ksession2 = marshaller.unmarshall(bais, ks.getSessionConfiguration(), ks.getEnvironment());
Collection items = ksession2.getFactHandles();
Assert.assertTrue(items.size() == 2);
for (Object item : items) {
FactHandle factHandle = (FactHandle) item;
Assert.assertTrue(srcItems.contains(((DefaultFactHandle) factHandle).getObject()));
}
} catch (RuntimeException npe) {
// Here ocurrs the bug that shows that NamedObjectMarshallingStrategies are required.
Assert.fail("This error only happens if identity ObjectMarshallingStrategy use old name");
} finally {
bais.close();
}
}
}
use of org.kie.api.marshalling.ObjectMarshallingStrategy in project drools by kiegroup.
the class ObjectMarshallingStrategyStoreTest method testThrowErrorWhenExistMultipleMarshallingStrategiesWithSameName.
@Test
public void testThrowErrorWhenExistMultipleMarshallingStrategiesWithSameName() throws IOException, ClassNotFoundException {
Environment env = EnvironmentFactory.newEnvironment();
final Thing entityOne = new Thing(1, "Object 1");
final Thing entityTwo = new Thing(2, "Object 2");
Collection srcItems = new ArrayList();
srcItems.add(entityOne);
srcItems.add(entityTwo);
ObjectMarshallingStrategy[] strats = new ObjectMarshallingStrategy[] { new IdentityPlaceholderResolverStrategy(new ObjectMarshallingStrategyAcceptor() {
@Override
public boolean accept(Object object) {
return entityOne.equals(object);
}
}, Collections.singletonMap(entityOne.id, (Object) entityOne)), new IdentityPlaceholderResolverStrategy(new ObjectMarshallingStrategyAcceptor() {
@Override
public boolean accept(Object object) {
return entityTwo.equals(object);
}
}, Collections.singletonMap(entityTwo.id, (Object) entityTwo)) };
env.set(EnvironmentName.OBJECT_MARSHALLING_STRATEGIES, strats);
KieSessionConfiguration ksc = SessionConfiguration.newInstance();
final KieBaseConfiguration kbconf = RuleBaseFactory.newKnowledgeBaseConfiguration();
kbconf.setOption(EventProcessingOption.STREAM);
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(RuleBaseFactory.newRuleBase(kbconf));
KieSession ks = kbase.newKieSession(ksc, env);
ks.insert(entityOne);
ks.insert(entityTwo);
try {
ProtobufMarshaller marshaller = (ProtobufMarshaller) MarshallerFactory.newMarshaller(kbase, strats);
// Here ocurrs the bug that shows that NamedObjectMarshallingStrategies are required.
Assert.fail("A runtime error must be thrown while found strategies with same name");
} catch (RuntimeException re) {
Assert.assertTrue(re.getMessage().contains("Multiple"));
}
}
use of org.kie.api.marshalling.ObjectMarshallingStrategy in project drools by kiegroup.
the class PersisterHelper method writeStrategiesIndex.
private static void writeStrategiesIndex(MarshallerWriteContext context, ProtobufMessages.Header.Builder _header) throws IOException {
for (Entry<ObjectMarshallingStrategy, Integer> entry : context.getUsedStrategies().entrySet()) {
Builder _strat = ProtobufMessages.Header.StrategyIndex.newBuilder().setId(entry.getValue().intValue()).setName(entry.getKey().getName());
Context ctx = context.getStrategyContext().get(entry.getKey());
if (ctx != null) {
Output os = ByteString.newOutput();
ctx.write(new DroolsObjectOutputStream(os));
_strat.setData(os.toByteString());
os.close();
}
_header.addStrategy(_strat.build());
}
}
Aggregations