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