source: trunk/CrossPare/src/de/ugoe/cs/cpdp/decentApp/DECENTEpsilonModelHandler.java @ 70

Last change on this file since 70 was 41, checked in by sherbold, 9 years ago
  • formatted code and added copyrights
  • Property svn:mime-type set to text/plain
File size: 12.9 KB
Line 
1// Copyright 2015 Georg-August-Universität Göttingen, Germany
2//
3//   Licensed under the Apache License, Version 2.0 (the "License");
4//   you may not use this file except in compliance with the License.
5//   You may obtain a copy of the License at
6//
7//       http://www.apache.org/licenses/LICENSE-2.0
8//
9//   Unless required by applicable law or agreed to in writing, software
10//   distributed under the License is distributed on an "AS IS" BASIS,
11//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//   See the License for the specific language governing permissions and
13//   limitations under the License.
14
15package de.ugoe.cs.cpdp.decentApp;
16
17import java.io.File;
18import java.net.URISyntaxException;
19import java.util.HashMap;
20
21import org.eclipse.emf.ecore.EPackage;
22import org.eclipse.emf.ecore.resource.Resource;
23import org.eclipse.epsilon.common.util.StringProperties;
24import org.eclipse.epsilon.emc.emf.EmfModel;
25import org.eclipse.epsilon.emc.emf.InMemoryEmfModel;
26import org.eclipse.epsilon.eol.exceptions.models.EolModelLoadingException;
27import org.eclipse.epsilon.eol.models.IModel;
28
29import de.ugoe.cs.cpdp.decentApp.models.arffx.ARFFxPackage;
30import de.ugoe.cs.cpdp.decentApp.models.decent.DECENTPackage;
31
32/**
33 * Class for handling decent and arffx model files.
34 *
35 * @author Philip Makedonski, Fabian Trautsch
36 *
37 */
38
39public class DECENTEpsilonModelHandler {
40    private HashMap<String, Object> metaModelCache = new HashMap<>();
41    private boolean useDECENTBinary = false;
42    private boolean useARFFxBinary = false;
43
44    public static String metaPath = "./decent/models/";
45
46    /**
47     * Returns the decent model as IModel instance
48     *
49     * @param decentModelLocation
50     *            location of the decent model file
51     * @param read
52     *            indicates if the model should be read from
53     * @param write
54     *            indicates if data should be written in the model
55     * @return EmFModel (IModel) instance from the decent model, which was loaded
56     * @throws Exception
57     */
58    public IModel getDECENTModel(String decentModelLocation, boolean read, boolean write)
59        throws Exception
60    {
61
62        EmfModel model;
63
64        if (isUseDECENTBinary()) {
65            unregisterMetaModels("");
66            if (!read) {
67                new File(decentModelLocation).delete();
68                new File(decentModelLocation + "bin").delete();
69            }
70            DECENTResourceTool tool = new DECENTResourceTool();
71            if (new File(decentModelLocation).exists() &&
72                !new File(decentModelLocation + "bin").exists())
73            {
74                Resource resource =
75                    tool.loadResourceFromXMI(decentModelLocation, "decent", DECENTPackage.eINSTANCE);
76                tool.storeBinaryResourceContents(resource.getContents(), decentModelLocation +
77                    "bin", "decentbin");
78            }
79
80            Resource resourceBin =
81                tool.loadResourceFromBinary(decentModelLocation + "bin", "decentbin",
82                                            DECENTPackage.eINSTANCE);
83            // alternative pattern
84            // model = createInMemoryEmfModel("DECENT", resourceLocation,
85            // "../DECENT.Meta/model/DECENTv3.ecore", read, write, resourceBin,
86            // DECENTPackage.eINSTANCE);
87            // restoreMetaModels();
88
89            // NOTE: Adding the package is essential as otherwise epsilon breaks
90            model = new InMemoryEmfModel("DECENT", resourceBin, DECENTPackage.eINSTANCE);
91            model.setStoredOnDisposal(write);
92            model.setReadOnLoad(read);
93            model.setCachingEnabled(true);
94            restoreMetaModels();
95        }
96        else {
97            model =
98                createEmfModel("DECENT", decentModelLocation, metaPath + "DECENTv3.ecore", read,
99                               write);
100        }
101
102        return model;
103    }
104
105    /**
106     * Converts the decent model to a binary form
107     *
108     * @param location
109     *            of the decent model file
110     */
111    public void convertDECENTModelToBinary(String location) {
112        unregisterMetaModels("");
113        DECENTResourceTool tool = new DECENTResourceTool();
114        Resource resource =
115            tool.loadResourceFromXMI(location + "/model.decent", "decent", DECENTPackage.eINSTANCE);
116        tool.storeBinaryResourceContents(resource.getContents(),
117                                         location + "/model.decent" + "bin", "decentbin");
118        restoreMetaModels();
119    }
120
121    /**
122     * Converts the decent model to a xmi form
123     *
124     * @param location
125     *            of the decent model file
126     */
127
128    public void convertDECENTModelToXMI(String location) {
129        unregisterMetaModels("");
130        DECENTResourceTool tool = new DECENTResourceTool();
131        Resource resource =
132            tool.loadResourceFromBinary(location + "/model.decentbin", "decentbin",
133                                        DECENTPackage.eINSTANCE);
134        restoreMetaModels();
135        tool.storeResourceContents(resource.getContents(), location + "/model.decent", "decent");
136    }
137
138    /**
139     * Returns the arffx model as IModel instance
140     *
141     * @param arffxModelLocation
142     *            location of the arffx model file
143     * @param read
144     *            indicates if the model should be read from
145     * @param write
146     *            indicates if data should be written in the model
147     * @return EmFModel (IModel) instance from the arffx model, which was loaded
148     * @throws Exception
149     */
150
151    public IModel getARFFxModel(String arffxModelLocation, boolean read, boolean write)
152        throws Exception
153    {
154
155        EmfModel model;
156
157        if (isUseARFFxBinary()) {
158            unregisterMetaModels("");
159            if (!read) {
160                new File(arffxModelLocation).delete();
161                new File(arffxModelLocation + "bin").delete();
162            }
163            ARFFxResourceTool tool = new ARFFxResourceTool();
164            if (new File(arffxModelLocation).exists() &&
165                !new File(arffxModelLocation + "bin").exists())
166            {
167                Resource resource =
168                    tool.loadResourceFromXMI(arffxModelLocation, "arffx", ARFFxPackage.eINSTANCE);
169                tool.storeBinaryResourceContents(resource.getContents(),
170                                                 arffxModelLocation + "bin", "arffxbin");
171            }
172
173            Resource resourceBin =
174                tool.loadResourceFromBinary(arffxModelLocation + "bin", "arffxbin",
175                                            ARFFxPackage.eINSTANCE);
176            // alternative pattern
177            // model = createInMemoryEmfModel("DECENT", resourceLocation,
178            // "../DECENT.Meta/model/DECENTv3.ecore", read, write, resourceBin,
179            // DECENTPackage.eINSTANCE);
180            // restoreMetaModels();
181
182            // NOTE: Adding the package is essential as otherwise epsilon breaks
183            model = new InMemoryEmfModel("ARFFx", resourceBin, ARFFxPackage.eINSTANCE);
184            // model.getModelImpl().getURI().toFileString()
185            model.setStoredOnDisposal(write);
186            model.setReadOnLoad(read);
187            model.setCachingEnabled(true);
188            restoreMetaModels();
189        }
190        else {
191            model =
192                createEmfModel("ARFFx", arffxModelLocation, metaPath + "ARFFx.ecore", read, write);
193        }
194
195        return model;
196    }
197
198    /**
199     * Converts an arffx model to a binary version
200     *
201     * @param location
202     *            of the arffx model
203     */
204    public void convertARFFxModelToBinary(String location) {
205        unregisterMetaModels("");
206        ARFFxResourceTool tool = new ARFFxResourceTool();
207        Resource resource =
208            tool.loadResourceFromXMI(location + "/model.arffx", "arffx", ARFFxPackage.eINSTANCE);
209        tool.storeBinaryResourceContents(resource.getContents(), location + "/model.arffx" + "bin",
210                                         "arffxbin");
211        restoreMetaModels();
212    }
213
214    /**
215     * Converts an arffx model to xmi
216     *
217     * @param location
218     *            of the arffx model
219     */
220
221    public void convertARFFxModelToXMI(String location) {
222        unregisterMetaModels("");
223        ARFFxResourceTool tool = new ARFFxResourceTool();
224        Resource resource =
225            tool.loadResourceFromBinary(location + "/model.arffxbin", "arffxbin",
226                                        DECENTPackage.eINSTANCE);
227        restoreMetaModels();
228        tool.storeResourceContents(resource.getContents(), location + "/model.arffx", "arffx");
229    }
230
231    /**
232     * Returns the log model as IModel instance
233     *
234     * @param logModelLocation
235     *            location of the log model file
236     * @param read
237     *            indicates if the model should be read from
238     * @param write
239     *            indicates if data should be written in the model
240     * @return EmFModel (IModel) instance from the log model, which was loaded
241     * @throws Exception
242     */
243
244    public IModel getLOGModel(String logModelLocation, boolean read, boolean write)
245        throws Exception
246    {
247        if (!new File(logModelLocation).exists()) {
248            read = false;
249        }
250        IModel model = createEmfModel("LOG", logModelLocation, metaPath + "LOG.ecore", read, write);
251        System.setProperty("epsilon.logFileAvailable", "true");
252        return model;
253    }
254
255    /**
256     * Creates an EMF Model
257     *
258     * @param name
259     *            of the emf model
260     * @param model
261     *            name of the model
262     * @param metamodel
263     *            name of the metamodel
264     * @param readOnLoad
265     *            indicates if the model should be read on load
266     * @param storeOnDisposal
267     *            indicates if the model should be stored on disposal
268     * @return
269     * @throws EolModelLoadingException
270     * @throws URISyntaxException
271     */
272
273    @SuppressWarnings("deprecation")
274    protected EmfModel createEmfModel(String name,
275                                      String model,
276                                      String metamodel,
277                                      boolean readOnLoad,
278                                      boolean storeOnDisposal) throws EolModelLoadingException,
279        URISyntaxException
280    {
281        EmfModel emfModel = new EmfModel();
282        StringProperties properties = new StringProperties();
283        properties.put(EmfModel.PROPERTY_NAME, name);
284        properties.put(EmfModel.PROPERTY_ALIASES, name);
285        properties.put(EmfModel.PROPERTY_FILE_BASED_METAMODEL_URI, "file:/" +
286            getFile(metamodel).getAbsolutePath());
287        properties.put(EmfModel.PROPERTY_MODEL_URI, "file:/" + getFile(model).getAbsolutePath());
288        properties.put(EmfModel.PROPERTY_IS_METAMODEL_FILE_BASED, "true");
289        properties.put(EmfModel.PROPERTY_READONLOAD, readOnLoad + "");
290        properties.put(EmfModel.PROPERTY_CACHED, "true");
291        properties.put(EmfModel.PROPERTY_STOREONDISPOSAL, storeOnDisposal + "");
292        emfModel.load(properties, "");
293        // System.out.println(emfModel.allContents());
294        return emfModel;
295    }
296
297    /**
298     * Returns a new File instance from the given filename
299     *
300     * @param fileName
301     *            of the file
302     * @return
303     * @throws URISyntaxException
304     */
305    public File getFile(String fileName) throws URISyntaxException {
306        ;
307        return new File(fileName);
308    }
309
310    /**
311     * Restores the metamodels, so that they are registered in the EPackage registry
312     */
313    private void restoreMetaModels() {
314        for (String key : metaModelCache.keySet()) {
315            EPackage.Registry.INSTANCE.put(key, metaModelCache.get(key));
316        };
317    }
318
319    /**
320     * Unregister the metamodels from the EPackage registry
321     *
322     * @param filter
323     *            for filtering out certain instances
324     */
325    private void unregisterMetaModels(String filter) {
326        for (String key : EPackage.Registry.INSTANCE.keySet()) {
327            if (key.contains(filter)) {
328                metaModelCache.put(key, EPackage.Registry.INSTANCE.get(key));
329            }
330        };
331        for (String key : metaModelCache.keySet()) {
332            EPackage.Registry.INSTANCE.remove(key);
333        };
334    }
335
336    /**
337     * Returns true if decent binary model is used
338     *
339     * @return
340     */
341
342    public boolean isUseDECENTBinary() {
343        return useDECENTBinary;
344    }
345
346    /**
347     * Sets the boolean which indicates, if the decent binary model is used
348     *
349     * @param useDECENTBinary
350     */
351    public void setUseDECENTBinary(boolean useDECENTBinary) {
352        this.useDECENTBinary = useDECENTBinary;
353    }
354
355    /**
356     * Returns true if arffx binary model is used
357     *
358     * @return
359     */
360    public boolean isUseARFFxBinary() {
361        return useARFFxBinary;
362    }
363
364    /**
365     * Sets the boolean which indicates, if the arffx binary model is used
366     *
367     * @param useARFFxBinary
368     */
369
370    public void setUseARFFxBinary(boolean useARFFxBinary) {
371        this.useARFFxBinary = useARFFxBinary;
372    }
373
374}
Note: See TracBrowser for help on using the repository browser.