Model.java 28 KB

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