CpsEdge.java 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. * Is true when a Connection to an Group Object, is connected inside the Group.
  30. * Is false when not
  31. */
  32. @Expose
  33. boolean isConnected;
  34. // for internal use --> flow of electricity (simulation state)
  35. ArrayList<Integer> tags;
  36. // for internal use --> flow of electricity (simulation state)
  37. ArrayList<Integer> pseudoTags;
  38. // Source
  39. @Expose
  40. AbstractCpsObject a;
  41. // Destination
  42. @Expose
  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 = true;
  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(boolean simMode) {
  128. if (flow > maxCapacity && maxCapacity != -1) {
  129. isWorking = false;
  130. } else {
  131. if (!simMode && (flow <= maxCapacity || maxCapacity == -1)) {
  132. isWorking = true;
  133. }
  134. }
  135. }
  136. /**
  137. * Getter for the Source.
  138. *
  139. * @return the a
  140. */
  141. public AbstractCpsObject getA() {
  142. return a;
  143. }
  144. /**
  145. * Set the Source to a new one.
  146. *
  147. * @param a
  148. * the a to set
  149. */
  150. public void setA(AbstractCpsObject a) {
  151. this.a = a;
  152. }
  153. /**
  154. * Getter for the destination.
  155. *
  156. * @return the b
  157. */
  158. public AbstractCpsObject getB() {
  159. return b;
  160. }
  161. /**
  162. * Set the Destination to a new one.
  163. *
  164. * @param b
  165. * the b to set
  166. */
  167. public void setB(AbstractCpsObject b) {
  168. this.b = b;
  169. }
  170. /**
  171. * Set the state manually to a new one.
  172. *
  173. * @param state
  174. * the state
  175. */
  176. public void setState(boolean state) {
  177. isWorking = state;
  178. }
  179. /**
  180. * Getter for the status.
  181. *
  182. * @return the state
  183. */
  184. public boolean getState() {
  185. return isWorking;
  186. }
  187. /**
  188. * set the tags into a new set of tags.
  189. *
  190. * @param tags
  191. * tags for this edge
  192. */
  193. public void setTags(ArrayList<Integer> tags) {
  194. this.tags = tags;
  195. }
  196. /**
  197. * Getter for the ArrayList of tags.
  198. *
  199. * @return tags tags for this edge
  200. */
  201. public ArrayList<Integer> getTags() {
  202. return tags;
  203. }
  204. /**
  205. * Add a new tag to the ArrayList.
  206. *
  207. * @param tag tag for the ArrayList
  208. */
  209. public void addTag(int tag) {
  210. if(!tags.contains(tag)){
  211. tags.add(tag);
  212. }
  213. }
  214. /**
  215. * checks wether tags contains all given tags
  216. * @param toCheck
  217. * tags that are checked
  218. * @return
  219. * true if all are contained in tags, false otherwise
  220. */
  221. public boolean containsTags(ArrayList<Integer> list, ArrayList<Integer> toCheck){
  222. if(toCheck.size() == 0){
  223. return true;
  224. }else{
  225. for(Integer i: toCheck){
  226. if(!(list.contains(i))){
  227. return false;
  228. }
  229. }
  230. return true;
  231. }
  232. }
  233. public void addPseudoTag(int tag){
  234. if(!pseudoTags.contains(tag)){
  235. pseudoTags.add(tag);
  236. }
  237. }
  238. public void setPseudoTag(ArrayList<Integer> list){
  239. pseudoTags = list;
  240. }
  241. public ArrayList<Integer> getPseudoTags(){
  242. return pseudoTags;
  243. }
  244. public void recalculateTags(){
  245. for(Integer i: pseudoTags){
  246. if(!tags.contains(i)){
  247. tags.add(i);
  248. }
  249. }
  250. }
  251. public boolean getConnected(){
  252. return isConnected;
  253. }
  254. public void setConnected(boolean state){
  255. isConnected = state;
  256. }
  257. }