Edge.java 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. private float throughput;
  17. ArrayList<Integer> tags;
  18. // for internal use --> flow of electricity (simulation state)
  19. ArrayList<Integer> pseudoTags;
  20. // Source
  21. AbstractCanvasObject a;
  22. // Destination
  23. AbstractCanvasObject b;
  24. /**
  25. * Getter for the length of the Cable.
  26. * Always calculate never saved.
  27. * Needs to be profiled whats better.
  28. * @return
  29. */
  30. public float getLength() {
  31. return (float)a.getPosition().Distance(b.getPosition());
  32. }
  33. @Expose
  34. private boolean breakedManuel = false;
  35. @Expose
  36. private boolean unlimitedCapacity = false;
  37. private boolean burned = false;
  38. /**
  39. * Constructor without max. capacity (by default as 100)
  40. *
  41. * @param a Source
  42. * @param b Destination
  43. */
  44. public Edge(AbstractCanvasObject a, AbstractCanvasObject b) {
  45. setA(a);
  46. setB(b);
  47. if(a == null) {
  48. System.out.println("A == NULL");
  49. }
  50. if(a == null) {
  51. System.out.println("B == NULL");
  52. }
  53. this.maxCapacity = 100;
  54. pseudoTags = new ArrayList<>();
  55. this.throughput = 0f;
  56. }
  57. /**
  58. * Constructor with a user-defined max. capacity
  59. *
  60. * @param a Source
  61. * @param b Destination
  62. * @param maxCap Maximum Capacity
  63. */
  64. public Edge(AbstractCanvasObject a, AbstractCanvasObject b, float maxCap) {
  65. setA(a);
  66. setB(b);
  67. this.maxCapacity = maxCap;
  68. pseudoTags = new ArrayList<>();
  69. this.throughput = 0f;
  70. }
  71. /**
  72. * Getter for the max. capacity
  73. *
  74. * @return the capacity
  75. */
  76. public float getCapacity() {
  77. return maxCapacity;
  78. }
  79. /**
  80. * Setter for the max. capacity
  81. *
  82. * @param cap the Capacity to set
  83. */
  84. public void setCapacity(float cap) {
  85. this.maxCapacity = cap;
  86. }
  87. /**
  88. * Getter for the Source.
  89. *
  90. * @return the a
  91. */
  92. public AbstractCanvasObject getA() {
  93. return a;
  94. }
  95. /**
  96. * Set the Source to a new one.
  97. *
  98. * @param a the a to set
  99. */
  100. public void setA(AbstractCanvasObject a) {
  101. this.a = a;
  102. }
  103. /**
  104. * Getter for the destination.
  105. *
  106. * @return the b
  107. */
  108. public AbstractCanvasObject getB() {
  109. return b;
  110. }
  111. /**
  112. * Set the Destination to a new one.
  113. *
  114. * @param b the b to set
  115. */
  116. public void setB(AbstractCanvasObject b) {
  117. this.b = b;
  118. }
  119. /**
  120. * Getter for the ArrayList of tags.
  121. *
  122. * @return tags tags for this edge
  123. */
  124. public ArrayList<Integer> getTags() {
  125. return tags;
  126. }
  127. /**
  128. * set the tags into a new set of tags.
  129. *
  130. * @param tags tags for this edge
  131. */
  132. public void setTags(ArrayList<Integer> tags) {
  133. this.tags = tags;
  134. }
  135. /**
  136. * Add a new tag to the ArrayList.
  137. *
  138. * @param tag tag for the ArrayList
  139. */
  140. public void addTag(int tag) {
  141. if (!tags.contains(tag)) {
  142. tags.add(tag);
  143. }
  144. }
  145. /**
  146. * checks whether list contains all given tags
  147. *
  148. * @param toCheck tags that are checked
  149. * @param list list to be checked
  150. * @return true if all tags in toCheck are contained in list, false otherwise
  151. */
  152. public boolean containsTags(ArrayList<Integer> list, ArrayList<Integer> toCheck) {
  153. if (toCheck.size() == 0) {
  154. return true;
  155. } else {
  156. for (Integer i : toCheck) {
  157. if (!(list.contains(i))) {
  158. return false;
  159. }
  160. }
  161. return true;
  162. }
  163. }
  164. public void addPseudoTag(int tag) {
  165. if (!pseudoTags.contains(tag)) {
  166. pseudoTags.add(tag);
  167. }
  168. }
  169. public void setPseudoTag(ArrayList<Integer> list) {
  170. pseudoTags = list;
  171. }
  172. public ArrayList<Integer> getPseudoTags() {
  173. return pseudoTags;
  174. }
  175. public void recalculateTags() {
  176. for (Integer i : pseudoTags) {
  177. if (!tags.contains(i)) {
  178. tags.add(i);
  179. }
  180. }
  181. }
  182. /**
  183. * Check if a CpsEdge is Connected to the AbstractCpsObject.
  184. * @param holonObject the AbstractCpsObject to check.
  185. * @return true if either a or b is the AbstractCpsObject to check.
  186. */
  187. public boolean isConnectedTo(AbstractCanvasObject holonObject)
  188. {
  189. return (holonObject.equals(a) || holonObject.equals(b));
  190. }
  191. @Override
  192. public String toString(){
  193. String A = (a == null) ? "null" : a.getName() + "[" + a.getId()+ "]";
  194. String B = (b == null) ? "null" : b.getName() + "[" + b.getId()+ "]";
  195. return "CpsEdge: " + A + " to " + B;
  196. }
  197. public boolean isBreakedManuel() {
  198. return breakedManuel;
  199. }
  200. public void setBreakedManuel(boolean breakedManuel) {
  201. this.breakedManuel = breakedManuel;
  202. }
  203. public boolean isUnlimitedCapacity() {
  204. return unlimitedCapacity;
  205. }
  206. public void setUnlimitedCapacity(boolean unlimitedCapacity) {
  207. this.unlimitedCapacity = unlimitedCapacity;
  208. }
  209. public float getThroughput() {
  210. return throughput;
  211. }
  212. public void setThroughput(float throughput) {
  213. // System.out.println(this+" "+throughput);
  214. this.throughput = throughput;
  215. }
  216. public boolean isBurned() {
  217. return burned;
  218. }
  219. public void setBurned(boolean burned) {
  220. this.burned = burned;
  221. }
  222. }