Search in sources :

Example 1 with LiteralRestrictionDescr

use of org.drools.compiler.lang.descr.LiteralRestrictionDescr in project drools by kiegroup.

the class KnowledgeBuilderTest method testDuplicateDeclaration.

// TODO we now allow bindings on declarations, so update the test for this
@Test
// TODO we now allow bindings on declarations, so update the test for this
@Ignore
public void testDuplicateDeclaration() {
    final KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
    final PackageDescr packageDescr = new PackageDescr("p1");
    final RuleDescr ruleDescr = new RuleDescr("rule-1");
    packageDescr.addRule(ruleDescr);
    final AndDescr lhs = new AndDescr();
    ruleDescr.setLhs(lhs);
    final PatternDescr pattern1 = new PatternDescr(Cheese.class.getName());
    lhs.addDescr(pattern1);
    final BindingDescr fieldBindingDescr = new BindingDescr("$type", "type");
    final FieldConstraintDescr literalDescr = new FieldConstraintDescr("type");
    literalDescr.addRestriction(new LiteralRestrictionDescr("==", "stilton"));
    pattern1.addConstraint(fieldBindingDescr);
    pattern1.addConstraint(literalDescr);
    final PatternDescr pattern2 = new PatternDescr(Cheese.class.getName());
    lhs.addDescr(pattern2);
    pattern2.addConstraint(fieldBindingDescr);
    ruleDescr.setConsequence("update(stilton);");
    builder.addPackage(packageDescr);
    assertLength(2, builder.getErrors().getErrors());
}
Also used : BindingDescr(org.drools.compiler.lang.descr.BindingDescr) PatternDescr(org.drools.compiler.lang.descr.PatternDescr) AndDescr(org.drools.compiler.lang.descr.AndDescr) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) FieldConstraintDescr(org.drools.compiler.lang.descr.FieldConstraintDescr) Cheese(org.drools.compiler.Cheese) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) LiteralRestrictionDescr(org.drools.compiler.lang.descr.LiteralRestrictionDescr) Ignore(org.junit.Ignore) CompositeObjectSinkAdapterTest(org.drools.core.reteoo.CompositeObjectSinkAdapterTest) Test(org.junit.Test)

Example 2 with LiteralRestrictionDescr

use of org.drools.compiler.lang.descr.LiteralRestrictionDescr in project drools by kiegroup.

the class KnowledgeBuilderTest method createBasicPackageWithOneRule.

private PackageDescr createBasicPackageWithOneRule(int line, int col) {
    PackageDescr packageDescr = new PackageDescr("p1");
    RuleDescr ruleDescr = new RuleDescr("rule-1");
    ruleDescr.setLocation(line, col);
    packageDescr.addRule(ruleDescr);
    AndDescr lhs = new AndDescr();
    ruleDescr.setLhs(lhs);
    PatternDescr patternDescr = new PatternDescr(Cheese.class.getName(), "stilton");
    FieldConstraintDescr literalDescr = new FieldConstraintDescr("type");
    literalDescr.addRestriction(new LiteralRestrictionDescr("==", null));
    patternDescr.addConstraint(literalDescr);
    ruleDescr.setConsequence("");
    return packageDescr;
}
Also used : PatternDescr(org.drools.compiler.lang.descr.PatternDescr) AndDescr(org.drools.compiler.lang.descr.AndDescr) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) FieldConstraintDescr(org.drools.compiler.lang.descr.FieldConstraintDescr) Cheese(org.drools.compiler.Cheese) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) LiteralRestrictionDescr(org.drools.compiler.lang.descr.LiteralRestrictionDescr)

Example 3 with LiteralRestrictionDescr

use of org.drools.compiler.lang.descr.LiteralRestrictionDescr in project drools by kiegroup.

the class KnowledgeBuilderTest method testDuplicateRuleNames.

