use of org.drools.core.base.extractors.ArrayElementReader in project drools by kiegroup.
the class PatternBuilderForQuery method build.
public void build(RuleBuildContext context, QueryDescr queryDescr) {
ObjectType queryObjectType = ClassObjectType.DroolsQuery_ObjectType;
final Pattern pattern = new Pattern(context.getNextPatternId(), // tupleIndex is 0 by default
0, // patternIndex is 0 by default
0, queryObjectType, null);
final InternalReadAccessor extractor = PatternBuilder.getFieldReadAccessor(context, queryDescr, pattern, "name", null, true);
final QueryNameConstraint constraint = new QueryNameConstraint(extractor, queryDescr.getName());
PatternBuilder.registerReadAccessor(context, queryObjectType, "name", constraint);
// adds appropriate constraint to the pattern
pattern.addConstraint(constraint);
ObjectType argsObjectType = ClassObjectType.DroolsQuery_ObjectType;
InternalReadAccessor arrayExtractor = PatternBuilder.getFieldReadAccessor(context, queryDescr, null, argsObjectType, "elements", null, true);
QueryImpl query = ((QueryImpl) context.getRule());
String[] params = getQueryParams(queryDescr);
String[] types = getQueryTypes(queryDescr, query);
Declaration[] declarations = new Declaration[params.length];
int i = 0;
try {
for (i = 0; i < params.length; i++) {
Declaration declr = pattern.addDeclaration(params[i]);
// this bit is different, notice its the ArrayElementReader that we wire up to, not the declaration.
ArrayElementReader reader = new ArrayElementReader(arrayExtractor, i, context.getDialect().getTypeResolver().resolveType(types[i]));
PatternBuilder.registerReadAccessor(context, argsObjectType, "elements", reader);
declr.setReadAccessor(reader);
declarations[i] = declr;
}
query.setParameters(declarations);
} catch (ClassNotFoundException e) {
context.addError(new DescrBuildError(context.getParentDescr(), queryDescr, e, "Unable to resolve type '" + types[i] + " for parameter" + params[i]));
}
context.setPrefixPattern(pattern);
postBuild(context, queryDescr, query, params, types, declarations);
}
use of org.drools.core.base.extractors.ArrayElementReader in project drools by kiegroup.
the class QueryElementBuilder method getVariableQueryArgument.
private QueryArgument getVariableQueryArgument(InternalReadAccessor arrayReader, Declaration[] params, int pos, Pattern pattern, String expression) {
// this bit is different, notice its the ArrayElementReader that we wire up to, not the declaration.
ArrayElementReader reader = new ArrayElementReader(arrayReader, pos, params[pos].getDeclarationClass());
// it's a variable that doesn't exist and doesn't contain a dot, so it's an output
pattern.addDeclaration(expression).setReadAccessor(reader);
return QueryArgument.VAR;
}
use of org.drools.core.base.extractors.ArrayElementReader in project drools by kiegroup.
the class KiePackagesBuilder method buildQueryPattern.
private RuleConditionElement buildQueryPattern(RuleContext ctx, QueryCallPattern queryPattern) {
Pattern pattern = new Pattern(ctx.getNextPatternIndex(), // tupleIndex is 0 by default
0, // patternIndex is 0 by default
0, ClassObjectType.ObjectArray_ObjectType, null);
QueryArgument[] arguments = new QueryArgument[queryPattern.getArguments().length];
List<Integer> varIndexList = new ArrayList<>();
List<Declaration> requiredDeclarations = new ArrayList<>();
for (int i = 0; i < queryPattern.getArguments().length; i++) {
Argument arg = queryPattern.getArguments()[i];
if (arg instanceof Variable) {
Variable var = ((Variable) arg);
Declaration decl = ctx.getDeclaration(var);
if (decl != null) {
requiredDeclarations.add(decl);
arguments[i] = new QueryArgument.Declr(decl);
} else {
ArrayElementReader reader = new ArrayElementReader(new SelfReferenceClassFieldReader(Object[].class), i, arg.getType());
Declaration varDeclaration = pattern.addDeclaration(var.getName());
varDeclaration.setReadAccessor(reader);
ctx.addDeclaration(var, varDeclaration);
arguments[i] = QueryArgument.VAR;
varIndexList.add(i);
}
} else if (arg instanceof Value) {
arguments[i] = new QueryArgument.Literal(((Value) arg).getValue());
} else {
throw new UnsupportedOperationException();
}
}
return new QueryElement(pattern, queryPattern.getQuery().getName(), arguments, varIndexList.stream().mapToInt(i -> i).toArray(), requiredDeclarations.toArray(new Declaration[requiredDeclarations.size()]), queryPattern.isOpen(), // TODO: query.isAbductive() );
false);
}
use of org.drools.core.base.extractors.ArrayElementReader in project drools by kiegroup.
the class KiePackagesBuilder method processFunctions.
private Variable processFunctions(RuleContext ctx, AccumulatePattern accPattern, RuleConditionElement source, Pattern pattern, Set<String> usedVariableName, Collection<Binding> bindings, boolean isGroupBy, AccumulateFunction accFunction, InternalReadAccessor selfReader, Accumulator[] accumulators, List<Declaration> requiredDeclarationList, int arrayIndexOffset, int i) {
Binding binding = findBindingForAccumulate(bindings, accFunction);
if (binding != null) {
for (Variable var : binding.getInputVariables()) {
usedVariableName.add(var.getName());
}
}
final BindingEvaluator bindingEvaluator = createBindingEvaluator(ctx, binding);
final Accumulator accumulator = createAccumulator(usedVariableName, bindingEvaluator, accFunction);
Variable boundVar = accPattern.getBoundVariables()[i];
Declaration declaration;
if (!isGroupBy && accumulators.length == 1) {
declaration = new Declaration(boundVar.getName(), new PatternExtractor(new ClassObjectType(boundVar.getType())), pattern, true);
} else {
// GroupBy or multi-accumulate always return an array
// If GroupBy has no bound function, it uses an anonymous one.
// The result is still in element 0 so must be offset using arrayIndexOffset
declaration = new Declaration(boundVar.getName(), new ArrayElementReader(selfReader, i + arrayIndexOffset, boundVar.getType()), pattern, true);
}
pattern.addDeclaration(declaration);
ctx.addDeclaration(boundVar, declaration);
accumulators[i] = accumulator;
Declaration[] requiredDeclarations = getRequiredDeclarationsForAccumulate(ctx, source, accFunction, binding, bindingEvaluator);
requiredDeclarationList.addAll(Arrays.asList(requiredDeclarations));
return boundVar;
}
Aggregations