use of org.kie.internal.runtime.StatefulKnowledgeSession in project drools by kiegroup.
the class StatelessKnowledgeSessionImpl method newWorkingMemory.
public StatefulKnowledgeSession newWorkingMemory() {
this.kBase.readLock();
try {
StatefulKnowledgeSession ksession = (StatefulKnowledgeSession) wmFactory.createWorkingMemory(this.kBase.nextWorkingMemoryCounter(), this.kBase, (SessionConfiguration) this.conf, this.environment);
StatefulKnowledgeSessionImpl ksessionImpl = (StatefulKnowledgeSessionImpl) ksession;
((Globals) ksessionImpl.getGlobalResolver()).setDelegate(this.sessionGlobals);
registerListeners(ksessionImpl);
for (Map.Entry<String, Channel> entry : this.channels.entrySet()) {
ksession.registerChannel(entry.getKey(), entry.getValue());
}
wmCreated.incrementAndGet();
return ksession;
} finally {
this.kBase.readUnlock();
}
}
use of org.kie.internal.runtime.StatefulKnowledgeSession in project drools by kiegroup.
the class StatelessKnowledgeSessionImpl method executeWithResults.
public List executeWithResults(Iterable objects, ObjectFilter filter) {
List list = new ArrayList();
StatefulKnowledgeSession ksession = newWorkingMemory();
try {
for (Object object : objects) {
ksession.insert(object);
}
ksession.fireAllRules();
for (FactHandle fh : ksession.getFactHandles(filter)) {
list.add(((InternalFactHandle) fh).getObject());
}
} finally {
dispose(ksession);
}
return list;
}
use of org.kie.internal.runtime.StatefulKnowledgeSession in project drools by kiegroup.
the class StatelessKnowledgeSessionImpl method execute.
public <T> T execute(Command<T> command) {
StatefulKnowledgeSession ksession = newWorkingMemory();
RegistryContext context = new ContextImpl().register(KieSession.class, ksession);
try {
if (command instanceof BatchExecutionCommand) {
((RegistryContext) context).register(ExecutionResultImpl.class, new ExecutionResultImpl());
}
((StatefulKnowledgeSessionImpl) ksession).startBatchExecution();
Object o = ((ExecutableCommand) command).execute(context);
// did the user take control of fireAllRules, if not we will auto execute
boolean autoFireAllRules = true;
if (command instanceof FireAllRulesCommand) {
autoFireAllRules = false;
} else if (command instanceof BatchExecutionCommandImpl) {
for (Command nestedCmd : ((BatchExecutionCommandImpl) command).getCommands()) {
if (nestedCmd instanceof FireAllRulesCommand) {
autoFireAllRules = false;
break;
}
}
}
if (autoFireAllRules) {
ksession.fireAllRules();
}
if (command instanceof BatchExecutionCommand) {
return (T) ((RegistryContext) context).lookup(ExecutionResultImpl.class);
} else {
return (T) o;
}
} finally {
((StatefulKnowledgeSessionImpl) ksession).endBatchExecution();
dispose(ksession);
}
}
use of org.kie.internal.runtime.StatefulKnowledgeSession in project drools by kiegroup.
the class SerializationHelper method getSerialisedStatefulKnowledgeSession.
public static StatefulKnowledgeSession getSerialisedStatefulKnowledgeSession(KieSession ksession, KieBase kbase, boolean dispose, boolean testRoundTrip) throws Exception {
ProtobufMarshaller marshaller = (ProtobufMarshaller) MarshallerFactory.newMarshaller(kbase, (ObjectMarshallingStrategy[]) ksession.getEnvironment().get(EnvironmentName.OBJECT_MARSHALLING_STRATEGIES));
long time = ksession.<SessionClock>getSessionClock().getCurrentTime();
// make sure globas are in the environment of the session
ksession.getEnvironment().set(EnvironmentName.GLOBALS, ksession.getGlobals());
// Serialize object
final byte[] b1;
{
ByteArrayOutputStream bos = new ByteArrayOutputStream();
marshaller.marshall(bos, ksession, time);
b1 = bos.toByteArray();
bos.close();
}
// Deserialize object
StatefulKnowledgeSession ksession2;
{
ByteArrayInputStream bais = new ByteArrayInputStream(b1);
ksession2 = marshaller.unmarshall(bais, ksession.getSessionConfiguration(), ksession.getEnvironment());
bais.close();
}
if (testRoundTrip) {
// for now, we can ensure the IDs will match because queries are creating untraceable fact handles at the moment
// int previous_id = ((StatefulKnowledgeSessionImpl)ksession).session.getFactHandleFactory().getId();
// long previous_recency = ((StatefulKnowledgeSessionImpl)ksession).session.getFactHandleFactory().getRecency();
// int current_id = ((StatefulKnowledgeSessionImpl)ksession2).session.getFactHandleFactory().getId();
// long current_recency = ((StatefulKnowledgeSessionImpl)ksession2).session.getFactHandleFactory().getRecency();
// ((StatefulKnowledgeSessionImpl)ksession2).session.getFactHandleFactory().clear( previous_id, previous_recency );
// Reserialize and check that byte arrays are the same
final byte[] b2;
{
ByteArrayOutputStream bos = new ByteArrayOutputStream();
marshaller.marshall(bos, ksession2, time);
b2 = bos.toByteArray();
bos.close();
}
// bytes should be the same.
if (!areByteArraysEqual(b1, b2)) {
// throw new IllegalArgumentException( "byte streams for serialisation test are not equal" );
}
// ((StatefulKnowledgeSessionImpl) ksession2).session.getFactHandleFactory().clear( current_id, current_recency );
// ((StatefulKnowledgeSessionImpl) ksession2).session.setGlobalResolver( ((StatefulKnowledgeSessionImpl) ksession).session.getGlobalResolver() );
}
if (dispose) {
ksession.dispose();
}
return ksession2;
}
use of org.kie.internal.runtime.StatefulKnowledgeSession in project drools by kiegroup.
the class JpaPersistenceTraitTest method testTripleBasedTraitsWithJPA.
@Test
public void testTripleBasedTraitsWithJPA() {
String str = "package org.drools.trait.test; \n" + "global java.util.List list; \n" + "" + "declare TBean \n" + " @propertyReactive \n" + " @Traitable \n" + " fld : String \n" + "end \n " + "" + "declare trait Mask \n" + " @propertyReactive \n" + " fld : String \n" + " xyz : int \n" + "end \n" + "\n " + "declare trait Cloak \n" + " @propertyReactive \n" + " fld : String \n" + " ijk : String \n" + "end \n" + "" + "rule Init \n" + "when \n" + "then \n" + " insert( new TBean(\"abc\") ); \n" + "end \n" + "" + "rule Don \n" + "no-loop \n" + "when \n" + " $b : TBean( ) \n" + "then \n" + " Mask m = don( $b, Mask.class ); \n" + " modify (m) { setXyz( 10 ); } \n" + " list.add( m ); \n" + " System.out.println( \"Don result : \" + m ); \n " + "end \n" + "\n" + "rule Don2 \n" + "no-loop \n" + "when \n" + " $b : TBean( ) \n" + "then \n" + " Cloak c = don( $b, Cloak.class ); \n" + " modify (c) { setIjk( \"ijklmn\" ); } \n" + " list.add( c ); \n" + " System.out.println( \"Don result : \" + c ); \n " + "end \n" + "";
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
if (kbuilder.hasErrors()) {
fail(kbuilder.getErrors().toString());
}
kbase.addPackages(kbuilder.getKnowledgePackages());
StatefulKnowledgeSession ksession = JPAKnowledgeService.newStatefulKnowledgeSession(kbase, null, env);
// StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
List<?> list = new ArrayList<Object>();
ksession.setGlobal("list", list);
ksession.fireAllRules();
assertEquals(2, list.size());
long id = ksession.getIdentifier();
StatefulKnowledgeSession ksession2 = JPAKnowledgeService.loadStatefulKnowledgeSession(id, kbase, null, env);
// StatefulKnowledgeSession ksession2 = kbase.newStatefulKnowledgeSession();
ksession2.fireAllRules();
Collection x = ksession2.getObjects();
assertEquals(3, x.size());
TraitableBean core = null;
for (Object o : x) {
if (o instanceof TraitableBean) {
core = (TraitableBean) o;
break;
}
}
assertNotNull(core);
assertEquals(2, core._getDynamicProperties().size());
assertNotNull(core.getTrait("org.drools.trait.test.Mask"));
assertNotNull(core.getTrait("org.drools.trait.test.Cloak"));
}
Aggregations