CpsEdge.java 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. public static final int CON_UPPER_NODE = 0;
  13. public static final int CON_UPPER_NODE_AND_INSIDE = 1;
  14. public static final int CON_UPPER_NODE_NOT_INSIDE = 2;
  15. public static final float CAPACITY_INFINITE = -1;
  16. public static final float CAPACITY_TO_UPPER_NODE = -2;
  17. // Max. capacity of the Edge, if flow is greater than the status --> is
  18. // Working would be false
  19. @Expose
  20. float maxCapacity;
  21. // for internal use --> flow of electricity (simulation state)
  22. // Current flow of electricity (through the edge)
  23. @Expose
  24. float flow;
  25. /*
  26. * Represents the actual status of the Edge (true = flows electricity; false
  27. * = no flow of electricity) State represents the real status of the edge If
  28. * it breaks --> stay ruin no matter the actual scenario The only way to
  29. * repair it is through manual change (setStateEdge)
  30. */
  31. @Expose
  32. boolean isWorking;
  33. /*
  34. * 0 = not connected to an upper node
  35. * 1 = connected to an uppder node & connected inside
  36. * 2 = connected to an upper node but not inside
  37. * Is false when not
  38. */
  39. @Expose
  40. int isConnected;
  41. ArrayList<Integer> tags;
  42. // for internal use --> flow of electricity (simulation state)
  43. ArrayList<Integer> pseudoTags;
  44. // Source
  45. AbstractCpsObject a;
  46. // Destination
  47. AbstractCpsObject b;
  48. /**
  49. * Constructor without max. capacity (by default as 100)
  50. *
  51. * @param a Source
  52. * @param b Destination
  53. */
  54. public CpsEdge(AbstractCpsObject a, AbstractCpsObject b) {
  55. setA(a);
  56. setB(b);
  57. this.a.addConnection(this);
  58. this.b.addConnection(this);
  59. this.maxCapacity = 100;
  60. flow = 0;
  61. isWorking = true;
  62. pseudoTags = new ArrayList<>();
  63. }
  64. /**
  65. * Constructor with a user-defined max. capacity
  66. *
  67. * @param a Source
  68. * @param b Destination
  69. * @param maxCap Maximum Capacity
  70. */
  71. public CpsEdge(AbstractCpsObject a, AbstractCpsObject b, float maxCap) {
  72. setA(a);
  73. setB(b);
  74. this.a.addConnection(this);
  75. this.b.addConnection(this);
  76. this.maxCapacity = maxCap;
  77. flow = 0;
  78. isWorking = true;
  79. isConnected = CON_UPPER_NODE;
  80. pseudoTags = new ArrayList<>();
  81. }
  82. /**
  83. * Getter for the max. capacity
  84. *
  85. * @return the capacity
  86. */
  87. public float getCapacity() {
  88. return maxCapacity;
  89. }
  90. /**
  91. * Setter for the max. capacity
  92. *
  93. * @param cap the Capacity to set
  94. */
  95. public void setCapacity(float cap) {
  96. this.maxCapacity = cap;
  97. }
  98. /**
  99. * Getter fot the current flow.
  100. *
  101. * @return the flow
  102. */
  103. public float getFlow() {
  104. return flow;
  105. }
  106. /**
  107. * Set the flow into a new one.
  108. *
  109. * @param flow the flow to set
  110. */
  111. public void setFlow(float flow) {
  112. this.flow = flow;
  113. /**
  114. * if (flow <= maxCapacity || flow == -1) { isWorking = true; } else {
  115. * isWorking = false; state = false;
  116. **/
  117. }
  118. /**
  119. * Calculates the state of the edge (see description of isWorking).
  120. */
  121. public void calculateState() {
  122. if (flow > maxCapacity
  123. && maxCapacity != CAPACITY_INFINITE
  124. && maxCapacity != CAPACITY_TO_UPPER_NODE) {
  125. isWorking = false;
  126. flow = 0;
  127. }
  128. }
  129. /**
  130. * Getter for the Source.
  131. *
  132. * @return the a
  133. */
  134. public AbstractCpsObject getA() {
  135. return a;
  136. }
  137. /**
  138. * Set the Source to a new one.
  139. *
  140. * @param a the a to set
  141. */
  142. public void setA(AbstractCpsObject a) {
  143. this.a = a;
  144. }
  145. /**
  146. * Getter for the destination.
  147. *
  148. * @return the b
  149. */
  150. public AbstractCpsObject getB() {
  151. return b;
  152. }
  153. /**
  154. * Set the Destination to a new one.
  155. *
  156. * @param b the b to set
  157. */
  158. public void setB(AbstractCpsObject b) {
  159. this.b = b;
  160. }
  161. /**
  162. * Getter for the status.
  163. *
  164. * @return the state
  165. */
  166. public boolean isWorking() {
  167. return isWorking;
  168. }
  169. /**
  170. * Set the state manually to a new one.
  171. *
  172. * @param state the state
  173. */
  174. public void setWorkingState(boolean state) {
  175. isWorking = state;
  176. }
  177. /**
  178. * Getter for the ArrayList of tags.
  179. *
  180. * @return tags tags for this edge
  181. */
  182. public ArrayList<Integer> getTags() {
  183. return tags;
  184. }
  185. /**
  186. * set the tags into a new set of tags.
  187. *
  188. * @param tags tags for this edge
  189. */
  190. public void setTags(ArrayList<Integer> tags) {
  191. this.tags = tags;
  192. }
  193. /**
  194. * Add a new tag to the ArrayList.
  195. *
  196. * @param tag tag for the ArrayList
  197. */
  198. public void addTag(int tag) {
  199. if (!tags.contains(tag)) {
  200. tags.add(tag);
  201. }
  202. }
  203. /**
  204. * checks whether list contains all given tags
  205. *
  206. * @param toCheck tags that are checked
  207. * @param list list to be checked
  208. * @return true if all tags in toCheck are contained in list, false otherwise
  209. */
  210. public boolean containsTags(ArrayList<Integer> list, ArrayList<Integer> toCheck) {
  211. if (toCheck.size() == 0) {
  212. return true;
  213. } else {
  214. for (Integer i : toCheck) {
  215. if (!(list.contains(i))) {
  216. return false;
  217. }
  218. }
  219. return true;
  220. }
  221. }
  222. public void addPseudoTag(int tag) {
  223. if (!pseudoTags.contains(tag)) {
  224. pseudoTags.add(tag);
  225. }
  226. }
  227. public void setPseudoTag(ArrayList<Integer> list) {
  228. pseudoTags = list;
  229. }
  230. public ArrayList<Integer> getPseudoTags() {
  231. return pseudoTags;
  232. }
  233. public void recalculateTags() {
  234. for (Integer i : pseudoTags) {
  235. if (!tags.contains(i)) {
  236. tags.add(i);
  237. }
  238. }
  239. }
  240. public int getConnected() {
  241. return isConnected;
  242. }
  243. public void setConnected(int state) {
  244. isConnected = state;
  245. if (state == CON_UPPER_NODE_AND_INSIDE) {
  246. //maxCapacity = CAPACITY_TO_UPPER_NODE;
  247. }
  248. }
  249. /**
  250. * Check if a CpsEdge is Connected to the AbstractCpsObject.
  251. * @param holonObject the AbstractCpsObject to check.
  252. * @return true if either a or b is the AbstractCpsObject to check.
  253. */
  254. public boolean isConnectedTo(AbstractCpsObject holonObject)
  255. {
  256. return (holonObject.equals(a) || holonObject.equals(b));
  257. }
  258. @Override
  259. public String toString(){
  260. String A = (a == null) ? "null" : a.getName() + "[" + a.getId()+ "]";
  261. String B = (b == null) ? "null" : b.getName() + "[" + b.getId()+ "]";
  262. return "CpsEdge: " + A + " to " + B;
  263. }
  264. }