Search in sources :

Example 1 with LiteralRestrictionDescr

use of org.drools.drl.ast.descr.LiteralRestrictionDescr in project drools by kiegroup.

the class KnowledgeBuilderTest method testNull.

@Test
public void testNull() {
    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 patternDescr = new PatternDescr(Cheese.class.getName(), "stilton");
    final FieldConstraintDescr literalDescr = new FieldConstraintDescr("type");
    literalDescr.addRestriction(new LiteralRestrictionDescr("==", null));
    patternDescr.addConstraint(literalDescr);
    ruleDescr.setConsequence("");
    builder.addPackage(packageDescr);
    InternalKnowledgePackage pkg = builder.getPackage(packageDescr.getName());
    final RuleImpl rule = pkg.getRule("rule-1");
    assertLength(0, builder.getErrors().getErrors());
}
Also used : PatternDescr(org.drools.drl.ast.descr.PatternDescr) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) AndDescr(org.drools.drl.ast.descr.AndDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) FieldConstraintDescr(org.drools.drl.ast.descr.FieldConstraintDescr) Cheese(org.drools.mvel.compiler.Cheese) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) PackageDescr(org.drools.drl.ast.descr.PackageDescr) LiteralRestrictionDescr(org.drools.drl.ast.descr.LiteralRestrictionDescr) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) Test(org.junit.Test)

Example 2 with LiteralRestrictionDescr

use of org.drools.drl.ast.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.drl.ast.descr.PatternDescr) AndDescr(org.drools.drl.ast.descr.AndDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) FieldConstraintDescr(org.drools.drl.ast.descr.FieldConstraintDescr) Cheese(org.drools.mvel.compiler.Cheese) PackageDescr(org.drools.drl.ast.descr.PackageDescr) LiteralRestrictionDescr(org.drools.drl.ast.descr.LiteralRestrictionDescr)

Example 3 with LiteralRestrictionDescr

use of org.drools.drl.ast.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.drl.ast.descr.PatternDescr) ParserError(org.drools.drl.parser.ParserError) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) AndDescr(org.drools.drl.ast.descr.AndDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) FieldConstraintDescr(org.drools.drl.ast.descr.FieldConstraintDescr) PackageDescr(org.drools.drl.ast.descr.PackageDescr) LiteralRestrictionDescr(org.drools.drl.ast.descr.LiteralRestrictionDescr) Test(org.junit.Test)

Example 4 with LiteralRestrictionDescr

use of org.drools.drl.ast.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.drl.ast.descr.BindingDescr) PatternDescr(org.drools.drl.ast.descr.PatternDescr) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) AndDescr(org.drools.drl.ast.descr.AndDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) FieldConstraintDescr(org.drools.drl.ast.descr.FieldConstraintDescr) Cheese(org.drools.mvel.compiler.Cheese) PackageDescr(org.drools.drl.ast.descr.PackageDescr) LiteralRestrictionDescr(org.drools.drl.ast.descr.LiteralRestrictionDescr) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 5 with LiteralRestrictionDescr

use of org.drools.drl.ast.descr.LiteralRestrictionDescr in project drools by kiegroup.

the class PatternBuilder method normalizeExpression.

private String normalizeExpression(RuleBuildContext context, Pattern pattern, RelationalExprDescr subDescr, String subExpr) {
    String leftValue = findLeftExpressionValue(subDescr);
    String rightValue = findRightExpressionValue(subDescr);
    String operator = subDescr.getOperator();
    subExpr = ConstraintUtil.inverseExpression(subDescr, subExpr, leftValue, rightValue, operator, pattern);
    ValueType valueType = getValueType(context, pattern, leftValue);
    if (valueType != null && valueType.isDate()) {
        FieldValue fieldValue = ConstraintBuilder.get().getMvelFieldValue(context, valueType, rightValue);
        if (fieldValue != null) {
            subExpr = subExpr.replace(rightValue, getNormalizeDate(valueType, fieldValue));
        }
        return subExpr;
    }
    if (operator.equals("str")) {
        return normalizeStringOperator(leftValue, rightValue, new LiteralRestrictionDescr(operator, subDescr.isNegated(), subDescr.getParameters(), rightValue, LiteralRestrictionDescr.TYPE_STRING));
    }
    // resolve ambiguity between mvel's "empty" keyword and constraints like: List(empty == ...)
    return normalizeEmptyKeyword(subExpr, operator);
}
Also used : ValueType(org.drools.core.base.ValueType) FieldValue(org.drools.core.spi.FieldValue) LiteralRestrictionDescr(org.drools.drl.ast.descr.LiteralRestrictionDescr)

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