HolonObject.java 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. package classes;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import ui.controller.MultiPurposeController;
  5. import ui.model.idCounter;
  6. public class HolonObject extends CpsObject {
  7. /* Array of all consumers */
  8. private ArrayList<HolonElement> elements;
  9. /* Array of all Indices of Elements*/
  10. private HashMap<String, Integer> EleIdx;
  11. /* Total of consumption */
  12. private float currentEnergy;
  13. /**
  14. * 0 = no energy, 1 = not supplied, 2 = supplied, 3 producer
  15. */
  16. int state = 0;
  17. /**
  18. * Constructor Set by default the name of the object equals to the category
  19. * name, until the user changes it.
  20. */
  21. public HolonObject(String ObjName) {
  22. super(ObjName);
  23. setElements(new ArrayList<HolonElement>());
  24. setEleIdx(new HashMap<String,Integer>());
  25. setState();
  26. }
  27. public HolonObject(String ObjName, String obj) {
  28. super(ObjName);
  29. super.setName(obj);
  30. setElements(new ArrayList<HolonElement>());
  31. setEleIdx(new HashMap<String,Integer>());
  32. setState();
  33. }
  34. public HolonObject(CpsObject obj) {
  35. super(obj);
  36. setEleIdx(MultiPurposeController.copyHashMap(((HolonObject) obj).getEleIdx()));
  37. setElements(copyElements(((HolonObject)obj).getElements()));
  38. setState();
  39. }
  40. /**
  41. * sets the State, wether object is a producer, zero Energy, supplied or not supplied
  42. */
  43. public void setState(){
  44. if(getCurrentEnergy() > 0){
  45. setState(3);
  46. }
  47. else{
  48. if(getCurrentEnergy() == 0){
  49. setState(0);
  50. }
  51. }
  52. }
  53. /**
  54. * @return the elements
  55. */
  56. public ArrayList<HolonElement> getElements() {
  57. return elements;
  58. }
  59. /**
  60. * @param elements
  61. * the elements to set
  62. */
  63. public void setElements(ArrayList<HolonElement> elements) {
  64. this.elements = elements;
  65. }
  66. public void addElements(HolonElement element) {
  67. elements.add(element);
  68. }
  69. /**
  70. * @return the currentEnergy
  71. */
  72. public float getCurrentEnergy() {
  73. float temp = 0;
  74. for (HolonElement e : getElements()) {
  75. if (e.getActive()) {
  76. temp = temp + e.getTotalEnergy();
  77. }
  78. }
  79. currentEnergy = temp;
  80. return currentEnergy;
  81. }
  82. public float getCurrentEnergyAtTimeStep(int x) {
  83. float temp = 0;
  84. for (HolonElement e : getElements()) {
  85. if (e.getActive()) {
  86. temp = temp + e.getTotalEnergyAtTimeStep(x);
  87. }
  88. }
  89. currentEnergy = temp;
  90. return currentEnergy;
  91. }
  92. /**
  93. * @param currentEnergy
  94. * the currentEnergy to set
  95. */
  96. public void setCurrentEnergy(float currentEnergy) {
  97. this.currentEnergy = currentEnergy;
  98. }
  99. /**
  100. * deletes Element
  101. *
  102. * @param idx
  103. */
  104. public void deleteElement(int idx) {
  105. elements.remove(idx);
  106. }
  107. /**
  108. * String of all consumers in this HolonObject
  109. *
  110. * @return all the names of this HolonObject separated by "," each object
  111. */
  112. public String toStringElements() {
  113. String objString = "Empty";
  114. for (HolonElement e : elements) {
  115. if (objString == "Empty") {
  116. objString = e.getEleName();
  117. } else {
  118. objString = objString + ", " + e.getEleName();
  119. }
  120. }
  121. return objString;
  122. }
  123. /**
  124. * @return the eleIdx
  125. */
  126. public HashMap<String, Integer> getEleIdx() {
  127. return EleIdx;
  128. }
  129. /**
  130. * @param eleIdx the eleIdx to set
  131. */
  132. public void setEleIdx(HashMap<String, Integer> eleIdx) {
  133. EleIdx = eleIdx;
  134. }
  135. /**
  136. * Copy all Elements into a New Array
  137. * @param arr
  138. * @return
  139. */
  140. public ArrayList<HolonElement> copyElements(ArrayList<HolonElement> arr) {
  141. ArrayList<HolonElement> newArr = new ArrayList<>();
  142. for (HolonElement t : arr) {
  143. newArr.add(new HolonElement(t));
  144. }
  145. return newArr;
  146. }
  147. /*
  148. * returns supplied
  149. */
  150. public int getState() {
  151. return this.state;
  152. }
  153. /**
  154. * @param supplied
  155. * boolean if the Object is fully supplied
  156. */
  157. public void setState(int st) {
  158. this.state = st;
  159. }
  160. }