Holon.java 4.1 KB

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