Model.java 18 KB

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