use of org.drools.core.rule.Declaration in project drools by kiegroup.
the class ConstraintEvaluator method findPatternAndRequiredDeclaration.
private Declaration findPatternAndRequiredDeclaration() {
Declaration patternDeclaration = null;
List<Declaration> requiredDeclarationsList = new ArrayList<>();
for (int i = 0; i < declarations.length; i++) {
if (pattern.getDeclaration().getIdentifier().equals(declarations[i].getIdentifier())) {
patternDeclaration = declarations[i];
} else {
requiredDeclarationsList.add(declarations[i]);
}
}
this.requiredDeclarations = requiredDeclarationsList.toArray(new Declaration[requiredDeclarationsList.size()]);
return patternDeclaration;
}
use of org.drools.core.rule.Declaration 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.Declaration in project drools by kiegroup.
the class KiePackagesBuilder method addQueryPattern.
private void addQueryPattern(Query query, QueryImpl queryImpl, RuleContext ctx) {
Pattern pattern = new Pattern(ctx.getNextPatternIndex(), // offset is 0 by default
0, ClassObjectType.DroolsQuery_ObjectType, null);
InternalReadAccessor extractor = new LambdaReadAccessor(DroolsQuery.class, q -> ((DroolsQuery) q).getName());
QueryNameConstraint constraint = new QueryNameConstraint(extractor, query.getName());
pattern.addConstraint(constraint);
queryImpl.getLhs().addChild(pattern);
Variable<?>[] args = query.getArguments();
Declaration[] declarations = new Declaration[args.length];
for (int i = 0; i < args.length; i++) {
int index = i;
LambdaReadAccessor accessor = new LambdaReadAccessor(index, args[index].getType(), obj -> ((DroolsQuery) obj).getElements()[index]);
declarations[i] = new Declaration(args[i].getName(), accessor, pattern, true);
pattern.addDeclaration(declarations[i]);
ctx.addQueryDeclaration(args[i], declarations[i]);
}
queryImpl.setParameters(declarations);
}
use of org.drools.core.rule.Declaration in project drools by kiegroup.
the class KiePackagesBuilder method buildPattern.
private Pattern buildPattern(RuleContext ctx, GroupElement group, Condition condition) {
org.drools.model.Pattern<?> modelPattern = (org.drools.model.Pattern) condition;
Pattern pattern = addPatternForVariable(ctx, group, modelPattern.getPatternVariable());
for (Binding binding : modelPattern.getBindings()) {
Declaration declaration = new Declaration(binding.getBoundVariable().getName(), new LambdaReadAccessor(binding.getBoundVariable().getType(), binding.getBindingFunction()), pattern, true);
pattern.addDeclaration(declaration);
if (binding.getReactOn() != null) {
addFieldsToPatternWatchlist(pattern, binding.getReactOn());
}
ctx.addInnerDeclaration(binding.getBoundVariable(), declaration);
}
Declaration queryArgDecl = ctx.getQueryDeclaration(modelPattern.getPatternVariable());
if (queryArgDecl != null) {
pattern.addConstraint(new UnificationConstraint(queryArgDecl));
}
addConstraintsToPattern(ctx, pattern, modelPattern, modelPattern.getConstraint());
addFieldsToPatternWatchlist(pattern, modelPattern.getWatchedProps());
return pattern;
}
use of org.drools.core.rule.Declaration in project drools by kiegroup.
the class KiePackagesBuilder method buildAccumulate.
private Accumulate buildAccumulate(RuleContext ctx, AccumulatePattern accPattern, RuleConditionElement source, Pattern pattern, List<String> usedVariableName, Binding binding) {
AccumulateFunction[] accFunctions = accPattern.getAccumulateFunctions();
Accumulate accumulate;
if (accFunctions.length == 1) {
final Class<?> functionClass = accFunctions[0].getFunctionClass();
final Accumulator accumulator = createAccumulator(usedVariableName, binding, functionClass);
final Variable boundVar = accPattern.getBoundVariables()[0];
final Declaration declaration = new Declaration(boundVar.getName(), getReadAcessor(JAVA_CLASS_OBJECT_TYPE), pattern, true);
pattern.addDeclaration(declaration);
Declaration[] bindingDeclaration = binding != null ? new Declaration[0] : new Declaration[] { ctx.getPattern(accFunctions[0].getSource()).getDeclaration() };
accumulate = new SingleAccumulate(source, bindingDeclaration, accumulator);
} else {
InternalReadAccessor reader = new SelfReferenceClassFieldReader(Object[].class);
Accumulator[] accumulators = new Accumulator[accFunctions.length];
for (int i = 0; i < accFunctions.length; i++) {
final Class<?> functionClass = accFunctions[i].getFunctionClass();
final Accumulator accumulator = createAccumulator(usedVariableName, binding, functionClass);
Variable boundVar = accPattern.getBoundVariables()[i];
pattern.addDeclaration(new Declaration(boundVar.getName(), new ArrayElementReader(reader, i, boundVar.getType()), pattern, true));
accumulators[i] = accumulator;
}
accumulate = new MultiAccumulate(source, new Declaration[0], accumulators);
}
for (Variable boundVar : accPattern.getBoundVariables()) {
ctx.addAccumulateSource(boundVar, accumulate);
}
return accumulate;
}
Aggregations