Search in sources :

Example 26 with RuleDescr

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

the class KnowledgeBuilderTest method createRule.

private RuleImpl createRule(final ConditionalElementDescr ceDescr, final KnowledgeBuilderImpl builder, final String consequence) throws Exception {
    final PackageDescr packageDescr = new PackageDescr("p1");
    final RuleDescr ruleDescr = new RuleDescr("rule-1");
    packageDescr.addRule(ruleDescr);
    final AndDescr lhs = new AndDescr();
    ruleDescr.setLhs(lhs);
    lhs.addDescr((BaseDescr) ceDescr);
    final PatternDescr patternDescr = new PatternDescr(Cheese.class.getName(), "stilton");
    patternDescr.addConstraint(new ExprConstraintDescr("type == \"stilton\" "));
    ceDescr.addDescr(patternDescr);
    ruleDescr.setConsequence(consequence);
    builder.addPackage(packageDescr);
    InternalKnowledgePackage pkg = builder.getPackage(packageDescr.getName());
    final RuleImpl rule = pkg.getRule("rule-1");
    assertEquals("rule-1", rule.getName());
    return rule;
}
Also used : 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) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) PackageDescr(org.drools.drl.ast.descr.PackageDescr) ExprConstraintDescr(org.drools.drl.ast.descr.ExprConstraintDescr) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 27 with RuleDescr

use of org.drools.drl.ast.descr.RuleDescr 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 28 with RuleDescr

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

the class KnowledgeBuilderTest method testEval.

@Test
public void testEval() 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"));
    final EvalDescr evalDescr = new EvalDescr("( ( Integer )map.get( new Integer(x) ) ).intValue() == y");
    lhs.addDescr(evalDescr);
    ruleDescr.setConsequence("update(stilton);");
    builder.addPackage(packageDescr);
    assertLength(0, builder.getErrors().getErrors());
    InternalKnowledgePackage pkg = builder.getPackage(packageDescr.getName());
    final RuleImpl rule = pkg.getRule("rule-1");
    final EvalCondition eval = (EvalCondition) rule.getLhs().getChildren().get(1);
    final CompiledInvoker invoker = (CompiledInvoker) eval.getEvalExpression();
    String s = invoker.getMethodBytecode();
}
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) Cheese(org.drools.mvel.compiler.Cheese) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) EvalCondition(org.drools.core.rule.EvalCondition) CompiledInvoker(org.drools.core.spi.CompiledInvoker) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) EvalDescr(org.drools.drl.ast.descr.EvalDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) PackageDescr(org.drools.drl.ast.descr.PackageDescr) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) Test(org.junit.Test)

Example 29 with RuleDescr

use of org.drools.drl.ast.descr.RuleDescr 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 30 with RuleDescr

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

the class KnowledgeBuilderTest method testLiteral.

@Test
public void testLiteral() 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);
    pattern.addConstraint(new ExprConstraintDescr("type == 'stilton'"));
    ruleDescr.setConsequence("update(stilton);");
    builder.addPackage(packageDescr);
    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) Cheese(org.drools.mvel.compiler.Cheese) PackageDescr(org.drools.drl.ast.descr.PackageDescr) ExprConstraintDescr(org.drools.drl.ast.descr.ExprConstraintDescr) Test(org.junit.Test)

Aggregations

RuleDescr (org.drools.drl.ast.descr.RuleDescr)191 Test (org.junit.Test)147 PackageDescr (org.drools.drl.ast.descr.PackageDescr)105 PatternDescr (org.drools.drl.ast.descr.PatternDescr)104 AndDescr (org.drools.drl.ast.descr.AndDescr)55 ExprConstraintDescr (org.drools.drl.ast.descr.ExprConstraintDescr)37 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)33 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)27 AttributeDescr (org.drools.drl.ast.descr.AttributeDescr)27 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)24 RuleBuildContext (org.drools.compiler.rule.builder.RuleBuildContext)17 Cheese (org.drools.mvel.compiler.Cheese)16 AccumulateDescr (org.drools.drl.ast.descr.AccumulateDescr)15 GlobalDescr (org.drools.drl.ast.descr.GlobalDescr)14 OrDescr (org.drools.drl.ast.descr.OrDescr)14 DialectCompiletimeRegistry (org.drools.compiler.compiler.DialectCompiletimeRegistry)13 Pattern (org.drools.core.rule.Pattern)13 List (java.util.List)12 BindingDescr (org.drools.drl.ast.descr.BindingDescr)11 FromDescr (org.drools.drl.ast.descr.FromDescr)11