Search in sources :

Example 96 with InternalWorkingMemory

use of org.drools.core.common.InternalWorkingMemory in project drools by kiegroup.

the class RemoveRuleTest method testPopulatedMultipleSharesRemoveFirst.

@Test
public void testPopulatedMultipleSharesRemoveFirst() throws Exception {
    InternalKnowledgeBase kbase1 = buildKnowledgeBase("r1", "   A(1;)  A(2;) B(1;) B(2;) C(1;) X() E()\n");
    InternalWorkingMemory wm = ((InternalWorkingMemory) kbase1.newKieSession());
    List list = new ArrayList();
    wm.setGlobal("list", list);
    InternalFactHandle fh1 = (InternalFactHandle) wm.insert(new A(1));
    InternalFactHandle fh2 = (InternalFactHandle) wm.insert(new A(2));
    InternalFactHandle fh3 = (InternalFactHandle) wm.insert(new A(2));
    InternalFactHandle fh4 = (InternalFactHandle) wm.insert(new A(3));
    InternalFactHandle fh5 = (InternalFactHandle) wm.insert(new B(1));
    InternalFactHandle fh6 = (InternalFactHandle) wm.insert(new B(2));
    InternalFactHandle fh7 = (InternalFactHandle) wm.insert(new C(1));
    InternalFactHandle fh8 = (InternalFactHandle) wm.insert(new C(2));
    InternalFactHandle fh9 = (InternalFactHandle) wm.insert(new X(1));
    InternalFactHandle fh10 = (InternalFactHandle) wm.insert(new E(1));
    wm.fireAllRules();
    assertEquals(2, list.size());
    kbase1.addPackages(buildKnowledgePackage("r2", "   A(1;)  A(2;) B(1;) B(2;) C(2;) X() E()\n"));
    kbase1.addPackages(buildKnowledgePackage("r3", "   A(1;)  A(3;) B(1;) B(2;) C(2;) X() E()\n"));
    wm.fireAllRules();
    assertEquals(5, list.size());
    kbase1.removeRule("org.kie", "r1");
    list.clear();
    wm.update(fh1, fh1.getObject());
    wm.update(fh2, fh2.getObject());
    wm.update(fh3, fh3.getObject());
    wm.update(fh4, fh4.getObject());
    wm.update(fh5, fh5.getObject());
    wm.update(fh6, fh6.getObject());
    wm.update(fh7, fh7.getObject());
    wm.update(fh8, fh8.getObject());
    wm.update(fh9, fh9.getObject());
    wm.update(fh10, fh10.getObject());
    wm.fireAllRules();
    assertEquals(3, list.size());
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) InternalFactHandle(org.drools.core.common.InternalFactHandle) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) Test(org.junit.Test)

Example 97 with InternalWorkingMemory

use of org.drools.core.common.InternalWorkingMemory in project drools by kiegroup.

the class BetaMemoryLeakOnDeleteTest method testBetaMemoryLeakOnFactDelete.

@Test
public void testBetaMemoryLeakOnFactDelete() {
    final String drl = "rule R1 when\n" + "    $a : Integer(this == 1)\n" + "    $b : String()\n" + "    $c : Integer(this == 2)\n" + "then \n" + "end\n" + "rule R2 when\n" + "    $a : Integer(this == 1)\n" + "    $b : String()\n" + "then \n" + "end\n";
    final KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL).build().newKieSession();
    final FactHandle fh1 = ksession.insert(1);
    final FactHandle fh2 = ksession.insert("test");
    ksession.fireAllRules();
    ksession.delete(fh1);
    ksession.delete(fh2);
    ksession.fireAllRules();
    final NodeMemories nodeMemories = ((InternalWorkingMemory) ksession).getNodeMemories();
    for (int i = 0; i < nodeMemories.length(); i++) {
        final Memory memory = nodeMemories.peekNodeMemory(i);
        if (memory != null && memory.getSegmentMemory() != null) {
            final LeftTuple deleteFirst = memory.getSegmentMemory().getStagedLeftTuples().getDeleteFirst();
            assertThat(deleteFirst).isNull();
        }
    }
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) FactHandle(org.kie.api.runtime.rule.FactHandle) Memory(org.drools.core.common.Memory) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) NodeMemories(org.drools.core.common.NodeMemories) LeftTuple(org.drools.core.reteoo.LeftTuple) Test(org.junit.Test)

Example 98 with InternalWorkingMemory

use of org.drools.core.common.InternalWorkingMemory in project drools by kiegroup.

the class TraitTest method testTMSConsistencyWithNonTraitableBeans.

