use of org.drools.compiler.lang.descr.FunctionImportDescr in project drools by kiegroup.
the class XmlDumper method processFunctionImportsList.
private String processFunctionImportsList(final List imports) {
String importList = "";
for (final Iterator it = imports.iterator(); it.hasNext(); ) {
final String importString = ((FunctionImportDescr) it.next()).getTarget();
final String importTemplate = "<importfunction name=\"" + importString + "\"/>" + XmlDumper.eol;
importList += importTemplate;
}
return importList + XmlDumper.eol;
}
use of org.drools.compiler.lang.descr.FunctionImportDescr in project drools by kiegroup.
the class PackageHandler method end.
public Object end(final String uri, final String localName, final ExtensibleXmlParser parser) throws SAXException {
final PackageDescr packageDescr = (PackageDescr) parser.getData();
final Element element = parser.endElementBuilder();
NodeList imports = element.getElementsByTagName("import");
for (int i = 0, length = imports.getLength(); i < length; i++) {
final String importEntry = ((Element) imports.item(i)).getAttribute("name");
if (importEntry == null || importEntry.trim().equals("")) {
throw new SAXParseException("<import> cannot be blank", parser.getLocator());
}
packageDescr.addImport(new ImportDescr(importEntry));
}
NodeList importfunctions = element.getElementsByTagName("importfunction");
for (int i = 0, length = importfunctions.getLength(); i < length; i++) {
final String importfunctionEntry = ((Element) importfunctions.item(i)).getAttribute("name");
if (importfunctionEntry == null || importfunctionEntry.trim().equals("")) {
throw new SAXParseException("<importfunction> cannot be blank", parser.getLocator());
}
FunctionImportDescr funcdescr = new FunctionImportDescr();
funcdescr.setTarget(importfunctionEntry);
packageDescr.addFunctionImport(funcdescr);
}
NodeList globals = element.getElementsByTagName("global");
for (int i = 0, length = globals.getLength(); i < length; i++) {
final String identifier = ((Element) globals.item(i)).getAttribute("identifier");
if (identifier == null || identifier.trim().equals("")) {
throw new SAXParseException("<global> must have an identifier", parser.getLocator());
}
final String type = ((Element) globals.item(i)).getAttribute("type");
if (type == null || type.trim().equals("")) {
throw new SAXParseException("<global> must have specify a type", parser.getLocator());
}
final GlobalDescr global = new GlobalDescr(identifier, type);
packageDescr.addGlobal(global);
}
return packageDescr;
}
use of org.drools.compiler.lang.descr.FunctionImportDescr in project drools by kiegroup.
the class KnowledgeBuilderImpl method processFunctions.
private 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);
}
}
use of org.drools.compiler.lang.descr.FunctionImportDescr in project drools by kiegroup.
the class PackageBuilderConfigurationTest method testMockDialect.
@Test
public void testMockDialect() {
InternalKnowledgePackage pkg = new KnowledgePackageImpl("org.pkg1");
KnowledgeBuilderConfigurationImpl cfg1 = new KnowledgeBuilderConfigurationImpl();
MockDialectConfiguration mockConf = new MockDialectConfiguration();
// cfg1.buildDialectRegistry().addDialect( "mock",
// mockConf.getDialect() );
cfg1.addDialect("mock", mockConf);
cfg1.setDefaultDialect("mock");
KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl(pkg, cfg1);
PackageRegistry pkgRegistry = builder.getPackageRegistry(pkg.getName());
DialectCompiletimeRegistry dialectRegistry = pkgRegistry.getDialectCompiletimeRegistry();
MockDialect mockDialect2 = (MockDialect) dialectRegistry.getDialect(cfg1.getDefaultDialect());
assertSame(pkg, mockDialect2.getPkg());
assertNull(mockDialect2.getRuleDescr());
RuleDescr ruleDescr = new RuleDescr("test rule");
ruleDescr.addAttribute(new AttributeDescr("dialect", "mock"));
ruleDescr.setLhs(new AndDescr());
EvalDescr evalDescr = new EvalDescr();
ruleDescr.getLhs().addDescr(evalDescr);
PackageDescr pkgDescr = new PackageDescr("org.pkg1");
pkgDescr.addImport(new ImportDescr("java.util.HashMap"));
FunctionImportDescr functionImportDescr = new FunctionImportDescr();
functionImportDescr.setTarget("java.lang.System.currentTimeMillis");
pkgDescr.addFunctionImport(functionImportDescr);
pkgDescr.addRule(ruleDescr);
builder.addPackage(pkgDescr);
assertSame(ruleDescr, mockDialect2.getRuleDescr());
assertTrue(mockDialect2.getImport().contains("java.util.HashMap"));
assertTrue(mockDialect2.getStaticImport().contains("java.lang.System.currentTimeMillis"));
assertEquals("eval was built", evalDescr.getContent());
assertEquals("consequence was built", ruleDescr.getConsequence());
assertTrue(mockDialect2.isCompileAll());
assertNotNull(pkg.getRule("test rule"));
// make sure there were no other general errors.
assertFalse(builder.hasErrors());
}
use of org.drools.compiler.lang.descr.FunctionImportDescr in project drools by kiegroup.
the class RuleParserTest method testFunctionImport2.
@Test
public void testFunctionImport2() throws Exception {
PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "test_FunctionImport.drl");
assertEquals(2, pkg.getFunctionImports().size());
assertEquals("abd.def.x", ((FunctionImportDescr) pkg.getFunctionImports().get(0)).getTarget());
assertFalse(((FunctionImportDescr) pkg.getFunctionImports().get(0)).getStartCharacter() == -1);
assertFalse(((FunctionImportDescr) pkg.getFunctionImports().get(0)).getEndCharacter() == -1);
assertEquals("qed.wah.*", ((FunctionImportDescr) pkg.getFunctionImports().get(1)).getTarget());
assertFalse(((FunctionImportDescr) pkg.getFunctionImports().get(1)).getStartCharacter() == -1);
assertFalse(((FunctionImportDescr) pkg.getFunctionImports().get(1)).getEndCharacter() == -1);
}
Aggregations