CpsEdge.java 4.8 KB

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