Edge.java 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package holeg.model;
  2. public class Edge {
  3. // Max. capacity of the Edge, if flow is greater than the status --> is
  4. // Working would be false
  5. public float maxCapacity;
  6. // Source
  7. AbstractCanvasObject a;
  8. // Destination
  9. AbstractCanvasObject b;
  10. public EdgeMode mode = EdgeMode.Normal;
  11. private transient EdgeState state = initState();
  12. private transient float flowEnergy = 0;
  13. /**
  14. * Constructor with a user-defined max. capacity
  15. *
  16. * @param a Source
  17. * @param b Destination
  18. * @param maxCap Maximum Capacity
  19. */
  20. public Edge(AbstractCanvasObject a, AbstractCanvasObject b, float maxCap) {
  21. setA(a);
  22. setB(b);
  23. this.maxCapacity = maxCap;
  24. }
  25. /**
  26. * Clone Constructor
  27. */
  28. public Edge(Edge other) {
  29. setA(other.a);
  30. setB(other.b);
  31. this.maxCapacity = other.maxCapacity;
  32. this.state = other.state;
  33. this.mode = other.mode;
  34. this.flowEnergy = other.flowEnergy;
  35. }
  36. /**
  37. * Getter for the length of the Cable.
  38. */
  39. public float getLength() {
  40. return a.getPosition().getDistance(b.getPosition());
  41. }
  42. /**
  43. * Getter for the Source.
  44. *
  45. * @return the a
  46. */
  47. public AbstractCanvasObject getA() {
  48. return a;
  49. }
  50. /**
  51. * Set the Source to a new one.
  52. *
  53. * @param a the a to set
  54. */
  55. public void setA(AbstractCanvasObject a) {
  56. this.a = a;
  57. }
  58. /**
  59. * Getter for the destination.
  60. *
  61. * @return the b
  62. */
  63. public AbstractCanvasObject getB() {
  64. return b;
  65. }
  66. /**
  67. * Set the Destination to a new one.
  68. *
  69. * @param b the b to set
  70. */
  71. public void setB(AbstractCanvasObject b) {
  72. this.b = b;
  73. }
  74. /**
  75. * Check if a CpsEdge is Connected to the AbstractCpsObject.
  76. *
  77. * @param holonObject the AbstractCpsObject to check.
  78. * @return true if either a or b is the AbstractCpsObject to check.
  79. */
  80. public boolean isConnectedTo(AbstractCanvasObject holonObject) {
  81. return (holonObject.equals(a) || holonObject.equals(b));
  82. }
  83. @Override
  84. public String toString() {
  85. String A = (a == null) ? "null" : a.getName() + "[" + a.getId() + "]";
  86. String B = (b == null) ? "null" : b.getName() + "[" + b.getId() + "]";
  87. return "CpsEdge: " + A + " to " + B;
  88. }
  89. public EdgeState getState() {
  90. return state;
  91. }
  92. //TODO(Tom 2021-12-3): public -> package
  93. public void setState(EdgeState state) {
  94. this.state = state;
  95. }
  96. //TODO(Tom 2021-12-3): public -> package
  97. public void reset() {
  98. state = initState();
  99. }
  100. private EdgeState initState() {
  101. if (mode == EdgeMode.Disconnected) {
  102. return EdgeState.Burned;
  103. }
  104. return EdgeState.Working;
  105. }
  106. public float getActualFlow() {
  107. return flowEnergy;
  108. }
  109. //TODO(Tom 2021-12-3): public -> package
  110. public void setActualFlow(float energyToSupplyInTheNetwork) {
  111. flowEnergy = energyToSupplyInTheNetwork;
  112. }
  113. public float getEnergyFromConneted() {
  114. float energy = 0.0f;
  115. if (getA() instanceof HolonObject hO && hO.getActualEnergy() > 0) {
  116. energy += hO.getActualEnergy();
  117. }
  118. if (getB() instanceof HolonObject hO && hO.getActualEnergy() > 0) {
  119. energy += hO.getActualEnergy();
  120. }
  121. return energy;
  122. }
  123. public enum EdgeMode {
  124. Normal, Unlimited, Disconnected
  125. }
  126. /*
  127. * STATE
  128. */
  129. public enum EdgeState {
  130. Working, Burned
  131. }
  132. }