use of org.drools.core.rule.EvalCondition in project drools by kiegroup.
the class KiePackagesBuilder method buildEval.
private EvalCondition buildEval(RuleContext ctx, EvalImpl eval) {
Declaration[] declarations = Stream.of(eval.getExpr().getVariables()).map(ctx::getDeclaration).toArray(Declaration[]::new);
EvalExpression evalExpr = new LambdaEvalExpression(declarations, eval.getExpr());
return new EvalCondition(evalExpr, declarations);
}
use of org.drools.core.rule.EvalCondition in project drools by kiegroup.
the class AbstractASMEvalBuilder method buildEval.
private RuleConditionElement buildEval(RuleBuildContext context, EvalDescr evalDescr, AnalysisResult analysis, Declaration[] declarations) {
String className = "eval" + context.getNextId();
evalDescr.setClassMethodName(className);
Arrays.sort(declarations, RuleTerminalNode.SortDeclarations.instance);
EvalCondition eval = EvalConditionFactory.Factory.get().createEvalCondition(declarations);
Map<String, Object> vars = createVariableContext(className, (String) evalDescr.getContent(), context, declarations, null, analysis.getBoundIdentifiers().getGlobals());
generateMethodTemplate("evalMethod", context, vars);
byte[] bytecode = createEvalBytecode(context, vars);
registerInvokerBytecode(context, vars, bytecode, eval);
return eval;
}
use of org.drools.core.rule.EvalCondition in project drools by kiegroup.
the class MVELEvalBuilder method build.
/**
* Builds and returns an Eval Conditional Element
*
* @param context The current build context
* @param descr The Eval Descriptor to build the eval conditional element from
*
* @return the Eval Conditional Element
*/
public RuleConditionElement build(final RuleBuildContext context, final BaseDescr descr, final Pattern prefixPattern) {
boolean typesafe = context.isTypesafe();
// it must be an EvalDescr
final EvalDescr evalDescr = (EvalDescr) descr;
try {
MVELDialect dialect = (MVELDialect) context.getDialect("mvel");
Map<String, Declaration> decls = context.getDeclarationResolver().getDeclarations(context.getRule());
AnalysisResult analysis = context.getDialect().analyzeExpression(context, evalDescr, evalDescr.getContent(), new BoundIdentifiers(DeclarationScopeResolver.getDeclarationClasses(decls), context));
final BoundIdentifiers usedIdentifiers = analysis.getBoundIdentifiers();
int i = usedIdentifiers.getDeclrClasses().keySet().size();
Declaration[] previousDeclarations = new Declaration[i];
i = 0;
for (String id : usedIdentifiers.getDeclrClasses().keySet()) {
previousDeclarations[i++] = decls.get(id);
}
Arrays.sort(previousDeclarations, SortDeclarations.instance);
MVELCompilationUnit unit = dialect.getMVELCompilationUnit((String) evalDescr.getContent(), analysis, previousDeclarations, null, null, context, "drools", KnowledgeHelper.class, false, MVELCompilationUnit.Scope.EXPRESSION);
final EvalCondition eval = EvalConditionFactory.Factory.get().createEvalCondition(previousDeclarations);
MVELEvalExpression expr = new MVELEvalExpression(unit, dialect.getId());
eval.setEvalExpression(KiePolicyHelper.isPolicyEnabled() ? new SafeEvalExpression(expr) : expr);
MVELDialectRuntimeData data = (MVELDialectRuntimeData) context.getPkg().getDialectRuntimeRegistry().getDialectData("mvel");
data.addCompileable(eval, expr);
expr.compile(data, context.getRule());
return eval;
} catch (final Exception e) {
copyErrorLocation(e, evalDescr);
context.addError(new DescrBuildError(context.getParentDescr(), evalDescr, e, "Unable to build expression for 'eval':" + e.getMessage() + " '" + evalDescr.getContent() + "'"));
return null;
} finally {
context.setTypesafe(typesafe);
}
}
use of org.drools.core.rule.EvalCondition in project drools by kiegroup.
the class JavaDialectBinaryEqualityTest method test1.
@Test
public void test1() {
KiePackage pkg1 = getKnowledgePackage1();
KiePackage pkg2 = getKnowledgePackage1();
KiePackage pkg3 = getKnowledgePackage2();
RuleImpl rule1 = ((InternalKnowledgePackage) pkg1).getRule("rule1");
RuleImpl rule2 = ((InternalKnowledgePackage) pkg2).getRule("rule1");
RuleImpl rule3 = ((InternalKnowledgePackage) pkg3).getRule("rule1");
// test return value
Pattern p1 = (Pattern) rule1.getLhs().getChildren().get(0);
Constraint rvc1 = p1.getConstraints().get(0);
Pattern p2 = (Pattern) rule2.getLhs().getChildren().get(0);
Constraint rvc2 = p2.getConstraints().get(0);
assertNotSame(rvc1, rvc2);
assertEquals(rvc1, rvc2);
Pattern p3 = (Pattern) rule3.getLhs().getChildren().get(0);
Constraint rvc3 = p3.getConstraints().get(0);
assertNotSame(rvc1, rvc3);
assertThat(rvc1, not(equalTo(rvc3)));
// test inline eval
PredicateConstraint pc1 = getPredicateConstraint(p1);
PredicateExpression pe1 = (PredicateExpression) pc1.getPredicateExpression();
PredicateConstraint pc2 = getPredicateConstraint(p2);
PredicateExpression pe2 = (PredicateExpression) pc2.getPredicateExpression();
assertNotSame(pe1, pe2);
assertEquals(pe1, pe2);
PredicateConstraint pc3 = getPredicateConstraint(p3);
PredicateExpression pe3 = (PredicateExpression) pc3.getPredicateExpression();
assertNotSame(pe1, pe3);
assertThat(pe1, not(equalTo(pe3)));
// test eval
EvalCondition ec1 = (EvalCondition) rule1.getLhs().getChildren().get(1);
EvalExpression ee1 = (EvalExpression) ec1.getEvalExpression();
EvalCondition ec2 = (EvalCondition) rule2.getLhs().getChildren().get(1);
EvalExpression ee2 = (EvalExpression) ec2.getEvalExpression();
assertNotSame(ee1, ee2);
assertEquals(ee1, ee2);
EvalCondition ec3 = (EvalCondition) rule3.getLhs().getChildren().get(1);
EvalExpression ee3 = (EvalExpression) ec3.getEvalExpression();
assertNotSame(ee1, ee3);
assertThat(ee1, not(equalTo(ee3)));
// test consequence
assertNotSame(rule1.getConsequence(), rule2.getConsequence());
assertEquals(rule1.getConsequence(), rule2.getConsequence());
assertNotSame(rule1.getConsequence(), rule3.getConsequence());
assertThat(rule1.getConsequence(), not(equalTo(rule3.getConsequence())));
// check LHS equals
assertNotSame(rule1.getLhs(), rule2.getLhs());
assertEquals(rule1.getLhs(), rule2.getLhs());
assertNotSame(rule1.getLhs(), rule3.getLhs());
assertThat(rule1.getLhs(), not(equalTo(rule3.getLhs())));
}
use of org.drools.core.rule.EvalCondition in project drools by kiegroup.
the class PhreakEvalNode method doLeftInserts.
public void doLeftInserts(EvalConditionNode evalNode, EvalMemory em, LeftTupleSink sink, ReteEvaluator reteEvaluator, TupleSets<LeftTuple> srcLeftTuples, TupleSets<LeftTuple> trgLeftTuples) {
EvalCondition condition = evalNode.getCondition();
for (LeftTuple leftTuple = srcLeftTuples.getInsertFirst(); leftTuple != null; ) {
LeftTuple next = leftTuple.getStagedNext();
final boolean allowed = condition.isAllowed(leftTuple, reteEvaluator, em.context);
if (allowed) {
boolean useLeftMemory = RuleNetworkEvaluator.useLeftMemory(evalNode, leftTuple);
trgLeftTuples.addInsert(sink.createLeftTuple(leftTuple, sink, leftTuple.getPropagationContext(), useLeftMemory));
}
leftTuple.clearStaged();
leftTuple = next;
}
}
Aggregations