Skip to content

Package: PackageDependencyGraph

PackageDependencyGraph

nameinstructionbranchcomplexitylinemethod
PackageDependencyGraph()
M: 0 C: 13
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
addPackage(String)
M: 0 C: 14
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
createNode(String)
M: 0 C: 13
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
getIerator()
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getNsURIToNodeMap()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
resolveNode(PackageDependencyGraph.PackageTreeNode)
M: 11 C: 120
92%
M: 1 C: 15
94%
M: 1 C: 8
89%
M: 2 C: 29
94%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2015 EclipseSource Muenchen GmbH and others.
3: *
4: * All rights reserved. This program and the accompanying materials
5: * are made available under the terms of the Eclipse Public License 2.0
6: * which accompanies this distribution, and is available at
7: * https://www.eclipse.org/legal/epl-2.0/
8: *
9: * SPDX-License-Identifier: EPL-2.0
10: *
11: * Contributors:
12: * jfaltermeier - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emf.ecp.view.edapt;
15:
16: import java.text.MessageFormat;
17: import java.util.Iterator;
18: import java.util.LinkedHashMap;
19: import java.util.LinkedHashSet;
20: import java.util.Map;
21: import java.util.Set;
22:
23: import org.eclipse.emf.common.util.TreeIterator;
24: import org.eclipse.emf.ecore.EClass;
25: import org.eclipse.emf.ecore.EClassifier;
26: import org.eclipse.emf.ecore.EObject;
27: import org.eclipse.emf.ecore.EPackage;
28: import org.eclipse.emf.ecore.EPackage.Registry;
29: import org.eclipse.emf.ecore.EStructuralFeature;
30:
31: /**
32: * Tree like datastructure representing EPackages and their dependencies to each other. Offers an Iterator to
33: * navigate over the dependencies.
34: *
35: * @author jfaltermeier
36: *
37: */
38: public class PackageDependencyGraph {
39:
40:         private final Map<String, PackageTreeNode> nsURIToNodeMap;
41:         private final Set<PackageTreeNode> roots;
42:
43:         /**
44:          * Constructs a new empty {@link PackageDependencyGraph}.
45:          */
46:         public PackageDependencyGraph() {
47:                 roots = new LinkedHashSet<PackageTreeNode>();
48:                 nsURIToNodeMap = new LinkedHashMap<String, PackageTreeNode>();
49:         }
50:
51:         /**
52:          * For testing purposes.
53:          *
54:          * @return a map of all registered nodes
55:          */
56:         Map<String, PackageTreeNode> getNsURIToNodeMap() {
57:                 return nsURIToNodeMap;
58:         }
59:
60:         /**
61:          * Adds a new {@link EPackage} with the given namespace URI to the tree. All required dependencies of the EPackage
62:          * will be registered as well.
63:          *
64:          * @param nsURI the namespace uri of the package to add
65:          */
66:         public void addPackage(String nsURI) {
67:•                if (nsURIToNodeMap.containsKey(nsURI)) {
68:                         return;
69:                 }
70:                 final PackageTreeNode node = createNode(nsURI);
71:                 resolveNode(node);
72:         }
73:
74:         /**
75:          * Adds the node and all dependencies (if not already there) to the tree.
76:          *
77:          * @param node the node to add
78:          */
79:         private void resolveNode(PackageTreeNode node) {
80:                 final Set<String> nsURIs = new LinkedHashSet<String>();
81:
82:                 /* resolve direct nsURIs */
83:                 final EPackage rootPackage = Registry.INSTANCE.getEPackage(node.getNSURI());
84:•                if (rootPackage == null) {
85:                         Activator.log(MessageFormat.format("No Package found for the ns-uri {0}", node.getNSURI())); //$NON-NLS-1$
86:                 } else {
87:                         final TreeIterator<EObject> iterator = rootPackage.eAllContents();
88:•                        while (iterator.hasNext()) {
89:                                 final EObject next = iterator.next();
90:
91:                                 /* check super types of contained classes */
92:•                                if (EClass.class.isInstance(next)) {
93:                                         final EClass eClass = (EClass) next;
94:•                                        for (final EClass superType : eClass.getESuperTypes()) {
95:                                                 nsURIs.add(superType.getEPackage().getNsURI());
96:                                         }
97:                                 }
98:
99:                                 /* check types of features */
100:•                                else if (EStructuralFeature.class.isInstance(next)) {
101:                                         final EStructuralFeature feature = (EStructuralFeature) next;
102:                                         final EClassifier eType = feature.getEType();
103:                                         nsURIs.add(eType.getEPackage().getNsURI());
104:                                 }
105:                         }
106:                 }
107:
108:                 /* remove self */
109:                 nsURIs.remove(node.getNSURI());
110:
111:                 /* root? */
112:•                if (nsURIs.isEmpty()) {
113:                         roots.add(node);
114:                 }
115:
116:                 /* insert dependencies in tree */
117:•                for (final String nsURI : nsURIs) {
118:                         /* existing */
119:•                        if (nsURIToNodeMap.containsKey(nsURI)) {
120:                                 final PackageTreeNode parent = nsURIToNodeMap.get(nsURI);
121:                                 node.addParent(parent);
122:                                 parent.addChild(node);
123:                         }
124:
125:                         /* non existing */
126:                         else {
127:                                 final PackageTreeNode parent = createNode(nsURI);
128:                                 resolveNode(parent);
129:                                 parent.addChild(node);
130:                                 node.addParent(parent);
131:                         }
132:                 }
133:         }
134:
135:         private PackageTreeNode createNode(String nsURI) {
136:                 final PackageTreeNode node = new PackageTreeNode(nsURI);
137:                 nsURIToNodeMap.put(nsURI, node);
138:                 return node;
139:         }
140:
141:         /**
142:          * Returns an iterator for the tree. It will return sets of namespace uris. The set that is returned by next will
143:          * always have no unvisited parents, meaning that all parents have been returned by next before.
144:          *
145:          * @return the iterator
146:          */
147:         public Iterator<Set<String>> getIerator() {
148:                 return new PackageDependencyIterator(roots, nsURIToNodeMap.values());
149:         }
150:
151:         /**
152:          * Simple tree data structure to order the changes of all required epackages during the migration.
153:          *
154:          * @author jfaltermeier
155:          *
156:          */
157:         static class PackageTreeNode {
158:
159:                 private final String nsURI;
160:                 private final Set<PackageTreeNode> parents;
161:                 private final Set<PackageTreeNode> children;
162:
163:                 /**
164:                  * Constructs a new treenode for the given ns uri.
165:                  *
166:                  * @param nsURI the uri
167:                  */
168:                 PackageTreeNode(String nsURI) {
169:                         this.nsURI = nsURI;
170:                         parents = new LinkedHashSet<PackageTreeNode>();
171:                         children = new LinkedHashSet<PackageTreeNode>();
172:                 }
173:
174:                 /**
175:                  * Returns the namesapce uri.
176:                  *
177:                  * @return the node's uri
178:                  */
179:                 public String getNSURI() {
180:                         return nsURI;
181:                 }
182:
183:                 /**
184:                  * Adds a parent to this node.
185:                  *
186:                  * @param node the parent node
187:                  */
188:                 public void addParent(PackageTreeNode node) {
189:                         parents.add(node);
190:                 }
191:
192:                 /**
193:                  * Adds a child to this node.
194:                  *
195:                  * @param node the child
196:                  */
197:                 public void addChild(PackageTreeNode node) {
198:                         children.add(node);
199:                 }
200:
201:                 /**
202:                  * Returns all parents of this node.
203:                  *
204:                  * @return parents
205:                  */
206:                 public Set<PackageTreeNode> getParents() {
207:                         return parents;
208:                 }
209:
210:                 /**
211:                  * Returns all children of this node.
212:                  *
213:                  * @return the children.
214:                  */
215:                 public Set<PackageTreeNode> getChildren() {
216:                         return children;
217:                 }
218:
219:         }
220:
221: }