1 | package de.uka.ipd.sdq.spa.basicsolver.operations; |
2 | |
3 | import java.util.ArrayList; |
4 | import java.util.Hashtable; |
5 | import java.util.List; |
6 | import java.util.Set; |
7 | |
8 | import de.uka.ipd.sdq.probfunction.math.IProbabilityMassFunction; |
9 | import de.uka.ipd.sdq.probfunction.math.ManagedPDF; |
10 | import de.uka.ipd.sdq.probfunction.math.exception.ConfigurationNotSetException; |
11 | import de.uka.ipd.sdq.probfunction.math.exception.ProbabilityFunctionException; |
12 | import de.uka.ipd.sdq.spa.basicsolver.exceptions.BothNullExpeception; |
13 | import de.uka.ipd.sdq.spa.resourcemodel.ActiveResource; |
14 | import de.uka.ipd.sdq.spa.resourcemodel.Resource; |
15 | import de.uka.ipd.sdq.spa.resourcemodel.ResourceUsage; |
16 | |
17 | public class RUPerformanceOps { |
18 | |
19 | private PDFPerformanceOps pdfPerformanceOps; |
20 | |
21 | public RUPerformanceOps(PDFPerformanceOps pdfPerformanceOps) { |
22 | super(); |
23 | this.pdfPerformanceOps = pdfPerformanceOps; |
24 | } |
25 | |
26 | public RUPerformanceOps() { |
27 | pdfPerformanceOps = new PDFPerformanceOps(); |
28 | } |
29 | |
30 | public Hashtable<ActiveResource, ManagedPDF> computeIteration( |
31 | Hashtable<ActiveResource, ManagedPDF> innerRUs, |
32 | IProbabilityMassFunction iterations) throws ConfigurationNotSetException { |
33 | Hashtable<ActiveResource, ManagedPDF> resultTable = new Hashtable<ActiveResource, ManagedPDF>(); |
34 | |
35 | try { |
36 | for (ActiveResource resource : innerRUs.keySet()) { |
37 | ManagedPDF ru = pdfPerformanceOps.computeIteration(innerRUs.get(resource), |
38 | iterations); |
39 | resultTable.put(resource, ru); |
40 | } |
41 | } catch (ProbabilityFunctionException e) { |
42 | e.printStackTrace(); |
43 | System.exit(1); |
44 | } |
45 | |
46 | return resultTable; |
47 | |
48 | } |
49 | |
50 | public Hashtable<Resource, ManagedPDF> getResourceUsageTimes( |
51 | List<ResourceUsage> resourceUsageList) { |
52 | |
53 | Hashtable<Resource, ManagedPDF> resultTable = new Hashtable<Resource, ManagedPDF>(); |
54 | for (ResourceUsage resourceUsage : resourceUsageList) { |
55 | Resource resource = resourceUsage.getResource(); |
56 | ManagedPDF managedPDF = new ManagedPDF( |
57 | resourceUsage.getUsageTime(), true); |
58 | resultTable.put(resource, managedPDF); |
59 | } |
60 | return resultTable; |
61 | } |
62 | |
63 | public Hashtable<ActiveResource, ManagedPDF> computeAlternative( |
64 | Hashtable<ActiveResource, ManagedPDF> leftRUs, double leftProb, |
65 | Hashtable<ActiveResource, ManagedPDF> rightRUs, double rightProb) { |
66 | |
67 | return performOperation(BinaryOperation.ALTERNATIVE, leftRUs, leftProb, |
68 | rightRUs, rightProb); |
69 | } |
70 | |
71 | public Hashtable<ActiveResource, ManagedPDF> computeParallel( |
72 | Hashtable<ActiveResource, ManagedPDF> leftRUs, |
73 | Hashtable<ActiveResource, ManagedPDF> rightRUs) { |
74 | |
75 | return performOperation(BinaryOperation.PARALLEL, leftRUs, 0, rightRUs, |
76 | 0); |
77 | } |
78 | |
79 | public Hashtable<ActiveResource, ManagedPDF> computeSequence( |
80 | Hashtable<ActiveResource, ManagedPDF> leftRUs, |
81 | Hashtable<ActiveResource, ManagedPDF> rightRUs) { |
82 | |
83 | return performOperation(BinaryOperation.SEQUENCE, leftRUs, 0, rightRUs, |
84 | 0); |
85 | } |
86 | |
87 | private Hashtable<ActiveResource, ManagedPDF> performOperation( |
88 | BinaryOperation op, Hashtable<ActiveResource, ManagedPDF> leftRUs, |
89 | double leftProbability, |
90 | Hashtable<ActiveResource, ManagedPDF> rightRUs, |
91 | double rightProbability) { |
92 | |
93 | Hashtable<ActiveResource, ManagedPDF> resultTable = new Hashtable<ActiveResource, ManagedPDF>(); |
94 | |
95 | try { |
96 | List<ActiveResource> keyList = getDisjointUnion(leftRUs.keySet(), |
97 | rightRUs.keySet()); |
98 | for (ActiveResource resource : keyList) { |
99 | ManagedPDF leftRU = leftRUs.get(resource); |
100 | ManagedPDF rightRU = rightRUs.get(resource); |
101 | ManagedPDF resultRU = performOperation(op, leftRU, |
102 | leftProbability, rightRU, rightProbability); |
103 | resultTable.put(resource, resultRU); |
104 | } |
105 | } catch (Exception e) { |
106 | e.printStackTrace(); |
107 | System.exit(1); |
108 | } |
109 | return resultTable; |
110 | } |
111 | |
112 | private ManagedPDF performOperation(BinaryOperation op, ManagedPDF leftRU, |
113 | double leftProbability, ManagedPDF rightRU, double rightProbability) |
114 | throws ProbabilityFunctionException, BothNullExpeception, ConfigurationNotSetException { |
115 | leftRU = ensureNotNull(leftRU, rightRU); |
116 | rightRU = ensureNotNull(rightRU, leftRU); |
117 | return pdfPerformanceOps.performOperation(op, leftRU, leftProbability, rightRU, |
118 | rightProbability); |
119 | } |
120 | |
121 | private ManagedPDF ensureNotNull(ManagedPDF rightRU, ManagedPDF leftRU) |
122 | throws BothNullExpeception, ConfigurationNotSetException { |
123 | if (rightRU == null) { |
124 | if (leftRU == null) { |
125 | throw new BothNullExpeception(); |
126 | } else { |
127 | rightRU = ManagedPDF.createDiracImpulse(); |
128 | } |
129 | } |
130 | return rightRU; |
131 | } |
132 | |
133 | private List<ActiveResource> getDisjointUnion(Set<ActiveResource> setOne, |
134 | Set<ActiveResource> name) { |
135 | List<ActiveResource> resultList = new ArrayList<ActiveResource>(setOne); |
136 | for (ActiveResource resource : name) { |
137 | if (!resultList.contains(resource)) { |
138 | resultList.add(resource); |
139 | } |
140 | } |
141 | return resultList; |
142 | } |
143 | } |