Edge.java 5.9 KB

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