Search in sources :

Example 26 with AttributeDescr

use of org.drools.compiler.lang.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.compiler.lang.descr.RuleDescr) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) AttributeDescr(org.drools.compiler.lang.descr.AttributeDescr) Test(org.junit.Test)

Example 27 with AttributeDescr

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

the class MVELConsequenceBuilderTest method setupTest.

private void setupTest(String consequence, Map<String, Object> namedConsequences) {
    builder = new MVELConsequenceBuilder();
    InternalKnowledgePackage pkg = new KnowledgePackageImpl("org.drools.compiler.test");
    pkg.addImport(new ImportDeclaration(Cheese.class.getCanonicalName()));
    KnowledgeBuilderConfigurationImpl conf = new KnowledgeBuilderConfigurationImpl();
    KnowledgeBuilderImpl pkgBuilder = new KnowledgeBuilderImpl(pkg, conf);
    ruleDescr = new RuleDescr("test consequence builder");
    ruleDescr.setConsequence(consequence);
    ruleDescr.addAttribute(new AttributeDescr("dialect", "mvel"));
    for (Entry<String, Object> entry : namedConsequences.entrySet()) {
        ruleDescr.addNamedConsequences(entry.getKey(), entry.getValue());
    }
    RuleImpl rule = new RuleImpl(ruleDescr.getName());
    rule.addPattern(new Pattern(0, new ClassObjectType(Cheese.class), "$cheese"));
    rule.addPattern(new Pattern(0, new ClassObjectType(Map.class), "$map"));
    PackageRegistry pkgRegistry = pkgBuilder.getPackageRegistry(pkg.getName());
    DialectCompiletimeRegistry reg = pkgBuilder.getPackageRegistry(pkg.getName()).getDialectCompiletimeRegistry();
    context = new RuleBuildContext(pkgBuilder, ruleDescr, reg, pkg, reg.getDialect(pkgRegistry.getDialect()));
    context.getDeclarationResolver().pushOnBuildStack(rule.getLhs());
    context.getDialect().getConsequenceBuilder().build(context, RuleImpl.DEFAULT_CONSEQUENCE_NAME);
    for (String name : namedConsequences.keySet()) {
        context.getDialect().getConsequenceBuilder().build(context, name);
    }
    context.getDialect().addRule(context);
    pkgRegistry.getPackage().addRule(context.getRule());
    pkgBuilder.compileAll();
    pkgBuilder.reloadAll();
    if (pkgBuilder.hasErrors()) {
        fail(pkgBuilder.getErrors().toString());
    }
}
Also used : Pattern(org.drools.core.rule.Pattern) ClassObjectType(org.drools.core.base.ClassObjectType) RuleBuildContext(org.drools.compiler.rule.builder.RuleBuildContext) KnowledgeBuilderConfigurationImpl(org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl) DialectCompiletimeRegistry(org.drools.compiler.compiler.DialectCompiletimeRegistry) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) PackageRegistry(org.drools.compiler.compiler.PackageRegistry) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) ImportDeclaration(org.drools.core.rule.ImportDeclaration) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) KnowledgePackageImpl(org.drools.core.definitions.impl.KnowledgePackageImpl) AttributeDescr(org.drools.compiler.lang.descr.AttributeDescr) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 28 with AttributeDescr

use of org.drools.compiler.lang.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.compiler.lang.descr.RuleDescr) AttributeDescr(org.drools.compiler.lang.descr.AttributeDescr) Test(org.junit.Test)

Example 29 with AttributeDescr

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

the class RuleParserTest method testEnabledExpression.

@Test
public void testEnabledExpression() throws Exception {
    final RuleDescr rule = (RuleDescr) parseResource("rule", "rule_enabled_expression.drl");
    assertEquals("simple_rule", rule.getName());
    assertEqualsIgnoreWhitespace("bar();", (String) rule.getConsequence());
    final Map<String, AttributeDescr> attrs = rule.getAttributes();
    assertEquals(3, attrs.size());
    AttributeDescr at = (AttributeDescr) attrs.get("enabled");
    assertEquals("enabled", at.getName());
    assertEquals("( 1 + 1 == 2 )", at.getValue());
    at = (AttributeDescr) attrs.get("salience");
    assertEquals("salience", at.getName());
    assertEquals("( 1+2 )", at.getValue());
    at = (AttributeDescr) attrs.get("lock-on-active");
    assertEquals("lock-on-active", at.getName());
    assertEquals("true", at.getValue());
}
Also used : RuleDescr(org.drools.compiler.lang.descr.RuleDescr) AttributeDescr(org.drools.compiler.lang.descr.AttributeDescr) Test(org.junit.Test)

Example 30 with AttributeDescr

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

the class RuleParserTest method testDurationExpression.

@Test
public void testDurationExpression() throws Exception {
    final RuleDescr rule = (RuleDescr) parseResource("rule", "rule_duration_expression.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("duration");
    assertEquals("duration", at.getName());
    assertEquals("1h30m", at.getValue());
    at = (AttributeDescr) attrs.get("lock-on-active");
    assertEquals("lock-on-active", at.getName());
    assertEquals("true", at.getValue());
}
Also used : RuleDescr(org.drools.compiler.lang.descr.RuleDescr) AttributeDescr(org.drools.compiler.lang.descr.AttributeDescr) Test(org.junit.Test)

Aggregations

AttributeDescr (org.drools.compiler.lang.descr.AttributeDescr)35 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)23 Test (org.junit.Test)20 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)11 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)6 RuleBuildContext (org.drools.compiler.rule.builder.RuleBuildContext)5 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)5 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)5 RecognitionException (org.antlr.runtime.RecognitionException)3 DialectCompiletimeRegistry (org.drools.compiler.compiler.DialectCompiletimeRegistry)3 InputStreamReader (java.io.InputStreamReader)2 ArrayList (java.util.ArrayList)2 Calendar (java.util.Calendar)2 HashMap (java.util.HashMap)2 KnowledgeBuilderConfigurationImpl (org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl)2 DrlParser (org.drools.compiler.compiler.DrlParser)2 DroolsError (org.drools.compiler.compiler.DroolsError)2 AndDescr (org.drools.compiler.lang.descr.AndDescr)2 FunctionImportDescr (org.drools.compiler.lang.descr.FunctionImportDescr)2 ImportDescr (org.drools.compiler.lang.descr.ImportDescr)2