use of org.drools.compiler.lang.descr.ImportDescr in project drools by kiegroup.
the class PackageDescrVisitor method visitImports.
private void visitImports(List<ImportDescr> importDescrs) throws IOException, ClassNotFoundException {
HashSet<String> imports = new HashSet<String>();
for (ImportDescr i : importDescrs) {
String fullName = i.getTarget();
String name = fullName.substring(fullName.lastIndexOf(".") + 1);
imports.add(fullName);
Import objectImport = new Import(i, rulePackage);
objectImport.setName(fullName);
objectImport.setShortName(name);
data.add(objectImport);
ObjectType objectType = this.data.getObjectTypeByFullName(fullName);
if (objectType == null) {
objectType = new ObjectType(i);
}
objectType.setName(name);
objectType.setFullName(fullName);
data.add(objectType);
}
PackageHeaderLoader packageHeaderLoader = new PackageHeaderLoader(imports, jars);
for (String factTypeName : packageHeaderLoader.getClassNames()) {
String name = factTypeName.substring(factTypeName.lastIndexOf(".") + 1);
Collection<String> fieldNames = packageHeaderLoader.getFieldNames(factTypeName);
for (String fieldName : fieldNames) {
ObjectType objectType = this.data.getObjectTypeByObjectTypeNameAndPackageName(name, rulePackage.getName());
Field field = data.getFieldByObjectTypeAndFieldName(objectType.getFullName(), fieldName);
if (field == null) {
field = ObjectTypeFactory.createField(objectType.getDescr(), fieldName, objectType);
field.setFieldType(packageHeaderLoader.getFieldType(objectType.getName(), fieldName));
data.add(field);
}
}
}
}
use of org.drools.compiler.lang.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<PackageDescr>());
packageDescrsForPackage.add(packageDescr);
Set<ImportDescr> imports = new HashSet<ImportDescr>();
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<String, AttributeDescr>();
this.packageAttributes.put(packageDescr.getNamespace(), pkgAttributes);
}
for (AttributeDescr attr : packageDescr.getAttributes()) {
pkgAttributes.put(attr.getName(), attr);
}
}
}
use of org.drools.compiler.lang.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 = new KnowledgePackageImpl(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.compiler.lang.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<String, AbstractClassTypeDeclarationDescr>();
List<TypeDefinition> unresolvedTypes = new ArrayList<TypeDefinition>();
List<AbstractClassTypeDeclarationDescr> unsortedDescrs = new ArrayList<AbstractClassTypeDeclarationDescr>();
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.compiler.lang.descr.ImportDescr in project drools by kiegroup.
the class DialectUtil method findClassByName.
public static Class<?> findClassByName(RuleBuildContext context, String className) {
if (className == null) {
return null;
}
String namespace = context.getRuleDescr().getNamespace();
KnowledgeBuilderImpl packageBuilder = context.getKnowledgeBuilder();
Class<?> clazz = null;
try {
clazz = Class.forName(className.indexOf('.') < 0 ? namespace + "." + className : className, false, packageBuilder.getRootClassLoader());
} catch (ClassNotFoundException e) {
}
if (clazz != null) {
return clazz;
}
Set<String> imports = new HashSet<String>();
List<PackageDescr> pkgDescrs = packageBuilder.getPackageDescrs(namespace);
if (pkgDescrs == null) {
return null;
}
for (PackageDescr pkgDescr : pkgDescrs) {
for (ImportDescr importDescr : pkgDescr.getImports()) {
imports.add(importDescr.getTarget());
}
}
return findClass(className, imports, packageBuilder.getRootClassLoader());
}
Aggregations