All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.umlg.javageneration.util.UmlgClassOperations Maven / Gradle / Ivy

package org.umlg.javageneration.util;

import org.apache.commons.lang.StringUtils;
import org.eclipse.ocl.uml.CollectionType;
import org.eclipse.uml2.uml.*;
import org.eclipse.uml2.uml.Class;
import org.eclipse.uml2.uml.Enumeration;
import org.eclipse.uml2.uml.internal.operations.ClassOperations;
import org.umlg.framework.ModelLoader;
import org.umlg.java.metamodel.OJPackage;
import org.umlg.java.metamodel.OJPathName;
import org.umlg.java.metamodel.OJVisibilityKind;

import java.util.*;

public class UmlgClassOperations extends ClassOperations {

    public static boolean hasLookupProperty(org.eclipse.uml2.uml.Class clazz) {
        Set properties = getAllProperties(clazz);
        for (Property property : properties) {
            if (new PropertyWrapper(property).hasLookup()) {
                return true;
            }
        }
        return false;
    }

    public static Set getChildPropertiesToDelete(org.eclipse.uml2.uml.Class clazz) {
        Set result = new HashSet<>();
        Set ownedProperties = getAllOwnedProperties(clazz);
        for (Property p : ownedProperties) {
            PropertyWrapper pWrap = new PropertyWrapper(p);
            if (!pWrap.isDerived() && (pWrap.isComposite() || (!pWrap.isPrimitive() && !pWrap.isEnumeration() && pWrap.getOtherEnd() == null))) {
                result.add(p);
            }
        }
        return result;
    }

    public static Set getPropertiesToClearOnDeletion(org.eclipse.uml2.uml.Class clazz) {
        Set result = new HashSet<>();
        Set ownedProperties = getAllOwnedProperties(clazz);
        for (Property p : ownedProperties) {
            PropertyWrapper pWrap = new PropertyWrapper(p);
            if (!pWrap.isDerived() && !pWrap.isComposite() && !pWrap.isPrimitive() && !pWrap.isEnumeration()) {
                result.add(p);
            }
        }
        return result;
    }

    /**
     * Returns all properties that are to be persisted on the clazz's vertex.
     * i.e. only simple types and enums
     */
    public static Set getOnePrimitiveOrEnumProperties(org.eclipse.uml2.uml.Class clazz) {
        Set result = new HashSet();
        for (Property p : clazz.getAttributes()) {
            PropertyWrapper pWrap = new PropertyWrapper(p);
            if (!pWrap.isDerived() && pWrap.isOne() && (pWrap.isPrimitive() || pWrap.isEnumeration())) {
                result.add(p);
            }
        }
        return result;
    }

//    public static Set getPrimitiveOrEnumOrComponentsProperties(org.eclipse.uml2.uml.Class clazz) {
//        Set result = new HashSet();
//        for (Property p : getAllOwnedProperties(clazz)) {
//            PropertyWrapper pWrap = new PropertyWrapper(p);
//            if ((pWrap.isOne() && !pWrap.isDerived() && !pWrap.isQualifier())
//                    || (!pWrap.isDerived() && pWrap.isMany() && (pWrap.isPrimitive() || pWrap.isEnumeration())) ||
//                    pWrap.isComponent()) {
//                result.add(p);
//            }
//        }
//        return result;
//    }

    public static Set getPrimitiveOrEnumOrComponentsExcludeOneProperties(org.eclipse.uml2.uml.Class clazz) {
        Set result = new HashSet();
        for (Property p : getAllOwnedProperties(clazz)) {
            PropertyWrapper pWrap = new PropertyWrapper(p);
            if (!pWrap.isDerived() && !pWrap.isQualifier()) {
                if (pWrap.isDataType() || pWrap.isComponent()) {
                    result.add(p);
                }
            }
        }
        return result;
    }

    public static Set getPropertiesThatHaveAndEdge(Class clazz) {
        Set result = new HashSet();
        for (Property p : getAllOwnedProperties(clazz)) {
            PropertyWrapper pWrap = new PropertyWrapper(p);
            if (!pWrap.isDerived() && !pWrap.isQualifier()) {
                if (pWrap.isMany() || !pWrap.isPrimitive()) {
                    result.add(p);
                }
            }
        }
        return result;
    }

