Class ModelGraph


  • public class ModelGraph
    extends Object
    ModelGraph is meant to represent (parts of) EMF models in a simple graph structure. Any element of such a graph may or may not refer to an EObject on the EMF side allowing the user to work with graphs mapping only partially to an existing EMF model. A root node may be specified but note that this class does not tie specific semantics to it.

    There are intentionally no restrictions on how often the same EObject may be referenced by different elements of the graph. Some examples where this may be useful are:

    • One-to-one mappings where edges refer to probably many-valued EReferences, i.e., an EReference may be represented by multiple edges.
    • Abstract model representations where EReferences are represented as nodes.
    Any constraints on the mappings between graph and EMF model need to be either enforced by the user or by extending this class accordingly.

    Nodes and edges are stored in the order of their insertion into the graph.

    Author:
    S. John
    • Constructor Detail

      • ModelGraph

        public ModelGraph()
      • ModelGraph

        public ModelGraph​(Integer expNodes,
                          Integer expEdges,
                          Integer expNodeToObjectReferences,
                          Integer expectedEdgeToObjectReferences)
        Creates a graph anticipating the maximum number of nodes, edges and objects referenced by either the set of nodes or the set of edges. The first two parameters influence the number of buckets of the HashSets used to store nodes and edges internally. The last two parameters influence the number of buckets of the IdentityHashMaps used to store the mappings between objects and their referencing elements. All parameters have an impact on performance and, if possible, should be chosen carefully to prevent excessive rehashing (to few buckets) and lengthy iterations (too many buckets). Parameters are specified as Integer objects to allow null values. In case of a null value the standard size of the respective data structure will be used.
        Parameters:
        expNodes - expected maximum number of nodes stored in the graph
        expEdges - expected maximum number of edges stored in the graph
        expNodeToObjectReferences - expected maximum number of objects referenced by the nodes of the graph.
        expectedEdgeToObjectReferences - expected maximum number of objects referenced by the edges of the graph.
    • Method Detail

      • addEdge

        public boolean addEdge​(ModelEdge edge)
                        throws GraphManipulationException
        Adds an edge to the graph if the edge is not already in the graph and its source and target nodes are part of the graph.
        Parameters:
        edge - edge to be added to the graph
        Returns:
        true if the edge has successfully been added - false otherwise
        Throws:
        GraphManipulationException - if edge is not in the graph yet but either source or target nodes of the edge are missing in the graph
        IllegalArgumentException - if edge is null
      • getNodes

        public Set<ModelNode> getNodes​(org.eclipse.emf.ecore.EObject object)
        Returns the set of nodes of the graph referencing the given object. Does not check for edges referencing the object.
        Parameters:
        object - object to look up
        Returns:
        set of nodes referencing the given object
      • getEdges

        public Set<ModelEdge> getEdges​(org.eclipse.emf.ecore.EObject object)
        Returns the set of edges of the graph referencing the given object. Does not check for nodes referencing the object.
        Parameters:
        object - object to look up
        Returns:
        set of edges referencing the given object
      • getElements

        public Set<ModelGraphElement> getElements​(org.eclipse.emf.ecore.EObject object)
        Returns the set of elements of the graph referencing the given object. Iterating over the elements will first iterate over all nodes and then over all edges referencing the object in the order of their insertion into the graph, respectively.
        Parameters:
        object - object to look up
        Returns:
        set of elements referencing the given object
      • getNodes

        public Set<ModelNode> getNodes()
        The nodes of the graph are returned in the order of their insertion.
        Returns:
        the nodes
      • getEdges

        public Set<ModelEdge> getEdges()
        The edges of the graph are returned in the order of their insertion.
        Returns:
        the edges
      • getElements

        public Set<ModelGraphElement> getElements()
        Returns all elements of the graph. Iterating over the elements will first iterate over all nodes and then over all edges in the order of their insertion into the graph, respectively.
        Returns:
      • removeNode

        public boolean removeNode​(ModelNode node)
        Removes the node and its connected edges from the graph.
        Parameters:
        node - a node to remove from the graph
        Returns:
        true if the node was present in the graph - false otherwise
      • removeEdge

        public boolean removeEdge​(ModelEdge edge)
        Removes the edge from the graph.
        Parameters:
        edge - an edege to remove from the graph
        Returns:
        true if the edge was present in the graph - false otherwise
      • getAllNodeReferences

        public Set<org.eclipse.emf.ecore.EObject> getAllNodeReferences()
      • getAllEdgeReferences

        public Set<org.eclipse.emf.ecore.EObject> getAllEdgeReferences()
      • setRoot

        public void setRoot​(ModelNode root)
                     throws GraphManipulationException
        Sets the root node of this graph. If root is not null it needs to be part of the graph. The semantics of the root are not defined here and may depend on the context in which the graph is used.
        Parameters:
        root - the node which should be treated as the root
        Throws:
        GraphManipulationException - if root is neither null nor part of the graph