| 1 | package de.uka.ipd.sdq.cip.configuration; |
| 2 | |
| 3 | import java.util.ArrayList; |
| 4 | import java.util.Collection; |
| 5 | |
| 6 | import org.eclipse.emf.common.util.TreeIterator; |
| 7 | import org.eclipse.emf.common.util.URI; |
| 8 | import org.eclipse.emf.ecore.EObject; |
| 9 | import org.eclipse.emf.ecore.EPackage; |
| 10 | import org.eclipse.emf.ecore.EPackage.Registry; |
| 11 | import org.eclipse.emf.ecore.resource.Resource; |
| 12 | import org.eclipse.emf.ecore.resource.ResourceSet; |
| 13 | import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; |
| 14 | |
| 15 | import de.uka.ipd.sdq.cip.workflow.jobs.PrepareTransformationsJob; |
| 16 | import de.uka.ipd.sdq.workflow.mdsd.blackboard.MDSDBlackboard; |
| 17 | import de.uka.ipd.sdq.workflow.mdsd.blackboard.ModelLocation; |
| 18 | import de.uka.ipd.sdq.workflow.mdsd.blackboard.ResourceSetPartition; |
| 19 | import de.uka.ipd.sdq.workflow.mdsd.emf.qvtr.QVTRScript; |
| 20 | import de.uka.ipd.sdq.workflow.mdsd.emf.qvtr.QVTRScriptInfo; |
| 21 | |
| 22 | public class QVTConfigurationHelper { |
| 23 | |
| 24 | public static Collection<Object> getMetaModelsFromBlackboard(MDSDBlackboard blackboard, String modelPartitionID) { |
| 25 | ResourceSetPartition partition = blackboard.getPartition(modelPartitionID); |
| 26 | Registry registry = partition.getResourceSet().getPackageRegistry(); |
| 27 | return registry.values(); |
| 28 | } |
| 29 | |
| 30 | public static Collection<Object> getMetaModelsFromFile(URI metamodelURI) { |
| 31 | ArrayList<Object> list = new ArrayList<Object>(); |
| 32 | ResourceSet rset = new ResourceSetImpl(); |
| 33 | Resource resource = rset.getResource(metamodelURI,true); |
| 34 | TreeIterator<EObject> iterator = resource.getAllContents(); |
| 35 | for(EObject object = null; iterator.hasNext(); object = iterator.next() ) { |
| 36 | if(object instanceof EPackage) |
| 37 | list.add(object); |
| 38 | } |
| 39 | |
| 40 | return list; |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Build the location objects out of the blackboards PCM model partition. |
| 45 | * |
| 46 | * @return The prepared model locations for the PCM models. |
| 47 | */ |
| 48 | public static ModelLocation[] getModelsFromBlackboard(MDSDBlackboard blackboard, String modelPartitionID) { |
| 49 | |
| 50 | ArrayList<ModelLocation> modelLocations = new ArrayList<ModelLocation>(); |
| 51 | ResourceSetPartition partition = blackboard.getPartition(modelPartitionID); |
| 52 | partition.resolveAllProxies(); |
| 53 | for (Resource r : partition.getResourceSet().getResources()) { |
| 54 | ModelLocation location = new ModelLocation(modelPartitionID, r.getURI()); |
| 55 | modelLocations.add(location); |
| 56 | } |
| 57 | |
| 58 | return modelLocations.toArray(new ModelLocation[]{}); |
| 59 | } |
| 60 | |
| 61 | public static URI getTraceFileURI (String projectID) { |
| 62 | String tracesFolder = PrepareTransformationsJob.getTracesFolder( |
| 63 | projectID).getFullPath().toString(); |
| 64 | URI tracesURI = URI.createPlatformResourceURI(tracesFolder, false); |
| 65 | return tracesURI; |
| 66 | } |
| 67 | |
| 68 | public static URI getHotTraceFileURI (String projectID) { |
| 69 | String tracesFolder = PrepareTransformationsJob.getHOTTracesFolder( |
| 70 | projectID).getFullPath().toString(); |
| 71 | URI tracesURI = URI.createPlatformResourceURI(tracesFolder, false); |
| 72 | return tracesURI; |
| 73 | } |
| 74 | |
| 75 | public static ModelLocation createResourceToInputPartition(MDSDBlackboard blackboard, String modelPartitionID, URI modelURI) { |
| 76 | ResourceSetPartition resourceSetPartition = blackboard.getPartition(modelPartitionID); |
| 77 | Resource r = resourceSetPartition.getResourceSet().createResource(modelURI); |
| 78 | return new ModelLocation(modelPartitionID, r.getURI()); |
| 79 | } |
| 80 | |
| 81 | public static ModelLocation loadResourceToInputPartition(MDSDBlackboard blackboard, String modelPartitionID, URI modelURI) { |
| 82 | ResourceSetPartition resourceSetPartition = blackboard.getPartition(modelPartitionID); |
| 83 | Resource r = resourceSetPartition.loadModel(modelURI); |
| 84 | return new ModelLocation(modelPartitionID, r.getURI()); |
| 85 | } |
| 86 | |
| 87 | public static String createTransformationID(CompletionConfiguration completionConfiguration, Transformation transformation) { |
| 88 | |
| 89 | URI qvtFileURI = URI.createURI(transformation.getQVTFileURI()); |
| 90 | int index = qvtFileURI.lastSegment().indexOf('.'); |
| 91 | |
| 92 | return qvtFileURI.lastSegment().substring(0, index) + "_" + |
| 93 | completionConfiguration.getTransformations().indexOf(transformation); |
| 94 | } |
| 95 | |
| 96 | public static QVTRScript createQVTScript(String script, Collection<Object> metaModels, String transformation, String direction) { |
| 97 | QVTRScript qvtrScript = new QVTRScript(); |
| 98 | qvtrScript.setQVTFile(script); |
| 99 | qvtrScript.setMetaModels(metaModels); |
| 100 | |
| 101 | QVTRScriptInfo scriptinfo = new QVTRScriptInfo(qvtrScript); |
| 102 | if(transformation == null) |
| 103 | transformation = scriptinfo.getTransformations()[0]; |
| 104 | if(direction == null) |
| 105 | direction = scriptinfo.getDirections(transformation)[1]; |
| 106 | |
| 107 | qvtrScript.setTransformationName(transformation); |
| 108 | qvtrScript.setTransformationDirection(direction); |
| 109 | |
| 110 | return qvtrScript; |
| 111 | } |
| 112 | } |