Search in sources :

Example 6 with RuleDescr

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

the class XmlDumper method processRules.

private String processRules(final List rules) {
    String ruleList = "";
    for (final Iterator iterator = rules.iterator(); iterator.hasNext(); ) {
        final RuleDescr ruleDescr = (RuleDescr) iterator.next();
        String rule = "<rule name=\"" + ruleDescr.getName() + "\">" + XmlDumper.eol;
        final String attribute = processAttribute(ruleDescr.getAttributes().values());
        String lhs = "";
        if (ruleDescr.getLhs().getDescrs() != Collections.EMPTY_LIST) {
            lhs = "<lhs>" + processDescrList(ruleDescr.getLhs().getDescrs()) + "</lhs>";
        } else {
            lhs = "<lhs> </lhs>";
        }
        final String rhs = "<rhs>" + replaceIllegalChars((String) ruleDescr.getConsequence()) + "</rhs>" + XmlDumper.eol;
        rule += attribute;
        rule += lhs;
        rule += rhs;
        rule += "</rule>";
        ruleList += rule;
    }
    return ruleList + XmlDumper.eol;
}
Also used : Iterator(java.util.Iterator) RuleDescr(org.drools.compiler.lang.descr.RuleDescr)

Example 7 with RuleDescr

use of org.drools.compiler.lang.descr.RuleDescr 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.compiler.lang.descr.RuleDescr) IntervalTimer(org.drools.core.time.impl.IntervalTimer) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) AttributeDescr(org.drools.compiler.lang.descr.AttributeDescr) Test(org.junit.Test)

Example 8 with RuleDescr

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

the class RuleBuilderTest method testBuildMetaAttributes.

@Test
public void testBuildMetaAttributes() 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.addAnnotation("ruleId", "123");
    ruleDescr.addAnnotation("author", "Bob Doe");
    ruleDescr.addAnnotation("text", "\"It's a quoted\\\" string\"");
    // creates expected results
    // defining expectations on the mock object
    when(context.getRule()).thenReturn(rule);
    when(context.getRuleDescr()).thenReturn(ruleDescr);
    when(context.getKnowledgeBuilder()).thenReturn(new KnowledgeBuilderImpl());
    // calling the build method
    RuleBuilder.buildMetaAttributes(context);
    // check expectations
    verify(rule).addMetaAttribute("ruleId", 123);
    verify(rule).addMetaAttribute("author", "Bob Doe");
    verify(rule).addMetaAttribute("text", "It's a quoted\" string");
}
Also used : RuleBuildContext(org.drools.compiler.rule.builder.RuleBuildContext) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) Test(org.junit.Test)

Example 9 with RuleDescr

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

the class RuleBuilderTest method testBuildBigDecimalLiteralConstraint.

@Test
public void testBuildBigDecimalLiteralConstraint() throws Exception {
    final PackageDescr pkgDescr = new PackageDescr("org.drools");
    final RuleDescr ruleDescr = new RuleDescr("Test Rule");
    AndDescr andDescr = new AndDescr();
    PatternDescr patDescr = new PatternDescr("java.math.BigDecimal", "$bd");
    ExprConstraintDescr fcd = new ExprConstraintDescr("this == 10");
    patDescr.addConstraint(fcd);
    andDescr.addDescr(patDescr);
    ruleDescr.setLhs(andDescr);
    ruleDescr.setConsequence("");
    pkgDescr.addRule(ruleDescr);
    final KnowledgeBuilderImpl kBuilder = new KnowledgeBuilderImpl();
    kBuilder.addPackage(pkgDescr);
    assertTrue(kBuilder.getErrors().toString(), kBuilder.getErrors().isEmpty());
    final RuleImpl rule = kBuilder.getPackages()[0].getRule("Test Rule");
    final GroupElement and = rule.getLhs();
    final Pattern pat = (Pattern) and.getChildren().get(0);
    if (pat.getConstraints().get(0) instanceof MvelConstraint) {
        final MvelConstraint fc = (MvelConstraint) pat.getConstraints().get(0);
        assertTrue("Wrong class. Expected java.math.BigDecimal. Found: " + fc.getField().getValue().getClass(), fc.getField().getValue() instanceof BigDecimal);
    }
}
Also used : Pattern(org.drools.core.rule.Pattern) PatternDescr(org.drools.compiler.lang.descr.PatternDescr) AndDescr(org.drools.compiler.lang.descr.AndDescr) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) GroupElement(org.drools.core.rule.GroupElement) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) MvelConstraint(org.drools.core.rule.constraint.MvelConstraint) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) ExprConstraintDescr(org.drools.compiler.lang.descr.ExprConstraintDescr) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 10 with RuleDescr

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

