Search in sources :

Example 31 with PackageDescr

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

the class DescrBuilderTest method testRuleRHSOptional.

@Test
public void testRuleRHSOptional() throws InstantiationException, IllegalAccessException {
    PackageDescr pkg = DescrFactory.newPackage().name("org.drools.compiler").newRule().name("r1").lhs().pattern("StockTick").constraint("company == \"RHT\"").end().end().end().getDescr();
    KiePackage kpkg = compilePkgDescr(pkg);
    assertEquals("org.drools.compiler", kpkg.getName());
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(Collections.singletonList(kpkg));
    KieSession ksession = kbase.newKieSession();
    ksession.insert(new StockTick(1, "RHT", 80, 1));
    int rules = ksession.fireAllRules();
    assertEquals(1, rules);
}
Also used : StockTick(org.drools.compiler.StockTick) KiePackage(org.kie.api.definition.KiePackage) KieSession(org.kie.api.runtime.KieSession) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) EntryPoint(org.kie.api.runtime.rule.EntryPoint) Test(org.junit.Test)

Example 32 with PackageDescr

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

the class DescrBuilderTest method testPackageAttributes.

@Test
public void testPackageAttributes() {
    PackageDescr pkg = DescrFactory.newPackage().name("org.drools").attribute("dialect").value("mvel").end().attribute("salience", "10").attribute("lock-on-active", "true", AttributeDescr.Type.BOOLEAN).getDescr();
    assertEquals("org.drools", pkg.getName());
    assertEquals(3, pkg.getAttributes().size());
    assertEquals("mvel", pkg.getAttribute("dialect").getValue());
    assertEquals("10", pkg.getAttribute("salience").getValue());
    assertEquals("true", pkg.getAttribute("lock-on-active").getValue());
    assertEquals(AttributeDescr.Type.BOOLEAN, pkg.getAttribute("lock-on-active").getType());
    assertNull(pkg.getAttribute("no-loop"));
    KiePackage kpkg = compilePkgDescr(pkg);
    assertEquals("org.drools", kpkg.getName());
}
Also used : KiePackage(org.kie.api.definition.KiePackage) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) Test(org.junit.Test)

Example 33 with PackageDescr

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

the class DescrBuilderTest method testDumperTimer.

@Test
public void testDumperTimer() {
    PackageDescr pkg = DescrFactory.newPackage().name("org.test").newRule().name("org.test").attribute("timer").value("cron:0/5 * * * * ?").end().lhs().end().rhs("").end().end().getDescr();
    String drl = new DrlDumper().dump(pkg);
    assertTrue(drl.contains("timer (cron:0/5 * * * * ?)"));
}
Also used : PackageDescr(org.drools.compiler.lang.descr.PackageDescr) DrlDumper(org.drools.compiler.lang.DrlDumper) Test(org.junit.Test)

Example 34 with PackageDescr

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

the class RuleParserTest method testFromWithInlineList.

@Test
public void testFromWithInlineList() throws Exception {
    String source = "rule XYZ \n" + " when \n" + " o: Order( ) \n" + " not( Number( ) from [1, 2, 3] ) \n" + " then \n" + " System.err.println(\"Invalid customer id found!\"); \n" + " o.addError(\"Invalid customer id\"); \n" + "end \n";
    PackageDescr pkg = (PackageDescr) parse("compilationUnit", source);
    assertFalse(parser.getErrors().toString(), parser.hasErrors());
    RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
    assertEquals("XYZ", rule.getName());
    PatternDescr number = (PatternDescr) ((NotDescr) rule.getLhs().getDescrs().get(1)).getDescrs().get(0);
    assertEquals("[1, 2, 3]", ((FromDescr) number.getSource()).getDataSource().toString());
}
Also used : NotDescr(org.drools.compiler.lang.descr.NotDescr) PatternDescr(org.drools.compiler.lang.descr.PatternDescr) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) FromDescr(org.drools.compiler.lang.descr.FromDescr) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) Test(org.junit.Test)

Example 35 with PackageDescr

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

