Model.java 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064
  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. // System.out.println("something is wrong "+current+" "+dist);
  830. return null;
  831. }
  832. float currDist = dist.get(current);
  833. for(AbstractCanvasObject aco : current.getConnectedObjects(0f)) {
  834. if(visited.contains(aco) || (aco instanceof HolonObject && !holarchy.contains(aco))) {
  835. continue;
  836. }
  837. float newDist = currDist + getEdgeBetweenObjects(current, aco, this.edgesOnCanvas).getLength();
  838. if(newDist < dist.get(aco)) {
  839. dist.put(aco, newDist);
  840. pre.put(aco, current);
  841. }
  842. }
  843. AbstractCanvasObject next = getMinInMap(dist, unvisited);
  844. visited.add(current);
  845. unvisited.remove(current);
  846. current = next;
  847. }
  848. //trace back path
  849. current = b;
  850. while(!a.equals(current)) {
  851. AbstractCanvasObject next = pre.get(current);
  852. es.add(getEdgeBetweenObjects(current, next, edges));
  853. current = next;
  854. }
  855. return es;
  856. }
  857. public AbstractCanvasObject getMinInMap(HashMap<AbstractCanvasObject, Float> map, ArrayList<AbstractCanvasObject> unvisited) {
  858. AbstractCanvasObject min = null;
  859. float m = Float.MAX_VALUE;
  860. for(AbstractCanvasObject aco : map.keySet()) {
  861. if(unvisited.contains(aco) && map.get(aco) < m) {
  862. m = map.get(aco);
  863. min = aco;
  864. }
  865. }
  866. return min;
  867. }
  868. public Edge getEdgeBetweenObjects(AbstractCanvasObject a, AbstractCanvasObject b, ArrayList<Edge> edges) {
  869. for(Edge e : edges) {
  870. AbstractCanvasObject a2 = e.getA();
  871. AbstractCanvasObject b2 = e.getB();
  872. if( (a.equals(a2) && b.equals(b2)) || (a.equals(b2) && b.equals(a2)) ) {
  873. return e;
  874. }
  875. }
  876. return null;
  877. }
  878. public boolean checkHolonObjectsAreConnected(Holon a, Holon b, float throughput) {
  879. if(a == null || b == null || a.equals(b)) {
  880. return false;
  881. }
  882. HashSet<AbstractCanvasObject> visited = new HashSet<AbstractCanvasObject>();
  883. ArrayList<AbstractCanvasObject> objects = new ArrayList<AbstractCanvasObject>();
  884. objects.addAll(a.getAllHolonObjects());
  885. objects.addAll(b.getAllHolonObjects());
  886. ArrayList<AbstractCanvasObject> toVisit = new ArrayList<AbstractCanvasObject>();
  887. toVisit.add(a.getHolonObject());
  888. while(!toVisit.isEmpty()) {
  889. AbstractCanvasObject aco = toVisit.remove(0);
  890. for(AbstractCanvasObject aco2 : aco.getConnectedObjects(throughput)) {
  891. if(visited.contains(aco2) || (aco2 instanceof HolonSwitch && !((HolonSwitch)aco2).isClosed()) ) {
  892. continue;
  893. }
  894. if(aco2 instanceof HolonObject && aco2.equals(b.getHolonObject())) {
  895. return true;
  896. }
  897. if( aco2 instanceof HolonSwitch || aco2 instanceof Node || (aco2 instanceof HolonObject && objects.contains(aco2)) ) {
  898. toVisit.add(aco2);
  899. }
  900. }
  901. visited.add(aco);
  902. }
  903. return false;
  904. }
  905. public ArrayList<String> getAllConnectionsFromObject(String objectID, String requester){
  906. ArrayList<String> connections = new ArrayList<String>();
  907. List<AbstractCanvasObject> objects = this.objectsOnCanvas.stream()
  908. .map(aco -> aco instanceof GroupNode ? ((GroupNode) aco).getAllObjectsInside() : List.of(aco))
  909. .flatMap(List::stream)
  910. .collect(Collectors.toList());
  911. for(AbstractCanvasObject aco : objects) {
  912. if(aco instanceof HolonSwitch && ((HolonSwitch) aco).getUniqueID().equals(objectID)) {
  913. connections.addAll(((HolonSwitch) aco).getAllConnectedHolons(new ArrayList<AbstractCanvasObject>()));
  914. break;
  915. } else if (aco instanceof Node && ((Node) aco).getUniqueID().equals(objectID)) {
  916. connections.addAll(((Node) aco).getAllConnectedHolons(new ArrayList<AbstractCanvasObject>()));
  917. break;
  918. } else if(aco instanceof GroupNode && ((GroupNode) aco).getUniqueID().equals(objectID)) {
  919. connections.addAll(((GroupNode) aco).getAllConnectedHolons(new ArrayList<>()));
  920. break;
  921. }
  922. }
  923. connections.remove(requester);
  924. return connections;
  925. }
  926. }