CpsEdge.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. // for internal use --> flow of electricity (simulation state)
  25. ArrayList<Integer> tags;
  26. // for internal use --> flow of electricity (simulation state)
  27. ArrayList<Integer> pseudoTags;
  28. // Source
  29. AbstractCpsObject a;
  30. // Destination
  31. AbstractCpsObject b;
  32. /**
  33. * Constructor without max. capacity (by default as 100)
  34. *
  35. * @param a
  36. * Source
  37. * @param b
  38. * Destination
  39. */
  40. public CpsEdge(AbstractCpsObject a, AbstractCpsObject b) {
  41. setA(a);
  42. setB(b);
  43. this.a.addConnection(this);
  44. this.b.addConnection(this);
  45. this.maxCapacity = 100;
  46. flow = 0;
  47. isWorking = true;
  48. pseudoTags = new ArrayList<Integer>();
  49. }
  50. /**
  51. * Constructor with a user-defined max. capacity
  52. *
  53. * @param a
  54. * Source
  55. * @param b
  56. * Destination
  57. * @param maxCap
  58. * Maximum Capacity
  59. */
  60. public CpsEdge(AbstractCpsObject a, AbstractCpsObject b, float maxCap) {
  61. setA(a);
  62. setB(b);
  63. this.a.addConnection(this);
  64. this.b.addConnection(this);
  65. this.maxCapacity = maxCap;
  66. flow = 0;
  67. isWorking = true;
  68. pseudoTags = new ArrayList<Integer>();
  69. }
  70. /**
  71. * Getter for the max. capacity
  72. *
  73. * @return the capacity
  74. */
  75. public float getCapacity() {
  76. return maxCapacity;
  77. }
  78. /**
  79. * Setter for the max. capacity
  80. *
  81. * @param cap
  82. * the Capacity to set
  83. */
  84. public void setCapacity(float cap) {
  85. this.maxCapacity = cap;
  86. }
  87. /**
  88. * Getter fot the current flow.
  89. *
  90. * @return the flow
  91. */
  92. public float getFlow() {
  93. return flow;
  94. }
  95. /**
  96. * Set the flow into a new one.
  97. *
  98. * @param flow
  99. * the flow to set
  100. */
  101. public void setFlow(float flow) {
  102. this.flow = flow;
  103. /**
  104. * if (flow <= maxCapacity || flow == -1) { isWorking = true; } else {
  105. * isWorking = false; state = false;
  106. **/
  107. }
  108. /**
  109. * Calculates the state of the edge (see description of isWorking).
  110. *
  111. * @param simMode
  112. * Simulation Mode
  113. */
  114. public void calculateState(boolean simMode) {
  115. if (flow > maxCapacity && maxCapacity != -1) {
  116. isWorking = false;
  117. } else {
  118. if (!simMode && (flow <= maxCapacity || maxCapacity == -1)) {
  119. isWorking = true;
  120. }
  121. }
  122. }
  123. /**
  124. * Getter for the Source.
  125. *
  126. * @return the a
  127. */
  128. public AbstractCpsObject getA() {
  129. return a;
  130. }
  131. /**
  132. * Set the Source to a new one.
  133. *
  134. * @param a
  135. * the a to set
  136. */
  137. public void setA(AbstractCpsObject a) {
  138. this.a = a;
  139. }
  140. /**
  141. * Getter for the destination.
  142. *
  143. * @return the b
  144. */
  145. public AbstractCpsObject getB() {
  146. return b;
  147. }
  148. /**
  149. * Set the Destination to a new one.
  150. *
  151. * @param b
  152. * the b to set
  153. */
  154. public void setB(AbstractCpsObject b) {
  155. this.b = b;
  156. }
  157. /**
  158. * Set the state manually to a new one.
  159. *
  160. * @param state
  161. * the state
  162. */
  163. public void setState(boolean state) {
  164. isWorking = state;
  165. }
  166. /**
  167. * Getter for the status.
  168. *
  169. * @return the state
  170. */
  171. public boolean getState() {
  172. return isWorking;
  173. }
  174. /**
  175. * set the tags into a new set of tags.
  176. *
  177. * @param tags
  178. * tags for this edge
  179. */
  180. public void setTags(ArrayList<Integer> tags) {
  181. this.tags = tags;
  182. }
  183. /**
  184. * Getter for the ArrayList of tags.
  185. *
  186. * @return tags tags for this edge
  187. */
  188. public ArrayList<Integer> getTags() {
  189. return tags;
  190. }
  191. /**
  192. * Add a new tag to the ArrayList.
  193. *
  194. * @param tag tag for the ArrayList
  195. */
  196. public void addTag(int tag) {
  197. if(!tags.contains(tag)){
  198. tags.add(tag);
  199. }
  200. }
  201. /**
  202. * checks wether tags contains all given tags
  203. * @param toCheck
  204. * tags that are checked
  205. * @return
  206. * true if all are contained in tags, false otherwise
  207. */
  208. public boolean containsTags(ArrayList<Integer> list, ArrayList<Integer> toCheck){
  209. if(toCheck.size() == 0){
  210. return true;
  211. }else{
  212. for(Integer i: toCheck){
  213. if(!(list.contains(i))){
  214. return false;
  215. }
  216. }
  217. return true;
  218. }
  219. }
  220. public void addPseudoTag(int tag){
  221. if(!pseudoTags.contains(tag)){
  222. pseudoTags.add(tag);
  223. }
  224. }
  225. public void setPseudoTag(ArrayList<Integer> list){
  226. pseudoTags = list;
  227. }
  228. public ArrayList<Integer> getPseudoTags(){
  229. return pseudoTags;
  230. }
  231. public void recalculateTags(){
  232. for(Integer i: pseudoTags){
  233. if(!tags.contains(i)){
  234. tags.add(i);
  235. }
  236. }
  237. }
  238. }