Search in sources :

Example 21 with AttributeDescr

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

the class ModelGenerator method ruleAttributes.

/**
 * Build a list of method calls, representing each needed {@link org.drools.model.impl.RuleBuilder#attribute(org.drools.model.Rule.Attribute, Object)}
 * starting from a drools-compiler {@link RuleDescr}.
 * The tuple represent the Rule Attribute expressed in JavParser form, and the attribute value expressed in JavaParser form.
 */
private static List<MethodCallExpr> ruleAttributes(RuleContext context, RuleDescr ruleDescr) {
    List<MethodCallExpr> ruleAttributes = new ArrayList<>();
    for (Entry<String, AttributeDescr> as : ruleDescr.getAttributes().entrySet()) {
        // dialect=mvel is not an attribute of DSL expr(), so we check it before.
        if (as.getKey().equals("dialect")) {
            if (as.getValue().getValue().equals("mvel")) {
                context.setRuleDialect(RuleDialect.MVEL);
            }
            continue;
        }
        MethodCallExpr attributeCall = new MethodCallExpr(null, ATTRIBUTE_CALL);
        attributeCall.addArgument(attributesMap.get(as.getKey()));
        switch(as.getKey()) {
            case "dialect":
                throw new RuntimeException("should not have reached this part of the code");
            case "no-loop":
            case "salience":
            case "enabled":
            case "auto-focus":
            case "lock-on-active":
                attributeCall.addArgument(parseExpression(as.getValue().getValue()));
                break;
            case "agenda-group":
            case "activation-group":
            case "ruleflow-group":
            case "duration":
            case "timer":
                attributeCall.addArgument(new StringLiteralExpr(as.getValue().getValue()));
                break;
            case "calendars":
                String value = as.getValue().getValue().trim();
                if (value.startsWith("[")) {
                    value = value.substring(1, value.length() - 1).trim();
                }
                Expression arrayExpr = parseExpression("new String[] { " + value + " }");
                attributeCall.addArgument(arrayExpr);
                break;
            case "date-effective":
            case "date-expires":
                attributeCall.addArgument(parseExpression(String.format("GregorianCalendar.from(LocalDate.parse(\"%s\", dateTimeFormatter).atStartOfDay(ZoneId.systemDefault()))", as.getValue().getValue())));
                break;
            default:
                throw new UnsupportedOperationException("Unhandled case for rule attribute: " + as.getKey());
        }
        ruleAttributes.add(attributeCall);
    }
    return ruleAttributes;
}
Also used : Expression(org.drools.javaparser.ast.expr.Expression) JavaParser.parseExpression(org.drools.javaparser.JavaParser.parseExpression) ArrayList(java.util.ArrayList) StringLiteralExpr(org.drools.javaparser.ast.expr.StringLiteralExpr) AttributeDescr(org.drools.compiler.lang.descr.AttributeDescr) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr)

Example 22 with AttributeDescr

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

the class XmlDumper method processAttribute.

private String processAttribute(final Collection<AttributeDescr> attributes) {
    String attributeList = "";
    for (final AttributeDescr attributeDescr : attributes) {
        visit(attributeDescr);
        attributeList += this.template;
    }
    return attributeList + XmlDumper.eol;
}
Also used : AttributeDescr(org.drools.compiler.lang.descr.AttributeDescr)

Example 23 with AttributeDescr

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

the class DRL6StrictParser method attribute.

/**
 * attribute :=
 *       salience
 *   |   enabled
 *   |   ( NO-LOOP
 *       | AUTO-FOCUS
 *       | LOCK-ON-ACTIVE
 *       | REFRACT
 *       | DIRECT
 *       ) BOOLEAN?
 *   |   ( AGENDA-GROUP
 *       | ACTIVATION-GROUP
 *       | RULEFLOW-GROUP
 *       | DATE-EFFECTIVE
 *       | DATE-EXPIRES
 *       | DIALECT
 *       ) STRING
 *   |   CALENDARS STRING (COMMA STRING)*
 *   |   TIMER ( DECIMAL | chunk_(_) )
 *   |   DURATION ( DECIMAL | chunk_(_) )
 *
 * The above syntax is not quite how this is parsed, because the soft keyword
 * is determined by look-ahead and passed on to one of the x-Attribute methods
 * (booleanAttribute, stringAttribute, stringListAttribute, intOrChunkAttribute)
 * which will actually gobble the tokens.
 *
 * @return
 */
