| 1 | package de.ugoe.cs.cpdp.decentApp; |
|---|
| 2 | |
|---|
| 3 | import java.io.File; |
|---|
| 4 | import java.net.URISyntaxException; |
|---|
| 5 | import java.util.HashMap; |
|---|
| 6 | |
|---|
| 7 | import org.eclipse.emf.ecore.EPackage; |
|---|
| 8 | import org.eclipse.emf.ecore.resource.Resource; |
|---|
| 9 | import org.eclipse.epsilon.common.util.StringProperties; |
|---|
| 10 | import org.eclipse.epsilon.emc.emf.EmfModel; |
|---|
| 11 | import org.eclipse.epsilon.emc.emf.InMemoryEmfModel; |
|---|
| 12 | import org.eclipse.epsilon.eol.exceptions.models.EolModelLoadingException; |
|---|
| 13 | import org.eclipse.epsilon.eol.models.IModel; |
|---|
| 14 | |
|---|
| 15 | import ARFFx.ARFFxPackage; |
|---|
| 16 | import DECENT.DECENTPackage; |
|---|
| 17 | |
|---|
| 18 | /** |
|---|
| 19 | * Class for handling decent and arffx model files. |
|---|
| 20 | * |
|---|
| 21 | * @author Philip Makedonski, Fabian Trautsch |
|---|
| 22 | * |
|---|
| 23 | */ |
|---|
| 24 | |
|---|
| 25 | 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 | |
|---|
| 304 | } |
|---|