Model.java 28 KB

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