StateEstimator.java 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package classes.holonControlUnit;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import classes.Holon;
  5. import classes.HolonElement;
  6. import classes.holonControlUnit.messages.Message;
  7. import classes.holonControlUnit.messages.StateMsg;
  8. import classes.holonControlUnit.messages.StateRequestMsg;
  9. /**
  10. * aggregates all states from sub holons and forwards the resulting own state to the state assembler
  11. * @author Jonas
  12. *
  13. */
  14. public class StateEstimator {
  15. /* indicates the current state of the holon */
  16. public enum StateIndicator {
  17. /* everything runs as expected, all parameters are inside their tresholds */
  18. DESIRED,
  19. /* everything is still as expected except for minor deviations, parts may be endangered/dysfunctional */
  20. ENDANGERED,
  21. /* stability/functionality of holon is not guaranteed */
  22. DYSFUNCTIONAL
  23. }
  24. private final float POWER_TRESHOLD = 0.95f;
  25. private HolonControlUnit hcu;
  26. private StateIndicator stateInd;
  27. private float powerUsage;
  28. private HashMap<String, StateMsg> childStates;
  29. public StateEstimator(HolonControlUnit hcu) {
  30. this.hcu = hcu;
  31. this.stateInd = StateIndicator.DESIRED;
  32. }
  33. public StateIndicator getStateIndicator() {
  34. return stateInd;
  35. }
  36. /**
  37. * Depth-first computation
  38. * aggregates childrens state first, then computes own state
  39. */
  40. public void computeState(int timeStep) {
  41. //request childrens state
  42. this.childStates = new HashMap<String, StateMsg>();
  43. ArrayList<String> children = this.hcu.getHierarchyController().getSubHolons();
  44. for(String child : children) {
  45. //send request to child
  46. this.hcu.getCommunicator().sendMsg(child, Message.Type.STATE_REQUEST,
  47. this.hcu.getCommunicator().getGson().toJson(new StateRequestMsg(timeStep)));
  48. }
  49. //wait for sub holons to compute state and send it
  50. if(this.childStates.size() < children.size()) {
  51. System.err.println("Something went wrong while computing state of "+this.hcu.getHolon().getUniqueID());
  52. System.err.println("\tchildStates "+childStates);
  53. System.err.println("\tchildren "+children);
  54. return;
  55. }
  56. //aggregate cub holons state
  57. this.powerUsage = 0;
  58. for(String s : this.childStates.keySet()) {
  59. this.powerUsage += this.childStates.get(s).getPowerUsage();
  60. }
  61. //add state from holon elements
  62. Holon holon = this.hcu.getHolon();
  63. if(holon.isPhysical) {
  64. this.powerUsage += holon.getHolonObject().getEnergyAtTimeStep(timeStep);
  65. }
  66. if(this.powerUsage < this.POWER_TRESHOLD * this.hcu.getStateAssembler().getDesiredPowerUsage()) {
  67. this.stateInd = StateIndicator.DESIRED;
  68. } else {
  69. this.stateInd = StateIndicator.ENDANGERED;
  70. }
  71. }
  72. public void receiveState(String sender, StateMsg stateMsg) {
  73. if(this.childStates == null)
  74. return;
  75. this.childStates.put(sender, stateMsg);
  76. }
  77. public void receiveStateRequest(String sender, StateRequestMsg req) {
  78. computeState(req.getTimeStep());
  79. //send current state to sender/super holon
  80. StateMsg stateMsg = new StateMsg(this.powerUsage);
  81. String body = this.hcu.getCommunicator().getGson().toJson(stateMsg);
  82. this.hcu.getCommunicator().sendMsg(sender, Message.Type.STATE, body);
  83. }
  84. public float getPowerUsage() {
  85. return this.powerUsage;
  86. }
  87. }