use of org.drools.core.reteoo.RuleTerminalNode in project drools by kiegroup.
the class SegmentCreationTest method testMultiSharedPattern.
@Test
public void testMultiSharedPattern() throws Exception {
KieBase kbase = buildKnowledgeBase(" A() \n", " A() B() \n", " A() B() C() \n");
InternalWorkingMemory wm = ((InternalWorkingMemory) kbase.newKieSession());
ObjectTypeNode aotn = getObjectTypeNode(kbase, LinkingTest.A.class);
LeftInputAdapterNode liaNode = (LeftInputAdapterNode) aotn.getObjectSinkPropagator().getSinks()[0];
RuleTerminalNode rtn1 = (RuleTerminalNode) liaNode.getSinkPropagator().getSinks()[0];
JoinNode bNode = (JoinNode) liaNode.getSinkPropagator().getSinks()[1];
RuleTerminalNode rtn2 = (RuleTerminalNode) bNode.getSinkPropagator().getSinks()[0];
JoinNode cNode = (JoinNode) bNode.getSinkPropagator().getSinks()[1];
RuleTerminalNode rtn3 = (RuleTerminalNode) cNode.getSinkPropagator().getSinks()[0];
wm.insert(new LinkingTest.A());
wm.insert(new LinkingTest.B());
wm.insert(new LinkingTest.C());
wm.flushPropagations();
// LiaNode is in it's own segment
LiaNodeMemory liaMem = (LiaNodeMemory) wm.getNodeMemory(liaNode);
SegmentMemory smem = liaMem.getSegmentMemory();
assertEquals(liaNode, smem.getRootNode());
assertEquals(liaNode, smem.getTipNode());
SegmentMemory rtnSmem1 = smem.getFirst();
assertEquals(rtn1, rtnSmem1.getRootNode());
assertEquals(rtn1, rtnSmem1.getTipNode());
SegmentMemory bSmem = rtnSmem1.getNext();
assertEquals(bNode, bSmem.getRootNode());
assertEquals(bNode, bSmem.getTipNode());
// child segment is not yet initialised, so null
assertNull(bSmem.getFirst());
// there is no next
assertNull(bSmem.getNext());
// child segments should now be initialised
wm.fireAllRules();
wm.flushPropagations();
SegmentMemory rtnSmem2 = bSmem.getFirst();
assertEquals(rtn2, rtnSmem2.getRootNode());
assertEquals(rtn2, rtnSmem2.getTipNode());
SegmentMemory cSmem = rtnSmem2.getNext();
assertEquals(cNode, cSmem.getRootNode());
// note rtn3 is in the same segment as C
assertEquals(rtn3, cSmem.getTipNode());
}
use of org.drools.core.reteoo.RuleTerminalNode in project drools by kiegroup.
the class MarshallingTest method testSinglePattern.
@Test
public void testSinglePattern() throws Exception {
String rule = "package org.drools.compiler.test;\n";
rule += "import org.drools.compiler.Person\n";
rule += "global java.util.List list\n";
rule += "rule \"Rule 1\"\n";
rule += "when\n";
rule += " $p : Person( ) \n";
rule += "then\n";
rule += " list.add( $p );\n";
rule += "end";
KieBase kBase = loadKnowledgeBaseFromString(rule);
// Make sure the rete node map is created correctly
Map<Integer, BaseNode> nodes = RuleBaseNodes.getNodeMap((InternalKnowledgeBase) kBase);
assertEquals(3, nodes.size());
assertEquals("Person", ((ClassObjectType) ((ObjectTypeNode) nodes.get(3)).getObjectType()).getClassType().getSimpleName());
assertEquals("Rule 1", ((RuleTerminalNode) nodes.get(5)).getRule().getName());
KieSession session = kBase.newKieSession();
List list = new ArrayList();
session.setGlobal("list", list);
Person p = new Person("bobba fet", 32);
session.insert(p);
session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, true);
session.fireAllRules();
assertEquals(1, ((List) session.getGlobal("list")).size());
assertEquals(p, ((List) session.getGlobal("list")).get(0));
}
use of org.drools.core.reteoo.RuleTerminalNode in project drools by kiegroup.
the class MVELConsequenceBuilderTest method testSimpleExpression.
@Test
public void testSimpleExpression() throws Exception {
PackageDescr pkgDescr = new PackageDescr("pkg1");
KnowledgeBuilderImpl pkgBuilder = new KnowledgeBuilderImpl();
pkgBuilder.addPackage(pkgDescr);
InternalKnowledgePackage pkg = pkgBuilder.getPackageRegistry("pkg1").getPackage();
final RuleDescr ruleDescr = new RuleDescr("rule 1");
ruleDescr.setNamespace("pkg1");
ruleDescr.setConsequence("modify (cheese) {price = 5 };\nretract (cheese)");
DialectCompiletimeRegistry dialectRegistry = pkgBuilder.getPackageRegistry(pkg.getName()).getDialectCompiletimeRegistry();
MVELDialect mvelDialect = (MVELDialect) dialectRegistry.getDialect("mvel");
final RuleBuildContext context = new RuleBuildContext(pkgBuilder, ruleDescr, dialectRegistry, pkg, mvelDialect);
final InstrumentedDeclarationScopeResolver declarationResolver = new InstrumentedDeclarationScopeResolver();
final ObjectType cheeseObjeectType = new ClassObjectType(Cheese.class);
final Pattern pattern = new Pattern(0, cheeseObjeectType, "cheese");
final GroupElement subrule = new GroupElement(GroupElement.AND);
subrule.addChild(pattern);
final Map<String, Declaration> map = new HashMap<String, Declaration>();
map.put("cheese", pattern.getDeclaration());
declarationResolver.setDeclarations(map);
context.setDeclarationResolver(declarationResolver);
final MVELConsequenceBuilder builder = new MVELConsequenceBuilder();
builder.build(context, RuleImpl.DEFAULT_CONSEQUENCE_NAME);
InternalKnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
PropagationContextFactory pctxFactory = kBase.getConfiguration().getComponentFactory().getPropagationContextFactory();
kBase.addPackage(pkg);
StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
final Cheese cheddar = new Cheese("cheddar", 10);
final InternalFactHandle f0 = (InternalFactHandle) ksession.insert(cheddar);
final LeftTupleImpl tuple = new LeftTupleImpl(f0, null, true);
f0.removeLeftTuple(tuple);
final AgendaItem item = new AgendaItemImpl(0, tuple, 10, pctxFactory.createPropagationContext(1, PropagationContext.Type.DELETION, null, tuple != null ? (TerminalNode) tuple.getTupleSink() : null, null), new RuleTerminalNode(0, new CompositeObjectSinkAdapterTest.MockBetaNode(), context.getRule(), subrule, 0, new BuildContext(kBase)), null);
final DefaultKnowledgeHelper kbHelper = new DefaultKnowledgeHelper(ksession);
kbHelper.setActivation(item);
((MVELConsequence) context.getRule().getConsequence()).compile((MVELDialectRuntimeData) pkgBuilder.getPackageRegistry(pkg.getName()).getDialectRuntimeRegistry().getDialectData("mvel"));
context.getRule().getConsequence().evaluate(kbHelper, ksession);
assertEquals(5, cheddar.getPrice());
}
use of org.drools.core.reteoo.RuleTerminalNode in project drools by kiegroup.
the class MVELSalienceBuilderTest method testSimpleExpression.
@Test
public void testSimpleExpression() {
StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
final Person p = new Person("mark", "", 31);
final InternalFactHandle f0 = (InternalFactHandle) ksession.insert(p);
final LeftTupleImpl tuple = new LeftTupleImpl(f0, null, true);
RuleTerminalNode rtn = new RuleTerminalNode();
rtn.setSalienceDeclarations(context.getDeclarationResolver().getDeclarations(context.getRule()).values().toArray(new Declaration[1]));
AgendaItem item = new AgendaItemImpl(0, tuple, 0, null, rtn, null);
assertEquals(25, context.getRule().getSalience().getValue(new DefaultKnowledgeHelper(item, ksession), context.getRule(), ksession));
}
use of org.drools.core.reteoo.RuleTerminalNode in project drools by kiegroup.
the class AddRuleTest method testSplitTwoBeforeCreatedSegment.
@Test
public void testSplitTwoBeforeCreatedSegment() throws Exception {
InternalKnowledgeBase kbase1 = buildKnowledgeBase("r1", " A(1;) A(2;) B(1;) B(2;) C(1;) C(2;) D(1;) D(2;) E(1;) E(2;)\n");
kbase1.addPackages(buildKnowledgePackage("r2", " A(1;) A(2;) B(1;) B(2;) C(1;) C(2;) D(1;) D(2;) E(1;) E(2;)\n"));
kbase1.addPackages(buildKnowledgePackage("r3", " A(1;) A(2;) B(1;) B(2;) C(1;) C(2;) D(1;) D(2;)\n"));
kbase1.addPackages(buildKnowledgePackage("r4", " A(1;) A(2;) B(1;) B(2;) C(1;) C(2;) \n"));
InternalWorkingMemory wm = ((InternalWorkingMemory) kbase1.newKieSession());
List list = new ArrayList();
wm.setGlobal("list", list);
wm.insert(new E(1));
wm.insert(new E(2));
wm.flushPropagations();
RuleTerminalNode rtn1 = getRtn("org.kie.r1", kbase1);
RuleTerminalNode rtn2 = getRtn("org.kie.r2", kbase1);
RuleTerminalNode rtn3 = getRtn("org.kie.r3", kbase1);
RuleTerminalNode rtn4 = getRtn("org.kie.r4", kbase1);
PathMemory pm1 = (PathMemory) wm.getNodeMemory(rtn1);
SegmentMemory[] smems = pm1.getSegmentMemories();
assertEquals(4, smems.length);
assertNull(smems[0]);
assertNull(smems[1]);
assertNull(smems[3]);
SegmentMemory sm = smems[2];
assertEquals(2, sm.getPos());
assertEquals(4, sm.getSegmentPosMaskBit());
assertEquals(4, pm1.getLinkedSegmentMask());
kbase1.addPackages(buildKnowledgePackage("r5", " A(1;) A(2;) B(1;) B(2;) \n"));
smems = pm1.getSegmentMemories();
assertEquals(5, smems.length);
assertNull(smems[0]);
assertNull(smems[1]);
assertNull(smems[2]);
sm = smems[3];
assertEquals(3, sm.getPos());
assertEquals(8, sm.getSegmentPosMaskBit());
assertEquals(8, pm1.getLinkedSegmentMask());
RuleTerminalNode rtn5 = getRtn("org.kie.r5", kbase1);
PathMemory pm5 = (PathMemory) wm.getNodeMemory(rtn5);
smems = pm5.getSegmentMemories();
assertEquals(2, smems.length);
assertNull(smems[0]);
assertNull(smems[1]);
}
Aggregations