HolonObject.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  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 = 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. /**
  333. * Calculates the minimumEnergy Needed to turn on atleast on device
  334. * of this HolonObject
  335. * @param x Timestep of the calculation
  336. * @return minEnergy, -inf if no Devices are consuming power
  337. */
  338. public float getMinEnergy(int x) {
  339. if (getElements().size() == 0) {
  340. return Float.NEGATIVE_INFINITY;
  341. }
  342. float minConsum = Float.NEGATIVE_INFINITY;
  343. for (HolonElement e : getElements()) {
  344. if (e.isActive()) {
  345. float overallEnergy = e.getOverallEnergyAtTimeStep(x);
  346. if (minConsum < 0 && (overallEnergy > minConsum && overallEnergy < 0)) {
  347. minConsum = overallEnergy;
  348. }
  349. }
  350. }
  351. return minConsum;
  352. }
  353. /**
  354. * Get the Color.
  355. *
  356. * @return stateColor the Color
  357. */
  358. public Color getColor() {
  359. return stateColor;
  360. }
  361. /**
  362. * Set the State Color.
  363. *
  364. * @param color the Color
  365. */
  366. public void setColor(Color color) {
  367. stateColor = color;
  368. }
  369. /**
  370. * Get the Array Production
  371. */
  372. public float[] getTrackingProd() {
  373. return this.trackingProd;
  374. }
  375. /**
  376. * Set the Array Production
  377. */
  378. public void setTrackingProd(float[] arr) {
  379. this.trackingProd = arr;
  380. }
  381. /**
  382. * Get the Array Consumption
  383. */
  384. public float[] getTrackingCons() {
  385. return this.trackingCons;
  386. }
  387. /**
  388. * Set the Array Consumption
  389. */
  390. public void setTrackingCons(float[] arr) {
  391. this.trackingCons = arr;
  392. }
  393. /**
  394. * Get the Array Consumption
  395. */
  396. public float getTotalFlex() {
  397. return totalFlex;
  398. }
  399. /**
  400. * Set the Array Consumption
  401. */
  402. public void setTotalFlex(float totalFlex) {
  403. this.totalFlex = totalFlex;
  404. }
  405. /**
  406. * Update the totalFlex
  407. */
  408. public void updateTotalFlex() {
  409. float tempFlex = 0;
  410. for (HolonElement e : getElements()) {
  411. if (e.isFlexible()) {
  412. tempFlex += e.getFlexibleEnergyAvailablePerElement() * e.getAmount();
  413. }
  414. }
  415. this.totalFlex = tempFlex;
  416. }
  417. /**
  418. * calculates total flexible Production
  419. */
  420. public float getFlexProd() {
  421. float tempFlex = 0;
  422. for (HolonElement e : getElements()) {
  423. if (e.getFlexibleEnergyAvailablePerElement() > 0) {
  424. tempFlex += e.getFlexibleEnergyAvailablePerElement() * e.getAmount();
  425. }
  426. }
  427. return tempFlex;
  428. }
  429. /**
  430. * calculates total flexible Concumption
  431. */
  432. public float getFlexCons() {
  433. float tempFlex = 0;
  434. for (HolonElement e : getElements()) {
  435. if (e.getFlexibleEnergyAvailablePerElement() < 0) {
  436. tempFlex += e.getFlexibleEnergyAvailablePerElement() * e.getAmount();
  437. }
  438. }
  439. return tempFlex;
  440. }
  441. /**
  442. * If the user track any HolonObject the tracking information will be
  443. * updated. (If the HolonObject enters into the untracked state, the array
  444. * will be reseted)
  445. */
  446. public void updateTrackingInfo() {
  447. float[] tempProd = new float[100];
  448. float[] tempCons = new float[100];
  449. for (int i = 0; i < 100; i++) {
  450. float valueProd = 0;
  451. float valueCons = 0;
  452. for (HolonElement e : getElements()) {
  453. if (e.isActive() && e.getSign() == '+') {
  454. valueProd = valueProd + e.getOverallEnergyAtTimeStep(i);
  455. }
  456. if (e.isActive() && e.getSign() == '-') {
  457. valueCons = valueCons + e.getOverallEnergyAtTimeStep(i);
  458. }
  459. }
  460. tempProd[i] = valueProd;
  461. tempCons[i] = valueCons;
  462. }
  463. this.trackingProd = tempProd;
  464. this.trackingCons = tempCons;
  465. }
  466. public String toString() {
  467. StringBuilder sb = new StringBuilder();
  468. sb.append("[HolonObject: ");
  469. sb.append("id=").append(id)
  470. .append(", name=").append(name)
  471. .append(", state=");
  472. switch (state) {
  473. case NO_ENERGY:
  474. sb.append("NO_ENERGY");
  475. break;
  476. case NOT_SUPPLIED:
  477. sb.append("NOT_SUPPLIED");
  478. break;
  479. case SUPPLIED:
  480. sb.append("SUPPLIED");
  481. break;
  482. case PRODUCER:
  483. sb.append("PRODUCER");
  484. break;
  485. case PARTIALLY_SUPPLIED:
  486. sb.append("PARTIALLY_SUPPLIED");
  487. break;
  488. case OVER_SUPPLIED:
  489. sb.append("OVER_SUPPLIED");
  490. break;
  491. }
  492. sb.append(", elements=[");
  493. for (int i = 0; i < getElements().size(); i++) {
  494. HolonElement el = getElements().get(i);
  495. if (i != 0) {
  496. sb.append(", ");
  497. }
  498. sb.append(el.getEleName());
  499. }
  500. sb.append("]]");
  501. return sb.toString();
  502. }
  503. /**
  504. * @return the {@link #currentSupply}
  505. */
  506. public float getCurrentSupply() {
  507. return currentSupply;
  508. }
  509. /**
  510. * @param currentSupply the {@link #currentSupply} to set
  511. */
  512. public void setCurrentSupply(float currentSupply) {
  513. this.currentSupply = currentSupply;
  514. }
  515. /**
  516. * @return {@link #suppliedPercentage}
  517. */
  518. public float getSuppliedPercentage(){
  519. return suppliedPercentage;
  520. }
  521. }