HolonObject.java 16 KB

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