public AttributeDescr attribute(AttributeSupportBuilder<?> as) {
    AttributeDescr attribute = null;
    try {
        if (state.backtracking == 0 && input.LA(1) != DRL6Lexer.EOF) {
            helper.emit(Location.LOCATION_RULE_HEADER_KEYWORD);
        }
        if (helper.validateIdentifierKey(DroolsSoftKeywords.SALIENCE)) {
            attribute = salience(as);
        } else if (helper.validateIdentifierKey(DroolsSoftKeywords.ENABLED)) {
            attribute = enabled(as);
        } else if (helper.validateIdentifierKey(DroolsSoftKeywords.NO) && helper.validateLT(2, "-") && helper.validateLT(3, DroolsSoftKeywords.LOOP)) {
            attribute = booleanAttribute(as, new String[] { DroolsSoftKeywords.NO, "-", DroolsSoftKeywords.LOOP });
        } else if (helper.validateIdentifierKey(DroolsSoftKeywords.AUTO) && helper.validateLT(2, "-") && helper.validateLT(3, DroolsSoftKeywords.FOCUS)) {
            attribute = booleanAttribute(as, new String[] { DroolsSoftKeywords.AUTO, "-", DroolsSoftKeywords.FOCUS });
        } else if (helper.validateIdentifierKey(DroolsSoftKeywords.LOCK) && helper.validateLT(2, "-") && helper.validateLT(3, DroolsSoftKeywords.ON) && helper.validateLT(4, "-") && helper.validateLT(5, DroolsSoftKeywords.ACTIVE)) {
            attribute = booleanAttribute(as, new String[] { DroolsSoftKeywords.LOCK, "-", DroolsSoftKeywords.ON, "-", DroolsSoftKeywords.ACTIVE });
        } else if (helper.validateIdentifierKey(DroolsSoftKeywords.REFRACT)) {
            attribute = booleanAttribute(as, new String[] { DroolsSoftKeywords.REFRACT });
        } else if (helper.validateIdentifierKey(DroolsSoftKeywords.DIRECT)) {
            attribute = booleanAttribute(as, new String[] { DroolsSoftKeywords.DIRECT });
        } else if (helper.validateIdentifierKey(DroolsSoftKeywords.AGENDA) && helper.validateLT(2, "-") && helper.validateLT(3, DroolsSoftKeywords.GROUP)) {
            attribute = stringAttribute(as, new String[] { DroolsSoftKeywords.AGENDA, "-", DroolsSoftKeywords.GROUP });
        } else if (helper.validateIdentifierKey(DroolsSoftKeywords.ACTIVATION) && helper.validateLT(2, "-") && helper.validateLT(3, DroolsSoftKeywords.GROUP)) {
            attribute = stringAttribute(as, new String[] { DroolsSoftKeywords.ACTIVATION, "-", DroolsSoftKeywords.GROUP });
        } else if (helper.validateIdentifierKey(DroolsSoftKeywords.RULEFLOW) && helper.validateLT(2, "-") && helper.validateLT(3, DroolsSoftKeywords.GROUP)) {
            attribute = stringAttribute(as, new String[] { DroolsSoftKeywords.RULEFLOW, "-", DroolsSoftKeywords.GROUP });
        } else if (helper.validateIdentifierKey(DroolsSoftKeywords.DATE) && helper.validateLT(2, "-") && helper.validateLT(3, DroolsSoftKeywords.EFFECTIVE)) {
            attribute = stringAttribute(as, new String[] { DroolsSoftKeywords.DATE, "-", DroolsSoftKeywords.EFFECTIVE });
            attribute.setType(AttributeDescr.Type.DATE);
        } else if (helper.validateIdentifierKey(DroolsSoftKeywords.DATE) && helper.validateLT(2, "-") && helper.validateLT(3, DroolsSoftKeywords.EXPIRES)) {
            attribute = stringAttribute(as, new String[] { DroolsSoftKeywords.DATE, "-", DroolsSoftKeywords.EXPIRES });
            attribute.setType(AttributeDescr.Type.DATE);
        } else if (helper.validateIdentifierKey(DroolsSoftKeywords.DIALECT)) {
            attribute = stringAttribute(as, new String[] { DroolsSoftKeywords.DIALECT });
        } else if (helper.validateIdentifierKey(DroolsSoftKeywords.CALENDARS)) {
            attribute = stringListAttribute(as, new String[] { DroolsSoftKeywords.CALENDARS });
        } else if (helper.validateIdentifierKey(DroolsSoftKeywords.TIMER)) {
            attribute = intOrChunkAttribute(as, new String[] { DroolsSoftKeywords.TIMER });
        } else if (helper.validateIdentifierKey(DroolsSoftKeywords.DURATION)) {
            attribute = intOrChunkAttribute(as, new String[] { DroolsSoftKeywords.DURATION });
        }
        if (state.backtracking == 0) {
            helper.emit(Location.LOCATION_RULE_HEADER);
        }
    } catch (RecognitionException re) {
        reportError(re);
    }
    return attribute;
}
Also used : AttributeDescr(org.drools.compiler.lang.descr.AttributeDescr) RecognitionException(org.antlr.runtime.RecognitionException)

