use of org.drools.drl.ast.descr.QualifiedName in project drools by kiegroup.
the class ClassDefinitionFactory method createClassDefinition.
protected ClassDefinition createClassDefinition(AbstractClassTypeDeclarationDescr typeDescr, TypeDeclaration type) {
// extracts type, supertype and interfaces
String fullName = typeDescr.getType().getFullName();
if (type.getKind().equals(TypeDeclaration.Kind.CLASS)) {
TypeDeclarationDescr tdescr = (TypeDeclarationDescr) typeDescr;
if (tdescr.getSuperTypes().size() > 1) {
kbuilder.addBuilderResult(new TypeDeclarationError(typeDescr, "Declared class " + fullName + " - has more than one supertype;"));
return null;
} else if (tdescr.getSuperTypes().isEmpty()) {
tdescr.addSuperType("java.lang.Object");
}
}
Traitable traitableAnn = getTypedAnnotation(typeDescr, Traitable.class);
boolean traitable = traitableAnn != null;
String[] fullSuperTypes = new String[typeDescr.getSuperTypes().size() + 1];
int j = 0;
for (QualifiedName qname : typeDescr.getSuperTypes()) {
fullSuperTypes[j++] = qname.getFullName();
}
fullSuperTypes[j] = Thing.class.getName();
List<String> interfaceList = new ArrayList<>();
interfaceList.add(traitable ? Externalizable.class.getName() : Serializable.class.getName());
if (traitable) {
interfaceList.add(TraitableBean.class.getName());
}
String[] interfaces = interfaceList.toArray(new String[interfaceList.size()]);
// prepares a class definition
ClassDefinition def;
switch(type.getKind()) {
case TRAIT:
def = new ClassDefinition(fullName, Object.class.getName(), fullSuperTypes);
break;
case ENUM:
def = new EnumClassDefinition(fullName, fullSuperTypes[0], null);
break;
case CLASS:
default:
def = new ClassDefinition(fullName, fullSuperTypes[0], interfaces);
def.setTraitable(traitable, traitableAnn != null && traitableAnn.logical());
}
return def;
}
use of org.drools.drl.ast.descr.QualifiedName in project drools by kiegroup.
the class ClassHierarchyManager method mergeInheritedFields.
/**
* In order to build a declared class, the fields inherited from its
* superclass(es) are added to its declaration. Inherited descriptors are
* marked as such to distinguish them from native ones. Various scenarioes
* are possible. (i) The superclass has been declared in the DRL as well :
* the fields are cloned as inherited (ii) The superclass is imported
* (external), but some of its fields have been tagged with metadata (iii)
* The superclass is imported.
* <p>
* The search for field descriptors is carried out in the order. (i) and
* (ii+iii) are mutually exclusive. The search is as such: (i) The
* superclass' declared fields are used to build the base class additional
* fields (iii) The superclass is inspected to discover its (public) fields,
* from which descriptors are generated (ii) Both (i) and (iii) are applied,
* but the declared fields override the inspected ones
*
* @param typeDescr The base class descriptor, to be completed with the inherited
* fields descriptors
*/
protected void mergeInheritedFields(TypeDeclarationDescr typeDescr, Map<String, AbstractClassTypeDeclarationDescr> unprocessableDescrs, TypeResolver typeResolver) {
if (typeDescr.getSuperTypes().isEmpty()) {
return;
}
for (int j = typeDescr.getSuperTypes().size() - 1; j >= 0; j--) {
QualifiedName qname = typeDescr.getSuperTypes().get(j);
String simpleSuperTypeName = qname.getName();
String superTypePackageName = qname.getNamespace();
String fullSuper = qname.getFullName();
mergeFields(simpleSuperTypeName, superTypePackageName, fullSuper, typeDescr, unprocessableDescrs, typeResolver);
}
}
use of org.drools.drl.ast.descr.QualifiedName 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.drl.ast.descr.QualifiedName in project drools by kiegroup.
the class ClassHierarchyManager method sortByHierarchy.
/**
* Utility method to sort declared beans. Linearizes the hierarchy,
* i.e.generates a sequence of declaration such that, if Sub is subclass of
* Sup, then the index of Sub will be > than the index of Sup in the
* resulting collection. This ensures that superclasses are processed before
* their subclasses
*/
protected List<AbstractClassTypeDeclarationDescr> sortByHierarchy(Collection<AbstractClassTypeDeclarationDescr> unsortedDescrs, KnowledgeBuilderImpl kbuilder) {
taxonomy = new HashMap<>();
Map<QualifiedName, AbstractClassTypeDeclarationDescr> cache = new HashMap<>();
for (AbstractClassTypeDeclarationDescr tdescr : unsortedDescrs) {
cache.put(tdescr.getType(), tdescr);
}
for (AbstractClassTypeDeclarationDescr tdescr : unsortedDescrs) {
QualifiedName name = tdescr.getType();
Collection<QualifiedName> supers = taxonomy.get(name);
if (supers == null) {
supers = new ArrayList<>();
taxonomy.put(name, supers);
} else {
kbuilder.addBuilderResult(new TypeDeclarationError(tdescr, "Found duplicate declaration for type " + tdescr.getType()));
}
boolean circular = false;
for (QualifiedName sup : tdescr.getSuperTypes()) {
if (!Object.class.getName().equals(name.getFullName())) {
if (!hasCircularDependency(tdescr.getType(), sup, taxonomy)) {
if (cache.containsKey(sup)) {
supers.add(sup);
}
} else {
circular = true;
kbuilder.addBuilderResult(new TypeDeclarationError(tdescr, "Found circular dependency for type " + tdescr.getTypeName()));
break;
}
}
}
if (circular) {
tdescr.getSuperTypes().clear();
}
}
for (AbstractClassTypeDeclarationDescr tdescr : unsortedDescrs) {
for (TypeFieldDescr field : tdescr.getFields().values()) {
QualifiedName name = tdescr.getType();
QualifiedName typeName = new QualifiedName(field.getPattern().getGenericType().getRawType());
if (!hasCircularDependency(name, typeName, taxonomy)) {
if (cache.containsKey(typeName)) {
taxonomy.get(name).add(typeName);
}
} else {
field.setRecursive(true);
}
}
}
List<QualifiedName> sorted = new HierarchySorter<QualifiedName>().sort(taxonomy);
ArrayList list = new ArrayList(sorted.size());
for (QualifiedName name : sorted) {
list.add(cache.get(name));
}
return list;
}
use of org.drools.drl.ast.descr.QualifiedName in project drools by kiegroup.
the class TypeDeclarationBuilder method mergeTypeDescriptors.
private boolean mergeTypeDescriptors(AbstractClassTypeDeclarationDescr prev, AbstractClassTypeDeclarationDescr descr) {
boolean isDef1 = isDefinition(prev);
boolean isDef2 = isDefinition(descr);
if (isDef1 && isDef2) {
return false;
}
if (!prev.getSuperTypes().isEmpty() && !descr.getSuperTypes().isEmpty() && prev.getSuperTypes().size() != descr.getSuperTypes().size()) {
return false;
}
if (prev.getSuperTypes().isEmpty()) {
for (QualifiedName qn : descr.getSuperTypes()) {
((TypeDeclarationDescr) prev).addSuperType(qn);
}
}
if (prev.getFields().isEmpty()) {
for (String fieldName : descr.getFields().keySet()) {
prev.addField(descr.getFields().get(fieldName));
}
}
for (AnnotationDescr ad : descr.getAnnotations()) {
prev.addQualifiedAnnotation(ad);
}
for (AnnotationDescr ad : prev.getAnnotations()) {
if (!descr.getAnnotations().contains(ad)) {
descr.addQualifiedAnnotation(ad);
}
}
return true;
}
Aggregations