use of org.drools.core.rule.TypeDeclaration in project drools by kiegroup.
the class ClassHierarchyManager method addDeclarationToPackagePreservingOrder.
public void addDeclarationToPackagePreservingOrder(TypeDeclaration type, AbstractClassTypeDeclarationDescr typeDescr, InternalKnowledgePackage tgtPackage, Map<String, PackageRegistry> pkgRegistryMap) {
Collection<QualifiedName> parents = taxonomy.get(new QualifiedName(type.getFullName()));
int index = getSortedDescriptors().indexOf(typeDescr);
if (parents != null && !parents.isEmpty()) {
for (QualifiedName parentName : parents) {
String nameSpace = parentName.getNamespace();
String name = parentName.getName();
PackageRegistry parentPkgRegistry = pkgRegistryMap.get(nameSpace);
if (parentPkgRegistry != null) {
TypeDeclaration parentDeclaration = parentPkgRegistry.getPackage().getTypeDeclaration(name);
if (parentDeclaration != null && parentDeclaration.getNature() == TypeDeclaration.Nature.DEFINITION) {
index = Math.max(index, parentDeclaration.getOrder());
}
}
}
}
type.setOrder(index + 1);
tgtPackage.addTypeDeclaration(type);
}
use of org.drools.core.rule.TypeDeclaration in project drools by kiegroup.
the class KnowledgeBuilderImpl method addPackage.
public synchronized void addPackage(InternalKnowledgePackage newPkg) {
PackageRegistry pkgRegistry = this.pkgRegistryMap.get(newPkg.getName());
InternalKnowledgePackage pkg = null;
if (pkgRegistry != null) {
pkg = pkgRegistry.getPackage();
}
if (pkg == null) {
PackageDescr packageDescr = new PackageDescr(newPkg.getName());
pkgRegistry = getOrCreatePackageRegistry(packageDescr);
mergePackage(this.pkgRegistryMap.get(packageDescr.getNamespace()), packageDescr);
pkg = pkgRegistry.getPackage();
}
// first merge anything related to classloader re-wiring
pkg.getDialectRuntimeRegistry().merge(newPkg.getDialectRuntimeRegistry(), this.rootClassLoader);
if (newPkg.getFunctions() != null) {
for (Map.Entry<String, Function> entry : newPkg.getFunctions().entrySet()) {
if (pkg.getFunctions().containsKey(entry.getKey())) {
addBuilderResult(new DuplicateFunction(entry.getValue(), this.configuration));
}
pkg.addFunction(entry.getValue());
}
}
pkg.getClassFieldAccessorStore().merge(newPkg.getClassFieldAccessorStore());
pkg.getDialectRuntimeRegistry().onBeforeExecute();
// we have to do this before the merging, as it does some classloader resolving
TypeDeclaration lastType = null;
try {
// Resolve the class for the type declaation
if (newPkg.getTypeDeclarations() != null) {
// add type declarations
for (TypeDeclaration type : newPkg.getTypeDeclarations().values()) {
lastType = type;
type.setTypeClass(this.rootClassLoader.loadClass(type.getTypeClassName()));
}
}
} catch (ClassNotFoundException e) {
throw new RuntimeException("unable to resolve Type Declaration class '" + lastType.getTypeName() + "'");
}
// now merge the new package into the existing one
mergePackage(pkg, newPkg);
}
use of org.drools.core.rule.TypeDeclaration in project drools by kiegroup.
the class TypeDeclarationBuilder method createBean.
protected void createBean(AbstractClassTypeDeclarationDescr typeDescr, PackageRegistry pkgRegistry, ClassHierarchyManager hierarchyManager, List<TypeDefinition> unresolvedTypes, Map<String, AbstractClassTypeDeclarationDescr> unprocesseableDescrs) {
// descriptor needs fields inherited from superclass
if (typeDescr instanceof TypeDeclarationDescr) {
hierarchyManager.inheritFields(pkgRegistry, typeDescr, hierarchyManager.getSortedDescriptors(), unresolvedTypes, unprocesseableDescrs);
}
TypeDeclaration type = typeDeclarationFactory.processTypeDeclaration(pkgRegistry, typeDescr);
boolean success = !kbuilder.hasErrors();
try {
// the type declaration is generated in any case (to be used by subclasses, if any)
// the actual class will be generated only if needed
ClassDefinition def = null;
if (success) {
def = classDefinitionFactory.generateDeclaredBean(typeDescr, type, pkgRegistry, unresolvedTypes, unprocesseableDescrs);
// this has to be done after the classDef has been generated
if (!type.isNovel()) {
typeDeclarationFactory.checkRedeclaration(typeDescr, type, pkgRegistry);
}
}
success = (def != null) && (!kbuilder.hasErrors());
if (success) {
updateTraitInformation(typeDescr, type, def, pkgRegistry);
}
success = !kbuilder.hasErrors();
if (success) {
declaredClassBuilder.generateBeanFromDefinition(typeDescr, type, pkgRegistry, def);
}
success = !kbuilder.hasErrors();
if (success) {
Class<?> clazz = pkgRegistry.getTypeResolver().resolveType(typeDescr.getType().getFullName());
type.setTypeClass(clazz);
type.setValid(true);
} else {
unprocesseableDescrs.put(typeDescr.getType().getFullName(), typeDescr);
type.setValid(false);
}
typeDeclarationConfigurator.finalize(type, typeDescr, pkgRegistry, kbuilder.getPackageRegistry(), hierarchyManager);
} catch (final ClassNotFoundException e) {
unprocesseableDescrs.put(typeDescr.getType().getFullName(), typeDescr);
kbuilder.addBuilderResult(new TypeDeclarationError(typeDescr, "Class '" + type.getTypeClassName() + "' not found for type declaration of '" + type.getTypeName() + "'"));
}
if (!success) {
unresolvedTypes.add(new TypeDefinition(type, typeDescr));
} else {
registerGeneratedType(typeDescr);
}
}
use of org.drools.core.rule.TypeDeclaration in project drools by kiegroup.
the class TypeDeclarationBuilder method processUnresolvedTypes.
private void processUnresolvedTypes(PackageDescr packageDescr, PackageRegistry pkgRegistry, Collection<AbstractClassTypeDeclarationDescr> unsortedDescrs, List<TypeDefinition> unresolvedTypes, Map<String, AbstractClassTypeDeclarationDescr> unprocesseableDescrs) {
// merge "duplicate" definitions and declarations
unsortedDescrs = compactDefinitionsAndDeclarations(unsortedDescrs, unprocesseableDescrs);
// now sort declarations by mutual dependencies
ClassHierarchyManager classHierarchyManager = new ClassHierarchyManager(unsortedDescrs, kbuilder);
for (AbstractClassTypeDeclarationDescr typeDescr : classHierarchyManager.getSortedDescriptors()) {
PackageRegistry pkgReg = getPackageRegistry(pkgRegistry, packageDescr, typeDescr);
createBean(typeDescr, pkgReg, classHierarchyManager, unresolvedTypes, unprocesseableDescrs);
}
for (AbstractClassTypeDeclarationDescr typeDescr : classHierarchyManager.getSortedDescriptors()) {
if (!unprocesseableDescrs.containsKey(typeDescr.getType().getFullName())) {
PackageRegistry pkgReg = getPackageRegistry(pkgRegistry, packageDescr, typeDescr);
InternalKnowledgePackage pkg = pkgReg.getPackage();
TypeDeclaration type = pkg.getTypeDeclaration(typeDescr.getType().getName());
typeDeclarationConfigurator.wireFieldAccessors(pkgReg, typeDescr, type);
if (kbuilder.getKnowledgeBase() != null) {
// in case of incremental compilatoin (re)register the new type declaration on the existing kbase
kbuilder.getKnowledgeBase().registerTypeDeclaration(type, pkg);
}
}
}
}
use of org.drools.core.rule.TypeDeclaration in project drools by kiegroup.
the class TypeDeclarationBuilder method updateTraitInformation.
protected void updateTraitInformation(AbstractClassTypeDeclarationDescr typeDescr, TypeDeclaration type, ClassDefinition def, PackageRegistry pkgRegistry) {
if (typeDescr.hasAnnotation(Traitable.class) || (!type.getKind().equals(TypeDeclaration.Kind.TRAIT) && kbuilder.getPackageRegistry().containsKey(def.getSuperClass()) && kbuilder.getPackageRegistry(def.getSuperClass()).getTraitRegistry().getTraitables().containsKey(def.getSuperClass()))) {
// traitable
if (type.isNovel()) {
try {
PackageRegistry reg = kbuilder.getPackageRegistry(typeDescr.getNamespace());
String availableName = typeDescr.getType().getFullName();
Class<?> resolvedType = reg.getTypeResolver().resolveType(availableName);
updateTraitDefinition(type, resolvedType, false);
} catch (ClassNotFoundException cnfe) {
// we already know the class exists
}
}
pkgRegistry.getTraitRegistry().addTraitable(def);
} else if (type.getKind().equals(TypeDeclaration.Kind.TRAIT) || typeDescr.hasAnnotation(Trait.class)) {
// trait
if (!type.isNovel()) {
try {
PackageRegistry reg = kbuilder.getPackageRegistry(typeDescr.getNamespace());
String availableName = typeDescr.getType().getFullName();
Class<?> resolvedType = reg.getTypeResolver().resolveType(availableName);
if (!Thing.class.isAssignableFrom(resolvedType)) {
if (!resolvedType.isInterface()) {
kbuilder.addBuilderResult(new TypeDeclarationError(typeDescr, "Unable to redeclare concrete class " + resolvedType.getName() + " as a trait."));
return;
}
updateTraitDefinition(type, resolvedType, false);
String target = typeDescr.getTypeName() + TraitFactory.SUFFIX;
TypeDeclarationDescr tempDescr = new TypeDeclarationDescr();
tempDescr.setNamespace(typeDescr.getNamespace());
tempDescr.setFields(typeDescr.getFields());
tempDescr.setType(target, typeDescr.getNamespace());
tempDescr.setTrait(true);
tempDescr.addSuperType(typeDescr.getType());
tempDescr.setResource(type.getResource());
TypeDeclaration tempDeclr = new TypeDeclaration(target);
tempDeclr.setKind(TypeDeclaration.Kind.TRAIT);
tempDeclr.setTypesafe(type.isTypesafe());
tempDeclr.setNovel(true);
tempDeclr.setTypeClassName(tempDescr.getType().getFullName());
tempDeclr.setResource(type.getResource());
ClassDefinition tempDef = new ClassDefinition(target);
tempDef.setClassName(tempDescr.getType().getFullName());
tempDef.setTraitable(false);
for (FieldDefinition fld : def.getFieldsDefinitions()) {
tempDef.addField(fld);
}
tempDef.setInterfaces(def.getInterfaces());
tempDef.setSuperClass(def.getClassName());
tempDef.setDefinedClass(resolvedType);
tempDef.setAbstrakt(true);
tempDeclr.setTypeClassDef(tempDef);
declaredClassBuilder.generateBeanFromDefinition(tempDescr, tempDeclr, pkgRegistry, tempDef);
try {
Class<?> clazz = pkgRegistry.getTypeResolver().resolveType(tempDescr.getType().getFullName());
tempDeclr.setTypeClass(clazz);
pkgRegistry.getTraitRegistry().addTrait(tempDef.getClassName().replace(TraitFactory.SUFFIX, ""), tempDef);
} catch (ClassNotFoundException cnfe) {
kbuilder.addBuilderResult(new TypeDeclarationError(typeDescr, "Internal Trait extension Class '" + target + "' could not be generated correctly'"));
} finally {
pkgRegistry.getPackage().addTypeDeclaration(tempDeclr);
}
} else {
updateTraitDefinition(type, resolvedType, true);
pkgRegistry.getTraitRegistry().addTrait(def);
}
} catch (ClassNotFoundException cnfe) {
// we already know the class exists
}
} else {
if (def.getClassName().endsWith(TraitFactory.SUFFIX)) {
pkgRegistry.getTraitRegistry().addTrait(def.getClassName().replace(TraitFactory.SUFFIX, ""), def);
} else {
pkgRegistry.getTraitRegistry().addTrait(def);
}
}
}
}
Aggregations