1 | package test.rewolucja.resources.physical.base; |
---|
2 | |
---|
3 | import java.util.ArrayList; |
---|
4 | import java.util.List; |
---|
5 | import java.util.Properties; |
---|
6 | import java.util.Stack; |
---|
7 | |
---|
8 | import test.rewolucja.energy.extension.EnergyExtension; |
---|
9 | import test.rewolucja.energy.profile.PowerInterface; |
---|
10 | import test.rewolucja.extensions.Extension; |
---|
11 | import test.rewolucja.extensions.ExtensionList; |
---|
12 | import test.rewolucja.extensions.ExtensionListImpl; |
---|
13 | import test.rewolucja.extensions.ExtensionType; |
---|
14 | import test.rewolucja.resources.Resource; |
---|
15 | import test.rewolucja.resources.ResourceCharacteristics; |
---|
16 | import test.rewolucja.resources.ResourceStatus; |
---|
17 | import test.rewolucja.resources.ResourceType; |
---|
18 | import test.rewolucja.resources.StructureEntityInterface; |
---|
19 | import test.rewolucja.resources.description.ExecResourceDescription; |
---|
20 | import test.rewolucja.resources.exception.ResourceException; |
---|
21 | import test.rewolucja.resources.logical.LogicalResource; |
---|
22 | import test.rewolucja.resources.properties.DefaultPropertiesBuilder; |
---|
23 | import test.rewolucja.resources.properties.PropertiesDirector; |
---|
24 | import test.rewolucja.resources.test.Event; |
---|
25 | import test.rewolucja.resources.utils.validator.ResourceNameValidator; |
---|
26 | import test.rewolucja.resources.utils.validator.ResourcePropertiesValidator; |
---|
27 | import test.rewolucja.resources.utils.validator.ResourceStatusValidator; |
---|
28 | import test.rewolucja.resources.utils.validator.ResourceTypeValidator; |
---|
29 | import test.rewolucja.resources.utils.validator.ResourceValidator; |
---|
30 | |
---|
31 | public class ComputingResource implements Resource, StructureEntityInterface<ComputingResource> { |
---|
32 | |
---|
33 | protected String name; |
---|
34 | protected ResourceType type; |
---|
35 | |
---|
36 | protected ResourceStatus status; |
---|
37 | |
---|
38 | protected ComputingResource parent; |
---|
39 | protected List<ComputingResource> children; |
---|
40 | |
---|
41 | protected ResourceCharacteristics resourceCharacteristic; |
---|
42 | //protected Properties properties; |
---|
43 | |
---|
44 | protected ExtensionList extensionList; |
---|
45 | |
---|
46 | |
---|
47 | public ExtensionList getExtensionList() { |
---|
48 | return extensionList; |
---|
49 | } |
---|
50 | |
---|
51 | /*public ComputingResource(String resourceName, ResourceType type, ResourceCharacteristics resourceCharacteristic) { |
---|
52 | super(); |
---|
53 | this.type = type; |
---|
54 | this.name = resourceName; |
---|
55 | this.status = ResourceStatus.FREE; |
---|
56 | this.resourceCharacteristic = resourceCharacteristic; |
---|
57 | this.extensionList = new ExtensionListImpl(1); |
---|
58 | }*/ |
---|
59 | |
---|
60 | public ComputingResource(ExecResourceDescription resDesc) { |
---|
61 | this.type = resDesc.getType(); |
---|
62 | this.name = resDesc.getResourceId(); |
---|
63 | this.status = ResourceStatus.FREE; |
---|
64 | this.extensionList = new ExtensionListImpl(1); |
---|
65 | initCharacteristics(resDesc); |
---|
66 | //this(resDesc.getResourceId(), resDesc.getType(), new ResourceCharacteristics( |
---|
67 | // resDesc.getResourceUnits())); |
---|
68 | } |
---|
69 | |
---|
70 | protected void initCharacteristics(ExecResourceDescription resDesc){ |
---|
71 | resourceCharacteristic = new ResourceCharacteristics(resDesc.getResourceUnits()); |
---|
72 | } |
---|
73 | |
---|
74 | public ComputingResource getParent() { |
---|
75 | return parent; |
---|
76 | } |
---|
77 | |
---|
78 | public void setParent(ComputingResource newParent) { |
---|
79 | parent = newParent; |
---|
80 | } |
---|
81 | |
---|
82 | public List<ComputingResource> getChildren() { |
---|
83 | return children; |
---|
84 | } |
---|
85 | |
---|
86 | public void addChild(ComputingResource child) { |
---|
87 | child.setParent(this); |
---|
88 | if (children == null) |
---|
89 | children = new ArrayList<ComputingResource>(); |
---|
90 | children.add(child); |
---|
91 | } |
---|
92 | |
---|
93 | public String getName() { |
---|
94 | return name; |
---|
95 | } |
---|
96 | |
---|
97 | public ResourceType getType() { |
---|
98 | return type; |
---|
99 | } |
---|
100 | |
---|
101 | public ResourceCharacteristics getResourceCharacteristic() { |
---|
102 | return resourceCharacteristic; |
---|
103 | } |
---|
104 | |
---|
105 | public ResourceStatus getStatus() { |
---|
106 | return status; |
---|
107 | } |
---|
108 | |
---|
109 | public void setStatus(ResourceStatus newStatus) { |
---|
110 | status = newStatus; |
---|
111 | |
---|
112 | if(children != null) |
---|
113 | { |
---|
114 | for(ComputingResource child: children){ |
---|
115 | child.setStatus(status); |
---|
116 | } |
---|
117 | } |
---|
118 | } |
---|
119 | |
---|
120 | public void triggerEventUp(Event event) { |
---|
121 | if(parent != null) |
---|
122 | parent.handleEvent(event); |
---|
123 | } |
---|
124 | |
---|
125 | public void handleEvent(Event event){ |
---|
126 | if (extensionList != null) { |
---|
127 | for (Extension extension : extensionList) { |
---|
128 | if (extension.supportsEvent(event)) { |
---|
129 | extension.handleEvent(event); |
---|
130 | } |
---|
131 | } |
---|
132 | } |
---|
133 | triggerEventUp(event); |
---|
134 | } |
---|
135 | |
---|
136 | public List <? extends ComputingResource> getDescendantsByType(ResourceType type) throws ResourceException { |
---|
137 | List<ResourceValidator> validators = new ArrayList<ResourceValidator>(); |
---|
138 | validators.add(new ResourceTypeValidator(type)); |
---|
139 | return searchDescendants(validators); |
---|
140 | } |
---|
141 | |
---|
142 | public List<? extends ComputingResource> getDescendantsByTypeAndStatus(ResourceType type, ResourceStatus status) throws ResourceException { |
---|
143 | List<ResourceValidator> validators = new ArrayList<ResourceValidator>(); |
---|
144 | validators.add(new ResourceStatusValidator(status)); |
---|
145 | validators.add(new ResourceTypeValidator(type)); |
---|
146 | return searchDescendants(validators); |
---|
147 | } |
---|
148 | |
---|
149 | public ComputingResource getDescendantsByName(String resourceName) throws ResourceException { |
---|
150 | List<ResourceValidator> validators = new ArrayList<ResourceValidator>(); |
---|
151 | validators.add(new ResourceNameValidator(resourceName)); |
---|
152 | List<? extends ComputingResource> resources = searchDescendants(validators); |
---|
153 | return resources.size() == 0 ? null : resources.get(0); |
---|
154 | } |
---|
155 | |
---|
156 | public List<? extends ComputingResource> filterDescendants(Properties properties) { |
---|
157 | List<ResourceValidator> validators = new ArrayList<ResourceValidator>(); |
---|
158 | validators.add(new ResourcePropertiesValidator(properties)); |
---|
159 | return searchDescendants(validators); |
---|
160 | } |
---|
161 | |
---|
162 | protected List<? extends ComputingResource> searchDescendants(List<ResourceValidator> validators) { |
---|
163 | |
---|
164 | List<ComputingResource> descendants = new ArrayList<ComputingResource>(); |
---|
165 | if (children != null) { |
---|
166 | Stack<ComputingResource> toExamine = new Stack<ComputingResource>(); |
---|
167 | toExamine.push(this); |
---|
168 | |
---|
169 | while (!toExamine.isEmpty()) { |
---|
170 | ComputingResource resource = toExamine.pop(); |
---|
171 | List<ComputingResource> resources = resource.getChildren(); |
---|
172 | if (resources == null) |
---|
173 | continue; |
---|
174 | int numberOfRes = resources.size(); |
---|
175 | for (int i = 0; i < numberOfRes; i++) { |
---|
176 | ComputingResource resourceChild = resources.get(i); |
---|
177 | if (resourceChild.match(validators)) { |
---|
178 | descendants.add(resourceChild); |
---|
179 | } else |
---|
180 | toExamine.insertElementAt(resourceChild, 0); |
---|
181 | //toExamine.push(resourceChild); |
---|
182 | } |
---|
183 | } |
---|
184 | } |
---|
185 | return descendants; |
---|
186 | } |
---|
187 | |
---|
188 | protected boolean match(List<ResourceValidator> validators){ |
---|
189 | for(ResourceValidator validator: validators){ |
---|
190 | if(validator.validate(this) == false) |
---|
191 | return false; |
---|
192 | } |
---|
193 | return true; |
---|
194 | } |
---|
195 | |
---|
196 | public Properties getProperties(){ |
---|
197 | PropertiesDirector propDirector = new PropertiesDirector(); |
---|
198 | propDirector.setPropertiesBuilder(new DefaultPropertiesBuilder()); |
---|
199 | propDirector.constructProperties(this); |
---|
200 | return propDirector.getProperties(); |
---|
201 | } |
---|
202 | |
---|
203 | |
---|
204 | public PowerInterface getPowerInterface(){ |
---|
205 | if (extensionList != null) { |
---|
206 | for (Extension extension : extensionList) { |
---|
207 | if (extension.getType() == ExtensionType.ENERGY_EXTENSION) { |
---|
208 | EnergyExtension ee = (EnergyExtension)extension; |
---|
209 | return ee.getPowerInterface(); |
---|
210 | } |
---|
211 | } |
---|
212 | } |
---|
213 | return null; |
---|
214 | } |
---|
215 | |
---|
216 | protected LogicalResource logicalResource; |
---|
217 | |
---|
218 | public LogicalResource getLogicalResource() { |
---|
219 | return logicalResource; |
---|
220 | } |
---|
221 | |
---|
222 | public void setLogicalResource(LogicalResource logRes) { |
---|
223 | logicalResource = logRes; |
---|
224 | } |
---|
225 | /*public void addLogicalResource(LogicalResource logicalResource) { |
---|
226 | if (logicalResources == null) |
---|
227 | logicalResources = new ArrayList<LogicalResource>(1); |
---|
228 | logicalResources.add(logicalResource); |
---|
229 | }*/ |
---|
230 | } |
---|