    public static Set getOneProperties(org.eclipse.uml2.uml.Class clazz) {
        Set result = new HashSet();
        for (Property p : getAllOwnedProperties(clazz)) {
            PropertyWrapper pWrap = new PropertyWrapper(p);
            if (!pWrap.isDerived() && !pWrap.isQualifier()) {
                if (!pWrap.isDataType() && (pWrap.isComponent() || pWrap.isOne())) {
                    //Skip composite owner
                    if (pWrap.isOne() && pWrap.getOtherEnd() != null && pWrap.getOtherEnd().isComposite()) {
                        continue;
                    }
                    result.add(p);
                }
            }
        }
        return result;
    }

    public static Set getNonCompositeRequiredManyProperties(org.eclipse.uml2.uml.Class clazz) {
        Set result = new HashSet<>();
        for (Property p : getAllOwnedProperties(clazz)) {
            PropertyWrapper pWrap = new PropertyWrapper(p);
            if (!pWrap.isDerived() && !pWrap.isQualifier() && !pWrap.isDataType() && pWrap.getLower() > 0 && pWrap.isMany() && !pWrap.isComposite()) {
                result.add(p);
            }
        }
        return result;
    }


    public static Set getNonCompositeProperties(org.eclipse.uml2.uml.Class clazz) {
        Set result = new HashSet();
        for (Property p : getAllOwnedProperties(clazz)) {
            PropertyWrapper pWrap = new PropertyWrapper(p);
            if (!pWrap.isDerived() && !pWrap.isQualifier() && !pWrap.isComposite()) {
                result.add(p);
            }
        }
        return result;
    }

    public static Set getPropertiesForToJson(org.eclipse.uml2.uml.Class clazz) {
        Set result = new HashSet();
        for (Property p : getAllOwnedProperties(clazz)) {
            PropertyWrapper pWrap = new PropertyWrapper(p);
            if (!pWrap.isQualified()) {
                if (pWrap.isComponent() || pWrap.isDataType()) {
                    result.add(p);
                } else if (!pWrap.isDataType() && pWrap.isOne()) {
                    result.add(p);
                }
            }
        }
        return result;
    }

    public static Set getPropertiesForToJsonExcludingCompositeParent(org.eclipse.uml2.uml.Class clazz) {
        Set result = new HashSet();
        for (Property p : getAllOwnedProperties(clazz)) {
            PropertyWrapper pWrap = new PropertyWrapper(p);
            if (!pWrap.isQualified()) {
                if (pWrap.isComponent() || pWrap.isDataType()) {
                    if (!(pWrap.getOtherEnd() != null && pWrap.getOtherEnd().isComposite())) {
                        result.add(p);
                    }
                } else if (!pWrap.isDataType() && pWrap.isOne()) {
                    if (!(pWrap.getOtherEnd() != null && pWrap.getOtherEnd().isComposite())) {
                        result.add(p);
                    }
                }
            }
        }
        return result;
    }

    /*
     * These include all properties that are on the other end of an association.
     * It does not include inherited properties
     * It does not include properties refined associations
     */
    public static Set getAllOwnedProperties(org.eclipse.uml2.uml.Classifier clazz) {
        return filterOutRefinedProperties(getAllOwnedPropertiesIncludingRefinedAssociationMemberEnds(clazz));
    }

    /*
     * These include all properties that are on the other end of an association.
     * It does not include inherited properties
     */
    public static Set getAllOwnedPropertiesIncludingRefinedAssociationMemberEnds(org.eclipse.uml2.uml.Classifier clazz) {

        Set result = new HashSet<>(clazz.getAttributes());
        List associations = clazz.getAssociations();
        for (Association association : associations) {
            List memberEnds = association.getMemberEnds();
            //For the case of an association to itself both ends are owned.
            //In particular to generate the collection initialization
            if (memberEnds.get(0).getType().equals(clazz) && memberEnds.get(1).getType().equals(clazz)) {
                result.addAll(memberEnds);
            } else {
                for (Property property : memberEnds) {
                    if (property.getType() != clazz) {
                        result.add(property);
                    }
                }
            }
        }
        if (clazz instanceof Class) {
            result.addAll(getPropertiesOnRealizedInterfaces((Class)clazz));
        }
        return result;
    }

