use of org.drools.core.impl.StatefulKnowledgeSessionImpl in project drools by kiegroup.
the class TruthMaintenanceTest method testLogicalWithDeleteException.
@Test(timeout = 10000)
public void testLogicalWithDeleteException() {
String droolsSource = "package org.drools.tms.test; \n" + "global java.util.List list; \n" + "rule Justify \n" + "when \n" + " String( this == 'go1' ) " + "then \n" + " insertLogical( 'f1' ); \n" + "end \n" + "";
KieBaseConfiguration kieConf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
kieConf.setOption(EqualityBehaviorOption.IDENTITY);
KieBase kbase = loadKnowledgeBaseFromString(kieConf, droolsSource);
KieSession session = kbase.newKieSession();
List list = new ArrayList();
session.setGlobal("list", list);
session.insert("go1");
session.fireAllRules();
TruthMaintenanceSystem tms = ((StatefulKnowledgeSessionImpl) session).getTruthMaintenanceSystem();
InternalFactHandle jfh1 = tms.get("f1").getLogicalFactHandle();
assertSame(jfh1, session.getFactHandle("f1"));
}
use of org.drools.core.impl.StatefulKnowledgeSessionImpl in project drools by kiegroup.
the class TruthMaintenanceTest method testLogicalThenUpdateAsStatedShadowSingleOccurance.
@Test(timeout = 10000)
public void testLogicalThenUpdateAsStatedShadowSingleOccurance() {
String droolsSource = "package org.drools.tms.test; \n" + "global java.util.List list; \n" + "rule Justify \n" + "when \n" + " String( this == 'go1' ) " + "then \n" + " insertLogical( 'f1' ); \n" + "end \n" + "rule StillHere \n" + "when \n" + " String( this == 'go2' ) " + " s : String( this == 'f1' ) " + "then \n" + " list.add( s ); \n" + "end \n" + "";
KieBaseConfiguration kieConf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
kieConf.setOption(EqualityBehaviorOption.IDENTITY);
KieBase kbase = loadKnowledgeBaseFromString(kieConf, droolsSource);
KieSession session = kbase.newKieSession();
List list = new ArrayList();
session.setGlobal("list", list);
session.insert("go1");
session.fireAllRules();
TruthMaintenanceSystem tms = ((StatefulKnowledgeSessionImpl) session).getTruthMaintenanceSystem();
InternalFactHandle jfh1 = tms.get("f1").getLogicalFactHandle();
assertEquals(EqualityKey.JUSTIFIED, jfh1.getEqualityKey().getStatus());
InternalFactHandle fh1 = (InternalFactHandle) session.insert("f1");
InternalFactHandle fh2 = (InternalFactHandle) session.insert("f2");
session.insert("go2");
session.fireAllRules();
assertEquals(EqualityKey.STATED, fh1.getEqualityKey().getStatus());
assertSame(fh1.getEqualityKey(), jfh1.getEqualityKey());
assertNotSame(fh1, jfh1);
// Make sure f1 only occurs once
assertEquals(1, list.size());
assertEquals("f1", list.get(0));
}
use of org.drools.core.impl.StatefulKnowledgeSessionImpl in project drools by kiegroup.
the class QueryTest method testDroolsQueryCleanup.
@Test
public void testDroolsQueryCleanup() throws Exception {
KieBase kbase = SerializationHelper.serializeObject(loadKnowledgeBase("test_QueryMemoryLeak.drl"));
KieSession session = createKieSession(kbase);
KieSession ksession = kbase.newKieSession();
String workerId = "B1234";
Worker worker = new Worker();
worker.setId(workerId);
FactHandle handle = ksession.insert(worker);
ksession.fireAllRules();
assertNotNull(handle);
Object retractedWorker = null;
for (int i = 0; i < 100; i++) {
retractedWorker = (Object) ksession.getQueryResults("getWorker", new Object[] { workerId });
}
assertNotNull(retractedWorker);
StatefulKnowledgeSessionImpl sessionImpl = (StatefulKnowledgeSessionImpl) ksession;
Collection<EntryPointNode> entryPointNodes = sessionImpl.getKnowledgeBase().getRete().getEntryPointNodes().values();
EntryPointNode defaultEntryPointNode = null;
for (EntryPointNode epNode : entryPointNodes) {
if (epNode.getEntryPoint().getEntryPointId().equals("DEFAULT")) {
defaultEntryPointNode = epNode;
break;
}
}
assertNotNull(defaultEntryPointNode);
Map<ObjectType, ObjectTypeNode> obnodes = defaultEntryPointNode.getObjectTypeNodes();
ObjectType key = new ClassObjectType(DroolsQuery.class);
ObjectTypeNode droolsQueryNode = obnodes.get(key);
Iterator<InternalFactHandle> it = ((ObjectTypeNodeMemory) sessionImpl.getNodeMemory(droolsQueryNode)).iterator();
assertFalse(it.hasNext());
}
use of org.drools.core.impl.StatefulKnowledgeSessionImpl in project drools by kiegroup.
the class KieContainerImpl method getKieSession.
public KieSession getKieSession(String kSessionName) {
KieSession kieSession = kSessions.get(kSessionName);
if (kieSession instanceof StatefulKnowledgeSessionImpl && !((StatefulKnowledgeSessionImpl) kieSession).isAlive()) {
kSessions.remove(kSessionName);
kieSession = null;
}
return kieSession != null ? kieSession : newKieSession(kSessionName);
}
use of org.drools.core.impl.StatefulKnowledgeSessionImpl in project drools by kiegroup.
the class KieContainerImpl method newKieSession.
public KieSession newKieSession(String kSessionName, Environment environment, KieSessionConfiguration conf) {
KieSessionModelImpl kSessionModel = kSessionName != null ? (KieSessionModelImpl) getKieSessionModel(kSessionName) : (KieSessionModelImpl) findKieSessionModel(false);
if (kSessionModel == null) {
log.error("Unknown KieSession name: " + kSessionName);
return null;
}
if (kSessionModel.getType() == KieSessionModel.KieSessionType.STATELESS) {
throw new RuntimeException("Trying to create a stateful KieSession from a stateless KieSessionModel: " + kSessionModel.getName());
}
KieBase kBase = getKieBase(kSessionModel.getKieBaseModel().getName());
if (kBase == null) {
log.error("Unknown KieBase name: " + kSessionModel.getKieBaseModel().getName());
return null;
}
KieSession kSession = kBase.newKieSession(conf != null ? conf : getKieSessionConfiguration(kSessionModel), environment);
if (isJndiAvailable()) {
wireSessionComponents(kSessionModel, kSession);
}
registerLoggers(kSessionModel, kSession);
((StatefulKnowledgeSessionImpl) kSession).initMBeans(containerId, ((InternalKnowledgeBase) kBase).getId(), kSessionModel.getName());
kSessions.put(kSessionModel.getName(), kSession);
return kSession;
}
Aggregations