HolonObject.java 16 KB

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