Skip to content
java
package com.atoz.utils;

import com.nomagic.magicdraw.uml.ConnectorsCollector;
import com.nomagic.magicdraw.uml.symbols.DiagramPresentationElement;
import com.nomagic.magicdraw.uml.symbols.PresentationElement;
import com.nomagic.magicdraw.uml.symbols.shapes.PartView;
import com.nomagic.magicdraw.uml.symbols.shapes.PortView;
import com.nomagic.uml2.ext.jmi.helpers.ModelHelper;
import com.nomagic.uml2.ext.magicdraw.classes.mdkernel.Class;
import com.nomagic.uml2.ext.magicdraw.classes.mdkernel.Element;
import com.nomagic.uml2.ext.magicdraw.classes.mdkernel.Property;
import com.nomagic.uml2.ext.magicdraw.compositestructures.mdinternalstructures.ConnectableElement;
import com.nomagic.uml2.ext.magicdraw.compositestructures.mdinternalstructures.Connector;
import com.nomagic.uml2.ext.magicdraw.compositestructures.mdinternalstructures.ConnectorEnd;
import com.nomagic.uml2.ext.magicdraw.compositestructures.mdports.Port;
import com.nomagic.uml2.ext.magicdraw.mdprofiles.Stereotype;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class ElementUtils {
    /**
     * 是否是valueProperty
     *
     * @param obj
     * @return
     */
    public static boolean isValueProperty(Element obj) {
        if ("Value Property".equals(obj.getHumanType())) {
            return true;
        }
        return isExtends(obj, "ValueProperty");
    }

    private static boolean isExtends(Element obj, String stereotypeName) {
        List<Stereotype> stereotypes = obj.getAppliedStereotype();
        if (stereotypes == null || stereotypes.isEmpty()) {
            return false;
        }
        for (Stereotype stereotype : stereotypes) {
            Collection<Class> classes = stereotype.getSuperClass();
            if (classes == null || classes.isEmpty()) {
                continue;
            }
            for (Class aClass : classes) {
                if (stereotypeName.equals(aClass.getName())) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 是否是Block
     */
    public static Boolean isBlock(Object object) {
        if (!(object instanceof Class)) {
            return false;
        }
        if ("Block".equals(((Class) object).getHumanType())) {
            return true;
        }
        return isExtends((Element) object, "Block");
    }


/**
     * 是否是Port
     */
    public static Boolean isPort(ConnectableElement obj) {
        if (obj instanceof Port) {
            return true;
        }
        if (isExtends(obj, "ProxyPort")) {
            return true;
        }
        if (isExtends(obj, "Port")) {
            return true;
        }
        if (isExtends(obj, "FullPort")) {
            return true;
        }
        if (isExtends(obj, "FlowPort")) {
            return true;
        }
        return false;
    }
    /**
     * 是否是PartProperty
     */
    public static Boolean isPartProperty(Property obj) {
        if ("Part Property".equals(obj.getHumanType())) {
            return true;
        }
        return isExtends(obj, "PartProperty");
    }

    /**
     * 根据port的connector获取另一端的port
     */
    public static Map<Connector, Port> portToPortByConnector(Port port) {
        Map<Connector, Port> portMap = new HashMap<>();
        for (Connector connector : ConnectorsCollector.collectConnectors(port)) {
            List<ConnectorEnd> connectorEnds = connector.getEnd();
            ConnectorEnd firstEnd = ModelHelper.getFirstEnd(connector);
            if (port.equals(firstEnd.getRole())) {
                portMap.put(connector, ((Port) ModelHelper.getSecondEnd(connector).getRole()));
            } else {
                portMap.put(connector, ((Port) firstEnd.getRole()));
            }
        }
        return portMap;
    }

    /**
     * 判断part是否在指定的图里
     *
     * @param diagram
     * @param part
     * @return partView
     */
    public static PartView checkPartView(DiagramPresentationElement diagram, Property part) {
        return (PartView) checkView(diagram, part);
    }

    /**
     * 判断port是否在指定的图里
     *
     * @param diagram
     * @param port
     * @return partView
     */
    public static PortView checkPortView(DiagramPresentationElement diagram, Port port) {
        return (PortView) checkView(diagram, port);
    }

    /**
     * 判断给定元素是否在指定的图里
     *
     * @param diagram
     * @param element
     * @return partView
     */
    public static PresentationElement checkView(DiagramPresentationElement diagram, Element element) {
        return diagram.findPresentationElement(element, PresentationElement.class);
    }

      /**
     * 根据connector获取两端Port
     * @param connectors
     * @return
     */
    public static Map<Port,Port> getPortToPortByConnector(Collection<Connector> connectors){
        Map<Port,Port> res = new HashMap<>();
        for (Connector connector : connectors) {
           res.put((Port) ModelHelper.getFirstEnd(connector).getRole(),(Port) ModelHelper.getSecondEnd(connector).getRole());
        }
        return res;
    }

     /**
     * 收集当前图所属block下所有的归属的子part,port
     * @param block 所选block
     * @param ownerMap 需要返回的结果map
     * @param blockPart part--初始传null
     */
    public static void collectStructure(Class block, Map<Element, Property> ownerMap, Property blockPart) {
        for (Property element : block.getOwnedAttribute()) {
            if (ElementUtils.isPartProperty(element)) {
                Type type = element.getType();
                if (type != null) {
                    ownerMap.put(element, blockPart);
                    collectStructure((Class) type, ownerMap, element);
                }
            } else if (ElementUtils.isPort(element)) {
                ownerMap.put(element, blockPart);
            }
        }
    }
}