HolonObject.java 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. * State of the building: 0 = fully supplied (currentEnergy == 0) 1 = not
  15. * enough energy (currentEnergy > 0) 2 = oversupplied (currentEnergy < 0)
  16. */
  17. int state;
  18. /**
  19. * Constructor Set by default the name of the object equals to the category
  20. * name, until the user changes it.
  21. */
  22. public HolonObject(String ObjName) {
  23. super(ObjName);
  24. setElements(new ArrayList<HolonElement>());
  25. setEleIdx(new HashMap<String,Integer>());
  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. }
  33. public HolonObject(CpsObject obj) {
  34. super(obj);
  35. setEleIdx(MultiPurposeController.copyHashMap(((HolonObject) obj).getEleIdx()));
  36. setElements(copyElements(((HolonObject)obj).getElements()));
  37. }
  38. /**
  39. * @return the elements
  40. */
  41. public ArrayList<HolonElement> getElements() {
  42. return elements;
  43. }
  44. /**
  45. * @param elements
  46. * the elements to set
  47. */
  48. public void setElements(ArrayList<HolonElement> elements) {
  49. this.elements = elements;
  50. }
  51. public void addElements(HolonElement element) {
  52. elements.add(element);
  53. }
  54. /**
  55. * @return the currentEnergy
  56. */
  57. public float getCurrentEnergy() {
  58. float temp = 0;
  59. for (HolonElement e : getElements()) {
  60. if (e.getActive()) {
  61. temp = temp + e.getTotalEnergy();
  62. }
  63. }
  64. currentEnergy = temp;
  65. return currentEnergy;
  66. }
  67. /**
  68. * @param currentEnergy
  69. * the currentEnergy to set
  70. */
  71. public void setCurrentEnergy(float currentEnergy) {
  72. this.currentEnergy = currentEnergy;
  73. }
  74. /**
  75. * deletes Element
  76. *
  77. * @param idx
  78. */
  79. public void deleteElement(int idx) {
  80. elements.remove(idx);
  81. }
  82. public float calculateCurrentEnergy() {
  83. return currentEnergy;
  84. }
  85. /**
  86. * String of all consumers in this HolonObject
  87. *
  88. * @return all the names of this HolonObject separated by "," each object
  89. */
  90. public String toStringElements() {
  91. String objString = "Empty";
  92. for (HolonElement e : elements) {
  93. if (objString == "Empty") {
  94. objString = e.getEleName();
  95. } else {
  96. objString = objString + ", " + e.getEleName();
  97. }
  98. }
  99. return objString;
  100. }
  101. /**
  102. * @return the eleIdx
  103. */
  104. public HashMap<String, Integer> getEleIdx() {
  105. return EleIdx;
  106. }
  107. /**
  108. * @param eleIdx the eleIdx to set
  109. */
  110. public void setEleIdx(HashMap<String, Integer> eleIdx) {
  111. EleIdx = eleIdx;
  112. }
  113. /**
  114. * Copy all Elements into a New Array
  115. * @param arr
  116. * @return
  117. */
  118. public ArrayList<HolonElement> copyElements(ArrayList<HolonElement> arr) {
  119. ArrayList<HolonElement> newArr = new ArrayList<>();
  120. for (HolonElement t : arr) {
  121. newArr.add(new HolonElement(t));
  122. }
  123. return newArr;
  124. }
  125. }