HolonObject.java 16 KB

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