Search in sources :

Example 1 with FunctionDescr

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

the class KnowledgeBuilderImpl method compileFunctions.

private void compileFunctions(PackageDescr packageDescr, PackageRegistry pkgRegistry) {
    List<FunctionDescr> functions = packageDescr.getFunctions();
    if (!functions.isEmpty()) {
        for (FunctionDescr functionDescr : functions) {
            if (isEmpty(functionDescr.getNamespace())) {
                // make sure namespace is set on components
                functionDescr.setNamespace(packageDescr.getNamespace());
            }
            // make sure functions are compiled using java dialect
            functionDescr.setDialect("java");
            preCompileAddFunction(functionDescr, pkgRegistry);
        }
        // iterate and compile
        for (FunctionDescr functionDescr : functions) {
            if (filterAccepts(ResourceChange.Type.FUNCTION, functionDescr.getNamespace(), functionDescr.getName())) {
                // inherit the dialect from the package
                addFunction(functionDescr, pkgRegistry);
            }
        }
        // We need to compile all the functions now, so scripting
        // languages like mvel can find them
        compileAll();
        for (FunctionDescr functionDescr : functions) {
            if (filterAccepts(ResourceChange.Type.FUNCTION, functionDescr.getNamespace(), functionDescr.getName())) {
                postCompileAddFunction(functionDescr, pkgRegistry);
            }
        }
    }
}
Also used : FunctionDescr(org.drools.compiler.lang.descr.FunctionDescr)

Example 2 with FunctionDescr

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

the class FunctionHandler method end.

public Object end(final String uri, final String localName, final ExtensibleXmlParser parser) throws SAXException {
    final Element element = parser.endElementBuilder();
    FunctionDescr functionDescr = (FunctionDescr) parser.getCurrent();
    final NodeList parameters = element.getElementsByTagName("parameter");
    for (int i = 0, length = parameters.getLength(); i < length; i++) {
        final String identifier = ((Element) parameters.item(i)).getAttribute("identifier");
        final String type = ((Element) parameters.item(i)).getAttribute("type");
        emptyAttributeCheck("parameter", "identifier", identifier, parser);
        emptyAttributeCheck("parameter", "type", type, parser);
        functionDescr.addParameter(type, identifier);
    }
    // we allow empty, "", bodies - but make sure that we atleast have a body element
    NodeList list = element.getElementsByTagName("body");
    if (list.getLength() == 0) {
        throw new SAXParseException("function must have a <body>", parser.getLocator());
    }
    functionDescr.setText(((org.w3c.dom.Text) list.item(0).getChildNodes().item(0)).getWholeText());
    final PackageDescr packageDescr = (PackageDescr) parser.getData();
    packageDescr.addFunction(functionDescr);
    return functionDescr;
}
Also used : SAXParseException(org.xml.sax.SAXParseException) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) FunctionDescr(org.drools.compiler.lang.descr.FunctionDescr)

Example 3 with FunctionDescr

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

the class RuleParserTest method testFunctionWithArrays.

// public void FIXME_testLatinChars() throws Exception {
// final DrlParser parser = new DrlParser();
// final Reader drl = new InputStreamReader( this.getClass().getResourceAsStream( "latin-sample.dslr" ) );
// final Reader dsl = new InputStreamReader( this.getClass().getResourceAsStream( "latin.dsl" ) );
// 
// final PackageDescr pkg = parser.parse( drl,
// dsl );
// 
// // MN: will get some errors due to the char encoding on my FC5 install
// // others who use the right encoding may not see this, feel free to
// // uncomment
// // the following assertion.
// assertFalse( parser.hasErrors() );
// 
// assertEquals( "br.com.auster.drools.sample",
// pkg.getName() );
// assertEquals( 1,
// pkg.getRules().size() );
// 
// }
// 
@Test
public void testFunctionWithArrays() throws Exception {
    PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "function_arrays.drl");
    assertEquals("foo", pkg.getName());
    assertEquals(1, pkg.getRules().size());
    final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
    assertEqualsIgnoreWhitespace("yourFunction(new String[3] {\"a\",\"b\",\"c\"});", (String) rule.getConsequence());
    final FunctionDescr func = (FunctionDescr) pkg.getFunctions().get(0);
    assertEquals("String[]", func.getReturnType());
    assertEquals("args[]", func.getParameterNames().get(0));
    assertEquals("String", func.getParameterTypes().get(0));
}
Also used : RuleDescr(org.drools.compiler.lang.descr.RuleDescr) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) FunctionDescr(org.drools.compiler.lang.descr.FunctionDescr) Test(org.junit.Test)

Example 4 with FunctionDescr

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

the class DialectUtil method lookupFunction.

private static FunctionDescr lookupFunction(RuleBuildContext context, String functionName) {
    String packageName = context.getRule().getPackageName();
    List<PackageDescr> pkgDescrs = context.getKnowledgeBuilder().getPackageDescrs(packageName);
    for (PackageDescr pkgDescr : pkgDescrs) {
        for (FunctionDescr function : pkgDescr.getFunctions()) {
            if (function.getName().equals(functionName)) {
                return function;
            }
        }
    }
    return null;
}
Also used : PackageDescr(org.drools.compiler.lang.descr.PackageDescr) FunctionDescr(org.drools.compiler.lang.descr.FunctionDescr)

Example 5 with FunctionDescr

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

the class DialectUtil method findFunctionReturnedClass.

private static Class<?> findFunctionReturnedClass(RuleBuildContext context, String statement) {
    String functionName = statement.substring(0, statement.indexOf('('));
    FunctionDescr function = lookupFunction(context, functionName);
    return function == null ? null : findClassByName(context, function.getReturnType());
}
Also used : FunctionDescr(org.drools.compiler.lang.descr.FunctionDescr)

Aggregations

FunctionDescr (org.drools.compiler.lang.descr.FunctionDescr)11 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)8 Test (org.junit.Test)5 List (java.util.List)4 GlobalDescr (org.drools.compiler.lang.descr.GlobalDescr)4 InputStreamReader (java.io.InputStreamReader)3 XmlPackageReader (org.drools.compiler.compiler.xml.XmlPackageReader)3 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)3 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Comparator (java.util.Comparator)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 Set (java.util.Set)1 KnowledgeBuilderConfigurationImpl (org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl)1 DrlParser (org.drools.compiler.compiler.DrlParser)1 DuplicateFunction (org.drools.compiler.compiler.DuplicateFunction)1 InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)1 AndDescr (org.drools.compiler.lang.descr.AndDescr)1