CpsEdge.java 5.2 KB

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