package holeg.ui.model; import java.util.AbstractMap.SimpleEntry; import java.util.logging.Logger; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Set; import java.util.stream.Collectors; import java.util.stream.Stream; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import holeg.adapter.AbstractCpsObjectAdapter; import holeg.adapter.PairAdapter; import holeg.adapter.PositionAdapter; import holeg.model.AbstractCanvasObject; import holeg.model.Edge; import holeg.model.Flexibility; import holeg.model.GroupNode; import holeg.model.HolonElement; import holeg.model.HolonObject; import holeg.model.HolonSwitch; import holeg.model.Node; import holeg.ui.view.main.Category; import holeg.utility.Vector2Int; /** * 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 static final int GRAPH_ITERATIONS = 100; // Canvas Attributes private String imgPath = ""; private int backgroundMode = 0; private int backgroundWidth = 0; private int backgroundHeight = 0; private int canvasX = 3000; private int canvasY = 3000; private int curIteration = 0; private Edge selectedEdge; private Set selectedObjects = new HashSet<>(); private Set clipboardObjects = new HashSet<>(); // Capacity for Edge private float maxCapacity; // Iteration Speed private int timerSpeed = 1000; // number of the current autosave private int autoSaveNr = -1; // number of max simultaneous autosaves private int numberOfSaves = 35; /** whether the supplyBars should be shown or not */ private boolean showSupplyBars = true; /** the amount of iterations */ private int iterations=100; /** * 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 } /** the Fairness model in use */ private FairnessModel fairnessModel = FairnessModel.MininumDemandFirst; /* * Array of all categories in the model. It is set by default with the * categories ENERGY, BUILDINGS and COMPONENTS */ private ArrayList categories; /* * Array of all CpsObjects in our canvas. It is set by default as an empty * list. */ private ArrayList objectsOnCanvas; private HashMap cgIdx; private HashMap cvsObjIdx; /* * Array of all CpsObjects in our canvas. It is set by default as an empty * list. */ private ArrayList edgesOnCanvas; //TODO(Tom2021-12-20) remove replace with groupnode private ArrayList holonObjectsOnCanvas = new ArrayList(); private ArrayList nodesOnCanvas= new ArrayList(); private ArrayList switchsOnCanvas= new ArrayList(); private HashMap hashcodeMap = new HashMap<>(); //TODO(Tom2021-12-20) should be moved in Save/Load Controller private Gson gson; /** * Constructor for the model. It initializes the categories and * objectsOnCanvas by default values. Listeners are also initialized by * default values. */ public Model() { log.fine("Init Model"); setCategories(new ArrayList<>()); setObjectsOnCanvas(new ArrayList<>()); setEdgesOnCanvas(new ArrayList<>()); setCgIdx(new HashMap<>()); setCvsObjIdx(new HashMap<>()); initGson(); } /** * Returns all Categories. * * @return the categories */ public ArrayList getCategories() { return categories; } /** * Sets all Categories. * * @param categories the categories to set */ public void setCategories(ArrayList categories) { this.categories = categories; } /** * Returns all Objects on the Canvas. * * @return the objectsOnCanvas */ public ArrayList getObjectsOnCanvas() { return objectsOnCanvas; } /** * Sets all Objects on the Canvas. * * @param objectsOnCanvas the objectsOnCanvas to set */ public void setObjectsOnCanvas(ArrayList objectsOnCanvas) { this.objectsOnCanvas = objectsOnCanvas; } /** * Get all Edges on the Canvas. * * @return the edgesOnCanvas */ public ArrayList getEdgesOnCanvas() { return edgesOnCanvas; } /** * Sets the edges on the Canvas. * * @param arrayList the edgesOnCanvas to set */ public void setEdgesOnCanvas(ArrayList arrayList) { this.edgesOnCanvas = arrayList; } /** * 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 all selected Objects on the Canvas. * * @return The selected Objects */ public Set getSelectedObjects() { return selectedObjects; } /** * Returns all selected Objects on the Canvas. * * @return The selected Objects */ public void setSelectedCpsObjects(Set arr) { this.selectedObjects = arr; } /** * Returns the maximum ITERATIONS. * * @return ITERATIONS */ public int getMaxIterations() { return iterations; } /** * Returns the current iteration. * * @return current iteration */ public int getActualTimeStep() { return curIteration; } /** * sets the current Iteration. * * @param curIT the current Iteration */ public void setCurrentIteration(int value) { this.curIteration = value; } /** * Returns the selected Edge. * * @return selectedEdge */ public Edge getSelectedEdge() { return selectedEdge; } /** * Set the selected Edge. * * @param edge that is selected */ public void setSelectedEdge(Edge edge) { this.selectedEdge = edge; } /** * Returns the Categorie Index. * * @return the cgIdx */ public HashMap getCgIdx() { return cgIdx; } /** * Sets the Categorie Index. * * @param cgIdx the cgIdx to set */ public void setCgIdx(HashMap cgIdx) { this.cgIdx = cgIdx; } /** * Returns the CanvasObject Index. * * @return the cvsObjIdx */ public HashMap getCvsObjIdx() { return cvsObjIdx; } /** * Sets the CanvasObject Index. * * @param cvsObjIdx the cvsObjIdx to set */ public void setCvsObjIdx(HashMap cvsObjIdx) { this.cvsObjIdx = cvsObjIdx; } /** * Returns the auto save Number. * * @return the auto save Number */ public int getAutoSaveNr() { return autoSaveNr; } /** * Sets the auto save Number. * * @param autoSaveNr the auto save number */ public void setAutoSaveNr(int autoSaveNr) { this.autoSaveNr = autoSaveNr; } /** * Returns the Number of Saves. * * @return the numberOfSaves */ public int getNumberOfSaves() { return numberOfSaves; } /** * Set the Number of Saves. * * @param numberOfSaves the numberOfSaves to set */ public void setNumberOfSaves(int numberOfSaves) { this.numberOfSaves = numberOfSaves; } /** * Returns all Objects in the Clipboard. * * @return Objects in the Clipboard */ public Set getClipboradObjects() { return clipboardObjects; } /** * Sets the ClipboardObjects. * * @param c Array of Objects */ public void setClipboradObjects(Set c) { this.clipboardObjects = c; } /** * @return the maxCapacity */ public float getMaxCapacity() { return maxCapacity; } /** * @param maxCapacity the maxCapacity to set */ public void setMaxCapacity(float maxCapacity) { this.maxCapacity = maxCapacity; } /** * get the Interval in ms between each Iteration. * * @return timerSpeed speed for the Iterations */ public int getTimerSpeed() { return this.timerSpeed; } /** * Sets the Interval in ms between each Iteration. * * @param t speed for the Iterations */ public void setTimerSpeed(int t) { this.timerSpeed = t; } /** * Get Canvas X Size. * * @return the cANVAS_X */ public int getCanvasX() { return canvasX; } /** * Set Canvas X Size. * * @param canvasX the cANVAS_X to set */ public void setCanvasX(int canvasX) { this.canvasX = canvasX; } /** * get Canvas Y size. * * @return the cANVAS_Y */ public int getCanvasY() { return canvasY; } /** * Set Canvas Y size. * * @param canvasY the cANVAS_Y to set */ public void setCanvasY(int canvasY) { this.canvasY = canvasY; } public List getAllHolonElements() { return getAllHolonObjectsOnCanvas().stream().flatMap(hO -> hO.getElements().stream()).collect(Collectors.toList()); } public List getAllFlexibilities() { return getAllHolonObjectsOnCanvas().stream().flatMap(hO -> hO.getElements().stream().flatMap(ele -> ele.flexList.stream())).collect(Collectors.toList()); } public void reset() { resetFlexibilities(); resetEdges(); } private void resetFlexibilities() { getAllFlexibilities().forEach(flex -> flex.reset()); } private void resetEdges() { this.getEdgesOnCanvas().forEach(edge -> edge.reset()); } public ArrayList getAllHolonObjectsOnCanvas(){ ArrayList objectToReturn = new ArrayList(); objectToReturn.addAll(this.holonObjectsOnCanvas); //TODO(Tom2021-12-18) create GroupNodeOnCanvas for(AbstractCanvasObject aCps : objectsOnCanvas) { if(aCps instanceof GroupNode groupnode){ objectToReturn.addAll(groupnode.getAllHolonObjectsRecursive().toList()); } } return objectToReturn; } public ArrayList getAllAbstractObjectsOnCanvas(){ ArrayList objectToReturn = new ArrayList(); getAllAsbtractObjectsRecursive(objectToReturn, getObjectsOnCanvas().stream()); return objectToReturn; } private void getAllAsbtractObjectsRecursive(ArrayList addObjectsToThisList, Stream listOfObjectsToSearch){ listOfObjectsToSearch.forEach(aCps -> { if(aCps instanceof GroupNode groupnode){ getAllAsbtractObjectsRecursive(addObjectsToThisList, groupnode.getObjectsInThisLayer()); } else{ addObjectsToThisList.add(aCps); } }); } /** * get all Switches */ public ArrayList getAllSwitches() { ArrayList switches = new ArrayList<>(); for (AbstractCanvasObject obj : getObjectsOnCanvas()) { if (obj instanceof HolonSwitch sw) { switches.add(sw); } else if (obj instanceof GroupNode groupnode) { switches.addAll(groupnode.getAllSwitchObjectsRecursive().toList()); } } return switches; } /** * Returns the Path for the background Image of the Canvas. * * @return imgPath the Path */ public String getCanvasImagePath() { return imgPath; } /** * Set the Path for the background Image of the Canvas. * * @param path the Path */ public void setCanvasImagePath(String path) { imgPath = path; } /** * Returns the mode for the background Image of the Canvas. *

