Search in sources :

Example 6 with LiteralRestrictionDescr

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);
}
Also used : Pattern(org.drools.core.rule.Pattern) ValueType(org.drools.core.base.ValueType) XpathConstraint(org.drools.core.rule.constraint.XpathConstraint) PredicateConstraint(org.drools.core.rule.PredicateConstraint) NegConstraint(org.drools.core.rule.constraint.NegConstraint) Constraint(org.drools.core.spi.Constraint) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) FieldValue(org.drools.core.spi.FieldValue) TypeDeclaration(org.drools.core.rule.TypeDeclaration) Declaration(org.drools.core.rule.Declaration) XpathConstraint(org.drools.core.rule.constraint.XpathConstraint) PredicateConstraint(org.drools.core.rule.PredicateConstraint) NegConstraint(org.drools.core.rule.constraint.NegConstraint) Constraint(org.drools.core.spi.Constraint) LiteralRestrictionDescr(org.drools.drl.ast.descr.LiteralRestrictionDescr)

Example 7 with LiteralRestrictionDescr

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;
}
Also used : Field(java.lang.reflect.Field) LiteralRestrictionDescr(org.drools.drl.ast.descr.LiteralRestrictionDescr) XpathConstraint(org.drools.core.rule.constraint.XpathConstraint) PredicateConstraint(org.drools.core.rule.PredicateConstraint) NegConstraint(org.drools.core.rule.constraint.NegConstraint) Constraint(org.drools.core.spi.Constraint)

Aggregations

LiteralRestrictionDescr (org.drools.drl.ast.descr.LiteralRestrictionDescr)7 AndDescr (org.drools.drl.ast.descr.AndDescr)4 FieldConstraintDescr (org.drools.drl.ast.descr.FieldConstraintDescr)4 PackageDescr (org.drools.drl.ast.descr.PackageDescr)4 PatternDescr (org.drools.drl.ast.descr.PatternDescr)4 RuleDescr (org.drools.drl.ast.descr.RuleDescr)4 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)3 Cheese (org.drools.mvel.compiler.Cheese)3 Test (org.junit.Test)3 ValueType (org.drools.core.base.ValueType)2 PredicateConstraint (org.drools.core.rule.PredicateConstraint)2 NegConstraint (org.drools.core.rule.constraint.NegConstraint)2 XpathConstraint (org.drools.core.rule.constraint.XpathConstraint)2 Constraint (org.drools.core.spi.Constraint)2 FieldValue (org.drools.core.spi.FieldValue)2 Field (java.lang.reflect.Field)1 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)1 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)1 Declaration (org.drools.core.rule.Declaration)1 Pattern (org.drools.core.rule.Pattern)1