use of org.drools.core.base.ClassObjectType in project drools by kiegroup.
the class PropertySpecificTest method testBetaSharedAlphaNoWatches.
@Test
public void testBetaSharedAlphaNoWatches() {
String rule1 = "$b : B( a == 15) @watch(c, !a) A( a == 10, s == 15, b == $b.b )";
String rule2 = "$b : B( a == 15) @watch(j, !i) A( a == 10, i == 20, b == $b.b )";
KieBase kbase = getKnowledgeBase(rule1, rule2);
InternalWorkingMemory wm = ((InternalWorkingMemory) kbase.newKieSession());
ObjectTypeNode otn = getObjectTypeNode(kbase, "A");
assertNotNull(otn);
Class classType = ((ClassObjectType) otn.getObjectType()).getClassType();
List<String> sp = getSettableProperties(wm, otn);
AlphaNode alphaNode1 = (AlphaNode) otn.getObjectSinkPropagator().getSinks()[0];
assertEquals(calculatePositiveMask(classType, list("a"), sp), alphaNode1.getDeclaredMask());
assertEquals(calculatePositiveMask(classType, list("a", "b", "s", "i"), sp), alphaNode1.getInferredMask());
// first share
AlphaNode alphaNode1_1 = (AlphaNode) alphaNode1.getObjectSinkPropagator().getSinks()[0];
assertEquals(calculatePositiveMask(classType, list("s"), sp), alphaNode1_1.getDeclaredMask());
assertEquals(calculatePositiveMask(classType, list("a", "s", "b"), sp), alphaNode1_1.getInferredMask());
BetaNode betaNode1 = (BetaNode) alphaNode1_1.getObjectSinkPropagator().getSinks()[0];
assertEquals(calculatePositiveMask(classType, list("b"), sp), betaNode1.getRightDeclaredMask());
assertEquals(calculatePositiveMask(classType, list("a", "s", "b"), sp), betaNode1.getRightInferredMask());
assertEquals(calculatePositiveMask(classType, list("b", "c"), sp), betaNode1.getLeftDeclaredMask());
assertEquals(calculatePositiveMask(classType, list("b", "c"), sp), betaNode1.getLeftInferredMask());
assertEquals(calculateNegativeMask(classType, list("!a"), sp), betaNode1.getLeftNegativeMask());
// second share
AlphaNode alphaNode1_2 = (AlphaNode) alphaNode1.getObjectSinkPropagator().getSinks()[1];
assertEquals(calculatePositiveMask(classType, list("i"), sp), alphaNode1_2.getDeclaredMask());
assertEquals(calculatePositiveMask(classType, list("a", "i", "b"), sp), alphaNode1_2.getInferredMask());
BetaNode betaNode2 = (BetaNode) alphaNode1_2.getObjectSinkPropagator().getSinks()[0];
assertEquals(calculatePositiveMask(classType, list("b"), sp), betaNode2.getRightDeclaredMask());
assertEquals(calculatePositiveMask(classType, list("a", "i", "b"), sp), betaNode2.getRightInferredMask());
assertEquals(calculatePositiveMask(classType, list("b", "j"), sp), betaNode2.getLeftDeclaredMask());
assertEquals(calculatePositiveMask(classType, list("a", "b", "j"), sp), betaNode2.getLeftInferredMask());
assertEquals(calculateNegativeMask(classType, list("!i"), sp), betaNode2.getLeftNegativeMask());
// test rule removal
kbase.removeRule("org.drools.compiler.integrationtests", "r0");
assertEquals(calculatePositiveMask(classType, list("a"), sp), alphaNode1.getDeclaredMask());
assertEquals(calculatePositiveMask(classType, list("a", "i", "b"), sp), alphaNode1.getInferredMask());
assertEquals(calculatePositiveMask(classType, list("i"), sp), alphaNode1_2.getDeclaredMask());
assertEquals(calculatePositiveMask(classType, list("a", "i", "b"), sp), alphaNode1_2.getInferredMask());
assertEquals(calculatePositiveMask(classType, list("b"), sp), betaNode2.getRightDeclaredMask());
assertEquals(calculatePositiveMask(classType, list("a", "i", "b"), sp), betaNode2.getRightInferredMask());
assertEquals(calculatePositiveMask(classType, list("b", "c"), sp), betaNode1.getLeftDeclaredMask());
assertEquals(calculatePositiveMask(classType, list("b", "c"), sp), betaNode1.getLeftInferredMask());
assertEquals(calculateNegativeMask(classType, list("!a"), sp), betaNode1.getLeftNegativeMask());
// have to rebuild to remove r1
kbase = getKnowledgeBase(rule1, rule2);
kbase.removeRule("org.drools.compiler.integrationtests", "r1");
otn = getObjectTypeNode(kbase, "A");
alphaNode1 = (AlphaNode) otn.getObjectSinkPropagator().getSinks()[0];
assertEquals(calculatePositiveMask(classType, list("a"), sp), alphaNode1.getDeclaredMask());
assertEquals(calculatePositiveMask(classType, list("a", "s", "b"), sp), alphaNode1.getInferredMask());
alphaNode1_1 = (AlphaNode) alphaNode1.getObjectSinkPropagator().getSinks()[0];
assertEquals(calculatePositiveMask(classType, list("s"), sp), alphaNode1_1.getDeclaredMask());
assertEquals(calculatePositiveMask(classType, list("a", "s", "b"), sp), alphaNode1_1.getInferredMask());
betaNode1 = (BetaNode) alphaNode1_1.getObjectSinkPropagator().getSinks()[0];
assertEquals(calculatePositiveMask(classType, list("b"), sp), betaNode1.getRightDeclaredMask());
assertEquals(calculatePositiveMask(classType, list("a", "s", "b"), sp), betaNode1.getRightInferredMask());
assertEquals(calculatePositiveMask(classType, list("b", "j"), sp), betaNode2.getLeftDeclaredMask());
assertEquals(calculatePositiveMask(classType, list("a", "b", "j"), sp), betaNode2.getLeftInferredMask());
assertEquals(calculateNegativeMask(classType, list("!i"), sp), betaNode2.getLeftNegativeMask());
}
use of org.drools.core.base.ClassObjectType in project drools by kiegroup.
the class PropertySpecificTest method testRtnWithConstraintsNoWatches.
@Test
public void testRtnWithConstraintsNoWatches() {
String rule1 = "A( a == 10 )";
KieBase kbase = getKnowledgeBase(rule1);
InternalWorkingMemory wm = ((InternalWorkingMemory) kbase.newKieSession());
ObjectTypeNode otn = getObjectTypeNode(kbase, "A");
assertNotNull(otn);
Class classType = ((ClassObjectType) otn.getObjectType()).getClassType();
List<String> sp = getSettableProperties(wm, otn);
AlphaNode alphaNode = (AlphaNode) otn.getObjectSinkPropagator().getSinks()[0];
assertEquals(calculatePositiveMask(classType, list("a"), sp), alphaNode.getDeclaredMask());
assertEquals(calculatePositiveMask(classType, list("a"), sp), alphaNode.getInferredMask());
LeftInputAdapterNode liaNode = (LeftInputAdapterNode) alphaNode.getObjectSinkPropagator().getSinks()[0];
RuleTerminalNode rtNode = (RuleTerminalNode) liaNode.getSinkPropagator().getSinks()[0];
// rtn declares nothing
assertEquals(EmptyBitMask.get(), rtNode.getDeclaredMask());
// rtn infers from alpha
assertEquals(calculatePositiveMask(classType, list("a"), sp), rtNode.getInferredMask());
}
use of org.drools.core.base.ClassObjectType in project drools by kiegroup.
the class PropertySpecificTest method testComplexBetaSharedAlphaWithWatchesRemoveR3.
@Test
public void testComplexBetaSharedAlphaWithWatchesRemoveR3() {
String rule1 = "$b : B( b == 15) @watch(i) A( a == 10, b == 15 ) @watch(c)";
String rule2 = "$b : B( b == 15) @watch(j) A( a == 10, i == 20 ) @watch(s)";
String rule3 = "$b : B( c == 15) @watch(k) A( a == 10, i == 20, b == 10 ) @watch(j)";
KieBase kbase = getKnowledgeBase(rule1, rule2, rule3);
InternalWorkingMemory wm = ((InternalWorkingMemory) kbase.newKieSession());
kbase.removeRule("org.drools.compiler.integrationtests", "r2");
ObjectTypeNode otn = getObjectTypeNode(kbase, "A");
assertNotNull(otn);
Class classType = ((ClassObjectType) otn.getObjectType()).getClassType();
List<String> sp = getSettableProperties(wm, otn);
AlphaNode alphaNode1 = (AlphaNode) otn.getObjectSinkPropagator().getSinks()[0];
assertEquals(calculatePositiveMask(classType, list("a"), sp), alphaNode1.getDeclaredMask());
assertEquals(calculatePositiveMask(classType, list("a", "b", "c", "i", "s"), sp), alphaNode1.getInferredMask());
// first share
AlphaNode alphaNode1_1 = (AlphaNode) alphaNode1.getObjectSinkPropagator().getSinks()[0];
assertEquals(calculatePositiveMask(classType, list("b"), sp), alphaNode1_1.getDeclaredMask());
assertEquals(calculatePositiveMask(classType, list("a", "b", "c"), sp), alphaNode1_1.getInferredMask());
// first split
BetaNode betaNode1 = (BetaNode) alphaNode1_1.getObjectSinkPropagator().getSinks()[0];
assertEquals(calculatePositiveMask(classType, list("c"), sp), betaNode1.getRightDeclaredMask());
assertEquals(calculatePositiveMask(classType, list("a", "b", "c"), sp), betaNode1.getRightInferredMask());
assertEquals(calculatePositiveMask(classType, list("i"), sp), betaNode1.getLeftDeclaredMask());
assertEquals(calculatePositiveMask(classType, list("b", "i"), sp), betaNode1.getLeftInferredMask());
// second split
AlphaNode alphaNode1_2 = (AlphaNode) alphaNode1.getObjectSinkPropagator().getSinks()[1];
assertEquals(calculatePositiveMask(classType, list("i"), sp), alphaNode1_2.getDeclaredMask());
assertEquals(calculatePositiveMask(classType, list("a", "i", "s"), sp), alphaNode1_2.getInferredMask());
BetaNode betaNode2 = (BetaNode) alphaNode1_2.getObjectSinkPropagator().getSinks()[0];
assertEquals(calculatePositiveMask(classType, list("s"), sp), betaNode2.getRightDeclaredMask());
assertEquals(calculatePositiveMask(classType, list("a", "i", "s"), sp), betaNode2.getRightInferredMask());
assertEquals(calculatePositiveMask(classType, list("j"), sp), betaNode2.getLeftDeclaredMask());
assertEquals(calculatePositiveMask(classType, list("b", "j"), sp), betaNode2.getLeftInferredMask());
}
use of org.drools.core.base.ClassObjectType 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.base.ClassObjectType in project drools by kiegroup.
the class KieBaseUpdater method clearInstancesOfModifiedClass.
protected void clearInstancesOfModifiedClass(Class<?> cls) {
// remove all ObjectTypeNodes for the modified classes
ClassObjectType objectType = new ClassObjectType(cls);
for (EntryPointNode epn : ctx.kBase.getRete().getEntryPointNodes().values()) {
epn.removeObjectType(objectType);
}
// remove all instance of the old class from the object stores
for (InternalWorkingMemory wm : ctx.kBase.getWorkingMemories()) {
for (EntryPoint ep : wm.getEntryPoints()) {
InternalWorkingMemoryEntryPoint wmEp = (InternalWorkingMemoryEntryPoint) wm.getWorkingMemoryEntryPoint(ep.getEntryPointId());
ClassAwareObjectStore store = ((ClassAwareObjectStore) wmEp.getObjectStore());
if (store.clearClassStore(cls)) {
log.warn("Class " + cls.getName() + " has been modified and therfore its old instances will no longer match");
}
}
}
}
Aggregations