    /*
     * These include all properties that are on the other end of an association.
     * It includes inherited properties
     */
    public static Set getAllProperties(Classifier classifier) {
        Set result = new HashSet<>(classifier.getAllAttributes());
        Set associations = getAllAssociations(classifier);
        for (Association association : associations) {

            List memberEnds = association.getMemberEnds();
            Property memberEnd1 = memberEnds.get(0);
            Property memberEnd2 = memberEnds.get(1);

            // This is for the case of hierarchies, i.e association to itself
            if (isSpecializationOf(classifier, memberEnd1.getType()) && isSpecializationOf(classifier, memberEnd2.getType())) {
                result.add(memberEnd1);
                result.add(memberEnd2);
            }
            // This is to prevent getting the near side of an association
            if (!isSpecializationOf(classifier, memberEnd1.getType())) {
                result.add(memberEnd1);
            }
            // This is to prevent getting the near side of an association
            if (!isSpecializationOf(classifier, memberEnd2.getType())) {
                result.add(memberEnd2);
            }

        }
        if (classifier instanceof Class) {
            result.addAll(getPropertiesOnRealizedInterfaces((Class)classifier));
        }
        return filterOutRefinedProperties(result);
    }

    private static Set filterOutRefinedProperties(Set result) {
        Set properties = new HashSet<>();
        //Ignore associations that are refined abstraction
        for (Property p : result) {
            if (!new PropertyWrapper(p).isRefined()) {
                properties.add(p);
            }
        }
        return properties;
    }

    private static List filterOutRefinedAssociation(List result) {
        List associations = new ArrayList<>();
        //Ignore associations that are refined abstraction
        for (Association a : result) {
            if (!ModelLoader.INSTANCE.isRefinedAssociation(a)) {
                associations.add(a);
            }
        }
        return associations;
    }

    public static Set getPropertiesOnRealizedInterfaces(org.eclipse.uml2.uml.Class clazz) {
        Set result = new HashSet();
        List interfaces = clazz.getImplementedInterfaces();
        for (Interface inf : interfaces) {
            Set properties = UmlgInterfaceOperations.getAllProperties(inf);
            for (Property p : properties) {
                result.add(p);
            }
        }
        return result;
    }

    public static Set getOtherEndToComposite(Classifier classifier) {
        Set compositeOwners = new HashSet();
        Set associations = getAllAssociations(classifier);
        for (Association association : associations) {
            List memberEnds = association.getMemberEnds();
            for (Property property : memberEnds) {
                if (!property.isComposite() /*&& property.getType() != classifier*/ && property.getOtherEnd().isComposite()
                        && isSpecializationOf(classifier, property.getOtherEnd().getType())) {
                    compositeOwners.add(property);
                }
            }
        }
        return compositeOwners;
    }

    /*
     * Only BehavioredClassifier can realize interfaces
     */
    public static boolean isSpecializationOf(Classifier special, Type type) {
        if (special == type) {
            return true;
        }
        if ((special instanceof BehavioredClassifier) && ((BehavioredClassifier) special).getAllImplementedInterfaces().contains(type)) {
            return true;
        }
        for (Classifier general : special.getGenerals()) {
            if (isSpecializationOf(general, type)) {
                return true;
            }
        }
        return false;
    }

    public static Set getAllAssociations(Classifier classifier) {
        Set result = new HashSet();
        if (classifier instanceof Class) {
            for (Interface implementedInterface : ((Class) classifier).getAllImplementedInterfaces()) {
                result.addAll(filterOutRefinedAssociation(implementedInterface.getAssociations()));
            }
        }
        getAllAssociationsFromGenerals(classifier, result);
        return result;
    }

    public static void getAllAssociationsFromGenerals(Classifier classifier, Set result) {
        result.addAll(classifier.getAssociations());
        for (Classifier general : classifier.getGenerals()) {
            getAllAssociationsFromGenerals(general, result);
        }
    }

    public static Set getOtherEndToCompositePathName(Class clazz) {
        Set result = new HashSet();
        Set endsToComposite = getOtherEndToComposite(clazz);
        for (Property p : endsToComposite) {
            result.add(getPathName(p.getType()));
        }
        return result;
    }

    public static OJPathName getPathName(Type type) {
        String className = Namer.name(type);
        String fullPackageName = Namer.name(type.getNearestPackage());
        OJPathName ojPathName = new OJPathName(fullPackageName + "." + className);
        return ojPathName;
    }

    public static boolean hasSupertype(Class clazz) {
        return !getConcreteGenerals(clazz).isEmpty();
    }

