Search in sources :

Example 6 with ImportDescr

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;
}
Also used : GlobalDescr(org.drools.drl.ast.descr.GlobalDescr) FunctionImportDescr(org.drools.drl.ast.descr.FunctionImportDescr) SAXParseException(org.xml.sax.SAXParseException) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) ImportDescr(org.drools.drl.ast.descr.ImportDescr) FunctionImportDescr(org.drools.drl.ast.descr.FunctionImportDescr) PackageDescr(org.drools.drl.ast.descr.PackageDescr)

Example 7 with ImportDescr

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);
        }
    }
}
Also used : LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) CompositePackageDescr(org.drools.compiler.lang.descr.CompositePackageDescr) AbstractClassTypeDeclarationDescr(org.drools.drl.ast.descr.AbstractClassTypeDeclarationDescr) ArrayList(java.util.ArrayList) ImportDescr(org.drools.drl.ast.descr.ImportDescr) FunctionImportDescr(org.drools.drl.ast.descr.FunctionImportDescr) AccumulateImportDescr(org.drools.drl.ast.descr.AccumulateImportDescr)

Example 8 with 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;
}
Also used : Iterator(java.util.Iterator) FunctionImportDescr(org.drools.drl.ast.descr.FunctionImportDescr) ImportDescr(org.drools.drl.ast.descr.ImportDescr)

Example 9 with ImportDescr

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);
        }
    }
}
Also used : GlobalDescr(org.drools.drl.ast.descr.GlobalDescr) PackageRegistry(org.drools.compiler.compiler.PackageRegistry) CompositePackageDescr(org.drools.compiler.lang.descr.CompositePackageDescr) ImportDescr(org.drools.drl.ast.descr.ImportDescr) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 10 with ImportDescr

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);
            }
        }
    }
}
Also used : AbstractClassTypeDeclarationDescr(org.drools.drl.ast.descr.AbstractClassTypeDeclarationDescr) TypeDeclarationDescr(org.drools.drl.ast.descr.TypeDeclarationDescr) EnumDeclarationDescr(org.drools.drl.ast.descr.EnumDeclarationDescr) AbstractClassTypeDeclarationDescr(org.drools.drl.ast.descr.AbstractClassTypeDeclarationDescr) ImportDescr(org.drools.drl.ast.descr.ImportDescr) PackageDescr(org.drools.drl.ast.descr.PackageDescr)

Aggregations

ImportDescr (org.drools.drl.ast.descr.ImportDescr)20 FunctionImportDescr (org.drools.drl.ast.descr.FunctionImportDescr)11 PackageDescr (org.drools.drl.ast.descr.PackageDescr)9 AccumulateImportDescr (org.drools.drl.ast.descr.AccumulateImportDescr)8 GlobalDescr (org.drools.drl.ast.descr.GlobalDescr)5 Test (org.junit.Test)5 HashSet (java.util.HashSet)4 PackageRegistry (org.drools.compiler.compiler.PackageRegistry)4 CompositePackageDescr (org.drools.compiler.lang.descr.CompositePackageDescr)4 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)4 AbstractClassTypeDeclarationDescr (org.drools.drl.ast.descr.AbstractClassTypeDeclarationDescr)4 AttributeDescr (org.drools.drl.ast.descr.AttributeDescr)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 EnumDeclarationDescr (org.drools.drl.ast.descr.EnumDeclarationDescr)2 RuleDescr (org.drools.drl.ast.descr.RuleDescr)2 TypeDeclarationDescr (org.drools.drl.ast.descr.TypeDeclarationDescr)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1