diff --git a/edu.kit.ipd.descartes.adaptation.model/src/edu/kit/ipd/descartes/adaptation/model/AbstractEcoreModelLoader.java b/edu.kit.ipd.descartes.adaptation.model/src/edu/kit/ipd/descartes/adaptation/model/AbstractEcoreModelLoader.java index 8f18ab65213f7d209cf92a8dbc64855df9442b3a..1fb86c8d8d1d284790faf80578ee499fe455f681 100644 --- a/edu.kit.ipd.descartes.adaptation.model/src/edu/kit/ipd/descartes/adaptation/model/AbstractEcoreModelLoader.java +++ b/edu.kit.ipd.descartes.adaptation.model/src/edu/kit/ipd/descartes/adaptation/model/AbstractEcoreModelLoader.java @@ -9,31 +9,35 @@ import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IWorkspaceRoot; import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.emf.common.util.URI; +import org.eclipse.emf.common.util.WrappedException; import org.eclipse.emf.ecore.EObject; import org.eclipse.emf.ecore.resource.Resource; import org.eclipse.emf.ecore.resource.ResourceSet; import org.eclipse.emf.ecore.util.EcoreUtil; +import org.eclipse.emf.ecore.xmi.UnresolvedReferenceException; import org.eclipse.emf.ecore.xmi.XMIResource; public abstract class AbstractEcoreModelLoader { - - private static Logger logger = Logger.getLogger(AbstractEcoreModelLoader.class); - private ResourceSet resourceSet = null; - private URI currentUri = null; - private EObject loadedModel; - + + private static Logger logger = Logger + .getLogger(AbstractEcoreModelLoader.class); + private ResourceSet resourceSet = null; + private URI currentUri = null; + private EObject loadedModel; + protected ResourceSet getResourceSet() { - if (resourceSet == null) - throw new NullPointerException("INTITIALIZE RESOURCE SET FIRST! "); - return resourceSet; - } + if (resourceSet == null) + throw new NullPointerException("INTITIALIZE RESOURCE SET FIRST! "); + return resourceSet; + } protected void setResourceSet(ResourceSet resourceSet) { - this.resourceSet = resourceSet; - } + this.resourceSet = resourceSet; + } - /**Implement this method to initialize your {@link ResourceSet}, e.g., - * if you load your models in a standalone application. + /** + * Implement this method to initialize your {@link ResourceSet}, e.g., if + * you load your models in a standalone application. * * @param resourceSet */ @@ -46,79 +50,95 @@ public abstract class AbstractEcoreModelLoader { * @return * @throws IOException */ - public EObject load(URI uri) { - - if (null == uri) - throw new IllegalArgumentException("Missing file URI."); - - if (uri.equals(currentUri) && loadedModel != null) - return loadedModel; - - // Resource resource = resourceSet.createResource(fileURI); - Resource resource = resourceSet.getResource(uri, true); - try { - resource.load(null); - logger.info("Model from URI " + uri.toFileString() + " loaded."); - } catch (IOException e) { - logger.error("Error while loading model " + uri.toFileString(), e); - e.printStackTrace(); - } - currentUri = uri; - EcoreUtil.resolveAll(resourceSet); - loadedModel = resource.getContents().get(0); - return resource.getContents().get(0); - } - - /** - * Persists the specified <code>obj</code> at the given - * <code>uri</code>. - * - * @param uri - * @param obj - * @throws IOException - */ + public EObject load(URI uri) { + + if (null == uri) + throw new IllegalArgumentException("Missing file URI."); + + if (uri.equals(currentUri) && loadedModel != null) + return loadedModel; + + // Resource resource = resourceSet.createResource(fileURI); + Resource resource = null; + try { + resource = resourceSet.getResource(uri, true); + } catch (WrappedException e) { + if (e.exception() instanceof UnresolvedReferenceException) { + logger.error("Could not load model! Reason: " + + e.exception().getMessage()); + logger.error("This is intended since currently a PerformanceDataRepository is used!"); + logger.error("To fix this issue, please remove any 'lastImpact' references from your adaptation process model"); + } + throw e; + } + + try { + resource.load(null); + logger.info("Model from URI " + uri.toFileString() + " loaded."); + } catch (IOException e) { + logger.error("Error while loading model " + uri.toFileString(), e); + e.printStackTrace(); + } + currentUri = uri; + EcoreUtil.resolveAll(resourceSet); + loadedModel = resource.getContents().get(0); + return resource.getContents().get(0); + } + + /** + * Persists the specified <code>obj</code> at the given <code>uri</code>. + * + * @param uri + * @param obj + * @throws IOException + */ public void save(URI uri, EObject obj) throws IOException { - if (null == uri) { - throw new IllegalArgumentException("Missing file URI."); - } - Resource resource = resourceSet.createResource(uri); - resource.getContents().add(obj); - Map<String, Boolean> options = new HashMap<String, Boolean>(); - options.put(XMIResource.OPTION_SCHEMA_LOCATION, Boolean.TRUE); - options.put(XMIResource.OPTION_SAVE_ONLY_IF_CHANGED_MEMORY_BUFFER, Boolean.TRUE); - resource.save(options); - } - - /** Convenience method to create an URI. + if (null == uri) { + throw new IllegalArgumentException("Missing file URI."); + } + Resource resource = resourceSet.createResource(uri); + resource.getContents().add(obj); + Map<String, Boolean> options = new HashMap<String, Boolean>(); + options.put(XMIResource.OPTION_SCHEMA_LOCATION, Boolean.TRUE); + options.put(XMIResource.OPTION_SAVE_ONLY_IF_CHANGED_MEMORY_BUFFER, + Boolean.TRUE); + resource.save(options); + } + + /** + * Convenience method to create an URI. * * @param projectname * @param filename * @param filename_extension * @return */ - public static URI createFileURI(String projectname, String filename, String filename_extension) { - if (null == projectname) { - throw new IllegalArgumentException("Missing project name."); - } - if (null == filename) { - throw new IllegalArgumentException("Missing file name."); - } - if (null == filename_extension) { - throw new IllegalArgumentException("Missing file name extension."); - } - /* TODO check for null pointer exceptions */ - IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); - IProject project = root.getProject(projectname); - String projectPath = project.getLocationURI().getPath(); - URI fileURI = URI.createFileURI(projectPath); - return fileURI.appendSegment(filename).appendFileExtension(filename_extension); - } - - /** - * Convenience method to determine the current model location. - * @return - */ - public URI getCurrentModelUri() { - return currentUri; - } + public static URI createFileURI(String projectname, String filename, + String filename_extension) { + if (null == projectname) { + throw new IllegalArgumentException("Missing project name."); + } + if (null == filename) { + throw new IllegalArgumentException("Missing file name."); + } + if (null == filename_extension) { + throw new IllegalArgumentException("Missing file name extension."); + } + /* TODO check for null pointer exceptions */ + IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); + IProject project = root.getProject(projectname); + String projectPath = project.getLocationURI().getPath(); + URI fileURI = URI.createFileURI(projectPath); + return fileURI.appendSegment(filename).appendFileExtension( + filename_extension); + } + + /** + * Convenience method to determine the current model location. + * + * @return + */ + public URI getCurrentModelUri() { + return currentUri; + } } diff --git a/edu.kit.ipd.descartes.adaptation.model/src/edu/kit/ipd/descartes/adaptation/model/dmm/DmlModelLoader.java b/edu.kit.ipd.descartes.adaptation.model/src/edu/kit/ipd/descartes/adaptation/model/dmm/DmlModelLoader.java index 8c1a0764e00a4c6e671ea51dc65697132cea296d..ad0ea8d32401d36bbeed6cb608ef9ba166edc23d 100644 --- a/edu.kit.ipd.descartes.adaptation.model/src/edu/kit/ipd/descartes/adaptation/model/dmm/DmlModelLoader.java +++ b/edu.kit.ipd.descartes.adaptation.model/src/edu/kit/ipd/descartes/adaptation/model/dmm/DmlModelLoader.java @@ -93,7 +93,7 @@ public class DmlModelLoader extends AbstractEcoreModelLoader { @Override public AdaptationProcess load(URI uri) { - return (AdaptationProcess) super.load(uri); + return (AdaptationProcess) super.load(uri); } public static void startRecording() {