@Test
public void testTMSConsistencyWithNonTraitableBeans() {
    String s1 = "package org.drools.test;\n" + "import org.drools.traits.compiler.Person; \n" + "import org.drools.core.factmodel.traits.Traitable; \n" + "" + "declare Person @Traitable end \n" + "" + "rule \"Init\"\n" + "when\n" + "then\n" + "  insertLogical( new Person( \"x\", 18 ) );\n" + "end\n" + "\n" + "declare trait Student\n" + "  age  : int\n" + "  name : String\n" + "end\n" + "\n" + "rule \"Trait\"\n" + "when\n" + "    $p : Person( )\n" + "then\n" + "    don( $p, Student.class, true );\n" + "end\n";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(new ByteArrayResource(s1.getBytes()), ResourceType.DRL);
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    KieSession ksession = kbase.newKieSession();
    TraitFactoryImpl.setMode(mode, ksession.getKieBase());
    ksession.fireAllRules();
    FactHandle personHandle = ksession.getFactHandles(new ClassObjectFilter(Person.class)).iterator().next();
    InternalFactHandle h = ((InternalFactHandle) personHandle);
    ObjectTypeConfigurationRegistry reg = h.getEntryPoint((InternalWorkingMemory) ksession).getObjectTypeConfigurationRegistry();
    ObjectTypeConf conf = reg.getOrCreateObjectTypeConf(h.getEntryPointId(), ((InternalFactHandle) personHandle).getObject());
    assertTrue(conf.isTMSEnabled());
    ksession.dispose();
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) ObjectTypeConf(org.drools.core.reteoo.ObjectTypeConf) ClassObjectFilter(org.kie.api.runtime.ClassObjectFilter) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) InternalFactHandle(org.drools.core.common.InternalFactHandle) Person(org.drools.traits.compiler.Person) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) ObjectTypeConfigurationRegistry(org.drools.core.common.ObjectTypeConfigurationRegistry) CommonTraitTest(org.drools.traits.compiler.CommonTraitTest) Test(org.junit.Test)

Example 99 with InternalWorkingMemory

use of org.drools.core.common.InternalWorkingMemory in project drools by kiegroup.

the class AddRemoveRule method removeRule.

/**
 * This method is called before the rule nodes are removed from the network.
 * For remove tuples are processed before the segments and pmems have been adjusted
 */
public static void removeRule(TerminalNode tn, Collection<InternalWorkingMemory> wms, RuleBase kBase) {
    if (log.isTraceEnabled()) {
        log.trace("Removing Rule {}", tn.getRule().getName());
    }
    boolean hasProtos = kBase.hasSegmentPrototypes();
    boolean hasWms = !wms.isEmpty();
    if (!hasProtos && !hasWms) {
        return;
    }
    RuleImpl rule = tn.getRule();
    LeftTupleNode firstSplit = getNetworkSplitPoint(tn);
    PathEndNodes pathEndNodes = getPathEndNodes(kBase, firstSplit, tn, rule, hasProtos, hasWms);
    for (InternalWorkingMemory wm : wms) {
        wm.flushPropagations();
        PathEndNodeMemories tnms = getPathEndMemories(wm, pathEndNodes);
        if (!tnms.subjectPmems.isEmpty()) {
            if (NodeTypeEnums.LeftInputAdapterNode == firstSplit.getType() && firstSplit.getAssociationsSize() == 1) {
                if (tnms.subjectPmem != null) {
                    flushStagedTuples(firstSplit, tnms.subjectPmem, wm);
                }
                processLeftTuples(firstSplit, wm, false, tn.getRule());
                removeNewPaths(wm, tnms.subjectPmems);
            } else {
                flushStagedTuples(tn, tnms.subjectPmem, pathEndNodes, wm);
                processLeftTuples(firstSplit, wm, false, tn.getRule());
                removeNewPaths(wm, tnms.subjectPmems);
                Map<PathMemory, SegmentMemory[]> prevSmemsLookup = reInitPathMemories(tnms.otherPmems, tn);
                // must collect all visited SegmentMemories, for link notification
                Set<SegmentMemory> smemsToNotify = handleExistingPaths(tn, prevSmemsLookup, tnms.otherPmems, wm, ExistingPathStrategy.REMOVE_STRATEGY);
                notifySegments(smemsToNotify, wm);
            }
        }
        if (tnms.subjectPmem != null && tnms.subjectPmem.isInitialized() && tnms.subjectPmem.getRuleAgendaItem().isQueued()) {
            // SubjectPmem can be null, if it was never initialized
            tnms.subjectPmem.getRuleAgendaItem().dequeue();
        }
    }
    if (!hasWms) {
        for (PathEndNode node : pathEndNodes.otherEndNodes) {
            node.resetPathMemSpec(null);
        }
    }
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) SegmentMemory(org.drools.core.reteoo.SegmentMemory) LeftTupleNode(org.drools.core.reteoo.LeftTupleNode) PathEndNode(org.drools.core.reteoo.PathEndNode) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) PathMemory(org.drools.core.reteoo.PathMemory)

