Model.java 19 KB

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