Model.java 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068
  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 all edges that connect objects inside the holarchy
  774. * requires that all objects are connected
  775. * energy always takes the shortest path between two objects -> all shortest paths are included
  776. * all traversed objects are whether a switch or holon that is part of the holarchy
  777. * group nodes not supported
  778. * @param from
  779. * @param to
  780. * @return
  781. */
  782. public ArrayList<Edge> getAllEdgesInHolarchy(ArrayList<HolonObject> holarchy){
  783. if(holarchy.size() < 2) {
  784. return new ArrayList<Edge>();
  785. }
  786. //construct subgraph that contains all objects of the holarchy and all switches from the canvas
  787. ArrayList<AbstractCanvasObject> vertices = new ArrayList<AbstractCanvasObject>();
  788. for(AbstractCanvasObject aco : this.objectsOnCanvas) {
  789. if(aco instanceof HolonSwitch || aco instanceof Node
  790. || (aco instanceof HolonObject && holarchy.contains(aco)) ) {
  791. vertices.add(aco);
  792. }
  793. }
  794. ArrayList<Edge> edges = new ArrayList<Edge>();
  795. for(Edge e : this.edgesOnCanvas) {
  796. AbstractCanvasObject a = e.getA();
  797. AbstractCanvasObject b = e.getB();
  798. if(vertices.contains(a) && vertices.contains(b)) {
  799. edges.add(e);
  800. }
  801. }
  802. //find shortest paths between the objects
  803. ArrayList<Edge> es = new ArrayList<Edge>();
  804. if(holarchy.size() < 2) {
  805. return es;
  806. }
  807. for(int i=0; i<holarchy.size(); i++) {
  808. HolonObject a = holarchy.get(i);
  809. for(int j=i+1; j<holarchy.size(); j++) {
  810. es.addAll(dijkstra(a, holarchy.get(j), holarchy, vertices, edges));
  811. }
  812. }
  813. return es;
  814. }
  815. public HashMap<Float, ArrayList<Edge>> getShortestPathToHolarchy(MinimumModel minModel, MinimumModel minModel2, ArrayList<HolonObject> holarchy){
  816. ArrayList<AbstractCanvasObject> objects = new ArrayList<AbstractCanvasObject>();
  817. objects.addAll(minModel.getHolonObjectList());
  818. objects.addAll(minModel.getNodeList());
  819. objects.addAll(minModel.getSwitchList());
  820. ArrayList<AbstractCanvasObject> objects2 = new ArrayList<AbstractCanvasObject>();
  821. objects2.addAll(minModel2.getHolonObjectList());
  822. objects2.addAll(minModel2.getNodeList());
  823. objects2.addAll(minModel2.getSwitchList());
  824. HashMap<Float, ArrayList<Edge>> map = new HashMap<Float, ArrayList<Edge>>();
  825. for(AbstractCanvasObject aco : objects) {
  826. for(AbstractCanvasObject aco2 : objects2) {
  827. ArrayList<Edge> edges = dijkstra(aco, aco2, holarchy, this.objectsOnCanvas, this.edgesOnCanvas);
  828. float dist = 0;
  829. for(Edge e : edges) {
  830. dist += e.getLength();
  831. }
  832. map.put(dist, edges);
  833. }
  834. }
  835. return map;
  836. }
  837. public ArrayList<Edge> dijkstra(AbstractCanvasObject a, AbstractCanvasObject b, ArrayList<HolonObject> holarchy, ArrayList<AbstractCanvasObject> vertices, ArrayList<Edge> edges) {
  838. ArrayList<Edge> es = new ArrayList<Edge>();
  839. HashSet<AbstractCanvasObject> visited = new HashSet<AbstractCanvasObject>();
  840. ArrayList<AbstractCanvasObject> unvisited = new ArrayList<AbstractCanvasObject>();
  841. HashMap<AbstractCanvasObject, Float> dist = new HashMap<AbstractCanvasObject, Float>();
  842. for(AbstractCanvasObject aco : vertices) {
  843. unvisited.add(aco);
  844. dist.put(aco, Float.MAX_VALUE);
  845. }
  846. dist.put(a, 0f);
  847. HashMap<AbstractCanvasObject, AbstractCanvasObject> pre = new HashMap<AbstractCanvasObject, AbstractCanvasObject>();
  848. AbstractCanvasObject current = a;
  849. visited.add(a);
  850. unvisited.remove(a);
  851. while(!unvisited.isEmpty()) {
  852. if(b.equals(current))
  853. break;
  854. float currDist = dist.get(current);
  855. for(AbstractCanvasObject aco : current.getConnectedObjects()) {
  856. if(visited.contains(aco) || (aco instanceof HolonObject && !holarchy.contains(aco))) {
  857. continue;
  858. }
  859. float newDist = currDist + getEdgeBetweenObjects(current, aco, this.edgesOnCanvas).getLength();
  860. if(newDist < dist.get(aco)) {
  861. dist.put(aco, newDist);
  862. pre.put(aco, current);
  863. }
  864. }
  865. AbstractCanvasObject next = getMinInMap(dist, unvisited);
  866. visited.add(current);
  867. unvisited.remove(current);
  868. current = next;
  869. }
  870. //trace back path
  871. current = b;
  872. while(!current.equals(a)) {
  873. AbstractCanvasObject next = pre.get(current);
  874. es.add(getEdgeBetweenObjects(current, next, edges));
  875. current = next;
  876. }
  877. return es;
  878. }
  879. public AbstractCanvasObject getMinInMap(HashMap<AbstractCanvasObject, Float> map, ArrayList<AbstractCanvasObject> unvisited) {
  880. AbstractCanvasObject min = null;
  881. float m = Float.MAX_VALUE;
  882. for(AbstractCanvasObject aco : map.keySet()) {
  883. if(unvisited.contains(aco) && map.get(aco) < m) {
  884. // System.out.println("new min "+min);
  885. m = map.get(aco);
  886. min = aco;
  887. }
  888. }
  889. return min;
  890. }
  891. public Edge getEdgeBetweenObjects(AbstractCanvasObject a, AbstractCanvasObject b, ArrayList<Edge> edges) {
  892. for(Edge e : edges) {
  893. AbstractCanvasObject a2 = e.getA();
  894. AbstractCanvasObject b2 = e.getB();
  895. if( (a.equals(a2) && b.equals(b2)) || (a.equals(b2) && b.equals(a2)) ) {
  896. return e;
  897. }
  898. }
  899. return null;
  900. }
  901. public boolean checkHolonObjectsAreConnected(Holon a, Holon b) {
  902. if(a.equals(b)) {
  903. return false;
  904. }
  905. HashSet<AbstractCanvasObject> visited = new HashSet<AbstractCanvasObject>();
  906. ArrayList<AbstractCanvasObject> objects = new ArrayList<AbstractCanvasObject>();
  907. objects.addAll(a.getAllHolonObjects());
  908. objects.addAll(b.getAllHolonObjects());
  909. ArrayList<AbstractCanvasObject> toVisit = new ArrayList<AbstractCanvasObject>();
  910. toVisit.add(a.getHolonObject());
  911. while(!toVisit.isEmpty()) {
  912. AbstractCanvasObject aco = toVisit.remove(0);
  913. for(AbstractCanvasObject aco2 : aco.getConnectedObjects()) {
  914. if(visited.contains(aco2) || (aco2 instanceof HolonSwitch && !((HolonSwitch)aco2).isClosed()) ) {
  915. continue;
  916. }
  917. if(aco2 instanceof HolonObject && aco2.equals(b.getHolonObject())) {
  918. return true;
  919. }
  920. if( aco2 instanceof HolonSwitch || aco2 instanceof Node || (aco2 instanceof HolonObject && objects.contains(aco2)) ) {
  921. toVisit.add(aco2);
  922. }
  923. }
  924. visited.add(aco);
  925. }
  926. return false;
  927. }
  928. }