HolonObject.java 16 KB

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