HolonObject.java 17 KB

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