Search in sources :

Example 1 with AttributeDescr

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

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

the class RuleBuilderTest method testBuildDurationExpression.

@Test
public void testBuildDurationExpression() throws Exception {
    // creates mock objects
    final RuleBuildContext context = mock(RuleBuildContext.class);
    final RuleImpl rule = mock(RuleImpl.class);
    // creates input object
    final RuleDescr ruleDescr = new RuleDescr("my rule");
    ruleDescr.addAttribute(new AttributeDescr("duration", "( 1h30m )"));
    ruleDescr.addAttribute(new AttributeDescr("calendars", "[\"cal1\", \"cal2\"]"));
    // defining expectations on the mock object
    when(context.getRule()).thenReturn(rule);
    when(context.getRuleDescr()).thenReturn(ruleDescr);
    // calling the build method
    RuleBuilder.buildAttributes(context);
    // check expectations
    verify(rule).setTimer(new IntervalTimer(null, null, -1, TimeUtils.parseTimeString("1h30m"), 0));
    verify(rule).setCalendars(new String[] { "cal1", "cal2" });
}
Also used : RuleBuildContext(org.drools.compiler.rule.builder.RuleBuildContext) RuleDescr(org.drools.drl.ast.descr.RuleDescr) IntervalTimer(org.drools.core.time.impl.IntervalTimer) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) AttributeDescr(org.drools.drl.ast.descr.AttributeDescr) Test(org.junit.Test)

Example 3 with AttributeDescr

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

the class RuleBuilderTest method testInvalidDialect.

@Test
public void testInvalidDialect() throws Exception {
    final PackageDescr pkgDescr = new PackageDescr("org.drools");
    final RuleDescr ruleDescr = new RuleDescr("Test Rule");
    ruleDescr.addAttribute(new AttributeDescr("dialect", "mvl"));
    ruleDescr.setConsequence("");
    pkgDescr.addRule(ruleDescr);
    final KnowledgeBuilderImpl kBuilder = new KnowledgeBuilderImpl();
    kBuilder.addPackage(pkgDescr);
    assertFalse(kBuilder.getErrors().toString(), kBuilder.getErrors().isEmpty());
}
Also used : KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) 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 4 with AttributeDescr

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

the class PackageDescrTest method testAttributeOverriding.

@Test
public void testAttributeOverriding() {
    PackageDescr desc = new PackageDescr("foo");
    AttributeDescr at1 = new AttributeDescr("foo", "bar");
    AttributeDescr at2 = new AttributeDescr("foo2", "default");
    desc.addAttribute(at1);
    desc.addAttribute(at2);
    RuleDescr rule = new RuleDescr("abc");
    rule.addAttribute(new AttributeDescr("foo", "overridden"));
    desc.addRule(rule);
    List pkgAts = desc.getAttributes();
    assertEquals("bar", ((AttributeDescr) pkgAts.get(0)).getValue());
    assertEquals("default", ((AttributeDescr) pkgAts.get(1)).getValue());
    desc.afterRuleAdded(rule);
    Map<String, AttributeDescr> ruleAts = rule.getAttributes();
    assertEquals("overridden", ((AttributeDescr) ruleAts.get("foo")).getValue());
    assertEquals("default", ((AttributeDescr) ruleAts.get("foo2")).getValue());
}
Also used : RuleDescr(org.drools.drl.ast.descr.RuleDescr) List(java.util.List) PackageDescr(org.drools.drl.ast.descr.PackageDescr) AttributeDescr(org.drools.drl.ast.descr.AttributeDescr) Test(org.junit.Test)

Example 5 with AttributeDescr

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

the class RuleParserTest method testRuleFlowGroup.

@Test
public void testRuleFlowGroup() throws Exception {
    final RuleDescr rule = (RuleDescr) parseResource("rule", "ruleflowgroup.drl");
    assertFalse(parser.getErrors().toString(), parser.hasErrors());
    assertNotNull(rule);
    assertEquals("rule1", rule.getName());
    final AttributeDescr att = (AttributeDescr) rule.getAttributes().get("ruleflow-group");
    assertEquals("a group", att.getValue());
    assertEquals("ruleflow-group", att.getName());
}
Also used : RuleDescr(org.drools.drl.ast.descr.RuleDescr) AttributeDescr(org.drools.drl.ast.descr.AttributeDescr) Test(org.junit.Test)

Aggregations

AttributeDescr (org.drools.drl.ast.descr.AttributeDescr)34 RuleDescr (org.drools.drl.ast.descr.RuleDescr)26 Test (org.junit.Test)21 PackageDescr (org.drools.drl.ast.descr.PackageDescr)13 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)7 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)6 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)6 RuleBuildContext (org.drools.compiler.rule.builder.RuleBuildContext)5 DialectCompiletimeRegistry (org.drools.compiler.compiler.DialectCompiletimeRegistry)4 HashSet (java.util.HashSet)3 List (java.util.List)3 Pattern (org.drools.core.rule.Pattern)3 AndDescr (org.drools.drl.ast.descr.AndDescr)3 FunctionImportDescr (org.drools.drl.ast.descr.FunctionImportDescr)3 ImportDescr (org.drools.drl.ast.descr.ImportDescr)3 ArrayList (java.util.ArrayList)2 Calendar (java.util.Calendar)2 HashMap (java.util.HashMap)2 KnowledgeBuilderConfigurationImpl (org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl)2 PackageRegistry (org.drools.compiler.compiler.PackageRegistry)2