Edge.java 3.7 KB

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