Model.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  1. package ui.model;
  2. import java.util.AbstractMap.SimpleEntry;
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.HashSet;
  6. import java.util.List;
  7. import java.util.Set;
  8. import java.util.stream.Collectors;
  9. import com.google.gson.Gson;
  10. import com.google.gson.GsonBuilder;
  11. import adapter.AbstractCpsObjectAdapter;
  12. import adapter.PairAdapter;
  13. import adapter.PositionAdapter;
  14. import model.AbstractCanvasObject;
  15. import model.Edge;
  16. import model.GroupNode;
  17. import model.HolonElement;
  18. import model.HolonObject;
  19. import model.HolonSwitch;
  20. import model.Node;
  21. import ui.view.main.Category;
  22. import utility.Vector2Int;
  23. /**
  24. * The Class Model is the class where everything is saved. All changes made to
  25. * the Data is managed via a controller.
  26. *
  27. * @author Gruppe14
  28. */
  29. public class Model {
  30. private static final int GRAPH_ITERATIONS = 100;
  31. // Global Variables
  32. private static int scale = 50; // Picture Scale
  33. private static int halfScale = scale / 2;
  34. // Canvas Attributes
  35. private String imgPath = "";
  36. private int backgroundMode = 0;
  37. private int backgroundWidth = 0;
  38. private int backgroundHeight = 0;
  39. private int canvasX = 3000;
  40. private int canvasY = 3000;
  41. private int curIteration = 0;
  42. private Edge selectedEdge;
  43. private Set<AbstractCanvasObject> selectedObjects = new HashSet<>();
  44. private Set<AbstractCanvasObject> clipboardObjects = new HashSet<>();
  45. // Capacity for Edge
  46. private float maxCapacity;
  47. // Iteration Speed
  48. private int timerSpeed = 1000;
  49. // number of the current autosave
  50. private int autoSaveNr = -1;
  51. // number of max simultaneous autosaves
  52. private int numberOfSaves = 35;
  53. /** whether the supplyBars should be shown or not */
  54. private boolean showSupplyBars = true;
  55. /** the amount of iterations */
  56. private int iterations=100;
  57. /**
  58. * All implemented FairnessModels:<br>
  59. * {@link FairnessModel#MininumDemandFirst}<br>
  60. * {@link FairnessModel#AllEqual}
  61. */
  62. public enum FairnessModel{
  63. /**
  64. * One Element of each HolonObject will be powered first, starting with the
  65. * smallest Demand. If ale HolonObjects have an active Element, the
  66. * simulation will try to fully supply as many HolonObjects as possible.
  67. */
  68. MininumDemandFirst,
  69. /**
  70. * All HolonObjects will receive the same amount of energy.
  71. */
  72. AllEqual
  73. }
  74. /** the Fairness model in use */
  75. private FairnessModel fairnessModel = FairnessModel.MininumDemandFirst;
  76. /*
  77. * Array of all categories in the model. It is set by default with the
  78. * categories ENERGY, BUILDINGS and COMPONENTS
  79. */
  80. private ArrayList<Category> categories;
  81. /*
  82. * Array of all CpsObjects in our canvas. It is set by default as an empty
  83. * list.
  84. */
  85. private ArrayList<AbstractCanvasObject> objectsOnCanvas;
  86. private HashMap<String, Integer> cgIdx;
  87. private HashMap<Integer, Integer> cvsObjIdx;
  88. /*
  89. * Array of all CpsObjects in our canvas. It is set by default as an empty
  90. * list.
  91. */
  92. private ArrayList<Edge> edgesOnCanvas;
  93. private ArrayList<HolonObject> holonObjectsOnCanvas = new ArrayList<HolonObject>();
  94. private ArrayList<Node> nodesOnCanvas= new ArrayList<Node>();
  95. private ArrayList<HolonSwitch> switchsOnCanvas= new ArrayList<HolonSwitch>();
  96. private HashMap<Integer, GroupNode> hashcodeMap = new HashMap<>();
  97. private Gson gson;
  98. /**
  99. * Constructor for the model. It initializes the categories and
  100. * objectsOnCanvas by default values. Listeners are also initialized by
  101. * default values.
  102. */
  103. public Model() {
  104. setCategories(new ArrayList<>());
  105. setObjectsOnCanvas(new ArrayList<>());
  106. setEdgesOnCanvas(new ArrayList<>());
  107. setCgIdx(new HashMap<>());
  108. setCvsObjIdx(new HashMap<>());
  109. initGson();
  110. }
  111. /**
  112. * Returns all Categories.
  113. *
  114. * @return the categories
  115. */
  116. public ArrayList<Category> getCategories() {
  117. return categories;
  118. }
  119. /**
  120. * Sets all Categories.
  121. *
  122. * @param categories the categories to set
  123. */
  124. public void setCategories(ArrayList<Category> categories) {
  125. this.categories = categories;
  126. }
  127. /**
  128. * Returns all Objects on the Canvas.
  129. *
  130. * @return the objectsOnCanvas
  131. */
  132. public ArrayList<AbstractCanvasObject> getObjectsOnCanvas() {
  133. return objectsOnCanvas;
  134. }
  135. /**
  136. * Sets all Objects on the Canvas.
  137. *
  138. * @param objectsOnCanvas the objectsOnCanvas to set
  139. */
  140. public void setObjectsOnCanvas(ArrayList<AbstractCanvasObject> objectsOnCanvas) {
  141. this.objectsOnCanvas = objectsOnCanvas;
  142. }
  143. /**
  144. * Get all Edges on the Canvas.
  145. *
  146. * @return the edgesOnCanvas
  147. */
  148. public ArrayList<Edge> getEdgesOnCanvas() {
  149. return edgesOnCanvas;
  150. }
  151. /**
  152. * Sets the edges on the Canvas.
  153. *
  154. * @param arrayList the edgesOnCanvas to set
  155. */
  156. public void setEdgesOnCanvas(ArrayList<Edge> arrayList) {
  157. this.edgesOnCanvas = arrayList;
  158. }
  159. /**
  160. * Adds an Edge to The Canvas.
  161. *
  162. * @param edge the edgesOnCanvas to add
  163. */
  164. public void addEdgeOnCanvas(Edge edge) {
  165. this.edgesOnCanvas.add(edge);
  166. }
  167. /**
  168. * Remove an edge from the Canvas.
  169. *
  170. * @param edge the edge to remove
  171. */
  172. public void removeEdgesOnCanvas(Edge edge) {
  173. this.edgesOnCanvas.remove(edge);
  174. }
  175. /**
  176. * Returns all selected Objects on the Canvas.
  177. *
  178. * @return The selected Objects
  179. */
  180. public Set<AbstractCanvasObject> getSelectedObjects() {
  181. return selectedObjects;
  182. }
  183. /**
  184. * Returns all selected Objects on the Canvas.
  185. *
  186. * @return The selected Objects
  187. */
  188. public void setSelectedCpsObjects(Set<AbstractCanvasObject> arr) {
  189. this.selectedObjects = arr;
  190. }
  191. /**
  192. * Returns the sCale (Scale for the Images).
  193. *
  194. * @return scale
  195. */
  196. public int getScale() {
  197. return scale;
  198. }
  199. /**
  200. * Returns scaledIV2 (The Scale divided by 2).
  201. *
  202. * @return scaledIV2
  203. */
  204. public static int getScaleDiv2() {
  205. return halfScale;
  206. }
  207. /**
  208. * Sets the Image Scale.
  209. *
  210. * @param value for the image
  211. */
  212. public static void setScale(int value) {
  213. scale = value;
  214. halfScale = (value + 1) / 2;
  215. }
  216. /**
  217. * Returns the maximum ITERATIONS.
  218. *
  219. * @return ITERATIONS
  220. */
  221. public int getMaxIterations() {
  222. return iterations;
  223. }
  224. /**
  225. * Returns the current iteration.
  226. *
  227. * @return current iteration
  228. */
  229. public int getActualTimeStep() {
  230. return curIteration;
  231. }
  232. /**
  233. * sets the current Iteration.
  234. *
  235. * @param curIT the current Iteration
  236. */
  237. public void setCurrentIteration(int value) {
  238. this.curIteration = value;
  239. }
  240. /**
  241. * Returns the selected Edge.
  242. *
  243. * @return selectedEdge
  244. */
  245. public Edge getSelectedEdge() {
  246. return selectedEdge;
  247. }
  248. /**
  249. * Set the selected Edge.
  250. *
  251. * @param edge that is selected
  252. */
  253. public void setSelectedEdge(Edge edge) {
  254. this.selectedEdge = edge;
  255. }
  256. /**
  257. * Returns the Categorie Index.
  258. *
  259. * @return the cgIdx
  260. */
  261. public HashMap<String, Integer> getCgIdx() {
  262. return cgIdx;
  263. }
  264. /**
  265. * Sets the Categorie Index.
  266. *
  267. * @param cgIdx the cgIdx to set
  268. */
  269. public void setCgIdx(HashMap<String, Integer> cgIdx) {
  270. this.cgIdx = cgIdx;
  271. }
  272. /**
  273. * Returns the CanvasObject Index.
  274. *
  275. * @return the cvsObjIdx
  276. */
  277. public HashMap<Integer, Integer> getCvsObjIdx() {
  278. return cvsObjIdx;
  279. }
  280. /**
  281. * Sets the CanvasObject Index.
  282. *
  283. * @param cvsObjIdx the cvsObjIdx to set
  284. */
  285. public void setCvsObjIdx(HashMap<Integer, Integer> cvsObjIdx) {
  286. this.cvsObjIdx = cvsObjIdx;
  287. }
  288. /**
  289. * Returns the auto save Number.
  290. *
  291. * @return the auto save Number
  292. */
  293. public int getAutoSaveNr() {
  294. return autoSaveNr;
  295. }
  296. /**
  297. * Sets the auto save Number.
  298. *
  299. * @param autoSaveNr the auto save number
  300. */
  301. public void setAutoSaveNr(int autoSaveNr) {
  302. this.autoSaveNr = autoSaveNr;
  303. }
  304. /**
  305. * Returns the Number of Saves.
  306. *
  307. * @return the numberOfSaves
  308. */
  309. public int getNumberOfSaves() {
  310. return numberOfSaves;
  311. }
  312. /**
  313. * Set the Number of Saves.
  314. *
  315. * @param numberOfSaves the numberOfSaves to set
  316. */
  317. public void setNumberOfSaves(int numberOfSaves) {
  318. this.numberOfSaves = numberOfSaves;
  319. }
  320. /**
  321. * Returns all Objects in the Clipboard.
  322. *
  323. * @return Objects in the Clipboard
  324. */
  325. public Set<AbstractCanvasObject> getClipboradObjects() {
  326. return clipboardObjects;
  327. }
  328. /**
  329. * Sets the ClipboardObjects.
  330. *
  331. * @param c Array of Objects
  332. */
  333. public void setClipboradObjects(Set<AbstractCanvasObject> c) {
  334. this.clipboardObjects = c;
  335. }
  336. /**
  337. * @return the maxCapacity
  338. */
  339. public float getMaxCapacity() {
  340. return maxCapacity;
  341. }
  342. /**
  343. * @param maxCapacity the maxCapacity to set
  344. */
  345. public void setMaxCapacity(float maxCapacity) {
  346. this.maxCapacity = maxCapacity;
  347. }
  348. /**
  349. * get the Interval in ms between each Iteration.
  350. *
  351. * @return timerSpeed speed for the Iterations
  352. */
  353. public int getTimerSpeed() {
  354. return this.timerSpeed;
  355. }
  356. /**
  357. * Sets the Interval in ms between each Iteration.
  358. *
  359. * @param t speed for the Iterations
  360. */
  361. public void setTimerSpeed(int t) {
  362. this.timerSpeed = t;
  363. }
  364. /**
  365. * Get Canvas X Size.
  366. *
  367. * @return the cANVAS_X
  368. */
  369. public int getCanvasX() {
  370. return canvasX;
  371. }
  372. /**
  373. * Set Canvas X Size.
  374. *
  375. * @param canvasX the cANVAS_X to set
  376. */
  377. public void setCanvasX(int canvasX) {
  378. this.canvasX = canvasX;
  379. }
  380. /**
  381. * get Canvas Y size.
  382. *
  383. * @return the cANVAS_Y
  384. */
  385. public int getCanvasY() {
  386. return canvasY;
  387. }
  388. /**
  389. * Set Canvas Y size.
  390. *
  391. * @param canvasY the cANVAS_Y to set
  392. */
  393. public void setCanvasY(int canvasY) {
  394. this.canvasY = canvasY;
  395. }
  396. public List<HolonElement> getAllHolonElemnts() {
  397. return getAllHolonObjectsOnCanvas().stream().flatMap(hO -> hO.getElements().stream()).collect(Collectors.toList());
  398. }
  399. public ArrayList<HolonObject> getAllHolonObjectsOnCanvas(){
  400. ArrayList<HolonObject> objectToReturn = new ArrayList<HolonObject>();
  401. getAllHolonObjectsRecursive(objectToReturn, getObjectsOnCanvas());
  402. return objectToReturn;
  403. }
  404. private void getAllHolonObjectsRecursive(ArrayList<HolonObject> addObjectsToThisList, List<AbstractCanvasObject> listOfObjectsToSearch){
  405. for(AbstractCanvasObject aCps : listOfObjectsToSearch) {
  406. if(aCps instanceof HolonObject) {
  407. addObjectsToThisList.add((HolonObject) aCps);
  408. }else if(aCps instanceof GroupNode){
  409. getAllHolonObjectsRecursive(addObjectsToThisList, ((GroupNode)aCps).getNodes());
  410. }
  411. }
  412. }
  413. public ArrayList<AbstractCanvasObject> getAllAbstractObjectsOnCanvas(){
  414. ArrayList<AbstractCanvasObject> objectToReturn = new ArrayList<AbstractCanvasObject>();
  415. getAllAsbtractObjectsRecursive(objectToReturn, getObjectsOnCanvas());
  416. return objectToReturn;
  417. }
  418. private void getAllAsbtractObjectsRecursive(ArrayList<AbstractCanvasObject> addObjectsToThisList, List<AbstractCanvasObject> listOfObjectsToSearch){
  419. for(AbstractCanvasObject aCps : listOfObjectsToSearch) {
  420. if(aCps instanceof GroupNode){
  421. getAllAsbtractObjectsRecursive(addObjectsToThisList, ((GroupNode)aCps).getNodes());
  422. }
  423. else{
  424. addObjectsToThisList.add(aCps);
  425. }
  426. }
  427. }
  428. /**
  429. * get all Switches
  430. */
  431. public ArrayList<HolonSwitch> getAllSwitches() {
  432. ArrayList<HolonSwitch> switches = new ArrayList<>();
  433. for (AbstractCanvasObject obj : getObjectsOnCanvas()) {
  434. if (obj instanceof HolonSwitch) {
  435. switches.add((HolonSwitch) obj);
  436. } else if (obj instanceof GroupNode) {
  437. getSwitchesRec(((GroupNode) obj).getNodes(), switches);
  438. }
  439. }
  440. return switches;
  441. }
  442. /**
  443. * get the Amount of Switches help function
  444. *
  445. * @param objects objects
  446. * @param switches List of switches
  447. */
  448. private ArrayList<HolonSwitch> getSwitchesRec(ArrayList<AbstractCanvasObject> objects,
  449. ArrayList<HolonSwitch> switches) {
  450. for (AbstractCanvasObject obj : objects) {
  451. if (obj instanceof HolonSwitch) {
  452. switches.add((HolonSwitch) obj);
  453. } else if (obj instanceof GroupNode) {
  454. getSwitchesRec(((GroupNode) obj).getNodes(), switches);
  455. }
  456. }
  457. return switches;
  458. }
  459. /**
  460. * Returns the Path for the background Image of the Canvas.
  461. *
  462. * @return imgPath the Path
  463. */
  464. public String getCanvasImagePath() {
  465. return imgPath;
  466. }
  467. /**
  468. * Set the Path for the background Image of the Canvas.
  469. *
  470. * @param path the Path
  471. */
  472. public void setCanvasImagePath(String path) {
  473. imgPath = path;
  474. }
  475. /**
  476. * Returns the mode for the background Image of the Canvas.
  477. * <p>
  478. * 0 take size of the Image 1 stretch the Image 2 Custom Image size
  479. *
  480. * @return backgroundMode the mode
  481. */
  482. public int getCanvasImageMode() {
  483. return backgroundMode;
  484. }
  485. /**
  486. * Set the mode for the background Image of the Canvas.
  487. * <p>
  488. * 0 take size of the Image, 1 stretch the Image, 2 Custom Image size
  489. *
  490. * @param mode the backgroundMode
  491. */
  492. public void setCanvasImageMode(int mode) {
  493. backgroundMode = mode;
  494. }
  495. /**
  496. * Returns the Custom width of the background Image of the Canvas.
  497. *
  498. * @return backgroundWidth the Width
  499. */
  500. public int getCanvasImageWidth() {
  501. return backgroundWidth;
  502. }
  503. /**
  504. * Set the Custom width of the background Image of the Canvas.
  505. *
  506. * @param width the Width
  507. */
  508. public void setCanvasImageWidth(int width) {
  509. backgroundWidth = width;
  510. }
  511. /**
  512. * Returns the Custom height of the background Image of the Canvas.
  513. *
  514. * @return backgroundHeight the height
  515. */
  516. public int getCanvasImageHeight() {
  517. return backgroundHeight;
  518. }
  519. /**
  520. * Set the Custom height of the background Image of the Canvas.
  521. *
  522. * @param height the height
  523. */
  524. public void setCanvasImageHeight(int height) {
  525. backgroundHeight = height;
  526. }
  527. /**
  528. * @return true if SupplyBars should be shown
  529. */
  530. public boolean getShowSupplyBars() {
  531. return showSupplyBars;
  532. }
  533. /**
  534. * @param showSupplyBars true if the SupplyBars should be shown
  535. */
  536. public void setShowSupplyBars(boolean showSupplyBars) {
  537. this.showSupplyBars = showSupplyBars;
  538. }
  539. /**
  540. * @param iterations the number of steps for this simulation
  541. */
  542. public void setIterations(int iterations){
  543. this.iterations=iterations;
  544. }
  545. /**
  546. * @return the fairnessModel
  547. */
  548. public FairnessModel getFairnessModel() {
  549. return fairnessModel;
  550. }
  551. /**
  552. * @param fairnessModel the fairnessModel to set
  553. */
  554. public void setFairnessModel(FairnessModel fairnessModel) {
  555. this.fairnessModel = fairnessModel;
  556. }
  557. public int getGraphIterations(){
  558. return GRAPH_ITERATIONS;
  559. }
  560. /**
  561. * Initialize the Gson with wanted parameters
  562. */
  563. private void initGson() {
  564. GsonBuilder builder = new GsonBuilder();
  565. builder.serializeNulls();
  566. builder.excludeFieldsWithoutExposeAnnotation();
  567. builder.setPrettyPrinting();
  568. builder.registerTypeAdapter(AbstractCanvasObject.class, new AbstractCpsObjectAdapter());
  569. builder.registerTypeAdapter(Vector2Int.class, new PositionAdapter());
  570. builder.registerTypeAdapter(SimpleEntry.class, new PairAdapter());
  571. // use the builder and make a instance of the Gson
  572. this.setGson(builder.create());
  573. }
  574. /**
  575. * @return the gson
  576. */
  577. public Gson getGson() {
  578. return gson;
  579. }
  580. /**
  581. * @param gson the gson to set
  582. */
  583. public void setGson(Gson gson) {
  584. this.gson = gson;
  585. }
  586. /**
  587. * @return the hashcodeMap
  588. */
  589. public HashMap<Integer, GroupNode> getHashcodeMap() {
  590. return hashcodeMap;
  591. }
  592. /**
  593. * @param hashcodeMap the hashcodeMap to set
  594. */
  595. public void setHashcodeMap(HashMap<Integer, GroupNode> hashcodeMap) {
  596. this.hashcodeMap = hashcodeMap;
  597. }
  598. public ArrayList<HolonSwitch> getSwitchsOnCanvas() {
  599. return switchsOnCanvas;
  600. }
  601. public void setSwitchsOnCanvas(ArrayList<HolonSwitch> switchsOnCanvas) {
  602. this.switchsOnCanvas = switchsOnCanvas;
  603. }
  604. public ArrayList<Node> getNodesOnCanvas() {
  605. return nodesOnCanvas;
  606. }
  607. public void setNodesOnCanvas(ArrayList<Node> nodesOnCanvas) {
  608. this.nodesOnCanvas = nodesOnCanvas;
  609. }
  610. public ArrayList<HolonObject> getHolonObjectsOnCanvas() {
  611. return holonObjectsOnCanvas;
  612. }
  613. public void setHolonObjectsOnCanvas(ArrayList<HolonObject> holonObjectsOnCanvas) {
  614. this.holonObjectsOnCanvas = holonObjectsOnCanvas;
  615. }
  616. }