Search in sources :

Example 1 with RuleDescr

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

the class NestingTest method testNesting.

@Test
public void testNesting() throws Exception {
    final String drl = "package org.drools.compiler.integrationtests.drl;\n" + "\n" + "dialect \"mvel\"\n" + "\n" + "import " + Person.class.getCanonicalName() + ";\n" + "import " + Address.class.getCanonicalName() + ";\n" + "\n" + "rule \"test something\"\n" + "\n" + "    when\n" + "        p: Person( name==\"Michael\",\n" + "                                (addresses[1].street == \"Low\" &&\n" + "                                addresses[0].street == \"High\"  )\n" + "                                )\n" + "    then\n" + "        p.name = \"goober\";\n" + "        System.out.println(p.name);\n" + "        insert(new Address(\"Latona\"));\n" + "end";
    final Person p = new Person();
    p.setName("Michael");
    final Address add1 = new Address();
    add1.setStreet("High");
    final Address add2 = new Address();
    add2.setStreet("Low");
    final List<Address> l = new ArrayList<>();
    l.add(add1);
    l.add(add2);
    p.setAddresses(l);
    final DrlParser parser = new DrlParser(LanguageLevelOption.DRL5);
    final PackageDescr desc = parser.parse(new StringReader(drl));
    final List packageAttrs = desc.getAttributes();
    assertEquals(1, desc.getRules().size());
    assertEquals(1, packageAttrs.size());
    final RuleDescr rule = desc.getRules().get(0);
    final Map<String, AttributeDescr> ruleAttrs = rule.getAttributes();
    assertEquals(1, ruleAttrs.size());
    assertEquals("mvel", ruleAttrs.get("dialect").getValue());
    assertEquals("dialect", ruleAttrs.get("dialect").getName());
    final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("nesting-test", kieBaseTestConfiguration, drl);
    final KieSession ksession = kbase.newKieSession();
    try {
        ksession.insert(p);
        ksession.fireAllRules();
    } finally {
        ksession.dispose();
    }
}
Also used : Address(org.drools.testcoverage.common.model.Address) ArrayList(java.util.ArrayList) KieBase(org.kie.api.KieBase) StringReader(java.io.StringReader) RuleDescr(org.drools.drl.ast.descr.RuleDescr) DrlParser(org.drools.drl.parser.DrlParser) ArrayList(java.util.ArrayList) List(java.util.List) KieSession(org.kie.api.runtime.KieSession) PackageDescr(org.drools.drl.ast.descr.PackageDescr) Person(org.drools.testcoverage.common.model.Person) AttributeDescr(org.drools.drl.ast.descr.AttributeDescr) Test(org.junit.Test)

Example 2 with RuleDescr

use of org.drools.drl.ast.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 : DescrBuildError(org.drools.compiler.compiler.DescrBuildError) AndDescr(org.drools.drl.ast.descr.AndDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) CompilationProblem(org.kie.internal.jci.CompilationProblem) Test(org.junit.Test)

Example 3 with RuleDescr

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

the class XmlPackageReaderTest method testParseSimpleRule.

@Test
public void testParseSimpleRule() throws Exception {
    final XmlPackageReader xmlPackageReader = getXmReader();
    xmlPackageReader.read(new InputStreamReader(getClass().getResourceAsStream("test_SimpleRule1.xml")));
    final PackageDescr packageDescr = xmlPackageReader.getPackageDescr();
    assertNotNull(packageDescr);
    assertEquals("com.sample", packageDescr.getName());
    final List imports = packageDescr.getImports();
    assertEquals(2, imports.size());
    assertEquals("java.util.List", ((ImportDescr) imports.get(0)).getTarget());
    assertEquals("org.drools.mvel.compiler.Person", ((ImportDescr) imports.get(1)).getTarget());
    RuleDescr ruleDescr = (RuleDescr) packageDescr.getRules().get(0);
    assertEquals("simple_rule1", ruleDescr.getName());
    AndDescr lhs = ruleDescr.getLhs();
    PatternDescr patternDescr = (PatternDescr) lhs.getDescrs().get(0);
    assertEquals("Person", patternDescr.getObjectType());
    ExprConstraintDescr expr = (ExprConstraintDescr) ((AndDescr) patternDescr.getConstraint()).getDescrs().get(0);
    assertEquals("name == \"darth\"", expr.getExpression());
    ruleDescr = (RuleDescr) packageDescr.getRules().get(1);
    assertEquals("simple_rule2", ruleDescr.getName());
    lhs = ruleDescr.getLhs();
    patternDescr = (PatternDescr) lhs.getDescrs().get(0);
    assertEquals("Person", patternDescr.getObjectType());
    expr = (ExprConstraintDescr) ((AndDescr) patternDescr.getConstraint()).getDescrs().get(0);
    assertEquals("age == 35 || == -3.5", expr.getExpression());
    ruleDescr = (RuleDescr) packageDescr.getRules().get(2);
    assertEquals("simple_rule3", ruleDescr.getName());
    lhs = ruleDescr.getLhs();
    patternDescr = (PatternDescr) lhs.getDescrs().get(0);
    assertEquals("Person", patternDescr.getObjectType());
    expr = (ExprConstraintDescr) ((AndDescr) patternDescr.getConstraint()).getDescrs().get(0);
    assertEquals("age == 35 || (!= 7.0 && != -70)", expr.getExpression());
    ruleDescr = (RuleDescr) packageDescr.getRules().get(3);
    assertEquals("simple_rule3", ruleDescr.getName());
    lhs = ruleDescr.getLhs();
    patternDescr = (PatternDescr) lhs.getDescrs().get(1);
    assertEquals("Person", patternDescr.getObjectType());
    expr = (ExprConstraintDescr) ((AndDescr) patternDescr.getConstraint()).getDescrs().get(0);
    assertEquals("name == $s", expr.getExpression());
    ruleDescr = (RuleDescr) packageDescr.getRules().get(4);
    assertEquals("simple_rule4", ruleDescr.getName());
    lhs = ruleDescr.getLhs();
    patternDescr = (PatternDescr) lhs.getDescrs().get(1);
    assertEquals("Person", patternDescr.getObjectType());
    expr = (ExprConstraintDescr) ((AndDescr) patternDescr.getConstraint()).getDescrs().get(0);
    assertEquals("(name == $s) || (age == 35 || (!= 7.0 && != -70))", expr.getExpression());
    ruleDescr = (RuleDescr) packageDescr.getRules().get(5);
    assertEquals("simple_rule5", ruleDescr.getName());
    lhs = ruleDescr.getLhs();
    patternDescr = (PatternDescr) lhs.getDescrs().get(1);
    assertEquals("Person", patternDescr.getObjectType());
    expr = (ExprConstraintDescr) ((AndDescr) patternDescr.getConstraint()).getDescrs().get(0);
    assertEquals("(name == $s) || ((age != 34) && (age != 37) && (name != \"yoda\"))", expr.getExpression());
}
Also used : InputStreamReader(java.io.InputStreamReader) PatternDescr(org.drools.drl.ast.descr.PatternDescr) AndDescr(org.drools.drl.ast.descr.AndDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) List(java.util.List) PackageDescr(org.drools.drl.ast.descr.PackageDescr) ExprConstraintDescr(org.drools.drl.ast.descr.ExprConstraintDescr) XmlPackageReader(org.drools.compiler.compiler.xml.XmlPackageReader) Test(org.junit.Test)

