Search in sources :

Example 11 with AttributeDescr

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

Example 12 with AttributeDescr

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

the class ParserHelper method start.

@SuppressWarnings("unchecked")
public <T extends DescrBuilder<?, ?>> T start(DescrBuilder<?, ?> ctxBuilder, Class<T> clazz, String param) {
    if (state.backtracking == 0) {
        if (PackageDescrBuilder.class.isAssignableFrom(clazz)) {
            pushParaphrases(DroolsParaphraseTypes.PACKAGE);
            beginSentence(DroolsSentenceType.PACKAGE);
            setStart(ctxBuilder);
        } else if (ImportDescrBuilder.class.isAssignableFrom(clazz)) {
            ImportDescrBuilder imp;
            if (validateLT(2, DroolsSoftKeywords.FUNCTION) || validateLT(2, DroolsSoftKeywords.STATIC)) {
                imp = ctxBuilder == null ? DescrFactory.newPackage().newFunctionImport() : ((PackageDescrBuilder) ctxBuilder).newFunctionImport();
            } else {
                imp = ctxBuilder == null ? DescrFactory.newPackage().newImport() : ((PackageDescrBuilder) ctxBuilder).newImport();
            }
            pushParaphrases(DroolsParaphraseTypes.IMPORT);
            beginSentence(DroolsSentenceType.IMPORT_STATEMENT);
            setStart(imp);
            return (T) imp;
        } else if (UnitDescrBuilder.class.isAssignableFrom(clazz)) {
            UnitDescrBuilder imp = ctxBuilder == null ? DescrFactory.newPackage().newUnit() : ((PackageDescrBuilder) ctxBuilder).newUnit();
            pushParaphrases(DroolsParaphraseTypes.UNIT);
            beginSentence(DroolsSentenceType.UNIT);
            setStart(imp);
            return (T) imp;
        } else if (AccumulateImportDescrBuilder.class.isAssignableFrom(clazz)) {
            AccumulateImportDescrBuilder imp = ctxBuilder == null ? DescrFactory.newPackage().newAccumulateImport() : ((PackageDescrBuilder) ctxBuilder).newAccumulateImport();
            pushParaphrases(DroolsParaphraseTypes.ACCUMULATE_IMPORT);
            beginSentence(DroolsSentenceType.ACCUMULATE_IMPORT_STATEMENT);
            setStart(imp);
            return (T) imp;
        } else if (GlobalDescrBuilder.class.isAssignableFrom(clazz)) {
            GlobalDescrBuilder global = ctxBuilder == null ? DescrFactory.newPackage().newGlobal() : ((PackageDescrBuilder) ctxBuilder).newGlobal();
            pushParaphrases(DroolsParaphraseTypes.GLOBAL);
            beginSentence(DroolsSentenceType.GLOBAL);
            setStart(global);
            return (T) global;
        } else if (DeclareDescrBuilder.class.isAssignableFrom(clazz)) {
            DeclareDescrBuilder declare = ctxBuilder == null ? DescrFactory.newPackage().newDeclare() : ((PackageDescrBuilder) ctxBuilder).newDeclare();
            return (T) declare;
        } else if (TypeDeclarationDescrBuilder.class.isAssignableFrom(clazz)) {
            TypeDeclarationDescrBuilder declare = ctxBuilder == null ? DescrFactory.newPackage().newDeclare().type() : ((DeclareDescrBuilder) ctxBuilder).type();
            pushParaphrases(DroolsParaphraseTypes.TYPE_DECLARE);
            beginSentence(DroolsSentenceType.TYPE_DECLARATION);
            setStart(declare);
            return (T) declare;
        } else if (EnumDeclarationDescrBuilder.class.isAssignableFrom(clazz)) {
            EnumDeclarationDescrBuilder declare = ctxBuilder == null ? DescrFactory.newPackage().newDeclare().enumerative() : ((DeclareDescrBuilder) ctxBuilder).enumerative();
            pushParaphrases(DroolsParaphraseTypes.ENUM_DECLARE);
            beginSentence(DroolsSentenceType.ENUM_DECLARATION);
            setStart(declare);
            return (T) declare;
        } else if (EntryPointDeclarationDescrBuilder.class.isAssignableFrom(clazz)) {
            EntryPointDeclarationDescrBuilder declare = ctxBuilder == null ? DescrFactory.newPackage().newDeclare().entryPoint() : ((DeclareDescrBuilder) ctxBuilder).entryPoint();
            pushParaphrases(DroolsParaphraseTypes.ENTRYPOINT_DECLARE);
            beginSentence(DroolsSentenceType.ENTRYPOINT_DECLARATION);
            setStart(declare);
            return (T) declare;
        } else if (WindowDeclarationDescrBuilder.class.isAssignableFrom(clazz)) {
            WindowDeclarationDescrBuilder declare = ctxBuilder == null ? DescrFactory.newPackage().newDeclare().window() : ((DeclareDescrBuilder) ctxBuilder).window();
            pushParaphrases(DroolsParaphraseTypes.WINDOW_DECLARE);
            beginSentence(DroolsSentenceType.WINDOW_DECLARATION);
            setStart(declare);
            return (T) declare;
        } else if (FieldDescrBuilder.class.isAssignableFrom(clazz)) {
            FieldDescrBuilder field = ((AbstractClassTypeDeclarationBuilder) ctxBuilder).newField(param);
            setStart(field);
            return (T) field;
        } else if (EnumLiteralDescrBuilder.class.isAssignableFrom(clazz)) {
            EnumLiteralDescrBuilder literal = ((EnumDeclarationDescrBuilder) ctxBuilder).newEnumLiteral(param);
            setStart(literal);
            return (T) literal;
        } else if (FunctionDescrBuilder.class.isAssignableFrom(clazz)) {
            FunctionDescrBuilder function;
            if (ctxBuilder == null) {
                function = DescrFactory.newPackage().newFunction();
            } else {
                PackageDescrBuilder pkg = (PackageDescrBuilder) ctxBuilder;
                function = pkg.newFunction().namespace(pkg.getDescr().getName());
                AttributeDescr attribute = pkg.getDescr().getAttribute("dialect");
                if (attribute != null) {
                    function.dialect(attribute.getValue());
                }
            }
            pushParaphrases(DroolsParaphraseTypes.FUNCTION);
            beginSentence(DroolsSentenceType.FUNCTION);
            setStart(function);
            return (T) function;
        } else if (RuleDescrBuilder.class.isAssignableFrom(clazz)) {
            RuleDescrBuilder rule = ctxBuilder == null ? DescrFactory.newPackage().newRule() : ((PackageDescrBuilder) ctxBuilder).newRule();
            pushParaphrases(DroolsParaphraseTypes.RULE);
            beginSentence(DroolsSentenceType.RULE);
            setStart(rule);
            return (T) rule;
        } else if (QueryDescrBuilder.class.isAssignableFrom(clazz)) {
            QueryDescrBuilder query = ctxBuilder == null ? DescrFactory.newPackage().newQuery() : ((PackageDescrBuilder) ctxBuilder).newQuery();
            pushParaphrases(DroolsParaphraseTypes.QUERY);
            beginSentence(DroolsSentenceType.QUERY);
            setStart(query);
            return (T) query;
        } else if (AttributeDescrBuilder.class.isAssignableFrom(clazz)) {
            AttributeDescrBuilder<?> attribute = ((AttributeSupportBuilder<?>) ctxBuilder).attribute(param);
            setStart(attribute);
            return (T) attribute;
        } else if (EvalDescrBuilder.class.isAssignableFrom(clazz)) {
            EvalDescrBuilder<?> eval = ((CEDescrBuilder<?, ?>) ctxBuilder).eval();
            pushParaphrases(DroolsParaphraseTypes.EVAL);
            beginSentence(DroolsSentenceType.EVAL);
            setStart(eval);
            return (T) eval;
        } else if (ForallDescrBuilder.class.isAssignableFrom(clazz)) {
            ForallDescrBuilder<?> forall = ((CEDescrBuilder<?, ?>) ctxBuilder).forall();
            setStart(forall);
            return (T) forall;
        } else if (CEDescrBuilder.class.isAssignableFrom(clazz)) {
            setStart(ctxBuilder);
            return (T) ctxBuilder;
        } else if (PatternDescrBuilder.class.isAssignableFrom(clazz)) {
            PatternDescrBuilder<?> pattern = ((PatternContainerDescrBuilder<?, ?>) ctxBuilder).pattern();
            pushParaphrases(DroolsParaphraseTypes.PATTERN);
            setStart(pattern);
            return (T) pattern;
        } else if (CollectDescrBuilder.class.isAssignableFrom(clazz)) {
            CollectDescrBuilder<?> collect = ((PatternDescrBuilder<?>) ctxBuilder).from().collect();
            setStart(collect);
            return (T) collect;
        } else if (AccumulateDescrBuilder.class.isAssignableFrom(clazz)) {
            AccumulateDescrBuilder<?> accumulate = ((PatternDescrBuilder<?>) ctxBuilder).from().accumulate();
            setStart(accumulate);
            return (T) accumulate;
        } else if (BehaviorDescrBuilder.class.isAssignableFrom(clazz)) {
            BehaviorDescrBuilder<?> behavior = ((PatternDescrBuilder<?>) ctxBuilder).behavior();
            setStart(behavior);
            return (T) behavior;
        } else if (NamedConsequenceDescrBuilder.class.isAssignableFrom(clazz)) {
            NamedConsequenceDescrBuilder<?> namedConsequence = ((CEDescrBuilder<?, ?>) ctxBuilder).namedConsequence();
            setStart(namedConsequence);
            return (T) namedConsequence;
        } else if (ConditionalBranchDescrBuilder.class.isAssignableFrom(clazz)) {
            ConditionalBranchDescrBuilder<?> conditionalBranch = ((CEDescrBuilder<?, ?>) ctxBuilder).conditionalBranch();
            setStart(conditionalBranch);
            return (T) conditionalBranch;
        }
    }
    return null;
}
Also used : PackageDescrBuilder(org.drools.compiler.lang.api.PackageDescrBuilder) DeclareDescrBuilder(org.drools.compiler.lang.api.DeclareDescrBuilder) AttributeDescrBuilder(org.drools.compiler.lang.api.AttributeDescrBuilder) ForallDescrBuilder(org.drools.compiler.lang.api.ForallDescrBuilder) NamedConsequenceDescrBuilder(org.drools.compiler.lang.api.NamedConsequenceDescrBuilder) AccumulateImportDescrBuilder(org.drools.compiler.lang.api.AccumulateImportDescrBuilder) ImportDescrBuilder(org.drools.compiler.lang.api.ImportDescrBuilder) FunctionDescrBuilder(org.drools.compiler.lang.api.FunctionDescrBuilder) CEDescrBuilder(org.drools.compiler.lang.api.CEDescrBuilder) FieldDescrBuilder(org.drools.compiler.lang.api.FieldDescrBuilder) QueryDescrBuilder(org.drools.compiler.lang.api.QueryDescrBuilder) UnitDescrBuilder(org.drools.compiler.lang.api.UnitDescrBuilder) GlobalDescrBuilder(org.drools.compiler.lang.api.GlobalDescrBuilder) EntryPointDeclarationDescrBuilder(org.drools.compiler.lang.api.EntryPointDeclarationDescrBuilder) WindowDeclarationDescrBuilder(org.drools.compiler.lang.api.WindowDeclarationDescrBuilder) RuleDescrBuilder(org.drools.compiler.lang.api.RuleDescrBuilder) AccumulateImportDescrBuilder(org.drools.compiler.lang.api.AccumulateImportDescrBuilder) PatternDescrBuilder(org.drools.compiler.lang.api.PatternDescrBuilder) AccumulateDescrBuilder(org.drools.compiler.lang.api.AccumulateDescrBuilder) EnumDeclarationDescrBuilder(org.drools.compiler.lang.api.EnumDeclarationDescrBuilder) EnumLiteralDescrBuilder(org.drools.compiler.lang.api.EnumLiteralDescrBuilder) AbstractClassTypeDeclarationBuilder(org.drools.compiler.lang.api.AbstractClassTypeDeclarationBuilder) TypeDeclarationDescrBuilder(org.drools.compiler.lang.api.TypeDeclarationDescrBuilder) AttributeDescr(org.drools.compiler.lang.descr.AttributeDescr)

