Search in sources :

Example 1 with FunctionDescr

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

the class XmlPackageReaderTest method testParseRhs.

@Test
public void testParseRhs() throws Exception {
    final XmlPackageReader xmlPackageReader = getXmReader();
    xmlPackageReader.read(new InputStreamReader(getClass().getResourceAsStream("test_ParseRhs.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 RuleDescr ruleDescr = (RuleDescr) packageDescr.getRules().get(0);
    assertEquals("my rule", ruleDescr.getName());
    final String consequence = (String) ruleDescr.getConsequence();
    assertNotNull(consequence);
    assertEquals("System.out.println( \"hello\" );", consequence.trim());
}
Also used : GlobalDescr(org.drools.drl.ast.descr.GlobalDescr) InputStreamReader(java.io.InputStreamReader) RuleDescr(org.drools.drl.ast.descr.RuleDescr) 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 2 with FunctionDescr

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

the class RuleParserTest method testFunctions.

@Test
public void testFunctions() throws Exception {
    final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "functions.drl");
    assertEquals(2, pkg.getRules().size());
    final List<FunctionDescr> functions = pkg.getFunctions();
    assertEquals(2, functions.size());
    FunctionDescr func = functions.get(0);
    assertEquals("functionA", func.getName());
    assertEquals("String", func.getReturnType());
    assertEquals(2, func.getParameterNames().size());
    assertEquals(2, func.getParameterTypes().size());
    assertEquals(19, func.getLine());
    assertEquals(0, func.getColumn());
    assertEquals("String", func.getParameterTypes().get(0));
    assertEquals("s", func.getParameterNames().get(0));
    assertEquals("Integer", func.getParameterTypes().get(1));
    assertEquals("i", func.getParameterNames().get(1));
    assertEqualsIgnoreWhitespace("foo();", func.getBody());
    func = functions.get(1);
    assertEquals("functionB", func.getName());
    assertEqualsIgnoreWhitespace("bar();", func.getText());
}
Also used : PackageDescr(org.drools.drl.ast.descr.PackageDescr) FunctionDescr(org.drools.drl.ast.descr.FunctionDescr) Test(org.junit.Test)

Example 3 with FunctionDescr

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

Example 4 with FunctionDescr

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

Example 5 with FunctionDescr

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