Model.java 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063
  1. package ui.model;
  2. import java.awt.Color;
  3. import java.util.ArrayList;
  4. import java.util.Arrays;
  5. import java.util.Collection;
  6. import java.util.Collections;
  7. import java.util.Comparator;
  8. import java.util.HashMap;
  9. import java.util.HashSet;
  10. import java.util.Iterator;
  11. import java.util.LinkedList;
  12. import java.util.List;
  13. import java.util.Map;
  14. import java.util.stream.Collectors;
  15. import javax.swing.JTable;
  16. import com.google.gson.Gson;
  17. import com.google.gson.GsonBuilder;
  18. import TypeAdapter.AbstractCpsObjectAdapter;
  19. import TypeAdapter.ColorAdapter;
  20. import TypeAdapter.PairAdapter;
  21. import TypeAdapter.PositionAdapter;
  22. import classes.AbstractCanvasObject;
  23. import classes.Category;
  24. import classes.Edge;
  25. import classes.GroupNode;
  26. import classes.Holon;
  27. import classes.HolonElement;
  28. import classes.HolonObject;
  29. import classes.HolonSwitch;
  30. import classes.Node;
  31. import classes.Pair;
  32. import classes.Position;
  33. import interfaces.GraphListener;
  34. import interfaces.ObjectListener;
  35. import ui.view.DefaulTable;
  36. import ui.view.PropertyTable;
  37. /**
  38. * The Class Model is the class where everything is saved. All changes made to
  39. * the Data is managed via a controller.
  40. *
  41. * @author Gruppe14
  42. */
  43. public class Model {
  44. private static final int GRAPH_ITERATIONS = 100;
  45. // Global Variables
  46. private static int sCALE = 50; // Picture Scale
  47. private static int sCALEdIV2 = sCALE / 2;
  48. public String[] colNames = {"Field", "Information"};
  49. // Canvas Attributes
  50. private String imgPath = "";
  51. private int backgroundMode = 0;
  52. private int backgroundWidth = 0;
  53. private int backgroundHeight = 0;
  54. private int canvasX = 3000;
  55. private int canvasY = 3000;
  56. private int curIteration = 0;
  57. private HolonElement selectedHolonElement;
  58. private Edge selectedEdge;
  59. private ArrayList<AbstractCanvasObject> selectedObjects = new ArrayList<>();
  60. private ArrayList<AbstractCanvasObject> clipboardObjects = new ArrayList<>();
  61. private HashMap<Integer, ArrayList<HolonElement>> eleToDelete;
  62. // Capacity for Edge
  63. private float maxCapacity;
  64. // Table for HolonElements --> all cells are editable
  65. private JTable tableHolonElement;
  66. /** Table for the properties of HolonObjects, Edges etc */
  67. private JTable propertyTable;
  68. private ArrayList<GraphListener> graphListeners = new ArrayList<GraphListener>();
  69. // Iteration Speed
  70. private int timerSpeed = 1000;
  71. private int selectedID = 0;
  72. // number of the current autosave
  73. private int autoSaveNr = -1;
  74. // number of max simultaneous autosaves
  75. private int numberOfSaves = 35;
  76. /** whether the supplyBars should be shown or not */
  77. private boolean showSupplyBars = true;
  78. //TODO:
  79. private int iterations=100;
  80. /**
  81. * All implemented FairnessModels:<br>
  82. * {@link FairnessModel#MininumDemandFirst}<br>
  83. * {@link FairnessModel#AllEqual}
  84. */
  85. public enum FairnessModel{
  86. /**
  87. * One Element of each HolonObject will be powered first, starting with the
  88. * smallest Demand. If ale HolonObjects have an active Element, the
  89. * simulation will try to fully supply as many HolonObjects as possible.
  90. */
  91. MininumDemandFirst,
  92. /**
  93. * All HolonObjects will receive the same amount of energy.
  94. */
  95. AllEqual
  96. }
  97. /** the Fairness model in use */
  98. private FairnessModel fairnessModel = FairnessModel.MininumDemandFirst;
  99. /*
  100. * Array of all categories in the model. It is set by default with the
  101. * categories ENERGY, BUILDINGS and COMPONENTS
  102. */
  103. private ArrayList<Category> categories;
  104. /*
  105. * Array of all CpsObjects in our canvas. It is set by default as an empty
  106. * list.
  107. */
  108. private ArrayList<AbstractCanvasObject> objectsOnCanvas;
  109. private HashMap<String, Integer> cgIdx;
  110. private HashMap<Integer, Integer> cvsObjIdx;
  111. /*
  112. * Array of all CpsObjects in our canvas. It is set by default as an empty
  113. * list.
  114. */
  115. private ArrayList<Edge> edgesOnCanvas;
  116. private ArrayList<HolonObject> holonObjectsOnCanvas = new ArrayList<HolonObject>();
  117. private ArrayList<Node> nodesOnCanvas= new ArrayList<Node>();
  118. private ArrayList<HolonSwitch> switchsOnCanvas= new ArrayList<HolonSwitch>();
  119. private List<ObjectListener> objectListeners;
  120. private PropertyTable tableModelHolonElementMulti;
  121. private PropertyTable tableModelHolonElementSingle;
  122. private DefaulTable tableModelProperties;
  123. private HashMap<Integer, GroupNode> hashcodeMap = new HashMap<>();
  124. private Holon stateHolon = new Holon("All Holons", this);
  125. private Map<String, Holon> holonsByID = new HashMap();
  126. private Gson gson;
  127. /**
  128. * Constructor for the model. It initializes the categories and
  129. * objectsOnCanvas by default values. Listeners are also initialized by
  130. * default values.
  131. */
  132. public Model() {
  133. setCategories(new ArrayList<>());
  134. setObjectsOnCanvas(new ArrayList<>());
  135. setEdgesOnCanvas(new ArrayList<>());
  136. setObjectListeners(new LinkedList<>());
  137. setCgIdx(new HashMap<>());
  138. setCvsObjIdx(new HashMap<>());
  139. setClipboradObjects(new ArrayList<>());
  140. setEleToDelete(new HashMap<>());
  141. setSingleTable(new PropertyTable());
  142. setMultiTable(new PropertyTable());
  143. setPropertyTable(new DefaulTable(1000, colNames.length));
  144. getPropertyTable().setColumnIdentifiers(colNames);
  145. setTableHolonElement(new JTable());
  146. initGson();
  147. this.holonsByID.put(stateHolon.getUniqueID(), stateHolon);
  148. }
  149. /**
  150. * Returns all Categories.
  151. *
  152. * @return the categories
  153. */
  154. public ArrayList<Category> getCategories() {
  155. return categories;
  156. }
  157. /**
  158. * Sets all Categories.
  159. *
  160. * @param categories the categories to set
  161. */
  162. public void setCategories(ArrayList<Category> categories) {
  163. this.categories = categories;
  164. }
  165. /**
  166. * Transform the Arraylist of categories into a string of all objectName
  167. * with a separation (',') between each name.
  168. *
  169. * @return String of all names separeted by ','
  170. */
  171. public String toStringCat() {
  172. String text = "";
  173. for (int i = 0; i < categories.size(); i++) {
  174. if (text.equals("")) {
  175. text = categories.get(i).getName();
  176. } else {
  177. text = text + ", " + categories.get(i).getName();
  178. }
  179. }
  180. return text;
  181. }
  182. /**
  183. * Returns all Objects on the Canvas.
  184. *
  185. * @return the objectsOnCanvas
  186. */
  187. public ArrayList<AbstractCanvasObject> getObjectsOnCanvas() {
  188. return objectsOnCanvas;
  189. }
  190. /**
  191. * Sets all Objects on the Canvas.
  192. *
  193. * @param objectsOnCanvas the objectsOnCanvas to set
  194. */
  195. public void setObjectsOnCanvas(ArrayList<AbstractCanvasObject> objectsOnCanvas) {
  196. this.objectsOnCanvas = objectsOnCanvas;
  197. }
  198. /**
  199. * Get all Edges on the Canvas.
  200. *
  201. * @return the edgesOnCanvas
  202. */
  203. public ArrayList<Edge> getEdgesOnCanvas() {
  204. return edgesOnCanvas;
  205. }
  206. /**
  207. * Sets the edges on the Canvas.
  208. *
  209. * @param arrayList the edgesOnCanvas to set
  210. */
  211. public void setEdgesOnCanvas(ArrayList<Edge> arrayList) {
  212. this.edgesOnCanvas = arrayList;
  213. }
  214. /**
  215. * Adds an Edge to The Canvas.
  216. *
  217. * @param edge the edgesOnCanvas to add
  218. */
  219. public void addEdgeOnCanvas(Edge edge) {
  220. this.edgesOnCanvas.add(edge);
  221. }
  222. /**
  223. * Remove an edge from the Canvas.
  224. *
  225. * @param edge the edge to remove
  226. */
  227. public void removeEdgesOnCanvas(Edge edge) {
  228. this.edgesOnCanvas.remove(edge);
  229. }
  230. /**
  231. * Returns the ObjectListener.
  232. *
  233. * @return the objectListeners
  234. */
  235. public List<ObjectListener> getObjectListeners() {
  236. return objectListeners;
  237. }
  238. /**
  239. * Sets the ObjectListener.
  240. *
  241. * @param linkedList the objectListeners to set
  242. */
  243. public void setObjectListeners(LinkedList<ObjectListener> linkedList) {
  244. this.objectListeners = linkedList;
  245. }
  246. /**
  247. * Returns the ID of the selected Object 0 = no Object is selected.
  248. *
  249. * @return ID
  250. */
  251. public int getSelectedObjectID() {
  252. return selectedID;
  253. }
  254. /**
  255. * Set the ID of the selected Object 0 = no Object is selected.
  256. *
  257. * @param id the ID
  258. */
  259. public void setSelectedObjectID(int id) {
  260. this.selectedID = id;
  261. }
  262. /**
  263. * Returns all selected Objects on the Canvas.
  264. *
  265. * @return The selected Objects
  266. */
  267. public ArrayList<AbstractCanvasObject> getSelectedCpsObjects() {
  268. return selectedObjects;
  269. }
  270. /**
  271. * Returns all selected Objects on the Canvas.
  272. *
  273. * @return The selected Objects
  274. */
  275. public void setSelectedCpsObjects(ArrayList<AbstractCanvasObject> arr) {
  276. this.selectedObjects = arr;
  277. }
  278. /**
  279. * Returns the Selected Holon Element.
  280. *
  281. * @return selected Holon Element
  282. */
  283. public HolonElement getSelectedHolonElement() {
  284. return selectedHolonElement;
  285. }
  286. /**
  287. * Sets the Selecte HolonElement.
  288. *
  289. * @param selectedHolonElement that is Selected
  290. */
  291. public void setSelectedHolonElement(HolonElement selectedHolonElement) {
  292. this.selectedHolonElement = selectedHolonElement;
  293. }
  294. /**
  295. * Returns the sCale (Scale for the Images).
  296. *
  297. * @return sCALE
  298. */
  299. public int getScale() {
  300. return sCALE;
  301. }
  302. public Holon getStateHolon() {
  303. return stateHolon;
  304. }
  305. /**
  306. * Sets the Image Scale.
  307. *
  308. * @param scale for the image
  309. */
  310. public void setScale(int scale) {
  311. sCALE = scale;
  312. if ((sCALE & 1) == 0)
  313. sCALEdIV2 = sCALE / 2;
  314. else
  315. sCALEdIV2 = (sCALE + 1) / 2;
  316. }
  317. /**
  318. * Returns sCALEdIV2 (The Scale divided by 2).
  319. *
  320. * @return sCALEdIV2
  321. */
  322. public int getScaleDiv2() {
  323. return sCALEdIV2;
  324. }
  325. /**
  326. * Returns the maximum ITERATIONS.
  327. *
  328. * @return ITERATIONS
  329. */
  330. public int getIterations() {
  331. return iterations;
  332. }
  333. private void notifyGraphListeners() {
  334. for (GraphListener gl : graphListeners) {
  335. gl.repaintTree();
  336. }
  337. }
  338. /**
  339. * Returns cURiTERATION.
  340. *
  341. * @return cURiTERATION
  342. */
  343. public int getCurIteration() {
  344. return curIteration;
  345. }
  346. /**
  347. * sets the current Iteration.
  348. *
  349. * @param curIT the current Iteration
  350. */
  351. public void setCurIteration(int curIT) {
  352. this.curIteration = curIT;
  353. notifyGraphListeners();
  354. }
  355. /**
  356. * Returns the selected Edge.
  357. *
  358. * @return selectedEdge
  359. */
  360. public Edge getSelectedEdge() {
  361. return selectedEdge;
  362. }
  363. /**
  364. * Set the selected Edge.
  365. *
  366. * @param edge that is selected
  367. */
  368. public void setSelectedEdge(Edge edge) {
  369. this.selectedEdge = edge;
  370. }
  371. /**
  372. * Returns the Categorie Index.
  373. *
  374. * @return the cgIdx
  375. */
  376. public HashMap<String, Integer> getCgIdx() {
  377. return cgIdx;
  378. }
  379. /**
  380. * Sets the Categorie Index.
  381. *
  382. * @param cgIdx the cgIdx to set
  383. */
  384. public void setCgIdx(HashMap<String, Integer> cgIdx) {
  385. this.cgIdx = cgIdx;
  386. }
  387. /**
  388. * Returns the CanvasObject Index.
  389. *
  390. * @return the cvsObjIdx
  391. */
  392. public HashMap<Integer, Integer> getCvsObjIdx() {
  393. return cvsObjIdx;
  394. }
  395. /**
  396. * Sets the CanvasObject Index.
  397. *
  398. * @param cvsObjIdx the cvsObjIdx to set
  399. */
  400. public void setCvsObjIdx(HashMap<Integer, Integer> cvsObjIdx) {
  401. this.cvsObjIdx = cvsObjIdx;
  402. }
  403. /**
  404. * Returns the auto save Number.
  405. *
  406. * @return the auto save Number
  407. */
  408. public int getAutoSaveNr() {
  409. return autoSaveNr;
  410. }
  411. /**
  412. * Sets the auto save Number.
  413. *
  414. * @param autoSaveNr the auto save number
  415. */
  416. public void setAutoSaveNr(int autoSaveNr) {
  417. this.autoSaveNr = autoSaveNr;
  418. }
  419. /**
  420. * Returns the Number of Saves.
  421. *
  422. * @return the numberOfSaves
  423. */
  424. public int getNumberOfSaves() {
  425. return numberOfSaves;
  426. }
  427. /**
  428. * Set the Number of Saves.
  429. *
  430. * @param numberOfSaves the numberOfSaves to set
  431. */
  432. public void setNumberOfSaves(int numberOfSaves) {
  433. this.numberOfSaves = numberOfSaves;
  434. }
  435. /**
  436. * Returns all Objects in the Clipboard.
  437. *
  438. * @return Objects in the Clipboard
  439. */
  440. public ArrayList<AbstractCanvasObject> getClipboradObjects() {
  441. return clipboardObjects;
  442. }
  443. /**
  444. * Sets the ClipboardObjects.
  445. *
  446. * @param c Array of Objects
  447. */
  448. public void setClipboradObjects(ArrayList<AbstractCanvasObject> c) {
  449. this.clipboardObjects = c;
  450. }
  451. /**
  452. * @return the maxCapacity
  453. */
  454. public float getMaxCapacity() {
  455. return maxCapacity;
  456. }
  457. /**
  458. * @param maxCapacity the maxCapacity to set
  459. */
  460. public void setMaxCapacity(float maxCapacity) {
  461. this.maxCapacity = maxCapacity;
  462. }
  463. /**
  464. * get the Interval in ms between each Iteration.
  465. *
  466. * @return timerSpeed speed for the Iterations
  467. */
  468. public int getTimerSpeed() {
  469. return this.timerSpeed;
  470. }
  471. /**
  472. * Sets the Interval in ms between each Iteration.
  473. *
  474. * @param t speed for the Iterations
  475. */
  476. public void setTimerSpeed(int t) {
  477. this.timerSpeed = t;
  478. }
  479. /**
  480. * Get Canvas X Size.
  481. *
  482. * @return the cANVAS_X
  483. */
  484. public int getCanvasX() {
  485. return canvasX;
  486. }
  487. /**
  488. * Set Canvas X Size.
  489. *
  490. * @param canvasX the cANVAS_X to set
  491. */
  492. public void setCanvasX(int canvasX) {
  493. this.canvasX = canvasX;
  494. }
  495. /**
  496. * get Canvas Y size.
  497. *
  498. * @return the cANVAS_Y
  499. */
  500. public int getCanvasY() {
  501. return canvasY;
  502. }
  503. /**
  504. * Set Canvas Y size.
  505. *
  506. * @param canvasY the cANVAS_Y to set
  507. */
  508. public void setCanvasY(int canvasY) {
  509. this.canvasY = canvasY;
  510. }
  511. public HashMap<Integer, ArrayList<HolonElement>> getEleToDelete() {
  512. return this.eleToDelete;
  513. }
  514. public void setEleToDelete(HashMap<Integer, ArrayList<HolonElement>> theHash) {
  515. this.eleToDelete = theHash;
  516. }
  517. public PropertyTable getSingleTable() {
  518. return this.tableModelHolonElementSingle;
  519. }
  520. public void setSingleTable(PropertyTable pt) {
  521. this.tableModelHolonElementSingle = pt;
  522. }
  523. public PropertyTable getMultiTable() {
  524. return this.tableModelHolonElementMulti;
  525. }
  526. public void setMultiTable(PropertyTable pt) {
  527. this.tableModelHolonElementMulti = pt;
  528. }
  529. public DefaulTable getPropertyTable() {
  530. return this.tableModelProperties;
  531. }
  532. public void setPropertyTable(DefaulTable pt) {
  533. this.tableModelProperties = pt;
  534. }
  535. public JTable getTableHolonElement() {
  536. return tableHolonElement;
  537. }
  538. public void setTableHolonElement(JTable tableHolonElement) {
  539. this.tableHolonElement = tableHolonElement;
  540. }
  541. /**
  542. * @return the tableProperties
  543. */
  544. public JTable getTableProperties() {
  545. return propertyTable;
  546. }
  547. /**
  548. * @return the tableProperties
  549. */
  550. public void setTableProperties(JTable propertyTable) {
  551. this.propertyTable = propertyTable;
  552. }
  553. public List<HolonElement> getAllHolonElemnts() {
  554. return getAllHolonObjectsOnCanvas().stream().flatMap(hO -> hO.getElements().stream()).collect(Collectors.toList());
  555. }
  556. public ArrayList<HolonObject> getAllHolonObjectsOnCanvas(){
  557. ArrayList<HolonObject> objectToReturn = new ArrayList<HolonObject>();
  558. getAllHolonObjectsRecursive(objectToReturn, getObjectsOnCanvas());
  559. return objectToReturn;
  560. }
  561. private void getAllHolonObjectsRecursive(ArrayList<HolonObject> addObjectsToThisList, List<AbstractCanvasObject> listOfObjectsToSearch){
  562. for(AbstractCanvasObject aCps : listOfObjectsToSearch) {
  563. if(aCps instanceof HolonObject) {
  564. addObjectsToThisList.add((HolonObject) aCps);
  565. }else if(aCps instanceof GroupNode){
  566. getAllHolonObjectsRecursive(addObjectsToThisList, ((GroupNode)aCps).getNodes());
  567. }
  568. }
  569. }
  570. /**
  571. * get all Switches
  572. */
  573. public ArrayList<HolonSwitch> getAllSwitches() {
  574. ArrayList<HolonSwitch> switches = new ArrayList<>();
  575. for (AbstractCanvasObject obj : getObjectsOnCanvas()) {
  576. if (obj instanceof HolonSwitch) {
  577. switches.add((HolonSwitch) obj);
  578. } else if (obj instanceof GroupNode) {
  579. getSwitchesRec(((GroupNode) obj).getNodes(), switches);
  580. }
  581. }
  582. return switches;
  583. }
  584. /**
  585. * get the Amount of Switches help function
  586. *
  587. * @param objects objects
  588. * @param switches List of switches
  589. */
  590. private ArrayList<HolonSwitch> getSwitchesRec(ArrayList<AbstractCanvasObject> objects,
  591. ArrayList<HolonSwitch> switches) {
  592. for (AbstractCanvasObject obj : objects) {
  593. if (obj instanceof HolonSwitch) {
  594. switches.add((HolonSwitch) obj);
  595. } else if (obj instanceof GroupNode) {
  596. getSwitchesRec(((GroupNode) obj).getNodes(), switches);
  597. }
  598. }
  599. return switches;
  600. }
  601. /**
  602. * Returns the Path for the background Image of the Canvas.
  603. *
  604. * @return imgPath the Path
  605. */
  606. public String getCanvasImagePath() {
  607. return imgPath;
  608. }
  609. /**
  610. * Set the Path for the background Image of the Canvas.
  611. *
  612. * @param path the Path
  613. */
  614. public void setCanvasImagePath(String path) {
  615. imgPath = path;
  616. }
  617. /**
  618. * Returns the mode for the background Image of the Canvas.
  619. * <p>
  620. * 0 take size of the Image 1 stretch the Image 2 Custom Image size
  621. *
  622. * @return backgroundMode the mode
  623. */
  624. public int getCanvasImageMode() {
  625. return backgroundMode;
  626. }
  627. /**
  628. * Set the mode for the background Image of the Canvas.
  629. * <p>
  630. * 0 take size of the Image, 1 stretch the Image, 2 Custom Image size
  631. *
  632. * @param mode the backgroundMode
  633. */
  634. public void setCanvasImageMode(int mode) {
  635. backgroundMode = mode;
  636. }
  637. /**
  638. * Returns the Custom width of the background Image of the Canvas.
  639. *
  640. * @return backgroundWidth the Width
  641. */
  642. public int getCanvasImageWidth() {
  643. return backgroundWidth;
  644. }
  645. /**
  646. * Set the Custom width of the background Image of the Canvas.
  647. *
  648. * @param width the Width
  649. */
  650. public void setCanvasImageWidth(int width) {
  651. backgroundWidth = width;
  652. }
  653. /**
  654. * Returns the Custom height of the background Image of the Canvas.
  655. *
  656. * @return backgroundHeight the height
  657. */
  658. public int getCanvasImageHeight() {
  659. return backgroundHeight;
  660. }
  661. /**
  662. * Set the Custom height of the background Image of the Canvas.
  663. *
  664. * @param height the height
  665. */
  666. public void setCanvasImageHeight(int height) {
  667. backgroundHeight = height;
  668. }
  669. /**
  670. * @return true if SupplyBars should be shown
  671. */
  672. public boolean getShowSupplyBars() {
  673. return showSupplyBars;
  674. }
  675. /**
  676. * @param showSupplyBars true if the SupplyBars should be shown
  677. */
  678. public void setShowSupplyBars(boolean showSupplyBars) {
  679. this.showSupplyBars = showSupplyBars;
  680. }
  681. /**
  682. * @param iterations the number of steps for this simulation
  683. */
  684. public void setIterations(int iterations){
  685. this.iterations=iterations;
  686. }
  687. /**
  688. * @return the fairnessModel
  689. */
  690. public FairnessModel getFairnessModel() {
  691. return fairnessModel;
  692. }
  693. /**
  694. * @param fairnessModel the fairnessModel to set
  695. */
  696. public void setFairnessModel(FairnessModel fairnessModel) {
  697. this.fairnessModel = fairnessModel;
  698. }
  699. public int getGraphIterations(){
  700. return GRAPH_ITERATIONS;
  701. }
  702. /**
  703. * Initialize the Gson with wanted parameters
  704. */
  705. private void initGson() {
  706. GsonBuilder builder = new GsonBuilder();
  707. builder.serializeNulls();
  708. builder.excludeFieldsWithoutExposeAnnotation();
  709. builder.setPrettyPrinting();
  710. builder.registerTypeAdapter(AbstractCanvasObject.class, new AbstractCpsObjectAdapter());
  711. builder.registerTypeAdapter(Position.class, new PositionAdapter());
  712. builder.registerTypeAdapter(Color.class, new ColorAdapter());
  713. builder.registerTypeAdapter(Pair.class, new PairAdapter());
  714. // use the builder and make a instance of the Gson
  715. this.setGson(builder.create());
  716. }
  717. /**
  718. * @return the gson
  719. */
  720. public Gson getGson() {
  721. return gson;
  722. }
  723. /**
  724. * @param gson the gson to set
  725. */
  726. public void setGson(Gson gson) {
  727. this.gson = gson;
  728. }
  729. /**
  730. * @return the hashcodeMap
  731. */
  732. public HashMap<Integer, GroupNode> getHashcodeMap() {
  733. return hashcodeMap;
  734. }
  735. /**
  736. * @param hashcodeMap the hashcodeMap to set
  737. */
  738. public void setHashcodeMap(HashMap<Integer, GroupNode> hashcodeMap) {
  739. this.hashcodeMap = hashcodeMap;
  740. }
  741. public ArrayList<HolonSwitch> getSwitchsOnCanvas() {
  742. return switchsOnCanvas;
  743. }
  744. public void setSwitchsOnCanvas(ArrayList<HolonSwitch> switchsOnCanvas) {
  745. this.switchsOnCanvas = switchsOnCanvas;
  746. }
  747. public ArrayList<Node> getNodesOnCanvas() {
  748. return nodesOnCanvas;
  749. }
  750. public void setNodesOnCanvas(ArrayList<Node> nodesOnCanvas) {
  751. this.nodesOnCanvas = nodesOnCanvas;
  752. }
  753. public ArrayList<HolonObject> getHolonObjectsOnCanvas() {
  754. return holonObjectsOnCanvas;
  755. }
  756. public void setHolonObjectsOnCanvas(ArrayList<HolonObject> holonObjectsOnCanvas) {
  757. this.holonObjectsOnCanvas = holonObjectsOnCanvas;
  758. }
  759. public void defineLists() {
  760. switchsOnCanvas.clear();
  761. nodesOnCanvas.clear();
  762. holonObjectsOnCanvas.clear();
  763. for(AbstractCanvasObject aCps : this.objectsOnCanvas) {
  764. if(aCps instanceof HolonObject)holonObjectsOnCanvas.add((HolonObject) aCps);
  765. else if(aCps instanceof Node)nodesOnCanvas.add((Node) aCps);
  766. else if(aCps instanceof HolonSwitch)switchsOnCanvas.add((HolonSwitch) aCps);
  767. }
  768. }
  769. public Map<String, Holon> getHolonsByID() {
  770. return holonsByID;
  771. }
  772. /**
  773. * get a list of paths between the two holarchies/minModels
  774. * list contains shortest path between each pair of objects (o1, o2) in (minModel x minModel2)
  775. * @param minModel
  776. * @param minModel2
  777. * @param holarchy
  778. * @return
  779. */
  780. public HashMap<Float, ArrayList<Edge>> getShortestPathToHolarchy(MinimumModel minModel, MinimumModel minModel2){
  781. ArrayList<AbstractCanvasObject> objects = new ArrayList<AbstractCanvasObject>();
  782. objects.addAll(minModel.getHolonObjectList());
  783. objects.addAll(minModel.getNodeList());
  784. objects.addAll(minModel.getSwitchList());
  785. ArrayList<AbstractCanvasObject> objects2 = new ArrayList<AbstractCanvasObject>();
  786. objects2.addAll(minModel2.getHolonObjectList());
  787. objects2.addAll(minModel2.getNodeList());
  788. objects2.addAll(minModel2.getSwitchList());
  789. List<AbstractCanvasObject> vertices = this.objectsOnCanvas.stream()
  790. .map(aco -> aco instanceof GroupNode ? ((GroupNode) aco).getAllObjectsInside() : List.of(aco))
  791. .flatMap(List::stream)
  792. .collect(Collectors.toList());
  793. HashMap<Float, ArrayList<Edge>> map = new HashMap<Float, ArrayList<Edge>>();
  794. for(AbstractCanvasObject aco : objects) {
  795. for(AbstractCanvasObject aco2 : objects2) {
  796. if(aco.equals(aco2))
  797. continue;
  798. ArrayList<Edge> edges = dijkstra(aco, aco2, minModel2.getHolonObjectList(), vertices, this.edgesOnCanvas);
  799. if(edges == null)
  800. continue;
  801. float dist = 0;
  802. for(Edge e : edges) {
  803. dist += e.getLength();
  804. }
  805. map.put(dist, edges);
  806. }
  807. }
  808. return map;
  809. }
  810. public ArrayList<Edge> dijkstra(AbstractCanvasObject a, AbstractCanvasObject b, ArrayList<HolonObject> holarchy,
  811. List<AbstractCanvasObject> vertices, ArrayList<Edge> edges) {
  812. ArrayList<Edge> es = new ArrayList<Edge>();
  813. HashSet<AbstractCanvasObject> visited = new HashSet<AbstractCanvasObject>();
  814. ArrayList<AbstractCanvasObject> unvisited = new ArrayList<AbstractCanvasObject>();
  815. HashMap<AbstractCanvasObject, Float> dist = new HashMap<AbstractCanvasObject, Float>();
  816. for(AbstractCanvasObject aco : vertices) {
  817. unvisited.add(aco);
  818. dist.put(aco, Float.MAX_VALUE);
  819. }
  820. dist.put(a, 0f);
  821. HashMap<AbstractCanvasObject, AbstractCanvasObject> pre = new HashMap<AbstractCanvasObject, AbstractCanvasObject>();
  822. AbstractCanvasObject current = a;
  823. visited.add(a);
  824. unvisited.remove(a);
  825. while(!unvisited.isEmpty()) {
  826. if(b.equals(current))
  827. break;
  828. if(!dist.containsKey(current)) {
  829. return null;
  830. }
  831. float currDist = dist.get(current);
  832. for(AbstractCanvasObject aco : current.getConnectedObjects()) {
  833. if(visited.contains(aco) || (aco instanceof HolonObject && !holarchy.contains(aco))) {
  834. continue;
  835. }
  836. float newDist = currDist + getEdgeBetweenObjects(current, aco, this.edgesOnCanvas).getLength();
  837. if(newDist < dist.get(aco)) {
  838. dist.put(aco, newDist);
  839. pre.put(aco, current);
  840. }
  841. }
  842. AbstractCanvasObject next = getMinInMap(dist, unvisited);
  843. visited.add(current);
  844. unvisited.remove(current);
  845. current = next;
  846. }
  847. //trace back path
  848. current = b;
  849. while(!a.equals(current)) {
  850. AbstractCanvasObject next = pre.get(current);
  851. es.add(getEdgeBetweenObjects(current, next, edges));
  852. current = next;
  853. }
  854. return es;
  855. }
  856. public AbstractCanvasObject getMinInMap(HashMap<AbstractCanvasObject, Float> map, ArrayList<AbstractCanvasObject> unvisited) {
  857. AbstractCanvasObject min = null;
  858. float m = Float.MAX_VALUE;
  859. for(AbstractCanvasObject aco : map.keySet()) {
  860. if(unvisited.contains(aco) && map.get(aco) < m) {
  861. m = map.get(aco);
  862. min = aco;
  863. }
  864. }
  865. return min;
  866. }
  867. public Edge getEdgeBetweenObjects(AbstractCanvasObject a, AbstractCanvasObject b, List<Edge> edges) {
  868. for(Edge e : edges) {
  869. AbstractCanvasObject a2 = e.getA();
  870. AbstractCanvasObject b2 = e.getB();
  871. if( (a.equals(a2) && b.equals(b2)) || (a.equals(b2) && b.equals(a2)) ) {
  872. return e;
  873. }
  874. }
  875. return null;
  876. }
  877. public boolean checkHolonObjectsAreConnected(Holon a, Holon b) {
  878. if(a == null || b == null || a.equals(b)) {
  879. return false;
  880. }
  881. HashSet<AbstractCanvasObject> visited = new HashSet<AbstractCanvasObject>();
  882. ArrayList<AbstractCanvasObject> objects = new ArrayList<AbstractCanvasObject>();
  883. ArrayList<AbstractCanvasObject> toVisit = new ArrayList<AbstractCanvasObject>();
  884. toVisit.add(a.getHolonObject());
  885. while(!toVisit.isEmpty()) {
  886. AbstractCanvasObject aco = toVisit.remove(0);
  887. if(aco == null)
  888. continue;
  889. for(AbstractCanvasObject aco2 : aco.getConnectedObjects()) {
  890. if(visited.contains(aco2) || (aco2 instanceof HolonSwitch && !((HolonSwitch)aco2).isClosed()) ) {
  891. continue;
  892. }
  893. if(aco2 instanceof HolonObject && aco2.equals(b.getHolonObject())) {
  894. return true;
  895. }
  896. if( aco2 instanceof HolonSwitch || aco2 instanceof Node || (aco2 instanceof HolonObject && objects.contains(aco2)) ) {
  897. toVisit.add(aco2);
  898. }
  899. }
  900. visited.add(aco);
  901. }
  902. return false;
  903. }
  904. public ArrayList<String> getAllConnectionsFromObject(String objectID, String requester){
  905. ArrayList<String> connections = new ArrayList<String>();
  906. List<AbstractCanvasObject> objects = this.objectsOnCanvas.stream()
  907. .map(aco -> aco instanceof GroupNode ? ((GroupNode) aco).getAllObjectsInside() : List.of(aco))
  908. .flatMap(List::stream)
  909. .collect(Collectors.toList());
  910. for(AbstractCanvasObject aco : objects) {
  911. if(aco instanceof HolonSwitch && ((HolonSwitch) aco).getUniqueID().equals(objectID)) {
  912. connections.addAll(((HolonSwitch) aco).getAllConnectedHolons(new ArrayList<AbstractCanvasObject>()));
  913. break;
  914. } else if (aco instanceof Node && ((Node) aco).getUniqueID().equals(objectID)) {
  915. connections.addAll(((Node) aco).getAllConnectedHolons(new ArrayList<AbstractCanvasObject>()));
  916. break;
  917. } else if(aco instanceof GroupNode && ((GroupNode) aco).getUniqueID().equals(objectID)) {
  918. connections.addAll(((GroupNode) aco).getAllConnectedHolons(new ArrayList<>()));
  919. break;
  920. }
  921. }
  922. connections.remove(requester);
  923. return connections;
  924. }
  925. }