Model.java 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106
  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. // /* //TODO: remove
  135. // Blackstart needs
  136. // */
  137. // private StorageProductionController storageProductionController;
  138. /**
  139. * Constructor for the model. It initializes the categories and
  140. * objectsOnCanvas by default values. Listeners are also initialized by
  141. * default values.
  142. */
  143. public Model() {
  144. setCategories(new ArrayList<>());
  145. setObjectsOnCanvas(new ArrayList<>());
  146. setEdgesOnCanvas(new ArrayList<>());
  147. setCategoryListeners(new LinkedList<>());
  148. setObjectListeners(new LinkedList<>());
  149. setCgIdx(new HashMap<>());
  150. setCvsObjIdx(new HashMap<>());
  151. setClipboradObjects(new ArrayList<>());
  152. setTrackingObj(new ArrayList<>());
  153. setEleToDelete(new HashMap<>());
  154. setSingleTable(new PropertyTable());
  155. setMultiTable(new PropertyTable());
  156. setPropertyTable(new DefaulTable(1000, colNames.length));
  157. getPropertyTable().setColumnIdentifiers(colNames);
  158. setTableHolonElement(new JTable());
  159. // storageProductionController = new StorageProductionController(); //TODO:remove
  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<AbstractCpsObject> 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<AbstractCpsObject> objectsOnCanvas) {
  209. this.objectsOnCanvas = objectsOnCanvas;
  210. }
  211. /**
  212. * Get all Edges on the Canvas.
  213. *
  214. * @return the edgesOnCanvas
  215. */
  216. public ArrayList<CpsEdge> 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<CpsEdge> 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(CpsEdge 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(CpsEdge 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 AbstractCpsObject getSelectedCpsObject() {
  297. return selectedCpsObject;
  298. }
  299. /**
  300. * Set the Selected Objecs.
  301. *
  302. * @param selectedCpsObject Objects that are selected
  303. */
  304. public void setSelectedCpsObject(AbstractCpsObject 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<AbstractCpsObject> 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<AbstractCpsObject> 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 CpsEdge getSelectedEdge() {
  403. return selectedEdge;
  404. }
  405. /**
  406. * Set the selected Edge.
  407. *
  408. * @param edge that is selected
  409. */
  410. public void setSelectedEdge(CpsEdge 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<AbstractCpsObject> getClipboradObjects() {
  483. return clipboardObjects;
  484. }
  485. /**
  486. * Sets the ClipboardObjects.
  487. *
  488. * @param c Array of Objects
  489. */
  490. public void setClipboradObjects(ArrayList<AbstractCpsObject> 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<AbstractCpsObject> getTrackingObj() {
  587. return trackedObjects;
  588. }
  589. public void setTrackingObj(ArrayList<AbstractCpsObject> 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 ArrayList<HolonObject> getAllHolonObjectsOnCanvas(){
  676. ArrayList<HolonObject> objectToReturn = new ArrayList<HolonObject>();
  677. getAllHolonObjectsRecursive(objectToReturn, getObjectsOnCanvas());
  678. return objectToReturn;
  679. }
  680. private void getAllHolonObjectsRecursive(ArrayList<HolonObject> addObjectsToThisList, List<AbstractCpsObject> listOfObjectsToSearch){
  681. for(AbstractCpsObject aCps : listOfObjectsToSearch) {
  682. if(aCps instanceof HolonObject) {
  683. addObjectsToThisList.add((HolonObject) aCps);
  684. }else if(aCps instanceof CpsUpperNode){
  685. getAllHolonObjectsRecursive(addObjectsToThisList, ((CpsUpperNode)aCps).getNodes());
  686. }
  687. }
  688. }
  689. /**
  690. * get all Switches
  691. */
  692. public ArrayList<HolonSwitch> getSwitches() {
  693. ArrayList<HolonSwitch> switches = new ArrayList<>();
  694. for (AbstractCpsObject obj : getObjectsOnCanvas()) {
  695. if (obj instanceof HolonSwitch) {
  696. switches.add((HolonSwitch) obj);
  697. } else if (obj instanceof CpsUpperNode) {
  698. getSwitchesRec(((CpsUpperNode) obj).getNodes(), switches);
  699. }
  700. }
  701. return switches;
  702. }
  703. /**
  704. * get the Amount of Switches help function
  705. *
  706. * @param objects objects
  707. * @param switches List of switches
  708. */
  709. private ArrayList<HolonSwitch> getSwitchesRec(ArrayList<AbstractCpsObject> objects,
  710. ArrayList<HolonSwitch> switches) {
  711. for (AbstractCpsObject obj : objects) {
  712. if (obj instanceof HolonSwitch) {
  713. switches.add((HolonSwitch) obj);
  714. } else if (obj instanceof CpsUpperNode) {
  715. getSwitchesRec(((CpsUpperNode) obj).getNodes(), switches);
  716. }
  717. }
  718. return switches;
  719. }
  720. /**
  721. * Returns the Path for the background Image of the Canvas.
  722. *
  723. * @return imgPath the Path
  724. */
  725. public String getCanvasImagePath() {
  726. return imgPath;
  727. }
  728. /**
  729. * Set the Path for the background Image of the Canvas.
  730. *
  731. * @param path the Path
  732. */
  733. public void setCanvasImagePath(String path) {
  734. imgPath = path;
  735. }
  736. /**
  737. * Returns the mode for the background Image of the Canvas.
  738. * <p>
  739. * 0 take size of the Image 1 stretch the Image 2 Custom Image size
  740. *
  741. * @return backgroundMode the mode
  742. */
  743. public int getCanvasImageMode() {
  744. return backgroundMode;
  745. }
  746. /**
  747. * Set the mode for the background Image of the Canvas.
  748. * <p>
  749. * 0 take size of the Image, 1 stretch the Image, 2 Custom Image size
  750. *
  751. * @param mode the backgroundMode
  752. */
  753. public void setCanvasImageMode(int mode) {
  754. backgroundMode = mode;
  755. }
  756. /**
  757. * Returns the Custom width of the background Image of the Canvas.
  758. *
  759. * @return backgroundWidth the Width
  760. */
  761. public int getCanvasImageWidth() {
  762. return backgroundWidth;
  763. }
  764. /**
  765. * Set the Custom width of the background Image of the Canvas.
  766. *
  767. * @param width the Width
  768. */
  769. public void setCanvasImageWidth(int width) {
  770. backgroundWidth = width;
  771. }
  772. /**
  773. * Returns the Custom height of the background Image of the Canvas.
  774. *
  775. * @return backgroundHeight the height
  776. */
  777. public int getCanvasImageHeight() {
  778. return backgroundHeight;
  779. }
  780. /**
  781. * Set the Custom height of the background Image of the Canvas.
  782. *
  783. * @param height the height
  784. */
  785. public void setCanvasImageHeight(int height) {
  786. backgroundHeight = height;
  787. }
  788. /**
  789. * Returns the graphtable for Statistic Graphs.
  790. */
  791. public Hashtable<String, StatisticGraphPanel> getGraphTable() {
  792. return statisticGraphTable;
  793. }
  794. /**
  795. * Set the graphtable for Statistic Graphs
  796. */
  797. public void setGraphTable(Hashtable<String, StatisticGraphPanel> gT) {
  798. statisticGraphTable = gT;
  799. }
  800. /**
  801. * Returns if the Simulation is running.
  802. */
  803. public boolean getIsSimRunning() {
  804. return isSimRunning;
  805. }
  806. /**
  807. * Sets isSimRunning.
  808. *
  809. * @param isRunning
  810. */
  811. public void setIsSimRunning(boolean isRunning) {
  812. isSimRunning = isRunning;
  813. }
  814. /**
  815. * @return the statisticData
  816. */
  817. public ArrayList<JsonObject> getStatisticData() {
  818. return statisticData;
  819. }
  820. /**
  821. * @param statisticData the statisticData to set
  822. */
  823. public void setStatisticData(ArrayList<JsonObject> statisticData) {
  824. this.statisticData = statisticData;
  825. }
  826. /**
  827. * Returns showConsoleLog.
  828. */
  829. public boolean getShowConsoleLog() {
  830. return this.showConsoleLog;
  831. }
  832. /**
  833. * Sets showConsoleLog.
  834. *
  835. * @param showConsoleLog
  836. */
  837. public void setShowConsoleLog(boolean showConsoleLog) {
  838. this.showConsoleLog = showConsoleLog;
  839. }
  840. public boolean useFlexibleDevices() {
  841. return this.useFlexibleDevices;
  842. }
  843. public void setUseFlexibleDevices(boolean useFlexibleDevices) {
  844. this.useFlexibleDevices = useFlexibleDevices;
  845. }
  846. /**
  847. * @return true if SupplyBars should be shown
  848. */
  849. public boolean getShowSupplyBars() {
  850. return showSupplyBars;
  851. }
  852. /**
  853. * @param showSupplyBars true if the SupplyBars should be shown
  854. */
  855. public void setShowSupplyBars(boolean showSupplyBars) {
  856. this.showSupplyBars = showSupplyBars;
  857. }
  858. /**
  859. * @param iterations the number of steps for this simulation
  860. */
  861. public void setIterations(int iterations){
  862. this.iterations=iterations;
  863. }
  864. /**
  865. * @return the fairnessModel
  866. */
  867. public FairnessModel getFairnessModel() {
  868. return fairnessModel;
  869. }
  870. /**
  871. * @param fairnessModel the fairnessModel to set
  872. */
  873. public void setFairnessModel(FairnessModel fairnessModel) {
  874. this.fairnessModel = fairnessModel;
  875. }
  876. public int getGraphIterations(){
  877. return GRAPH_ITERATIONS;
  878. }
  879. /**
  880. * Initialize the Gson with wanted parameters
  881. */
  882. private void initGson() {
  883. GsonBuilder builder = new GsonBuilder();
  884. builder.serializeNulls();
  885. builder.excludeFieldsWithoutExposeAnnotation();
  886. builder.setPrettyPrinting();
  887. builder.registerTypeAdapter(AbstractCpsObject.class, new AbstractCpsObjectAdapter());
  888. builder.registerTypeAdapter(Position.class, new PositionAdapter());
  889. builder.registerTypeAdapter(Color.class, new ColorAdapter());
  890. builder.registerTypeAdapter(Pair.class, new PairAdapter());
  891. // use the builder and make a instance of the Gson
  892. this.setGson(builder.create());
  893. }
  894. /**
  895. * @return the gson
  896. */
  897. public Gson getGson() {
  898. return gson;
  899. }
  900. /**
  901. * @param gson the gson to set
  902. */
  903. public void setGson(Gson gson) {
  904. this.gson = gson;
  905. }
  906. public StatisticPanel getStatPanel() {
  907. return statPanel;
  908. }
  909. public void setStatPanel(StatisticPanel sP) {
  910. statPanel = sP;
  911. }
  912. /**
  913. * @return the hashcodeMap
  914. */
  915. public HashMap<Integer, CpsUpperNode> getHashcodeMap() {
  916. return hashcodeMap;
  917. }
  918. /**
  919. * @param hashcodeMap the hashcodeMap to set
  920. */
  921. public void setHashcodeMap(HashMap<Integer, CpsUpperNode> hashcodeMap) {
  922. this.hashcodeMap = hashcodeMap;
  923. }
  924. public ArrayList<HolonSwitch> getSwitchsOnCanvas() {
  925. return switchsOnCanvas;
  926. }
  927. public void setSwitchsOnCanvas(ArrayList<HolonSwitch> switchsOnCanvas) {
  928. this.switchsOnCanvas = switchsOnCanvas;
  929. }
  930. public ArrayList<CpsNode> getNodesOnCanvas() {
  931. return nodesOnCanvas;
  932. }
  933. public void setNodesOnCanvas(ArrayList<CpsNode> nodesOnCanvas) {
  934. this.nodesOnCanvas = nodesOnCanvas;
  935. }
  936. public ArrayList<HolonObject> getHolonObjectsOnCanvas() {
  937. return holonObjectsOnCanvas;
  938. }
  939. public void setHolonObjectsOnCanvas(ArrayList<HolonObject> holonObjectsOnCanvas) {
  940. this.holonObjectsOnCanvas = holonObjectsOnCanvas;
  941. }
  942. public void defineLists() {
  943. switchsOnCanvas.clear();
  944. nodesOnCanvas.clear();
  945. holonObjectsOnCanvas.clear();
  946. for(AbstractCpsObject aCps : this.objectsOnCanvas) {
  947. if(aCps instanceof HolonObject)holonObjectsOnCanvas.add((HolonObject) aCps);
  948. else if(aCps instanceof CpsNode)nodesOnCanvas.add((CpsNode) aCps);
  949. else if(aCps instanceof HolonSwitch)switchsOnCanvas.add((HolonSwitch) aCps);
  950. }
  951. }
  952. }