use of org.drools.core.base.ValueType 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);
}
Aggregations