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

Last change on this file since 140 was 136, checked in by sherbold, 8 years ago
  • more code documentation
  • Property svn:mime-type set to text/plain
File size: 13.0 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    /**
45     * path for DECENT model meta data
46     */
47    public static String metaPath = "./decent/models/";
48
49    /**
50     * Returns the decent model as IModel instance
51     *
52     * @param decentModelLocation
53     *            location of the decent model file
54     * @param read
55     *            indicates if the model should be read from
56     * @param write
57     *            indicates if data should be written in the model
58     * @return EmFModel (IModel) instance from the decent model, which was loaded
59     * @throws Exception
60     */
61    public IModel getDECENTModel(String decentModelLocation, boolean read, boolean write)
62        throws Exception
63    {
64
65        EmfModel model;
66
67        if (isUseDECENTBinary()) {
68            unregisterMetaModels("");
69            if (!read) {
70                new File(decentModelLocation).delete();
71                new File(decentModelLocation + "bin").delete();
72            }
73            DECENTResourceTool tool = new DECENTResourceTool();
74            if (new File(decentModelLocation).exists() &&
75                !new File(decentModelLocation + "bin").exists())
76            {
77                Resource resource =
78                    tool.loadResourceFromXMI(decentModelLocation, "decent", DECENTPackage.eINSTANCE);
79                tool.storeBinaryResourceContents(resource.getContents(), decentModelLocation +
80                    "bin", "decentbin");
81            }
82
83            Resource resourceBin =
84                tool.loadResourceFromBinary(decentModelLocation + "bin", "decentbin",
85                                            DECENTPackage.eINSTANCE);
86            // alternative pattern
87            // model = createInMemoryEmfModel("DECENT", resourceLocation,
88            // "../DECENT.Meta/model/DECENTv3.ecore", read, write, resourceBin,
89            // DECENTPackage.eINSTANCE);
90            // restoreMetaModels();
91
92            // NOTE: Adding the package is essential as otherwise epsilon breaks
93            model = new InMemoryEmfModel("DECENT", resourceBin, DECENTPackage.eINSTANCE);
94            model.setStoredOnDisposal(write);
95            model.setReadOnLoad(read);
96            model.setCachingEnabled(true);
97            restoreMetaModels();
98        }
99        else {
100            model =
101                createEmfModel("DECENT", decentModelLocation, metaPath + "DECENTv3.ecore", read,
102                               write);
103        }
104
105        return model;
106    }
107
108    /**
109     * Converts the decent model to a binary form
110     *
111     * @param location
112     *            of the decent model file
113     */
114    public void convertDECENTModelToBinary(String location) {
115        unregisterMetaModels("");
116        DECENTResourceTool tool = new DECENTResourceTool();
117        Resource resource =
118            tool.loadResourceFromXMI(location + "/model.decent", "decent", DECENTPackage.eINSTANCE);
119        tool.storeBinaryResourceContents(resource.getContents(),
120                                         location + "/model.decent" + "bin", "decentbin");
121        restoreMetaModels();
122    }
123
124    /**
125     * Converts the decent model to a xmi form
126     *
127     * @param location
128     *            of the decent model file
129     */
130
131    public void convertDECENTModelToXMI(String location) {
132        unregisterMetaModels("");
133        DECENTResourceTool tool = new DECENTResourceTool();
134        Resource resource =
135            tool.loadResourceFromBinary(location + "/model.decentbin", "decentbin",
136                                        DECENTPackage.eINSTANCE);
137        restoreMetaModels();
138        tool.storeResourceContents(resource.getContents(), location + "/model.decent", "decent");
139    }
140
141    /**
142     * Returns the arffx model as IModel instance
143     *
144     * @param arffxModelLocation
145     *            location of the arffx model file
146     * @param read
147     *            indicates if the model should be read from
148     * @param write
149     *            indicates if data should be written in the model
150     * @return EmFModel (IModel) instance from the arffx model, which was loaded
151     * @throws Exception
152     */
153
154    public IModel getARFFxModel(String arffxModelLocation, boolean read, boolean write)
155        throws Exception
156    {
157
158        EmfModel model;
159
160        if (isUseARFFxBinary()) {
161            unregisterMetaModels("");
162            if (!read) {
163                new File(arffxModelLocation).delete();
164                new File(arffxModelLocation + "bin").delete();
165            }
166            ARFFxResourceTool tool = new ARFFxResourceTool();
167            if (new File(arffxModelLocation).exists() &&
168                !new File(arffxModelLocation + "bin").exists())
169            {
170                Resource resource =
171                    tool.loadResourceFromXMI(arffxModelLocation, "arffx", ARFFxPackage.eINSTANCE);
172                tool.storeBinaryResourceContents(resource.getContents(),
173                                                 arffxModelLocation + "bin", "arffxbin");
174            }
175
176            Resource resourceBin =
177                tool.loadResourceFromBinary(arffxModelLocation + "bin", "arffxbin",
178                                            ARFFxPackage.eINSTANCE);
179            // alternative pattern
180            // model = createInMemoryEmfModel("DECENT", resourceLocation,
181            // "../DECENT.Meta/model/DECENTv3.ecore", read, write, resourceBin,
182            // DECENTPackage.eINSTANCE);
183            // restoreMetaModels();
184
185            // NOTE: Adding the package is essential as otherwise epsilon breaks
186            model = new InMemoryEmfModel("ARFFx", resourceBin, ARFFxPackage.eINSTANCE);
187            // model.getModelImpl().getURI().toFileString()
188            model.setStoredOnDisposal(write);
189            model.setReadOnLoad(read);
190            model.setCachingEnabled(true);
191            restoreMetaModels();
192        }
193        else {
194            model =
195                createEmfModel("ARFFx", arffxModelLocation, metaPath + "ARFFx.ecore", read, write);
196        }
197
198        return model;
199    }
200
201    /**
202     * Converts an arffx model to a binary version
203     *
204     * @param location
205     *            of the arffx model
206     */
207    public void convertARFFxModelToBinary(String location) {
208        unregisterMetaModels("");
209        ARFFxResourceTool tool = new ARFFxResourceTool();
210        Resource resource =
211            tool.loadResourceFromXMI(location + "/model.arffx", "arffx", ARFFxPackage.eINSTANCE);
212        tool.storeBinaryResourceContents(resource.getContents(), location + "/model.arffx" + "bin",
213                                         "arffxbin");
214        restoreMetaModels();
215    }
216
217    /**
218     * Converts an arffx model to xmi
219     *
220     * @param location
221     *            of the arffx model
222     */
223
224    public void convertARFFxModelToXMI(String location) {
225        unregisterMetaModels("");
226        ARFFxResourceTool tool = new ARFFxResourceTool();
227        Resource resource =
228            tool.loadResourceFromBinary(location + "/model.arffxbin", "arffxbin",
229                                        DECENTPackage.eINSTANCE);
230        restoreMetaModels();
231        tool.storeResourceContents(resource.getContents(), location + "/model.arffx", "arffx");
232    }
233
234    /**
235     * Returns the log model as IModel instance
236     *
237     * @param logModelLocation
238     *            location of the log model file
239     * @param read
240     *            indicates if the model should be read from
241     * @param write
242     *            indicates if data should be written in the model
243     * @return EmFModel (IModel) instance from the log model, which was loaded
244     * @throws Exception
245     */
246
247    public IModel getLOGModel(String logModelLocation, boolean read, boolean write)
248        throws Exception
249    {
250        if (!new File(logModelLocation).exists()) {
251            read = false;
252        }
253        IModel model = createEmfModel("LOG", logModelLocation, metaPath + "LOG.ecore", read, write);
254        System.setProperty("epsilon.logFileAvailable", "true");
255        return model;
256    }
257
258    /**
259     * Creates an EMF Model
260     *
261     * @param name
262     *            of the emf model
263     * @param model
264     *            name of the model
265     * @param metamodel
266     *            name of the metamodel
267     * @param readOnLoad
268     *            indicates if the model should be read on load
269     * @param storeOnDisposal
270     *            indicates if the model should be stored on disposal
271     * @return
272     * @throws EolModelLoadingException
273     * @throws URISyntaxException
274     */
275
276    @SuppressWarnings("deprecation")
277    protected EmfModel createEmfModel(String name,
278                                      String model,
279                                      String metamodel,
280                                      boolean readOnLoad,
281                                      boolean storeOnDisposal) throws EolModelLoadingException,
282        URISyntaxException
283    {
284        EmfModel emfModel = new EmfModel();
285        StringProperties properties = new StringProperties();
286        properties.put(EmfModel.PROPERTY_NAME, name);
287        properties.put(EmfModel.PROPERTY_ALIASES, name);
288        properties.put(EmfModel.PROPERTY_FILE_BASED_METAMODEL_URI, "file:/" +
289            getFile(metamodel).getAbsolutePath());
290        properties.put(EmfModel.PROPERTY_MODEL_URI, "file:/" + getFile(model).getAbsolutePath());
291        properties.put(EmfModel.PROPERTY_IS_METAMODEL_FILE_BASED, "true");
292        properties.put(EmfModel.PROPERTY_READONLOAD, readOnLoad + "");
293        properties.put(EmfModel.PROPERTY_CACHED, "true");
294        properties.put(EmfModel.PROPERTY_STOREONDISPOSAL, storeOnDisposal + "");
295        emfModel.load(properties, "");
296        // System.out.println(emfModel.allContents());
297        return emfModel;
298    }
299
300    /**
301     * Returns a new File instance from the given filename
302     *
303     * @param fileName
304     *            of the file
305     * @return file name
306     * @throws URISyntaxException
307     */
308    public File getFile(String fileName) throws URISyntaxException {
309        ;
310        return new File(fileName);
311    }
312
313    /**
314     * Restores the metamodels, so that they are registered in the EPackage registry
315     */
316    private void restoreMetaModels() {
317        for (String key : metaModelCache.keySet()) {
318            EPackage.Registry.INSTANCE.put(key, metaModelCache.get(key));
319        };
320    }
321
322    /**
323     * Unregister the metamodels from the EPackage registry
324     *
325     * @param filter
326     *            for filtering out certain instances
327     */
328    private void unregisterMetaModels(String filter) {
329        for (String key : EPackage.Registry.INSTANCE.keySet()) {
330            if (key.contains(filter)) {
331                metaModelCache.put(key, EPackage.Registry.INSTANCE.get(key));
332            }
333        };
334        for (String key : metaModelCache.keySet()) {
335            EPackage.Registry.INSTANCE.remove(key);
336        };
337    }
338
339    /**
340     * Returns true if decent binary model is used
341     *
342     * @return true if binary
343     */
344
345    public boolean isUseDECENTBinary() {
346        return useDECENTBinary;
347    }
348
349    /**
350     * Sets the boolean which indicates, if the decent binary model is used
351     *
352     * @param useDECENTBinary
353     */
354    public void setUseDECENTBinary(boolean useDECENTBinary) {
355        this.useDECENTBinary = useDECENTBinary;
356    }
357
358    /**
359     * Returns true if arffx binary model is used
360     *
361     * @return true if ARFFx
362     */
363    public boolean isUseARFFxBinary() {
364        return useARFFxBinary;
365    }
366
367    /**
368     * Sets the boolean which indicates, if the arffx binary model is used
369     *
370     * @param useARFFxBinary
371     */
372
373    public void setUseARFFxBinary(boolean useARFFxBinary) {
374        this.useARFFxBinary = useARFFxBinary;
375    }
376
377}
Note: See TracBrowser for help on using the repository browser.