Search in sources :

Example 6 with BindingDescr

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

the class KnowledgeBuilderTest method createPredicateRule.

private void createPredicateRule(final PackageDescr packageDescr, final String expression) {
    final RuleDescr ruleDescr = new RuleDescr("rule-1");
    packageDescr.addRule(ruleDescr);
    final AndDescr lhs = new AndDescr();
    ruleDescr.setLhs(lhs);
    final PatternDescr pattern = new PatternDescr(Cheese.class.getName(), "stilton");
    lhs.addDescr(pattern);
    final BindingDescr fieldBindingDescr = new BindingDescr("x", "price");
    pattern.addConstraint(fieldBindingDescr);
    final BindingDescr fieldBindingDescr2 = new BindingDescr("y", "price");
    pattern.addConstraint(fieldBindingDescr2);
    packageDescr.addGlobal(new GlobalDescr("map", "java.util.Map"));
    pattern.addConstraint(new ExprConstraintDescr(expression));
    ruleDescr.setConsequence("update(stilton);");
}
Also used : GlobalDescr(org.drools.drl.ast.descr.GlobalDescr) BindingDescr(org.drools.drl.ast.descr.BindingDescr) PatternDescr(org.drools.drl.ast.descr.PatternDescr) AndDescr(org.drools.drl.ast.descr.AndDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) Cheese(org.drools.mvel.compiler.Cheese) ExprConstraintDescr(org.drools.drl.ast.descr.ExprConstraintDescr)

Example 7 with BindingDescr

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

the class KnowledgeBuilderTest method testReturnValue.

@Test
public void testReturnValue() throws Exception {
    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 pattern = new PatternDescr(Cheese.class.getName(), "stilton");
    lhs.addDescr(pattern);
    BindingDescr fieldBindingDescr = new BindingDescr("x", "price");
    pattern.addConstraint(fieldBindingDescr);
    fieldBindingDescr = new BindingDescr("y", "price");
    pattern.addConstraint(fieldBindingDescr);
    packageDescr.addGlobal(new GlobalDescr("map", "java.util.Map"));
    pattern.addConstraint(new ExprConstraintDescr("price == (( (Integer) map.get( new Integer( x )) ).intValue() * y)"));
    ruleDescr.setConsequence("update(stilton);");
    builder.addPackage(packageDescr);
    assertEquals("Should not have any errors", 0, builder.getErrors().getErrors().length);
}
Also used : GlobalDescr(org.drools.drl.ast.descr.GlobalDescr) 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) Cheese(org.drools.mvel.compiler.Cheese) PackageDescr(org.drools.drl.ast.descr.PackageDescr) ExprConstraintDescr(org.drools.drl.ast.descr.ExprConstraintDescr) Test(org.junit.Test)

Example 8 with BindingDescr

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

the class PatternBuilderForQueryTest method testRuleWithQuery.

