use of org.drools.core.rule.Declaration in project drools by kiegroup.
the class PatternBuilder method buildOperators.
protected static Map<String, EvaluatorWrapper> buildOperators(RuleBuildContext context, Pattern pattern, BaseDescr predicateDescr, Map<String, OperatorDescr> aliases) {
if (aliases.isEmpty()) {
return Collections.emptyMap();
}
Map<String, EvaluatorWrapper> operators = new HashMap<String, EvaluatorWrapper>();
for (Map.Entry<String, OperatorDescr> entry : aliases.entrySet()) {
OperatorDescr op = entry.getValue();
Declaration leftDecl = createDeclarationForOperator(context, pattern, op.getLeftString());
Declaration rightDecl = createDeclarationForOperator(context, pattern, op.getRightString());
Target left = leftDecl != null && leftDecl.isPatternDeclaration() ? Target.HANDLE : Target.FACT;
Target right = rightDecl != null && rightDecl.isPatternDeclaration() ? Target.HANDLE : Target.FACT;
op.setLeftIsHandle(left == Target.HANDLE);
op.setRightIsHandle(right == Target.HANDLE);
Evaluator evaluator = getConstraintBuilder(context).getEvaluator(context, predicateDescr, ValueType.OBJECT_TYPE, op.getOperator(), // the rewrite takes care of negation
false, op.getParametersText(), left, right);
EvaluatorWrapper wrapper = getConstraintBuilder(context).wrapEvaluator(evaluator, leftDecl, rightDecl);
operators.put(entry.getKey(), wrapper);
}
return operators;
}
use of org.drools.core.rule.Declaration in project drools by kiegroup.
the class PatternBuilder method createDeclarationObject.
protected static Declaration createDeclarationObject(final RuleBuildContext context, final String identifier, final String expr, final Pattern pattern) {
final BindingDescr implicitBinding = new BindingDescr(identifier, expr);
final Declaration declaration = new Declaration(identifier, null, pattern, true);
InternalReadAccessor extractor = getFieldReadAccessor(context, implicitBinding, pattern, implicitBinding.getExpression(), declaration, false);
if (extractor == null) {
return null;
}
declaration.setReadAccessor(extractor);
return declaration;
}
use of org.drools.core.rule.Declaration in project drools by kiegroup.
the class PatternBuilder method build.
protected List<Constraint> build(RuleBuildContext context, PatternDescr patternDescr, Pattern pattern, ConstraintConnectiveDescr descr, MVELDumper.MVELDumperContext mvelCtx) {
List<Constraint> constraints = new ArrayList<Constraint>();
List<BaseDescr> initialDescrs = new ArrayList<BaseDescr>(descr.getDescrs());
for (BaseDescr d : initialDescrs) {
boolean isXPath = isXPathDescr(d);
if (isXPath && pattern.hasXPath()) {
registerDescrBuildError(context, patternDescr, "More than a single oopath constraint is not allowed in the same pattern");
return constraints;
}
Constraint constraint = isXPath ? buildXPathDescr(context, patternDescr, pattern, d, mvelCtx) : buildCcdDescr(context, patternDescr, pattern, d, descr, mvelCtx);
if (constraint != null) {
Declaration declCorrXpath = getDeclarationCorrespondingToXpath(pattern, isXPath, constraint);
if (declCorrXpath == null) {
constraints.add(constraint);
} else {
// A constraint is using a declration bound to an xpath in the same pattern
// Move the constraint inside the last chunk of the xpath defining this declaration, rewriting it as 'this'
Pattern modifiedPattern = pattern.clone();
modifiedPattern.setObjectType(new ClassObjectType(declCorrXpath.getDeclarationClass()));
constraint = buildCcdDescr(context, patternDescr, modifiedPattern, d.replaceVariable(declCorrXpath.getBindingName(), "this"), descr, mvelCtx);
if (constraint != null) {
pattern.getXpathConstraint().getChunks().getLast().addConstraint(constraint);
}
}
}
}
if (descr.getDescrs().size() > initialDescrs.size()) {
// The initial build process may have generated other constraint descrs.
// This happens when null-safe references or inline-casts are used
// These additional constraints must be built, and added as
List<BaseDescr> additionalDescrs = new ArrayList<BaseDescr>(descr.getDescrs());
additionalDescrs.removeAll(initialDescrs);
if (!additionalDescrs.isEmpty()) {
List<Constraint> additionalConstraints = new ArrayList<Constraint>();
for (BaseDescr d : additionalDescrs) {
Constraint constraint = buildCcdDescr(context, patternDescr, pattern, d, descr, mvelCtx);
if (constraint != null) {
additionalConstraints.add(constraint);
}
}
constraints.addAll(0, additionalConstraints);
}
}
return constraints;
}
use of org.drools.core.rule.Declaration in project drools by kiegroup.
the class PatternBuilder method buildConstraintForPattern.
protected Constraint buildConstraintForPattern(final RuleBuildContext context, final Pattern pattern, final RelationalExprDescr relDescr, String expr, String value1, String value2, boolean isConstant, Map<String, OperatorDescr> aliases) {
InternalReadAccessor extractor = getFieldReadAccessor(context, relDescr, pattern, value1, null, true);
if (extractor == null) {
return null;
}
int dotPos = value1.indexOf('.');
if (dotPos > 0) {
String part0 = value1.substring(0, dotPos).trim();
if ("this".equals(part0.trim())) {
value1 = value1.substring(dotPos + 1);
} else if (pattern.getDeclaration() != null && part0.equals(pattern.getDeclaration().getIdentifier())) {
value1 = value1.substring(dotPos + 1);
expr = expr.substring(dotPos + 1);
}
}
ValueType vtype = extractor.getValueType();
String operator = relDescr.getOperator().trim();
LiteralRestrictionDescr restrictionDescr = buildLiteralRestrictionDescr(context, relDescr, value2, operator, isConstant);
if (restrictionDescr != null) {
FieldValue field = getFieldValue(context, vtype, restrictionDescr.getText().trim());
if (field != null) {
Constraint constraint = getConstraintBuilder(context).buildLiteralConstraint(context, pattern, vtype, field, expr, value1, operator, value2, extractor, restrictionDescr, aliases);
if (constraint != null) {
return constraint;
}
}
}
value2 = context.getDeclarationResolver().normalizeValueForUnit(value2);
Declaration declr = null;
if (value2.indexOf('(') < 0 && value2.indexOf('.') < 0 && value2.indexOf('[') < 0) {
declr = context.getDeclarationResolver().getDeclaration(value2);
if (declr == null) {
// trying to create implicit declaration
final Pattern thisPattern = (Pattern) context.getDeclarationResolver().peekBuildStack();
declr = createDeclarationObject(context, value2, thisPattern);
}
}
Declaration[] declarations = null;
if (declr == null) {
String[] parts = value2.split("\\.");
if (parts.length == 2) {
if ("this".equals(parts[0].trim())) {
declr = createDeclarationObject(context, parts[1].trim(), (Pattern) context.getDeclarationResolver().peekBuildStack());
value2 = parts[1].trim();
} else {
declr = context.getDeclarationResolver().getDeclaration(parts[0].trim());
// if a declaration exists, then it may be a variable direct property access
if (declr != null) {
if (declr.isPatternDeclaration()) {
// TODO: no need to extract inner declaration when using mvel constraint
declarations = new Declaration[] { declr };
declr = createDeclarationObject(context, parts[1].trim(), declr.getPattern());
value2 = parts[1].trim();
} else {
// we will later fallback to regular predicates, so don't raise error
return null;
}
}
}
}
}
if (declarations == null) {
if (declr != null) {
declarations = new Declaration[] { declr };
} else {
declarations = getDeclarationsForReturnValue(context, relDescr, operator, value2);
if (declarations == null) {
return null;
}
}
}
return getConstraintBuilder(context).buildVariableConstraint(context, pattern, expr, declarations, value1, relDescr.getOperatorDescr(), value2, extractor, declr, relDescr, aliases);
}
use of org.drools.core.rule.Declaration in project drools by kiegroup.
the class MVELAccumulateBuilder method build.
@SuppressWarnings("unchecked")
public RuleConditionElement build(final RuleBuildContext context, final BaseDescr descr, final Pattern prefixPattern) {
boolean typesafe = context.isTypesafe();
try {
final AccumulateDescr accumDescr = (AccumulateDescr) descr;
if (!accumDescr.hasValidInput()) {
return null;
}
final RuleConditionBuilder builder = (RuleConditionBuilder) context.getDialect().getBuilder(accumDescr.getInput().getClass());
// create source CE
final RuleConditionElement source = builder.build(context, accumDescr.getInput());
if (source == null) {
return null;
}
MVELDialect dialect = (MVELDialect) context.getDialect();
Map<String, Declaration> decls = context.getDeclarationResolver().getDeclarations(context.getRule());
Map<String, Declaration> sourceOuterDeclr = source.getOuterDeclarations();
Map<String, Class<?>> declarationClasses = DeclarationScopeResolver.getDeclarationClasses(decls);
declarationClasses.putAll(DeclarationScopeResolver.getDeclarationClasses(sourceOuterDeclr));
BoundIdentifiers boundIds = new BoundIdentifiers(declarationClasses, context);
final boolean readLocalsFromTuple = PackageBuilderUtil.isReadLocalsFromTuple(context, accumDescr, source);
Accumulator[] accumulators;
if (accumDescr.isExternalFunction()) {
// uses accumulate functions
accumulators = buildExternalFunctions(context, accumDescr, dialect, decls, sourceOuterDeclr, boundIds, readLocalsFromTuple);
} else {
// it is a custom accumulate
accumulators = buildCustomAccumulate(context, accumDescr, dialect, decls, sourceOuterDeclr, boundIds, readLocalsFromTuple);
}
List<Declaration> requiredDeclarations = new ArrayList<Declaration>();
for (Accumulator acc : accumulators) {
MvelAccumulator mvelAcc = (MvelAccumulator) acc;
Collections.addAll(requiredDeclarations, mvelAcc.getRequiredDeclarations());
}
MVELDialectRuntimeData data = (MVELDialectRuntimeData) context.getPkg().getDialectRuntimeRegistry().getDialectData("mvel");
Accumulate accumulate;
if (accumDescr.isMultiFunction()) {
accumulate = new MultiAccumulate(source, requiredDeclarations.toArray(new Declaration[requiredDeclarations.size()]), accumulators);
int index = 0;
for (Accumulator accumulator : accumulators) {
data.addCompileable(((MultiAccumulate) accumulate).new Wirer(index++), (MVELCompileable) accumulator);
((MVELCompileable) accumulator).compile(data, context.getRule());
}
} else {
accumulate = new SingleAccumulate(source, requiredDeclarations.toArray(new Declaration[requiredDeclarations.size()]), accumulators[0]);
data.addCompileable(((SingleAccumulate) accumulate).new Wirer(), (MVELCompileable) accumulators[0]);
((MVELCompileable) accumulators[0]).compile(data, context.getRule());
}
return accumulate;
} catch (Exception e) {
DialectUtil.copyErrorLocation(e, descr);
context.addError(new DescrBuildError(context.getParentDescr(), descr, e, "Unable to build expression for 'accumulate' : " + e.getMessage()));
return null;
} finally {
context.setTypesafe(typesafe);
}
}
Aggregations