use of org.drools.core.common.PropagationContextFactory in project drools by kiegroup.
the class AlphaNodeTest method testLiteralConstraintAssertObjectWithoutMemory.
@Test
public void testLiteralConstraintAssertObjectWithoutMemory() throws Exception {
InternalKnowledgeBase kBase = (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase();
BuildContext buildContext = new BuildContext(kBase);
buildContext.setRule(new RuleImpl("test"));
StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
final RuleImpl rule = new RuleImpl("test-rule");
PropagationContextFactory pctxFactory = kBase.getConfiguration().getComponentFactory().getPropagationContextFactory();
final PropagationContext context = pctxFactory.createPropagationContext(0, PropagationContext.Type.INSERTION, null, null, null);
final MockObjectSource source = new MockObjectSource(buildContext.getNextId());
final ClassFieldReader extractor = store.getReader(Cheese.class, "type");
final FieldValue field = FieldFactory.getInstance().getFieldValue("cheddar");
final MvelConstraint constraint = new MvelConstraintTestUtil("type == \"cheddar\"", field, extractor);
// With Memory
final AlphaNode alphaNode = new AlphaNode(buildContext.getNextId(), constraint, source, // no memory
buildContext);
final MockObjectSink sink = new MockObjectSink();
alphaNode.addObjectSink(sink);
final Cheese cheddar = new Cheese("cheddar", 5);
final DefaultFactHandle f0 = (DefaultFactHandle) ksession.insert(cheddar);
// check sink is empty
assertLength(0, sink.getAsserted());
// object should assert as it passes text
alphaNode.assertObject(f0, context, ksession);
assertEquals(1, sink.getAsserted().size());
Object[] list = (Object[]) sink.getAsserted().get(0);
assertSame(cheddar, ksession.getObject((DefaultFactHandle) list[0]));
final Cheese stilton = new Cheese("stilton", 6);
final DefaultFactHandle f1 = new DefaultFactHandle(1, stilton);
// object should NOT assert as it does not pass test
alphaNode.assertObject(f1, context, ksession);
assertLength(1, sink.getAsserted());
list = (Object[]) sink.getAsserted().get(0);
assertSame(cheddar, ksession.getObject((DefaultFactHandle) list[0]));
}
use of org.drools.core.common.PropagationContextFactory in project drools by kiegroup.
the class RuleUnlinkingTest method setUp.
public void setUp(int type) {
kBase = KnowledgeBaseFactory.newKnowledgeBase();
buildContext = new BuildContext(kBase, Collections.emptyList());
PropagationContextFactory pctxFactory = new PhreakPropagationContextFactory();
context = pctxFactory.createPropagationContext(0, PropagationContext.Type.INSERTION, null, null, null);
ObjectTypeNode otn = new ObjectTypeNode(4, null, new ClassObjectType(String.class), buildContext);
liaNode = new LeftInputAdapterNode(5, otn, buildContext);
n1 = (BetaNode) createNetworkNode(10, type, liaNode, null);
n2 = (BetaNode) createNetworkNode(11, type, n1, null);
n3 = (BetaNode) createNetworkNode(12, type, n2, null);
rule1 = new RuleImpl("rule1");
rule1.setActivationListener("agenda");
rtn1 = (RuleTerminalNode) createNetworkNode(18, RULE_TERMINAL_NODE, n3, rule1);
n4 = (BetaNode) createNetworkNode(13, type, n3, null);
n5 = (BetaNode) createNetworkNode(14, type, n4, null);
rule2 = new RuleImpl("rule2");
rule2.setActivationListener("agenda");
rtn2 = (RuleTerminalNode) createNetworkNode(19, RULE_TERMINAL_NODE, n5, rule2);
n6 = (BetaNode) createNetworkNode(15, type, n5, null);
n7 = (BetaNode) createNetworkNode(16, type, n6, null);
n8 = (BetaNode) createNetworkNode(17, type, n7, null);
rule3 = new RuleImpl("rule3");
rule3.setActivationListener("agenda");
rtn3 = (RuleTerminalNode) createNetworkNode(20, RULE_TERMINAL_NODE, n8, rule3);
// n1 -> n2 -> n3 -> r1
// \
// n4 -> n5 -> r2
// \
// n6 -> n7 -> n8 -> r3
liaNode.addAssociation(rule1);
liaNode.addAssociation(rule2);
liaNode.addAssociation(rule3);
n1.addAssociation(rule1);
n1.addAssociation(rule2);
n1.addAssociation(rule3);
n2.addAssociation(rule1);
n2.addAssociation(rule2);
n2.addAssociation(rule3);
n3.addAssociation(rule1);
n3.addAssociation(rule2);
n3.addAssociation(rule3);
n4.addAssociation(rule2);
n4.addAssociation(rule3);
n5.addAssociation(rule2);
n5.addAssociation(rule3);
n6.addAssociation(rule3);
n7.addAssociation(rule3);
n8.addAssociation(rule3);
}
use of org.drools.core.common.PropagationContextFactory in project drools by kiegroup.
the class NodeSegmentUnlinkingTest method setUp.
public void setUp(int... type) {
kBase = KnowledgeBaseFactory.newKnowledgeBase();
buildContext = new BuildContext(kBase, Collections.emptyList());
PropagationContextFactory pctxFactory = new PhreakPropagationContextFactory();
context = pctxFactory.createPropagationContext(0, PropagationContext.Type.INSERTION, null, null, null);
MockTupleSource mockTupleSource = new MockTupleSource(9, buildContext);
rule1 = new RuleImpl("rule1");
rule2 = new RuleImpl("rule2");
rule3 = new RuleImpl("rule3");
ObjectTypeNode otn = new ObjectTypeNode(3, null, new ClassObjectType(String.class), buildContext);
liaNode = new LeftInputAdapterNode(4, otn, buildContext);
// 3, 4, 5, 6 are in same shared segment
n1 = createBetaNode(10, type[0], liaNode);
n2 = createBetaNode(11, type[1], n1);
RuleTerminalNode rtn1 = new RuleTerminalNode(18, n2, rule1, rule1.getLhs(), 0, buildContext);
rtn1.attach(buildContext);
n3 = createBetaNode(12, type[2], n1);
n4 = createBetaNode(13, type[3], n3);
n5 = createBetaNode(14, type[4], n4);
n6 = createBetaNode(15, type[5], n5);
RuleTerminalNode rtn2 = new RuleTerminalNode(19, n6, rule2, rule2.getLhs(), 0, buildContext);
rtn2.attach(buildContext);
n7 = createBetaNode(16, type[6], n6);
n8 = createBetaNode(17, type[7], n7);
RuleTerminalNode rtn3 = new RuleTerminalNode(20, n8, rule3, rule3.getLhs(), 0, buildContext);
rtn3.attach(buildContext);
// n1 -> n2 -> r1
// \
// n3 -> n4 -> n5 -> n6 -> r2
// \
// n7 -> n8 -> r3
n1.addAssociation(rule1);
n1.addAssociation(rule2);
n1.addAssociation(rule3);
n2.addAssociation(rule1);
n2.addAssociation(rule2);
n2.addAssociation(rule3);
n3.addAssociation(rule2);
n3.addAssociation(rule3);
n4.addAssociation(rule2);
n4.addAssociation(rule3);
n5.addAssociation(rule2);
n5.addAssociation(rule3);
n6.addAssociation(rule2);
n6.addAssociation(rule3);
n7.addAssociation(rule3);
n8.addAssociation(rule3);
}
use of org.drools.core.common.PropagationContextFactory in project drools by kiegroup.
the class RuleUnlinkingWithSegmentMemoryTest method setUp.
public void setUp(int type) {
KieBaseConfiguration kconf = RuleBaseFactory.newKnowledgeBaseConfiguration();
kBase = KnowledgeBaseFactory.newKnowledgeBase(RuleBaseFactory.newRuleBase(kconf));
buildContext = new BuildContext(kBase, Collections.emptyList());
PropagationContextFactory pctxFactory = new PhreakPropagationContextFactory();
context = pctxFactory.createPropagationContext(0, PropagationContext.Type.INSERTION, null, null, null);
ObjectTypeNode otn = new ObjectTypeNode(4, null, new ClassObjectType(String.class), buildContext);
lian = new LeftInputAdapterNode(5, otn, buildContext);
n1 = (BetaNode) createNetworkNode(10, type, lian, null);
n2 = (BetaNode) createNetworkNode(11, type, n1, null);
n3 = (BetaNode) createNetworkNode(12, type, n2, null);
rule1 = new RuleImpl("rule1");
rule1.setActivationListener("agenda");
rtn1 = (RuleTerminalNode) createNetworkNode(18, RULE_TERMINAL_NODE, n3, rule1);
n4 = (BetaNode) createNetworkNode(13, type, n3, null);
n5 = (BetaNode) createNetworkNode(14, type, n4, null);
rule2 = new RuleImpl("rule2");
rule2.setActivationListener("agenda");
rtn2 = (RuleTerminalNode) createNetworkNode(19, RULE_TERMINAL_NODE, n5, rule2);
n6 = (BetaNode) createNetworkNode(15, type, n5, null);
n7 = (BetaNode) createNetworkNode(16, type, n6, null);
n8 = (BetaNode) createNetworkNode(17, type, n7, null);
rule3 = new RuleImpl("rule3");
rule3.setActivationListener("agenda");
rtn3 = (RuleTerminalNode) createNetworkNode(20, RULE_TERMINAL_NODE, n8, rule3);
lian.addAssociation(rule1);
lian.addAssociation(rule2);
lian.addAssociation(rule3);
n1.addAssociation(rule1);
n1.addAssociation(rule2);
n1.addAssociation(rule3);
n2.addAssociation(rule1);
n2.addAssociation(rule2);
n2.addAssociation(rule3);
n3.addAssociation(rule1);
n3.addAssociation(rule2);
n3.addAssociation(rule3);
n4.addAssociation(rule2);
n4.addAssociation(rule3);
n5.addAssociation(rule2);
n5.addAssociation(rule3);
n6.addAssociation(rule3);
n7.addAssociation(rule3);
n8.addAssociation(rule3);
}
use of org.drools.core.common.PropagationContextFactory 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 = RuntimeComponentFactory.get().getPropagationContextFactory();
kBase.addPackage(pkg);
StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
BuildContext buildContext = new BuildContext(kBase, Collections.emptyList());
MockTupleSource source = new MockTupleSource(1, buildContext);
source.setObjectCount(1);
RuleTerminalNode rtn = new RuleTerminalNode(0, source, context.getRule(), subrule, 0, buildContext);
final Cheese cheddar = new Cheese("cheddar", 10);
final InternalFactHandle f0 = (InternalFactHandle) ksession.insert(cheddar);
final LeftTupleImpl tuple = new LeftTupleImpl(f0, rtn, true);
f0.removeLeftTuple(tuple);
final AgendaItem item = new AgendaItemImpl(0, tuple, 10, pctxFactory.createPropagationContext(1, PropagationContext.Type.DELETION, null, null, null), rtn, 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());
}
Aggregations