@Test
public void testRuleWithQuery() throws Exception {
    final KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
    final PackageDescr packageDescr = new PackageDescr("p1");
    QueryDescr queryDescr = new QueryDescr("query1");
    queryDescr.addParameter("String", "$name");
    queryDescr.addParameter("int", "$age");
    queryDescr.addParameter("String", "$likes");
    packageDescr.addRule(queryDescr);
    AndDescr lhs = new AndDescr();
    queryDescr.setLhs(lhs);
    PatternDescr pattern = new PatternDescr(Person.class.getName());
    lhs.addDescr(pattern);
    pattern.addConstraint(new BindingDescr("$name", "name", true));
    pattern.addConstraint(new BindingDescr("$age", "age", true));
    pattern.addConstraint(new BindingDescr("$likes", "likes", true));
    RuleDescr ruleDescr = new RuleDescr("rule-1");
    packageDescr.addRule(ruleDescr);
    lhs = new AndDescr();
    ruleDescr.setLhs(lhs);
    pattern = new PatternDescr(Cheese.class.getName());
    lhs.addDescr(pattern);
    pattern.addConstraint(new BindingDescr("$type", "type"));
    pattern = new PatternDescr("query1");
    pattern.setQuery(true);
    lhs.addDescr(pattern);
    ExprConstraintDescr expr = new ExprConstraintDescr("'bobba'");
    expr.setPosition(0);
    expr.setType(Type.POSITIONAL);
    pattern.addConstraint(expr);
    expr = new ExprConstraintDescr("$age");
    expr.setPosition(1);
    expr.setType(Type.POSITIONAL);
    pattern.addConstraint(expr);
    expr = new ExprConstraintDescr("$type");
    expr.setPosition(2);
    expr.setType(Type.POSITIONAL);
    pattern.addConstraint(expr);
    ruleDescr.setConsequence("System.out.println(\"age: \" + $age);");
    builder.addPackage(packageDescr);
    assertLength(0, builder.getErrors().getErrors());
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(Arrays.asList(new KiePackage[] { builder.getPackage(packageDescr.getName()) }));
    final KieSession session = kbase.newKieSession();
    session.insert(new Person("bobba", "stilton", 90));
    session.insert(new Person("bobba", "brie", 80));
    session.insert(new Person("bobba", "brie", 75));
    session.insert(new Person("darth", "brie", 100));
    session.insert(new Person("luke", "brie", 25));
    session.insert(new Cheese("brie", 25));
    session.fireAllRules();
}
Also used : BindingDescr(org.drools.drl.ast.descr.BindingDescr) PatternDescr(org.drools.drl.ast.descr.PatternDescr) AndDescr(org.drools.drl.ast.descr.AndDescr) Cheese(org.drools.mvel.compiler.Cheese) QueryDescr(org.drools.drl.ast.descr.QueryDescr) KiePackage(org.kie.api.definition.KiePackage) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) RuleDescr(org.drools.drl.ast.descr.RuleDescr) KieSession(org.kie.api.runtime.KieSession) PackageDescr(org.drools.drl.ast.descr.PackageDescr) Person(org.drools.mvel.compiler.Person) ExprConstraintDescr(org.drools.drl.ast.descr.ExprConstraintDescr) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) Test(org.junit.Test)

Example 9 with BindingDescr

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

the class PatternBuilder method rewriteCompositeExpressions.

private String rewriteCompositeExpressions(RuleBuildContext context, Pattern pattern, ConstraintConnectiveDescr d) {
    int i = 0;
    StringBuilder sb = new StringBuilder();
    for (BaseDescr subDescr : d.getDescrs()) {
        if (subDescr instanceof BindingDescr) {
            continue;
        }
        if (i++ > 0) {
            sb.append(" ").append(d.getConnective().getConnective()).append(" ");
        }
        String normalizedExpr;
        if (subDescr instanceof RelationalExprDescr && isSimpleExpr((RelationalExprDescr) subDescr)) {
            RelationalExprDescr relDescr = (RelationalExprDescr) subDescr;
            if (relDescr.getExpression() != null) {
                normalizedExpr = normalizeExpression(context, pattern, relDescr, relDescr.getExpression());
            } else {
                i--;
                normalizedExpr = "";
            }
        } else if (subDescr instanceof ConstraintConnectiveDescr) {
            String rewrittenExpr = rewriteCompositeExpressions(context, pattern, (ConstraintConnectiveDescr) subDescr);
            if (rewrittenExpr == null) {
                return null;
            }
            normalizedExpr = "(" + rewrittenExpr + ")";
        } else if (subDescr instanceof AtomicExprDescr) {
            normalizedExpr = ((AtomicExprDescr) subDescr).getRewrittenExpression();
        } else {
            return null;
        }
        sb.append(normalizedExpr);
    }
    return sb.toString();
}
Also used : BindingDescr(org.drools.drl.ast.descr.BindingDescr) BaseDescr(org.drools.drl.ast.descr.BaseDescr) AtomicExprDescr(org.drools.drl.ast.descr.AtomicExprDescr) ConstraintConnectiveDescr(org.drools.drl.ast.descr.ConstraintConnectiveDescr) 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) RelationalExprDescr(org.drools.drl.ast.descr.RelationalExprDescr)