the class RuleParserTest method testPluggableOperators.

@Test
public void testPluggableOperators() throws Exception {
    final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "pluggable_operators.drl");
    assertFalse(parser.getErrors().toString(), parser.hasErrors());
    assertEquals(1, pkg.getRules().size());
    final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
    assertEquals(5, rule.getLhs().getDescrs().size());
    final PatternDescr eventA = (PatternDescr) rule.getLhs().getDescrs().get(0);
    assertEquals("$a", eventA.getIdentifier());
    assertEquals("EventA", eventA.getObjectType());
    final PatternDescr eventB = (PatternDescr) rule.getLhs().getDescrs().get(1);
    assertEquals("$b", eventB.getIdentifier());
    assertEquals("EventB", eventB.getObjectType());
    assertEquals(1, eventB.getConstraint().getDescrs().size());
    assertEquals(1, eventB.getConstraint().getDescrs().size());
    final ExprConstraintDescr fcdB = (ExprConstraintDescr) eventB.getConstraint().getDescrs().get(0);
    assertEquals("this after[1,10] $a || this not after[15,20] $a", fcdB.getExpression());
    final PatternDescr eventC = (PatternDescr) rule.getLhs().getDescrs().get(2);
    assertEquals("$c", eventC.getIdentifier());
    assertEquals("EventC", eventC.getObjectType());
    assertEquals(1, eventC.getConstraint().getDescrs().size());
    final ExprConstraintDescr fcdC = (ExprConstraintDescr) eventC.getConstraint().getDescrs().get(0);
    assertEquals("this finishes $b", fcdC.getExpression());
    final PatternDescr eventD = (PatternDescr) rule.getLhs().getDescrs().get(3);
    assertEquals("$d", eventD.getIdentifier());
    assertEquals("EventD", eventD.getObjectType());
    assertEquals(1, eventD.getConstraint().getDescrs().size());
    final ExprConstraintDescr fcdD = (ExprConstraintDescr) eventD.getConstraint().getDescrs().get(0);
    assertEquals("this not starts $a", fcdD.getExpression());
    final PatternDescr eventE = (PatternDescr) rule.getLhs().getDescrs().get(4);
    assertEquals("$e", eventE.getIdentifier());
    assertEquals("EventE", eventE.getObjectType());
    assertEquals(1, eventE.getConstraint().getDescrs().size());
    ExprConstraintDescr fcdE = (ExprConstraintDescr) eventE.getConstraint().getDescrs().get(0);
    assertEquals("this not before[1, 10] $b || after[1, 10] $c && this after[1, 5] $d", fcdE.getExpression());
}
Also used : PatternDescr(org.drools.compiler.lang.descr.PatternDescr) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) ExprConstraintDescr(org.drools.compiler.lang.descr.ExprConstraintDescr) Test(org.junit.Test)

Aggregations

PackageDescr (org.drools.compiler.lang.descr.PackageDescr)243 Test (org.junit.Test)194 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)103 PatternDescr (org.drools.compiler.lang.descr.PatternDescr)85 DrlParser (org.drools.compiler.compiler.DrlParser)46 AndDescr (org.drools.compiler.lang.descr.AndDescr)39 InputStreamReader (java.io.InputStreamReader)27 CompositeObjectSinkAdapterTest (org.drools.core.reteoo.CompositeObjectSinkAdapterTest)27 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)24 ExprConstraintDescr (org.drools.compiler.lang.descr.ExprConstraintDescr)23 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)19 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)19 KiePackage (org.kie.api.definition.KiePackage)18 XmlPackageReader (org.drools.compiler.compiler.xml.XmlPackageReader)17 Cheese (org.drools.compiler.Cheese)16 KieSession (org.kie.api.runtime.KieSession)16 List (java.util.List)15 AccumulateDescr (org.drools.compiler.lang.descr.AccumulateDescr)15 GlobalDescr (org.drools.compiler.lang.descr.GlobalDescr)15 DrlDumper (org.drools.compiler.lang.DrlDumper)14