HolonObject.java 16 KB

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