1 | package de.uka.ipd.sdq.pcm.transformations; |
2 | |
3 | import org.apache.log4j.Logger; |
4 | import org.eclipse.core.resources.IFolder; |
5 | import org.eclipse.core.resources.IProject; |
6 | import org.eclipse.core.runtime.IProgressMonitor; |
7 | import org.eclipse.emf.common.util.URI; |
8 | import org.eclipse.emf.ecore.resource.Resource; |
9 | |
10 | import de.uka.ipd.sdq.codegen.simucontroller.SimuControllerPlugin; |
11 | import de.uka.ipd.sdq.codegen.simucontroller.runconfig.AbstractSimulationWorkflowConfiguration; |
12 | import de.uka.ipd.sdq.workflow.pcm.blackboard.PCMResourceSetPartition; |
13 | import de.uka.ipd.sdq.workflow.pcm.jobs.CreatePluginProjectJob; |
14 | import de.uka.ipd.sdq.workflow.pcm.jobs.LoadMiddlewareConfigurationIntoBlackboardJob; |
15 | import de.uka.ipd.sdq.workflow.pcm.jobs.LoadPCMModelsIntoBlackboardJob; |
16 | import de.uka.ipd.sdq.featureconfig.Configuration; |
17 | import de.uka.ipd.sdq.pcm.allocation.AllocationContext; |
18 | import de.uka.ipd.sdq.pcm.allocation.AllocationFactory; |
19 | import de.uka.ipd.sdq.pcm.core.composition.AssemblyConnector; |
20 | import de.uka.ipd.sdq.pcm.core.composition.AssemblyContext; |
21 | import de.uka.ipd.sdq.pcm.core.composition.CompositionFactory; |
22 | import de.uka.ipd.sdq.pcm.core.composition.CompositionPackage; |
23 | import de.uka.ipd.sdq.pcm.repository.Repository; |
24 | import de.uka.ipd.sdq.pcm.repository.RepositoryFactory; |
25 | import de.uka.ipd.sdq.pcm.resourceenvironment.ResourceContainer; |
26 | import de.uka.ipd.sdq.pcm.resourceenvironment.ResourceenvironmentPackage; |
27 | import de.uka.ipd.sdq.pcm.transformations.builder.connectors.ConnectorReplacingBuilder; |
28 | import de.uka.ipd.sdq.pcm.transformations.builder.util.PCMAndCompletionModelHolder; |
29 | import de.uka.ipd.sdq.workflow.IBlackboardInteractingJob; |
30 | import de.uka.ipd.sdq.workflow.exceptions.JobFailedException; |
31 | import de.uka.ipd.sdq.workflow.exceptions.RollbackFailedException; |
32 | import de.uka.ipd.sdq.workflow.exceptions.UserCanceledException; |
33 | import de.uka.ipd.sdq.workflow.mdsd.blackboard.MDSDBlackboard; |
34 | import de.uka.ipd.sdq.workflow.mdsd.blackboard.ResourceSetPartition; |
35 | |
36 | public class ApplyConnectorCompletionsJob |
37 | implements IBlackboardInteractingJob<MDSDBlackboard> { |
38 | |
39 | private static Logger logger = Logger.getLogger(ApplyConnectorCompletionsJob.class); |
40 | |
41 | public static final String COMPLETION_REPOSITORY_PARTITION = "de.uka.ipd.sdq.pcm.completionRepositoryPartition"; |
42 | |
43 | private MDSDBlackboard blackboard; |
44 | private AbstractSimulationWorkflowConfiguration configuration; |
45 | |
46 | public ApplyConnectorCompletionsJob(AbstractSimulationWorkflowConfiguration configuration) { |
47 | super(); |
48 | |
49 | this.configuration = configuration; |
50 | } |
51 | |
52 | public void execute(IProgressMonitor monitor) throws JobFailedException, UserCanceledException { |
53 | |
54 | PCMResourceSetPartition pcmModels = (PCMResourceSetPartition) blackboard.getPartition(LoadPCMModelsIntoBlackboardJob.PCM_MODELS_PARTITION_ID); |
55 | ResourceSetPartition middlewareRepository = blackboard.getPartition(LoadMiddlewareConfigurationIntoBlackboardJob.MIDDLEWARE_PARTITION_ID); |
56 | |
57 | logger.info("Create completion repository..."); |
58 | ResourceSetPartition completionRepositoryPartition = new ResourceSetPartition(); |
59 | Repository completionRepository = RepositoryFactory.eINSTANCE.createRepository(); |
60 | completionRepository.setEntityName("CompletionsRepository"); |
61 | String tempDir = SimuControllerPlugin.getDefault().getStateLocation().append("temp").toOSString(); |
62 | |
63 | // IProject project = CreatePluginProjectJob.getProject(configuration.getStoragePluginID()); |
64 | // IFolder modelFolder = project.getFolder("model"); |
65 | // URI u = pcmModels.getAllocation().eResource().getURI().trimSegments(1); |
66 | // u = u.appendSegment("completions.repository"); |
67 | // |
68 | // |
69 | // String modelBasePath = modelFolder.getLocation().toOSString(); |
70 | // String tempDir = modelBasePath; |
71 | |
72 | // u |
73 | Resource r = completionRepositoryPartition.getResourceSet().createResource(URI.createFileURI(tempDir)); |
74 | r.getContents().add(completionRepository); |
75 | this.blackboard.addPartition(COMPLETION_REPOSITORY_PARTITION, completionRepositoryPartition); |
76 | |
77 | final PCMAndCompletionModelHolder models = new PCMAndCompletionModelHolder( |
78 | pcmModels.getResourceTypeRepository(), |
79 | null, |
80 | pcmModels.getSystem(), |
81 | pcmModels.getAllocation(), |
82 | null, |
83 | completionRepository, |
84 | (Repository)middlewareRepository.getResourceSet().getResources().get(0).getContents().get(0) |
85 | ); |
86 | |
87 | final Configuration featureConfiguration = pcmModels.getFeatureConfig(); |
88 | |
89 | new AllInstancesTransformer<ResourceContainer>( |
90 | ResourceenvironmentPackage.eINSTANCE.getResourceContainer(), |
91 | models.getAllocation().getTargetResourceEnvironment_Allocation()) { |
92 | |
93 | @Override |
94 | protected void transform(ResourceContainer object) { |
95 | addMiddleware(models, object); |
96 | } |
97 | |
98 | }.transform(); |
99 | |
100 | logger.info("Replace connectors with completions..."); |
101 | new AllInstancesTransformer<AssemblyConnector>( |
102 | CompositionPackage.eINSTANCE.getAssemblyConnector(), |
103 | models.getSystem()) { |
104 | |
105 | @Override |
106 | protected void transform(AssemblyConnector connector) { |
107 | if ( configuration.getSimulateLinkingResources() ) { |
108 | ConnectorReplacingBuilder replacer = new ConnectorReplacingBuilder(models,connector,featureConfiguration.getDefaultConfig()); |
109 | replacer.build(); |
110 | } |
111 | } |
112 | |
113 | }.transform(); |
114 | } |
115 | |
116 | /** |
117 | * Creates a middleware component instance and allocates it to the given resource container |
118 | */ |
119 | private void addMiddleware(PCMAndCompletionModelHolder models, ResourceContainer resContainer) { |
120 | AssemblyContext ctx = CompositionFactory.eINSTANCE.createAssemblyContext(); |
121 | ctx.setEntityName("AssCtx Middleware "+resContainer.getEntityName()); |
122 | ctx.setEncapsulatedComponent__AssemblyContext(models.getMiddlewareRepository().getComponents__Repository().get(0)); // TODO: Parameterise me! |
123 | models.getSystem().getAssemblyContexts__ComposedStructure().add(ctx); |
124 | |
125 | models.getSystem().getAssemblyContexts__ComposedStructure().add(ctx); |
126 | AllocationContext allocCtx = AllocationFactory.eINSTANCE.createAllocationContext(); |
127 | allocCtx.setEntityName("AllocCtx Middleware "+resContainer.getEntityName()); |
128 | allocCtx.setAssemblyContext_AllocationContext(ctx); |
129 | allocCtx.setResourceContainer_AllocationContext(resContainer); |
130 | models.getAllocation().getAllocationContexts_Allocation().add(allocCtx); |
131 | |
132 | logger.info("Added middleware component >"+ctx.getEncapsulatedComponent__AssemblyContext().getEntityName()+ |
133 | "< to resource container >"+resContainer.getEntityName()+"<"); |
134 | } |
135 | |
136 | public void setBlackboard(MDSDBlackboard blackboard) { |
137 | this.blackboard = blackboard; |
138 | } |
139 | |
140 | public String getName() { |
141 | return "Add connector completions job"; |
142 | } |
143 | |
144 | public void rollback(IProgressMonitor monitor) |
145 | throws RollbackFailedException { |
146 | // Nothing to do here |
147 | } |
148 | } |