HolonObject.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. package classes;
  2. import java.awt.Color;
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import ui.controller.MultiPurposeController;
  6. import ui.model.idCounter;
  7. public class HolonObject extends CpsObject {
  8. private Color stateColor;
  9. /* Array of all consumers */
  10. private ArrayList<HolonElement> elements;
  11. /* Array of all Indices of Elements */
  12. private HashMap<String, Integer> EleIdx;
  13. /* Total of consumption */
  14. private float currentEnergy;
  15. /**
  16. * 0 = no energy, 1 = not supplied, 2 = supplied, 3 producer
  17. */
  18. int state = 0;
  19. /**
  20. * Constructor Set by default the name of the object equals to the category
  21. * name, until the user changes it.
  22. */
  23. public HolonObject(String ObjName) {
  24. super(ObjName);
  25. setElements(new ArrayList<HolonElement>());
  26. setEleIdx(new HashMap<String, Integer>());
  27. setState();
  28. }
  29. public HolonObject(String ObjName, String obj) {
  30. super(ObjName);
  31. super.setName(obj);
  32. setElements(new ArrayList<HolonElement>());
  33. setEleIdx(new HashMap<String, Integer>());
  34. setState();
  35. }
  36. public HolonObject(CpsObject obj) {
  37. super(obj);
  38. setEleIdx(MultiPurposeController.copyHashMap(((HolonObject) obj).getEleIdx()));
  39. setElements(copyElements(((HolonObject) obj).getElements()));
  40. setState();
  41. }
  42. /**
  43. * sets the State, wether object is a producer, zero Energy, supplied or not
  44. * supplied
  45. */
  46. public void setState() {
  47. if (getCurrentEnergy() > 0) {
  48. setState(3);
  49. stateColor = Color.lightGray;
  50. } else {
  51. if (getCurrentEnergy() == 0) {
  52. setState(0);
  53. stateColor = Color.WHITE;
  54. }else {
  55. if(checkIfPartiallySupplied(0)){
  56. stateColor = Color.yellow;
  57. }else {
  58. stateColor = new Color(230,120,100);
  59. }
  60. }
  61. }
  62. }
  63. /**
  64. * @return the elements
  65. */
  66. public ArrayList<HolonElement> getElements() {
  67. return elements;
  68. }
  69. /**
  70. * @param elements
  71. * the elements to set
  72. */
  73. public void setElements(ArrayList<HolonElement> elements) {
  74. this.elements = elements;
  75. }
  76. public void addElements(HolonElement element) {
  77. elements.add(element);
  78. }
  79. /**
  80. * @return the currentEnergy
  81. */
  82. public float getCurrentEnergy() {
  83. float temp = 0;
  84. for (HolonElement e : getElements()) {
  85. if (e.getActive()) {
  86. temp = temp + e.getTotalEnergy();
  87. }
  88. }
  89. currentEnergy = temp;
  90. return currentEnergy;
  91. }
  92. public float getCurrentEnergyAtTimeStep(int x) {
  93. float temp = 0;
  94. for (HolonElement e : getElements()) {
  95. if (e.getActive()) {
  96. temp = temp + e.getTotalEnergyAtTimeStep(x);
  97. }
  98. }
  99. currentEnergy = temp;
  100. return currentEnergy;
  101. }
  102. /**
  103. * @param currentEnergy
  104. * the currentEnergy to set
  105. */
  106. public void setCurrentEnergy(float currentEnergy) {
  107. this.currentEnergy = currentEnergy;
  108. }
  109. /**
  110. * deletes Element
  111. *
  112. * @param idx
  113. */
  114. public void deleteElement(int idx) {
  115. elements.remove(idx);
  116. }
  117. /**
  118. * String of all consumers in this HolonObject
  119. *
  120. * @return all the names of this HolonObject separated by "," each object
  121. */
  122. public String toStringElements() {
  123. String objString = "Empty";
  124. for (HolonElement e : elements) {
  125. if (objString == "Empty") {
  126. objString = e.getEleName();
  127. } else {
  128. objString = objString + ", " + e.getEleName();
  129. }
  130. }
  131. return objString;
  132. }
  133. /**
  134. * @return the eleIdx
  135. */
  136. public HashMap<String, Integer> getEleIdx() {
  137. return EleIdx;
  138. }
  139. /**
  140. * @param eleIdx
  141. * the eleIdx to set
  142. */
  143. public void setEleIdx(HashMap<String, Integer> eleIdx) {
  144. EleIdx = eleIdx;
  145. }
  146. /**
  147. * Copy all Elements into a New Array
  148. *
  149. * @param arr
  150. * @return
  151. */
  152. public ArrayList<HolonElement> copyElements(ArrayList<HolonElement> arr) {
  153. ArrayList<HolonElement> newArr = new ArrayList<>();
  154. for (HolonElement t : arr) {
  155. newArr.add(new HolonElement(t));
  156. }
  157. return newArr;
  158. }
  159. /*
  160. * returns supplied
  161. */
  162. public int getState() {
  163. return this.state;
  164. }
  165. /**
  166. * @param supplied
  167. * boolean if the Object is fully supplied
  168. */
  169. public void setState(int st) {
  170. this.state = st;
  171. switch(st){
  172. case 0: stateColor = Color.WHITE;
  173. break;
  174. case 1: stateColor = new Color(230,120,100);
  175. break;
  176. case 2: stateColor = Color.GREEN;
  177. break;
  178. case 3: stateColor = Color.lightGray;
  179. break;
  180. case 4: stateColor = Color.YELLOW;
  181. }
  182. }
  183. public HolonElement searchElement(String name) {
  184. HolonElement ele = null;
  185. for (HolonElement e : getElements()) {
  186. if (e.getEleName().equals(name)) {
  187. ele = e;
  188. }
  189. }
  190. return ele;
  191. }
  192. public HolonElement searchElementById(int id) {
  193. HolonElement ele = null;
  194. for (HolonElement e : getElements()) {
  195. if (e.getId() == id) {
  196. ele = e;
  197. }
  198. }
  199. return ele;
  200. }
  201. public boolean checkIfPartiallySupplied(int x) {
  202. if (getElements().size() == 0) {
  203. return false;
  204. }
  205. float minConsum = getElements().get(0).getTotalEnergyAtTimeStep(x);
  206. float prod = 0;
  207. for (HolonElement e : getElements()) {
  208. if (e.getActive()) {
  209. if (e.getTotalEnergyAtTimeStep(x) > 0) {
  210. prod = prod + e.getTotalEnergyAtTimeStep(x);
  211. }
  212. if (minConsum < 0 && (e.getTotalEnergyAtTimeStep(x) > minConsum && e.getTotalEnergyAtTimeStep(x) < 0)) {
  213. minConsum = e.getTotalEnergyAtTimeStep(x);
  214. } else if (minConsum >= 0 && e.getTotalEnergyAtTimeStep(x) < minConsum) {
  215. minConsum = e.getTotalEnergyAtTimeStep(x);
  216. }
  217. }
  218. }
  219. // System.out.println("minCons: " + minConsum + " prod: " + prod);
  220. if (minConsum < 0 && prod >= -minConsum) {
  221. return true;
  222. } else {
  223. return false;
  224. }
  225. }
  226. public void setColor(Color color){
  227. stateColor = color;
  228. }
  229. public Color getColor(){
  230. return stateColor;
  231. }
  232. }