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());
}
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());
}
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));
}
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);
}
}
}
}
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;
}
Aggregations