HierarchyControlUnit.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. package classes.holonControlUnit;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.Map;
  5. import classes.AbstractCanvasObject;
  6. import classes.Edge;
  7. import classes.GroupNode;
  8. import classes.Holon;
  9. import classes.HolonObject;
  10. import classes.HolonSwitch;
  11. import classes.Node;
  12. import classes.holonControlUnit.messages.NeighborhoodMsg;
  13. import classes.holonControlUnit.messages.SplitMsg;
  14. import classes.holonControlUnit.messages.SplitMsg.SenderRealtion;
  15. import classes.holonControlUnit.messages.StateMsg;
  16. import classes.holonControlUnit.messages.MergeMsg;
  17. import classes.holonControlUnit.messages.Message;
  18. public class HierarchyControlUnit {
  19. private HolonControlUnit hcu;
  20. private String superHolon;
  21. private ArrayList<String> subHolons;
  22. private ArrayList<String> physicalNeighbors;
  23. private ArrayList<String> virtualNeighbors;
  24. private ArrayList<ArrayList<MergeMsg>> receivedMergeRequests; //store merge requests for each timestep
  25. private ArrayList<HashMap<String, MergeMsg>> sendMergeRequests;
  26. public HierarchyControlUnit(HolonControlUnit hcu) {
  27. this.hcu = hcu;
  28. this.subHolons = new ArrayList<>();
  29. this.physicalNeighbors = new ArrayList<>();
  30. this.virtualNeighbors = new ArrayList<>();
  31. this.receivedMergeRequests = new ArrayList<ArrayList<MergeMsg>>();
  32. this.sendMergeRequests = new ArrayList<HashMap<String, MergeMsg>>();
  33. }
  34. public ArrayList<String> getPhysicalNeighbors() {
  35. return physicalNeighbors;
  36. }
  37. public void setPhysicalNeighbors(ArrayList<String> physicalNeighbors) {
  38. this.physicalNeighbors = physicalNeighbors;
  39. }
  40. public void addPhysicalNeighbor(String physicalNeighbor) {
  41. if(!this.physicalNeighbors.contains(physicalNeighbor)) {
  42. this.physicalNeighbors.add(physicalNeighbor);
  43. }
  44. }
  45. public ArrayList<String> getVirtualNeighbors() {
  46. return virtualNeighbors;
  47. }
  48. public void setVirtualNeighbors(ArrayList<String> virtualNeighbors) {
  49. this.virtualNeighbors = virtualNeighbors;
  50. }
  51. public void resetVirtualNeighbors() {
  52. this.virtualNeighbors.clear();
  53. }
  54. public void addSubHolon(String subHolon) {
  55. if(this.subHolons.contains(subHolon))
  56. return;
  57. //propagate new virtual neighbor for all subholons
  58. ArrayList<String> list = new ArrayList<String>();
  59. list.add(subHolon);
  60. String body = this.hcu.getCommunicator().getGson().toJson(new NeighborhoodMsg(NeighborhoodMsg.Type.NEW_VIRTUAL_NEIGHBOR, list));
  61. for(String sub : this.subHolons) {
  62. this.hcu.getCommunicator().sendMsg(sub, Message.Type.NEIGHBORHOOD, body);
  63. }
  64. if(this.subHolons.size() > 0) {
  65. body = this.hcu.getCommunicator().getGson().toJson(new NeighborhoodMsg(NeighborhoodMsg.Type.NEW_VIRTUAL_NEIGHBOR, this.subHolons));
  66. this.hcu.getCommunicator().sendMsg(subHolon, Message.Type.NEIGHBORHOOD, body);
  67. }
  68. this.subHolons.add(subHolon);
  69. }
  70. public void removeSubHolon(String subHolon) {
  71. if(!this.subHolons.remove(subHolon)) {
  72. System.err.println(this.hcu.getHolon().getUniqueID()+" could not remove sub holon "+subHolon+"\n\t"+subHolons);
  73. return;
  74. }
  75. ArrayList<String> list = new ArrayList<String>();
  76. list.add(subHolon);
  77. String body = this.hcu.getCommunicator().getGson().toJson(new NeighborhoodMsg(NeighborhoodMsg.Type.REMOVE_VIRTUAL_NEIGHBOR, list));
  78. for(String sub : this.subHolons) {
  79. this.hcu.getCommunicator().sendMsg(sub, Message.Type.NEIGHBORHOOD, body);
  80. }
  81. }
  82. public String getSuperHolon() {
  83. return this.superHolon;
  84. }
  85. public void setSuperHolon(String parent) {
  86. this.superHolon = parent;
  87. }
  88. public ArrayList<String> getSubHolons() {
  89. return this.subHolons;
  90. }
  91. public ArrayList<ArrayList<MergeMsg>> getMergeRequests() {
  92. return receivedMergeRequests;
  93. }
  94. public ArrayList<MergeMsg> getMergeRequestsForTimeStep(int timeStep){
  95. if(timeStep >= 0 && timeStep < this.receivedMergeRequests.size()) {
  96. return this.receivedMergeRequests.get(timeStep);
  97. }
  98. return new ArrayList<MergeMsg>();
  99. }
  100. public void addVirtualNeighbor(String virtualNeighbor) {
  101. if(!this.virtualNeighbors.contains(virtualNeighbor)) {
  102. this.virtualNeighbors.add(virtualNeighbor);
  103. }
  104. }
  105. public void addVirtualNeighbors(ArrayList<String> virtualNeighbors) {
  106. for(int i=0; i<virtualNeighbors.size(); i++) {
  107. String virtualNeighbor = virtualNeighbors.get(i);
  108. if(!this.virtualNeighbors.contains(virtualNeighbor))
  109. this.virtualNeighbors.add(virtualNeighbor);
  110. }
  111. }
  112. public void removeVirtualNeighbor(String virtualNeighbor) {
  113. this.virtualNeighbors.remove(virtualNeighbor);
  114. }
  115. public void removeVirtualNeighbors(ArrayList<String> virtualNeighbor) {
  116. this.virtualNeighbors.removeAll(virtualNeighbor);
  117. }
  118. public void addEdgeTo(AbstractCanvasObject b) {
  119. if(b instanceof HolonObject && ((HolonObject)b).holon != null) {
  120. //add holon of b as physical neighbor
  121. Holon ho = ((HolonObject) b).holon;
  122. if(!this.physicalNeighbors.contains(ho.getUniqueID())) {
  123. this.physicalNeighbors.add(ho.getUniqueID());
  124. }
  125. } else if(b instanceof HolonSwitch) {
  126. //get edges of b and add them if switch is open
  127. HolonSwitch hs = (HolonSwitch) b;
  128. if(!this.physicalNeighbors.contains(hs.getUniqueID())) {
  129. this.physicalNeighbors.add(hs.getUniqueID());
  130. }
  131. } else if(b instanceof Node) {
  132. Node n = (Node) b;
  133. if(!this.physicalNeighbors.contains(n.getUniqueID()))
  134. this.physicalNeighbors.add(n.getUniqueID());
  135. } else if (b instanceof GroupNode) {
  136. GroupNode n = (GroupNode) b;
  137. if(!this.physicalNeighbors.contains(n.getUniqueID()))
  138. this.physicalNeighbors.add(n.getUniqueID());
  139. }
  140. }
  141. public void removeEdgeTo(AbstractCanvasObject b) {
  142. if(b instanceof HolonObject) {
  143. //remove holon of b as physical neighbor
  144. Holon ho = ((HolonObject) b).holon;
  145. this.physicalNeighbors.remove(ho.getUniqueID());
  146. } else if(b instanceof HolonSwitch) {
  147. //get edges of b and add them if switch is open
  148. HolonSwitch hs = (HolonSwitch) b;
  149. this.physicalNeighbors.remove(hs.getUniqueID());
  150. } else if(b instanceof Node) {
  151. Node n = (Node) b;
  152. this.physicalNeighbors.remove(n.getUniqueID());
  153. } else if (b instanceof GroupNode) {
  154. GroupNode n = (GroupNode) b;
  155. this.physicalNeighbors.remove(n.getUniqueID());
  156. }
  157. }
  158. public void removeEdge(Edge e) {
  159. AbstractCanvasObject a = e.getA();
  160. AbstractCanvasObject b = e.getB();
  161. if(a.equals(this.hcu.getHolon().getHolonObject())) {
  162. removeEdgeTo(b);
  163. } else {
  164. removeEdgeTo(a);
  165. }
  166. //check if connection to super or subholon is affected
  167. Holon h = this.hcu.getHolon();
  168. if(h.getLayer() > 1 && h.getParent().getPathsToChildren().get(h).contains(e)) {
  169. //path to superholon is affected
  170. h.merge("State Holon");
  171. }
  172. Map<Holon, ArrayList<Edge>> set = Map.copyOf(h.getPathsToChildren());
  173. for(Holon c : set.keySet()) {
  174. if(set.get(c).contains(e)) {
  175. //path to subholon c is affected
  176. c.merge("State Holon");
  177. }
  178. }
  179. }
  180. public void receiveMergeReq(MergeMsg msg, String sender) {
  181. if(!canMerge(msg.getRequester(), msg.getPower())) {
  182. return;
  183. }
  184. int timeStep = msg.getTimeStep();
  185. if(timeStep >= 0) {
  186. if(timeStep >= this.receivedMergeRequests.size() && timeStep <= this.hcu.getHolon().model.getIterations()) {
  187. for(int i=this.receivedMergeRequests.size(); i<=timeStep; i++) {
  188. this.receivedMergeRequests.add(new ArrayList<MergeMsg>());
  189. }
  190. }
  191. if(this.receivedMergeRequests.get(timeStep) == null)
  192. this.receivedMergeRequests.add(new ArrayList<MergeMsg>());
  193. ArrayList<MergeMsg> list = this.receivedMergeRequests.get(timeStep);
  194. for(MergeMsg m : list) {
  195. if(m.getPower() == msg.getPower() && m.getRequester() == msg.getRequester() && m.getTimeStep() == msg.getTimeStep()) {
  196. boolean b = true;
  197. for(int i=0; i<m.getPredictedPowerUsage().size(); i++) {
  198. if(m.getPredictedPowerUsage().get(i) != msg.getPredictedPowerUsage().get(i)) {
  199. b = false;
  200. break;
  201. }
  202. }
  203. //the request is a duplicate
  204. if(b)
  205. return;
  206. }
  207. }
  208. list.add(msg);
  209. }
  210. }
  211. public void receiveMergeAck(MergeMsg msg, String sender) {
  212. if(this.superHolon.equals(sender) || this.subHolons.contains(sender) || !canMerge(msg.getRequester(), msg.getPower())) {
  213. return;
  214. }
  215. HashMap<String, MergeMsg> map = this.sendMergeRequests.get(msg.getTimeStep()-1);
  216. if(map.containsKey(sender) && this.hcu.matchPowerRange(map.get(sender).getPower(), msg.getPower(),
  217. map.get(sender).getPredictedPowerUsage(), this.hcu.getOptimizer().getCurrentPowerThreshold())) {
  218. //tell sender that we are still interested in merging
  219. map.clear(); //delete all merge request for this timestep so we dont merge with two holons at a time
  220. StateMsg state = new StateMsg(this.hcu.getStateEstimator().getPowerUsage(), this.hcu.getStateEstimator().getNetThroughput(),
  221. this.hcu.getStateEstimator().getPredictedPowerUsage(), this.hcu.getStateEstimator().getStateIndicator());
  222. MergeMsg m = new MergeMsg(MergeMsg.Type.ACK_II, msg.getPower(), this.hcu.getStateEstimator().getNetThroughput(),
  223. msg.getPredictedPowerUsage(), this.hcu.getHolon().getUniqueID(), msg.getTimeStep(), state, msg.getRedirectedBy());
  224. String body = this.hcu.getCommunicator().getGson().toJson(m, MergeMsg.class);
  225. this.hcu.getCommunicator().sendMsg(sender, Message.Type.MERGE, body);
  226. return;
  227. }
  228. int i = msg.getRedirectedBy().size();
  229. if(msg.getTimeStep() < 1 || msg.getTimeStep()-1-i < 0)
  230. return;
  231. String red = i > 0 ? msg.getRedirectedBy().get(0) : null;
  232. map = this.sendMergeRequests.get(msg.getTimeStep()-1-i);
  233. if(red != null && map.containsKey(red) && this.hcu.matchPowerRange(map.get(red).getPower(), msg.getPower(),
  234. map.get(red).getPredictedPowerUsage(), this.hcu.getOptimizer().getCurrentPowerThreshold())) {
  235. //tell sender that we are still interested in merging
  236. map.clear(); //delete all merge request for this timestep so we dont merge with two holons at a time
  237. StateMsg state = new StateMsg(this.hcu.getStateEstimator().getPowerUsage(), this.hcu.getStateEstimator().getNetThroughput(),
  238. this.hcu.getStateEstimator().getPredictedPowerUsage(), this.hcu.getStateEstimator().getStateIndicator());
  239. MergeMsg m = new MergeMsg(MergeMsg.Type.ACK_II, msg.getPower(), this.hcu.getStateEstimator().getNetThroughput(),
  240. msg.getPredictedPowerUsage(), this.hcu.getHolon().getUniqueID(), msg.getTimeStep(), state, msg.getRedirectedBy());
  241. String body = this.hcu.getCommunicator().getGson().toJson(m, MergeMsg.class);
  242. this.hcu.getCommunicator().sendMsg(sender, Message.Type.MERGE, body);
  243. }
  244. }
  245. public void receiveMergeAckII(MergeMsg msg, String sender) {
  246. if(!canMerge(msg.getRequester(), msg.getPower())) {
  247. return;
  248. }
  249. this.hcu.getHolon().merge(sender);
  250. if(msg.getRedirectedBy() == null || msg.getRedirectedBy().size() == 0) {
  251. this.hcu.getStateEstimator().receiveState(sender, msg.getState());
  252. }
  253. }
  254. public void sendMergeReq(float powerUsage, ArrayList<Float> predictedPowerUsage, int timeStep, String physicalNeighbor) {
  255. MergeMsg msg = new MergeMsg(MergeMsg.Type.REQ, powerUsage, this.hcu.getStateEstimator().getNetThroughput(),
  256. predictedPowerUsage, this.hcu.getHolon().getUniqueID(), timeStep, null, new ArrayList<>());
  257. if(this.sendMergeRequests.size() <= timeStep) {
  258. for(int i=this.sendMergeRequests.size(); i<=timeStep; i++) {
  259. this.sendMergeRequests.add(new HashMap<>());
  260. }
  261. }
  262. this.sendMergeRequests.get(timeStep).put(physicalNeighbor, msg);
  263. String body = this.hcu.getCommunicator().getGson().toJson(msg, MergeMsg.class);
  264. this.hcu.getCommunicator().sendMsg(physicalNeighbor, Message.Type.MERGE, body);
  265. }
  266. public void sendMergeAck(int timeStep, MergeMsg req) {
  267. MergeMsg msg = new MergeMsg(MergeMsg.Type.ACK, req.getPower(), this.hcu.getStateEstimator().getNetThroughput(), req.getPredictedPowerUsage(),
  268. this.hcu.getHolon().getUniqueID(), timeStep, req.getState(), req.getRedirectedBy());
  269. String body = this.hcu.getCommunicator().getGson().toJson(msg, MergeMsg.class);
  270. this.hcu.getCommunicator().sendMsg(req.getRequester(), Message.Type.MERGE, body);
  271. }
  272. public void propagateMergeReqToParent(int timeStep) {
  273. if(timeStep < 0 || timeStep >= this.hcu.getHolon().model.getIterations() || timeStep >= this.receivedMergeRequests.size()
  274. || this.hcu.getHolon().getLayer() <= 1)
  275. return;
  276. for(MergeMsg req : this.receivedMergeRequests.get(timeStep)) {
  277. if(req.getRequester().equals(this.hcu.getHolon().getUniqueID()) || req.getRequester().equals(this.superHolon))
  278. continue;
  279. req.getRedirectedBy().add(this.hcu.getHolon().getUniqueID());
  280. String body = this.hcu.getCommunicator().getGson().toJson(req, MergeMsg.class);
  281. this.hcu.getCommunicator().sendMsg(this.superHolon, Message.Type.MERGE, body);
  282. }
  283. }
  284. public void splitSubHolon(String id, int timeStep) {
  285. if(!this.subHolons.contains(id))
  286. return;
  287. SplitMsg msg = new SplitMsg(SenderRealtion.SUPERHOLON, timeStep);
  288. String body = this.hcu.getCommunicator().getGson().toJson(msg, SplitMsg.class);
  289. this.hcu.getCommunicator().sendMsg(id, Message.Type.SPLIT, body);
  290. this.hcu.getHolon().splitById(id);
  291. }
  292. public void splitSuperHolon(int timeStep) {
  293. SplitMsg msg = new SplitMsg(SenderRealtion.SUBHOLON, timeStep);
  294. String body = this.hcu.getCommunicator().getGson().toJson(msg, SplitMsg.class);
  295. this.hcu.getCommunicator().sendMsg(this.superHolon, Message.Type.SPLIT, body);
  296. this.hcu.getHolon().split(null);
  297. }
  298. public void receiveSplitMsg(SplitMsg msg, String sender) {
  299. if(msg.getType() == SenderRealtion.SUPERHOLON && this.superHolon.equals(sender)) {
  300. // System.out.println(this.hcu.getHolon().getUniqueID()+" my super holon kicked me out :(");
  301. } else if (msg.getType() == SenderRealtion.SUBHOLON && this.subHolons.contains(sender)) {
  302. // System.out.println(this.hcu.getHolon().getUniqueID()+" my child holon abandoned me :(");
  303. }
  304. }
  305. /**
  306. * get all physical neighbors that are not subholons (or a subholon of a subholon) and not virtual neighbors
  307. * @return
  308. */
  309. public ArrayList<String> getPhysicalNeighborsFromOtherHolarchy(){
  310. ArrayList<String> neighbors = new ArrayList<String>();
  311. for(String s : this.physicalNeighbors) {
  312. if(!s.contains("Switch") && !s.contains("Node") && !s.contains("Group Node") && !s.equals(this.superHolon)
  313. && !this.subHolons.contains(s) && !this.virtualNeighbors.contains(s)) {
  314. neighbors.add(s);
  315. } else if(s.contains("Switch") || s.contains("Node") || s.contains("Group Node")) {
  316. ArrayList<String> potNeighbors = this.hcu.getHolon().model.getAllConnectionsFromObject(s, this.hcu.getHolon().getUniqueID());
  317. for(String s2 : potNeighbors) {
  318. if(!s2.equals(this.superHolon) && !this.subHolons.contains(s2) && !this.virtualNeighbors.contains(s2)) {
  319. neighbors.add(s2);
  320. }
  321. }
  322. }
  323. }
  324. return neighbors;
  325. }
  326. /**
  327. * checks if the other holon is a subholon (or subholon of subholon), this superholon or if the added throughput would burn any edges
  328. * @param other
  329. * @param addedThroughput
  330. * @return
  331. */
  332. public boolean canMerge(String other, float addedThroughput) {
  333. Holon thisHolon = this.hcu.getHolon();
  334. Holon otherHolon = thisHolon.model.getHolonsByID().get(other);
  335. if(other.equals(thisHolon.getUniqueID()) || !thisHolon.model.checkHolonObjectsAreConnected(thisHolon, otherHolon, addedThroughput)
  336. || thisHolon.isASuperiorHolon(other) || otherHolon.isASuperiorHolon(this.hcu.getHolon().getUniqueID())) {
  337. // this holon and the other holon can not merge because:
  338. // a) this holon and other holon are the same
  339. // b) other holon is not connected to this holon or the new throughput would burn a cable
  340. // c) other holon is a superior holon of this holon
  341. // d) this holon is a superior holon of other holon
  342. return false;
  343. }
  344. return true;
  345. }
  346. }