Search in sources :

Example 6 with AttributeDescr

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

the class RuleParserTest method testCalendars2.

@Test
public void testCalendars2() throws Exception {
    final RuleDescr rule = (RuleDescr) parseResource("rule", "rule_calendars_attribute2.drl");
    assertFalse(parser.getErrors().toString(), parser.hasErrors());
    assertEquals("simple_rule", rule.getName());
    assertEqualsIgnoreWhitespace("bar();", (String) rule.getConsequence());
    final Map<String, AttributeDescr> attrs = rule.getAttributes();
    assertEquals(2, attrs.size());
    AttributeDescr at = (AttributeDescr) attrs.get("calendars");
    assertEquals("calendars", at.getName());
    assertEquals("[ \"cal 1\", \"cal 2\", \"cal 3\" ]", at.getValue());
    at = (AttributeDescr) attrs.get("lock-on-active");
    assertEquals("lock-on-active", at.getName());
    assertEquals("true", at.getValue());
}
Also used : RuleDescr(org.drools.drl.ast.descr.RuleDescr) AttributeDescr(org.drools.drl.ast.descr.AttributeDescr) Test(org.junit.Test)

Example 7 with AttributeDescr

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

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

the class PackageBuilderConfigurationTest method testMockDialect.

@Test
public void testMockDialect() {
    InternalKnowledgePackage pkg = CoreComponentFactory.get().createKnowledgePackage("org.pkg1");
    KnowledgeBuilderConfigurationImpl cfg1 = new KnowledgeBuilderConfigurationImpl();
    MockDialectConfiguration mockConf = new MockDialectConfiguration();
    // cfg1.buildDialectRegistry().addDialect( "mock",
    // mockConf.getDialect() );
    cfg1.addDialect("mock", mockConf);
    cfg1.setDefaultDialect("mock");
    KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl(pkg, cfg1);
    PackageRegistry pkgRegistry = builder.getPackageRegistry(pkg.getName());
    DialectCompiletimeRegistry dialectRegistry = pkgRegistry.getDialectCompiletimeRegistry();
    MockDialect mockDialect2 = (MockDialect) dialectRegistry.getDialect(cfg1.getDefaultDialect());
    assertSame(pkg, mockDialect2.getPkg());
    assertNull(mockDialect2.getRuleDescr());
    RuleDescr ruleDescr = new RuleDescr("test rule");
    ruleDescr.addAttribute(new AttributeDescr("dialect", "mock"));
    ruleDescr.setLhs(new AndDescr());
    EvalDescr evalDescr = new EvalDescr();
    ruleDescr.getLhs().addDescr(evalDescr);
    PackageDescr pkgDescr = new PackageDescr("org.pkg1");
    pkgDescr.addImport(new ImportDescr("java.util.HashMap"));
    FunctionImportDescr functionImportDescr = new FunctionImportDescr();
    functionImportDescr.setTarget("java.lang.System.currentTimeMillis");
    pkgDescr.addFunctionImport(functionImportDescr);
    pkgDescr.addRule(ruleDescr);
    builder.addPackage(pkgDescr);
    assertSame(ruleDescr, mockDialect2.getRuleDescr());
    assertTrue(mockDialect2.getImport().contains("java.util.HashMap"));
    assertTrue(mockDialect2.getStaticImport().contains("java.lang.System.currentTimeMillis"));
    assertEquals("eval was built", evalDescr.getContent());
    assertEquals("consequence was built", ruleDescr.getConsequence());
    assertTrue(mockDialect2.isCompileAll());
    assertNotNull(pkg.getRule("test rule"));
    // make sure there were no other general errors.
    assertFalse(builder.hasErrors());
}
Also used : FunctionImportDescr(org.drools.drl.ast.descr.FunctionImportDescr) KnowledgeBuilderConfigurationImpl(org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl) DialectCompiletimeRegistry(org.drools.compiler.compiler.DialectCompiletimeRegistry) AndDescr(org.drools.drl.ast.descr.AndDescr) ImportDescr(org.drools.drl.ast.descr.ImportDescr) FunctionImportDescr(org.drools.drl.ast.descr.FunctionImportDescr) PackageRegistry(org.drools.compiler.compiler.PackageRegistry) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) EvalDescr(org.drools.drl.ast.descr.EvalDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) PackageDescr(org.drools.drl.ast.descr.PackageDescr) AttributeDescr(org.drools.drl.ast.descr.AttributeDescr) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) Test(org.junit.Test)

Example 9 with AttributeDescr

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

the class RuleParserTest method testCalendars.

@Test
public void testCalendars() throws Exception {
    final RuleDescr rule = (RuleDescr) parseResource("rule", "rule_calendars_attribute.drl");
    assertEquals("simple_rule", rule.getName());
    assertEqualsIgnoreWhitespace("bar();", (String) rule.getConsequence());
    final Map<String, AttributeDescr> attrs = rule.getAttributes();
    assertEquals(2, attrs.size());
    AttributeDescr at = (AttributeDescr) attrs.get("calendars");
    assertEquals("calendars", at.getName());
    assertEquals("[ \"cal1\" ]", at.getValue());
    at = (AttributeDescr) attrs.get("lock-on-active");
    assertEquals("lock-on-active", at.getName());
    assertEquals("true", at.getValue());
}
Also used : RuleDescr(org.drools.drl.ast.descr.RuleDescr) AttributeDescr(org.drools.drl.ast.descr.AttributeDescr) Test(org.junit.Test)

Example 10 with AttributeDescr

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

the class RuleParserTest method testNoLoop.

@Test
public void testNoLoop() throws Exception {
    final RuleDescr rule = (RuleDescr) parseResource("rule", "no-loop.drl");
    assertFalse(parser.getErrors().toString(), parser.hasErrors());
    assertNotNull(rule);
    assertEquals("rule1", rule.getName());
    final AttributeDescr att = (AttributeDescr) rule.getAttributes().get("no-loop");
    assertEquals("false", att.getValue());
    assertEquals("no-loop", 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