Example 13 with AttributeDescr

use of org.drools.compiler.lang.descr.AttributeDescr 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();
                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.compiler.compiler.DroolsError) Calendar(java.util.Calendar) RuleBuildError(org.drools.compiler.compiler.RuleBuildError) RuleDescr(org.drools.compiler.lang.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.compiler.lang.descr.AttributeDescr) Date(java.util.Date) ParseException(java.text.ParseException)

Example 14 with AttributeDescr

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

the class RuleModelDRLPersistenceImpl method parseAttributes.

private boolean parseAttributes(final RuleModel m, final Map<String, AttributeDescr> attributes) {
    boolean isJavaDialect = false;
    for (Map.Entry<String, AttributeDescr> entry : attributes.entrySet()) {
        String name = entry.getKey();
        String value = normalizeAttributeValue(entry.getValue().getValue().trim());
        RuleAttribute ruleAttribute = new RuleAttribute(name, value);
        m.addAttribute(ruleAttribute);
        isJavaDialect |= name.equals("dialect") && value.equals("java");
    }
    return isJavaDialect;
}
Also used : RuleAttribute(org.drools.workbench.models.datamodel.rule.RuleAttribute) Map(java.util.Map) HashMap(java.util.HashMap) AttributeDescr(org.drools.compiler.lang.descr.AttributeDescr)

