Edge.java 6.3 KB

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