Example 4 with RuleDescr

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

the class XmlPackageReaderTest method testParseExists.

@Test
public void testParseExists() throws Exception {
    final XmlPackageReader xmlPackageReader = getXmReader();
    xmlPackageReader.read(new InputStreamReader(getClass().getResourceAsStream("test_ParseExists.xml")));
    final PackageDescr packageDescr = xmlPackageReader.getPackageDescr();
    assertNotNull(packageDescr);
    RuleDescr obj = (RuleDescr) packageDescr.getRules().get(0);
    Object existdescr = obj.getLhs().getDescrs().get(0);
    assertTrue(existdescr instanceof ExistsDescr);
    Object patternDescriptor = ((ExistsDescr) existdescr).getDescrs().get(0);
    assertTrue(patternDescriptor instanceof PatternDescr);
    assertEquals(((PatternDescr) patternDescriptor).getObjectType(), "Person");
    Object notDescr = obj.getLhs().getDescrs().get(1);
    assertEquals(notDescr.getClass().getName(), NotDescr.class.getName());
    existdescr = ((NotDescr) notDescr).getDescrs().get(0);
    patternDescriptor = ((ExistsDescr) existdescr).getDescrs().get(0);
    assertTrue(patternDescriptor instanceof PatternDescr);
    assertEquals(((PatternDescr) patternDescriptor).getObjectType(), "Cheese");
}
Also used : NotDescr(org.drools.drl.ast.descr.NotDescr) ExistsDescr(org.drools.drl.ast.descr.ExistsDescr) InputStreamReader(java.io.InputStreamReader) PatternDescr(org.drools.drl.ast.descr.PatternDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) PackageDescr(org.drools.drl.ast.descr.PackageDescr) XmlPackageReader(org.drools.compiler.compiler.xml.XmlPackageReader) Test(org.junit.Test)

Example 5 with RuleDescr

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

the class XmlPackageReaderTest method testParseFrom.

@Test
public void testParseFrom() throws Exception {
    final XmlPackageReader xmlPackageReader = getXmReader();
    xmlPackageReader.read(new InputStreamReader(getClass().getResourceAsStream("test_ParseFrom.xml")));
    final PackageDescr packageDescr = xmlPackageReader.getPackageDescr();
    assertNotNull(packageDescr);
    RuleDescr obj = (RuleDescr) packageDescr.getRules().get(0);
    PatternDescr patterndescr = (PatternDescr) obj.getLhs().getDescrs().get(0);
    FromDescr from = (FromDescr) patterndescr.getSource();
    MVELExprDescr accessordescriptor = (MVELExprDescr) from.getDataSource();
    assertEquals("cheesery.getCheeses(i+4)", accessordescriptor.getExpression());
    assertEquals(patterndescr.getObjectType(), "Cheese");
    assertEquals(patterndescr.getIdentifier(), "cheese");
}
Also used : InputStreamReader(java.io.InputStreamReader) PatternDescr(org.drools.drl.ast.descr.PatternDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) FromDescr(org.drools.drl.ast.descr.FromDescr) MVELExprDescr(org.drools.drl.ast.descr.MVELExprDescr) PackageDescr(org.drools.drl.ast.descr.PackageDescr) XmlPackageReader(org.drools.compiler.compiler.xml.XmlPackageReader) 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