use of org.drools.model.Variable in project drools by kiegroup.
the class ViewFlowBuilder method addInputFromVariableSource.
private static void addInputFromVariableSource(Map<Variable<?>, InputViewItemImpl<?>> inputs, Variable<?> patterVariable) {
if (patterVariable instanceof Declaration) {
Declaration declaration = ((Declaration) patterVariable);
if (declaration.getSource() instanceof From) {
Variable var = ((From) declaration.getSource()).getVariable();
addInputFromVariableSource(inputs, var);
if (var.isFact()) {
inputs.putIfAbsent(var, (InputViewItemImpl) input(var));
}
}
}
}
use of org.drools.model.Variable in project drools by kiegroup.
the class ViewFlowBuilder method viewItems2Condition.
private static CompositePatterns viewItems2Condition(BuildContext ctx, Condition.Type type, boolean topLevel) {
List<Condition> conditions = new ArrayList<>();
Map<Variable<?>, Condition> conditionMap = new HashMap<>();
Map<String, Consequence> consequences = topLevel ? new LinkedHashMap<>() : null;
Iterator<RuleItem> ruleItemIterator = ctx.ruleItems.iterator();
while (ruleItemIterator.hasNext()) {
Map<Variable<?>, InputViewItemImpl<?>> scopedInputs = type.createsScope() ? new LinkedHashMap<>(ctx.inputs) : ctx.inputs;
RuleItem ruleItem = ruleItemIterator.next();
if (ruleItem instanceof FixedValueItem) {
conditions.add(new EvalImpl(((FixedValueItem) ruleItem).isValue()));
continue;
}
if (ruleItem instanceof Consequence) {
if (!topLevel) {
throw new IllegalStateException("A consequence can be only a top level item");
}
Consequence consequence = (Consequence) ruleItem;
String name = ruleItemIterator.hasNext() ? generateName("consequence") : RuleImpl.DEFAULT_CONSEQUENCE_NAME;
consequences.put(name, consequence);
conditions.add(new NamedConsequenceImpl(name, consequence.isBreaking()));
continue;
}
if (ruleItem instanceof ConditionalConsequence) {
if (!topLevel) {
throw new IllegalStateException("A consequence can be only a top level item");
}
conditions.add(createConditionalNamedConsequence(consequences, (ConditionalConsequence) ruleItem));
continue;
}
ViewItem viewItem = (ViewItem) ruleItem;
if (viewItem instanceof CombinedExprViewItem) {
CombinedExprViewItem combined = (CombinedExprViewItem) viewItem;
conditions.add(viewItems2Condition(new BuildContext(ctx, combined.getExpressions(), scopedInputs), combined.getType(), false));
continue;
}
if (viewItem instanceof QueryCallViewItem) {
QueryCallViewItem query = ((QueryCallViewItem) viewItem);
for (Argument arg : query.getArguments()) {
if (arg instanceof Variable) {
ctx.usedVars.add(((Variable) arg));
}
}
conditions.add(new QueryCallPattern(query));
continue;
}
if (viewItem instanceof Binding) {
Binding bindViewItem = (Binding) viewItem;
PatternImpl pattern = (PatternImpl) conditionMap.get(bindViewItem.getInputVariable());
if (pattern == null) {
// This should probably be the bindViewItem.getBoundVariable() instead of the input
// as the input variables can be many
pattern = new PatternImpl(bindViewItem.getInputVariable());
pattern.addWatchedProps(bindViewItem.getWatchedProps());
ctx.usedVars.add(bindViewItem.getInputVariable());
conditions.add(pattern);
conditionMap.put(bindViewItem.getInputVariable(), pattern);
}
pattern.addBinding(bindViewItem);
ctx.usedVars.add(viewItem.getFirstVariable());
ctx.addBinding(bindViewItem);
scopedInputs.putIfAbsent(viewItem.getFirstVariable(), (InputViewItemImpl) input(viewItem.getFirstVariable()));
continue;
}
Variable<?> patterVariable = findPatterVariable(viewItem, scopedInputs.keySet());
if (viewItem instanceof InputViewItemImpl) {
scopedInputs.put(patterVariable, (InputViewItemImpl) viewItem);
PatternImpl condition = new PatternImpl(patterVariable);
condition.addWatchedProps(((InputViewItemImpl) viewItem).getWatchedProps());
conditions.add(condition);
conditionMap.put(patterVariable, condition);
ctx.usedVars.add(patterVariable);
continue;
}
if (viewItem instanceof ExistentialExprViewItem) {
ExistentialExprViewItem existential = ((ExistentialExprViewItem) viewItem);
if (patterVariable != null && !existential.isQueryExpression()) {
registerInputsFromViewItem(existential.getExpression(), conditionMap, scopedInputs, patterVariable);
}
Condition condition = new PatternImpl(patterVariable, SingleConstraint.TRUE, ctx.bindings.get(patterVariable));
conditions.add(new ExistentialPatternImpl(viewItem2Condition(existential.getExpression(), condition, new BuildContext(ctx, new LinkedHashMap<>())), existential.getType()));
continue;
}
if (ruleItem instanceof ExprViewItem && ctx.boundVars.contains(patterVariable)) {
conditions.add(new EvalImpl(createConstraint((ExprViewItem) ruleItem)));
continue;
}
ctx.usedVars.add(patterVariable);
Condition condition;
if (type == Type.AND) {
condition = conditionMap.get(patterVariable);
if (condition == null) {
condition = new PatternImpl(patterVariable, SingleConstraint.TRUE, ctx.bindings.get(patterVariable));
conditions.add(condition);
if (!(viewItem instanceof AccumulateExprViewItem)) {
conditionMap.put(patterVariable, condition);
}
scopedInputs.putIfAbsent(patterVariable, (InputViewItemImpl) input(patterVariable));
}
} else {
condition = new PatternImpl(patterVariable);
conditions.add(condition);
}
addInputFromVariableSource(scopedInputs, patterVariable);
registerInputsFromViewItem(viewItem, conditionMap, scopedInputs, null);
Condition modifiedPattern = viewItem2Condition(viewItem, condition, new BuildContext(ctx, scopedInputs));
conditions.set(conditions.indexOf(condition), modifiedPattern);
if (type == Type.AND && !(viewItem instanceof AccumulateExprViewItem)) {
conditionMap.put(patterVariable, modifiedPattern);
}
}
return new CompositePatterns(type, conditions, ctx.usedVars, consequences);
}
use of org.drools.model.Variable in project drools by kiegroup.
the class BruteForceEngine method match.
private boolean match(Constraint constraint, TupleHandle tuple) {
switch(constraint.getType()) {
case SINGLE:
SingleConstraint singleCon = (SingleConstraint) constraint;
Variable[] vars = singleCon.getVariables();
switch(vars.length) {
case 0:
try {
return singleCon.getPredicate().test();
} catch (Exception e) {
throw new RuntimeException(e);
}
case 1:
Object obj = tuple.get(vars[0]);
try {
return singleCon.getPredicate().test(obj);
} catch (Exception e) {
throw new RuntimeException(e);
}
case 2:
Object obj1 = tuple.get(vars[0]);
Object obj2 = tuple.get(vars[1]);
try {
return singleCon.getPredicate().test(obj1, obj2);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
case AND:
AndConstraints andCon = (AndConstraints) constraint;
return andCon.getChildren().stream().allMatch(con -> match(con, tuple));
case OR:
OrConstraints orCon = (OrConstraints) constraint;
return orCon.getChildren().stream().anyMatch(con -> match(con, tuple));
}
return false;
}
use of org.drools.model.Variable 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.model.Variable 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