* 0 take size of the Image 1 stretch the Image 2 Custom Image size * * @return backgroundMode the mode */ public int getCanvasImageMode() { return backgroundMode; } /** * Set the mode for the background Image of the Canvas. *

* 0 take size of the Image, 1 stretch the Image, 2 Custom Image size * * @param mode the backgroundMode */ public void setCanvasImageMode(int mode) { backgroundMode = mode; } /** * Returns the Custom width of the background Image of the Canvas. * * @return backgroundWidth the Width */ public int getCanvasImageWidth() { return backgroundWidth; } /** * Set the Custom width of the background Image of the Canvas. * * @param width the Width */ public void setCanvasImageWidth(int width) { backgroundWidth = width; } /** * Returns the Custom height of the background Image of the Canvas. * * @return backgroundHeight the height */ public int getCanvasImageHeight() { return backgroundHeight; } /** * Set the Custom height of the background Image of the Canvas. * * @param height the height */ public void setCanvasImageHeight(int height) { backgroundHeight = height; } /** * @return true if SupplyBars should be shown */ public boolean getShowSupplyBars() { return showSupplyBars; } /** * @param showSupplyBars true if the SupplyBars should be shown */ public void setShowSupplyBars(boolean showSupplyBars) { this.showSupplyBars = showSupplyBars; } /** * @param iterations the number of steps for this simulation */ public void setIterations(int iterations){ this.iterations=iterations; } /** * @return the fairnessModel */ public FairnessModel getFairnessModel() { return fairnessModel; } /** * @param fairnessModel the fairnessModel to set */ public void setFairnessModel(FairnessModel fairnessModel) { this.fairnessModel = fairnessModel; } public int getGraphIterations(){ return GRAPH_ITERATIONS; } /** * Initialize the Gson with wanted parameters */ private void initGson() { GsonBuilder builder = new GsonBuilder(); builder.serializeNulls(); builder.excludeFieldsWithoutExposeAnnotation(); builder.setPrettyPrinting(); builder.registerTypeAdapter(AbstractCanvasObject.class, new AbstractCpsObjectAdapter()); builder.registerTypeAdapter(Vector2Int.class, new PositionAdapter()); builder.registerTypeAdapter(SimpleEntry.class, new PairAdapter()); // use the builder and make a instance of the Gson this.setGson(builder.create()); } /** * @return the gson */ public Gson getGson() { return gson; } /** * @param gson the gson to set */ public void setGson(Gson gson) { this.gson = gson; } /** * @return the hashcodeMap */ public HashMap getHashcodeMap() { return hashcodeMap; } /** * @param hashcodeMap the hashcodeMap to set */ public void setHashcodeMap(HashMap hashcodeMap) { this.hashcodeMap = hashcodeMap; } public ArrayList getSwitchsOnCanvas() { return switchsOnCanvas; } public void setSwitchsOnCanvas(ArrayList switchsOnCanvas) { this.switchsOnCanvas = switchsOnCanvas; } public ArrayList getNodesOnCanvas() { return nodesOnCanvas; } public void setNodesOnCanvas(ArrayList nodesOnCanvas) { this.nodesOnCanvas = nodesOnCanvas; } public ArrayList getHolonObjectsOnCanvas() { return holonObjectsOnCanvas; } public void setHolonObjectsOnCanvas(ArrayList holonObjectsOnCanvas) { this.holonObjectsOnCanvas = holonObjectsOnCanvas; } }