12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064 |
- package ui.model;
- import java.awt.Color;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Collection;
- import java.util.Collections;
- import java.util.Comparator;
- import java.util.HashMap;
- import java.util.HashSet;
- import java.util.Iterator;
- import java.util.LinkedList;
- import java.util.List;
- import java.util.Map;
- import java.util.stream.Collectors;
- import javax.swing.JTable;
- import com.google.gson.Gson;
- import com.google.gson.GsonBuilder;
- import TypeAdapter.AbstractCpsObjectAdapter;
- import TypeAdapter.ColorAdapter;
- import TypeAdapter.PairAdapter;
- import TypeAdapter.PositionAdapter;
- import classes.AbstractCanvasObject;
- import classes.Category;
- import classes.Edge;
- import classes.GroupNode;
- import classes.Holon;
- import classes.HolonElement;
- import classes.HolonObject;
- import classes.HolonSwitch;
- import classes.Node;
- import classes.Pair;
- import classes.Position;
- import interfaces.GraphListener;
- import interfaces.ObjectListener;
- import ui.view.DefaulTable;
- import ui.view.PropertyTable;
- /**
- * 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 int GRAPH_ITERATIONS = 100;
- // Global Variables
- private static int sCALE = 50; // Picture Scale
- private static int sCALEdIV2 = sCALE / 2;
- public String[] colNames = {"Field", "Information"};
- // 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 HolonElement selectedHolonElement;
- private Edge selectedEdge;
- private ArrayList<AbstractCanvasObject> selectedObjects = new ArrayList<>();
- private ArrayList<AbstractCanvasObject> clipboardObjects = new ArrayList<>();
- private HashMap<Integer, ArrayList<HolonElement>> eleToDelete;
- // Capacity for Edge
- private float maxCapacity;
- // Table for HolonElements --> all cells are editable
- private JTable tableHolonElement;
- /** Table for the properties of HolonObjects, Edges etc */
- private JTable propertyTable;
-
- private ArrayList<GraphListener> graphListeners = new ArrayList<GraphListener>();
- // Iteration Speed
- private int timerSpeed = 1000;
- private int selectedID = 0;
- // 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;
- //TODO:
- private int iterations=100;
-
- /**
- * All implemented FairnessModels:<br>
- * {@link FairnessModel#MininumDemandFirst}<br>
- * {@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<Category> categories;
- /*
- * Array of all CpsObjects in our canvas. It is set by default as an empty
- * list.
- */
- private ArrayList<AbstractCanvasObject> objectsOnCanvas;
- private HashMap<String, Integer> cgIdx;
- private HashMap<Integer, Integer> cvsObjIdx;
- /*
- * Array of all CpsObjects in our canvas. It is set by default as an empty
- * list.
- */
- private ArrayList<Edge> edgesOnCanvas;
-
-
- private ArrayList<HolonObject> holonObjectsOnCanvas = new ArrayList<HolonObject>();
- private ArrayList<Node> nodesOnCanvas= new ArrayList<Node>();
- private ArrayList<HolonSwitch> switchsOnCanvas= new ArrayList<HolonSwitch>();
-
- private List<ObjectListener> objectListeners;
- private PropertyTable tableModelHolonElementMulti;
- private PropertyTable tableModelHolonElementSingle;
- private DefaulTable tableModelProperties;
- private HashMap<Integer, GroupNode> hashcodeMap = new HashMap<>();
- private Holon stateHolon = new Holon("All Holons", this);
-
- private Map<String, Holon> holonsByID = new HashMap();
- 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() {
- setCategories(new ArrayList<>());
- setObjectsOnCanvas(new ArrayList<>());
- setEdgesOnCanvas(new ArrayList<>());
- setObjectListeners(new LinkedList<>());
- setCgIdx(new HashMap<>());
- setCvsObjIdx(new HashMap<>());
- setClipboradObjects(new ArrayList<>());
- setEleToDelete(new HashMap<>());
- setSingleTable(new PropertyTable());
- setMultiTable(new PropertyTable());
- setPropertyTable(new DefaulTable(1000, colNames.length));
- getPropertyTable().setColumnIdentifiers(colNames);
- setTableHolonElement(new JTable());
- initGson();
- this.holonsByID.put(stateHolon.getUniqueID(), stateHolon);
- }
- /**
- * Returns all Categories.
- *
- * @return the categories
- */
- public ArrayList<Category> getCategories() {
- return categories;
- }
- /**
- * Sets all Categories.
- *
- * @param categories the categories to set
- */
- public void setCategories(ArrayList<Category> categories) {
- this.categories = categories;
- }
- /**
- * Transform the Arraylist of categories into a string of all objectName
- * with a separation (',') between each name.
- *
- * @return String of all names separeted by ','
- */
- public String toStringCat() {
- String text = "";
- for (int i = 0; i < categories.size(); i++) {
- if (text.equals("")) {
- text = categories.get(i).getName();
- } else {
- text = text + ", " + categories.get(i).getName();
- }
- }
- return text;
- }
- /**
- * Returns all Objects on the Canvas.
- *
- * @return the objectsOnCanvas
- */
- public ArrayList<AbstractCanvasObject> getObjectsOnCanvas() {
- return objectsOnCanvas;
- }
- /**
- * Sets all Objects on the Canvas.
- *
- * @param objectsOnCanvas the objectsOnCanvas to set
- */
- public void setObjectsOnCanvas(ArrayList<AbstractCanvasObject> objectsOnCanvas) {
- this.objectsOnCanvas = objectsOnCanvas;
- }
- /**
- * Get all Edges on the Canvas.
- *
- * @return the edgesOnCanvas
- */
- public ArrayList<Edge> getEdgesOnCanvas() {
- return edgesOnCanvas;
- }
- /**
- * Sets the edges on the Canvas.
- *
- * @param arrayList the edgesOnCanvas to set
- */
- public void setEdgesOnCanvas(ArrayList<Edge> 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 the ObjectListener.
- *
- * @return the objectListeners
- */
- public List<ObjectListener> getObjectListeners() {
- return objectListeners;
- }
- /**
- * Sets the ObjectListener.
- *
- * @param linkedList the objectListeners to set
- */
- public void setObjectListeners(LinkedList<ObjectListener> linkedList) {
- this.objectListeners = linkedList;
- }
- /**
- * Returns the ID of the selected Object 0 = no Object is selected.
- *
- * @return ID
- */
- public int getSelectedObjectID() {
- return selectedID;
- }
- /**
- * Set the ID of the selected Object 0 = no Object is selected.
- *
- * @param id the ID
- */
- public void setSelectedObjectID(int id) {
- this.selectedID = id;
- }
- /**
- * Returns all selected Objects on the Canvas.
- *
- * @return The selected Objects
- */
- public ArrayList<AbstractCanvasObject> getSelectedCpsObjects() {
- return selectedObjects;
- }
- /**
- * Returns all selected Objects on the Canvas.
- *
- * @return The selected Objects
- */
- public void setSelectedCpsObjects(ArrayList<AbstractCanvasObject> arr) {
- this.selectedObjects = arr;
- }
- /**
- * Returns the Selected Holon Element.
- *
- * @return selected Holon Element
- */
- public HolonElement getSelectedHolonElement() {
- return selectedHolonElement;
- }
- /**
- * Sets the Selecte HolonElement.
- *
- * @param selectedHolonElement that is Selected
- */
- public void setSelectedHolonElement(HolonElement selectedHolonElement) {
- this.selectedHolonElement = selectedHolonElement;
- }
- /**
- * Returns the sCale (Scale for the Images).
- *
- * @return sCALE
- */
- public int getScale() {
- return sCALE;
- }
-
-
- public Holon getStateHolon() {
- return stateHolon;
- }
- /**
- * Sets the Image Scale.
- *
- * @param scale for the image
- */
- public void setScale(int scale) {
- sCALE = scale;
- if ((sCALE & 1) == 0)
- sCALEdIV2 = sCALE / 2;
- else
- sCALEdIV2 = (sCALE + 1) / 2;
- }
- /**
- * Returns sCALEdIV2 (The Scale divided by 2).
- *
- * @return sCALEdIV2
- */
- public int getScaleDiv2() {
- return sCALEdIV2;
- }
- /**
- * Returns the maximum ITERATIONS.
- *
- * @return ITERATIONS
- */
- public int getIterations() {
- return iterations;
- }
- private void notifyGraphListeners() {
- for (GraphListener gl : graphListeners) {
- gl.repaintTree();
- }
- }
- /**
- * Returns cURiTERATION.
- *
- * @return cURiTERATION
- */
- public int getCurIteration() {
- return curIteration;
- }
- /**
- * sets the current Iteration.
- *
- * @param curIT the current Iteration
- */
- public void setCurIteration(int curIT) {
- this.curIteration = curIT;
- notifyGraphListeners();
- }
- /**
- * 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<String, Integer> getCgIdx() {
- return cgIdx;
- }
- /**
- * Sets the Categorie Index.
- *
- * @param cgIdx the cgIdx to set
- */
- public void setCgIdx(HashMap<String, Integer> cgIdx) {
- this.cgIdx = cgIdx;
- }
- /**
- * Returns the CanvasObject Index.
- *
- * @return the cvsObjIdx
- */
- public HashMap<Integer, Integer> getCvsObjIdx() {
- return cvsObjIdx;
- }
- /**
- * Sets the CanvasObject Index.
- *
- * @param cvsObjIdx the cvsObjIdx to set
- */
- public void setCvsObjIdx(HashMap<Integer, Integer> 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 ArrayList<AbstractCanvasObject> getClipboradObjects() {
- return clipboardObjects;
- }
- /**
- * Sets the ClipboardObjects.
- *
- * @param c Array of Objects
- */
- public void setClipboradObjects(ArrayList<AbstractCanvasObject> 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 HashMap<Integer, ArrayList<HolonElement>> getEleToDelete() {
- return this.eleToDelete;
- }
- public void setEleToDelete(HashMap<Integer, ArrayList<HolonElement>> theHash) {
- this.eleToDelete = theHash;
- }
- public PropertyTable getSingleTable() {
- return this.tableModelHolonElementSingle;
- }
- public void setSingleTable(PropertyTable pt) {
- this.tableModelHolonElementSingle = pt;
- }
- public PropertyTable getMultiTable() {
- return this.tableModelHolonElementMulti;
- }
- public void setMultiTable(PropertyTable pt) {
- this.tableModelHolonElementMulti = pt;
- }
- public DefaulTable getPropertyTable() {
- return this.tableModelProperties;
- }
- public void setPropertyTable(DefaulTable pt) {
- this.tableModelProperties = pt;
- }
- public JTable getTableHolonElement() {
- return tableHolonElement;
- }
- public void setTableHolonElement(JTable tableHolonElement) {
- this.tableHolonElement = tableHolonElement;
- }
-
- /**
- * @return the tableProperties
- */
- public JTable getTableProperties() {
- return propertyTable;
- }
-
- /**
- * @return the tableProperties
- */
- public void setTableProperties(JTable propertyTable) {
- this.propertyTable = propertyTable;
- }
- public List<HolonElement> getAllHolonElemnts() {
- return getAllHolonObjectsOnCanvas().stream().flatMap(hO -> hO.getElements().stream()).collect(Collectors.toList());
- }
-
-
- public ArrayList<HolonObject> getAllHolonObjectsOnCanvas(){
- ArrayList<HolonObject> objectToReturn = new ArrayList<HolonObject>();
- getAllHolonObjectsRecursive(objectToReturn, getObjectsOnCanvas());
- return objectToReturn;
- }
-
- private void getAllHolonObjectsRecursive(ArrayList<HolonObject> addObjectsToThisList, List<AbstractCanvasObject> listOfObjectsToSearch){
- for(AbstractCanvasObject aCps : listOfObjectsToSearch) {
- if(aCps instanceof HolonObject) {
- addObjectsToThisList.add((HolonObject) aCps);
- }else if(aCps instanceof GroupNode){
- getAllHolonObjectsRecursive(addObjectsToThisList, ((GroupNode)aCps).getNodes());
- }
- }
- }
-
-
- /**
- * get all Switches
- */
- public ArrayList<HolonSwitch> getAllSwitches() {
- ArrayList<HolonSwitch> switches = new ArrayList<>();
- for (AbstractCanvasObject obj : getObjectsOnCanvas()) {
- if (obj instanceof HolonSwitch) {
- switches.add((HolonSwitch) obj);
- } else if (obj instanceof GroupNode) {
- getSwitchesRec(((GroupNode) obj).getNodes(), switches);
- }
- }
- return switches;
- }
- /**
- * get the Amount of Switches help function
- *
- * @param objects objects
- * @param switches List of switches
- */
- private ArrayList<HolonSwitch> getSwitchesRec(ArrayList<AbstractCanvasObject> objects,
- ArrayList<HolonSwitch> switches) {
- for (AbstractCanvasObject obj : objects) {
- if (obj instanceof HolonSwitch) {
- switches.add((HolonSwitch) obj);
- } else if (obj instanceof GroupNode) {
- getSwitchesRec(((GroupNode) obj).getNodes(), switches);
- }
- }
- 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.
- * <p>
- * 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.
- * <p>
- * 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(Position.class, new PositionAdapter());
- builder.registerTypeAdapter(Color.class, new ColorAdapter());
- builder.registerTypeAdapter(Pair.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<Integer, GroupNode> getHashcodeMap() {
- return hashcodeMap;
- }
- /**
- * @param hashcodeMap the hashcodeMap to set
- */
- public void setHashcodeMap(HashMap<Integer, GroupNode> hashcodeMap) {
- this.hashcodeMap = hashcodeMap;
- }
-
-
-
-
-
- public ArrayList<HolonSwitch> getSwitchsOnCanvas() {
- return switchsOnCanvas;
- }
- public void setSwitchsOnCanvas(ArrayList<HolonSwitch> switchsOnCanvas) {
- this.switchsOnCanvas = switchsOnCanvas;
- }
- public ArrayList<Node> getNodesOnCanvas() {
- return nodesOnCanvas;
- }
- public void setNodesOnCanvas(ArrayList<Node> nodesOnCanvas) {
- this.nodesOnCanvas = nodesOnCanvas;
- }
- public ArrayList<HolonObject> getHolonObjectsOnCanvas() {
- return holonObjectsOnCanvas;
- }
- public void setHolonObjectsOnCanvas(ArrayList<HolonObject> holonObjectsOnCanvas) {
- this.holonObjectsOnCanvas = holonObjectsOnCanvas;
- }
-
- public void defineLists() {
- switchsOnCanvas.clear();
- nodesOnCanvas.clear();
- holonObjectsOnCanvas.clear();
- for(AbstractCanvasObject aCps : this.objectsOnCanvas) {
- if(aCps instanceof HolonObject)holonObjectsOnCanvas.add((HolonObject) aCps);
- else if(aCps instanceof Node)nodesOnCanvas.add((Node) aCps);
- else if(aCps instanceof HolonSwitch)switchsOnCanvas.add((HolonSwitch) aCps);
- }
- }
- public Map<String, Holon> getHolonsByID() {
- return holonsByID;
- }
-
- /**
- * get a list of paths between the two holarchies/minModels
- * list contains shortest path between each pair of objects (o1, o2) in (minModel x minModel2)
- * @param minModel
- * @param minModel2
- * @param holarchy
- * @return
- */
- public HashMap<Float, ArrayList<Edge>> getShortestPathToHolarchy(MinimumModel minModel, MinimumModel minModel2){
- ArrayList<AbstractCanvasObject> objects = new ArrayList<AbstractCanvasObject>();
- objects.addAll(minModel.getHolonObjectList());
- objects.addAll(minModel.getNodeList());
- objects.addAll(minModel.getSwitchList());
-
- ArrayList<AbstractCanvasObject> objects2 = new ArrayList<AbstractCanvasObject>();
- objects2.addAll(minModel2.getHolonObjectList());
- objects2.addAll(minModel2.getNodeList());
- objects2.addAll(minModel2.getSwitchList());
-
- List<AbstractCanvasObject> vertices = this.objectsOnCanvas.stream()
- .map(aco -> aco instanceof GroupNode ? ((GroupNode) aco).getAllObjectsInside() : List.of(aco))
- .flatMap(List::stream)
- .collect(Collectors.toList());
-
- HashMap<Float, ArrayList<Edge>> map = new HashMap<Float, ArrayList<Edge>>();
- for(AbstractCanvasObject aco : objects) {
- for(AbstractCanvasObject aco2 : objects2) {
- if(aco.equals(aco2))
- continue;
- ArrayList<Edge> edges = dijkstra(aco, aco2, minModel2.getHolonObjectList(), vertices, this.edgesOnCanvas);
- if(edges == null)
- continue;
- float dist = 0;
- for(Edge e : edges) {
- dist += e.getLength();
- }
- map.put(dist, edges);
- }
- }
-
- return map;
- }
-
- public ArrayList<Edge> dijkstra(AbstractCanvasObject a, AbstractCanvasObject b, ArrayList<HolonObject> holarchy,
- List<AbstractCanvasObject> vertices, ArrayList<Edge> edges) {
- ArrayList<Edge> es = new ArrayList<Edge>();
-
- HashSet<AbstractCanvasObject> visited = new HashSet<AbstractCanvasObject>();
- ArrayList<AbstractCanvasObject> unvisited = new ArrayList<AbstractCanvasObject>();
- HashMap<AbstractCanvasObject, Float> dist = new HashMap<AbstractCanvasObject, Float>();
- for(AbstractCanvasObject aco : vertices) {
- unvisited.add(aco);
- dist.put(aco, Float.MAX_VALUE);
- }
- dist.put(a, 0f);
- HashMap<AbstractCanvasObject, AbstractCanvasObject> pre = new HashMap<AbstractCanvasObject, AbstractCanvasObject>();
- AbstractCanvasObject current = a;
- visited.add(a);
- unvisited.remove(a);
- while(!unvisited.isEmpty()) {
- if(b.equals(current))
- break;
- if(!dist.containsKey(current)) {
- // System.out.println("something is wrong "+current+" "+dist);
- return null;
- }
- float currDist = dist.get(current);
- for(AbstractCanvasObject aco : current.getConnectedObjects(0f)) {
- if(visited.contains(aco) || (aco instanceof HolonObject && !holarchy.contains(aco))) {
- continue;
- }
- float newDist = currDist + getEdgeBetweenObjects(current, aco, this.edgesOnCanvas).getLength();
- if(newDist < dist.get(aco)) {
- dist.put(aco, newDist);
- pre.put(aco, current);
- }
- }
- AbstractCanvasObject next = getMinInMap(dist, unvisited);
- visited.add(current);
- unvisited.remove(current);
- current = next;
- }
-
- //trace back path
- current = b;
- while(!a.equals(current)) {
- AbstractCanvasObject next = pre.get(current);
- es.add(getEdgeBetweenObjects(current, next, edges));
- current = next;
- }
- return es;
- }
-
- public AbstractCanvasObject getMinInMap(HashMap<AbstractCanvasObject, Float> map, ArrayList<AbstractCanvasObject> unvisited) {
- AbstractCanvasObject min = null;
- float m = Float.MAX_VALUE;
-
- for(AbstractCanvasObject aco : map.keySet()) {
- if(unvisited.contains(aco) && map.get(aco) < m) {
- m = map.get(aco);
- min = aco;
- }
- }
-
- return min;
- }
-
- public Edge getEdgeBetweenObjects(AbstractCanvasObject a, AbstractCanvasObject b, ArrayList<Edge> edges) {
- for(Edge e : edges) {
- AbstractCanvasObject a2 = e.getA();
- AbstractCanvasObject b2 = e.getB();
- if( (a.equals(a2) && b.equals(b2)) || (a.equals(b2) && b.equals(a2)) ) {
- return e;
- }
- }
- return null;
- }
-
- public boolean checkHolonObjectsAreConnected(Holon a, Holon b, float throughput) {
- if(a == null || b == null || a.equals(b)) {
- return false;
- }
- HashSet<AbstractCanvasObject> visited = new HashSet<AbstractCanvasObject>();
- ArrayList<AbstractCanvasObject> objects = new ArrayList<AbstractCanvasObject>();
- objects.addAll(a.getAllHolonObjects());
- objects.addAll(b.getAllHolonObjects());
-
- ArrayList<AbstractCanvasObject> toVisit = new ArrayList<AbstractCanvasObject>();
- toVisit.add(a.getHolonObject());
- while(!toVisit.isEmpty()) {
- AbstractCanvasObject aco = toVisit.remove(0);
- for(AbstractCanvasObject aco2 : aco.getConnectedObjects(throughput)) {
- if(visited.contains(aco2) || (aco2 instanceof HolonSwitch && !((HolonSwitch)aco2).isClosed()) ) {
- continue;
- }
- if(aco2 instanceof HolonObject && aco2.equals(b.getHolonObject())) {
- return true;
- }
- if( aco2 instanceof HolonSwitch || aco2 instanceof Node || (aco2 instanceof HolonObject && objects.contains(aco2)) ) {
- toVisit.add(aco2);
- }
- }
- visited.add(aco);
- }
-
- return false;
- }
-
- public ArrayList<String> getAllConnectionsFromObject(String objectID, String requester){
- ArrayList<String> connections = new ArrayList<String>();
- List<AbstractCanvasObject> objects = this.objectsOnCanvas.stream()
- .map(aco -> aco instanceof GroupNode ? ((GroupNode) aco).getAllObjectsInside() : List.of(aco))
- .flatMap(List::stream)
- .collect(Collectors.toList());
-
- for(AbstractCanvasObject aco : objects) {
- if(aco instanceof HolonSwitch && ((HolonSwitch) aco).getUniqueID().equals(objectID)) {
- connections.addAll(((HolonSwitch) aco).getAllConnectedHolons(new ArrayList<AbstractCanvasObject>()));
- break;
- } else if (aco instanceof Node && ((Node) aco).getUniqueID().equals(objectID)) {
- connections.addAll(((Node) aco).getAllConnectedHolons(new ArrayList<AbstractCanvasObject>()));
- break;
- } else if(aco instanceof GroupNode && ((GroupNode) aco).getUniqueID().equals(objectID)) {
- connections.addAll(((GroupNode) aco).getAllConnectedHolons(new ArrayList<>()));
- break;
- }
- }
- connections.remove(requester);
-
- return connections;
- }
- }
|