package holeg.model; import java.util.HashSet; import java.util.List; import java.util.Set; import java.util.logging.Logger; import java.util.stream.Collectors; /** * The Class Model is the class where everything is saved. All changes made to * the Data is managed via a controller. * * @author Gruppe14 */ public class Model { private static final Logger log = Logger.getLogger(Model.class.getName()); private GroupNode canvas = new GroupNode("Canvas"); public transient Set holons = new HashSet<>(); /** the amount of iterations */ private int currentIteration = 0; private int maxIterations = 100; /** * the Fairness model in use */ private FairnessModel fairnessModel = FairnessModel.MininumDemandFirst; /* * Array of all CpsObjects in our canvas. It is set by default as an empty * list. */ private Set edgesOnCanvas = new HashSet<>(); public Model() { log.fine("Init Model"); } public GroupNode getCanvas() { return canvas; } /** * Get all Edges on the Canvas. * * @return the edgesOnCanvas */ public Set getEdgesOnCanvas() { return edgesOnCanvas; } /** * Adds an Edge to The Canvas. * * @param edge the edgesOnCanvas to add */ public void addEdgeOnCanvas(Edge edge) { this.edgesOnCanvas.add(edge); } /** * Remove an edge from the Canvas. * * @param edge the edge to remove */ public void removeEdgesOnCanvas(Edge edge) { this.edgesOnCanvas.remove(edge); } /** * Returns the maximum iterations. * * @return iterations */ public int getMaxIterations() { return maxIterations; } /** * Returns the current iteration. * * @return current iteration */ public int getCurrentIteration() { return currentIteration; } /** * sets the current Iteration. * * @param value the current Iteration */ public void setCurrentIteration(int value) { this.currentIteration = value; } public List getAllHolonElements() { return canvas.getAllHolonObjectsRecursive().flatMap(HolonObject::elementsStream).collect(Collectors.toList()); } public List getAllFlexibilities() { return canvas.getAllHolonObjectsRecursive().flatMap(hO -> hO.elementsStream().flatMap(ele -> ele.flexList.stream())).collect(Collectors.toList()); } public void reset() { resetFlexibilities(); resetEdges(); } private void resetFlexibilities() { getAllFlexibilities().forEach(Flexibility::reset); } private void resetEdges() { this.getEdgesOnCanvas().forEach(Edge::reset); } /** * @param iterations the number of steps for this simulation */ public void setIterations(int iterations) { this.maxIterations = iterations; } /** * @return the fairnessModel */ public FairnessModel getFairnessModel() { return fairnessModel; } /** * @param fairnessModel the fairnessModel to set */ public void setFairnessModel(FairnessModel fairnessModel) { this.fairnessModel = fairnessModel; } public void clear() { this.edgesOnCanvas.clear(); canvas.clear(); } public void setCanvas(GroupNode canvas) { this.canvas = canvas; } public void setEdgesOnCanvas(Set edgesOnCanvas) { this.edgesOnCanvas = edgesOnCanvas; } /** * All implemented FairnessModels:
* {@link FairnessModel#MininumDemandFirst}
* {@link FairnessModel#AllEqual} */ public enum FairnessModel { /** * One Element of each HolonObject will be powered first, starting with the * smallest Demand. If ale HolonObjects have an active Element, the * simulation will try to fully supply as many HolonObjects as possible. */ MininumDemandFirst, /** * All HolonObjects will receive the same amount of energy. */ AllEqual } }