| 1 | package de.uka.ipd.sdq.pcmsolver.transformations; |
| 2 | |
| 3 | import java.io.File; |
| 4 | import java.io.FileNotFoundException; |
| 5 | import java.io.IOException; |
| 6 | import java.util.Collections; |
| 7 | |
| 8 | import org.apache.log4j.Logger; |
| 9 | import org.eclipse.emf.common.util.BasicEList; |
| 10 | import org.eclipse.emf.common.util.EList; |
| 11 | import org.eclipse.emf.common.util.URI; |
| 12 | import org.eclipse.emf.ecore.EClass; |
| 13 | import org.eclipse.emf.ecore.EObject; |
| 14 | import org.eclipse.emf.ecore.resource.Resource; |
| 15 | import org.eclipse.emf.ecore.resource.ResourceSet; |
| 16 | import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; |
| 17 | import org.eclipse.emf.ecore.util.EcoreUtil; |
| 18 | import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl; |
| 19 | import org.eclipse.emf.query.conditions.eobjects.EObjectCondition; |
| 20 | import org.eclipse.emf.query.conditions.eobjects.EObjectTypeRelationCondition; |
| 21 | import org.eclipse.emf.query.conditions.eobjects.TypeRelation; |
| 22 | import org.eclipse.emf.query.statements.FROM; |
| 23 | import org.eclipse.emf.query.statements.IQueryResult; |
| 24 | import org.eclipse.emf.query.statements.SELECT; |
| 25 | import org.eclipse.emf.query.statements.WHERE; |
| 26 | |
| 27 | import de.uka.ipd.sdq.pcm.allocation.AllocationPackage; |
| 28 | import de.uka.ipd.sdq.pcm.parameter.ParameterPackage; |
| 29 | import de.uka.ipd.sdq.pcm.repository.RepositoryPackage; |
| 30 | import de.uka.ipd.sdq.pcm.resourceenvironment.ResourceenvironmentPackage; |
| 31 | import de.uka.ipd.sdq.pcm.resourcetype.ResourcetypePackage; |
| 32 | import de.uka.ipd.sdq.pcm.seff.SeffPackage; |
| 33 | import de.uka.ipd.sdq.pcm.system.SystemPackage; |
| 34 | import de.uka.ipd.sdq.pcm.usagemodel.UsagemodelPackage; |
| 35 | |
| 36 | /** |
| 37 | * Provides utility functions for EMF models. |
| 38 | * |
| 39 | * @author brosch, martens |
| 40 | * |
| 41 | */ |
| 42 | public class EMFHelper { |
| 43 | |
| 44 | /** |
| 45 | * Retrieves all model elements of a given EMF type under some root element. |
| 46 | * |
| 47 | * @param root |
| 48 | * the root element |
| 49 | * @param type |
| 50 | * the type of objects to find |
| 51 | * @return all objects of the given type or a sub type |
| 52 | */ |
| 53 | public EList<EObject> getElements(final EObject root, final EClass type) { |
| 54 | |
| 55 | // Prepare the result list: |
| 56 | EList<EObject> resultList = new BasicEList<EObject>(); |
| 57 | |
| 58 | // Search for elements that have the same type of a sub type of the |
| 59 | // given type: |
| 60 | EObjectCondition hasCompatibleType = new EObjectTypeRelationCondition( |
| 61 | type, TypeRelation.SAMETYPE_OR_SUBTYPE_LITERAL); |
| 62 | |
| 63 | // Perform an EMF Model Query: |
| 64 | IQueryResult queryResult = new SELECT(new FROM(root), new WHERE( |
| 65 | hasCompatibleType)).execute(); |
| 66 | |
| 67 | // Fill the resulting list: |
| 68 | for (Object result : queryResult) { |
| 69 | resultList.add((EObject) result); |
| 70 | } |
| 71 | |
| 72 | // Return the result: |
| 73 | return resultList; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Save the given EObject to the file given by filename. |
| 78 | * |
| 79 | * @param modelToSave |
| 80 | * The EObject to save |
| 81 | * @param fileName |
| 82 | * The filename where to save. |
| 83 | */ |
| 84 | public static void saveToXMIFile(final EObject modelToSave, final String fileName) { |
| 85 | |
| 86 | Logger logger = Logger.getLogger("de.uka.ipd.sdq.dsexplore"); |
| 87 | |
| 88 | logger.debug("Saving " + modelToSave.toString() + " to " + fileName); |
| 89 | |
| 90 | // Create a resource set. |
| 91 | ResourceSet resourceSet = new ResourceSetImpl(); |
| 92 | |
| 93 | // Register the default resource factory -- only needed for stand-alone! |
| 94 | resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap() |
| 95 | .put(Resource.Factory.Registry.DEFAULT_EXTENSION, |
| 96 | new XMIResourceFactoryImpl()); |
| 97 | |
| 98 | URI fileURI = URI.createFileURI(new File(fileName).getAbsolutePath()); |
| 99 | Resource resource = resourceSet.createResource(fileURI); |
| 100 | resource.getContents().add(modelToSave); |
| 101 | |
| 102 | |
| 103 | |
| 104 | try { |
| 105 | resource.save(Collections.EMPTY_MAP); |
| 106 | } catch (FileNotFoundException e){ |
| 107 | if (fileName.length() > 250){ |
| 108 | //try again with a shorter filename |
| 109 | saveToXMIFile(modelToSave, fileName.substring(0, fileName.indexOf("-"))+"-shortened-"+fileName.hashCode()); |
| 110 | } |
| 111 | } catch (IOException e) { |
| 112 | logger.error(e.getMessage()); |
| 113 | } |
| 114 | // logger.debug("Saved " + fileURI); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * |
| 119 | * @param fileName |
| 120 | * the filename specifying the file to load from |
| 121 | * @return The EObject loaded from the file |
| 122 | */ |
| 123 | public static EObject loadFromXMIFile(final String fileName) { |
| 124 | // Create a resource set to hold the resources. |
| 125 | ResourceSet resourceSet = new ResourceSetImpl(); |
| 126 | |
| 127 | // Register the appropriate resource factory to handle all file |
| 128 | // extensions. |
| 129 | resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap() |
| 130 | .put(Resource.Factory.Registry.DEFAULT_EXTENSION, |
| 131 | new XMIResourceFactoryImpl()); |
| 132 | |
| 133 | // Register the package to ensure it is available during loading. |
| 134 | registerPackages(resourceSet); |
| 135 | |
| 136 | // Construct the URI for the instance file. |
| 137 | // The argument is treated as a file path only if it denotes an existing |
| 138 | // file. Otherwise, it's directly treated as a URL. |
| 139 | File file = new File(fileName); |
| 140 | URI uri = file.isFile() ? URI.createFileURI(file.getAbsolutePath()) |
| 141 | : URI.createURI(fileName); |
| 142 | |
| 143 | Resource resource = null; |
| 144 | // Demand load resource for this file. |
| 145 | try { |
| 146 | resource = resourceSet.getResource(uri, true); |
| 147 | } catch (Exception e) { |
| 148 | Logger.getLogger("de.uka.ipd.sdq.dsexplore").error(e.getMessage()); |
| 149 | return null; |
| 150 | } |
| 151 | |
| 152 | // logger.debug("Loaded " + uri); |
| 153 | |
| 154 | // if (!fileName.endsWith(".assembly") && |
| 155 | // !fileName.endsWith("repository")) { |
| 156 | // // Validate the contents of the loaded resource. |
| 157 | // for (Iterator j = resource.getContents().iterator(); j.hasNext();) { |
| 158 | // EObject eObject = (EObject) j.next(); |
| 159 | // Diagnostic diagnostic = Diagnostician.INSTANCE |
| 160 | // .validate(eObject); |
| 161 | // if (diagnostic.getSeverity() != Diagnostic.OK) { |
| 162 | // System.out.println(); |
| 163 | // System.out.println(diagnostic.getMessage()); |
| 164 | // // printDiagnostic(diagnostic, ""); |
| 165 | // |
| 166 | // } |
| 167 | // } |
| 168 | // } |
| 169 | EObject eObject = (EObject) resource.getContents().iterator().next(); |
| 170 | return EcoreUtil.getRootContainer(eObject); |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Copied From de.uka.ipd.sdq.pcmsolver.models.PCMInstance. |
| 175 | * |
| 176 | * @param resourceSet |
| 177 | * The resource set to register all contained model packages |
| 178 | * with. |
| 179 | */ |
| 180 | private static void registerPackages(final ResourceSet resourceSet) { |
| 181 | |
| 182 | resourceSet.getPackageRegistry().put(AllocationPackage.eNS_URI, |
| 183 | AllocationPackage.eINSTANCE); |
| 184 | resourceSet.getPackageRegistry().put(ParameterPackage.eNS_URI, |
| 185 | ParameterPackage.eINSTANCE); |
| 186 | resourceSet.getPackageRegistry().put( |
| 187 | ResourceenvironmentPackage.eNS_URI, |
| 188 | ResourceenvironmentPackage.eINSTANCE); |
| 189 | resourceSet.getPackageRegistry().put(ResourcetypePackage.eNS_URI, |
| 190 | ResourcetypePackage.eINSTANCE); |
| 191 | resourceSet.getPackageRegistry().put(RepositoryPackage.eNS_URI, |
| 192 | RepositoryPackage.eINSTANCE); |
| 193 | resourceSet.getPackageRegistry().put(SeffPackage.eNS_URI, |
| 194 | SeffPackage.eINSTANCE); |
| 195 | resourceSet.getPackageRegistry().put(SystemPackage.eNS_URI, |
| 196 | SystemPackage.eINSTANCE); |
| 197 | resourceSet.getPackageRegistry().put(UsagemodelPackage.eNS_URI, |
| 198 | UsagemodelPackage.eINSTANCE); |
| 199 | |
| 200 | } |
| 201 | } |