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);
}
}
}
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));
}
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);
}
}
}
}
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));
}
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);
}
}
}
}
Aggregations