Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 67516332c13806c01c8f31711a395f3877efc974 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*******************************************************************************
 * Copyright (c) 2008, 2010 Mia-Software.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Nicolas Bros (Mia-Software) - initial API and implementation
 *******************************************************************************/
package org.eclipse.papyrus.emf.facet.custom.metamodel.v0_2_0.custom.presentation;

import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.TreeIterator;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.ENamedElement;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.InternalEObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.util.EcoreUtil;

/** Utility functions related to EMF */
public class EMFUtil {

	/**
	 * @return whether the given EObject is in the first resource of its
	 *         resource set
	 */
	public static boolean isInFirstResource(final EObject eObject) {
		final Resource resource = eObject.eResource();
		if (resource == null) {
			return false;
		}
		final ResourceSet resourceSet = resource.getResourceSet();
		if (resourceSet == null) {
			return false;
		}
		final EList<Resource> resources = resourceSet.getResources();
		if (resources.size() > 0) {
			if (resourceSet.getResources().get(0) == resource) {
				return true;
			}
		}
		return false;
	}

	/**
	 * Search for instances of the given EClass
	 *
	 * @param eClass
	 *            the EClass of the elements that are looked for
	 * @param resource
	 *            the resource to search in
	 * @return elements that are instances of the given EClass
	 */
	public Collection<EObject> findInstancesOf(final EClass eClass, final Resource resource) {
		final ArrayList<EObject> instances = new ArrayList<EObject>();
		final TreeIterator<EObject> allContents = resource.getAllContents();
		while (allContents.hasNext()) {
			final EObject eObject = allContents.next();
			if (eObject.eClass() == eClass) {
				instances.add(eObject);
			}
		}
		return instances;
	}

	/**
	 * Add all the classes in <code>resources</code> to the <code>classes</code> set
	 */
	private static void addAll(final Set<EClass> classes, final List<Resource> resources) {
		for (final Resource resource : resources) {
			final TreeIterator<EObject> allContents = resource.getAllContents();
			while (allContents.hasNext()) {
				final EObject eObject = allContents.next();
				if (eObject instanceof EClass) {
					final EClass eClass = (EClass) eObject;
					classes.add(eClass);
				}
			}
		}
	}

	/**
	 * Find the most specific metaclass that is common to all of the given
	 * elements.
	 *
	 * @return the common metaclass or <code>null</code> if none was found
	 */
	public static EClass findCommonClass(final List<EObject> elements) {
		// FIXME this algorithm can make a choice about a branch early and not
		// find
		// a solution, when a solution does exist
		EClass eClass = null;
		for (final EObject element : elements) {
			final EClass elementEClass = element.eClass();
			if (eClass == null) {
				eClass = elementEClass;
			} else {
				if (eClass.isSuperTypeOf(elementEClass)) {
					continue;
				} else if (elementEClass.isSuperTypeOf(eClass)) {
					eClass = elementEClass;
				} else {
					// elements are on two different branches
					// try to find a common metaclass
					eClass = findSuperType(eClass, elementEClass);
					if (eClass == null) {
						return null;
					}
				}
			}
		}
		return eClass;
	}

	/**
	 * Find the first superclass of <code>eClass</code> that is a superclass of <code>element</code>
	 */
	private static EClass findSuperType(final EClass eClass, final EClass element) {
		// do a breadth-first search (with a bottom-up tree)
		final LinkedList<EClass> breadthFirstList = new LinkedList<EClass>();
		breadthFirstList.addFirst(eClass);

		while (!breadthFirstList.isEmpty()) {
			final EClass candidateClass = breadthFirstList.poll();

			if (candidateClass.isSuperTypeOf(element)) {
				return candidateClass;
			}

			// add all the direct super-types of this class
			for (final EClass supertype : candidateClass.getESuperTypes()) {
				breadthFirstList.addLast(supertype);
			}
		}

		return null;
	}

	/**
	 * Try to resolve the given proxy.
	 *
	 * @return whether the proxy could be resolved
	 */
	public static boolean tryResolve(final EObject eObject) {
		final EObject resolved = EcoreUtil.resolve(eObject, (EObject) null);
		return !resolved.eIsProxy();
	}

	/** @return the proxy URI for the given EObject or an empty String if none */
	public static String proxyURI(final EObject eObject) {
		final URI eProxyURI = ((InternalEObject) eObject).eProxyURI();
		if (eProxyURI == null) {
			return ""; //$NON-NLS-1$
		}
		return eProxyURI.toString();
	}

	public static <T extends ENamedElement> T findElementWithName(final String name,
			final EList<T> elements) {
		for (T element : elements) {
			if (name.equals(element.getName())) {
				return element;
			}
		}
		return null;
	}

	/**
	 * @return the name of the package in the package registry with this nsURI,
	 *         or <code>null</code> if not found
	 */
	public static String getMetamodelName(final String nsURI) {
		if (nsURI != null) {
			final EPackage ePackage = EPackage.Registry.INSTANCE.getEPackage(nsURI);
			if (ePackage != null) {
				final String name = ePackage.getName();
				if (name != null) {
					return name;
				}
			}

			final URI uri = URI.createURI(nsURI);
			final String lastSegment = uri.lastSegment();
			if (lastSegment != null) {
				return lastSegment;
			}
		}
		return null;
	}

	/** @return the list of {@link EClass}es found in the following resource */
	public static List<EClass> getMetaclasses(final Resource metamodel) {
		final List<EClass> metaclasses = new ArrayList<EClass>();
		final TreeIterator<EObject> allContents = metamodel.getAllContents();
		while (allContents.hasNext()) {
			final EObject eObject = allContents.next();
			if (eObject instanceof EClass) {
				final EClass eClass = (EClass) eObject;
				metaclasses.add(eClass);
			}
		}
		return metaclasses;
	}

	/**
	 * Find the {@link EObject} with the given URI in the given {@link ResourceSet}
	 *
	 * @return the element or <code>null</code> if no element was found with
	 *         this URI in the given {@link ResourceSet}
	 */
	public static EObject findElementByURI(final String uriFragment, final ResourceSet resourceSet) {
		EList<Resource> resources = resourceSet.getResources();
		for (Resource resource : resources) {
			EObject eObject = resource.getEObject(uriFragment);
			if (eObject != null) {
				return eObject;
			}
		}
		return null;
	}

}

Back to the top