    public static List getConcreteGenerals(Classifier clazz) {
        List result = new ArrayList();
        List generals = clazz.getGenerals();
        for (Classifier classifier : generals) {
            if (classifier instanceof Class) {
                result.add((Class) classifier);
            }
        }
        return result;
    }

    private static void getConcreteImplementations(Set result, Classifier clazz) {
        if (!clazz.isAbstract() && !(clazz instanceof Interface)) {
            result.add(clazz);
        }
        List generalizations = ModelLoader.INSTANCE.getSpecifics(clazz);
        for (Generalization generalization : generalizations) {
            Classifier specific = generalization.getSpecific();
            getConcreteImplementations(result, specific);
        }
    }

    private static void internalGetSpecializations(Set result, Classifier clazz) {
        result.add(clazz);
        List generalizations = ModelLoader.INSTANCE.getSpecifics(clazz);
        for (Generalization generalization : generalizations) {
            Classifier specific = generalization.getSpecific();
            internalGetSpecializations(result, specific);
        }
    }

    public static Set getRealizationWithCompositeOwner(Interface inf) {
        Set result = new HashSet();
        List interfaceRealizations = ModelLoader.INSTANCE.getInterfaceRealization(inf);
        for (InterfaceRealization interfaceRealization : interfaceRealizations) {
            BehavioredClassifier c = interfaceRealization.getImplementingClassifier();
            if (hasCompositeOwner(c)) {
                result.add(c);
            }
        }
        return result;
    }

    public static Set getConcreteRealization(Classifier classifier) {
        if (classifier instanceof Interface) {
            return getConcreteRealizationForInterface((Interface) classifier);
        } else {
            return getConcreteRealizationForClass((Class) classifier);
        }
    }

    private static Set getConcreteRealizationForInterface(Interface inf) {
        Set result = new HashSet();
        List interfaceRealizations = ModelLoader.INSTANCE.getInterfaceRealization(inf);
        for (InterfaceRealization interfaceRealization : interfaceRealizations) {
            BehavioredClassifier c = interfaceRealization.getImplementingClassifier();
            result.add(c);
        }
        return result;
    }

    public static Set getConcreteRealizationForClass(Class clazz) {
        return getSpecializations(clazz);
    }

    public static Set getRealizationWithoutCompositeOwner(Interface inf) {
        Set result = new HashSet();
        List interfaceRealizations = ModelLoader.INSTANCE.getInterfaceRealization(inf);
        for (InterfaceRealization interfaceRealization : interfaceRealizations) {
            BehavioredClassifier c = interfaceRealization.getImplementingClassifier();
            if (!hasCompositeOwner(c)) {
                result.add(c);
            }
        }
        return result;
    }

    public static Set getSpecializationWithCompositeOwner(Classifier clazz) {
        Set result = new HashSet();
        Set specializations = getSpecializations(clazz);
        for (Classifier c : specializations) {
            if (hasCompositeOwner(c)) {
                result.add(c);
            }
        }
        return result;
    }

    public static Set getConcreteSpecializationsWithoutCompositeOwner(Classifier clazz) {
        Set result = new HashSet();
        Set specializations = getSpecializations(clazz);
        for (Classifier c : specializations) {
            if (!c.isAbstract() && c instanceof Class && !hasCompositeOwner((Class) c)) {
                result.add((Class) c);
            }
        }
        return result;
    }

    public static Set getSpecializations(Classifier clazz) {
        Set result = new HashSet();
        internalGetSpecializations(result, clazz);
        result.remove(clazz);
        return result;
    }

    /**
     * Returns all concrete implementations sorted by the classifier's name
     *
     * @param clazz
     * @return
     */
    public static SortedSet getConcreteImplementations(Classifier clazz) {
        SortedSet result = new TreeSet(new Comparator() {
            @Override
            public int compare(Classifier o1, Classifier o2) {
                return o1.getName().compareTo(o2.getName());
            }
        });
        getConcreteImplementations(result, clazz);
        if (clazz instanceof Interface) {
            result.addAll(getConcreteRealization((Interface) clazz));
        }
        return result;
    }

    public static boolean hasCompositeOwner(Classifier classifier) {
        return !getOtherEndToComposite(classifier).isEmpty();
    }

