1 | package de.uka.ipd.sdq.dsexplore.helper; |
2 | |
3 | import java.io.File; |
4 | import java.io.FileNotFoundException; |
5 | import java.io.IOException; |
6 | import java.util.ArrayList; |
7 | import java.util.Collection; |
8 | import java.util.Collections; |
9 | import java.util.Iterator; |
10 | import java.util.LinkedList; |
11 | import java.util.List; |
12 | |
13 | import org.apache.log4j.Logger; |
14 | import org.eclipse.emf.common.util.URI; |
15 | import org.eclipse.emf.ecore.EObject; |
16 | import org.eclipse.emf.ecore.resource.Resource; |
17 | import org.eclipse.emf.ecore.resource.ResourceSet; |
18 | import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; |
19 | import org.eclipse.emf.ecore.util.EcoreUtil; |
20 | import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl; |
21 | |
22 | import de.uka.ipd.sdq.dsexplore.opt4j.representation.DSEIndividual; |
23 | import de.uka.ipd.sdq.dsexplore.opt4j.start.Opt4JStarter; |
24 | import de.uka.ipd.sdq.identifier.Identifier; |
25 | import de.uka.ipd.sdq.pcm.allocation.Allocation; |
26 | import de.uka.ipd.sdq.pcm.allocation.AllocationContext; |
27 | import de.uka.ipd.sdq.pcm.allocation.AllocationPackage; |
28 | import de.uka.ipd.sdq.pcm.core.composition.AssemblyContext; |
29 | import de.uka.ipd.sdq.pcm.core.entity.ComposedProvidingRequiringEntity; |
30 | import de.uka.ipd.sdq.pcm.core.entity.Entity; |
31 | import de.uka.ipd.sdq.pcm.designdecision.Candidates; |
32 | import de.uka.ipd.sdq.pcm.designdecision.designdecisionFactory; |
33 | import de.uka.ipd.sdq.pcm.parameter.ParameterPackage; |
34 | import de.uka.ipd.sdq.pcm.repository.BasicComponent; |
35 | import de.uka.ipd.sdq.pcm.repository.PassiveResource; |
36 | import de.uka.ipd.sdq.pcm.repository.Repository; |
37 | import de.uka.ipd.sdq.pcm.repository.RepositoryComponent; |
38 | import de.uka.ipd.sdq.pcm.repository.RepositoryPackage; |
39 | import de.uka.ipd.sdq.pcm.resourceenvironment.ResourceenvironmentPackage; |
40 | import de.uka.ipd.sdq.pcm.resourcetype.ResourcetypePackage; |
41 | import de.uka.ipd.sdq.pcm.seff.SeffPackage; |
42 | import de.uka.ipd.sdq.pcm.system.SystemPackage; |
43 | import de.uka.ipd.sdq.pcm.usagemodel.UsagemodelPackage; |
44 | |
45 | /** |
46 | * Also see {@link EcoreUtil} for more helper functions |
47 | * like {@link EcoreUtil#equals(EObject, EObject)} to |
48 | * test for equality. |
49 | * @author martens |
50 | * |
51 | */ |
52 | public class EMFHelper { |
53 | |
54 | /** |
55 | * Checks for two PCM model elements whether they are the same, i.e. whether |
56 | * they have the same ID. The model elements have to be derived from |
57 | * Identifier. Note that two systems might use the same assembly contexts |
58 | * and components, but still are two different systems. If one of the |
59 | * Identifiers in null, false is returned. |
60 | * |
61 | * @param i1 |
62 | * One Identifier |
63 | * @param i2 |
64 | * Another Identifier |
65 | * @return true if i1.getId().equals(i2.getId()), false otherwise |
66 | */ |
67 | public static boolean checkIdentity(EObject i1, EObject i2) { |
68 | if (i1 == null || i2 == null) |
69 | return false; |
70 | if (i1 instanceof Identifier && i2 instanceof Identifier){ |
71 | if (((Identifier) i1).getId().equals(((Identifier) i2).getId())) { |
72 | // logger.debug("Two model elements match with Id: "+i1.getId()); |
73 | return true; |
74 | } else { |
75 | return false; |
76 | }} else { |
77 | return EcoreUtil.equals(i1, i2); |
78 | } |
79 | } |
80 | |
81 | /** |
82 | * Implements an identifier-based contains. Calls {@link #checkIdentity(Identifier, Identifier)} |
83 | * to compare the {@link Identifier} i with the contents of the collection. |
84 | * |
85 | * @param coll |
86 | * @param i |
87 | * @return true if there is an {@link Identifier} in coll with an id equal to i.getID(). |
88 | */ |
89 | public static boolean contains(Collection<? extends EObject> coll, EObject i){ |
90 | for (EObject identifier : coll) { |
91 | if (checkIdentity(identifier, i)){ |
92 | return true; |
93 | } |
94 | } |
95 | return false; |
96 | } |
97 | |
98 | public static boolean retainAll(Collection<? extends Identifier> collection, Collection<? extends EObject> itemsToRetain){ |
99 | boolean removedAny = false; |
100 | for (Iterator<? extends Identifier> iterator = collection.iterator(); iterator.hasNext();) { |
101 | Identifier identifier = iterator.next(); |
102 | boolean identifierContainedInItemsToRetain = false; |
103 | for (EObject identifierToRetain : itemsToRetain) { |
104 | if (checkIdentity(identifier, identifierToRetain)){ |
105 | identifierContainedInItemsToRetain = true; |
106 | } |
107 | } |
108 | if (!identifierContainedInItemsToRetain){ |
109 | iterator.remove(); |
110 | removedAny = true; |
111 | } |
112 | } |
113 | return removedAny; |
114 | } |
115 | |
116 | /** |
117 | * Save the given EObject to the file given by filename. |
118 | * |
119 | * @param modelToSave |
120 | * The EObject to save |
121 | * @param fileName |
122 | * The filename where to save. |
123 | */ |
124 | public static void saveToXMIFile(final EObject modelToSave, final String fileName) { |
125 | |
126 | Logger logger = Logger.getLogger("de.uka.ipd.sdq.dsexplore"); |
127 | |
128 | logger.debug("Saving " + modelToSave.toString() + " to " + fileName); |
129 | |
130 | // Create a resource set. |
131 | ResourceSet resourceSet = new ResourceSetImpl(); |
132 | |
133 | // Register the default resource factory -- only needed for stand-alone! |
134 | resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap() |
135 | .put(Resource.Factory.Registry.DEFAULT_EXTENSION, |
136 | new XMIResourceFactoryImpl()); |
137 | |
138 | URI fileURI = URI.createFileURI(new File(fileName).getAbsolutePath()); |
139 | Resource resource = resourceSet.createResource(fileURI); |
140 | resource.getContents().add(modelToSave); |
141 | |
142 | |
143 | |
144 | try { |
145 | resource.save(Collections.EMPTY_MAP); |
146 | } catch (FileNotFoundException e){ |
147 | if (fileName.length() > 250){ |
148 | //try again with a shorter filename |
149 | saveToXMIFile(modelToSave, fileName.substring(0, fileName.indexOf("-"))+"-shortened-"+fileName.hashCode()); |
150 | } |
151 | } catch (IOException e) { |
152 | logger.error(e.getMessage()); |
153 | } |
154 | // logger.debug("Saved " + fileURI); |
155 | } |
156 | |
157 | /** |
158 | * Copied From de.uka.ipd.sdq.pcmsolver.models.PCMInstance. |
159 | * |
160 | * @param fileName |
161 | * the filename specifying the file to load from |
162 | * @return The EObject loaded from the file |
163 | */ |
164 | public static EObject loadFromXMIFile(final String fileName) { |
165 | // Create a resource set to hold the resources. |
166 | ResourceSet resourceSet = new ResourceSetImpl(); |
167 | |
168 | // Register the appropriate resource factory to handle all file |
169 | // extensions. |
170 | resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap() |
171 | .put(Resource.Factory.Registry.DEFAULT_EXTENSION, |
172 | new XMIResourceFactoryImpl()); |
173 | |
174 | // Register the package to ensure it is available during loading. |
175 | registerPackages(resourceSet); |
176 | |
177 | return loadFromXMIFile(fileName, resourceSet); |
178 | } |
179 | |
180 | public static EObject loadFromXMIFile(final String fileName, ResourceSet resourceSet){ |
181 | // Construct the URI for the instance file. |
182 | // The argument is treated as a file path only if it denotes an existing |
183 | // file. Otherwise, it's directly treated as a URL. |
184 | File file = new File(fileName); |
185 | URI uri = file.isFile() ? URI.createFileURI(file.getAbsolutePath()) |
186 | : URI.createURI(fileName); |
187 | |
188 | Resource resource = null; |
189 | // Demand load resource for this file. |
190 | try { |
191 | resource = resourceSet.getResource(uri, true); |
192 | } catch (Exception e) { |
193 | Logger.getLogger("de.uka.ipd.sdq.dsexplore").error(e.getMessage()); |
194 | return null; |
195 | } |
196 | |
197 | // logger.debug("Loaded " + uri); |
198 | |
199 | // if (!fileName.endsWith(".assembly") && |
200 | // !fileName.endsWith("repository")) { |
201 | // // Validate the contents of the loaded resource. |
202 | // for (Iterator j = resource.getContents().iterator(); j.hasNext();) { |
203 | // EObject eObject = (EObject) j.next(); |
204 | // Diagnostic diagnostic = Diagnostician.INSTANCE |
205 | // .validate(eObject); |
206 | // if (diagnostic.getSeverity() != Diagnostic.OK) { |
207 | // System.out.println(); |
208 | // System.out.println(diagnostic.getMessage()); |
209 | // // printDiagnostic(diagnostic, ""); |
210 | // |
211 | // } |
212 | // } |
213 | // } |
214 | EObject eObject = (EObject) resource.getContents().iterator().next(); |
215 | return EcoreUtil.getRootContainer(eObject); |
216 | } |
217 | |
218 | /** |
219 | * Copied From de.uka.ipd.sdq.pcmsolver.models.PCMInstance. |
220 | * |
221 | * @param resourceSet |
222 | * The resource set to register all contained model packages |
223 | * with. |
224 | */ |
225 | private static void registerPackages(final ResourceSet resourceSet) { |
226 | |
227 | resourceSet.getPackageRegistry().put(AllocationPackage.eNS_URI, |
228 | AllocationPackage.eINSTANCE); |
229 | resourceSet.getPackageRegistry().put(ParameterPackage.eNS_URI, |
230 | ParameterPackage.eINSTANCE); |
231 | resourceSet.getPackageRegistry().put( |
232 | ResourceenvironmentPackage.eNS_URI, |
233 | ResourceenvironmentPackage.eINSTANCE); |
234 | resourceSet.getPackageRegistry().put(ResourcetypePackage.eNS_URI, |
235 | ResourcetypePackage.eINSTANCE); |
236 | resourceSet.getPackageRegistry().put(RepositoryPackage.eNS_URI, |
237 | RepositoryPackage.eINSTANCE); |
238 | resourceSet.getPackageRegistry().put(SeffPackage.eNS_URI, |
239 | SeffPackage.eINSTANCE); |
240 | resourceSet.getPackageRegistry().put(SystemPackage.eNS_URI, |
241 | SystemPackage.eINSTANCE); |
242 | resourceSet.getPackageRegistry().put(UsagemodelPackage.eNS_URI, |
243 | UsagemodelPackage.eINSTANCE); |
244 | |
245 | } |
246 | |
247 | public static Entity retrieveEntityByID(List<? extends EObject> entities, EObject object){ |
248 | if (object instanceof Entity){ |
249 | List<Entity> castedEntities = new ArrayList<Entity>(); |
250 | for (EObject eObject : entities) { |
251 | if (eObject instanceof Entity){ |
252 | castedEntities.add((Entity) eObject); |
253 | } |
254 | } |
255 | return retrieveEntityByID(castedEntities, ((Entity)object).getId()); |
256 | } |
257 | return null; |
258 | } |
259 | |
260 | public static Entity retrieveEntityByID(List<? extends Entity> entities, String id) { |
261 | for (Entity entity : entities) { |
262 | |
263 | if (entity.getId().equals(id)){ |
264 | return entity; |
265 | } |
266 | } |
267 | return null; |
268 | } |
269 | |
270 | @SuppressWarnings("unchecked") |
271 | public static int indexOfByID(List entities, String id) { |
272 | Entity entity = retrieveEntityByID(entities, id); |
273 | return entities.indexOf(entity); |
274 | } |
275 | |
276 | public static Candidates createEMFCandidates(Collection<DSEIndividual> individuals) { |
277 | Candidates candidates = designdecisionFactory.eINSTANCE.createCandidates(); |
278 | candidates.setProblem(Opt4JStarter.getProblem().getEMFProblem()); |
279 | |
280 | for (DSEIndividual dseIndividual : individuals) { |
281 | candidates.getCandidate().add(dseIndividual.getGenotype().getEMFCandidate()); |
282 | } |
283 | return candidates; |
284 | } |
285 | |
286 | public static List<PassiveResource> getPassiveResources(List<Repository> repositoryList){ |
287 | |
288 | |
289 | List<PassiveResource> passiveResourceList = new ArrayList<PassiveResource>(repositoryList.size()); |
290 | |
291 | for (Repository repository : repositoryList) { |
292 | List<RepositoryComponent> repoComponents = repository |
293 | .getComponents__Repository(); |
294 | for (RepositoryComponent repositoryComponent : repoComponents) { |
295 | if (repositoryComponent instanceof BasicComponent) { |
296 | BasicComponent basicComponent = (BasicComponent) repositoryComponent; |
297 | List<PassiveResource> passiveResourceOfComponentList = basicComponent |
298 | .getPassiveResource_BasicComponent(); |
299 | for (PassiveResource passiveResource : passiveResourceOfComponentList) { |
300 | |
301 | passiveResourceList.add(passiveResource); |
302 | } |
303 | |
304 | } |
305 | } |
306 | } |
307 | return passiveResourceList; |
308 | } |
309 | |
310 | /** Recursively get all contained AssemblyContexts in one flat list. |
311 | * */ |
312 | public static List<AssemblyContext> getAllUsedAssemblyContexts(ComposedProvidingRequiringEntity composite){ |
313 | List<AssemblyContext> resultList = new LinkedList<AssemblyContext>(); |
314 | |
315 | List<AssemblyContext> currentAssemblyContexts = composite.getAssemblyContexts__ComposedStructure(); |
316 | resultList.addAll(currentAssemblyContexts); |
317 | |
318 | for (AssemblyContext assemblyContext : currentAssemblyContexts) { |
319 | RepositoryComponent innerComponent = assemblyContext.getEncapsulatedComponent__AssemblyContext(); |
320 | if (innerComponent instanceof ComposedProvidingRequiringEntity){ |
321 | resultList.addAll(getAllUsedAssemblyContexts((ComposedProvidingRequiringEntity) innerComponent)); |
322 | } |
323 | } |
324 | return resultList; |
325 | |
326 | } |
327 | |
328 | public static List<AllocationContext> getAllUsedAllocationContexts( |
329 | Allocation allocation) { |
330 | return allocation.getAllocationContexts_Allocation(); |
331 | } |
332 | |
333 | } |