HolonObject.java 16 KB

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