Search in sources :

Example 81 with ClassObjectType

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());
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) BetaNode(org.drools.core.reteoo.BetaNode) ClassObjectType(org.drools.core.base.ClassObjectType) KieBase(org.kie.api.KieBase) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) AlphaNode(org.drools.core.reteoo.AlphaNode) Test(org.junit.Test)

Example 82 with ClassObjectType

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());
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) ClassObjectType(org.drools.core.base.ClassObjectType) KieBase(org.kie.api.KieBase) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) AlphaNode(org.drools.core.reteoo.AlphaNode) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) Test(org.junit.Test)

Example 83 with ClassObjectType

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());
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) BetaNode(org.drools.core.reteoo.BetaNode) ClassObjectType(org.drools.core.base.ClassObjectType) KieBase(org.kie.api.KieBase) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) AlphaNode(org.drools.core.reteoo.AlphaNode) Test(org.junit.Test)

Example 84 with ClassObjectType

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());
}
Also used : ClassObjectType(org.drools.core.base.ClassObjectType) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) ObjectTypeNodeMemory(org.drools.core.reteoo.ObjectTypeNode.ObjectTypeNodeMemory) EntryPointNode(org.drools.core.reteoo.EntryPointNode) ClassObjectType(org.drools.core.base.ClassObjectType) ObjectType(org.drools.core.spi.ObjectType) KieBase(org.kie.api.KieBase) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) Worker(org.drools.compiler.Worker) KieSession(org.kie.api.runtime.KieSession) DomainObject(org.drools.compiler.DomainObject) InsertedObject(org.drools.compiler.InsertedObject) InternalFactHandle(org.drools.core.common.InternalFactHandle) Test(org.junit.Test)

Example 85 with ClassObjectType

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");
            }
        }
    }
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) EntryPointNode(org.drools.core.reteoo.EntryPointNode) ClassObjectType(org.drools.core.base.ClassObjectType) InternalWorkingMemoryEntryPoint(org.drools.core.common.InternalWorkingMemoryEntryPoint) EntryPoint(org.kie.api.runtime.rule.EntryPoint) ClassAwareObjectStore(org.drools.core.common.ClassAwareObjectStore) InternalWorkingMemoryEntryPoint(org.drools.core.common.InternalWorkingMemoryEntryPoint)

Aggregations

ClassObjectType (org.drools.core.base.ClassObjectType)123 Test (org.junit.Test)76 Pattern (org.drools.core.rule.Pattern)37 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)34 KieBase (org.kie.api.KieBase)32 Declaration (org.drools.core.rule.Declaration)28 ObjectType (org.drools.core.spi.ObjectType)28 InternalReadAccessor (org.drools.core.spi.InternalReadAccessor)24 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)23 BetaNode (org.drools.core.reteoo.BetaNode)17 AlphaNode (org.drools.core.reteoo.AlphaNode)15 ArrayList (java.util.ArrayList)14 List (java.util.List)13 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)13 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)13 StatefulKnowledgeSessionImpl (org.drools.core.impl.StatefulKnowledgeSessionImpl)13 RuleTerminalNode (org.drools.core.reteoo.RuleTerminalNode)13 Cheese (org.drools.core.test.model.Cheese)13 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)12 InternalFactHandle (org.drools.core.common.InternalFactHandle)12