Search in sources :

Example 1 with FunctionImportDescr

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

the class PackageBuilderConfigurationTest method testMockDialect.

@Test
public void testMockDialect() {
    InternalKnowledgePackage pkg = CoreComponentFactory.get().createKnowledgePackage("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.drl.ast.descr.FunctionImportDescr) KnowledgeBuilderConfigurationImpl(org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl) DialectCompiletimeRegistry(org.drools.compiler.compiler.DialectCompiletimeRegistry) AndDescr(org.drools.drl.ast.descr.AndDescr) ImportDescr(org.drools.drl.ast.descr.ImportDescr) FunctionImportDescr(org.drools.drl.ast.descr.FunctionImportDescr) PackageRegistry(org.drools.compiler.compiler.PackageRegistry) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) EvalDescr(org.drools.drl.ast.descr.EvalDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) 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 2 with FunctionImportDescr

use of org.drools.drl.ast.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.drl.ast.descr.GlobalDescr) FunctionImportDescr(org.drools.drl.ast.descr.FunctionImportDescr) SAXParseException(org.xml.sax.SAXParseException) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) ImportDescr(org.drools.drl.ast.descr.ImportDescr) FunctionImportDescr(org.drools.drl.ast.descr.FunctionImportDescr) PackageDescr(org.drools.drl.ast.descr.PackageDescr)

Example 3 with FunctionImportDescr

use of org.drools.drl.ast.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.drl.ast.descr.FunctionImportDescr) Iterator(java.util.Iterator)

Example 4 with FunctionImportDescr

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

the class KnowledgeBuilderImpl method processFunctions.

protected 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.drl.ast.descr.FunctionImportDescr) FunctionDescr(org.drools.drl.ast.descr.FunctionDescr)

Example 5 with FunctionImportDescr

use of org.drools.drl.ast.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.drl.ast.descr.FunctionImportDescr) PackageDescr(org.drools.drl.ast.descr.PackageDescr) Test(org.junit.Test)

Aggregations

FunctionImportDescr (org.drools.drl.ast.descr.FunctionImportDescr)6 ImportDescr (org.drools.drl.ast.descr.ImportDescr)3 PackageDescr (org.drools.drl.ast.descr.PackageDescr)3 AttributeDescr (org.drools.drl.ast.descr.AttributeDescr)2 FunctionDescr (org.drools.drl.ast.descr.FunctionDescr)2 GlobalDescr (org.drools.drl.ast.descr.GlobalDescr)2 RuleDescr (org.drools.drl.ast.descr.RuleDescr)2 Test (org.junit.Test)2 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 KnowledgeBuilderConfigurationImpl (org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl)1 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)1 DialectCompiletimeRegistry (org.drools.compiler.compiler.DialectCompiletimeRegistry)1 DuplicateFunction (org.drools.compiler.compiler.DuplicateFunction)1 PackageRegistry (org.drools.compiler.compiler.PackageRegistry)1 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)1 Function (org.drools.core.rule.Function)1 AccumulateImportDescr (org.drools.drl.ast.descr.AccumulateImportDescr)1 AndDescr (org.drools.drl.ast.descr.AndDescr)1 EntryPointDeclarationDescr (org.drools.drl.ast.descr.EntryPointDeclarationDescr)1