@Test
public void testDuplicateRuleNames() throws Exception {
    final KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
    RuleDescr ruleDescr;
    AndDescr lhs;
    PatternDescr patternDescr;
    FieldConstraintDescr literalDescr;
    final PackageDescr packageDescr = createBasicPackageWithOneRule(1, 1);
    ruleDescr = new RuleDescr("rule-1");
    ruleDescr.setLocation(42, 43);
    packageDescr.addRule(ruleDescr);
    lhs = new AndDescr();
    ruleDescr.setLhs(lhs);
    patternDescr = new PatternDescr(Cheese.class.getName(), "stilton");
    literalDescr = new FieldConstraintDescr("type");
    literalDescr.addRestriction(new LiteralRestrictionDescr("!=", null));
    patternDescr.addConstraint(literalDescr);
    ruleDescr.setConsequence("");
    ruleDescr = new RuleDescr("rule-2");
    ruleDescr.setLocation(42, 43);
    packageDescr.addRule(ruleDescr);
    lhs = new AndDescr();
    ruleDescr.setLhs(lhs);
    patternDescr = new PatternDescr(Cheese.class.getName(), "stilton");
    literalDescr = new FieldConstraintDescr("type");
    literalDescr.addRestriction(new LiteralRestrictionDescr("!=", null));
    patternDescr.addConstraint(literalDescr);
    ruleDescr.setConsequence("");
    builder.addPackage(packageDescr);
    assertLength(2, builder.getErrors().getErrors());
    final ParserError err = (ParserError) builder.getErrors().getErrors()[0];
    assertEquals(42, err.getRow());
    assertEquals(43, err.getCol());
}
Also used : PatternDescr(org.drools.compiler.lang.descr.PatternDescr) ParserError(org.drools.compiler.compiler.ParserError) AndDescr(org.drools.compiler.lang.descr.AndDescr) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) FieldConstraintDescr(org.drools.compiler.lang.descr.FieldConstraintDescr) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) LiteralRestrictionDescr(org.drools.compiler.lang.descr.LiteralRestrictionDescr) CompositeObjectSinkAdapterTest(org.drools.core.reteoo.CompositeObjectSinkAdapterTest) Test(org.junit.Test)

Example 4 with LiteralRestrictionDescr

use of org.drools.compiler.lang.descr.LiteralRestrictionDescr in project drools by kiegroup.

the class PatternBuilder method buildLiteralRestrictionDescr.

protected LiteralRestrictionDescr buildLiteralRestrictionDescr(RuleBuildContext context, RelationalExprDescr exprDescr, String rightValue, String operator, boolean isRightLiteral) {
    // is it a literal? Does not include enums
    if (isRightLiteral) {
        return new LiteralRestrictionDescr(operator, exprDescr.isNegated(), exprDescr.getParameters(), rightValue, LiteralRestrictionDescr.TYPE_STRING);
    }
    // is it an enum?
    int dotPos = rightValue.lastIndexOf('.');
    if (dotPos >= 0) {
        final String mainPart = rightValue.substring(0, dotPos);
        String lastPart = rightValue.substring(dotPos + 1);
        try {
            context.getDialect().getTypeResolver().resolveType(mainPart);
            if (lastPart.indexOf('(') < 0 && lastPart.indexOf('.') < 0 && lastPart.indexOf('[') < 0) {
                return new LiteralRestrictionDescr(operator, 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 : LiteralRestrictionDescr(org.drools.compiler.lang.descr.LiteralRestrictionDescr) XpathConstraint(org.drools.core.rule.constraint.XpathConstraint) NegConstraint(org.drools.core.rule.constraint.NegConstraint) Constraint(org.drools.core.spi.Constraint) MvelConstraint(org.drools.core.rule.constraint.MvelConstraint) EvaluatorConstraint(org.drools.core.rule.constraint.EvaluatorConstraint) PredicateConstraint(org.drools.core.rule.PredicateConstraint)

Example 5 with LiteralRestrictionDescr

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

Aggregations

LiteralRestrictionDescr (org.drools.compiler.lang.descr.LiteralRestrictionDescr)7 AndDescr (org.drools.compiler.lang.descr.AndDescr)4 FieldConstraintDescr (org.drools.compiler.lang.descr.FieldConstraintDescr)4 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)4 PatternDescr (org.drools.compiler.lang.descr.PatternDescr)4 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)4 Cheese (org.drools.compiler.Cheese)3 CompositeObjectSinkAdapterTest (org.drools.core.reteoo.CompositeObjectSinkAdapterTest)3 Test (org.junit.Test)3 PredicateConstraint (org.drools.core.rule.PredicateConstraint)2 EvaluatorConstraint (org.drools.core.rule.constraint.EvaluatorConstraint)2 MvelConstraint (org.drools.core.rule.constraint.MvelConstraint)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 ParserError (org.drools.compiler.compiler.ParserError)1 BindingDescr (org.drools.compiler.lang.descr.BindingDescr)1 ValueType (org.drools.core.base.ValueType)1 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)1