Model.java 27 KB

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