Example 10 with BindingDescr

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

the class PatternBuilder method processPositional.

private void processPositional(RuleBuildContext context, PatternDescr patternDescr, Declaration xpathStartDeclaration, Pattern pattern, ExprConstraintDescr descr) {
    if (descr.getType() == ExprConstraintDescr.Type.POSITIONAL && pattern.getObjectType() instanceof ClassObjectType) {
        TypeDeclaration tDecl = context.getKnowledgeBuilder().getTypeDeclaration(pattern.getObjectType());
        if (tDecl == null) {
            registerDescrBuildError(context, patternDescr, "Unable to find @positional definitions for :" + pattern.getObjectType() + "\n");
            return;
        }
        ClassDefinition clsDef = tDecl.getTypeClassDef();
        if (clsDef == null) {
            registerDescrBuildError(context, patternDescr, "Unable to find @Positional field " + descr.getPosition() + " for class " + tDecl.getTypeName() + "\n");
            return;
        }
        FieldDefinition field = clsDef.getField(descr.getPosition());
        if (field == null) {
            registerDescrBuildError(context, patternDescr, "Unable to find @Positional field " + descr.getPosition() + " for class " + tDecl.getTypeName() + "\n");
            return;
        }
        String expr = descr.getExpression();
        boolean isSimpleIdentifier = isIdentifier(expr);
        if (isSimpleIdentifier) {
            // create a binding
            BindingDescr binder = new BindingDescr();
            binder.setUnification(true);
            binder.setExpression(field.getName());
            binder.setVariable(descr.getExpression());
            buildRuleBindings(context, patternDescr, xpathStartDeclaration, pattern, binder);
        } else {
            // create a constraint
            build(context, patternDescr, xpathStartDeclaration, pattern, descr, field.getName() + " == " + descr.getExpression());
        }
    }
}
Also used : BindingDescr(org.drools.drl.ast.descr.BindingDescr) AcceptsClassObjectType(org.drools.core.spi.AcceptsClassObjectType) ClassObjectType(org.drools.core.base.ClassObjectType) FieldDefinition(org.drools.core.factmodel.FieldDefinition) ClassDefinition(org.drools.core.factmodel.ClassDefinition) TypeDeclaration(org.drools.core.rule.TypeDeclaration)

Aggregations

BindingDescr (org.drools.drl.ast.descr.BindingDescr)42 ConstraintConnectiveDescr (org.drools.drl.ast.descr.ConstraintConnectiveDescr)17 Test (org.junit.Test)17 BaseDescr (org.drools.drl.ast.descr.BaseDescr)13 PatternDescr (org.drools.drl.ast.descr.PatternDescr)11 RuleDescr (org.drools.drl.ast.descr.RuleDescr)11 AtomicExprDescr (org.drools.drl.ast.descr.AtomicExprDescr)10 ExprConstraintDescr (org.drools.drl.ast.descr.ExprConstraintDescr)10 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)9 AndDescr (org.drools.drl.ast.descr.AndDescr)8 Cheese (org.drools.mvel.compiler.Cheese)8 PackageDescr (org.drools.drl.ast.descr.PackageDescr)7 RelationalExprDescr (org.drools.drl.ast.descr.RelationalExprDescr)7 DumperContext (org.drools.compiler.lang.DumperContext)6 GlobalDescr (org.drools.drl.ast.descr.GlobalDescr)6 ClassObjectType (org.drools.core.base.ClassObjectType)5 Declaration (org.drools.core.rule.Declaration)5 TypeDeclaration (org.drools.core.rule.TypeDeclaration)5 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)4 PredicateConstraint (org.drools.core.rule.PredicateConstraint)4