Example 15 with AttributeDescr

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

the class KnowledgeBuilderImpl method initPackage.

private void initPackage(PackageDescr packageDescr) {
    // Gather all imports for all PackageDescrs for the current package and replicate into
    // all PackageDescrs for the current package, thus maintaining a complete list of
    // ImportDescrs for all PackageDescrs for the current package.
    List<PackageDescr> packageDescrsForPackage = packages.computeIfAbsent(packageDescr.getName(), k -> new ArrayList<PackageDescr>());
    packageDescrsForPackage.add(packageDescr);
    Set<ImportDescr> imports = new HashSet<ImportDescr>();
    for (PackageDescr pd : packageDescrsForPackage) {
        imports.addAll(pd.getImports());
    }
    for (PackageDescr pd : packageDescrsForPackage) {
        pd.getImports().clear();
        pd.addAllImports(imports);
    }
    // Copy package level attributes for inclusion on individual rules
    if (!packageDescr.getAttributes().isEmpty()) {
        Map<String, AttributeDescr> pkgAttributes = packageAttributes.get(packageDescr.getNamespace());
        if (pkgAttributes == null) {
            pkgAttributes = new HashMap<String, AttributeDescr>();
            this.packageAttributes.put(packageDescr.getNamespace(), pkgAttributes);
        }
        for (AttributeDescr attr : packageDescr.getAttributes()) {
            pkgAttributes.put(attr.getName(), attr);
        }
    }
}
Also used : ImportDescr(org.drools.compiler.lang.descr.ImportDescr) FunctionImportDescr(org.drools.compiler.lang.descr.FunctionImportDescr) AccumulateImportDescr(org.drools.compiler.lang.descr.AccumulateImportDescr) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) CompositePackageDescr(org.drools.compiler.lang.descr.CompositePackageDescr) AttributeDescr(org.drools.compiler.lang.descr.AttributeDescr) HashSet(java.util.HashSet)

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