Example 100 with InternalWorkingMemory

use of org.drools.core.common.InternalWorkingMemory in project drools by kiegroup.

the class AddRemoveRule method removeNewPaths.

private static void removeNewPaths(InternalWorkingMemory wm, List<PathMemory> pmems) {
    Set<Integer> visitedNodes = new HashSet<>();
    for (PathMemory pmem : pmems) {
        LeftTupleSink tipNode = pmem.getPathEndNode();
        LeftTupleNode child = tipNode;
        LeftTupleNode parent = tipNode.getLeftTupleSource();
        while (true) {
            if (child.getAssociationsSize() == 1 && NodeTypeEnums.isBetaNode(child)) {
                // If this is a beta node, it'll delete all the right input data
                deleteRightInputData((LeftTupleSink) child, wm);
            }
            if (parent != null && parent.getAssociationsSize() != 1 && child.getAssociationsSize() == 1) {
                // all right input data must be propagated
                if (!visitedNodes.contains(child.getId())) {
                    Memory mem = wm.getNodeMemories().peekNodeMemory(parent);
                    if (mem != null && mem.getSegmentMemory() != null) {
                        SegmentMemory sm = mem.getSegmentMemory();
                        if (sm.getFirst() != null) {
                            SegmentMemory childSm = wm.getNodeMemories().peekNodeMemory(child).getSegmentMemory();
                            sm.remove(childSm);
                        }
                    }
                }
            } else {
                Memory mem = wm.getNodeMemories().peekNodeMemory(child);
                // The root of each segment
                if (mem != null) {
                    SegmentMemory sm = mem.getSegmentMemory();
                    if (sm != null && sm.getPathMemories().contains(pmem)) {
                        mem.getSegmentMemory().removePathMemory(pmem);
                    }
                }
            }
            if (parent == null) {
                break;
            }
            visitedNodes.add(child.getId());
            child = parent;
            parent = parent.getLeftTupleSource();
        }
    }
}
Also used : SegmentMemory(org.drools.core.reteoo.SegmentMemory) Memory(org.drools.core.common.Memory) PathMemory(org.drools.core.reteoo.PathMemory) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) SegmentNodeMemory(org.drools.core.reteoo.SegmentNodeMemory) ObjectTypeNodeMemory(org.drools.core.reteoo.ObjectTypeNode.ObjectTypeNodeMemory) RiaNodeMemory(org.drools.core.reteoo.RightInputAdapterNode.RiaNodeMemory) AccumulateMemory(org.drools.core.reteoo.AccumulateNode.AccumulateMemory) TupleMemory(org.drools.core.reteoo.TupleMemory) BetaMemory(org.drools.core.reteoo.BetaMemory) SegmentMemory(org.drools.core.reteoo.SegmentMemory) FromMemory(org.drools.core.reteoo.FromNode.FromMemory) LeftTupleSink(org.drools.core.reteoo.LeftTupleSink) LeftTupleNode(org.drools.core.reteoo.LeftTupleNode) HashSet(java.util.HashSet) PathMemory(org.drools.core.reteoo.PathMemory)

Aggregations

InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)285 Test (org.junit.Test)217 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)140 KieBase (org.kie.api.KieBase)129 ArrayList (java.util.ArrayList)96 LeftInputAdapterNode (org.drools.core.reteoo.LeftInputAdapterNode)91 List (java.util.List)76 SegmentMemory (org.drools.core.reteoo.SegmentMemory)74 KieSession (org.kie.api.runtime.KieSession)71 RuleTerminalNode (org.drools.core.reteoo.RuleTerminalNode)69 JoinNode (org.drools.core.reteoo.JoinNode)62 BetaMemory (org.drools.core.reteoo.BetaMemory)57 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)48 PathMemory (org.drools.core.reteoo.PathMemory)48 LiaNodeMemory (org.drools.core.reteoo.LeftInputAdapterNode.LiaNodeMemory)45 BetaNode (org.drools.core.reteoo.BetaNode)44 FactHandle (org.kie.api.runtime.rule.FactHandle)44 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)35 AlphaNode (org.drools.core.reteoo.AlphaNode)35 ClassObjectType (org.drools.core.base.ClassObjectType)30