use of org.drools.core.rule.EvalCondition in project drools by kiegroup.
the class PhreakEvalNode method doLeftUpdates.
public void doLeftUpdates(EvalConditionNode evalNode, EvalMemory em, LeftTupleSink sink, InternalWorkingMemory wm, TupleSets<LeftTuple> srcLeftTuples, TupleSets<LeftTuple> trgLeftTuples, TupleSets<LeftTuple> stagedLeftTuples) {
EvalCondition condition = evalNode.getCondition();
for (LeftTuple leftTuple = srcLeftTuples.getUpdateFirst(); leftTuple != null; ) {
LeftTuple next = leftTuple.getStagedNext();
boolean wasPropagated = leftTuple.getFirstChild() != null && leftTuple.getContextObject() != EVAL_LEFT_TUPLE_DELETED;
boolean allowed = condition.isAllowed(leftTuple, wm, em.context);
if (allowed) {
leftTuple.setContextObject(null);
if (wasPropagated) {
// update
LeftTuple childLeftTuple = leftTuple.getFirstChild();
childLeftTuple.setPropagationContext(leftTuple.getPropagationContext());
normalizeStagedTuples(stagedLeftTuples, childLeftTuple);
trgLeftTuples.addUpdate(childLeftTuple);
} else {
// assert
trgLeftTuples.addInsert(sink.createLeftTuple(leftTuple, sink, leftTuple.getPropagationContext(), true));
}
} else {
if (wasPropagated) {
// retract
leftTuple.setContextObject(EVAL_LEFT_TUPLE_DELETED);
LeftTuple childLeftTuple = leftTuple.getFirstChild();
childLeftTuple.setPropagationContext(leftTuple.getPropagationContext());
RuleNetworkEvaluator.unlinkAndDeleteChildLeftTuple(childLeftTuple, trgLeftTuples, stagedLeftTuples);
}
// else do nothing
}
leftTuple.clearStaged();
leftTuple = next;
}
}
use of org.drools.core.rule.EvalCondition in project drools by kiegroup.
the class MVELEvalBuilderTest method testSimpleExpression.
@Test
public void testSimpleExpression() {
InternalKnowledgePackage pkg = new KnowledgePackageImpl("pkg1");
final RuleDescr ruleDescr = new RuleDescr("rule 1");
KnowledgeBuilderImpl pkgBuilder = new KnowledgeBuilderImpl(pkg);
final KnowledgeBuilderConfigurationImpl conf = pkgBuilder.getBuilderConfiguration();
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 InternalReadAccessor extractor = store.getReader(Cheese.class, "price");
final Pattern pattern = new Pattern(0, new ClassObjectType(int.class));
final Declaration declaration = new Declaration("a", extractor, pattern);
final Map map = new HashMap();
map.put("a", declaration);
declarationResolver.setDeclarations(map);
context.setDeclarationResolver(declarationResolver);
final EvalDescr evalDescr = new EvalDescr();
evalDescr.setContent("a == 10");
final MVELEvalBuilder builder = new MVELEvalBuilder();
final EvalCondition eval = (EvalCondition) builder.build(context, evalDescr);
((MVELEvalExpression) eval.getEvalExpression()).compile((MVELDialectRuntimeData) pkgBuilder.getPackageRegistry(pkg.getName()).getDialectRuntimeRegistry().getDialectData("mvel"));
InternalKnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
MockLeftTupleSink sink = new MockLeftTupleSink();
final Cheese cheddar = new Cheese("cheddar", 10);
final InternalFactHandle f0 = (InternalFactHandle) ksession.insert(cheddar);
final LeftTupleImpl tuple = new LeftTupleImpl(f0, sink, true);
f0.removeLeftTuple(tuple);
Object evalContext = eval.createContext();
assertTrue(eval.isAllowed(tuple, ksession, evalContext));
cheddar.setPrice(9);
ksession.update(f0, cheddar);
assertFalse(eval.isAllowed(tuple, ksession, evalContext));
}
use of org.drools.core.rule.EvalCondition in project drools by kiegroup.
the class KnowledgeBuilderTest method testEvalMethodCompare.
@Test
public void testEvalMethodCompare() {
final KnowledgeBuilderImpl builder1 = new KnowledgeBuilderImpl();
final PackageDescr packageDescr1 = new PackageDescr("package1");
createEvalRule(packageDescr1, "1==1");
builder1.addPackage(packageDescr1);
final EvalCondition eval1 = (EvalCondition) ((RuleImpl) builder1.getPackage("package1").getRules().iterator().next()).getLhs().getChildren().get(0);
final KnowledgeBuilderImpl builder2 = new KnowledgeBuilderImpl();
final PackageDescr packageDescr2 = new PackageDescr("package2");
createEvalRule(packageDescr2, "1==1");
builder2.addPackage(packageDescr2);
final EvalCondition eval2 = (EvalCondition) ((RuleImpl) builder2.getPackage("package2").getRules().iterator().next()).getLhs().getChildren().get(0);
final KnowledgeBuilderImpl builder3 = new KnowledgeBuilderImpl();
final PackageDescr packageDescr3 = new PackageDescr("package3");
createEvalRule(packageDescr3, "1==3");
builder3.addPackage(packageDescr3);
final EvalCondition eval3 = (EvalCondition) ((RuleImpl) builder3.getPackage("package3").getRules().iterator().next()).getLhs().getChildren().get(0);
assertEquals(eval1, eval2);
assertFalse(eval1.equals(eval3));
assertFalse(eval2.equals(eval3));
}
use of org.drools.core.rule.EvalCondition in project drools by kiegroup.
the class EvalBuilder method build.
/**
* @inheritDoc
*/
public void build(final BuildContext context, final BuildUtils utils, final RuleConditionElement rce) {
final EvalCondition eval = (EvalCondition) rce;
context.pushRuleComponent(rce);
context.setTupleSource(utils.attachNode(context, CoreComponentFactory.get().getNodeFactoryService().buildEvalNode(context.getNextNodeId(), context.getTupleSource(), eval, context)));
context.popRuleComponent();
}
use of org.drools.core.rule.EvalCondition in project drools by kiegroup.
the class KiePackagesBuilder method buildConditionalConsequence.
private ConditionalBranch buildConditionalConsequence(RuleContext ctx, ConditionalNamedConsequenceImpl consequence) {
// This makes the assumption the variable is for a pattern (not field) binding, and this binding must already exist.
// It's used for the implicit bindings
// The main issue with all of this, is it doesn't allow access to other previous variables. And it should work with
// with pattern bindings and field bindings. What if getVariables() returns an array.length > 1?
EvalCondition evalCondition;
if (consequence.getExpr() != null) {
Pattern pattern = ctx.getPattern(consequence.getExpr().getVariables()[0]);
if (pattern.getDeclaration() != null) {
// pattern has a root binding, so use that.
EvalExpression eval = new LambdaEvalExpression(pattern, consequence.getExpr());
evalCondition = new EvalCondition(eval, new Declaration[] { pattern.getDeclaration() });
} else {
// Pattern does not have root binding, so use field bindings.
Declaration[] declarations = pattern.getDeclarations().values().toArray(new Declaration[pattern.getDeclarations().size()]);
EvalExpression eval = new LambdaEvalExpression(declarations, consequence.getExpr());
evalCondition = new EvalCondition(eval, declarations);
}
} else {
evalCondition = new EvalCondition(LambdaEvalExpression.EMPTY, null);
}
return new ConditionalBranch(evalCondition, new NamedConsequence(consequence.getThenConsequence().getName(), consequence.getThenConsequence().isBreaking()), consequence.getElseBranch() != null ? buildConditionalConsequence(ctx, consequence.getElseBranch()) : null);
}
Aggregations