Edge.java 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. package classes;
  2. import com.google.gson.annotations.Expose;
  3. import java.util.ArrayList;
  4. /**
  5. * The class "CpsEdge" represents the connections on the GUI. Each connection
  6. * contains a max. capacity, a flow, a status (isWorking), tags (for internal
  7. * use of electricity flow), source and destination
  8. *
  9. * @author Gruppe14
  10. */
  11. public class Edge {
  12. // Max. capacity of the Edge, if flow is greater than the status --> is
  13. // Working would be false
  14. @Expose
  15. private float maxCapacity;
  16. ArrayList<Integer> tags;
  17. // for internal use --> flow of electricity (simulation state)
  18. ArrayList<Integer> pseudoTags;
  19. // Source
  20. AbstractCanvasObject a;
  21. // Destination
  22. AbstractCanvasObject b;
  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 (float)a.getPosition().Distance(b.getPosition());
  31. }
  32. @Expose
  33. private boolean breakedManuel = false;
  34. @Expose
  35. private boolean unlimitedCapacity = false;
  36. /**
  37. * Constructor without max. capacity (by default as 100)
  38. *
  39. * @param a Source
  40. * @param b Destination
  41. */
  42. public Edge(AbstractCanvasObject a, AbstractCanvasObject b) {
  43. setA(a);
  44. setB(b);
  45. if(a == null) {
  46. System.out.println("A == NULL");
  47. }
  48. if(a == null) {
  49. System.out.println("B == NULL");
  50. }
  51. this.maxCapacity = 100;
  52. pseudoTags = new ArrayList<>();
  53. }
  54. /**
  55. * Constructor with a user-defined max. capacity
  56. *
  57. * @param a Source
  58. * @param b Destination
  59. * @param maxCap Maximum Capacity
  60. */
  61. public Edge(AbstractCanvasObject a, AbstractCanvasObject b, float maxCap) {
  62. setA(a);
  63. setB(b);
  64. this.maxCapacity = maxCap;
  65. pseudoTags = new ArrayList<>();
  66. }
  67. /**
  68. * Getter for the max. capacity
  69. *
  70. * @return the capacity
  71. */
  72. public float getCapacity() {
  73. return maxCapacity;
  74. }
  75. /**
  76. * Setter for the max. capacity
  77. *
  78. * @param cap the Capacity to set
  79. */
  80. public void setCapacity(float cap) {
  81. this.maxCapacity = cap;
  82. }
  83. /**
  84. * Getter for the Source.
  85. *
  86. * @return the a
  87. */
  88. public AbstractCanvasObject getA() {
  89. return a;
  90. }
  91. /**
  92. * Set the Source to a new one.
  93. *
  94. * @param a the a to set
  95. */
  96. public void setA(AbstractCanvasObject a) {
  97. this.a = a;
  98. }
  99. /**
  100. * Getter for the destination.
  101. *
  102. * @return the b
  103. */
  104. public AbstractCanvasObject getB() {
  105. return b;
  106. }
  107. /**
  108. * Set the Destination to a new one.
  109. *
  110. * @param b the b to set
  111. */
  112. public void setB(AbstractCanvasObject b) {
  113. this.b = b;
  114. }
  115. /**
  116. * Getter for the ArrayList of tags.
  117. *
  118. * @return tags tags for this edge
  119. */
  120. public ArrayList<Integer> getTags() {
  121. return tags;
  122. }
  123. /**
  124. * set the tags into a new set of tags.
  125. *
  126. * @param tags tags for this edge
  127. */
  128. public void setTags(ArrayList<Integer> tags) {
  129. this.tags = tags;
  130. }
  131. /**
  132. * Add a new tag to the ArrayList.
  133. *
  134. * @param tag tag for the ArrayList
  135. */
  136. public void addTag(int tag) {
  137. if (!tags.contains(tag)) {
  138. tags.add(tag);
  139. }
  140. }
  141. /**
  142. * checks whether list contains all given tags
  143. *
  144. * @param toCheck tags that are checked
  145. * @param list list to be checked
  146. * @return true if all tags in toCheck are contained in list, false otherwise
  147. */
  148. public boolean containsTags(ArrayList<Integer> list, ArrayList<Integer> toCheck) {
  149. if (toCheck.size() == 0) {
  150. return true;
  151. } else {
  152. for (Integer i : toCheck) {
  153. if (!(list.contains(i))) {
  154. return false;
  155. }
  156. }
  157. return true;
  158. }
  159. }
  160. public void addPseudoTag(int tag) {
  161. if (!pseudoTags.contains(tag)) {
  162. pseudoTags.add(tag);
  163. }
  164. }
  165. public void setPseudoTag(ArrayList<Integer> list) {
  166. pseudoTags = list;
  167. }
  168. public ArrayList<Integer> getPseudoTags() {
  169. return pseudoTags;
  170. }
  171. public void recalculateTags() {
  172. for (Integer i : pseudoTags) {
  173. if (!tags.contains(i)) {
  174. tags.add(i);
  175. }
  176. }
  177. }
  178. /**
  179. * Check if a CpsEdge is Connected to the AbstractCpsObject.
  180. * @param holonObject the AbstractCpsObject to check.
  181. * @return true if either a or b is the AbstractCpsObject to check.
  182. */
  183. public boolean isConnectedTo(AbstractCanvasObject holonObject)
  184. {
  185. return (holonObject.equals(a) || holonObject.equals(b));
  186. }
  187. @Override
  188. public String toString(){
  189. String A = (a == null) ? "null" : a.getName() + "[" + a.getId()+ "]";
  190. String B = (b == null) ? "null" : b.getName() + "[" + b.getId()+ "]";
  191. return "CpsEdge: " + A + " to " + B;
  192. }
  193. public boolean isBreakedManuel() {
  194. return breakedManuel;
  195. }
  196. public void setBreakedManuel(boolean breakedManuel) {
  197. this.breakedManuel = breakedManuel;
  198. }
  199. public boolean isUnlimitedCapacity() {
  200. return unlimitedCapacity;
  201. }
  202. public void setUnlimitedCapacity(boolean unlimitedCapacity) {
  203. this.unlimitedCapacity = unlimitedCapacity;
  204. }
  205. }