Search in sources :

Example 21 with AttributeDescr

use of org.drools.drl.ast.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.drl.ast.dsl.PackageDescrBuilder) DeclareDescrBuilder(org.drools.drl.ast.dsl.DeclareDescrBuilder) AttributeDescrBuilder(org.drools.drl.ast.dsl.AttributeDescrBuilder) ForallDescrBuilder(org.drools.drl.ast.dsl.ForallDescrBuilder) NamedConsequenceDescrBuilder(org.drools.drl.ast.dsl.NamedConsequenceDescrBuilder) AccumulateImportDescrBuilder(org.drools.drl.ast.dsl.AccumulateImportDescrBuilder) ImportDescrBuilder(org.drools.drl.ast.dsl.ImportDescrBuilder) FunctionDescrBuilder(org.drools.drl.ast.dsl.FunctionDescrBuilder) CEDescrBuilder(org.drools.drl.ast.dsl.CEDescrBuilder) FieldDescrBuilder(org.drools.drl.ast.dsl.FieldDescrBuilder) QueryDescrBuilder(org.drools.drl.ast.dsl.QueryDescrBuilder) UnitDescrBuilder(org.drools.drl.ast.dsl.UnitDescrBuilder) GlobalDescrBuilder(org.drools.drl.ast.dsl.GlobalDescrBuilder) EntryPointDeclarationDescrBuilder(org.drools.drl.ast.dsl.EntryPointDeclarationDescrBuilder) WindowDeclarationDescrBuilder(org.drools.drl.ast.dsl.WindowDeclarationDescrBuilder) RuleDescrBuilder(org.drools.drl.ast.dsl.RuleDescrBuilder) AccumulateImportDescrBuilder(org.drools.drl.ast.dsl.AccumulateImportDescrBuilder) PatternDescrBuilder(org.drools.drl.ast.dsl.PatternDescrBuilder) AccumulateDescrBuilder(org.drools.drl.ast.dsl.AccumulateDescrBuilder) EnumDeclarationDescrBuilder(org.drools.drl.ast.dsl.EnumDeclarationDescrBuilder) EnumLiteralDescrBuilder(org.drools.drl.ast.dsl.EnumLiteralDescrBuilder) AbstractClassTypeDeclarationBuilder(org.drools.drl.ast.dsl.AbstractClassTypeDeclarationBuilder) TypeDeclarationDescrBuilder(org.drools.drl.ast.dsl.TypeDeclarationDescrBuilder) AttributeDescr(org.drools.drl.ast.descr.AttributeDescr)

Example 22 with AttributeDescr

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

the class RuleHandler method end.

public Object end(final String uri, final String localName, final ExtensibleXmlParser parser) throws SAXException {
    final Element element = parser.endElementBuilder();
    final RuleDescr ruleDescr = (RuleDescr) parser.getCurrent();
    final AndDescr lhs = ruleDescr.getLhs();
    if (lhs == null) {
        throw new SAXParseException("<rule> requires a LHS", parser.getLocator());
    }
    NodeList list = element.getElementsByTagName("rhs");
    if (list.getLength() == 0) {
        throw new SAXParseException("<rule> requires a <rh> child element", parser.getLocator());
    }
    ruleDescr.setConsequence(((org.w3c.dom.Text) list.item(0).getChildNodes().item(0)).getWholeText());
    NodeList attributes = element.getElementsByTagName("rule-attribute");
    for (int i = 0, length = attributes.getLength(); i < length; i++) {
        final String name = ((Element) attributes.item(i)).getAttribute("name");
        emptyAttributeCheck("rule-attribute", "name", name, parser);
        final String value = ((Element) attributes.item(i)).getAttribute("value");
        ruleDescr.addAttribute(new AttributeDescr(name, value));
    }
    ((PackageDescr) parser.getData()).addRule(ruleDescr);
    return ruleDescr;
}
Also used : AndDescr(org.drools.drl.ast.descr.AndDescr) SAXParseException(org.xml.sax.SAXParseException) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) RuleDescr(org.drools.drl.ast.descr.RuleDescr) PackageDescr(org.drools.drl.ast.descr.PackageDescr) AttributeDescr(org.drools.drl.ast.descr.AttributeDescr)

Example 23 with AttributeDescr

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

Example 24 with AttributeDescr

use of org.drools.drl.ast.descr.AttributeDescr 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.mvel.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.drl.ast.descr.RuleDescr) DrlParser(org.drools.drl.parser.DrlParser) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) PackageDescr(org.drools.drl.ast.descr.PackageDescr) AttributeDescr(org.drools.drl.ast.descr.AttributeDescr) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) Test(org.junit.Test)

Example 25 with AttributeDescr

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

Aggregations

AttributeDescr (org.drools.drl.ast.descr.AttributeDescr)34 RuleDescr (org.drools.drl.ast.descr.RuleDescr)26 Test (org.junit.Test)21 PackageDescr (org.drools.drl.ast.descr.PackageDescr)13 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)7 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)6 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)6 RuleBuildContext (org.drools.compiler.rule.builder.RuleBuildContext)5 DialectCompiletimeRegistry (org.drools.compiler.compiler.DialectCompiletimeRegistry)4 HashSet (java.util.HashSet)3 List (java.util.List)3 Pattern (org.drools.core.rule.Pattern)3 AndDescr (org.drools.drl.ast.descr.AndDescr)3 FunctionImportDescr (org.drools.drl.ast.descr.FunctionImportDescr)3 ImportDescr (org.drools.drl.ast.descr.ImportDescr)3 ArrayList (java.util.ArrayList)2 Calendar (java.util.Calendar)2 HashMap (java.util.HashMap)2 KnowledgeBuilderConfigurationImpl (org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl)2 PackageRegistry (org.drools.compiler.compiler.PackageRegistry)2