HolonObject.java 6.8 KB

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