package classes; import java.util.ArrayList; import java.util.HashMap; import ui.controller.MultiPurposeController; import ui.model.idCounter; public class HolonObject extends CpsObject { /* Array of all consumers */ private ArrayList elements; /* Array of all Indices of Elements */ private HashMap EleIdx; /* Total of consumption */ private float currentEnergy; /** * 0 = no energy, 1 = not supplied, 2 = supplied, 3 producer */ int state = 0; /** * Constructor Set by default the name of the object equals to the category * name, until the user changes it. */ public HolonObject(String ObjName) { super(ObjName); setElements(new ArrayList()); setEleIdx(new HashMap()); setState(); } public HolonObject(String ObjName, String obj) { super(ObjName); super.setName(obj); setElements(new ArrayList()); setEleIdx(new HashMap()); setState(); } public HolonObject(CpsObject obj) { super(obj); setEleIdx(MultiPurposeController.copyHashMap(((HolonObject) obj).getEleIdx())); setElements(copyElements(((HolonObject) obj).getElements())); setState(); } /** * sets the State, wether object is a producer, zero Energy, supplied or not * supplied */ public void setState() { if (getCurrentEnergy() > 0) { setState(3); } else { if (getCurrentEnergy() == 0) { setState(0); } } } /** * @return the elements */ public ArrayList getElements() { return elements; } /** * @param elements * the elements to set */ public void setElements(ArrayList elements) { this.elements = elements; } public void addElements(HolonElement element) { elements.add(element); } /** * @return the currentEnergy */ public float getCurrentEnergy() { float temp = 0; for (HolonElement e : getElements()) { if (e.getActive()) { temp = temp + e.getTotalEnergy(); } } currentEnergy = temp; return currentEnergy; } public float getCurrentEnergyAtTimeStep(int x) { float temp = 0; for (HolonElement e : getElements()) { if (e.getActive()) { temp = temp + e.getTotalEnergyAtTimeStep(x); } } currentEnergy = temp; return currentEnergy; } /** * @param currentEnergy * the currentEnergy to set */ public void setCurrentEnergy(float currentEnergy) { this.currentEnergy = currentEnergy; } /** * deletes Element * * @param idx */ public void deleteElement(int idx) { elements.remove(idx); } /** * String of all consumers in this HolonObject * * @return all the names of this HolonObject separated by "," each object */ public String toStringElements() { String objString = "Empty"; for (HolonElement e : elements) { if (objString == "Empty") { objString = e.getEleName(); } else { objString = objString + ", " + e.getEleName(); } } return objString; } /** * @return the eleIdx */ public HashMap getEleIdx() { return EleIdx; } /** * @param eleIdx * the eleIdx to set */ public void setEleIdx(HashMap eleIdx) { EleIdx = eleIdx; } /** * Copy all Elements into a New Array * * @param arr * @return */ public ArrayList copyElements(ArrayList arr) { ArrayList newArr = new ArrayList<>(); for (HolonElement t : arr) { newArr.add(new HolonElement(t)); } return newArr; } /* * returns supplied */ public int getState() { return this.state; } /** * @param supplied * boolean if the Object is fully supplied */ public void setState(int st) { this.state = st; } public HolonElement searchElement(String name) { HolonElement ele = null; for (HolonElement e : getElements()) { if (e.getEleName().equals(name)) { ele = e; } } return ele; } }