Search in sources :

Example 76 with RuleDescr

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

the class RuleParserTest method testAccumulateMultiPattern.

@Test
public void testAccumulateMultiPattern() throws Exception {
    final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "accumulate_multi_pattern.drl");
    assertEquals(1, pkg.getRules().size());
    final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
    assertEquals(1, rule.getLhs().getDescrs().size());
    final PatternDescr outPattern = (PatternDescr) rule.getLhs().getDescrs().get(0);
    final AccumulateDescr accum = (AccumulateDescr) outPattern.getSource();
    assertEqualsIgnoreWhitespace("$counter", outPattern.getIdentifier());
    assertEqualsIgnoreWhitespace("int x = 0 ;", accum.getInitCode());
    assertEqualsIgnoreWhitespace("x++;", accum.getActionCode());
    assertEqualsIgnoreWhitespace("new Integer(x)", accum.getResultCode());
    final AndDescr and = (AndDescr) accum.getInput();
    assertEquals(2, and.getDescrs().size());
    final PatternDescr person = (PatternDescr) and.getDescrs().get(0);
    final PatternDescr cheese = (PatternDescr) and.getDescrs().get(1);
    assertEquals("Person", person.getObjectType());
    assertEquals("Cheese", cheese.getObjectType());
}
Also used : PatternDescr(org.drools.drl.ast.descr.PatternDescr) AndDescr(org.drools.drl.ast.descr.AndDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) PackageDescr(org.drools.drl.ast.descr.PackageDescr) AccumulateDescr(org.drools.drl.ast.descr.AccumulateDescr) Test(org.junit.Test)

Example 77 with RuleDescr

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

Example 78 with RuleDescr

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

the class RuleBuilder method buildAttributes.

public static void buildAttributes(final RuleBuildContext context) {
    final RuleImpl rule = context.getRule();
    final RuleDescr ruleDescr = context.getRuleDescr();
    boolean enforceEager = false;
    for (final AttributeDescr attributeDescr : ruleDescr.getAttributes().values()) {
        final String name = attributeDescr.getName();
        switch(name) {
            case "no-loop":
                rule.setNoLoop(getBooleanValue(attributeDescr, true));
                enforceEager = true;
                break;
            case "auto-focus":
                rule.setAutoFocus(getBooleanValue(attributeDescr, true));
                break;
            case "agenda-group":
                if (StringUtils.isEmpty(rule.getRuleFlowGroup())) {
                    // don't override if RFG has already set this
                    rule.setAgendaGroup(attributeDescr.getValue());
                } else {
                    if (rule.getRuleFlowGroup().equals(attributeDescr.getValue())) {
                        DroolsWarning warn = new RuleBuildWarning(rule, context.getParentDescr(), null, "Both an agenda-group ( " + attributeDescr.getValue() + " ) and a ruleflow-group ( " + rule.getRuleFlowGroup() + " ) are defined for rule " + rule.getName() + ". Since version 6.x the " + "two concepts have been unified, the ruleflow-group name will override the agenda-group. ");
                        context.addWarning(warn);
                    }
                }
                break;
            case "activation-group":
                rule.setActivationGroup(attributeDescr.getValue());
                break;
            case "ruleflow-group":
                rule.setRuleFlowGroup(attributeDescr.getValue());
                if (!rule.getAgendaGroup().equals(AgendaGroup.MAIN) && !rule.getAgendaGroup().equals(attributeDescr.getValue())) {
                    DroolsWarning warn = new RuleBuildWarning(rule, context.getParentDescr(), null, "Both an agenda-group ( " + attributeDescr.getValue() + " ) and a ruleflow-group ( " + rule.getRuleFlowGroup() + " ) are defined for rule " + rule.getName() + ". Since version 6.x the " + "two concepts have been unified, the ruleflow-group name will override the agenda-group. ");
                    context.addWarning(warn);
                }
                // assign AG to the same name as RFG, as they are aliased to AGs anyway
                rule.setAgendaGroup(attributeDescr.getValue());
                break;
            case "lock-on-active":
                boolean lockOnActive = getBooleanValue(attributeDescr, true);
                rule.setLockOnActive(lockOnActive);
                enforceEager |= lockOnActive;
                break;
            case DroolsSoftKeywords.DURATION:
            case DroolsSoftKeywords.TIMER:
                String duration = attributeDescr.getValue();
                rule.setTimer(buildTimer(rule, duration, context));
                break;
            case "calendars":
                buildCalendars(rule, attributeDescr.getValue(), context);
                break;
            case "date-effective":
                try {
                    Date date = DateUtils.parseDate(attributeDescr.getValue());
                    final Calendar cal = Calendar.getInstance();
                    cal.setTime(date);
                    rule.setDateEffective(cal);
                } catch (Exception e) {
                    DroolsError err = new RuleBuildError(rule, context.getParentDescr(), null, "Wrong date-effective value: " + e.getMessage());
                    context.addError(err);
                }
                break;
            case "date-expires":
                try {
                    Date date = DateUtils.parseDate(attributeDescr.getValue());
                    final Calendar cal = Calendar.getInstance();
                    cal.setTime(date);
                    rule.setDateExpires(cal);
                } catch (Exception e) {
                    DroolsError err = new RuleBuildError(rule, context.getParentDescr(), null, "Wrong date-expires value: " + e.getMessage());
                    context.addError(err);
                }
                break;
        }
    }
    buildSalience(context);
    buildEnabled(context);
    parseAnnotation(context, rule, ruleDescr, enforceEager);
}
Also used : DroolsError(org.drools.drl.parser.DroolsError) Calendar(java.util.Calendar) RuleBuildError(org.drools.compiler.compiler.RuleBuildError) RuleDescr(org.drools.drl.ast.descr.RuleDescr) DroolsWarning(org.drools.compiler.compiler.DroolsWarning) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) RuleBuildWarning(org.drools.compiler.compiler.RuleBuildWarning) AttributeDescr(org.drools.drl.ast.descr.AttributeDescr) Date(java.util.Date) ParseException(java.text.ParseException)

