HolonObject.java 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. package classes;
  2. import java.awt.Color;
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import ui.controller.MultiPurposeController;
  6. /**
  7. * The class HolonObject represents any Object on the system which capability of
  8. * injecting or consuming energy on the network, for instance a house or a power
  9. * plant.
  10. *
  11. * @author Gruppe14
  12. *
  13. */
  14. public class HolonObject extends AbstractCpsObject {
  15. /*
  16. * Color of the actual state (red = no supplied, yellow = partially supplied
  17. * and green = supplied)
  18. */
  19. private Color stateColor;
  20. /* Array of all consumers */
  21. private ArrayList<HolonElement> elements;
  22. /* Array of all Indices of Elements */
  23. private HashMap<String, Integer> eleIdx;
  24. /* Total of consumption */
  25. private float currentEnergy;
  26. /*
  27. * 0 = no energy, 1 = not supplied, 2 = supplied, 3 producer
  28. */
  29. private int state = 0;
  30. /**
  31. * Constructor Set by default the name of the object equals to the category
  32. * name, until the user changes it.
  33. *
  34. * @param objName
  35. * name of the Object
  36. */
  37. public HolonObject(String objName) {
  38. super(objName);
  39. setElements(new ArrayList<HolonElement>());
  40. setEleIdx(new HashMap<String, Integer>());
  41. setState();
  42. }
  43. /**
  44. * Contructor of a copy of an Object.
  45. *
  46. * @param obj
  47. * object to be copied
  48. */
  49. public HolonObject(AbstractCpsObject obj) {
  50. super(obj);
  51. setEleIdx(MultiPurposeController.copyHashMap(((HolonObject) obj).getEleIdx()));
  52. setElements(copyElements(((HolonObject) obj).getElements()));
  53. setState();
  54. }
  55. /**
  56. * sets the State, wether object is a producer, zero Energy, supplied or
  57. * not.
  58. */
  59. public void setState() {
  60. if (getCurrentEnergy() > 0) {
  61. setState(3);
  62. stateColor = Color.lightGray;
  63. } else {
  64. if (getCurrentEnergy() == 0) {
  65. setState(0);
  66. stateColor = Color.WHITE;
  67. } else {
  68. if (checkIfPartiallySupplied(0)) {
  69. stateColor = Color.yellow;
  70. } else {
  71. stateColor = new Color(230, 120, 100);
  72. }
  73. }
  74. }
  75. }
  76. /**
  77. * Getter for all Elements in the HolonObject.
  78. *
  79. * @return the elements ArrayList
  80. */
  81. public ArrayList<HolonElement> getElements() {
  82. return elements;
  83. }
  84. /**
  85. * Set a new ArrayList with HolonElements into the HolonObject.
  86. *
  87. * @param elements
  88. * the elements to set
  89. */
  90. public void setElements(ArrayList<HolonElement> elements) {
  91. this.elements = elements;
  92. }
  93. /**
  94. * add an Element to the Object.
  95. *
  96. * @param element
  97. * the Element to add
  98. */
  99. public void addElements(HolonElement element) {
  100. elements.add(element);
  101. }
  102. /**
  103. * Doesn't take into account which timestep is watched, calculates the max
  104. * values.
  105. *
  106. * @return the currentEnergy
  107. */
  108. public float getCurrentEnergy() {
  109. float temp = 0;
  110. for (HolonElement e : getElements()) {
  111. if (e.getActive()) {
  112. temp = temp + e.getTotalEnergy();
  113. }
  114. }
  115. currentEnergy = temp;
  116. return currentEnergy;
  117. }
  118. /**
  119. * Getter for the current energy at a given timestep.
  120. *
  121. * @param x
  122. * timestep
  123. * @return corresponding energy
  124. */
  125. public float getCurrentEnergyAtTimeStep(int x) {
  126. float temp = 0;
  127. for (HolonElement e : getElements()) {
  128. if (e.getActive()) {
  129. temp = temp + e.getTotalEnergyAtTimeStep(x);
  130. }
  131. }
  132. currentEnergy = temp;
  133. return currentEnergy;
  134. }
  135. /**
  136. * deletes Element at a given index.
  137. *
  138. * @param idx
  139. * index
  140. */
  141. public void deleteElement(int idx) {
  142. elements.remove(idx);
  143. }
  144. /**
  145. * String of all consumers in this HolonObject.
  146. *
  147. * @return all the names of this HolonObject separated by "," each object
  148. */
  149. public String toStringElements() {
  150. String objString = "Empty";
  151. for (HolonElement e : elements) {
  152. if (objString == "Empty") {
  153. objString = e.getEleName();
  154. } else {
  155. objString = objString + ", " + e.getEleName();
  156. }
  157. }
  158. return objString;
  159. }
  160. /**
  161. * Getter index of all elements in the HolonObject.
  162. *
  163. * @return the eleIdx
  164. */
  165. public HashMap<String, Integer> getEleIdx() {
  166. return eleIdx;
  167. }
  168. /**
  169. * Set the indexes of all elements.
  170. *
  171. * @param eleIdx
  172. * the eleIdx to set
  173. */
  174. public void setEleIdx(HashMap<String, Integer> eleIdx) {
  175. this.eleIdx = eleIdx;
  176. }
  177. /**
  178. * Copy all Elements into a New Array.
  179. *
  180. * @param arr
  181. * to copy
  182. * @return the copy of arr
  183. */
  184. public ArrayList<HolonElement> copyElements(ArrayList<HolonElement> arr) {
  185. ArrayList<HolonElement> newArr = new ArrayList<>();
  186. for (HolonElement t : arr) {
  187. newArr.add(new HolonElement(t));
  188. }
  189. return newArr;
  190. }
  191. /**
  192. * Get the state of the Object.
  193. *
  194. * @return state the State of the Element
  195. */
  196. public int getState() {
  197. return this.state;
  198. }
  199. /**
  200. * Set the state of the Object.
  201. *
  202. * @param state
  203. * boolean if the Object is fully supplied
  204. */
  205. public void setState(int state) {
  206. this.state = state;
  207. switch (state) {
  208. case 0:
  209. stateColor = Color.WHITE;
  210. break;
  211. case 1:
  212. stateColor = new Color(230, 120, 100);
  213. break;
  214. case 2:
  215. stateColor = Color.GREEN;
  216. break;
  217. case 3:
  218. stateColor = Color.lightGray;
  219. break;
  220. case 4:
  221. stateColor = Color.YELLOW;
  222. }
  223. }
  224. /**
  225. * Search for the element with the name.
  226. *
  227. * @param name
  228. * name of the object to be searched
  229. * @return the searched HolonElement
  230. */
  231. public HolonElement searchElement(String name) {
  232. HolonElement ele = null;
  233. for (HolonElement e : getElements()) {
  234. if (e.getEleName().equals(name)) {
  235. ele = e;
  236. }
  237. }
  238. return ele;
  239. }
  240. /**
  241. * Search for the element with the id.
  242. *
  243. * @param id
  244. * id of the element to be founded
  245. * @return the element
  246. */
  247. public HolonElement searchElementById(int id) {
  248. HolonElement ele = null;
  249. for (HolonElement e : getElements()) {
  250. if (e.getId() == id) {
  251. ele = e;
  252. }
  253. }
  254. return ele;
  255. }
  256. /**
  257. * Check if Partially Supplied.
  258. *
  259. * @param x
  260. * current Iteration
  261. * @return boolean is partially supplied
  262. */
  263. public boolean checkIfPartiallySupplied(int x) {
  264. if (getElements().size() == 0) {
  265. return false;
  266. }
  267. float minConsum = getElements().get(0).getTotalEnergyAtTimeStep(x);
  268. float prod = 0;
  269. for (HolonElement e : getElements()) {
  270. if (e.getActive()) {
  271. if (e.getTotalEnergyAtTimeStep(x) > 0) {
  272. prod = prod + e.getTotalEnergyAtTimeStep(x);
  273. }
  274. if (minConsum < 0 && (e.getTotalEnergyAtTimeStep(x) > minConsum && e.getTotalEnergyAtTimeStep(x) < 0)) {
  275. minConsum = e.getTotalEnergyAtTimeStep(x);
  276. } else if (minConsum >= 0 && e.getTotalEnergyAtTimeStep(x) < minConsum) {
  277. minConsum = e.getTotalEnergyAtTimeStep(x);
  278. }
  279. }
  280. }
  281. // System.out.println("minCons: " + minConsum + " prod: " + prod);
  282. if (minConsum < 0 && prod >= -minConsum) {
  283. return true;
  284. } else {
  285. return false;
  286. }
  287. }
  288. /**
  289. * Set the State Color.
  290. *
  291. * @param color
  292. * the Color
  293. */
  294. public void setColor(Color color) {
  295. stateColor = color;
  296. }
  297. /**
  298. * Get the Color.
  299. *
  300. * @return stateColor the Color
  301. */
  302. public Color getColor() {
  303. return stateColor;
  304. }
  305. }