Search in sources :

Example 31 with PackageDescr

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

the class RuleParserTest method testOrWithBinding.

/**
 * test basic foo : Fact() || Fact() stuff
 */
@Test
public void testOrWithBinding() throws Exception {
    final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "or_binding.drl");
    assertEquals(1, pkg.getRules().size());
    final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
    assertEquals(2, rule.getLhs().getDescrs().size());
    final OrDescr or = (OrDescr) rule.getLhs().getDescrs().get(0);
    assertEquals(2, or.getDescrs().size());
    final PatternDescr leftPattern = (PatternDescr) or.getDescrs().get(0);
    assertEquals("Person", leftPattern.getObjectType());
    assertEquals("foo", leftPattern.getIdentifier());
    final PatternDescr rightPattern = (PatternDescr) or.getDescrs().get(1);
    assertEquals("Person", rightPattern.getObjectType());
    assertEquals("foo", rightPattern.getIdentifier());
    final PatternDescr cheeseDescr = (PatternDescr) rule.getLhs().getDescrs().get(1);
    assertEquals("Cheese", cheeseDescr.getObjectType());
    assertEquals(null, cheeseDescr.getIdentifier());
    assertEqualsIgnoreWhitespace("System.out.println( \"Mark and Michael\" + bar );", (String) rule.getConsequence());
}
Also used : PatternDescr(org.drools.drl.ast.descr.PatternDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) PackageDescr(org.drools.drl.ast.descr.PackageDescr) OrDescr(org.drools.drl.ast.descr.OrDescr) Test(org.junit.Test)

Example 32 with PackageDescr

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

the class RuleParserTest method testWithPredicate.

@Test
public void testWithPredicate() throws Exception {
    final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "with_predicate.drl");
    assertEquals(1, pkg.getRules().size());
    final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
    assertEquals(1, rule.getLhs().getDescrs().size());
    final PatternDescr col = (PatternDescr) rule.getLhs().getDescrs().get(0);
    AndDescr and = (AndDescr) col.getConstraint();
    assertEquals(2, and.getDescrs().size());
    final ExprConstraintDescr field = (ExprConstraintDescr) col.getDescrs().get(0);
    final ExprConstraintDescr pred = (ExprConstraintDescr) and.getDescrs().get(1);
    assertEquals("$age2:age", field.getExpression());
    assertEqualsIgnoreWhitespace("$age2 == $age1+2", pred.getExpression());
}
Also used : PatternDescr(org.drools.drl.ast.descr.PatternDescr) AndDescr(org.drools.drl.ast.descr.AndDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) PackageDescr(org.drools.drl.ast.descr.PackageDescr) ExprConstraintDescr(org.drools.drl.ast.descr.ExprConstraintDescr) Test(org.junit.Test)

Example 33 with PackageDescr

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

the class RuleParserTest method testAttributes2.

@Test
public void testAttributes2() throws Exception {
    final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "rule_attributes2.drl");
    assertFalse(parser.getErrors().toString(), parser.hasErrors());
    List<RuleDescr> rules = pkg.getRules();
    assertEquals(3, rules.size());
    RuleDescr rule = rules.get(0);
    assertEquals("rule1", rule.getName());
    Map<String, AttributeDescr> attrs = rule.getAttributes();
    assertEquals(2, attrs.size());
    AttributeDescr at = (AttributeDescr) attrs.get("salience");
    assertEquals("salience", at.getName());
    assertEquals("(42)", at.getValue());
    at = (AttributeDescr) attrs.get("agenda-group");
    assertEquals("agenda-group", at.getName());
    assertEquals("my_group", at.getValue());
    rule = rules.get(1);
    assertEquals("rule2", rule.getName());
    attrs = rule.getAttributes();
    assertEquals(2, attrs.size());
    at = (AttributeDescr) attrs.get("salience");
    assertEquals("salience", at.getName());
    assertEquals("(Integer.MIN_VALUE)", at.getValue());
    at = (AttributeDescr) attrs.get("no-loop");
    assertEquals("no-loop", at.getName());
    rule = rules.get(2);
    assertEquals("rule3", rule.getName());
    attrs = rule.getAttributes();
    assertEquals(2, attrs.size());
    at = (AttributeDescr) attrs.get("enabled");
    assertEquals("enabled", at.getName());
    assertEquals("(Boolean.TRUE)", at.getValue());
    at = (AttributeDescr) attrs.get("activation-group");
    assertEquals("activation-group", at.getName());
    assertEquals("my_activation_group", at.getValue());
}
Also used : RuleDescr(org.drools.drl.ast.descr.RuleDescr) PackageDescr(org.drools.drl.ast.descr.PackageDescr) AttributeDescr(org.drools.drl.ast.descr.AttributeDescr) Test(org.junit.Test)

