use of org.kie.internal.runtime.StatefulKnowledgeSession in project drools by kiegroup.
the class StatefulKnowledgeSessionImpl method writeExternal.
public void writeExternal(ObjectOutput out) throws IOException {
// all we do is create marshall to a byte[] and write to the stream
StatefulKnowledgeSession ksession = (StatefulKnowledgeSession) getKnowledgeRuntime();
Marshaller marshaller = MarshallerFactory.newMarshaller(ksession.getKieBase(), new ObjectMarshallingStrategy[] { MarshallerFactory.newSerializeMarshallingStrategy() });
ByteArrayOutputStream stream = new ByteArrayOutputStream();
marshaller.marshall(stream, (StatefulKnowledgeSession) getKnowledgeRuntime());
stream.close();
byte[] bytes = stream.toByteArray();
out.writeInt(bytes.length);
out.write(bytes);
}
use of org.kie.internal.runtime.StatefulKnowledgeSession in project drools by kiegroup.
the class StatelessKnowledgeSessionImpl method execute.
public void execute(Object object) {
StatefulKnowledgeSession ksession = newWorkingMemory();
try {
ksession.insert(object);
ksession.fireAllRules();
} finally {
dispose(ksession);
}
}
use of org.kie.internal.runtime.StatefulKnowledgeSession 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 = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
kbconf.setOption(EventProcessingOption.STREAM);
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(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.internal.runtime.StatefulKnowledgeSession 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.internal.runtime.StatefulKnowledgeSession in project drools by kiegroup.
the class StatefulKnowledgeSessionImpl method writeExternal.
public void writeExternal(ObjectOutput out) throws IOException {
// all we do is create marshall to a byte[] and write to the stream
StatefulKnowledgeSession ksession = (StatefulKnowledgeSession) getKnowledgeRuntime();
Marshaller marshaller = MarshallerFactory.newMarshaller(ksession.getKieBase(), new ObjectMarshallingStrategy[] { MarshallerFactory.newSerializeMarshallingStrategy() });
ByteArrayOutputStream stream = new ByteArrayOutputStream();
marshaller.marshall(stream, (StatefulKnowledgeSession) getKnowledgeRuntime());
stream.close();
byte[] bytes = stream.toByteArray();
out.writeInt(bytes.length);
out.write(bytes);
}
Aggregations