the class RuleBuilderTest method testBuild.

@Test
public void testBuild() throws Exception {
    final DrlParser parser = new DrlParser(LanguageLevelOption.DRL5);
    final KnowledgeBuilderImpl kBuilder = new KnowledgeBuilderImpl();
    kBuilder.addPackage(new PackageDescr("org.drools"));
    InternalKnowledgePackage pkg = kBuilder.getPackage("org.drools");
    final PackageDescr pkgDescr = parser.parse(new InputStreamReader(getClass().getResourceAsStream("nestedConditionalElements.drl")));
    // just checking there is no parsing errors
    assertFalse(parser.getErrors().toString(), parser.hasErrors());
    pkg.addGlobal("results", List.class);
    final RuleDescr ruleDescr = pkgDescr.getRules().get(0);
    final String ruleClassName = "RuleClassName.java";
    ruleDescr.setClassName(ruleClassName);
    ruleDescr.addAttribute(new AttributeDescr("dialect", "java"));
    kBuilder.addPackage(pkgDescr);
    assertTrue(kBuilder.getErrors().toString(), kBuilder.getErrors().isEmpty());
    final RuleImpl rule = kBuilder.getPackage("org.drools.compiler").getRule("test nested CEs");
    assertEquals("There should be 2 rule level declarations", 2, rule.getDeclarations().size());
    // second GE should be a not
    final GroupElement not = (GroupElement) rule.getLhs().getChildren().get(1);
    assertTrue(not.isNot());
    // not has no outer declarations
    assertTrue(not.getOuterDeclarations().isEmpty());
    assertEquals(1, not.getInnerDeclarations().size());
    assertTrue(not.getInnerDeclarations().keySet().contains("$state"));
    // second not
    final GroupElement not2 = (GroupElement) ((GroupElement) not.getChildren().get(0)).getChildren().get(1);
    assertTrue(not2.isNot());
    // not has no outer declarations
    assertTrue(not2.getOuterDeclarations().isEmpty());
    assertEquals(1, not2.getInnerDeclarations().size());
    assertTrue(not2.getInnerDeclarations().keySet().contains("$likes"));
}
Also used : InputStreamReader(java.io.InputStreamReader) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) GroupElement(org.drools.core.rule.GroupElement) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) DrlParser(org.drools.compiler.compiler.DrlParser) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) AttributeDescr(org.drools.compiler.lang.descr.AttributeDescr) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) Test(org.junit.Test)

Aggregations

RuleDescr (org.drools.compiler.lang.descr.RuleDescr)185 Test (org.junit.Test)145 PatternDescr (org.drools.compiler.lang.descr.PatternDescr)104 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)103 AndDescr (org.drools.compiler.lang.descr.AndDescr)54 ExprConstraintDescr (org.drools.compiler.lang.descr.ExprConstraintDescr)37 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)25 AttributeDescr (org.drools.compiler.lang.descr.AttributeDescr)24 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)22 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)20 RuleBuildContext (org.drools.compiler.rule.builder.RuleBuildContext)17 Cheese (org.drools.compiler.Cheese)16 AccumulateDescr (org.drools.compiler.lang.descr.AccumulateDescr)15 OrDescr (org.drools.compiler.lang.descr.OrDescr)14 CompositeObjectSinkAdapterTest (org.drools.core.reteoo.CompositeObjectSinkAdapterTest)14 GlobalDescr (org.drools.compiler.lang.descr.GlobalDescr)13 Pattern (org.drools.core.rule.Pattern)13 DialectCompiletimeRegistry (org.drools.compiler.compiler.DialectCompiletimeRegistry)12 List (java.util.List)11 BindingDescr (org.drools.compiler.lang.descr.BindingDescr)11