1 | package de.uka.ipd.sdq.pcmsolver.models; |
2 | |
3 | import java.util.ArrayList; |
4 | import java.util.HashSet; |
5 | import java.util.List; |
6 | import java.util.Properties; |
7 | import java.util.Set; |
8 | |
9 | import org.eclipse.core.runtime.CoreException; |
10 | import org.eclipse.debug.core.ILaunchConfiguration; |
11 | import org.eclipse.emf.ecore.EObject; |
12 | |
13 | import de.uka.ipd.sdq.context.aggregatedUsageContext.AggregatedUsageContextFactory; |
14 | import de.uka.ipd.sdq.context.aggregatedUsageContext.ComputedAggregatedUsage; |
15 | import de.uka.ipd.sdq.context.computed_allocation.ComputedAllocation; |
16 | import de.uka.ipd.sdq.context.computed_allocation.ComputedAllocationContext; |
17 | import de.uka.ipd.sdq.context.computed_allocation.ComputedAllocationFactory; |
18 | import de.uka.ipd.sdq.context.computed_usage.ComputedUsage; |
19 | import de.uka.ipd.sdq.context.computed_usage.ComputedUsageFactory; |
20 | import de.uka.ipd.sdq.pcm.allocation.Allocation; |
21 | import de.uka.ipd.sdq.pcm.repository.Repository; |
22 | import de.uka.ipd.sdq.pcm.resourceenvironment.ResourceContainer; |
23 | import de.uka.ipd.sdq.pcm.resourceenvironment.ResourceEnvironment; |
24 | import de.uka.ipd.sdq.pcm.resourcetype.ResourceRepository; |
25 | import de.uka.ipd.sdq.pcm.usagemodel.UsageModel; |
26 | import de.uka.ipd.sdq.pcmsolver.transformations.EMFHelper; |
27 | import de.uka.ipd.sdq.workflow.pcm.ConstantsContainer; |
28 | import de.uka.ipd.sdq.workflow.pcm.blackboard.PCMResourceSetPartition; |
29 | import de.uka.ipd.sdq.workflow.pcm.configurations.AbstractPCMWorkflowRunConfiguration; |
30 | |
31 | |
32 | public class PCMInstance { |
33 | |
34 | private ComputedUsage computedUsage = ComputedUsageFactory.eINSTANCE.createComputedUsage();; |
35 | |
36 | private ComputedAllocation actualAllocation = ComputedAllocationFactory.eINSTANCE.createComputedAllocation(); |
37 | |
38 | private ComputedAggregatedUsage computedAggregatedUsage = AggregatedUsageContextFactory.eINSTANCE.createComputedAggregatedUsage();; |
39 | |
40 | private String storagePath; |
41 | |
42 | private PCMResourceSetPartition resourceSetPartition; |
43 | |
44 | public PCMInstance(Properties config) { |
45 | this.storagePath = config.getProperty("Storage_Path"); |
46 | ArrayList <String> fileList = new ArrayList <String>(); |
47 | fileList.add(config.getProperty("Filename_UsageModel")); |
48 | fileList.add(config.getProperty("Filename_AllocationModel")); |
49 | createResourceSetPartition(fileList); |
50 | // loadFromFiles(config); |
51 | } |
52 | |
53 | @Deprecated |
54 | public PCMInstance(ILaunchConfiguration configuration) { |
55 | try { |
56 | this.storagePath = configuration.getAttribute("outputPath", "."); |
57 | |
58 | ArrayList <String> fileList = new ArrayList <String>(); |
59 | fileList.add(configuration.getAttribute( |
60 | ConstantsContainer.USAGE_FILE, "")); |
61 | fileList.add(configuration.getAttribute( |
62 | ConstantsContainer.ALLOCATION_FILE, "")); |
63 | createResourceSetPartition(fileList); |
64 | |
65 | |
66 | // String filename = configuration.getAttribute( |
67 | // ConstantsContainer.ALLOCATION_FILE, ""); |
68 | // this.allocation = ((Allocation) loadFromXMIFile(filename)); |
69 | // |
70 | // filename = configuration.getAttribute( |
71 | // ConstantsContainer.USAGE_FILE, ""); |
72 | // this.usageModel = ((UsageModel) loadFromXMIFile(filename)); |
73 | |
74 | // filename = configuration.getAttribute( |
75 | // ConstantsContainer.REPOSITORY_FILE, ""); |
76 | // this.repository = ((Repository) loadFromXMIFile(filename)); |
77 | // |
78 | // filename = configuration.getAttribute( |
79 | // ConstantsContainer.RESOURCEENVIRONMENT_FILE, ""); |
80 | // this.resourceEnvironment = ((ResourceEnvironment) loadFromXMIFile(filename)); |
81 | // |
82 | // filename = configuration.getAttribute( |
83 | // ConstantsContainer.RESOURCETYPEREPOSITORY_FILE, ""); |
84 | // this.resourceRepository = ((ResourceRepository) loadFromXMIFile(filename)); |
85 | // |
86 | // filename = configuration.getAttribute( |
87 | // ConstantsContainer.SYSTEM_FILE, ""); |
88 | // this.system = ((de.uka.ipd.sdq.pcm.system.System) loadFromXMIFile(filename)); |
89 | |
90 | |
91 | |
92 | } catch (CoreException e) { |
93 | e.printStackTrace(); |
94 | } |
95 | } |
96 | |
97 | //BRG |
98 | public PCMInstance(PCMResourceSetPartition pcmModel) { |
99 | this.storagePath = "."; |
100 | this.resourceSetPartition = pcmModel; |
101 | |
102 | |
103 | } |
104 | |
105 | //BRG |
106 | |
107 | /** |
108 | * Loads PCM Model contents. |
109 | * |
110 | * @param modelFiles List of Strings containing Usage and Allocation FileNames |
111 | */ |
112 | @SuppressWarnings("deprecation") |
113 | private void createResourceSetPartition(List<String> modelFiles) { |
114 | |
115 | resourceSetPartition = new PCMResourceSetPartition(); |
116 | resourceSetPartition.initialiseResourceSetEPackages(AbstractPCMWorkflowRunConfiguration.PCM_EPACKAGES); |
117 | for (String modelFile : modelFiles) { |
118 | resourceSetPartition.loadModel(modelFile); |
119 | } |
120 | resourceSetPartition.resolveAllProxies(); |
121 | |
122 | } |
123 | |
124 | public Allocation getAllocation() { |
125 | return resourceSetPartition.getAllocation(); |
126 | } |
127 | |
128 | //BRG |
129 | // public void setAllocation(Allocation allocation) { |
130 | // this.allocation = allocation; |
131 | // } |
132 | |
133 | public List<Repository> getRepositories() { |
134 | return resourceSetPartition.getRepositories(); |
135 | } |
136 | |
137 | // public void setRepository(Repository repository) { |
138 | // this.repository = repository; |
139 | // } |
140 | |
141 | public ResourceEnvironment getResourceEnvironment() { |
142 | return resourceSetPartition.getResourceEnvironment(); |
143 | } |
144 | |
145 | // public void setResourceEnvironment(ResourceEnvironment resourceEnvironment) { |
146 | // this.resourceEnvironment = resourceEnvironment; |
147 | // } |
148 | |
149 | public ResourceRepository getResourceRepository() { |
150 | return resourceSetPartition.getResourceTypeRepository(); |
151 | } |
152 | |
153 | // public void setResourceRepository(ResourceRepository resourceRepository) { |
154 | // this.resourceRepository = resourceRepository; |
155 | // } |
156 | |
157 | public de.uka.ipd.sdq.pcm.system.System getSystem() { |
158 | return resourceSetPartition.getSystem(); |
159 | } |
160 | |
161 | // public void setSystem(de.uka.ipd.sdq.pcm.system.System system) { |
162 | // this.system = system; |
163 | // } |
164 | |
165 | public UsageModel getUsageModel() { |
166 | return resourceSetPartition.getUsageModel(); |
167 | } |
168 | |
169 | // public void setUsageModel(UsageModel usageModel) { |
170 | // this.usageModel = usageModel; |
171 | // } |
172 | |
173 | public ComputedAllocation getComputedAllocation() { |
174 | return actualAllocation; |
175 | } |
176 | |
177 | // public void setComputedAllocation(ComputedAllocation actualAllocation) { |
178 | // this.actualAllocation = actualAllocation; |
179 | // } |
180 | |
181 | public ComputedUsage getComputedUsage() { |
182 | return computedUsage; |
183 | } |
184 | |
185 | // public void setComputedUsage(ComputedUsage usage) { |
186 | // this.usage = usage; |
187 | // } |
188 | |
189 | //BRG |
190 | // private void loadFromFiles(Properties config) { |
191 | // String filename = config.getProperty("Filename_Allocation"); |
192 | // this.allocation = ((Allocation) loadFromXMIFile(filename)); |
193 | // filename = config.getProperty("Filename_Repository"); |
194 | // this.repository = ((Repository) loadFromXMIFile(filename)); |
195 | // filename = config.getProperty("Filename_ResourceEnvironment"); |
196 | // this.resourceEnvironment = ((ResourceEnvironment) loadFromXMIFile(filename)); |
197 | // filename = config.getProperty("Filename_ResourceType"); |
198 | // this.resourceRepository = ((ResourceRepository) loadFromXMIFile(filename)); |
199 | // filename = config.getProperty("Filename_System"); |
200 | // this.system = ((de.uka.ipd.sdq.pcm.system.System) loadFromXMIFile(filename)); |
201 | // filename = config.getProperty("Filename_UsageModel"); |
202 | // this.usageModel = ((UsageModel) loadFromXMIFile(filename)); |
203 | // } |
204 | |
205 | public void saveToFiles(String fileNamePrefix) { |
206 | fileNamePrefix = storagePath + "\\" + fileNamePrefix; |
207 | saveToXMIFile(getAllocation(), fileNamePrefix + ".allocation"); |
208 | List<Repository> repositories = getRepositories(); |
209 | for (Repository repository : repositories) { |
210 | saveToXMIFile(repository, fileNamePrefix + ".repository"); |
211 | } |
212 | saveToXMIFile(getResourceEnvironment(), fileNamePrefix |
213 | + ".resourceenvironment"); |
214 | saveToXMIFile(getResourceRepository(), fileNamePrefix + ".resourcetype"); |
215 | saveToXMIFile(getSystem(), fileNamePrefix + ".system"); |
216 | saveToXMIFile(getUsageModel(), fileNamePrefix + ".usagemodel"); |
217 | |
218 | saveToXMIFile(computedUsage, fileNamePrefix + ".usage"); |
219 | saveToXMIFile(actualAllocation, fileNamePrefix + ".actualallocation"); |
220 | |
221 | } |
222 | |
223 | // public void saveComputedContextToFiles(String fileNamePrefix) { |
224 | // |
225 | // saveToXMIFile(computedUsage, fileNamePrefix + ".usage"); |
226 | // saveToXMIFile(actualAllocation, fileNamePrefix + ".actualallocation"); |
227 | // |
228 | // } |
229 | |
230 | //BRG |
231 | // private EObject loadFromXMIFile(String fileName) { |
232 | // // Create a resource set to hold the resources. |
233 | // ResourceSet resourceSet = new ResourceSetImpl(); |
234 | // |
235 | // // Register the appropriate resource factory to handle all file |
236 | // // extentions. |
237 | // resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap() |
238 | // .put(Resource.Factory.Registry.DEFAULT_EXTENSION, |
239 | // new XMIResourceFactoryImpl()); |
240 | // |
241 | // // Register the package to ensure it is available during loading. |
242 | // registerPackages(resourceSet); |
243 | // |
244 | // // Construct the URI for the instance file. |
245 | // // The argument is treated as a file path only if it denotes an existing |
246 | // // file. Otherwise, it's directly treated as a URL. |
247 | // File file = new File(fileName); |
248 | // URI uri = file.isFile() ? URI.createFileURI(file.getAbsolutePath()) |
249 | // : URI.createURI(fileName); |
250 | // |
251 | // Resource resource = null; |
252 | // // Demand load resource for this file. |
253 | // try { |
254 | // resource = resourceSet.getResource(uri, true); |
255 | // } catch (Exception e) { |
256 | // logger.error(e.getMessage()); |
257 | // return null; |
258 | // } |
259 | // |
260 | // // logger.debug("Loaded " + uri); |
261 | // |
262 | // // if (!fileName.endsWith(".assembly") && |
263 | // // !fileName.endsWith("repository")) { |
264 | // // // Validate the contents of the loaded resource. |
265 | // // for (Iterator j = resource.getContents().iterator(); j.hasNext();) { |
266 | // // EObject eObject = (EObject) j.next(); |
267 | // // Diagnostic diagnostic = Diagnostician.INSTANCE |
268 | // // .validate(eObject); |
269 | // // if (diagnostic.getSeverity() != Diagnostic.OK) { |
270 | // // System.out.println(); |
271 | // // System.out.println(diagnostic.getMessage()); |
272 | // // // printDiagnostic(diagnostic, ""); |
273 | // // |
274 | // // } |
275 | // // } |
276 | // // } |
277 | // EObject eObject = (EObject) resource.getContents().iterator().next(); |
278 | // return EcoreUtil.getRootContainer(eObject); |
279 | // } |
280 | |
281 | public void saveToXMIFile(EObject modelToSave, String fileName) { |
282 | EMFHelper.saveToXMIFile(modelToSave, fileName); |
283 | } |
284 | |
285 | //BRG |
286 | // private void registerPackages(ResourceSet resourceSet) { |
287 | // |
288 | // resourceSet.getPackageRegistry().put(AllocationPackage.eNS_URI, |
289 | // AllocationPackage.eINSTANCE); |
290 | // resourceSet.getPackageRegistry().put(ParameterPackage.eNS_URI, |
291 | // ParameterPackage.eINSTANCE); |
292 | // resourceSet.getPackageRegistry().put( |
293 | // ResourceenvironmentPackage.eNS_URI, |
294 | // ResourceenvironmentPackage.eINSTANCE); |
295 | // resourceSet.getPackageRegistry().put(ResourcetypePackage.eNS_URI, |
296 | // ResourcetypePackage.eINSTANCE); |
297 | // resourceSet.getPackageRegistry().put(RepositoryPackage.eNS_URI, |
298 | // RepositoryPackage.eINSTANCE); |
299 | // resourceSet.getPackageRegistry().put(SeffPackage.eNS_URI, |
300 | // SeffPackage.eINSTANCE); |
301 | // resourceSet.getPackageRegistry().put(SystemPackage.eNS_URI, |
302 | // SystemPackage.eINSTANCE); |
303 | // resourceSet.getPackageRegistry().put(UsagemodelPackage.eNS_URI, |
304 | // UsagemodelPackage.eINSTANCE); |
305 | // |
306 | // } |
307 | |
308 | public boolean isValid(){ |
309 | if (getAllocation() == null || |
310 | getRepositories().size() == 0 || |
311 | getResourceEnvironment() == null || |
312 | getResourceRepository() == null || |
313 | getSystem() == null || |
314 | computedUsage == null){ |
315 | return false; |
316 | } else return true; |
317 | |
318 | } |
319 | |
320 | /** |
321 | * Get the used ResourceContainers. These are all resource containers that are references from ComputedAllocationContexts inside the ComputedAllocation. |
322 | * |
323 | * If the ComputedAllocation has not yet been calculated, this method returns all resource containers. |
324 | * |
325 | * @return A set of all used resource containers. Each container is just contained once. |
326 | */ |
327 | public Set<ResourceContainer> getUsedResourceContainer() { |
328 | //The maximal size of this list is the number of resource containers in the resource environment. |
329 | Set<ResourceContainer> usedRCList = new HashSet<ResourceContainer>(this.getResourceEnvironment().getResourceContainer_ResourceEnvironment().size()); |
330 | |
331 | //Check whether the ComputedAllocation has been determined already. If not, return all ResourceContainers. |
332 | if (this.actualAllocation != null && this.actualAllocation.getComputedAllocationContexts_ComputedAllocation().size() != 0){ |
333 | List<ComputedAllocationContext> computedAllocationContextList = this.actualAllocation.getComputedAllocationContexts_ComputedAllocation(); |
334 | for (ComputedAllocationContext cac : computedAllocationContextList) { |
335 | ResourceContainer rc = cac.getAllocationContext_ComputedAllocationContext().getResourceContainer_AllocationContext(); |
336 | if (!usedRCList.contains(rc)){ |
337 | usedRCList.add(rc); |
338 | } |
339 | |
340 | } |
341 | } else { |
342 | //return all resource containers |
343 | usedRCList.addAll(this.getResourceEnvironment().getResourceContainer_ResourceEnvironment()); |
344 | } |
345 | return usedRCList; |
346 | } |
347 | |
348 | /** |
349 | * @return the computedAggregatedUsage |
350 | */ |
351 | public ComputedAggregatedUsage getComputedAggregatedUsage() { |
352 | return computedAggregatedUsage; |
353 | } |
354 | |
355 | public void setComputedContexts(ComputedAllocation computedAllocation, |
356 | ComputedUsage computedUsage, |
357 | ComputedAggregatedUsage aggregatedUsage) { |
358 | this.computedUsage = computedUsage; |
359 | this.actualAllocation = computedAllocation; |
360 | this.computedAggregatedUsage = aggregatedUsage; |
361 | |
362 | } |
363 | |
364 | public void resetComputedContexts(){ |
365 | this.computedUsage = ComputedUsageFactory.eINSTANCE.createComputedUsage();; |
366 | this.actualAllocation = ComputedAllocationFactory.eINSTANCE.createComputedAllocation(); |
367 | this.computedAggregatedUsage = AggregatedUsageContextFactory.eINSTANCE.createComputedAggregatedUsage();; |
368 | } |
369 | } |