123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
- package classes;
- import java.awt.Color;
- import java.util.ArrayList;
- import java.util.HashMap;
- import ui.controller.MultiPurposeController;
- import ui.model.idCounter;
- public class HolonObject extends CpsObject {
-
- private Color stateColor;
- /* Array of all consumers */
- private ArrayList<HolonElement> elements;
- /* Array of all Indices of Elements */
- private HashMap<String, Integer> 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<HolonElement>());
- setEleIdx(new HashMap<String, Integer>());
- setState();
- }
- public HolonObject(String ObjName, String obj) {
- super(ObjName);
- super.setName(obj);
- setElements(new ArrayList<HolonElement>());
- setEleIdx(new HashMap<String, Integer>());
- 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);
- stateColor = Color.lightGray;
- } else {
- if (getCurrentEnergy() == 0) {
- setState(0);
- stateColor = Color.WHITE;
- }else {
- if(checkIfPartiallySupplied(0)){
- stateColor = Color.yellow;
- }else {
- stateColor = new Color(230,120,100);
- }
-
- }
- }
- }
- /**
- * @return the elements
- */
- public ArrayList<HolonElement> getElements() {
- return elements;
- }
- /**
- * @param elements
- * the elements to set
- */
- public void setElements(ArrayList<HolonElement> 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<String, Integer> getEleIdx() {
- return EleIdx;
- }
- /**
- * @param eleIdx
- * the eleIdx to set
- */
- public void setEleIdx(HashMap<String, Integer> eleIdx) {
- EleIdx = eleIdx;
- }
- /**
- * Copy all Elements into a New Array
- *
- * @param arr
- * @return
- */
- public ArrayList<HolonElement> copyElements(ArrayList<HolonElement> arr) {
- ArrayList<HolonElement> 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;
- switch(st){
- case 0: stateColor = Color.WHITE;
- break;
-
- case 1: stateColor = new Color(230,120,100);
- break;
-
- case 2: stateColor = Color.GREEN;
- break;
-
- case 3: stateColor = Color.lightGray;
- break;
-
- case 4: stateColor = Color.YELLOW;
- }
- }
- public HolonElement searchElement(String name) {
- HolonElement ele = null;
- for (HolonElement e : getElements()) {
- if (e.getEleName().equals(name)) {
- ele = e;
- }
- }
- return ele;
- }
- public HolonElement searchElementById(int id) {
- HolonElement ele = null;
- for (HolonElement e : getElements()) {
- if (e.getId() == id) {
- ele = e;
- }
- }
- return ele;
- }
- public boolean checkIfPartiallySupplied(int x) {
- if (getElements().size() == 0) {
- return false;
- }
- float minConsum = getElements().get(0).getTotalEnergyAtTimeStep(x);
- float prod = 0;
- for (HolonElement e : getElements()) {
- if (e.getActive()) {
- if (e.getTotalEnergyAtTimeStep(x) > 0) {
- prod = prod + e.getTotalEnergyAtTimeStep(x);
- }
- if (minConsum < 0 && (e.getTotalEnergyAtTimeStep(x) > minConsum && e.getTotalEnergyAtTimeStep(x) < 0)) {
- minConsum = e.getTotalEnergyAtTimeStep(x);
- } else if (minConsum >= 0 && e.getTotalEnergyAtTimeStep(x) < minConsum) {
- minConsum = e.getTotalEnergyAtTimeStep(x);
- }
- }
- }
- // System.out.println("minCons: " + minConsum + " prod: " + prod);
- if (minConsum < 0 && prod >= -minConsum) {
- return true;
- } else {
- return false;
- }
- }
-
- public void setColor(Color color){
- stateColor = color;
- }
-
- public Color getColor(){
- return stateColor;
- }
- }
|