use of org.drools.drl.ast.descr.ImportDescr in project drools by kiegroup.
the class ImpactModelBuilderImpl method addPackage.
@Override
public void addPackage(final PackageDescr packageDescr) {
if (compositePackagesMap == null) {
compositePackagesMap = new HashMap<>();
if (compositePackages != null) {
for (CompositePackageDescr pkg : compositePackages) {
compositePackagesMap.put(pkg.getNamespace(), pkg);
}
} else {
compositePackagesMap.put(packageDescr.getNamespace(), new CompositePackageDescr(packageDescr.getResource(), packageDescr));
}
compositePackages = null;
}
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 KnowledgeBuilderImpl method initPackage.
private void initPackage(PackageDescr packageDescr) {
// Gather all imports for all PackageDescrs for the current package and replicate into
// all PackageDescrs for the current package, thus maintaining a complete list of
// ImportDescrs for all PackageDescrs for the current package.
List<PackageDescr> packageDescrsForPackage = packages.computeIfAbsent(packageDescr.getName(), k -> new ArrayList<>());
packageDescrsForPackage.add(packageDescr);
Set<ImportDescr> imports = new HashSet<>();
for (PackageDescr pd : packageDescrsForPackage) {
imports.addAll(pd.getImports());
}
for (PackageDescr pd : packageDescrsForPackage) {
pd.getImports().clear();
pd.addAllImports(imports);
}
// Copy package level attributes for inclusion on individual rules
if (!packageDescr.getAttributes().isEmpty()) {
Map<String, AttributeDescr> pkgAttributes = packageAttributes.get(packageDescr.getNamespace());
if (pkgAttributes == null) {
pkgAttributes = new HashMap<>();
this.packageAttributes.put(packageDescr.getNamespace(), pkgAttributes);
}
for (AttributeDescr attr : packageDescr.getAttributes()) {
pkgAttributes.put(attr.getName(), attr);
}
}
}
use of org.drools.drl.ast.descr.ImportDescr in project drools by kiegroup.
the class KnowledgeBuilderImpl method mergePackage.
void mergePackage(PackageRegistry pkgRegistry, PackageDescr packageDescr) {
for (final ImportDescr importDescr : packageDescr.getImports()) {
pkgRegistry.addImport(importDescr);
}
normalizeTypeDeclarationAnnotations(packageDescr, pkgRegistry.getTypeResolver());
processAccumulateFunctions(pkgRegistry, packageDescr);
processEntryPointDeclarations(pkgRegistry, packageDescr);
Map<String, AbstractClassTypeDeclarationDescr> unprocesseableDescrs = new HashMap<>();
List<TypeDefinition> unresolvedTypes = new ArrayList<>();
List<AbstractClassTypeDeclarationDescr> unsortedDescrs = new ArrayList<>();
unsortedDescrs.addAll(packageDescr.getTypeDeclarations());
unsortedDescrs.addAll(packageDescr.getEnumDeclarations());
typeBuilder.processTypeDeclarations(packageDescr, pkgRegistry, unsortedDescrs, unresolvedTypes, unprocesseableDescrs);
for (AbstractClassTypeDeclarationDescr descr : unprocesseableDescrs.values()) {
this.addBuilderResult(new TypeDeclarationError(descr, "Unable to process type " + descr.getTypeName()));
}
processOtherDeclarations(pkgRegistry, packageDescr);
normalizeRuleAnnotations(packageDescr, pkgRegistry.getTypeResolver());
}
use of org.drools.drl.ast.descr.ImportDescr in project drools by kiegroup.
the class KnowledgeBuilderImpl method createPackageRegistry.
private PackageRegistry createPackageRegistry(PackageDescr packageDescr) {
initPackage(packageDescr);
InternalKnowledgePackage pkg;
if (this.kBase == null || (pkg = this.kBase.getPackage(packageDescr.getName())) == null) {
// there is no rulebase or it does not define this package so define it
pkg = CoreComponentFactory.get().createKnowledgePackage((packageDescr.getName()));
pkg.setClassFieldAccessorCache(new ClassFieldAccessorCache(this.rootClassLoader));
// if there is a rulebase then add the package.
if (this.kBase != null) {
try {
pkg = (InternalKnowledgePackage) this.kBase.addPackage(pkg).get();
} catch (InterruptedException | ExecutionException e) {
throw new RuntimeException(e);
}
} else {
// the RuleBase will also initialise the
pkg.getDialectRuntimeRegistry().onAdd(this.rootClassLoader);
}
}
PackageRegistry pkgRegistry = new PackageRegistry(rootClassLoader, configuration, pkg);
// add default import for this namespace
pkgRegistry.addImport(new ImportDescr(packageDescr.getNamespace() + ".*"));
for (ImportDescr importDescr : packageDescr.getImports()) {
pkgRegistry.registerImport(importDescr.getTarget());
}
return pkgRegistry;
}
use of org.drools.drl.ast.descr.ImportDescr in project drools by kiegroup.
the class TypeDeclarationUtils method resolveType.
/**
* Tries to determine the namespace (package) of a simple type chosen to be
* the superclass of a declared bean. Looks among imports, local
* declarations and previous declarations. Means that a class can't extend
* another class declared in package that has not been loaded yet.
*
* @param klass the simple name of the class
* @param packageDescr the descriptor of the package the base class is declared in
* @param pkgRegistry the current package registry
* @return the fully qualified name of the superclass
*/
public static String resolveType(String klass, PackageDescr packageDescr, PackageRegistry pkgRegistry) {
String arraySuffix = "";
int arrayIndex = klass.indexOf("[");
if (arrayIndex >= 0) {
arraySuffix = klass.substring(arrayIndex);
klass = klass.substring(0, arrayIndex);
}
// look among imports
String temp = klass;
while (temp.length() > 0) {
for (ImportDescr id : packageDescr.getImports()) {
String fqKlass = id.getTarget();
if (fqKlass.endsWith("." + temp)) {
// logger.info("Replace supertype " + sup + " with full name " + id.getTarget());
fqKlass = fqKlass.substring(0, fqKlass.lastIndexOf(temp)) + klass;
return arrayIndex < 0 ? fqKlass : fqKlass + arraySuffix;
}
}
temp = temp.substring(0, Math.max(0, temp.lastIndexOf('.')));
}
// look among local declarations
if (pkgRegistry != null) {
for (String declaredName : pkgRegistry.getPackage().getTypeDeclarations().keySet()) {
if (declaredName.equals(klass)) {
TypeDeclaration typeDeclaration = pkgRegistry.getPackage().getTypeDeclaration(declaredName);
if (typeDeclaration.getTypeClass() != null) {
klass = typeDeclaration.getTypeClass().getName();
}
}
}
}
if (!klass.contains(".") && packageDescr.getNamespace() != null && !packageDescr.getNamespace().isEmpty()) {
for (AbstractClassTypeDeclarationDescr td : packageDescr.getClassAndEnumDeclarationDescrs()) {
if (klass.equals(td.getTypeName())) {
if (td.getType().getFullName().contains(".")) {
klass = td.getType().getFullName();
}
}
}
}
return arrayIndex < 0 ? klass : klass + arraySuffix;
}
Aggregations