HolonObject.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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. @Deprecated
  15. enum State{
  16. NO_ENERGY, NOT_SUPPLIED, SUPPLIED, PRODUCER, PARTIALLY_SUPPLIED, OVER_SUPPLIED
  17. }
  18. @Deprecated
  19. public final static int NO_ENERGY = 0;
  20. @Deprecated
  21. public final static int NOT_SUPPLIED = 1;
  22. @Deprecated
  23. public final static int SUPPLIED = 2;
  24. @Deprecated
  25. public final static int PRODUCER = 3;
  26. @Deprecated
  27. public final static int PARTIALLY_SUPPLIED = 4;
  28. @Deprecated
  29. public final static int OVER_SUPPLIED = 5;
  30. /*
  31. * Color of the actual state (red = no supplied, yellow = partially supplied
  32. * and green = supplied)
  33. */
  34. @Expose
  35. private Color stateColor; //TODO: to draw function
  36. /* Array of all consumers */
  37. private ArrayList<HolonElement> elements;
  38. /* Total of consumption */
  39. @Expose
  40. private float currentEnergy;
  41. /* Array for tracking Production */
  42. private float[] trackingProd;
  43. /* Array for tracking Consumption */
  44. private float[] trackingCons;
  45. /* Total Flexibility */
  46. private float totalFlex;
  47. @Deprecated
  48. @Expose
  49. private int state = 0;
  50. /**
  51. * Energy level that was supplied by other HolonObjects in the current Calculation
  52. */
  53. private float currentSupply;
  54. /** TODO: outsourcing
  55. * Percentage of supplied energy of the energy level needed to supply all elements
  56. */
  57. private float suppliedPercentage;
  58. /**
  59. * Constructor Set by default the name of the object equals to the category
  60. * name, until the user changes it.
  61. *
  62. * @param objName name of the Object
  63. */
  64. public HolonObject(String objName) {
  65. super(objName);
  66. setElements(new ArrayList<>());
  67. setTrackingProd(new float[100]);
  68. setTrackingCons(new float[100]);
  69. }
  70. /**
  71. * Contructor of a copy of an Object.
  72. *
  73. * @param obj object to be copied
  74. */
  75. public HolonObject(AbstractCpsObject obj) {
  76. super(obj);
  77. setElements(copyElements(((HolonObject) obj).getElements()));
  78. setTrackingProd(new float[100]);
  79. setTrackingCons(new float[100]);
  80. }
  81. /**
  82. * Getter for all Elements in the HolonObject.
  83. *
  84. * @return the elements ArrayList
  85. */
  86. public ArrayList<HolonElement> getElements() {
  87. return elements;
  88. }
  89. /**
  90. * Set a new ArrayList with HolonElements into the HolonObject.
  91. *
  92. * @param elements the elements to set
  93. */
  94. public void setElements(ArrayList<HolonElement> elements) {
  95. this.elements = elements;
  96. }
  97. /**
  98. * adds an Element to the Object.
  99. *
  100. * @param element the Element to add
  101. */
  102. public void addElement(HolonElement element) {
  103. elements.add(element);
  104. }
  105. /**
  106. * deletes Element at a given index.
  107. *
  108. * @param idx index
  109. */
  110. public void deleteElement(int idx) {
  111. elements.remove(idx);
  112. }
  113. /**
  114. * Calculate the maximum energy a holonObject can consume from all object that are aktive this moment;
  115. *
  116. * @return the maximal active energy possibel
  117. */
  118. public float getMaxActiveEnergy() {
  119. float temp = 0;
  120. for (HolonElement e : getElements()) {
  121. if (e.isActive()) {
  122. temp += e.getOverallEnergy();
  123. }
  124. }
  125. currentEnergy = temp;
  126. return temp;
  127. }
  128. /**
  129. * Getter for the current energy at a given timestep.
  130. *
  131. * @param x timestep
  132. * @return corresponding energy
  133. */
  134. @Deprecated
  135. public float getCurrentEnergyAtTimeStepWithoutFlexiblesAndResetFlexibles(int x) {
  136. float temp = 0;
  137. for (HolonElement e : getElements()) {
  138. if (e.isActive() && !e.isFlexible()) {
  139. temp += e.getOverallEnergyAtTimeStep(x);
  140. } else if (e.isFlexible()) {
  141. e.setEnergyPerElement(0);
  142. }
  143. }
  144. currentEnergy = temp;
  145. return currentEnergy;
  146. }
  147. /**
  148. * String of all consumers in this HolonObject.
  149. *
  150. * @return all the names of this HolonObject separated by "," each object
  151. */
  152. public String toStringElements() {
  153. String objString = "Empty";
  154. for (HolonElement e : elements) {
  155. if (objString == "Empty") {
  156. objString = e.getEleName();
  157. } else {
  158. objString = objString + ", " + e.getEleName();
  159. }
  160. }
  161. return objString;
  162. }
  163. /**
  164. * Copy all Elements into a New Array.
  165. *
  166. * @param arr to copy
  167. * @return the copy of arr
  168. */
  169. public ArrayList<HolonElement> copyElements(ArrayList<HolonElement> arr) {
  170. ArrayList<HolonElement> newArr = new ArrayList<>();
  171. for (HolonElement t : arr) {
  172. newArr.add(new HolonElement(t));
  173. }
  174. return newArr;
  175. }
  176. /**
  177. * Get the state of the Object.
  178. *
  179. * @return state the State of the Element
  180. */
  181. @Deprecated
  182. public int getState() {
  183. return this.state;
  184. }
  185. /**
  186. * Set the state of the Object.
  187. *
  188. * @param state boolean if the Object is fully supplied
  189. */
  190. @Deprecated
  191. public void setState(int state) {
  192. this.state = state;
  193. switch (state) {
  194. case NO_ENERGY:
  195. stateColor = Color.WHITE;
  196. break;
  197. case NOT_SUPPLIED:
  198. stateColor = new Color(230, 120, 100);
  199. break;
  200. case SUPPLIED:
  201. stateColor = Color.GREEN;
  202. break;
  203. case PRODUCER:
  204. stateColor = Color.lightGray;
  205. break;
  206. case PARTIALLY_SUPPLIED:
  207. stateColor = Color.YELLOW;
  208. break;
  209. case OVER_SUPPLIED:
  210. // find different purple-tones at
  211. // http://www.rapidtables.com/web/color/purple-color.htm
  212. stateColor = new Color(138, 43, 226);
  213. break;
  214. }
  215. }
  216. /**
  217. * Search for the first element with the name.
  218. *
  219. * @param name name of the object to be searched
  220. * @return the searched HolonElement
  221. */
  222. public HolonElement searchElement(String name) {
  223. HolonElement ele = null;
  224. for (HolonElement e : getElements()) {
  225. if (e.getEleName().equals(name)) {
  226. ele = e;
  227. break;
  228. }
  229. }
  230. return ele;
  231. }
  232. /**
  233. * Search for the element with the id.
  234. *
  235. * @param id id of the element to be founded
  236. * @return the element
  237. */
  238. public HolonElement searchElementById(int id) {
  239. HolonElement ele = null;
  240. for (HolonElement e : getElements()) {
  241. if (e.getId() == id) {
  242. ele = e;
  243. break;
  244. }
  245. }
  246. return ele;
  247. }
  248. //New Methods:
  249. /**
  250. * This Method returns the smallest consuming HolonElement that is ACTIVE.
  251. * If the HolonObject has no Consumer its return null.
  252. * @param timestep is the TimeStep to compare the HolonElements.
  253. * @return The smallest consuming HolonElement or null.
  254. */
  255. public HolonElement getMinimumConsumingElement(int timestep){
  256. return getElements().stream().filter(element -> element.isActive() && (element.getEnergyAtTimeStep(timestep) < 0) ).max((lhs,rhs) -> Float.compare(lhs.getEnergyAtTimeStep(timestep), rhs.getEnergyAtTimeStep(timestep))).orElse(null);
  257. }
  258. /**
  259. * This Method returns the smallest consuming HolonElement'Energy that is ACTIVE.
  260. * If the HolonObject has no Consumer its return 0.
  261. * @param timestep is the TimeStep to compare the HolonElements.
  262. * @return The smallest consuming HolonElement or 0.
  263. */
  264. public float getMinimumConsumingElementEnergy(int timestep){
  265. return getElements().stream().filter(element -> element.isActive() && (element.getEnergyAtTimeStep(timestep) < 0) ).map(element -> -element.getEnergyAtTimeStep(timestep)).min((lhs,rhs) ->Float.compare(lhs, rhs)).orElse(0.0f);
  266. }
  267. /**
  268. * This Method returns the Energy of a HolonObject. Its sums all Energies from the HolonElements of the HolonObject that are ACTIVE.
  269. * If the HolonObject have no HolonElement its return 0;
  270. * Is the returned Energy negative then the HolonObject need Energy because its consuming HolonElements need more Energy then the producing HolonElements.
  271. * Is the returned Energy positive its reversed.
  272. * @param timestep is the TimeStep to compare the HolonElements.
  273. * @return The Energy of the HolonObject.
  274. */
  275. public float getEnergyAtTimeStep(int timestep)
  276. {
  277. return getElements().stream().filter(element -> element.isActive()).map(element -> element.getEnergyAtTimeStep(timestep)).reduce(0.0f, (a, b) -> a + b);
  278. }
  279. /**
  280. * This Method returns the Energy that all HolonElements from the HolonObject produce by itself. Its sums all Energies from the HolonElements of the HolonObject that are ACTIVE and are Producer.
  281. * If the HolonObject have no HolonElement its return 0;
  282. * @param timestep is the TimeStep to compare the HolonElements.
  283. * @return The Energy of the producing HolonElements.
  284. */
  285. public float getEnergySelfProducingFromProducingElements(int timestep) {
  286. return getElements().stream().filter(element -> element.isActive() && (element.getEnergyAtTimeStep(timestep) > 0)).map(element -> element.getEnergyAtTimeStep(timestep)).reduce(0.0f, (a, b) -> a + b);
  287. }
  288. /**
  289. * This Method returns the Energy of all HolonElements from the HolonObject that are consuming. Its sums all Energies from the HolonElements of the HolonObject that are ACTIVE and are Consumer.
  290. * If the HolonObject have no HolonElement its return 0;
  291. * @param timestep is the TimeStep to compare the HolonElements.
  292. * @return The Energy of the consuming HolonElements.
  293. */
  294. public float getEnergyNeededFromConsumingElements(int timestep) {
  295. return getElements().stream().filter(element -> element.isActive() && (element.getEnergyAtTimeStep(timestep) < 0)).map(element -> -element.getEnergyAtTimeStep(timestep)).reduce(0.0f, (a, b) -> a + b);
  296. }
  297. /**
  298. * This Method calculate the amount of HolonElements that are consuming Energy and are ACTIVE.
  299. * @param timestep is the TimeStep to compare the HolonElements.
  300. * @return The amount of HolonElements that are consuming Energy.
  301. */
  302. public int countConsumingElements(int timestep) {
  303. return (int) getElements().stream().filter(element -> element.isActive() && (element.getEnergyAtTimeStep(timestep) < 0)).count();
  304. }
  305. /**
  306. * This Method calculate the amount of HolonElements that are producing Energy and are ACTIVE.
  307. * @param timestep is the TimeStep to compare the HolonElements.
  308. * @return The amount of HolonElements that are producing Energy.
  309. */
  310. public int countProducingElements(int timestep) {
  311. return (int) getElements().stream().filter(element -> element.isActive() && (element.getEnergyAtTimeStep(timestep) > 0)).count();
  312. }
  313. /**
  314. * Get the Color.
  315. *
  316. * @return stateColor the Color
  317. */
  318. public Color getColor() {
  319. return stateColor;
  320. }
  321. /**
  322. * Set the State Color.
  323. *
  324. * @param color the Color
  325. */
  326. public void setColor(Color color) {
  327. stateColor = color;
  328. }
  329. /**
  330. * Get the Array Production
  331. */
  332. public float[] getTrackingProd() {
  333. return this.trackingProd;
  334. }
  335. /**
  336. * Set the Array Production
  337. */
  338. public void setTrackingProd(float[] arr) {
  339. this.trackingProd = arr;
  340. }
  341. /**
  342. * Get the Array Consumption
  343. */
  344. public float[] getTrackingCons() {
  345. return this.trackingCons;
  346. }
  347. /**
  348. * Set the Array Consumption
  349. */
  350. public void setTrackingCons(float[] arr) {
  351. this.trackingCons = arr;
  352. }
  353. /**
  354. * Get the Array Consumption
  355. */
  356. public float getTotalFlex() {
  357. return totalFlex;
  358. }
  359. /**
  360. * Set the Array Consumption
  361. */
  362. public void setTotalFlex(float totalFlex) {
  363. this.totalFlex = totalFlex;
  364. }
  365. /**
  366. * Update the totalFlex
  367. */
  368. public void updateTotalFlex() {
  369. float tempFlex = 0;
  370. for (HolonElement e : getElements()) {
  371. if (e.isFlexible()) {
  372. tempFlex += e.getFlexibleEnergyAvailablePerElement() * e.getAmount();
  373. }
  374. }
  375. this.totalFlex = tempFlex;
  376. }
  377. /**
  378. * calculates total flexible Production
  379. */
  380. public float getFlexProd() {
  381. float tempFlex = 0;
  382. for (HolonElement e : getElements()) {
  383. if (e.getFlexibleEnergyAvailablePerElement() > 0) {
  384. tempFlex += e.getFlexibleEnergyAvailablePerElement() * e.getAmount();
  385. }
  386. }
  387. return tempFlex;
  388. }
  389. /**
  390. * calculates total flexible Concumption
  391. */
  392. public float getFlexCons() {
  393. float tempFlex = 0;
  394. for (HolonElement e : getElements()) {
  395. if (e.getFlexibleEnergyAvailablePerElement() < 0) {
  396. tempFlex += e.getFlexibleEnergyAvailablePerElement() * e.getAmount();
  397. }
  398. }
  399. return tempFlex;
  400. }
  401. /**
  402. * If the user track any HolonObject the tracking information will be
  403. * updated. (If the HolonObject enters into the untracked state, the array
  404. * will be reseted)
  405. */
  406. public void updateTrackingInfo() {
  407. float[] tempProd = new float[100];
  408. float[] tempCons = new float[100];
  409. for (int i = 0; i < 100; i++) {
  410. float valueProd = 0;
  411. float valueCons = 0;
  412. for (HolonElement e : getElements()) {
  413. if (e.isActive() && e.isProducer()) {
  414. valueProd += e.getOverallEnergyAtTimeStep(i);
  415. }
  416. if (e.isActive() && e.isConsumer()) {
  417. valueCons += e.getOverallEnergyAtTimeStep(i);
  418. }
  419. }
  420. tempProd[i] = valueProd;
  421. tempCons[i] = valueCons;
  422. }
  423. this.trackingProd = tempProd;
  424. this.trackingCons = tempCons;
  425. }
  426. public String toString() {
  427. StringBuilder sb = new StringBuilder();
  428. sb.append("[HolonObject: ");
  429. sb.append("id=").append(id)
  430. .append(", name=").append(name)
  431. .append(", state=");
  432. switch (state) {
  433. case NO_ENERGY:
  434. sb.append("NO_ENERGY");
  435. break;
  436. case NOT_SUPPLIED:
  437. sb.append("NOT_SUPPLIED");
  438. break;
  439. case SUPPLIED:
  440. sb.append("SUPPLIED");
  441. break;
  442. case PRODUCER:
  443. sb.append("PRODUCER");
  444. break;
  445. case PARTIALLY_SUPPLIED:
  446. sb.append("PARTIALLY_SUPPLIED");
  447. break;
  448. case OVER_SUPPLIED:
  449. sb.append("OVER_SUPPLIED");
  450. break;
  451. }
  452. sb.append(", elements=[");
  453. for (int i = 0; i < getElements().size(); i++) {
  454. HolonElement el = getElements().get(i);
  455. if (i != 0) {
  456. sb.append(", ");
  457. }
  458. sb.append(el.getEleName());
  459. }
  460. sb.append("]]");
  461. return sb.toString();
  462. }
  463. /**
  464. * @return the {@link #currentSupply}
  465. */
  466. public float getCurrentSupply() {
  467. return currentSupply;
  468. }
  469. /**
  470. * @param currentSupply the {@link #currentSupply} to set
  471. */
  472. public void setCurrentSupply(float currentSupply) {
  473. this.currentSupply = currentSupply;
  474. }
  475. /**
  476. * @return {@link #suppliedPercentage}
  477. */
  478. public float getSuppliedPercentage(){
  479. return suppliedPercentage;
  480. }
  481. }