use of org.drools.drl.ast.descr.ImportDescr 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.drl.ast.descr.ImportDescr in project drools by kiegroup.
the class KnowledgeBuilderImpl method buildTypeDeclarations.
protected void buildTypeDeclarations(Collection<CompositePackageDescr> packages) {
Map<String, AbstractClassTypeDeclarationDescr> unprocesseableDescrs = new HashMap<>();
List<TypeDefinition> unresolvedTypes = new ArrayList<>();
List<AbstractClassTypeDeclarationDescr> unsortedDescrs = new ArrayList<>();
for (CompositePackageDescr packageDescr : packages) {
unsortedDescrs.addAll(packageDescr.getTypeDeclarations());
unsortedDescrs.addAll(packageDescr.getEnumDeclarations());
}
getTypeBuilder().processTypeDeclarations(packages, unsortedDescrs, unresolvedTypes, unprocesseableDescrs);
for (CompositePackageDescr packageDescr : packages) {
for (ImportDescr importDescr : packageDescr.getImports()) {
getPackageRegistry(packageDescr.getNamespace()).addImport(importDescr);
}
}
}
use of org.drools.drl.ast.descr.ImportDescr in project drools by kiegroup.
the class XmlDumper method processImportsList.
private String processImportsList(final List imports) {
String importList = "";
for (final Iterator iterator = imports.iterator(); iterator.hasNext(); ) {
final String importString = ((ImportDescr) iterator.next()).getTarget();
final String importTemplate = "<import name=\"" + importString + "\" /> " + XmlDumper.eol;
importList += importTemplate;
}
return importList + XmlDumper.eol;
}
use of org.drools.drl.ast.descr.ImportDescr in project drools by kiegroup.
the class ModelBuilderImpl method addPackage.
@Override
public void addPackage(final PackageDescr packageDescr) {
if (compositePackagesMap == null) {
compositePackagesMap = new HashMap<>();
}
CompositePackageDescr pkgDescr = compositePackagesMap.get(packageDescr.getNamespace());
if (pkgDescr == null) {
compositePackagesMap.put(packageDescr.getNamespace(), new CompositePackageDescr(packageDescr.getResource(), packageDescr));
} else {
pkgDescr.addPackageDescr(packageDescr.getResource(), packageDescr);
}
PackageRegistry pkgRegistry = getOrCreatePackageRegistry(packageDescr);
InternalKnowledgePackage pkg = pkgRegistry.getPackage();
for (final ImportDescr importDescr : packageDescr.getImports()) {
pkgRegistry.addImport(importDescr);
}
for (GlobalDescr globalDescr : packageDescr.getGlobals()) {
try {
Class<?> globalType = pkg.getTypeResolver().resolveType(globalDescr.getType());
addGlobal(globalDescr.getIdentifier(), globalType);
pkg.addGlobal(globalDescr.getIdentifier(), globalType);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
}
use of org.drools.drl.ast.descr.ImportDescr in project drools by kiegroup.
the class TypeDeclarationBuilder method normalizeForeignPackages.
protected void normalizeForeignPackages(PackageDescr packageDescr) {
Map<String, PackageDescr> foreignPackages = null;
for (AbstractClassTypeDeclarationDescr typeDescr : packageDescr.getClassAndEnumDeclarationDescrs()) {
if (kbuilder.filterAccepts(ResourceChange.Type.DECLARATION, typeDescr.getNamespace(), typeDescr.getTypeName())) {
if (!typeDescr.getNamespace().equals(packageDescr.getNamespace())) {
// If the type declaration is for a different namespace, process that separately.
PackageDescr altDescr;
if (foreignPackages == null) {
foreignPackages = new HashMap<>();
}
if (foreignPackages.containsKey(typeDescr.getNamespace())) {
altDescr = foreignPackages.get(typeDescr.getNamespace());
} else {
altDescr = new PackageDescr(typeDescr.getNamespace());
altDescr.setResource(packageDescr.getResource());
foreignPackages.put(typeDescr.getNamespace(), altDescr);
}
if (typeDescr instanceof TypeDeclarationDescr) {
altDescr.addTypeDeclaration((TypeDeclarationDescr) typeDescr);
} else if (typeDescr instanceof EnumDeclarationDescr) {
altDescr.addEnumDeclaration((EnumDeclarationDescr) typeDescr);
}
for (ImportDescr imp : packageDescr.getImports()) {
altDescr.addImport(imp);
}
kbuilder.getOrCreatePackageRegistry(altDescr);
}
}
}
}
Aggregations