123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- package classes;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.HashMap;
- import java.util.List;
- import ui.controller.Control;
- import ui.model.DecoratedNetwork;
- public class Holon {
- public String name = new String();
- private Holon parent = null;
- public ArrayList<Holon> childHolons = new ArrayList<Holon>();
- private List<HolonElement> elements = new ArrayList<HolonElement>();
- //HolonObject is the lowest representation of a holon.
- private HolonObject holonObject;
-
- public Holon(String name) {
- this.name = name;
- }
- public Holon(HolonObject object) {
- holonObject = object;
- object.holon = this;
- name = object.getName();
- elements.addAll(object.getElements());
- for(HolonElement ele : elements) {
- ele.holon = this;
- }
- }
-
-
- public void addElement(HolonElement element) {
- element.holon = this;
- elements.add(element);
- }
- public void removeElement(HolonElement element) {
- element.holon = null;
- elements.remove(element);
- }
- public int elementsCount() {
- return elements.size();
- }
-
- public void addChild(Holon child) {
- child.parent = this;
- childHolons.add(child);
- }
-
- public void addChildHolon(Holon child, int index) {
- child.parent = this;
- childHolons.add(index, child);
- }
-
- public void removeChildHolon(Holon child) {
- child.parent = null;
- childHolons.remove(child);
- }
-
- public void removeFromParent() {
- if(parent != null) {
- parent.removeChildHolon(this);
- }
- }
-
-
- public int getChildCount() {
- return childHolons.size();
- }
-
- public Holon getParent() {
- return parent;
- }
- public HolonObject getHolonObject() {
- return holonObject;
- }
-
-
-
- public List<Holon> getChildView(){
- return Collections.unmodifiableList(childHolons);
- }
-
- public List<HolonElement> getElementView(){
- return Collections.unmodifiableList(elements);
- }
-
- @Override
- public String toString() {
- return name;
- }
- public void reassignAllChildren(Holon other) {
- for(Holon child: this.childHolons) {
- other.addChild(child);
- }
- }
-
-
- public void removeAllRefrences() {
- parent = null;
- this.childHolons.clear();
- this.elements.clear();
- holonObject = null;
- }
-
-
-
- public int getLayer() {
- return parent != null ? parent.getLayer() + 1 : 0;
- }
-
-
-
- public Holon cloneWithoutParent() {
- Holon cloned = new Holon(this.name);
- cloned.childHolons = this.childHolons;
- cloned.elements = this.elements;
- return cloned;
- }
-
- public boolean checkHolonArePhysicalConnected(Holon other, Control control) {
- HashMap<HolonObject, DecoratedNetwork> table = control.getSimManager().getActualDecorState().getHolonObjectNetworkTable();
- HolonObject a = tryGetAPhysicalHolon();
- HolonObject b = other.tryGetAPhysicalHolon();
- boolean aHolonIsPureAbstract = a == null || b == null;
- return aHolonIsPureAbstract || table.get(a) == table.get(b);
- }
-
- private HolonObject tryGetAPhysicalHolon() {
- if(holonObject != null) {
- return holonObject;
- }
- for(Holon holon : childHolons) {
- HolonObject object = holon.tryGetAPhysicalHolon();
- if(object != null) {
- return object;
- }
- }
- return null;
- }
-
-
- public void checkRepairHolarchy(HashMap<HolonObject, DecoratedNetwork> table, Holon stateHolon) {
- if(childHolons.isEmpty()) {
- return;
- }
- //To establish the invariant that all child holons are repaired
- for(Holon other : childHolons) {
- other.checkRepairHolarchy(table, stateHolon);
- }
- //Repair this Holon
- HolonObject first = tryGetAPhysicalHolon();
- if(first == null) {
- return;
- }
- List<Holon> removeList = new ArrayList<Holon>();
- for(Holon other : childHolons) {
- HolonObject otherHolonObject = other.tryGetAPhysicalHolon();
- boolean isPureAbstract = otherHolonObject == null;
- if(isPureAbstract) {
- continue;
- }
- boolean isPhysicalConnected = table.get(first) == table.get(otherHolonObject);
- if(!isPhysicalConnected) {
- removeList.add(other);
- }
- }
- //Remove holons
- for(Holon holon : removeList) {
- holon.removeFromParent();
- stateHolon.addChild(holon);
- }
-
- }
-
- }
|