Search in sources :

Example 6 with BaseNode

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

the class ReteooBuilder method removeRules.

public synchronized void removeRules(Collection<RuleImpl> rulesToBeRemoved) {
    // reset working memories for potential propagation
    InternalWorkingMemory[] workingMemories = this.kBase.getWorkingMemories();
    for (RuleImpl rule : rulesToBeRemoved) {
        if (rule.hasChildren() && !rulesToBeRemoved.containsAll(rule.getChildren())) {
            throw new RuntimeException("Cannot remove parent rule " + rule + " without having removed all its chikdren");
        }
        final RuleRemovalContext context = new RuleRemovalContext(rule);
        context.setKnowledgeBase(kBase);
        BaseNode[] rulesTerminalNodes = rules.remove(rule.getFullyQualifiedName());
        if (rulesTerminalNodes == null) {
            // there couldn't be any rule to be removed if it comes from a broken drl
            continue;
        }
        for (BaseNode node : rulesTerminalNodes) {
            removeTerminalNode(context, (TerminalNode) node, workingMemories);
        }
        if (rule.isQuery()) {
            this.queries.remove(rule.getName());
        }
        if (rule.getParent() != null && !rulesToBeRemoved.contains(rule.getParent())) {
            rule.getParent().removeChild(rule);
        }
    }
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) BaseNode(org.drools.core.common.BaseNode) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl)

Example 7 with BaseNode

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

the class ReteooBuilder method removeTerminalNode.

public void removeTerminalNode(RuleRemovalContext context, TerminalNode tn, InternalWorkingMemory[] workingMemories) {
    AddRemoveRule.removeRule(tn, workingMemories, kBase);
    BaseNode node = (BaseNode) tn;
    removeNodeAssociation(node, context.getRule());
    resetMasks(removeNodes((AbstractTerminalNode) tn, workingMemories, context));
}
Also used : BaseNode(org.drools.core.common.BaseNode)

Example 8 with BaseNode

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

the class ReteDumper method visitNode.

private static void visitNode(BaseNode node, String ident, Set<BaseNode> visitedNodes, BiConsumer<String, BaseNode> consumer) {
    consumer.accept(ident, node);
    if (!visitedNodes.add(node)) {
        return;
    }
    Sink[] sinks = getSinks(node);
    if (sinks != null) {
        for (Sink sink : sinks) {
            if (sink instanceof BaseNode) {
                visitNode((BaseNode) sink, ident + "    ", visitedNodes, consumer);
            }
        }
    }
}
Also used : Sink(org.drools.core.reteoo.Sink) BaseNode(org.drools.core.common.BaseNode)

Example 9 with BaseNode

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

the class MarshallingTest method testDynamicEmptyRule.

@Test
public void testDynamicEmptyRule() throws Exception {
    String rule1 = "package org.drools.compiler.test;\n";
    rule1 += "global java.util.List list\n";
    rule1 += "rule \"Rule 1\"\n";
    rule1 += "when\n";
    rule1 += "then\n";
    rule1 += "    list.add( \"fired1\" );\n";
    rule1 += "end";
    String rule2 = "package org.drools.compiler.test;\n";
    rule2 += "global java.util.List list\n";
    rule2 += "rule \"Rule 2\"\n";
    rule2 += "when\n";
    rule2 += "then\n";
    rule2 += "    list.add( \"fired2\" );\n";
    rule2 += "end";
    InternalKnowledgeBase kBase = (InternalKnowledgeBase) loadKnowledgeBaseFromString(rule1);
    // Make sure the rete node map is created correctly
    Map<Integer, BaseNode> nodes = RuleBaseNodes.getNodeMap((InternalKnowledgeBase) kBase);
    // Make sure the rete node map is created correctly
    assertEquals(2, nodes.size());
    assertEquals("InitialFactImpl", ((ClassObjectType) ((ObjectTypeNode) nodes.get(2)).getObjectType()).getClassType().getSimpleName());
    assertEquals("Rule 1", ((RuleTerminalNode) nodes.get(4)).getRule().getName());
    KieSession session = kBase.newKieSession();
    List list = new ArrayList();
    session.setGlobal("list", list);
    KieSession session1 = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, false);
    session1.fireAllRules();
    assertEquals(1, ((List) session1.getGlobal("list")).size());
    KieSession session2 = SerializationHelper.getSerialisedStatefulKnowledgeSession(session1, kBase, false);
    session.dispose();
    session1.dispose();
    Collection<KiePackage> kpkgs = loadKnowledgePackagesFromString(rule2);
    kBase.addPackages(kpkgs);
    session2.fireAllRules();
    System.out.println(session2.getGlobal("list"));
    assertEquals(2, ((List) session2.getGlobal("list")).size());
    assertEquals("fired1", ((List) session2.getGlobal("list")).get(0));
    assertEquals("fired2", ((List) session2.getGlobal("list")).get(1));
}
Also used : ClassObjectType(org.drools.core.base.ClassObjectType) KiePackage(org.kie.api.definition.KiePackage) BaseNode(org.drools.core.common.BaseNode) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) IteratorToList(org.drools.compiler.integrationtests.IteratorToList) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) Test(org.junit.Test)

Example 10 with BaseNode

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

the class NodesPartitioningTest method traverse.

private void traverse(BaseNode node) {
    checkNode(node);
    Sink[] sinks = node.getSinks();
    if (sinks != null) {
        for (Sink sink : sinks) {
            if (sink instanceof BaseNode) {
                traverse((BaseNode) sink);
            }
        }
    }
}
Also used : ObjectSink(org.drools.core.reteoo.ObjectSink) Sink(org.drools.core.reteoo.Sink) BaseNode(org.drools.core.common.BaseNode)

Aggregations

BaseNode (org.drools.core.common.BaseNode)14 ArrayList (java.util.ArrayList)5 List (java.util.List)4 IteratorToList (org.drools.compiler.integrationtests.IteratorToList)4 ClassObjectType (org.drools.core.base.ClassObjectType)4 RuleTerminalNode (org.drools.core.reteoo.RuleTerminalNode)4 Test (org.junit.Test)4 KieSession (org.kie.api.runtime.KieSession)4 KieBase (org.kie.api.KieBase)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Person (org.drools.compiler.Person)2 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)2 Sink (org.drools.core.reteoo.Sink)2 TreeMap (java.util.TreeMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Cheese (org.drools.compiler.Cheese)1 ActivationListenerFactory (org.drools.core.ActivationListenerFactory)1 QueryResultsImpl (org.drools.core.QueryResultsImpl)1 DroolsQuery (org.drools.core.base.DroolsQuery)1