Holon.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. package classes;
  2. import java.util.ArrayList;
  3. import java.util.Collections;
  4. import java.util.HashMap;
  5. import java.util.List;
  6. import ui.controller.Control;
  7. import ui.model.DecoratedNetwork;
  8. public class Holon {
  9. public String name = new String();
  10. private Holon parent = null;
  11. public ArrayList<Holon> childHolons = new ArrayList<Holon>();
  12. private List<HolonElement> elements = new ArrayList<HolonElement>();
  13. //HolonObject is the lowest representation of a holon.
  14. private final HolonObject holonObject;
  15. public final boolean isPhysical;
  16. public Holon(String name) {
  17. this.name = name;
  18. this.holonObject = null;
  19. isPhysical = false;
  20. }
  21. public Holon(HolonObject object) {
  22. holonObject = object;
  23. object.holon = this;
  24. name = object.getName();
  25. elements.addAll(object.getElements());
  26. for(HolonElement ele : elements) {
  27. ele.holon = this;
  28. }
  29. isPhysical = true;
  30. }
  31. public void addElement(HolonElement element) {
  32. element.holon = this;
  33. elements.add(element);
  34. }
  35. public void removeElement(HolonElement element) {
  36. element.holon = null;
  37. elements.remove(element);
  38. }
  39. public int elementsCount() {
  40. return elements.size();
  41. }
  42. public void addChild(Holon child) {
  43. child.parent = this;
  44. childHolons.add(child);
  45. }
  46. public void addChildHolon(Holon child, int index) {
  47. child.parent = this;
  48. childHolons.add(index, child);
  49. }
  50. public void removeChildHolon(Holon child) {
  51. child.parent = null;
  52. childHolons.remove(child);
  53. }
  54. public void removeFromParent() {
  55. if(parent != null) {
  56. parent.removeChildHolon(this);
  57. }
  58. }
  59. public int getChildCount() {
  60. return childHolons.size();
  61. }
  62. public Holon getParent() {
  63. return parent;
  64. }
  65. public HolonObject getHolonObject() {
  66. return holonObject;
  67. }
  68. public List<Holon> getChildView(){
  69. return Collections.unmodifiableList(childHolons);
  70. }
  71. public List<HolonElement> getElementView(){
  72. return Collections.unmodifiableList(elements);
  73. }
  74. @Override
  75. public String toString() {
  76. return name;
  77. }
  78. public void reassignAllChildren(Holon other) {
  79. for(Holon child: this.childHolons) {
  80. other.addChild(child);
  81. }
  82. childHolons.clear();
  83. }
  84. public void removeAllRefrences() {
  85. parent = null;
  86. this.childHolons.clear();
  87. this.elements.clear();
  88. }
  89. public int getLayer() {
  90. return parent != null ? parent.getLayer() + 1 : 0;
  91. }
  92. public Holon cloneWithoutParent() {
  93. Holon cloned = new Holon(this.name);
  94. cloned.childHolons = this.childHolons;
  95. cloned.elements = this.elements;
  96. return cloned;
  97. }
  98. public boolean checkHolonArePhysicalConnected(Holon other, Control control) {
  99. HashMap<HolonObject, DecoratedNetwork> table = control.getSimManager().getActualDecorState().getHolonObjectNetworkTable();
  100. HolonObject a = tryGetAPhysicalHolon();
  101. HolonObject b = other.tryGetAPhysicalHolon();
  102. boolean aHolonIsPureAbstract = a == null || b == null;
  103. return aHolonIsPureAbstract || table.get(a) == table.get(b);
  104. }
  105. private HolonObject tryGetAPhysicalHolon() {
  106. if(holonObject != null) {
  107. return holonObject;
  108. }
  109. for(Holon holon : childHolons) {
  110. HolonObject object = holon.tryGetAPhysicalHolon();
  111. if(object != null) {
  112. return object;
  113. }
  114. }
  115. return null;
  116. }
  117. public void checkRepairHolarchy(HashMap<HolonObject, DecoratedNetwork> table, Holon stateHolon) {
  118. if(childHolons.isEmpty()) {
  119. return;
  120. }
  121. //To establish the invariant that all child holons are repaired
  122. for(Holon other : childHolons) {
  123. other.checkRepairHolarchy(table, stateHolon);
  124. }
  125. //Repair this Holon
  126. HolonObject first = tryGetAPhysicalHolon();
  127. if(first == null) {
  128. return;
  129. }
  130. List<Holon> removeList = new ArrayList<Holon>();
  131. for(Holon other : childHolons) {
  132. HolonObject otherHolonObject = other.tryGetAPhysicalHolon();
  133. boolean isPureAbstract = otherHolonObject == null;
  134. if(isPureAbstract) {
  135. continue;
  136. }
  137. boolean isPhysicalConnected = table.get(first) == table.get(otherHolonObject);
  138. if(!isPhysicalConnected) {
  139. removeList.add(other);
  140. }
  141. }
  142. //Remove holons
  143. for(Holon holon : removeList) {
  144. holon.removeFromParent();
  145. stateHolon.addChild(holon);
  146. }
  147. }
  148. }