Edge.java 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. public EdgeMode mode = EdgeMode.Normal;
  7. // Source
  8. AbstractCanvasObject a;
  9. // Destination
  10. AbstractCanvasObject b;
  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 "Edge: " + A + " to " + B;
  88. }
  89. public EdgeState getState() {
  90. return state;
  91. }
  92. public void setState(EdgeState state) {
  93. this.state = state;
  94. }
  95. void reset() {
  96. state = initState();
  97. }
  98. private EdgeState initState() {
  99. if (mode == EdgeMode.Disconnected) {
  100. return EdgeState.Burned;
  101. }
  102. return EdgeState.Working;
  103. }
  104. public float getActualFlow() {
  105. return flowEnergy;
  106. }
  107. public void setActualFlow(float energyToSupplyInTheNetwork) {
  108. flowEnergy = energyToSupplyInTheNetwork;
  109. }
  110. public float getEnergyFromConneted() {
  111. float energy = 0.0f;
  112. if (getA() instanceof HolonObject hO && hO.getActualEnergy() > 0) {
  113. energy += hO.getActualEnergy();
  114. }
  115. if (getB() instanceof HolonObject hO && hO.getActualEnergy() > 0) {
  116. energy += hO.getActualEnergy();
  117. }
  118. return energy;
  119. }
  120. public enum EdgeMode {
  121. Normal, Unlimited, Disconnected
  122. }
  123. /*
  124. * STATE
  125. */
  126. public enum EdgeState {
  127. Working, Burned
  128. }
  129. }