Holon.java 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 java.util.UUID;
  7. import classes.holonControlUnit.HolonControlUnit;
  8. import ui.controller.Control;
  9. import ui.model.DecoratedNetwork;
  10. import ui.model.Model;
  11. public class Holon {
  12. public String name = new String();
  13. private Holon parent = null;
  14. public ArrayList<Holon> childHolons = new ArrayList<Holon>();
  15. private List<HolonElement> elements = new ArrayList<HolonElement>();
  16. //HolonObject is the lowest representation of a holon.
  17. private final HolonObject holonObject;
  18. public final boolean isPhysical;
  19. public HolonControlUnit holonControlUnit;
  20. private String uniqueID;
  21. public Model model;
  22. public Holon(String name, Model model) {
  23. this.name = name;
  24. this.holonObject = null;
  25. isPhysical = false;
  26. this.holonControlUnit = new HolonControlUnit(this);
  27. this.uniqueID = UUID.randomUUID().toString();
  28. this.model = model;
  29. // System.out.println("Created new Holon "+name+" "+this.uniqueID);
  30. }
  31. public Holon(HolonObject object, Model model) {
  32. holonObject = object;
  33. object.holon = this;
  34. name = object.getName();
  35. this.holonControlUnit = new HolonControlUnit(this);
  36. elements.addAll(object.getElements());
  37. for(HolonElement ele : elements) {
  38. ele.holon = this;
  39. }
  40. isPhysical = true;
  41. this.uniqueID = UUID.randomUUID().toString();
  42. this.model = model;
  43. // System.out.println("Created new Holon "+name+" "+this.uniqueID);
  44. }
  45. public void addElement(HolonElement element) {
  46. element.holon = this;
  47. elements.add(element);
  48. }
  49. public void removeElement(HolonElement element) {
  50. element.holon = null;
  51. elements.remove(element);
  52. }
  53. public int elementsCount() {
  54. return elements.size();
  55. }
  56. public void addChild(Holon child) {
  57. if(!this.model.getHolonsByID().containsKey(child.getUniqueID()))
  58. return;
  59. child.parent = this;
  60. childHolons.add(child);
  61. this.holonControlUnit.addSubHolon(child);
  62. // System.out.println("Holon "+this.name+" added new child holon "+child.name);
  63. }
  64. public void addChildHolon(Holon child, int index) {
  65. child.parent = this;
  66. childHolons.add(index, child);
  67. this.holonControlUnit.addSubHolon(child);
  68. // System.out.println("Holon "+this.name+" added new child holon "+child.name);
  69. }
  70. public void removeChildHolon(Holon child) {
  71. child.parent = null;
  72. childHolons.remove(child);
  73. this.holonControlUnit.getHierarchyController().removeSubHolon(child.getUniqueID());
  74. }
  75. public void removeFromParent() {
  76. if(parent != null) {
  77. parent.removeChildHolon(this);
  78. }
  79. }
  80. public int getChildCount() {
  81. return childHolons.size();
  82. }
  83. public Holon getParent() {
  84. return parent;
  85. }
  86. public void setParent(Holon parent) {
  87. // System.out.println("Holon "+this.name+" replaced parent "+this.parent.name+" with "+parent.name);
  88. this.parent = parent;
  89. this.holonControlUnit.setSuperHolon(parent);
  90. }
  91. public HolonObject getHolonObject() {
  92. return holonObject;
  93. }
  94. public List<Holon> getChildView(){
  95. return Collections.unmodifiableList(childHolons);
  96. }
  97. public List<HolonElement> getElementView(){
  98. return Collections.unmodifiableList(elements);
  99. }
  100. @Override
  101. public String toString() {
  102. return name;
  103. }
  104. public void reassignAllChildren(Holon other) {
  105. for(Holon child: this.childHolons) {
  106. other.addChild(child);
  107. }
  108. childHolons.clear();
  109. }
  110. public void removeAllRefrences() {
  111. parent = null;
  112. this.childHolons.clear();
  113. this.elements.clear();
  114. }
  115. public int getLayer() {
  116. return parent != null ? parent.getLayer() + 1 : 0;
  117. }
  118. public Holon cloneWithoutParent() {
  119. Holon cloned = new Holon(this.name, this.model);
  120. model.getHolonsByID().put(cloned.getUniqueID(), cloned);
  121. cloned.childHolons = this.childHolons;
  122. cloned.elements = this.elements;
  123. return cloned;
  124. }
  125. public boolean checkHolonArePhysicalConnected(Holon other, Control control) {
  126. HashMap<HolonObject, DecoratedNetwork> table = control.getSimManager().getActualDecorState().getHolonObjectNetworkTable();
  127. HolonObject a = tryGetAPhysicalHolon();
  128. HolonObject b = other.tryGetAPhysicalHolon();
  129. boolean aHolonIsPureAbstract = a == null || b == null;
  130. return aHolonIsPureAbstract || table.get(a) == table.get(b);
  131. }
  132. private HolonObject tryGetAPhysicalHolon() {
  133. if(holonObject != null) {
  134. return holonObject;
  135. }
  136. for(Holon holon : childHolons) {
  137. HolonObject object = holon.tryGetAPhysicalHolon();
  138. if(object != null) {
  139. return object;
  140. }
  141. }
  142. return null;
  143. }
  144. public void checkRepairHolarchy(HashMap<HolonObject, DecoratedNetwork> table, Holon stateHolon) {
  145. if(childHolons.isEmpty()) {
  146. return;
  147. }
  148. //To establish the invariant that all child holons are repaired
  149. for(Holon other : childHolons) {
  150. other.checkRepairHolarchy(table, stateHolon);
  151. }
  152. //Repair this Holon
  153. HolonObject first = tryGetAPhysicalHolon();
  154. if(first == null) {
  155. return;
  156. }
  157. List<Holon> removeList = new ArrayList<Holon>();
  158. for(Holon other : childHolons) {
  159. HolonObject otherHolonObject = other.tryGetAPhysicalHolon();
  160. boolean isPureAbstract = otherHolonObject == null;
  161. if(isPureAbstract) {
  162. continue;
  163. }
  164. boolean isPhysicalConnected = table.get(first) == table.get(otherHolonObject);
  165. if(!isPhysicalConnected) {
  166. removeList.add(other);
  167. }
  168. }
  169. //Remove holons
  170. for(Holon holon : removeList) {
  171. holon.removeFromParent();
  172. stateHolon.addChild(holon);
  173. }
  174. }
  175. public void addNewVirtualNeighbor(String virtualNeighbor) {
  176. if(!this.equals(virtualNeighbor)) {
  177. this.holonControlUnit.addNewVirtualNeighbor(virtualNeighbor);
  178. }
  179. }
  180. public String getUniqueID() {
  181. return uniqueID;
  182. }
  183. }