EMMA Coverage Report (generated Sun Feb 05 10:43:15 CET 2012)
[all classes][de.uka.ipd.sdq.dsexplore.helper]

COVERAGE SUMMARY FOR SOURCE FILE [EMFHelper.java]

nameclass, %method, %block, %line, %
EMFHelper.java0%   (0/1)0%   (0/15)0%   (0/463)0%   (0/112)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class EMFHelper0%   (0/1)0%   (0/15)0%   (0/463)0%   (0/112)
EMFHelper (): void 0%   (0/1)0%   (0/3)0%   (0/1)
checkIdentity (EObject, EObject): boolean 0%   (0/1)0%   (0/28)0%   (0/7)
contains (Collection, EObject): boolean 0%   (0/1)0%   (0/19)0%   (0/4)
createEMFCandidates (Collection): Candidates 0%   (0/1)0%   (0/27)0%   (0/5)
getAllUsedAllocationContexts (Allocation): List 0%   (0/1)0%   (0/3)0%   (0/1)
getAllUsedAssemblyContexts (ComposedProvidingRequiringEntity): List 0%   (0/1)0%   (0/36)0%   (0/8)
getPassiveResources (List): List 0%   (0/1)0%   (0/57)0%   (0/12)
indexOfByID (List, String): int 0%   (0/1)0%   (0/8)0%   (0/2)
loadFromXMIFile (String): EObject 0%   (0/1)0%   (0/19)0%   (0/6)
loadFromXMIFile (String, ResourceSet): EObject 0%   (0/1)0%   (0/40)0%   (0/10)
registerPackages (ResourceSet): void 0%   (0/1)0%   (0/49)0%   (0/18)
retainAll (Collection, Collection): boolean 0%   (0/1)0%   (0/40)0%   (0/11)
retrieveEntityByID (List, EObject): Entity 0%   (0/1)0%   (0/34)0%   (0/7)
retrieveEntityByID (List, String): Entity 0%   (0/1)0%   (0/20)0%   (0/4)
saveToXMIFile (EObject, String): void 0%   (0/1)0%   (0/80)0%   (0/16)

1package de.uka.ipd.sdq.dsexplore.helper;
2 
3import java.io.File;
4import java.io.FileNotFoundException;
5import java.io.IOException;
6import java.util.ArrayList;
7import java.util.Collection;
8import java.util.Collections;
9import java.util.Iterator;
10import java.util.LinkedList;
11import java.util.List;
12 
13import org.apache.log4j.Logger;
14import org.eclipse.emf.common.util.URI;
15import org.eclipse.emf.ecore.EObject;
16import org.eclipse.emf.ecore.resource.Resource;
17import org.eclipse.emf.ecore.resource.ResourceSet;
18import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
19import org.eclipse.emf.ecore.util.EcoreUtil;
20import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;
21 
22import de.uka.ipd.sdq.dsexplore.opt4j.representation.DSEIndividual;
23import de.uka.ipd.sdq.dsexplore.opt4j.start.Opt4JStarter;
24import de.uka.ipd.sdq.identifier.Identifier;
25import de.uka.ipd.sdq.pcm.allocation.Allocation;
26import de.uka.ipd.sdq.pcm.allocation.AllocationContext;
27import de.uka.ipd.sdq.pcm.allocation.AllocationPackage;
28import de.uka.ipd.sdq.pcm.core.composition.AssemblyContext;
29import de.uka.ipd.sdq.pcm.core.entity.ComposedProvidingRequiringEntity;
30import de.uka.ipd.sdq.pcm.core.entity.Entity;
31import de.uka.ipd.sdq.pcm.designdecision.Candidates;
32import de.uka.ipd.sdq.pcm.designdecision.designdecisionFactory;
33import de.uka.ipd.sdq.pcm.parameter.ParameterPackage;
34import de.uka.ipd.sdq.pcm.repository.BasicComponent;
35import de.uka.ipd.sdq.pcm.repository.PassiveResource;
36import de.uka.ipd.sdq.pcm.repository.Repository;
37import de.uka.ipd.sdq.pcm.repository.RepositoryComponent;
38import de.uka.ipd.sdq.pcm.repository.RepositoryPackage;
39import de.uka.ipd.sdq.pcm.resourceenvironment.ResourceenvironmentPackage;
40import de.uka.ipd.sdq.pcm.resourcetype.ResourcetypePackage;
41import de.uka.ipd.sdq.pcm.seff.SeffPackage;
42import de.uka.ipd.sdq.pcm.system.SystemPackage;
43import 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 */
52public 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}

[all classes][de.uka.ipd.sdq.dsexplore.helper]
EMMA 2.0.9414 (unsupported private build) (C) Vladimir Roubtsov