Search in sources :

Example 76 with RuleDescr

use of org.drools.compiler.lang.descr.RuleDescr 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.compiler.lang.descr.GlobalDescr) 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) Cheese(org.drools.compiler.Cheese) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) ExprConstraintDescr(org.drools.compiler.lang.descr.ExprConstraintDescr) CompositeObjectSinkAdapterTest(org.drools.core.reteoo.CompositeObjectSinkAdapterTest) Test(org.junit.Test)

Example 77 with RuleDescr

use of org.drools.compiler.lang.descr.RuleDescr 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 78 with RuleDescr

use of org.drools.compiler.lang.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.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 79 with RuleDescr

use of org.drools.compiler.lang.descr.RuleDescr 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 80 with RuleDescr

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

the class RuleErrorTest method testNewLineInMessage.

@Test
public void testNewLineInMessage() {
    CompilationProblem[] probs = new CompilationProblem[3];
    probs[0] = new MockCompilationProblem();
    probs[1] = new MockCompilationProblem();
    probs[2] = new MockCompilationProblem();
    DescrBuildError err = new DescrBuildError(new RuleDescr("ruleName"), new AndDescr(), probs, "IM IN YR EROR");
    assertNotNull(err.toString());
    String msg = err.getMessage();
    assertTrue(msg.indexOf("IM IN YR EROR") != -1);
    System.err.println(msg);
    assertEquals("IM IN YR EROR problem\nproblem\nproblem", msg);
}
Also used : AndDescr(org.drools.compiler.lang.descr.AndDescr) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) CompilationProblem(org.drools.compiler.commons.jci.problems.CompilationProblem) Test(org.junit.Test)

Aggregations

RuleDescr (org.drools.compiler.lang.descr.RuleDescr)161 Test (org.junit.Test)143 PatternDescr (org.drools.compiler.lang.descr.PatternDescr)103 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)100 AndDescr (org.drools.compiler.lang.descr.AndDescr)51 ExprConstraintDescr (org.drools.compiler.lang.descr.ExprConstraintDescr)37 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)19 AttributeDescr (org.drools.compiler.lang.descr.AttributeDescr)19 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)19 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)17 Cheese (org.drools.compiler.Cheese)16 AccumulateDescr (org.drools.compiler.lang.descr.AccumulateDescr)15 CompositeObjectSinkAdapterTest (org.drools.core.reteoo.CompositeObjectSinkAdapterTest)14 OrDescr (org.drools.compiler.lang.descr.OrDescr)13 RuleBuildContext (org.drools.compiler.rule.builder.RuleBuildContext)13 GlobalDescr (org.drools.compiler.lang.descr.GlobalDescr)12 BindingDescr (org.drools.compiler.lang.descr.BindingDescr)11 FromDescr (org.drools.compiler.lang.descr.FromDescr)11 DialectCompiletimeRegistry (org.drools.compiler.compiler.DialectCompiletimeRegistry)10 NotDescr (org.drools.compiler.lang.descr.NotDescr)10