HolonObject.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. package classes;
  2. import com.google.gson.annotations.Expose;
  3. import java.awt.*;
  4. import java.util.ArrayList;
  5. /**
  6. * The class HolonObject represents any Object on the system which capability of
  7. * injecting or consuming energy on the network, for instance a house or a power
  8. * plant.
  9. *
  10. * @author Gruppe14
  11. */
  12. public class HolonObject extends AbstractCpsObject {
  13. /*
  14. * the constants showing a HolonObject's current state
  15. * (whether it needs no energy, whether it is not supplied, fully supplied,
  16. * a producer, partially supplied or over-supplied)
  17. */
  18. public final static int NO_ENERGY = 0;
  19. public final static int NOT_SUPPLIED = 1;
  20. public final static int SUPPLIED = 2;
  21. public final static int PRODUCER = 3;
  22. public final static int PARTIALLY_SUPPLIED = 4;
  23. public final static int OVER_SUPPLIED = 5;
  24. /*
  25. * Color of the actual state (red = no supplied, yellow = partially supplied
  26. * and green = supplied)
  27. */
  28. @Expose
  29. private Color stateColor;
  30. /* Array of all consumers */
  31. private ArrayList<HolonElement> elements;
  32. /* Total of consumption */
  33. @Expose
  34. private float currentEnergy;
  35. /* Array for tracking Production */
  36. private float[] trackingProd;
  37. /* Array for tracking Consumption */
  38. private float[] trackingCons;
  39. /* Total Flexibility */
  40. private float totalFlex;
  41. @Expose
  42. private int state = 0;
  43. /**
  44. * Constructor Set by default the name of the object equals to the category
  45. * name, until the user changes it.
  46. *
  47. * @param objName name of the Object
  48. */
  49. public HolonObject(String objName) {
  50. super(objName);
  51. setElements(new ArrayList<>());
  52. setState();
  53. setTrackingProd(new float[100]);
  54. setTrackingCons(new float[100]);
  55. }
  56. /**
  57. * Contructor of a copy of an Object.
  58. *
  59. * @param obj object to be copied
  60. */
  61. public HolonObject(AbstractCpsObject obj) {
  62. super(obj);
  63. setElements(copyElements(((HolonObject) obj).getElements()));
  64. setState();
  65. setTrackingProd(new float[100]);
  66. setTrackingCons(new float[100]);
  67. }
  68. /**
  69. * sets the State, whether object is a producer, zero Energy, supplied,
  70. * partially or over-supplied
  71. */
  72. public void setState() {
  73. if (getCurrentEnergy() > 0) {
  74. setState(PRODUCER);
  75. stateColor = Color.lightGray;
  76. } else {
  77. if (getCurrentEnergy() == 0) {
  78. setState(NO_ENERGY);
  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 the elements to set
  101. */
  102. public void setElements(ArrayList<HolonElement> elements) {
  103. this.elements = elements;
  104. }
  105. /**
  106. * adds an Element to the Object.
  107. *
  108. * @param element the Element to add
  109. */
  110. public void addElement(HolonElement element) {
  111. elements.add(element);
  112. }
  113. /**
  114. * Doesn't take into account which timestep is watched, calculates the max
  115. * values.
  116. *
  117. * @return the currentEnergy
  118. */
  119. public float getCurrentEnergy() {
  120. float temp = 0;
  121. for (HolonElement e : getElements()) {
  122. if (e.isActive()) {
  123. temp += e.getOverallEnergy();
  124. }
  125. }
  126. currentEnergy = temp;
  127. return currentEnergy;
  128. }
  129. /**
  130. * Getter for the current energy at a given timestep.
  131. *
  132. * @param x timestep
  133. * @return corresponding energy
  134. */
  135. public float getCurrentEnergyAtTimeStep(int x) {
  136. float temp = 0;
  137. for (HolonElement e : getElements()) {
  138. if (e.isActive()) {
  139. temp += e.getOverallEnergyAtTimeStep(x);
  140. }
  141. }
  142. currentEnergy = temp;
  143. return currentEnergy;
  144. }
  145. /**
  146. * Getter for the current energy at a given timestep.
  147. *
  148. * @param x timestep
  149. * @return corresponding energy
  150. */
  151. public float getCurrentEnergyAtTimeStepWithoutFlexiblesAndResetFlexibles(int x) {
  152. float temp = 0;
  153. for (HolonElement e : getElements()) {
  154. if (e.isActive() && !e.isFlexible()) {
  155. temp += e.getOverallEnergyAtTimeStep(x);
  156. } else if (e.isFlexible()) {
  157. e.setEnergyPerElement(0);
  158. }
  159. }
  160. currentEnergy = temp;
  161. return currentEnergy;
  162. }
  163. /**
  164. * deletes Element at a given index.
  165. *
  166. * @param idx index
  167. */
  168. public void deleteElement(int idx) {
  169. elements.remove(idx);
  170. }
  171. /**
  172. * String of all consumers in this HolonObject.
  173. *
  174. * @return all the names of this HolonObject separated by "," each object
  175. */
  176. public String toStringElements() {
  177. String objString = "Empty";
  178. for (HolonElement e : elements) {
  179. if (objString == "Empty") {
  180. objString = e.getEleName();
  181. } else {
  182. objString = objString + ", " + e.getEleName();
  183. }
  184. }
  185. return objString;
  186. }
  187. /**
  188. * Copy all Elements into a New Array.
  189. *
  190. * @param arr to copy
  191. * @return the copy of arr
  192. */
  193. public ArrayList<HolonElement> copyElements(ArrayList<HolonElement> arr) {
  194. ArrayList<HolonElement> newArr = new ArrayList<>();
  195. for (HolonElement t : arr) {
  196. newArr.add(new HolonElement(t));
  197. }
  198. return newArr;
  199. }
  200. /**
  201. * Get the state of the Object.
  202. *
  203. * @return state the State of the Element
  204. */
  205. public int getState() {
  206. return this.state;
  207. }
  208. /**
  209. * Set the state of the Object.
  210. *
  211. * @param state boolean if the Object is fully supplied
  212. */
  213. public void setState(int state) {
  214. this.state = state;
  215. switch (state) {
  216. case NO_ENERGY:
  217. stateColor = Color.WHITE;
  218. break;
  219. case NOT_SUPPLIED:
  220. stateColor = new Color(230, 120, 100);
  221. break;
  222. case SUPPLIED:
  223. stateColor = Color.GREEN;
  224. break;
  225. case PRODUCER:
  226. stateColor = Color.lightGray;
  227. break;
  228. case PARTIALLY_SUPPLIED:
  229. stateColor = Color.YELLOW;
  230. break;
  231. case OVER_SUPPLIED:
  232. // find different purple-tones at
  233. // http://www.rapidtables.com/web/color/purple-color.htm
  234. stateColor = new Color(138, 43, 226);
  235. break;
  236. }
  237. }
  238. /**
  239. * Search for the element with the name.
  240. *
  241. * @param name name of the object to be searched
  242. * @return the searched HolonElement
  243. */
  244. public HolonElement searchElement(String name) {
  245. HolonElement ele = null;
  246. for (HolonElement e : getElements()) {
  247. if (e.getEleName().equals(name)) {
  248. ele = e;
  249. }
  250. }
  251. return ele;
  252. }
  253. /**
  254. * Search for the element with the id.
  255. *
  256. * @param id id of the element to be founded
  257. * @return the element
  258. */
  259. public HolonElement searchElementById(int id) {
  260. HolonElement ele = null;
  261. for (HolonElement e : getElements()) {
  262. if (e.getId() == id) {
  263. ele = e;
  264. }
  265. }
  266. return ele;
  267. }
  268. /**
  269. * Check if Partially Supplied.
  270. *
  271. * @param x current Iteration
  272. * @return boolean is partially supplied
  273. */
  274. public boolean checkIfPartiallySupplied(int x) {
  275. if (getElements().size() == 0) {
  276. return false;
  277. }
  278. float minConsum = getElements().get(0).getOverallEnergyAtTimeStep(x);
  279. float prod = 0;
  280. for (HolonElement e : getElements()) {
  281. if (e.isActive()) {
  282. float overallEnergy = e.getOverallEnergyAtTimeStep(x);
  283. if (overallEnergy > 0) {
  284. prod = prod + overallEnergy;
  285. }
  286. if (minConsum < 0 && (overallEnergy > minConsum && overallEnergy < 0)) {
  287. minConsum = overallEnergy;
  288. } else if (minConsum >= 0 && overallEnergy < minConsum) {
  289. minConsum = overallEnergy;
  290. }
  291. }
  292. }
  293. // System.out.println("minCons: " + minConsum + " prod: " + prod);
  294. if (minConsum < 0 && prod >= -minConsum) {
  295. return true;
  296. } else {
  297. return false;
  298. }
  299. }
  300. /**
  301. * Calculates the minimumEnergy Needed to turn on atleast on device
  302. * of this HolonObject
  303. * @param x Timestep of the calculation
  304. * @return minEnergy, -inf if no Devices are consuming power
  305. */
  306. public float getMinEnergy(int x) {
  307. if (getElements().size() == 0) {
  308. return Float.NEGATIVE_INFINITY;
  309. }
  310. float minConsum = Float.NEGATIVE_INFINITY;
  311. for (HolonElement e : getElements()) {
  312. if (e.isActive()) {
  313. float overallEnergy = e.getOverallEnergyAtTimeStep(x);
  314. if (minConsum < 0 && (overallEnergy > minConsum && overallEnergy < 0)) {
  315. minConsum = overallEnergy;
  316. }
  317. }
  318. }
  319. return minConsum;
  320. }
  321. /**
  322. * Get the Color.
  323. *
  324. * @return stateColor the Color
  325. */
  326. public Color getColor() {
  327. return stateColor;
  328. }
  329. /**
  330. * Set the State Color.
  331. *
  332. * @param color the Color
  333. */
  334. public void setColor(Color color) {
  335. stateColor = color;
  336. }
  337. /**
  338. * Get the Array Production
  339. */
  340. public float[] getTrackingProd() {
  341. return this.trackingProd;
  342. }
  343. /**
  344. * Set the Array Production
  345. */
  346. public void setTrackingProd(float[] arr) {
  347. this.trackingProd = arr;
  348. }
  349. /**
  350. * Get the Array Consumption
  351. */
  352. public float[] getTrackingCons() {
  353. return this.trackingCons;
  354. }
  355. /**
  356. * Set the Array Consumption
  357. */
  358. public void setTrackingCons(float[] arr) {
  359. this.trackingCons = arr;
  360. }
  361. /**
  362. * Get the Array Consumption
  363. */
  364. public float getTotalFlex() {
  365. return totalFlex;
  366. }
  367. /**
  368. * Set the Array Consumption
  369. */
  370. public void setTotalFlex(float totalFlex) {
  371. this.totalFlex = totalFlex;
  372. }
  373. /**
  374. * Update the totalFlex
  375. */
  376. public void updateTotalFlex() {
  377. float tempFlex = 0;
  378. for (HolonElement e : getElements()) {
  379. if (e.isFlexible()) {
  380. tempFlex += e.getFlexibleEnergyAvailablePerElement() * e.getAmount();
  381. }
  382. }
  383. this.totalFlex = tempFlex;
  384. }
  385. /**
  386. * calculates total flexible Production
  387. */
  388. public float getFlexProd() {
  389. float tempFlex = 0;
  390. for (HolonElement e : getElements()) {
  391. if (e.getFlexibleEnergyAvailablePerElement() > 0) {
  392. tempFlex += e.getFlexibleEnergyAvailablePerElement() * e.getAmount();
  393. }
  394. }
  395. return tempFlex;
  396. }
  397. /**
  398. * calculates total flexible Concumption
  399. */
  400. public float getFlexCons() {
  401. float tempFlex = 0;
  402. for (HolonElement e : getElements()) {
  403. if (e.getFlexibleEnergyAvailablePerElement() < 0) {
  404. tempFlex += e.getFlexibleEnergyAvailablePerElement() * e.getAmount();
  405. }
  406. }
  407. return tempFlex;
  408. }
  409. /**
  410. * If the user track any HolonObject the tracking information will be
  411. * updated. (If the HolonObject enters into the untracked state, the array
  412. * will be reseted)
  413. */
  414. public void updateTrackingInfo() {
  415. float[] tempProd = new float[100];
  416. float[] tempCons = new float[100];
  417. for (int i = 0; i < 100; i++) {
  418. float valueProd = 0;
  419. float valueCons = 0;
  420. for (HolonElement e : getElements()) {
  421. if (e.isActive() && e.getSign() == '+') {
  422. valueProd = valueProd + e.getOverallEnergyAtTimeStep(i);
  423. }
  424. if (e.isActive() && e.getSign() == '-') {
  425. valueCons = valueCons + e.getOverallEnergyAtTimeStep(i);
  426. }
  427. }
  428. tempProd[i] = valueProd;
  429. tempCons[i] = valueCons;
  430. }
  431. this.trackingProd = tempProd;
  432. this.trackingCons = tempCons;
  433. }
  434. public String toString() {
  435. StringBuilder sb = new StringBuilder();
  436. sb.append("[HolonObject: ");
  437. sb.append("id=").append(id)
  438. .append(", name=").append(name)
  439. .append(", state=");
  440. switch (state) {
  441. case NO_ENERGY:
  442. sb.append("NO_ENERGY");
  443. break;
  444. case NOT_SUPPLIED:
  445. sb.append("NOT_SUPPLIED");
  446. break;
  447. case SUPPLIED:
  448. sb.append("SUPPLIED");
  449. break;
  450. case PRODUCER:
  451. sb.append("PRODUCER");
  452. break;
  453. case PARTIALLY_SUPPLIED:
  454. sb.append("PARTIALLY_SUPPLIED");
  455. break;
  456. case OVER_SUPPLIED:
  457. sb.append("OVER_SUPPLIED");
  458. break;
  459. }
  460. sb.append(", elements=[");
  461. for (int i = 0; i < getElements().size(); i++) {
  462. HolonElement el = getElements().get(i);
  463. if (i != 0) {
  464. sb.append(", ");
  465. }
  466. sb.append(el.getEleName());
  467. }
  468. sb.append("]]");
  469. return sb.toString();
  470. }
  471. }