Example 79 with RuleDescr

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

the class JavaRuleClassBuilder method buildRule.

/* (non-Javadoc)
     * @see org.kie.rule.builder.dialect.java.RuleClassBuilder#buildRule(org.kie.rule.builder.BuildContext, org.kie.rule.builder.dialect.java.BuildUtils, org.kie.lang.descr.RuleDescr)
     */
public String buildRule(final RuleBuildContext context) {
    // If there is no compiled code, return
    if (context.getMethods().isEmpty()) {
        return null;
    }
    final String lineSeparator = System.getProperty("line.separator");
    final StringBuilder buffer = new StringBuilder();
    buffer.append("package ").append(context.getPkg().getName()).append(";").append(lineSeparator);
    for (String s : context.getPkg().getImports().keySet()) {
        buffer.append("import ").append(s).append(";");
    }
    buffer.append("import ").append(KnowledgeHelper.class.getName()).append(";");
    for (String s : context.getPkg().getStaticImports()) {
        buffer.append("import static ").append(s).append(";");
    }
    buffer.append(lineSeparator);
    final RuleDescr ruleDescr = context.getRuleDescr();
    buffer.append("public class ").append(StringUtils.ucFirst(ruleDescr.getClassName())).append(" {").append(lineSeparator);
    buffer.append("    private static final long serialVersionUID = 510l;").append(lineSeparator);
    for (int i = 0, size = context.getMethods().size() - 1; i < size; i++) {
        buffer.append(context.getMethods().get(i)).append(lineSeparator);
    }
    final String[] lines = buffer.toString().split(lineSeparator, -1);
    ruleDescr.setConsequenceOffset(lines.length);
    buffer.append(context.getMethods().get(context.getMethods().size() - 1)).append(lineSeparator);
    buffer.append("}");
    return buffer.toString();
}
Also used : RuleDescr(org.drools.drl.ast.descr.RuleDescr)

Example 80 with RuleDescr

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

the class MVELDialect method addRule.

public void addRule(RuleBuildContext context) {
    // MVEL: Compiler change
    final RuleDescr ruleDescr = context.getRuleDescr();
    // setup the line mappins for this rule
    final String name = this.pkg.getName() + "." + StringUtils.ucFirst(ruleDescr.getClassName());
    final LineMappings mapping = new LineMappings(name);
    mapping.setStartLine(ruleDescr.getConsequenceLine());
    mapping.setOffset(ruleDescr.getConsequenceOffset());
    context.getPkg().getDialectRuntimeRegistry().getLineMappings().put(name, mapping);
}
Also used : LineMappings(org.drools.core.rule.LineMappings) RuleDescr(org.drools.drl.ast.descr.RuleDescr)

Aggregations

RuleDescr (org.drools.drl.ast.descr.RuleDescr)191 Test (org.junit.Test)147 PackageDescr (org.drools.drl.ast.descr.PackageDescr)105 PatternDescr (org.drools.drl.ast.descr.PatternDescr)104 AndDescr (org.drools.drl.ast.descr.AndDescr)55 ExprConstraintDescr (org.drools.drl.ast.descr.ExprConstraintDescr)37 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)33 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)27 AttributeDescr (org.drools.drl.ast.descr.AttributeDescr)27 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)24 RuleBuildContext (org.drools.compiler.rule.builder.RuleBuildContext)17 Cheese (org.drools.mvel.compiler.Cheese)16 AccumulateDescr (org.drools.drl.ast.descr.AccumulateDescr)15 GlobalDescr (org.drools.drl.ast.descr.GlobalDescr)14 OrDescr (org.drools.drl.ast.descr.OrDescr)14 DialectCompiletimeRegistry (org.drools.compiler.compiler.DialectCompiletimeRegistry)13 Pattern (org.drools.core.rule.Pattern)13 List (java.util.List)12 BindingDescr (org.drools.drl.ast.descr.BindingDescr)11 FromDescr (org.drools.drl.ast.descr.FromDescr)11