Example 34 with PackageDescr

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

the class VerifierImpl method addResourcesToVerify.

public void addResourcesToVerify(Resource resource, ResourceType type) {
    verifierPackageBuilder.addKnowledgeResource(resource, type, null);
    if (verifierPackageBuilder.hasErrors()) {
        addVerifierErrors(verifierPackageBuilder.getErrors());
    }
    PackageDescr pkg = verifierPackageBuilder.getPackageDescr();
    if (pkg != null) {
        addPackageDescr(pkg);
    } else {
        errors.add(new VerifierError("Verifier could not form a PackageDescr from the resources that it was trying to verify."));
    }
}
Also used : VerifierError(org.drools.verifier.VerifierError) PackageDescr(org.drools.drl.ast.descr.PackageDescr)

Example 35 with PackageDescr

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

the class ExprConstraintDescrVisitorTest method testVisitVariableRestriction.

@Test
public void testVisitVariableRestriction() throws Exception {
    PackageDescr packageDescr = getPackageDescr(getClass().getResourceAsStream("ExprConstraintDescr3.drl"));
    assertNotNull(packageDescr);
    packageDescrVisitor.visitPackageDescr(packageDescr);
    Collection<StringRestriction> allRestrictions = verifierData.getAll(VerifierComponentType.RESTRICTION);
    assertEquals(1, allRestrictions.size());
    assertContainsFields(1);
    assertContainsField("age");
    assertContainsVariable("Test 1", "var");
    assertContainsVariableRestriction(Operator.BuiltInOperator.EQUAL.getOperator(), "var");
}
Also used : StringRestriction(org.drools.verifier.components.StringRestriction) PackageDescr(org.drools.drl.ast.descr.PackageDescr) Test(org.junit.Test)

Aggregations

PackageDescr (org.drools.drl.ast.descr.PackageDescr)262 Test (org.junit.Test)210 RuleDescr (org.drools.drl.ast.descr.RuleDescr)104 PatternDescr (org.drools.drl.ast.descr.PatternDescr)85 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)52 AndDescr (org.drools.drl.ast.descr.AndDescr)39 DrlParser (org.drools.drl.parser.DrlParser)39 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)25 InputStreamReader (java.io.InputStreamReader)23 CompositePackageDescr (org.drools.compiler.lang.descr.CompositePackageDescr)23 ExprConstraintDescr (org.drools.drl.ast.descr.ExprConstraintDescr)23 KieSession (org.kie.api.runtime.KieSession)19 List (java.util.List)18 InternalKnowledgeBase (org.drools.kiesession.rulebase.InternalKnowledgeBase)18 XmlPackageReader (org.drools.compiler.compiler.xml.XmlPackageReader)17 GlobalDescr (org.drools.drl.ast.descr.GlobalDescr)17 KiePackage (org.kie.api.definition.KiePackage)17 DrlDumper (org.drools.mvel.DrlDumper)16 Cheese (org.drools.mvel.compiler.Cheese)16 AccumulateDescr (org.drools.drl.ast.descr.AccumulateDescr)15