Model.java 28 KB

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