use of org.drools.drl.ast.descr.LiteralRestrictionDescr 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;
}
if ("contains".equals(relDescr.getOperator()) && !isTypeCompatibleWithContainsOperator(extractor.getExtractToClass())) {
registerDescrBuildError(context, relDescr, "Cannot use contains on " + extractor.getExtractToClass() + " in expression '" + expr + "'");
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);
}
}
LiteralRestrictionDescr restrictionDescr = buildLiteralRestrictionDescr(context, relDescr, value2, isConstant);
if (restrictionDescr != null) {
ValueType vtype = extractor.getValueType();
FieldValue field = ConstraintBuilder.get().getMvelFieldValue(context, vtype, restrictionDescr.getText().trim());
if (field != null) {
Constraint constraint = getConstraintBuilder().buildLiteralConstraint(context, pattern, vtype, field, expr, value1, relDescr.getOperator(), relDescr.isNegated(), 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, value2);
if (declarations == null) {
return null;
}
}
}
return getConstraintBuilder().buildVariableConstraint(context, pattern, expr, declarations, value1, relDescr.getOperatorDescr(), value2, extractor, declr, relDescr, aliases);
}
use of org.drools.drl.ast.descr.LiteralRestrictionDescr in project drools by kiegroup.
the class PatternBuilder method buildLiteralRestrictionDescr.
protected LiteralRestrictionDescr buildLiteralRestrictionDescr(RuleBuildContext context, RelationalExprDescr exprDescr, String rightValue, boolean isRightLiteral) {
// is it a literal? Does not include enums
if (isRightLiteral) {
return new LiteralRestrictionDescr(exprDescr.getOperator(), exprDescr.isNegated(), exprDescr.getParameters(), rightValue, LiteralRestrictionDescr.TYPE_STRING);
}
// is it an enum or final?
int dotPos = rightValue.lastIndexOf('.');
if (dotPos >= 0) {
final String mainPart = rightValue.substring(0, dotPos);
String lastPart = rightValue.substring(dotPos + 1);
try {
Class<?> clazz = context.getDialect().getTypeResolver().resolveType(mainPart);
if (lastPart.indexOf('(') < 0 && lastPart.indexOf('.') < 0 && lastPart.indexOf('[') < 0) {
Field field = ClassUtils.getField(clazz, lastPart);
if (field != null && (field.isEnumConstant() || Modifier.isFinal(field.getModifiers()))) {
return new LiteralRestrictionDescr(exprDescr.getOperator(), exprDescr.isNegated(), exprDescr.getParameters(), rightValue, LiteralRestrictionDescr.TYPE_STRING);
}
}
} catch (ClassNotFoundException e) {
// do nothing as this is just probing to see if it was a class, which we now know it isn't :)
} catch (NoClassDefFoundError e) {
// do nothing as this is just probing to see if it was a class, which we now know it isn't :)
}
}
return null;
}
Aggregations