CpsEdge.java 5.3 KB

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