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

COVERAGE SUMMARY FOR SOURCE FILE [EMFHelper.java]

nameclass, %method, %block, %line, %
EMFHelper.java0%   (0/1)0%   (0/8)0%   (0/257)0%   (0/68)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class EMFHelper0%   (0/1)0%   (0/8)0%   (0/257)0%   (0/68)
<static initializer> 0%   (0/1)0%   (0/5)0%   (0/3)
EMFHelper (): void 0%   (0/1)0%   (0/3)0%   (0/1)
getElements (EObject, EClass): EList 0%   (0/1)0%   (0/40)0%   (0/8)
loadFromXMIFile (String): EObject 0%   (0/1)0%   (0/19)0%   (0/6)
loadFromXMIFile (String, String, EPackage): EObject 0%   (0/1)0%   (0/25)0%   (0/8)
loadXMI (String, ResourceSet): EObject 0%   (0/1)0%   (0/39)0%   (0/10)
registerPackages (ResourceSet): void 0%   (0/1)0%   (0/49)0%   (0/18)
saveToXMIFile (EObject, String): void 0%   (0/1)0%   (0/77)0%   (0/15)

1package de.uka.ipd.sdq.reliability.core.helper;
2 
3import java.io.File;
4import java.io.FileNotFoundException;
5import java.io.IOException;
6import java.util.Collections;
7 
8import org.apache.log4j.Logger;
9import org.eclipse.emf.common.util.BasicEList;
10import org.eclipse.emf.common.util.EList;
11import org.eclipse.emf.common.util.URI;
12import org.eclipse.emf.ecore.EClass;
13import org.eclipse.emf.ecore.EObject;
14import org.eclipse.emf.ecore.EPackage;
15import org.eclipse.emf.ecore.resource.Resource;
16import org.eclipse.emf.ecore.resource.ResourceSet;
17import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
18import org.eclipse.emf.ecore.util.EcoreUtil;
19import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;
20import org.eclipse.emf.query.conditions.eobjects.EObjectCondition;
21import org.eclipse.emf.query.conditions.eobjects.EObjectTypeRelationCondition;
22import org.eclipse.emf.query.conditions.eobjects.TypeRelation;
23import org.eclipse.emf.query.statements.FROM;
24import org.eclipse.emf.query.statements.IQueryResult;
25import org.eclipse.emf.query.statements.SELECT;
26import org.eclipse.emf.query.statements.WHERE;
27 
28import de.uka.ipd.sdq.pcm.allocation.AllocationPackage;
29import de.uka.ipd.sdq.pcm.parameter.ParameterPackage;
30import de.uka.ipd.sdq.pcm.repository.RepositoryPackage;
31import de.uka.ipd.sdq.pcm.resourceenvironment.ResourceenvironmentPackage;
32import de.uka.ipd.sdq.pcm.resourcetype.ResourcetypePackage;
33import de.uka.ipd.sdq.pcm.seff.SeffPackage;
34import de.uka.ipd.sdq.pcm.system.SystemPackage;
35import de.uka.ipd.sdq.pcm.usagemodel.UsagemodelPackage;
36 
37/**
38 * Provides utility functions for EMF models.
39 * 
40 * @author brosch, martens
41 * 
42 */
43public class EMFHelper {
44 
45        /**
46         * Log4J logging support.
47         */
48        private static Logger logger = Logger
49                        .getLogger(EMFHelper.class.getName());
50 
51        /**
52         * Retrieves all model elements of a given EMF type under some root element.
53         * 
54         * @param root
55         *            the root element
56         * @param type
57         *            the type of objects to find
58         * @return all objects of the given type or a sub type
59         */
60        public EList<EObject> getElements(final EObject root, final EClass type) {
61 
62                // Prepare the result list:
63                EList<EObject> resultList = new BasicEList<EObject>();
64 
65                // Search for elements that have the same type of a sub type of the
66                // given type:
67                EObjectCondition hasCompatibleType = new EObjectTypeRelationCondition(
68                                type, TypeRelation.SAMETYPE_OR_SUBTYPE_LITERAL);
69 
70                // Perform an EMF Model Query:
71                IQueryResult queryResult = new SELECT(new FROM(root), new WHERE(
72                                hasCompatibleType)).execute();
73 
74                // Fill the resulting list:
75                for (Object result : queryResult) {
76                        resultList.add((EObject) result);
77                }
78 
79                // Return the result:
80                return resultList;
81        }
82        
83        /**
84         * Save the given EObject to the file given by filename.
85         * 
86         * @param modelToSave
87         *            The EObject to save
88         * @param fileName
89         *            The filename where to save.
90         */
91        public static void saveToXMIFile(final EObject modelToSave, final String fileName) {
92                
93                logger.debug("Saving " + modelToSave.toString() + " to " + fileName);
94 
95                // Create a resource set.
96                ResourceSet resourceSet = new ResourceSetImpl();
97 
98                // Register the default resource factory -- only needed for stand-alone!
99                resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap()
100                                .put(Resource.Factory.Registry.DEFAULT_EXTENSION,
101                                                new XMIResourceFactoryImpl());
102        
103                URI fileURI = URI.createFileURI(new File(fileName).getAbsolutePath());
104                Resource resource = resourceSet.createResource(fileURI);
105                resource.getContents().add(modelToSave);
106                
107                try {
108                        resource.save(Collections.EMPTY_MAP);
109                } catch (FileNotFoundException e){
110                        if (fileName.length() > 250){
111                                //try again with a shorter filename
112                                saveToXMIFile(modelToSave, fileName.substring(0, fileName.indexOf("-"))+"-shortened-"+fileName.hashCode());
113                        }
114                } catch (IOException e) {
115                        logger.error(e.getMessage());
116                }
117                // logger.debug("Saved " + fileURI);
118        }
119        
120        /**
121         * 
122         * @param fileName
123         * @param eNamespaceURI
124         * @param ePackage
125         * @return
126         */
127        public static EObject loadFromXMIFile(final String fileName, final String eNamespaceURI, final EPackage ePackage){
128 
129                // Create a resource set to hold the resources.
130                ResourceSet resourceSet = new ResourceSetImpl();
131 
132                // Register the appropriate resource factory to handle all file
133                // extensions.
134                resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap()
135                                .put(Resource.Factory.Registry.DEFAULT_EXTENSION,
136                                                new XMIResourceFactoryImpl());
137 
138                // Register the package to ensure it is available during loading.
139                resourceSet.getPackageRegistry().put(eNamespaceURI,
140                                ePackage);
141                
142                // Register standard PCM packages to ensure they are available during loading:
143                registerPackages(resourceSet);
144 
145                // Load the file contents:
146                return loadXMI(fileName, resourceSet);
147        }
148        
149        /**
150         * 
151         * @param fileName
152         *            the filename specifying the file to load from
153         * @return The EObject loaded from the file
154         */
155        public static EObject loadFromXMIFile(final String fileName) {
156 
157                // Create a resource set to hold the resources.
158                ResourceSet resourceSet = new ResourceSetImpl();
159 
160                // Register the appropriate resource factory to handle all file
161                // extensions.
162                resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap()
163                                .put(Resource.Factory.Registry.DEFAULT_EXTENSION,
164                                                new XMIResourceFactoryImpl());
165 
166                // Register standard PCM packages to ensure they are available during loading:
167                registerPackages(resourceSet);
168 
169                // Load the file contents:
170                return loadXMI(fileName, resourceSet);
171        }
172 
173        /**
174         * 
175         * @param fileName
176         * @param resourceSet
177         * @return
178         */
179        private static EObject loadXMI(final String fileName,
180                        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.error(e.getMessage());
194                        return null;
195                }
196                EObject eObject = (EObject) resource.getContents().iterator().next();
197                return EcoreUtil.getRootContainer(eObject);
198        }
199        
200        /**
201         * Copied From de.uka.ipd.sdq.pcmsolver.models.PCMInstance.
202         * 
203         * @param resourceSet
204         *            The resource set to register all contained model packages
205         *            with.
206         */
207        private static void registerPackages(final ResourceSet resourceSet) {
208 
209                resourceSet.getPackageRegistry().put(AllocationPackage.eNS_URI,
210                                AllocationPackage.eINSTANCE);
211                resourceSet.getPackageRegistry().put(ParameterPackage.eNS_URI,
212                                ParameterPackage.eINSTANCE);
213                resourceSet.getPackageRegistry().put(
214                                ResourceenvironmentPackage.eNS_URI,
215                                ResourceenvironmentPackage.eINSTANCE);
216                resourceSet.getPackageRegistry().put(ResourcetypePackage.eNS_URI,
217                                ResourcetypePackage.eINSTANCE);
218                resourceSet.getPackageRegistry().put(RepositoryPackage.eNS_URI,
219                                RepositoryPackage.eINSTANCE);
220                resourceSet.getPackageRegistry().put(SeffPackage.eNS_URI,
221                                SeffPackage.eINSTANCE);
222                resourceSet.getPackageRegistry().put(SystemPackage.eNS_URI,
223                                SystemPackage.eINSTANCE);
224                resourceSet.getPackageRegistry().put(UsagemodelPackage.eNS_URI,
225                                UsagemodelPackage.eINSTANCE);
226        }
227}

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