Search in sources :

Example 6 with FunctionDescr

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

the class AsmUtil 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.drl.ast.descr.PackageDescr) FunctionDescr(org.drools.drl.ast.descr.FunctionDescr)

Example 7 with FunctionDescr

use of org.drools.drl.ast.descr.FunctionDescr 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 8 with FunctionDescr

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

the class XmlPackageReaderTest method testParseFunction.

@Test
public void testParseFunction() throws Exception {
    final XmlPackageReader xmlPackageReader = getXmReader();
    xmlPackageReader.read(new InputStreamReader(getClass().getResourceAsStream("test_ParseFunction.xml")));
    final PackageDescr packageDescr = xmlPackageReader.getPackageDescr();
    assertNotNull(packageDescr);
    assertEquals("com.sample", packageDescr.getName());
    final List imports = packageDescr.getImports();
    assertEquals(2, imports.size());
    assertEquals("java.util.HashMap", ((ImportDescr) imports.get(0)).getTarget());
    assertEquals("org.drools.mvel.compiler.*", ((ImportDescr) imports.get(1)).getTarget());
    final List globals = packageDescr.getGlobals();
    assertEquals(2, globals.size());
    final GlobalDescr x = (GlobalDescr) globals.get(0);
    final GlobalDescr yada = (GlobalDescr) globals.get(1);
    assertEquals("com.sample.X", x.getType());
    assertEquals("x", x.getIdentifier());
    assertEquals("com.sample.Yada", yada.getType());
    assertEquals("yada", yada.getIdentifier());
    final FunctionDescr functionDescr = (FunctionDescr) packageDescr.getFunctions().get(0);
    final List names = functionDescr.getParameterNames();
    assertEquals("foo", names.get(0));
    assertEquals("bada", names.get(1));
    final List types = functionDescr.getParameterTypes();
    assertEquals("Bar", types.get(0));
    assertEquals("Bing", types.get(1));
    assertEquals("System.out.println(\"hello world\");", functionDescr.getText().trim());
}
Also used : GlobalDescr(org.drools.drl.ast.descr.GlobalDescr) InputStreamReader(java.io.InputStreamReader) List(java.util.List) PackageDescr(org.drools.drl.ast.descr.PackageDescr) FunctionDescr(org.drools.drl.ast.descr.FunctionDescr) XmlPackageReader(org.drools.compiler.compiler.xml.XmlPackageReader) Test(org.junit.Test)

Example 9 with FunctionDescr

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

the class XmlPackageReaderTest method testParseQuery.

@Test
public void testParseQuery() throws Exception {
    final XmlPackageReader xmlPackageReader = getXmReader();
    xmlPackageReader.read(new InputStreamReader(getClass().getResourceAsStream("test_ParseQuery.xml")));
    final PackageDescr packageDescr = xmlPackageReader.getPackageDescr();
    assertNotNull(packageDescr);
    assertEquals("com.sample", packageDescr.getName());
    final List imports = packageDescr.getImports();
    assertEquals(2, imports.size());
    assertEquals("java.util.HashMap", ((ImportDescr) imports.get(0)).getTarget());
    assertEquals("org.drools.mvel.compiler.*", ((ImportDescr) imports.get(1)).getTarget());
    final List globals = packageDescr.getGlobals();
    assertEquals(2, globals.size());
    final GlobalDescr x = (GlobalDescr) globals.get(0);
    final GlobalDescr yada = (GlobalDescr) globals.get(1);
    assertEquals("com.sample.X", x.getType());
    assertEquals("x", x.getIdentifier());
    assertEquals("com.sample.Yada", yada.getType());
    assertEquals("yada", yada.getIdentifier());
    final FunctionDescr functionDescr = (FunctionDescr) packageDescr.getFunctions().get(0);
    final List names = functionDescr.getParameterNames();
    assertEquals("foo", names.get(0));
    assertEquals("bada", names.get(1));
    final List types = functionDescr.getParameterTypes();
    assertEquals("Bar", types.get(0));
    assertEquals("Bing", types.get(1));
    assertEquals("System.out.println(\"hello world\");", functionDescr.getText().trim());
    final QueryDescr queryDescr = (QueryDescr) packageDescr.getRules().get(0);
    assertEquals("my query", queryDescr.getName());
    final AndDescr lhs = queryDescr.getLhs();
    assertEquals(1, lhs.getDescrs().size());
    final PatternDescr patternDescr = (PatternDescr) lhs.getDescrs().get(0);
    assertEquals("Foo", patternDescr.getObjectType());
}
Also used : GlobalDescr(org.drools.drl.ast.descr.GlobalDescr) QueryDescr(org.drools.drl.ast.descr.QueryDescr) InputStreamReader(java.io.InputStreamReader) PatternDescr(org.drools.drl.ast.descr.PatternDescr) AndDescr(org.drools.drl.ast.descr.AndDescr) List(java.util.List) PackageDescr(org.drools.drl.ast.descr.PackageDescr) FunctionDescr(org.drools.drl.ast.descr.FunctionDescr) XmlPackageReader(org.drools.compiler.compiler.xml.XmlPackageReader) Test(org.junit.Test)

Example 10 with FunctionDescr

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

the class AsmUtil 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.drl.ast.descr.FunctionDescr)

Aggregations

FunctionDescr (org.drools.drl.ast.descr.FunctionDescr)13 PackageDescr (org.drools.drl.ast.descr.PackageDescr)9 GlobalDescr (org.drools.drl.ast.descr.GlobalDescr)5 Test (org.junit.Test)5 List (java.util.List)4 RuleDescr (org.drools.drl.ast.descr.RuleDescr)4 InputStreamReader (java.io.InputStreamReader)3 XmlPackageReader (org.drools.compiler.compiler.xml.XmlPackageReader)3 HashSet (java.util.HashSet)2 FunctionImportDescr (org.drools.drl.ast.descr.FunctionImportDescr)2 TypeDeclarationDescr (org.drools.drl.ast.descr.TypeDeclarationDescr)2 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Comparator (java.util.Comparator)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 Map (java.util.Map)1 Set (java.util.Set)1 KnowledgeBuilderConfigurationImpl (org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl)1