Search in sources :

Example 16 with AttributeDescr

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

the class KnowledgeBuilderImpl method processWindowDeclarations.

private void processWindowDeclarations(PackageRegistry pkgRegistry, PackageDescr packageDescr) {
    for (WindowDeclarationDescr wd : packageDescr.getWindowDeclarations()) {
        WindowDeclaration window = new WindowDeclaration(wd.getName(), packageDescr.getName());
        // TODO: process annotations
        // process pattern
        InternalKnowledgePackage pkg = pkgRegistry.getPackage();
        DialectCompiletimeRegistry ctr = pkgRegistry.getDialectCompiletimeRegistry();
        RuleDescr dummy = new RuleDescr(wd.getName() + " Window Declaration");
        dummy.setResource(packageDescr.getResource());
        dummy.addAttribute(new AttributeDescr("dialect", "java"));
        RuleBuildContext context = new RuleBuildContext(this, dummy, ctr, pkg, ctr.getDialect(pkgRegistry.getDialect()));
        final RuleConditionBuilder builder = (RuleConditionBuilder) context.getDialect().getBuilder(wd.getPattern().getClass());
        if (builder != null) {
            final Pattern pattern = (Pattern) builder.build(context, wd.getPattern(), null);
            if (pattern.getXpathConstraint() != null) {
                context.addError(new DescrBuildError(wd, context.getParentDescr(), null, "OOpath expression " + pattern.getXpathConstraint() + " not allowed in window declaration\n"));
            }
            window.setPattern(pattern);
        } else {
            throw new RuntimeException("BUG: assembler not found for descriptor class " + wd.getPattern().getClass());
        }
        if (!context.getErrors().isEmpty()) {
            for (DroolsError error : context.getErrors()) {
                addBuilderResult(error);
            }
        } else {
            pkgRegistry.getPackage().addWindowDeclaration(window);
        }
    }
}
Also used : Pattern(org.drools.core.rule.Pattern) DroolsError(org.drools.compiler.compiler.DroolsError) DescrBuildError(org.drools.compiler.compiler.DescrBuildError) RuleBuildContext(org.drools.compiler.rule.builder.RuleBuildContext) DialectCompiletimeRegistry(org.drools.compiler.compiler.DialectCompiletimeRegistry) WindowDeclaration(org.drools.core.rule.WindowDeclaration) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) RuleConditionBuilder(org.drools.compiler.rule.builder.RuleConditionBuilder) WindowDeclarationDescr(org.drools.compiler.lang.descr.WindowDeclarationDescr) AttributeDescr(org.drools.compiler.lang.descr.AttributeDescr) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 17 with AttributeDescr

use of org.drools.compiler.lang.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.compiler.lang.descr.AndDescr) SAXParseException(org.xml.sax.SAXParseException) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) AttributeDescr(org.drools.compiler.lang.descr.AttributeDescr)

Example 18 with AttributeDescr

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

the class PackageBuilderConfigurationTest method testMockDialect.

@Test
public void testMockDialect() {
    InternalKnowledgePackage pkg = new KnowledgePackageImpl("org.pkg1");
    KnowledgeBuilderConfigurationImpl cfg1 = new KnowledgeBuilderConfigurationImpl();
    MockDialectConfiguration mockConf = new MockDialectConfiguration();
    // cfg1.buildDialectRegistry().addDialect( "mock",
    // mockConf.getDialect() );
    cfg1.addDialect("mock", mockConf);
    cfg1.setDefaultDialect("mock");
    KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl(pkg, cfg1);
    PackageRegistry pkgRegistry = builder.getPackageRegistry(pkg.getName());
    DialectCompiletimeRegistry dialectRegistry = pkgRegistry.getDialectCompiletimeRegistry();
    MockDialect mockDialect2 = (MockDialect) dialectRegistry.getDialect(cfg1.getDefaultDialect());
    assertSame(pkg, mockDialect2.getPkg());
    assertNull(mockDialect2.getRuleDescr());
    RuleDescr ruleDescr = new RuleDescr("test rule");
    ruleDescr.addAttribute(new AttributeDescr("dialect", "mock"));
    ruleDescr.setLhs(new AndDescr());
    EvalDescr evalDescr = new EvalDescr();
    ruleDescr.getLhs().addDescr(evalDescr);
    PackageDescr pkgDescr = new PackageDescr("org.pkg1");
    pkgDescr.addImport(new ImportDescr("java.util.HashMap"));
    FunctionImportDescr functionImportDescr = new FunctionImportDescr();
    functionImportDescr.setTarget("java.lang.System.currentTimeMillis");
    pkgDescr.addFunctionImport(functionImportDescr);
    pkgDescr.addRule(ruleDescr);
    builder.addPackage(pkgDescr);
    assertSame(ruleDescr, mockDialect2.getRuleDescr());
    assertTrue(mockDialect2.getImport().contains("java.util.HashMap"));
    assertTrue(mockDialect2.getStaticImport().contains("java.lang.System.currentTimeMillis"));
    assertEquals("eval was built", evalDescr.getContent());
    assertEquals("consequence was built", ruleDescr.getConsequence());
    assertTrue(mockDialect2.isCompileAll());
    assertNotNull(pkg.getRule("test rule"));
    // make sure there were no other general errors.
    assertFalse(builder.hasErrors());
}
Also used : FunctionImportDescr(org.drools.compiler.lang.descr.FunctionImportDescr) KnowledgeBuilderConfigurationImpl(org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl) AndDescr(org.drools.compiler.lang.descr.AndDescr) ImportDescr(org.drools.compiler.lang.descr.ImportDescr) FunctionImportDescr(org.drools.compiler.lang.descr.FunctionImportDescr) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) EvalDescr(org.drools.compiler.lang.descr.EvalDescr) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) KnowledgePackageImpl(org.drools.core.definitions.impl.KnowledgePackageImpl) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) AttributeDescr(org.drools.compiler.lang.descr.AttributeDescr) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) Test(org.junit.Test)

Example 19 with AttributeDescr

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

the class PackageDescrResourceVisitor method visit.

protected void visit(final QueryDescr descr) {
    checkResource(descr);
    visit(descr.getLhs());
    for (AttributeDescr attrDescr : descr.getAttributes().values()) {
        visit(attrDescr);
    }
}
Also used : AttributeDescr(org.drools.compiler.lang.descr.AttributeDescr)

Example 20 with AttributeDescr

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

the class PackageDescrResourceVisitor method visit.

protected void visit(final RuleDescr descr) {
    checkResource(descr);
    for (AttributeDescr d : descr.getAttributes().values()) {
        visit(d);
    }
    visit(descr.getLhs());
    visitConsequence(descr.getConsequence());
    for (Object o : descr.getNamedConsequences().values()) {
        visitConsequence(o);
    }
}
Also used : AttributeDescr(org.drools.compiler.lang.descr.AttributeDescr)

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