Edge.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. package holeg.model;
  2. import com.google.gson.annotations.Expose;
  3. /**
  4. * The class "CpsEdge" represents the connections on the GUI. Each connection
  5. * contains a max. capacity, a flow, a status (isWorking), tags (for internal
  6. * use of electricity flow), source and destination
  7. *
  8. * @author Gruppe14
  9. */
  10. public class Edge {
  11. // Max. capacity of the Edge, if flow is greater than the status --> is
  12. // Working would be false
  13. @Expose
  14. public float maxCapacity;
  15. // Source
  16. AbstractCanvasObject a;
  17. // Destination
  18. AbstractCanvasObject b;
  19. @Expose
  20. private boolean breakedManuel = false;
  21. @Expose
  22. private boolean unlimitedCapacity = false;
  23. /**
  24. * Getter for the length of the Cable.
  25. * Always calculate never saved.
  26. * Needs to be profiled whats better.
  27. * @return
  28. */
  29. public float getLength() {
  30. return a.getPosition().getDistance(b.getPosition());
  31. }
  32. /**
  33. * Constructor with a user-defined max. capacity
  34. *
  35. * @param a Source
  36. * @param b Destination
  37. * @param maxCap Maximum Capacity
  38. */
  39. public Edge(AbstractCanvasObject a, AbstractCanvasObject b, float maxCap) {
  40. setA(a);
  41. setB(b);
  42. this.maxCapacity = maxCap;
  43. }
  44. /**
  45. * Clone Constructor
  46. *
  47. * @param a Source
  48. * @param b Destination
  49. * @param maxCap Maximum Capacity
  50. */
  51. public Edge(Edge other) {
  52. setA(other.a);
  53. setB(other.b);
  54. this.maxCapacity = other.maxCapacity;
  55. }
  56. /**
  57. * Getter for the Source.
  58. *
  59. * @return the a
  60. */
  61. public AbstractCanvasObject getA() {
  62. return a;
  63. }
  64. /**
  65. * Set the Source to a new one.
  66. *
  67. * @param a the a to set
  68. */
  69. public void setA(AbstractCanvasObject a) {
  70. this.a = a;
  71. }
  72. /**
  73. * Getter for the destination.
  74. *
  75. * @return the b
  76. */
  77. public AbstractCanvasObject getB() {
  78. return b;
  79. }
  80. /**
  81. * Set the Destination to a new one.
  82. *
  83. * @param b the b to set
  84. */
  85. public void setB(AbstractCanvasObject b) {
  86. this.b = b;
  87. }
  88. /**
  89. * Check if a CpsEdge is Connected to the AbstractCpsObject.
  90. * @param holonObject the AbstractCpsObject to check.
  91. * @return true if either a or b is the AbstractCpsObject to check.
  92. */
  93. public boolean isConnectedTo(AbstractCanvasObject holonObject)
  94. {
  95. return (holonObject.equals(a) || holonObject.equals(b));
  96. }
  97. @Override
  98. public String toString(){
  99. String A = (a == null) ? "null" : a.getName() + "[" + a.getId()+ "]";
  100. String B = (b == null) ? "null" : b.getName() + "[" + b.getId()+ "]";
  101. return "CpsEdge: " + A + " to " + B;
  102. }
  103. public boolean isBreakedManuel() {
  104. return breakedManuel;
  105. }
  106. public void setBreakedManuel(boolean breakedManuel) {
  107. this.breakedManuel = breakedManuel;
  108. }
  109. public boolean isUnlimitedCapacity() {
  110. return unlimitedCapacity;
  111. }
  112. public void setUnlimitedCapacity(boolean unlimitedCapacity) {
  113. this.unlimitedCapacity = unlimitedCapacity;
  114. }
  115. /*
  116. * STATE
  117. */
  118. public enum EdgeState{
  119. Working, Burned
  120. }
  121. private EdgeState state = initState();
  122. private float flowEnergy = 0;
  123. public EdgeState getState() {
  124. return state;
  125. }
  126. //TODO(Tom 2021-12-3): public -> package
  127. public void reset() {
  128. state = initState();
  129. }
  130. private EdgeState initState() {
  131. if (breakedManuel) {
  132. return EdgeState.Burned;
  133. }else {
  134. return EdgeState.Working;
  135. }
  136. }
  137. public float getActualFlow() {
  138. return flowEnergy;
  139. }
  140. public float getEnergyFromConneted() {
  141. float energy = 0.0f;
  142. if(getA() instanceof HolonObject hO && hO.getActualEnergy() > 0) {
  143. energy += hO.getActualEnergy();
  144. }
  145. if(getB() instanceof HolonObject hO && hO.getActualEnergy() > 0) {
  146. energy += hO.getActualEnergy();
  147. }
  148. return energy;
  149. }
  150. //TODO(Tom 2021-12-3): public -> package
  151. public void setActualFlow(float energyToSupplyInTheNetwork) {
  152. flowEnergy = energyToSupplyInTheNetwork;
  153. }
  154. //TODO(Tom 2021-12-3): public -> package
  155. public void setState(EdgeState state) {
  156. this.state = state;
  157. }
  158. }