    public static String className(Classifier clazz) {
        if (clazz instanceof CollectionType) {
            CollectionType collectionType = (CollectionType) clazz;
            StringBuilder sb = new StringBuilder();
            sb.append(UmlgCollectionKindEnum.from(collectionType.getKind()).getOjPathName().getLast());
            sb.append("<");
            sb.append(className(collectionType.getElementType()));
            sb.append(">");
            return sb.toString();
        } else {

            if (!(clazz instanceof PrimitiveType) && !(clazz instanceof Enumeration) && clazz instanceof DataType) {
                return DataTypeEnum.getPathNameFromDataType((DataType) clazz).getLast();
            } else {
                return Namer.name(clazz);
            }


        }
    }

    public static String getMetaClassName(Classifier clazz) {
        if (clazz instanceof CollectionType) {
            CollectionType collectionType = (CollectionType) clazz;
            StringBuilder sb = new StringBuilder();
            sb.append(UmlgCollectionKindEnum.from(collectionType.getKind()).getOjPathName().getLast());
            sb.append("<");
            sb.append(className(collectionType.getElementType()));
            sb.append(">");
            return sb.toString();
        } else {
            return Namer.getMetaName(clazz);
        }
    }

    public static OJPathName getMetaClassPathName(Classifier clazz) {
        OJPackage ojPackage = new OJPackage(Namer.name(clazz.getNearestPackage()) + ".meta");
        OJPathName result = ojPackage.getPathName().append(UmlgClassOperations.getMetaClassName(clazz));
        return result;
    }

    public static String propertyEnumName(Type type) {
        return Namer.name(type) + "RuntimePropertyEnum";
    }

    public static OJVisibilityKind getVisibility(VisibilityKind visibility) {
        switch (visibility) {
            case PRIVATE_LITERAL:
                return OJVisibilityKind.PRIVATE;
            case PROTECTED_LITERAL:
                return OJVisibilityKind.PROTECTED;
            case PUBLIC_LITERAL:
                return OJVisibilityKind.PUBLIC;
            case PACKAGE_LITERAL:
                return OJVisibilityKind.DEFAULT;
            default:
                throw new RuntimeException("Not supported");
        }
    }

    public static boolean isRoot(Class clazz) {
        return false;
    }

    public static OJPathName getAuditPathName(Class c) {
        OJPathName pathName = getPathName(c);
        return pathName.renameLast(pathName.getLast() + "Audit");
    }

    public static Property getAttribute(Class c, String name) {
        for (Property p : c.getAllAttributes()) {
            if (p.getName().equals(name)) {
                return p;
            }
        }
        return null;
    }

    public static boolean isOnInterface(PropertyWrapper pWrap) {
        return pWrap.getOwningType() instanceof Interface;
    }

    public static boolean isHierarchy(org.eclipse.uml2.uml.Class clazz) {
        if (realizesHierarchy(clazz)) {
            return true;
        }
        List generals = getGenerals(clazz);
        for (Classifier general : generals) {
            if (realizesHierarchy((Class) general)) {
                return true;
            }
        }
        return false;
    }

    private static boolean realizesHierarchy(org.eclipse.uml2.uml.Class clazz) {
        List realizedInterfaces = clazz.getImplementedInterfaces();
        for (Interface interface1 : realizedInterfaces) {
            if (interface1.getQualifiedName().equals("umlglib::org::umlg::hierarchy::Hierarchy")) {
                return true;
            }
        }
        return false;
    }

    public static boolean isEnumeration(Type owningType) {
        return owningType instanceof Enumeration;
    }

    public static String checkClassConstraintName(Constraint constraint) {
        return "checkClassConstraint" + StringUtils.capitalize(constraint.getName());
    }

    public static boolean isAssociationClass(Class clazz) {
        return clazz instanceof AssociationClass;
    }

    public static List getGeneralizationHierarchy(Class clazz) {
        List result = new ArrayList();
        result.add(clazz);
        getGeneralizationHierarchy(result, clazz);
        return result;
    }

    private static void getGeneralizationHierarchy(List hierarchy, Classifier clazz) {
        List generals = clazz.getGenerals();
        if (generals.size() > 1) {
            throw new IllegalStateException(
                    String.format("Multiple inheritance is not supported! Class %s has more than on generalization.", clazz.getName()));
        }
        if (!generals.isEmpty()) {
            hierarchy.add(generals.get(0));
            getGeneralizationHierarchy(hierarchy, generals.get(0));
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy