Changeset 41 for trunk/CrossPare/src/de/ugoe/cs/cpdp/decentApp
- Timestamp:
- 09/24/15 10:59:05 (9 years ago)
- Location:
- trunk/CrossPare/src/de/ugoe/cs/cpdp/decentApp
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/CrossPare/src/de/ugoe/cs/cpdp/decentApp/ARFFxResourceTool.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 … … 11 25 * 12 26 * @author Philip Makedonski, Fabian Trautsch 13 * 27 * 14 28 */ 15 29 public class ARFFxResourceTool extends ResourceTool { 16 17 /** 18 * Initializes the Tool Factory, from which the models can be loaded and 19 * inizializes the validator. 20 */ 21 public ARFFxResourceTool(){ 22 super(ARFFxResourceTool.class.getName()); 23 ARFFxPackageImpl.init(); 24 25 // Commented, because simulation has problems with this 26 initializeValidator(); 27 } 28 29 /** 30 * Inizializes the model validator 31 */ 32 @Override 33 protected void initializeValidator(){ 34 super.initializeValidator(); 35 EObjectValidator validator = new EObjectValidator(); 36 EValidator.Registry.INSTANCE.put(ARFFxPackage.eINSTANCE, validator); 37 } 38 30 31 /** 32 * Initializes the Tool Factory, from which the models can be loaded and inizializes the 33 * validator. 34 */ 35 public ARFFxResourceTool() { 36 super(ARFFxResourceTool.class.getName()); 37 ARFFxPackageImpl.init(); 38 39 // Commented, because simulation has problems with this 40 initializeValidator(); 41 } 42 43 /** 44 * Inizializes the model validator 45 */ 46 @Override 47 protected void initializeValidator() { 48 super.initializeValidator(); 49 EObjectValidator validator = new EObjectValidator(); 50 EValidator.Registry.INSTANCE.put(ARFFxPackage.eINSTANCE, validator); 51 } 39 52 40 53 } -
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 } -
trunk/CrossPare/src/de/ugoe/cs/cpdp/decentApp/DECENTResourceTool.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 … … 12 26 * 13 27 * @author Philip Makedonski, Fabian Trautsch 14 * 28 * 15 29 */ 16 30 public class DECENTResourceTool extends ResourceTool { 17 18 /** 19 * Initializes the Tool Factory, from which the models can be loaded and 20 * inizializes the validator. 21 */ 22 public DECENTResourceTool(){ 23 super(DECENTResourceTool.class.getName()); 24 DECENTPackageImpl.init(); 25 this.resourceFactory = new DECENTResourceFactoryImpl(); 26 initializeValidator(); 27 } 28 29 /** 30 * Inizializes the model validator 31 */ 32 @Override 33 protected void initializeValidator(){ 34 super.initializeValidator(); 35 EObjectValidator validator = new EObjectValidator(); 36 EValidator.Registry.INSTANCE.put(DECENTPackage.eINSTANCE, validator); 37 } 38 39 31 32 /** 33 * Initializes the Tool Factory, from which the models can be loaded and inizializes the 34 * validator. 35 */ 36 public DECENTResourceTool() { 37 super(DECENTResourceTool.class.getName()); 38 DECENTPackageImpl.init(); 39 this.resourceFactory = new DECENTResourceFactoryImpl(); 40 initializeValidator(); 41 } 42 43 /** 44 * Inizializes the model validator 45 */ 46 @Override 47 protected void initializeValidator() { 48 super.initializeValidator(); 49 EObjectValidator validator = new EObjectValidator(); 50 EValidator.Registry.INSTANCE.put(DECENTPackage.eINSTANCE, validator); 51 } 40 52 41 53 } -
trunk/CrossPare/src/de/ugoe/cs/cpdp/decentApp/FileWatcher.java
r32 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 … … 8 22 * 9 23 * @author Philip Makedonski 10 * 24 * 11 25 */ 12 26 public abstract class FileWatcher extends TimerTask { 13 // Last timestamp 14 private long timeStamp; 15 16 // File to watch 17 private File file; 27 // Last timestamp 28 private long timeStamp; 18 29 19 /** 20 * Constructor 21 * @param file 22 */ 23 public FileWatcher(File file) { 24 this.file = file; 25 this.timeStamp = file.lastModified(); 26 } 30 // File to watch 31 private File file; 27 32 28 /** 29 * Watches a file and executes the onChange Method 30 * if a file is changed 31 */ 32 public final void run() { 33 long timeStamp = file.lastModified(); 33 /** 34 * Constructor 35 * 36 * @param file 37 */ 38 public FileWatcher(File file) { 39 this.file = file; 40 this.timeStamp = file.lastModified(); 41 } 34 42 35 if (this.timeStamp != timeStamp) { 36 this.timeStamp = timeStamp; 37 onChange(file); 38 } 39 } 43 /** 44 * Watches a file and executes the onChange Method if a file is changed 45 */ 46 public final void run() { 47 long timeStamp = file.lastModified(); 40 48 41 protected abstract void onChange(File file); 49 if (this.timeStamp != timeStamp) { 50 this.timeStamp = timeStamp; 51 onChange(file); 52 } 53 } 54 55 protected abstract void onChange(File file); 42 56 } -
trunk/CrossPare/src/de/ugoe/cs/cpdp/decentApp/ResourceTool.java
r32 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 … … 32 46 * 33 47 * @author Philip Makedonski 34 * 48 * 35 49 */ 36 50 public class ResourceTool { 37 51 38 protected ResourceFactoryImpl resourceFactory = new XMIResourceFactoryImpl(); 39 40 /** 41 * Constructor 42 * @param loggedClass 43 */ 44 public ResourceTool(String loggedClass) { 45 System.setProperty("org.slf4j.simpleLogger.logFile","validation.log"); 46 System.setProperty("org.slf4j.simpleLogger.logFile","System.out"); 47 } 48 49 /** 50 * Initializes the validator 51 */ 52 protected void initializeValidator() { 53 // OCL.initialize(null); 54 String oclDelegateURI = OCLConstants.OCL_DELEGATE_URI+"/Pivot"; 55 56 EOperation.Internal.InvocationDelegate.Factory.Registry.INSTANCE.put(oclDelegateURI, 57 new OCLInvocationDelegateFactory(oclDelegateURI)); 58 EStructuralFeature.Internal.SettingDelegate.Factory.Registry.INSTANCE.put(oclDelegateURI, 59 new OCLSettingDelegateFactory(oclDelegateURI)); 60 EValidator.ValidationDelegate.Registry.INSTANCE.put(oclDelegateURI, 61 new OCLValidationDelegateFactory(oclDelegateURI)); 62 63 // EStructuralFeature.Internal.SettingDelegate.Factory.Registry.INSTANCE.put(oclDelegateURI, 64 // new OCLSettingDelegateFactory.Global()); 65 // QueryDelegate.Factory.Registry.INSTANCE.put(oclDelegateURI, new OCLQueryDelegateFactory.Global()); 66 67 } 68 69 /** 70 * Validates the ressource 71 * @param resource to validate 72 */ 73 public void validateResource(Resource resource) { 74 BasicDiagnostic diagnostics = new BasicDiagnostic(); 75 boolean valid = true; 76 for (EObject eo : resource.getContents()) 77 { 78 Map<Object, Object> context = new HashMap<Object, Object>(); 79 boolean validationResult = Diagnostician.INSTANCE.validate(eo, diagnostics, context); 80 showDiagnostics(diagnostics, ""); 81 valid &= validationResult; 82 } 83 84 if (!valid){ 85 System.out.println("Problem with validation!"); 86 } 87 } 88 89 /** 90 * Output method for showing diagnostics for different ressources 91 * @param diagnostics 92 * @param indent 93 */ 94 protected void showDiagnostics(Diagnostic diagnostics, String indent) { 95 indent+=" "; 96 for (Diagnostic d : diagnostics.getChildren()){ 97 System.out.println(indent+d.getSource()); 98 System.out.println(indent+" "+d.getMessage()); 99 showDiagnostics(d,indent); 100 } 101 } 102 103 104 /** 105 * Loads a ressource from XMI 106 * @param inputPath path to the xmi 107 * @param extension of the ressource to load 108 * @param p the given EPackage 109 * @return 110 */ 111 //TODO: workarounds copied from respective methods without EPackage parameter 112 @SuppressWarnings({ "rawtypes", "unchecked" }) 113 public Resource loadResourceFromXMI(String inputPath, String extension, EPackage p) { 114 Resource.Factory.Registry reg = Resource.Factory.Registry.INSTANCE; 115 Map<String, Object> m = reg.getExtensionToFactoryMap(); 116 m.put(extension, resourceFactory); 117 ResourceSet resSetIn = new ResourceSetImpl(); 118 //critical part 119 resSetIn.getPackageRegistry().put(p.getNsURI(), p); 120 121 Resource inputResource = resSetIn.createResource(URI.createURI(inputPath)); 122 try { 123 Map options = new HashMap<>(); 124 options.put(XMIResourceImpl.OPTION_DEFER_IDREF_RESOLUTION, Boolean.TRUE); 125 // options.put(XMIResourceImpl.OPTION_PROCESS_DANGLING_HREF, XMIResourceImpl.OPTION_PROCESS_DANGLING_HREF_DISCARD); 126 inputResource.load(options); 127 } catch (IOException e) { 128 e.printStackTrace(); 129 } 130 return inputResource; 131 } 132 133 /** 134 * Loads a ressource from XMI 135 * @param inputPath path to the xmi 136 * @param extension of the ressource to load 137 * @return 138 */ 139 140 @SuppressWarnings({ "rawtypes", "unchecked" }) 141 public Resource loadResourceFromXMI(String inputPath, String extension) { 142 Resource.Factory.Registry reg = Resource.Factory.Registry.INSTANCE; 143 Map<String, Object> m = reg.getExtensionToFactoryMap(); 144 m.put(extension, resourceFactory); 145 ResourceSet resSetIn = new ResourceSetImpl(); 146 Resource inputResource = resSetIn.createResource(URI.createURI(inputPath)); 147 try { 148 Map options = new HashMap<>(); 149 options.put(XMIResourceImpl.OPTION_DEFER_IDREF_RESOLUTION, Boolean.TRUE); 150 // options.put(XMIResourceImpl.OPTION_PROCESS_DANGLING_HREF, XMIResourceImpl.OPTION_PROCESS_DANGLING_HREF_DISCARD); 151 inputResource.load(options); 152 } catch (IOException e) { 153 e.printStackTrace(); 154 } 155 return inputResource; 156 } 157 158 /** 159 * Gets a ressource from a binary form 160 * @param inputPath path to the binary 161 * @param extension of the model to load 162 * @param p EPackage to put the loaded ressource in 163 * @return 164 */ 165 public Resource getResourceFromBinary(String inputPath, String extension, EPackage p) { 166 Resource.Factory.Registry reg = Resource.Factory.Registry.INSTANCE; 167 Map<String, Object> m = reg.getExtensionToFactoryMap(); 168 m.put(extension, new Resource.Factory() { 169 170 @Override 171 public Resource createResource(URI uri) { 172 return new BinaryResourceImpl(uri); 173 } 174 175 }); 176 177 ResourceSet resSetIn = new ResourceSetImpl(); 178 //critical part 179 resSetIn.getPackageRegistry().put(p.getNsURI(), p); 180 181 Resource inputResource = resSetIn.createResource(URI.createURI(inputPath)); 182 return inputResource; 183 } 184 185 186 /** 187 * Loads a ressource from a binary form 188 * @param inputPath path to the binary 189 * @param extension of the model to load 190 * @param p EPackage to put the loaded ressource in 191 * @return 192 */ 193 //TODO: workarounds copied from respective methods without EPackage parameter 194 @SuppressWarnings({ "rawtypes" }) 195 public Resource loadResourceFromBinary(String inputPath, String extension, EPackage p) { 196 Resource.Factory.Registry reg = Resource.Factory.Registry.INSTANCE; 197 Map<String, Object> m = reg.getExtensionToFactoryMap(); 198 m.put(extension, new Resource.Factory() { 199 200 @Override 201 public Resource createResource(URI uri) { 202 return new BinaryResourceImpl(uri); 203 } 204 205 }); 206 207 ResourceSet resSetIn = new ResourceSetImpl(); 208 //critical part 209 resSetIn.getPackageRegistry().put(p.getNsURI(), p); 210 211 Resource inputResource = resSetIn.createResource(URI.createURI(inputPath)); 212 if (new File(inputPath).exists()) { 213 214 try { 215 Map options = new HashMap<>(); 216 // options.put(BinaryResourceImpl.OPTION_DEFER_IDREF_RESOLUTION, Boolean.TRUE); 217 // options.put(BinaryResourceImpl.OPTION_DEFER_IDREF_RESOLUTION, Boolean.TRUE); 218 // options.put(XMIResourceImpl.OPTION_PROCESS_DANGLING_HREF, XMIResourceImpl.OPTION_PROCESS_DANGLING_HREF_DISCARD); 219 inputResource.load(options); 220 } catch (IOException e) { 221 e.printStackTrace(); 222 } 223 } 224 return inputResource; 225 } 226 227 /** 228 * Loads a ressource from a binary form 229 * 230 * @param inputPath path to the binary 231 * @param extension of the model to load 232 * @return 233 */ 234 @SuppressWarnings({ "rawtypes" }) 235 public Resource loadResourceFromBinary(String inputPath, String extension) { 236 Resource.Factory.Registry reg = Resource.Factory.Registry.INSTANCE; 237 Map<String, Object> m = reg.getExtensionToFactoryMap(); 238 m.put(extension, new Resource.Factory() { 239 240 @Override 241 public Resource createResource(URI uri) { 242 return new BinaryResourceImpl(uri); 243 } 244 245 }); 246 247 ResourceSet resSetIn = new ResourceSetImpl(); 248 Resource inputResource = resSetIn.createResource(URI.createURI(inputPath)); 249 try { 250 Map options = new HashMap<>(); 251 // options.put(XMIResourceImpl.OPTION_DEFER_IDREF_RESOLUTION, Boolean.TRUE); 252 // options.put(XMIResourceImpl.OPTION_PROCESS_DANGLING_HREF, XMIResourceImpl.OPTION_PROCESS_DANGLING_HREF_DISCARD); 253 inputResource.load(options); 254 } catch (IOException e) { 255 e.printStackTrace(); 256 } 257 return inputResource; 258 } 259 260 /** 261 * Stores the binary resource contents to a given path 262 * 263 * @param contents EList of different EObjects to store 264 * @param outputPath path to store to 265 * @param extension of the model to store 266 */ 267 @SuppressWarnings({ "rawtypes" }) 268 public void storeBinaryResourceContents(EList<EObject> contents, String outputPath, String extension) { 269 Resource.Factory.Registry reg = Resource.Factory.Registry.INSTANCE; 270 Map<String, Object> m = reg.getExtensionToFactoryMap(); 271 m.put(extension, new Resource.Factory() { 272 273 @Override 274 public Resource createResource(URI uri) { 275 return new BinaryResourceImpl(uri); 276 } 277 278 }); 279 280 ResourceSet resSet = new ResourceSetImpl(); 281 Resource outputResource = resSet.createResource(URI.createURI(outputPath)); 282 outputResource.getContents().addAll(contents); 283 try { 284 Map options = new HashMap<>(); 285 // options.put(XMIResourceImpl.OPTION_PROCESS_DANGLING_HREF, XMIResourceImpl.OPTION_PROCESS_DANGLING_HREF_DISCARD); 286 outputResource.save(options); 287 } catch (IOException e) { 288 e.printStackTrace(); 289 } 290 } 291 292 /** 293 * Stores the resource contents to a given path 294 * 295 * @param contents EList of different EObjects to store 296 * @param outputPath path to store to 297 * @param extension of the model to store 298 */ 299 @SuppressWarnings({ "unchecked", "rawtypes" }) 300 public void storeResourceContents(EList<EObject> contents, String outputPath, String extension) { 301 //TODO: duplicated from loadResourceFromXMI => move to a more appropriate location 302 Resource.Factory.Registry reg = Resource.Factory.Registry.INSTANCE; 303 Map<String, Object> m = reg.getExtensionToFactoryMap(); 304 m.put(extension, resourceFactory); 305 306 ResourceSet resSet = new ResourceSetImpl(); 307 Resource outputResource = resSet.createResource(URI.createURI(outputPath)); 308 outputResource.getContents().addAll(contents); 309 try { 310 Map options = new HashMap<>(); 311 options.put(XMIResourceImpl.OPTION_ENCODING, "UTF-8"); 312 // options.put(XMIResourceImpl.OPTION_PROCESS_DANGLING_HREF, XMIResourceImpl.OPTION_PROCESS_DANGLING_HREF_DISCARD); 313 outputResource.save(options); 314 } catch (IOException e) { 315 e.printStackTrace(); 316 } 317 } 318 52 protected ResourceFactoryImpl resourceFactory = new XMIResourceFactoryImpl(); 53 54 /** 55 * Constructor 56 * 57 * @param loggedClass 58 */ 59 public ResourceTool(String loggedClass) { 60 System.setProperty("org.slf4j.simpleLogger.logFile", "validation.log"); 61 System.setProperty("org.slf4j.simpleLogger.logFile", "System.out"); 62 } 63 64 /** 65 * Initializes the validator 66 */ 67 protected void initializeValidator() { 68 // OCL.initialize(null); 69 String oclDelegateURI = OCLConstants.OCL_DELEGATE_URI + "/Pivot"; 70 71 EOperation.Internal.InvocationDelegate.Factory.Registry.INSTANCE 72 .put(oclDelegateURI, new OCLInvocationDelegateFactory(oclDelegateURI)); 73 EStructuralFeature.Internal.SettingDelegate.Factory.Registry.INSTANCE 74 .put(oclDelegateURI, new OCLSettingDelegateFactory(oclDelegateURI)); 75 EValidator.ValidationDelegate.Registry.INSTANCE 76 .put(oclDelegateURI, new OCLValidationDelegateFactory(oclDelegateURI)); 77 78 // EStructuralFeature.Internal.SettingDelegate.Factory.Registry.INSTANCE.put(oclDelegateURI, 79 // new OCLSettingDelegateFactory.Global()); 80 // QueryDelegate.Factory.Registry.INSTANCE.put(oclDelegateURI, new 81 // OCLQueryDelegateFactory.Global()); 82 83 } 84 85 /** 86 * Validates the ressource 87 * 88 * @param resource 89 * to validate 90 */ 91 public void validateResource(Resource resource) { 92 BasicDiagnostic diagnostics = new BasicDiagnostic(); 93 boolean valid = true; 94 for (EObject eo : resource.getContents()) { 95 Map<Object, Object> context = new HashMap<Object, Object>(); 96 boolean validationResult = Diagnostician.INSTANCE.validate(eo, diagnostics, context); 97 showDiagnostics(diagnostics, ""); 98 valid &= validationResult; 99 } 100 101 if (!valid) { 102 System.out.println("Problem with validation!"); 103 } 104 } 105 106 /** 107 * Output method for showing diagnostics for different ressources 108 * 109 * @param diagnostics 110 * @param indent 111 */ 112 protected void showDiagnostics(Diagnostic diagnostics, String indent) { 113 indent += " "; 114 for (Diagnostic d : diagnostics.getChildren()) { 115 System.out.println(indent + d.getSource()); 116 System.out.println(indent + " " + d.getMessage()); 117 showDiagnostics(d, indent); 118 } 119 } 120 121 /** 122 * Loads a ressource from XMI 123 * 124 * @param inputPath 125 * path to the xmi 126 * @param extension 127 * of the ressource to load 128 * @param p 129 * the given EPackage 130 * @return 131 */ 132 // TODO: workarounds copied from respective methods without EPackage parameter 133 @SuppressWarnings( 134 { "rawtypes", "unchecked" }) 135 public Resource loadResourceFromXMI(String inputPath, String extension, EPackage p) { 136 Resource.Factory.Registry reg = Resource.Factory.Registry.INSTANCE; 137 Map<String, Object> m = reg.getExtensionToFactoryMap(); 138 m.put(extension, resourceFactory); 139 ResourceSet resSetIn = new ResourceSetImpl(); 140 // critical part 141 resSetIn.getPackageRegistry().put(p.getNsURI(), p); 142 143 Resource inputResource = resSetIn.createResource(URI.createURI(inputPath)); 144 try { 145 Map options = new HashMap<>(); 146 options.put(XMIResourceImpl.OPTION_DEFER_IDREF_RESOLUTION, Boolean.TRUE); 147 // options.put(XMIResourceImpl.OPTION_PROCESS_DANGLING_HREF, 148 // XMIResourceImpl.OPTION_PROCESS_DANGLING_HREF_DISCARD); 149 inputResource.load(options); 150 } 151 catch (IOException e) { 152 e.printStackTrace(); 153 } 154 return inputResource; 155 } 156 157 /** 158 * Loads a ressource from XMI 159 * 160 * @param inputPath 161 * path to the xmi 162 * @param extension 163 * of the ressource to load 164 * @return 165 */ 166 167 @SuppressWarnings( 168 { "rawtypes", "unchecked" }) 169 public Resource loadResourceFromXMI(String inputPath, String extension) { 170 Resource.Factory.Registry reg = Resource.Factory.Registry.INSTANCE; 171 Map<String, Object> m = reg.getExtensionToFactoryMap(); 172 m.put(extension, resourceFactory); 173 ResourceSet resSetIn = new ResourceSetImpl(); 174 Resource inputResource = resSetIn.createResource(URI.createURI(inputPath)); 175 try { 176 Map options = new HashMap<>(); 177 options.put(XMIResourceImpl.OPTION_DEFER_IDREF_RESOLUTION, Boolean.TRUE); 178 // options.put(XMIResourceImpl.OPTION_PROCESS_DANGLING_HREF, 179 // XMIResourceImpl.OPTION_PROCESS_DANGLING_HREF_DISCARD); 180 inputResource.load(options); 181 } 182 catch (IOException e) { 183 e.printStackTrace(); 184 } 185 return inputResource; 186 } 187 188 /** 189 * Gets a ressource from a binary form 190 * 191 * @param inputPath 192 * path to the binary 193 * @param extension 194 * of the model to load 195 * @param p 196 * EPackage to put the loaded ressource in 197 * @return 198 */ 199 public Resource getResourceFromBinary(String inputPath, String extension, EPackage p) { 200 Resource.Factory.Registry reg = Resource.Factory.Registry.INSTANCE; 201 Map<String, Object> m = reg.getExtensionToFactoryMap(); 202 m.put(extension, new Resource.Factory() { 203 204 @Override 205 public Resource createResource(URI uri) { 206 return new BinaryResourceImpl(uri); 207 } 208 209 }); 210 211 ResourceSet resSetIn = new ResourceSetImpl(); 212 // critical part 213 resSetIn.getPackageRegistry().put(p.getNsURI(), p); 214 215 Resource inputResource = resSetIn.createResource(URI.createURI(inputPath)); 216 return inputResource; 217 } 218 219 /** 220 * Loads a ressource from a binary form 221 * 222 * @param inputPath 223 * path to the binary 224 * @param extension 225 * of the model to load 226 * @param p 227 * EPackage to put the loaded ressource in 228 * @return 229 */ 230 // TODO: workarounds copied from respective methods without EPackage parameter 231 @SuppressWarnings( 232 { "rawtypes" }) 233 public Resource loadResourceFromBinary(String inputPath, String extension, EPackage p) { 234 Resource.Factory.Registry reg = Resource.Factory.Registry.INSTANCE; 235 Map<String, Object> m = reg.getExtensionToFactoryMap(); 236 m.put(extension, new Resource.Factory() { 237 238 @Override 239 public Resource createResource(URI uri) { 240 return new BinaryResourceImpl(uri); 241 } 242 243 }); 244 245 ResourceSet resSetIn = new ResourceSetImpl(); 246 // critical part 247 resSetIn.getPackageRegistry().put(p.getNsURI(), p); 248 249 Resource inputResource = resSetIn.createResource(URI.createURI(inputPath)); 250 if (new File(inputPath).exists()) { 251 252 try { 253 Map options = new HashMap<>(); 254 // options.put(BinaryResourceImpl.OPTION_DEFER_IDREF_RESOLUTION, Boolean.TRUE); 255 // options.put(BinaryResourceImpl.OPTION_DEFER_IDREF_RESOLUTION, Boolean.TRUE); 256 // options.put(XMIResourceImpl.OPTION_PROCESS_DANGLING_HREF, 257 // XMIResourceImpl.OPTION_PROCESS_DANGLING_HREF_DISCARD); 258 inputResource.load(options); 259 } 260 catch (IOException e) { 261 e.printStackTrace(); 262 } 263 } 264 return inputResource; 265 } 266 267 /** 268 * Loads a ressource from a binary form 269 * 270 * @param inputPath 271 * path to the binary 272 * @param extension 273 * of the model to load 274 * @return 275 */ 276 @SuppressWarnings( 277 { "rawtypes" }) 278 public Resource loadResourceFromBinary(String inputPath, String extension) { 279 Resource.Factory.Registry reg = Resource.Factory.Registry.INSTANCE; 280 Map<String, Object> m = reg.getExtensionToFactoryMap(); 281 m.put(extension, new Resource.Factory() { 282 283 @Override 284 public Resource createResource(URI uri) { 285 return new BinaryResourceImpl(uri); 286 } 287 288 }); 289 290 ResourceSet resSetIn = new ResourceSetImpl(); 291 Resource inputResource = resSetIn.createResource(URI.createURI(inputPath)); 292 try { 293 Map options = new HashMap<>(); 294 // options.put(XMIResourceImpl.OPTION_DEFER_IDREF_RESOLUTION, Boolean.TRUE); 295 // options.put(XMIResourceImpl.OPTION_PROCESS_DANGLING_HREF, 296 // XMIResourceImpl.OPTION_PROCESS_DANGLING_HREF_DISCARD); 297 inputResource.load(options); 298 } 299 catch (IOException e) { 300 e.printStackTrace(); 301 } 302 return inputResource; 303 } 304 305 /** 306 * Stores the binary resource contents to a given path 307 * 308 * @param contents 309 * EList of different EObjects to store 310 * @param outputPath 311 * path to store to 312 * @param extension 313 * of the model to store 314 */ 315 @SuppressWarnings( 316 { "rawtypes" }) 317 public void storeBinaryResourceContents(EList<EObject> contents, 318 String outputPath, 319 String extension) 320 { 321 Resource.Factory.Registry reg = Resource.Factory.Registry.INSTANCE; 322 Map<String, Object> m = reg.getExtensionToFactoryMap(); 323 m.put(extension, new Resource.Factory() { 324 325 @Override 326 public Resource createResource(URI uri) { 327 return new BinaryResourceImpl(uri); 328 } 329 330 }); 331 332 ResourceSet resSet = new ResourceSetImpl(); 333 Resource outputResource = resSet.createResource(URI.createURI(outputPath)); 334 outputResource.getContents().addAll(contents); 335 try { 336 Map options = new HashMap<>(); 337 // options.put(XMIResourceImpl.OPTION_PROCESS_DANGLING_HREF, 338 // XMIResourceImpl.OPTION_PROCESS_DANGLING_HREF_DISCARD); 339 outputResource.save(options); 340 } 341 catch (IOException e) { 342 e.printStackTrace(); 343 } 344 } 345 346 /** 347 * Stores the resource contents to a given path 348 * 349 * @param contents 350 * EList of different EObjects to store 351 * @param outputPath 352 * path to store to 353 * @param extension 354 * of the model to store 355 */ 356 @SuppressWarnings( 357 { "unchecked", "rawtypes" }) 358 public void storeResourceContents(EList<EObject> contents, String outputPath, String extension) 359 { 360 // TODO: duplicated from loadResourceFromXMI => move to a more appropriate location 361 Resource.Factory.Registry reg = Resource.Factory.Registry.INSTANCE; 362 Map<String, Object> m = reg.getExtensionToFactoryMap(); 363 m.put(extension, resourceFactory); 364 365 ResourceSet resSet = new ResourceSetImpl(); 366 Resource outputResource = resSet.createResource(URI.createURI(outputPath)); 367 outputResource.getContents().addAll(contents); 368 try { 369 Map options = new HashMap<>(); 370 options.put(XMIResourceImpl.OPTION_ENCODING, "UTF-8"); 371 // options.put(XMIResourceImpl.OPTION_PROCESS_DANGLING_HREF, 372 // XMIResourceImpl.OPTION_PROCESS_DANGLING_HREF_DISCARD); 373 outputResource.save(options); 374 } 375 catch (IOException e) { 376 e.printStackTrace(); 377 } 378 } 319 379 320 380 }
Note: See TracChangeset
for help on using the changeset viewer.