Search in sources :

Example 1 with FunctionImportDescr

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

the class XmlDumper method processFunctionImportsList.

private String processFunctionImportsList(final List imports) {
    String importList = "";
    for (final Iterator it = imports.iterator(); it.hasNext(); ) {
        final String importString = ((FunctionImportDescr) it.next()).getTarget();
        final String importTemplate = "<importfunction name=\"" + importString + "\"/>" + XmlDumper.eol;
        importList += importTemplate;
    }
    return importList + XmlDumper.eol;
}
Also used : FunctionImportDescr(org.drools.compiler.lang.descr.FunctionImportDescr) Iterator(java.util.Iterator)

Example 2 with FunctionImportDescr

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

the class PackageHandler method end.

public Object end(final String uri, final String localName, final ExtensibleXmlParser parser) throws SAXException {
    final PackageDescr packageDescr = (PackageDescr) parser.getData();
    final Element element = parser.endElementBuilder();
    NodeList imports = element.getElementsByTagName("import");
    for (int i = 0, length = imports.getLength(); i < length; i++) {
        final String importEntry = ((Element) imports.item(i)).getAttribute("name");
        if (importEntry == null || importEntry.trim().equals("")) {
            throw new SAXParseException("<import> cannot be blank", parser.getLocator());
        }
        packageDescr.addImport(new ImportDescr(importEntry));
    }
    NodeList importfunctions = element.getElementsByTagName("importfunction");
    for (int i = 0, length = importfunctions.getLength(); i < length; i++) {
        final String importfunctionEntry = ((Element) importfunctions.item(i)).getAttribute("name");
        if (importfunctionEntry == null || importfunctionEntry.trim().equals("")) {
            throw new SAXParseException("<importfunction> cannot be blank", parser.getLocator());
        }
        FunctionImportDescr funcdescr = new FunctionImportDescr();
        funcdescr.setTarget(importfunctionEntry);
        packageDescr.addFunctionImport(funcdescr);
    }
    NodeList globals = element.getElementsByTagName("global");
    for (int i = 0, length = globals.getLength(); i < length; i++) {
        final String identifier = ((Element) globals.item(i)).getAttribute("identifier");
        if (identifier == null || identifier.trim().equals("")) {
            throw new SAXParseException("<global> must have an identifier", parser.getLocator());
        }
        final String type = ((Element) globals.item(i)).getAttribute("type");
        if (type == null || type.trim().equals("")) {
            throw new SAXParseException("<global> must have specify a type", parser.getLocator());
        }
        final GlobalDescr global = new GlobalDescr(identifier, type);
        packageDescr.addGlobal(global);
    }
    return packageDescr;
}
Also used : GlobalDescr(org.drools.compiler.lang.descr.GlobalDescr) FunctionImportDescr(org.drools.compiler.lang.descr.FunctionImportDescr) SAXParseException(org.xml.sax.SAXParseException) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) ImportDescr(org.drools.compiler.lang.descr.ImportDescr) FunctionImportDescr(org.drools.compiler.lang.descr.FunctionImportDescr) PackageDescr(org.drools.compiler.lang.descr.PackageDescr)

Example 3 with FunctionImportDescr

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

the class KnowledgeBuilderImpl method processFunctions.

private void processFunctions(PackageRegistry pkgRegistry, PackageDescr packageDescr) {
    for (FunctionDescr function : packageDescr.getFunctions()) {
        Function existingFunc = pkgRegistry.getPackage().getFunctions().get(function.getName());
        if (existingFunc != null && function.getNamespace().equals(existingFunc.getNamespace())) {
            addBuilderResult(new DuplicateFunction(function, this.configuration));
        }
    }
    for (final FunctionImportDescr functionImport : packageDescr.getFunctionImports()) {
        String importEntry = functionImport.getTarget();
        pkgRegistry.addStaticImport(functionImport);
        pkgRegistry.getPackage().addStaticImport(importEntry);
    }
}
Also used : AccumulateFunction(org.kie.api.runtime.rule.AccumulateFunction) DuplicateFunction(org.drools.compiler.compiler.DuplicateFunction) Function(org.drools.core.rule.Function) DuplicateFunction(org.drools.compiler.compiler.DuplicateFunction) FunctionImportDescr(org.drools.compiler.lang.descr.FunctionImportDescr) FunctionDescr(org.drools.compiler.lang.descr.FunctionDescr)

Example 4 with FunctionImportDescr

use of org.drools.compiler.lang.descr.FunctionImportDescr 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 5 with FunctionImportDescr

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

the class RuleParserTest method testFunctionImport2.

@Test
public void testFunctionImport2() throws Exception {
    PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "test_FunctionImport.drl");
    assertEquals(2, pkg.getFunctionImports().size());
    assertEquals("abd.def.x", ((FunctionImportDescr) pkg.getFunctionImports().get(0)).getTarget());
    assertFalse(((FunctionImportDescr) pkg.getFunctionImports().get(0)).getStartCharacter() == -1);
    assertFalse(((FunctionImportDescr) pkg.getFunctionImports().get(0)).getEndCharacter() == -1);
    assertEquals("qed.wah.*", ((FunctionImportDescr) pkg.getFunctionImports().get(1)).getTarget());
    assertFalse(((FunctionImportDescr) pkg.getFunctionImports().get(1)).getStartCharacter() == -1);
    assertFalse(((FunctionImportDescr) pkg.getFunctionImports().get(1)).getEndCharacter() == -1);
}
Also used : FunctionImportDescr(org.drools.compiler.lang.descr.FunctionImportDescr) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) Test(org.junit.Test)

Aggregations

FunctionImportDescr (org.drools.compiler.lang.descr.FunctionImportDescr)5 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)3 ImportDescr (org.drools.compiler.lang.descr.ImportDescr)2 Test (org.junit.Test)2 Iterator (java.util.Iterator)1 KnowledgeBuilderConfigurationImpl (org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl)1 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)1 DuplicateFunction (org.drools.compiler.compiler.DuplicateFunction)1 AndDescr (org.drools.compiler.lang.descr.AndDescr)1 AttributeDescr (org.drools.compiler.lang.descr.AttributeDescr)1 EvalDescr (org.drools.compiler.lang.descr.EvalDescr)1 FunctionDescr (org.drools.compiler.lang.descr.FunctionDescr)1 GlobalDescr (org.drools.compiler.lang.descr.GlobalDescr)1 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)1 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)1 KnowledgePackageImpl (org.drools.core.definitions.impl.KnowledgePackageImpl)1 Function (org.drools.core.rule.Function)1 AccumulateFunction (org.kie.api.runtime.rule.AccumulateFunction)1 Element (org.w3c.dom.Element)1 NodeList (org.w3c.dom.NodeList)1