Example 24 with AttributeDescr

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

the class DRL5Parser method attribute.

/**
 * attribute :=
 *       salience
 *   |   enabled
 *   |   ( NO-LOOP
 *       | AUTO-FOCUS
 *       | LOCK-ON-ACTIVE
 *       | REFRACT
 *       ) BOOLEAN?
 *   |   ( AGENDA-GROUP
 *       | ACTIVATION-GROUP
 *       | RULEFLOW-GROUP
 *       | DATE-EFFECTIVE
 *       | DATE-EXPIRES
 *       | DIALECT
 *       ) STRING
 *   |   CALENDARS STRING (COMMA STRING)*
 *   |   TIMER ( DECIMAL | chunk_(_) )
 *   |   DURATION ( DECIMAL | chunk_(_) )
 *
 * The above syntax is not quite how this is parsed, because the soft keyword
 * is determined by look-ahead and passed on to one of the x-Attribute methods
 * (booleanAttribute, stringAttribute, stringListAttribute, intOrChunkAttribute)
 * which will actually gobble the tokens.
 *
 * @return
 */
public AttributeDescr attribute(AttributeSupportBuilder<?> as) {
    AttributeDescr attribute = null;
    try {
        if (state.backtracking == 0 && input.LA(1) != DRL5Lexer.EOF) {
            helper.emit(Location.LOCATION_RULE_HEADER_KEYWORD);
        }
        if (helper.validateIdentifierKey(DroolsSoftKeywords.SALIENCE)) {
            attribute = salience(as);
        } else if (helper.validateIdentifierKey(DroolsSoftKeywords.ENABLED)) {
            attribute = enabled(as);
        } else if (helper.validateIdentifierKey(DroolsSoftKeywords.NO) && helper.validateLT(2, "-") && helper.validateLT(3, DroolsSoftKeywords.LOOP)) {
            attribute = booleanAttribute(as, new String[] { DroolsSoftKeywords.NO, "-", DroolsSoftKeywords.LOOP });
        } else if (helper.validateIdentifierKey(DroolsSoftKeywords.AUTO) && helper.validateLT(2, "-") && helper.validateLT(3, DroolsSoftKeywords.FOCUS)) {
            attribute = booleanAttribute(as, new String[] { DroolsSoftKeywords.AUTO, "-", DroolsSoftKeywords.FOCUS });
        } else if (helper.validateIdentifierKey(DroolsSoftKeywords.LOCK) && helper.validateLT(2, "-") && helper.validateLT(3, DroolsSoftKeywords.ON) && helper.validateLT(4, "-") && helper.validateLT(5, DroolsSoftKeywords.ACTIVE)) {
            attribute = booleanAttribute(as, new String[] { DroolsSoftKeywords.LOCK, "-", DroolsSoftKeywords.ON, "-", DroolsSoftKeywords.ACTIVE });
        } else if (helper.validateIdentifierKey(DroolsSoftKeywords.REFRACT)) {
            attribute = booleanAttribute(as, new String[] { DroolsSoftKeywords.REFRACT });
        } else if (helper.validateIdentifierKey(DroolsSoftKeywords.AGENDA) && helper.validateLT(2, "-") && helper.validateLT(3, DroolsSoftKeywords.GROUP)) {
            attribute = stringAttribute(as, new String[] { DroolsSoftKeywords.AGENDA, "-", DroolsSoftKeywords.GROUP });
        } else if (helper.validateIdentifierKey(DroolsSoftKeywords.ACTIVATION) && helper.validateLT(2, "-") && helper.validateLT(3, DroolsSoftKeywords.GROUP)) {
            attribute = stringAttribute(as, new String[] { DroolsSoftKeywords.ACTIVATION, "-", DroolsSoftKeywords.GROUP });
        } else if (helper.validateIdentifierKey(DroolsSoftKeywords.RULEFLOW) && helper.validateLT(2, "-") && helper.validateLT(3, DroolsSoftKeywords.GROUP)) {
            attribute = stringAttribute(as, new String[] { DroolsSoftKeywords.RULEFLOW, "-", DroolsSoftKeywords.GROUP });
        } else if (helper.validateIdentifierKey(DroolsSoftKeywords.DATE) && helper.validateLT(2, "-") && helper.validateLT(3, DroolsSoftKeywords.EFFECTIVE)) {
            attribute = stringAttribute(as, new String[] { DroolsSoftKeywords.DATE, "-", DroolsSoftKeywords.EFFECTIVE });
            attribute.setType(AttributeDescr.Type.DATE);
        } else if (helper.validateIdentifierKey(DroolsSoftKeywords.DATE) && helper.validateLT(2, "-") && helper.validateLT(3, DroolsSoftKeywords.EXPIRES)) {
            attribute = stringAttribute(as, new String[] { DroolsSoftKeywords.DATE, "-", DroolsSoftKeywords.EXPIRES });
            attribute.setType(AttributeDescr.Type.DATE);
        } else if (helper.validateIdentifierKey(DroolsSoftKeywords.DIALECT)) {
            attribute = stringAttribute(as, new String[] { DroolsSoftKeywords.DIALECT });
        } else if (helper.validateIdentifierKey(DroolsSoftKeywords.CALENDARS)) {
            attribute = stringListAttribute(as, new String[] { DroolsSoftKeywords.CALENDARS });
        } else if (helper.validateIdentifierKey(DroolsSoftKeywords.TIMER)) {
            attribute = intOrChunkAttribute(as, new String[] { DroolsSoftKeywords.TIMER });
        } else if (helper.validateIdentifierKey(DroolsSoftKeywords.DURATION)) {
            attribute = intOrChunkAttribute(as, new String[] { DroolsSoftKeywords.DURATION });
        }
        if (state.backtracking == 0) {
            helper.emit(Location.LOCATION_RULE_HEADER);
        }
    } catch (RecognitionException re) {
        reportError(re);
    }
    return attribute;
}
Also used : AttributeDescr(org.drools.compiler.lang.descr.AttributeDescr) RecognitionException(org.antlr.runtime.RecognitionException)

