1 | package schedframe; |
---|
2 | |
---|
3 | import gridsim.GridSim; |
---|
4 | |
---|
5 | import java.util.ArrayList; |
---|
6 | import java.util.HashSet; |
---|
7 | import java.util.Iterator; |
---|
8 | import java.util.LinkedList; |
---|
9 | import java.util.List; |
---|
10 | import java.util.Set; |
---|
11 | |
---|
12 | import schedframe.exceptions.ResourceException; |
---|
13 | import schedframe.resources.ResourceHistoryChanges; |
---|
14 | import schedframe.resources.computing.ComputingResource; |
---|
15 | import schedframe.scheduling.Scheduler; |
---|
16 | |
---|
17 | public class SimulatedEnvironment { |
---|
18 | |
---|
19 | protected static Scheduler scheduler; |
---|
20 | protected static List<ComputingResource> computingResources; |
---|
21 | protected List<Initializable> toInit; |
---|
22 | protected Set<String> compResLayers; |
---|
23 | protected static List<ResourceHistoryChanges> compResHistory = new ArrayList<ResourceHistoryChanges>(); |
---|
24 | |
---|
25 | public SimulatedEnvironment(Scheduler logicalStructure, List<ComputingResource> compResources){ |
---|
26 | scheduler = logicalStructure; |
---|
27 | computingResources = compResources; |
---|
28 | } |
---|
29 | |
---|
30 | public Scheduler getScheduler() { |
---|
31 | return scheduler; |
---|
32 | } |
---|
33 | |
---|
34 | public List<ComputingResource> getComputingResources() { |
---|
35 | return computingResources; |
---|
36 | } |
---|
37 | |
---|
38 | public static ComputingResource getComputingResourceByName(String resourceName) throws ResourceException { |
---|
39 | ComputingResource resourceWithName = null; |
---|
40 | for (int i = 0; i < computingResources.size() && resourceWithName == null; i++) { |
---|
41 | ComputingResource resource = computingResources.get(i); |
---|
42 | if (resource.getFullName().equals(resourceName)) |
---|
43 | resourceWithName = resource; |
---|
44 | else |
---|
45 | resourceWithName = resource.getDescendantByName(resourceName); |
---|
46 | } |
---|
47 | return resourceWithName; |
---|
48 | } |
---|
49 | |
---|
50 | private static Scheduler getSchedulerByName(String schedulerName) { |
---|
51 | Scheduler schedulerWithName = null; |
---|
52 | |
---|
53 | if (scheduler.getName().compareTo(schedulerName) == 0) |
---|
54 | schedulerWithName = scheduler; |
---|
55 | else if (scheduler.getChildren() != null) { |
---|
56 | LinkedList<Scheduler> toExamine = new LinkedList<Scheduler>(); |
---|
57 | toExamine.push(scheduler); |
---|
58 | |
---|
59 | while (!toExamine.isEmpty() && schedulerWithName != null) { |
---|
60 | Scheduler scheduler = toExamine.pop(); |
---|
61 | List<Scheduler> schedulers = scheduler.getChildren(); |
---|
62 | |
---|
63 | int numberOfSched = schedulers.size(); |
---|
64 | for (int i = 0; i < numberOfSched; i++) { |
---|
65 | Scheduler schedulerChild = schedulers.get(i); |
---|
66 | if(scheduler.getName().equals(schedulerName)){ |
---|
67 | schedulerWithName = schedulerChild; |
---|
68 | break; |
---|
69 | } else |
---|
70 | toExamine.addLast(schedulerChild); |
---|
71 | } |
---|
72 | } |
---|
73 | } |
---|
74 | return schedulerWithName; |
---|
75 | } |
---|
76 | |
---|
77 | public static Scheduler getScheduler(String resName){ |
---|
78 | if(GridSim.getEntityId(resName) != -1){ |
---|
79 | return getSchedulerByName(resName); |
---|
80 | } |
---|
81 | ComputingResource resourceWithName = null; |
---|
82 | try { |
---|
83 | resourceWithName = getComputingResourceByName(resName); |
---|
84 | } catch (ResourceException e) { |
---|
85 | } |
---|
86 | /*for(int i = 0 ; i < computingResources.size() && resourceWithName == null; i++){ |
---|
87 | ComputingResource resource = computingResources.get(i); |
---|
88 | if(resource.getName().equals(resName)) |
---|
89 | resourceWithName = resource; |
---|
90 | else |
---|
91 | try { |
---|
92 | resourceWithName = resource.getDescendantByName(resName); |
---|
93 | } catch (ResourceException e) { |
---|
94 | return null; |
---|
95 | } |
---|
96 | }*/ |
---|
97 | if(resourceWithName == null) |
---|
98 | return null; |
---|
99 | List<ComputingResource> children = resourceWithName.getChildren(); |
---|
100 | Set<Scheduler> childrenSchedulers = new HashSet<Scheduler>(); |
---|
101 | if(children.isEmpty()) |
---|
102 | return null; |
---|
103 | for(ComputingResource child: children) { |
---|
104 | childrenSchedulers.add(child.getScheduler()); |
---|
105 | } |
---|
106 | |
---|
107 | Set<Scheduler> tempChildrenSchedulers = new HashSet<Scheduler>(childrenSchedulers); |
---|
108 | while(childrenSchedulers.size() > 1){ |
---|
109 | childrenSchedulers = new HashSet<Scheduler>(); |
---|
110 | for(Scheduler s: tempChildrenSchedulers){ |
---|
111 | childrenSchedulers.add(s.getParent()); |
---|
112 | } |
---|
113 | tempChildrenSchedulers = new HashSet<Scheduler>(childrenSchedulers); |
---|
114 | } |
---|
115 | Iterator<Scheduler> it = childrenSchedulers.iterator(); |
---|
116 | Scheduler potentialScheduler = it.next(); |
---|
117 | if(potentialScheduler.getCompResources().containsAll(children)) |
---|
118 | return potentialScheduler; |
---|
119 | return null; |
---|
120 | } |
---|
121 | |
---|
122 | public static ComputingResource getCommonComputingResourceParent(List<ComputingResource> compResources){ |
---|
123 | if(compResources.size() == 0) |
---|
124 | return null; |
---|
125 | Set<ComputingResource> candidates = new HashSet<ComputingResource>(compResources); |
---|
126 | while(candidates.size() > 1){ |
---|
127 | Set<ComputingResource> parents = new HashSet<ComputingResource>(); |
---|
128 | Iterator<ComputingResource> it = candidates.iterator(); |
---|
129 | while(it.hasNext()){ |
---|
130 | ComputingResource compRes = it.next(); |
---|
131 | if(compRes.getParent() != null){ |
---|
132 | parents.add(compRes.getParent()); |
---|
133 | } |
---|
134 | it.remove(); |
---|
135 | } |
---|
136 | candidates.addAll(parents); |
---|
137 | } |
---|
138 | return candidates.toArray(new ComputingResource[0])[0]; |
---|
139 | } |
---|
140 | |
---|
141 | public void setInitList(List<Initializable> toI) { |
---|
142 | toInit = toI; |
---|
143 | } |
---|
144 | |
---|
145 | public List<Initializable> getToInit() { |
---|
146 | return toInit; |
---|
147 | } |
---|
148 | |
---|
149 | public Set<String> getComputingResourceLayers() { |
---|
150 | return compResLayers; |
---|
151 | } |
---|
152 | |
---|
153 | public void setCompResLayers(Set<String> compResLayers) { |
---|
154 | this.compResLayers = compResLayers; |
---|
155 | } |
---|
156 | |
---|
157 | public static void traceResource(long timestamp, String resourceName, String operation, String paramter){ |
---|
158 | ResourceHistoryChanges rhc = new ResourceHistoryChanges(timestamp, resourceName, operation, paramter); |
---|
159 | compResHistory.add(rhc); |
---|
160 | } |
---|
161 | |
---|
162 | public static List<ResourceHistoryChanges> getCompResHistory() { |
---|
163 | return compResHistory; |
---|
164 | } |
---|
165 | } |
---|