CpsEdge.java 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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 CpsEdge {
  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. AbstractCpsObject a;
  21. // Destination
  22. AbstractCpsObject 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 CpsEdge(AbstractCpsObject a, AbstractCpsObject b) {
  43. setA(a);
  44. setB(b);
  45. this.a.addConnection(this);
  46. this.b.addConnection(this);
  47. this.maxCapacity = 100;
  48. pseudoTags = new ArrayList<>();
  49. }
  50. /**
  51. * Constructor with a user-defined max. capacity
  52. *
  53. * @param a Source
  54. * @param b Destination
  55. * @param maxCap Maximum Capacity
  56. */
  57. public CpsEdge(AbstractCpsObject a, AbstractCpsObject b, float maxCap) {
  58. setA(a);
  59. setB(b);
  60. this.a.addConnection(this);
  61. this.b.addConnection(this);
  62. this.maxCapacity = maxCap;
  63. pseudoTags = new ArrayList<>();
  64. }
  65. /**
  66. * Getter for the max. capacity
  67. *
  68. * @return the capacity
  69. */
  70. public float getCapacity() {
  71. return maxCapacity;
  72. }
  73. /**
  74. * Setter for the max. capacity
  75. *
  76. * @param cap the Capacity to set
  77. */
  78. public void setCapacity(float cap) {
  79. this.maxCapacity = cap;
  80. }
  81. /**
  82. * Getter for the Source.
  83. *
  84. * @return the a
  85. */
  86. public AbstractCpsObject getA() {
  87. return a;
  88. }
  89. /**
  90. * Set the Source to a new one.
  91. *
  92. * @param a the a to set
  93. */
  94. public void setA(AbstractCpsObject a) {
  95. this.a = a;
  96. }
  97. /**
  98. * Getter for the destination.
  99. *
  100. * @return the b
  101. */
  102. public AbstractCpsObject getB() {
  103. return b;
  104. }
  105. /**
  106. * Set the Destination to a new one.
  107. *
  108. * @param b the b to set
  109. */
  110. public void setB(AbstractCpsObject b) {
  111. this.b = b;
  112. }
  113. /**
  114. * Getter for the ArrayList of tags.
  115. *
  116. * @return tags tags for this edge
  117. */
  118. public ArrayList<Integer> getTags() {
  119. return tags;
  120. }
  121. /**
  122. * set the tags into a new set of tags.
  123. *
  124. * @param tags tags for this edge
  125. */
  126. public void setTags(ArrayList<Integer> tags) {
  127. this.tags = tags;
  128. }
  129. /**
  130. * Add a new tag to the ArrayList.
  131. *
  132. * @param tag tag for the ArrayList
  133. */
  134. public void addTag(int tag) {
  135. if (!tags.contains(tag)) {
  136. tags.add(tag);
  137. }
  138. }
  139. /**
  140. * checks whether list contains all given tags
  141. *
  142. * @param toCheck tags that are checked
  143. * @param list list to be checked
  144. * @return true if all tags in toCheck are contained in list, false otherwise
  145. */
  146. public boolean containsTags(ArrayList<Integer> list, ArrayList<Integer> toCheck) {
  147. if (toCheck.size() == 0) {
  148. return true;
  149. } else {
  150. for (Integer i : toCheck) {
  151. if (!(list.contains(i))) {
  152. return false;
  153. }
  154. }
  155. return true;
  156. }
  157. }
  158. public void addPseudoTag(int tag) {
  159. if (!pseudoTags.contains(tag)) {
  160. pseudoTags.add(tag);
  161. }
  162. }
  163. public void setPseudoTag(ArrayList<Integer> list) {
  164. pseudoTags = list;
  165. }
  166. public ArrayList<Integer> getPseudoTags() {
  167. return pseudoTags;
  168. }
  169. public void recalculateTags() {
  170. for (Integer i : pseudoTags) {
  171. if (!tags.contains(i)) {
  172. tags.add(i);
  173. }
  174. }
  175. }
  176. /**
  177. * Check if a CpsEdge is Connected to the AbstractCpsObject.
  178. * @param holonObject the AbstractCpsObject to check.
  179. * @return true if either a or b is the AbstractCpsObject to check.
  180. */
  181. public boolean isConnectedTo(AbstractCpsObject holonObject)
  182. {
  183. return (holonObject.equals(a) || holonObject.equals(b));
  184. }
  185. @Override
  186. public String toString(){
  187. String A = (a == null) ? "null" : a.getName() + "[" + a.getId()+ "]";
  188. String B = (b == null) ? "null" : b.getName() + "[" + b.getId()+ "]";
  189. return "CpsEdge: " + A + " to " + B;
  190. }
  191. public boolean isBreakedManuel() {
  192. return breakedManuel;
  193. }
  194. public void setBreakedManuel(boolean breakedManuel) {
  195. this.breakedManuel = breakedManuel;
  196. }
  197. public boolean isUnlimitedCapacity() {
  198. return unlimitedCapacity;
  199. }
  200. public void setUnlimitedCapacity(boolean unlimitedCapacity) {
  201. this.unlimitedCapacity = unlimitedCapacity;
  202. }
  203. }