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();
}
}
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" });
}
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());
}
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());
}
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());
}
Aggregations