Example 25 with AttributeDescr

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

the class RuleBuilderTest method testBuildAttributes.

@Test
public void testBuildAttributes() 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("no-loop", "true"));
    ruleDescr.addAttribute(new AttributeDescr("auto-focus", "false"));
    ruleDescr.addAttribute(new AttributeDescr("agenda-group", "my agenda"));
    ruleDescr.addAttribute(new AttributeDescr("activation-group", "my activation"));
    ruleDescr.addAttribute(new AttributeDescr("lock-on-active", ""));
    ruleDescr.addAttribute(new AttributeDescr("enabled", "false"));
    ruleDescr.addAttribute(new AttributeDescr("duration", "60"));
    ruleDescr.addAttribute(new AttributeDescr("calendars", "\"cal1\""));
    ruleDescr.addAttribute(new AttributeDescr("date-effective", "10-Jul-1974"));
    ruleDescr.addAttribute(new AttributeDescr("date-expires", "10-Jul-2040"));
    // creates expected results
    final Calendar effective = Calendar.getInstance();
    effective.setTime(DateUtils.parseDate("10-Jul-1974"));
    final Calendar expires = Calendar.getInstance();
    expires.setTime(DateUtils.parseDate("10-Jul-2040"));
    // 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.buildAttributes(context);
    // check expectations
    verify(rule).setNoLoop(true);
    verify(rule).setAutoFocus(false);
    verify(rule).setAgendaGroup("my agenda");
    verify(rule).setActivationGroup("my activation");
    verify(rule).setLockOnActive(true);
    verify(rule).setEnabled(EnabledBoolean.ENABLED_FALSE);
    verify(rule).setTimer(new IntervalTimer(null, null, -1, TimeUtils.parseTimeString("60"), 0));
    verify(rule).setCalendars(new String[] { "cal1" });
    verify(rule).setDateEffective(effective);
    verify(rule).setDateExpires(expires);
}
Also used : RuleBuildContext(org.drools.compiler.rule.builder.RuleBuildContext) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) Calendar(java.util.Calendar) 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)

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