- Timestamp:
- 09/24/15 10:59:05 (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/CrossPare/src/de/ugoe/cs/cpdp/decentApp/DECENTEpsilonModelHandler.java
r36 r41 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 1 15 package de.ugoe.cs.cpdp.decentApp; 2 16 … … 20 34 * 21 35 * @author Philip Makedonski, Fabian Trautsch 22 * 36 * 23 37 */ 24 38 25 39 public class DECENTEpsilonModelHandler { 26 private HashMap<String, Object> metaModelCache = new HashMap<>(); 27 private boolean useDECENTBinary = false; 28 private boolean useARFFxBinary = false; 29 30 public static String metaPath = "./decent/models/"; 31 32 /** 33 * Returns the decent model as IModel instance 34 * 35 * @param decentModelLocation location of the decent model file 36 * @param read indicates if the model should be read from 37 * @param write indicates if data should be written in the model 38 * @return EmFModel (IModel) instance from the decent model, which was loaded 39 * @throws Exception 40 */ 41 public IModel getDECENTModel(String decentModelLocation, boolean read, boolean write) throws Exception { 42 43 EmfModel model; 44 45 if (isUseDECENTBinary()) { 46 unregisterMetaModels(""); 47 if (!read) { 48 new File(decentModelLocation).delete(); 49 new File(decentModelLocation+"bin").delete(); 50 } 51 DECENTResourceTool tool = new DECENTResourceTool(); 52 if (new File(decentModelLocation).exists() && !new File(decentModelLocation+"bin").exists()) { 53 Resource resource = tool.loadResourceFromXMI(decentModelLocation,"decent", DECENTPackage.eINSTANCE); 54 tool.storeBinaryResourceContents(resource.getContents(), decentModelLocation+"bin", "decentbin"); 55 } 56 57 Resource resourceBin = tool.loadResourceFromBinary(decentModelLocation+"bin","decentbin", DECENTPackage.eINSTANCE); 58 //alternative pattern 59 // model = createInMemoryEmfModel("DECENT", resourceLocation, "../DECENT.Meta/model/DECENTv3.ecore", read, write, resourceBin, DECENTPackage.eINSTANCE); 60 // restoreMetaModels(); 61 62 //NOTE: Adding the package is essential as otherwise epsilon breaks 63 model = new InMemoryEmfModel("DECENT", resourceBin, DECENTPackage.eINSTANCE); 64 model.setStoredOnDisposal(write); 65 model.setReadOnLoad(read); 66 model.setCachingEnabled(true); 67 restoreMetaModels(); 68 } else { 69 model = createEmfModel("DECENT", decentModelLocation, metaPath+"DECENTv3.ecore", read, write); 70 } 71 72 return model; 73 } 74 75 /** 76 * Converts the decent model to a binary form 77 * 78 * @param location of the decent model file 79 */ 80 public void convertDECENTModelToBinary(String location) { 81 unregisterMetaModels(""); 82 DECENTResourceTool tool = new DECENTResourceTool(); 83 Resource resource = tool.loadResourceFromXMI(location+"/model.decent","decent", DECENTPackage.eINSTANCE); 84 tool.storeBinaryResourceContents(resource.getContents(), location+"/model.decent"+"bin", "decentbin"); 85 restoreMetaModels(); 86 } 87 88 /** 89 * Converts the decent model to a xmi form 90 * 91 * @param location of the decent model file 92 */ 93 94 public void convertDECENTModelToXMI(String location) { 95 unregisterMetaModels(""); 96 DECENTResourceTool tool = new DECENTResourceTool(); 97 Resource resource = tool.loadResourceFromBinary(location+"/model.decentbin","decentbin", DECENTPackage.eINSTANCE); 98 restoreMetaModels(); 99 tool.storeResourceContents(resource.getContents(), location+"/model.decent", "decent"); 100 } 101 102 /** 103 * Returns the arffx model as IModel instance 104 * 105 * @param arffxModelLocation location of the arffx model file 106 * @param read indicates if the model should be read from 107 * @param write indicates if data should be written in the model 108 * @return EmFModel (IModel) instance from the arffx model, which was loaded 109 * @throws Exception 110 */ 111 112 public IModel getARFFxModel(String arffxModelLocation, boolean read, boolean write) throws Exception { 113 114 EmfModel model; 115 116 if (isUseARFFxBinary()) { 117 unregisterMetaModels(""); 118 if (!read) { 119 new File(arffxModelLocation).delete(); 120 new File(arffxModelLocation+"bin").delete(); 121 } 122 ARFFxResourceTool tool = new ARFFxResourceTool(); 123 if (new File(arffxModelLocation).exists() && !new File(arffxModelLocation+"bin").exists()) { 124 Resource resource = tool.loadResourceFromXMI(arffxModelLocation,"arffx", ARFFxPackage.eINSTANCE); 125 tool.storeBinaryResourceContents(resource.getContents(), arffxModelLocation+"bin", "arffxbin"); 126 } 127 128 Resource resourceBin = tool.loadResourceFromBinary(arffxModelLocation+"bin","arffxbin", ARFFxPackage.eINSTANCE); 129 //alternative pattern 130 // model = createInMemoryEmfModel("DECENT", resourceLocation, "../DECENT.Meta/model/DECENTv3.ecore", read, write, resourceBin, DECENTPackage.eINSTANCE); 131 // restoreMetaModels(); 132 133 //NOTE: Adding the package is essential as otherwise epsilon breaks 134 model = new InMemoryEmfModel("ARFFx", resourceBin, ARFFxPackage.eINSTANCE); 135 // model.getModelImpl().getURI().toFileString() 136 model.setStoredOnDisposal(write); 137 model.setReadOnLoad(read); 138 model.setCachingEnabled(true); 139 restoreMetaModels(); 140 } else { 141 model = createEmfModel("ARFFx", arffxModelLocation, metaPath+"ARFFx.ecore", read, write); 142 } 143 144 return model; 145 } 146 147 148 /** 149 * Converts an arffx model to a binary version 150 * 151 * @param location of the arffx model 152 */ 153 public void convertARFFxModelToBinary(String location) { 154 unregisterMetaModels(""); 155 ARFFxResourceTool tool = new ARFFxResourceTool(); 156 Resource resource = tool.loadResourceFromXMI(location+"/model.arffx","arffx", ARFFxPackage.eINSTANCE); 157 tool.storeBinaryResourceContents(resource.getContents(), location+"/model.arffx"+"bin", "arffxbin"); 158 restoreMetaModels(); 159 } 160 161 /** 162 * Converts an arffx model to xmi 163 * 164 * @param location of the arffx model 165 */ 166 167 public void convertARFFxModelToXMI(String location) { 168 unregisterMetaModels(""); 169 ARFFxResourceTool tool = new ARFFxResourceTool(); 170 Resource resource = tool.loadResourceFromBinary(location+"/model.arffxbin","arffxbin", DECENTPackage.eINSTANCE); 171 restoreMetaModels(); 172 tool.storeResourceContents(resource.getContents(), location+"/model.arffx", "arffx"); 173 } 174 175 176 /** 177 * Returns the log model as IModel instance 178 * 179 * @param logModelLocation location of the log model file 180 * @param read indicates if the model should be read from 181 * @param write indicates if data should be written in the model 182 * @return EmFModel (IModel) instance from the log model, which was loaded 183 * @throws Exception 184 */ 185 186 public IModel getLOGModel(String logModelLocation, boolean read, boolean write) throws Exception { 187 if (!new File(logModelLocation).exists()) { 188 read = false; 189 } 190 IModel model = createEmfModel("LOG", logModelLocation, metaPath +"LOG.ecore", read, write); 191 System.setProperty("epsilon.logFileAvailable", "true"); 192 return model; 193 } 194 195 /** 196 * Creates an EMF Model 197 * 198 * @param name of the emf model 199 * @param model name of the model 200 * @param metamodel name of the metamodel 201 * @param readOnLoad indicates if the model should be read on load 202 * @param storeOnDisposal indicates if the model should be stored on disposal 203 * @return 204 * @throws EolModelLoadingException 205 * @throws URISyntaxException 206 */ 207 208 @SuppressWarnings("deprecation") 209 protected EmfModel createEmfModel(String name, String model, 210 String metamodel, boolean readOnLoad, boolean storeOnDisposal) 211 throws EolModelLoadingException, URISyntaxException { 212 EmfModel emfModel = new EmfModel(); 213 StringProperties properties = new StringProperties(); 214 properties.put(EmfModel.PROPERTY_NAME, name); 215 properties.put(EmfModel.PROPERTY_ALIASES, name); 216 properties.put(EmfModel.PROPERTY_FILE_BASED_METAMODEL_URI, 217 "file:/" + getFile(metamodel).getAbsolutePath()); 218 properties.put(EmfModel.PROPERTY_MODEL_URI, 219 "file:/" + getFile(model).getAbsolutePath()); 220 properties.put(EmfModel.PROPERTY_IS_METAMODEL_FILE_BASED, "true"); 221 properties.put(EmfModel.PROPERTY_READONLOAD, readOnLoad + ""); 222 properties.put(EmfModel.PROPERTY_CACHED, "true"); 223 properties.put(EmfModel.PROPERTY_STOREONDISPOSAL, 224 storeOnDisposal + ""); 225 emfModel.load(properties, ""); 226 //System.out.println(emfModel.allContents()); 227 return emfModel; 228 } 229 230 /** 231 * Returns a new File instance from the given filename 232 * 233 * @param fileName of the file 234 * @return 235 * @throws URISyntaxException 236 */ 237 public File getFile(String fileName) throws URISyntaxException {; 238 return new File(fileName); 239 } 240 241 /** 242 * Restores the metamodels, so that they are registered in the 243 * EPackage registry 244 */ 245 private void restoreMetaModels() { 246 for (String key : metaModelCache .keySet()) { 247 EPackage.Registry.INSTANCE.put(key, metaModelCache.get(key)); 248 }; 249 } 250 251 /** 252 * Unregister the metamodels from the EPackage registry 253 * 254 * @param filter for filtering out certain instances 255 */ 256 private void unregisterMetaModels(String filter) { 257 for (String key : EPackage.Registry.INSTANCE.keySet()) { 258 if (key.contains(filter)) { 259 metaModelCache.put(key, EPackage.Registry.INSTANCE.get(key)); 260 } 261 }; 262 for (String key : metaModelCache .keySet()) { 263 EPackage.Registry.INSTANCE.remove(key); 264 }; 265 } 266 267 /** 268 * Returns true if decent binary model is used 269 * @return 270 */ 271 272 public boolean isUseDECENTBinary() { 273 return useDECENTBinary; 274 } 275 276 /** 277 * Sets the boolean which indicates, if the decent binary 278 * model is used 279 * @param useDECENTBinary 280 */ 281 public void setUseDECENTBinary(boolean useDECENTBinary) { 282 this.useDECENTBinary = useDECENTBinary; 283 } 284 285 /** 286 * Returns true if arffx binary model is used 287 * @return 288 */ 289 public boolean isUseARFFxBinary() { 290 return useARFFxBinary; 291 } 292 293 /** 294 * Sets the boolean which indicates, if the arffx binary 295 * model is used 296 * @param useARFFxBinary 297 */ 298 299 public void setUseARFFxBinary(boolean useARFFxBinary) { 300 this.useARFFxBinary = useARFFxBinary; 301 } 302 303 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 304 374 }
Note: See TracChangeset
for help on using the changeset viewer.