| 1 | package de.uka.ipd.sdq.cip.workflow.jobs; |
| 2 | |
| 3 | import java.io.IOException; |
| 4 | import java.util.HashMap; |
| 5 | |
| 6 | import org.apache.log4j.Logger; |
| 7 | import org.eclipse.core.resources.IFolder; |
| 8 | import org.eclipse.core.runtime.IProgressMonitor; |
| 9 | import org.eclipse.emf.common.util.URI; |
| 10 | import org.eclipse.emf.ecore.resource.Resource; |
| 11 | import org.eclipse.emf.ecore.resource.ResourceSet; |
| 12 | |
| 13 | import de.uka.ipd.sdq.cip.configuration.CompletionConfiguration; |
| 14 | import de.uka.ipd.sdq.cip.configuration.Transformation; |
| 15 | import de.uka.ipd.sdq.workflow.IBlackboardInteractingJob; |
| 16 | import de.uka.ipd.sdq.workflow.IJob; |
| 17 | import de.uka.ipd.sdq.workflow.exceptions.JobFailedException; |
| 18 | import de.uka.ipd.sdq.workflow.exceptions.RollbackFailedException; |
| 19 | import de.uka.ipd.sdq.workflow.exceptions.UserCanceledException; |
| 20 | import de.uka.ipd.sdq.workflow.mdsd.blackboard.MDSDBlackboard; |
| 21 | import de.uka.ipd.sdq.workflow.mdsd.blackboard.ResourceSetPartition; |
| 22 | |
| 23 | /** |
| 24 | * Job to create a working copy of the models to simulate. |
| 25 | * This ensures that any downstream job changing the models |
| 26 | * does not modify the original models. |
| 27 | * |
| 28 | * Prerequisite of this job: |
| 29 | * This job copies the models to the configured project created in |
| 30 | * the workflow. It has to exist to be able to store the model copy |
| 31 | * into it. |
| 32 | * |
| 33 | * @author Benjamin Klatt |
| 34 | * |
| 35 | */ |
| 36 | public class CreateWorkingCopyOfModelsJob implements IJob, |
| 37 | IBlackboardInteractingJob<MDSDBlackboard> { |
| 38 | |
| 39 | /** The logger for this class */ |
| 40 | private static final Logger logger = Logger.getLogger(CreateWorkingCopyOfModelsJob.class); |
| 41 | |
| 42 | /** The blackboard to interact with */ |
| 43 | private MDSDBlackboard blackboard = null; |
| 44 | |
| 45 | /** The work flow configuration to get the required information from */ |
| 46 | //private SimuComWorkflowConfiguration configuration; |
| 47 | |
| 48 | private String projectID; |
| 49 | |
| 50 | private String modelPartitionID; |
| 51 | |
| 52 | private String transformationID; |
| 53 | |
| 54 | /** |
| 55 | * Constructor requiring the necessary configuration object. |
| 56 | * |
| 57 | * @param configuration The configuration for this job. |
| 58 | */ |
| 59 | public CreateWorkingCopyOfModelsJob(CompletionConfiguration completionConfiguration, |
| 60 | Transformation transformation) { |
| 61 | this.projectID = completionConfiguration.getProjectID(); |
| 62 | this.modelPartitionID = completionConfiguration.getInputPartitionName(); |
| 63 | |
| 64 | URI qvtFileURI = URI.createURI(transformation.getQVTFileURI()); |
| 65 | this.transformationID = qvtFileURI.lastSegment().replace(qvtFileURI.fileExtension(), "") + |
| 66 | completionConfiguration.getTransformations().indexOf(transformation); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Execute this job and create the model copy. |
| 71 | */ |
| 72 | @Override |
| 73 | public void execute(IProgressMonitor monitor) throws JobFailedException, |
| 74 | UserCanceledException { |
| 75 | |
| 76 | IFolder modelFolder = PrepareTransformationsJob.createFolder( |
| 77 | PrepareTransformationsJob.getModelFolder(projectID).getFolder( |
| 78 | transformationID), |
| 79 | logger); |
| 80 | |
| 81 | String modelBasePath = "file:/"+modelFolder.getLocation().toOSString(); |
| 82 | |
| 83 | // access the resources |
| 84 | ResourceSetPartition partition = (ResourceSetPartition) this.blackboard.getPartition(modelPartitionID); |
| 85 | ResourceSet resourceSet = partition.getResourceSet(); |
| 86 | for (Resource ressource : resourceSet.getResources()) { |
| 87 | |
| 88 | // we only need to copy the file models |
| 89 | if (ressource.getURI().isFile()) { |
| 90 | |
| 91 | URI uri = ressource.getURI(); |
| 92 | String relativePath = uri.lastSegment(); |
| 93 | URI newURI = URI.createURI(modelBasePath +"/"+ relativePath); |
| 94 | ressource.setURI(newURI); |
| 95 | |
| 96 | try { |
| 97 | ressource.save(new HashMap()); |
| 98 | partition.setContents(ressource.getURI(), ressource.getContents()); |
| 99 | } catch (IOException e) { |
| 100 | logger.error("Unable to store resource "+ressource.getURI(),e); |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | try { |
| 105 | partition.storeAllResources(); |
| 106 | } catch (IOException e) { |
| 107 | logger.error("unable to store all resources",e); |
| 108 | throw new JobFailedException("Unable to store all Resources",e); |
| 109 | } |
| 110 | // partition.resolveAllProxies(); |
| 111 | // this.blackboard.removePartition(LoadPCMModelsIntoBlackboardJob.PCM_MODELS_PARTITION_ID); |
| 112 | // this.blackboard.addPartition(LoadPCMModelsIntoBlackboardJob.PCM_MODELS_PARTITION_ID, partition); |
| 113 | } |
| 114 | |
| 115 | @Override |
| 116 | public String getName() { |
| 117 | return "Create working copy of models"; |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public void rollback(IProgressMonitor monitor) |
| 122 | throws RollbackFailedException { |
| 123 | logger.warn("Unable to perform rollback for this job"); |
| 124 | |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public void setBlackboard(MDSDBlackboard blackboard) { |
| 129 | this.blackboard = blackboard; |
| 130 | } |
| 131 | |
| 132 | } |