Model.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  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 java.util.stream.Stream;
  11. import com.google.gson.Gson;
  12. import com.google.gson.GsonBuilder;
  13. import holeg.adapter.AbstractCpsObjectAdapter;
  14. import holeg.adapter.PairAdapter;
  15. import holeg.adapter.PositionAdapter;
  16. import holeg.model.AbstractCanvasObject;
  17. import holeg.model.Edge;
  18. import holeg.model.Flexibility;
  19. import holeg.model.GroupNode;
  20. import holeg.model.HolonElement;
  21. import holeg.model.HolonObject;
  22. import holeg.model.HolonSwitch;
  23. import holeg.model.Node;
  24. import holeg.ui.view.main.Category;
  25. import holeg.utility.Vector2Int;
  26. /**
  27. * The Class Model is the class where everything is saved. All changes made to
  28. * the Data is managed via a controller.
  29. *
  30. * @author Gruppe14
  31. */
  32. public class Model {
  33. private static final Logger log = Logger.getLogger(Model.class.getName());
  34. private static final int GRAPH_ITERATIONS = 100;
  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. //TODO(Tom2021-12-20) remove replace with groupnode
  95. private ArrayList<HolonObject> holonObjectsOnCanvas = new ArrayList<HolonObject>();
  96. private ArrayList<Node> nodesOnCanvas= new ArrayList<Node>();
  97. private ArrayList<HolonSwitch> switchsOnCanvas= new ArrayList<HolonSwitch>();
  98. private HashMap<Integer, GroupNode> hashcodeMap = new HashMap<>();
  99. //TODO(Tom2021-12-20) should be moved in Save/Load Controller
  100. private Gson gson;
  101. /**
  102. * Constructor for the model. It initializes the categories and
  103. * objectsOnCanvas by default values. Listeners are also initialized by
  104. * default values.
  105. */
  106. public Model() {
  107. log.fine("Init Model");
  108. setCategories(new ArrayList<>());
  109. setObjectsOnCanvas(new ArrayList<>());
  110. setEdgesOnCanvas(new ArrayList<>());
  111. setCgIdx(new HashMap<>());
  112. setCvsObjIdx(new HashMap<>());
  113. initGson();
  114. }
  115. /**
  116. * Returns all Categories.
  117. *
  118. * @return the categories
  119. */
  120. public ArrayList<Category> getCategories() {
  121. return categories;
  122. }
  123. /**
  124. * Sets all Categories.
  125. *
  126. * @param categories the categories to set
  127. */
  128. public void setCategories(ArrayList<Category> categories) {
  129. this.categories = categories;
  130. }
  131. /**
  132. * Returns all Objects on the Canvas.
  133. *
  134. * @return the objectsOnCanvas
  135. */
  136. public ArrayList<AbstractCanvasObject> getObjectsOnCanvas() {
  137. return objectsOnCanvas;
  138. }
  139. /**
  140. * Sets all Objects on the Canvas.
  141. *
  142. * @param objectsOnCanvas the objectsOnCanvas to set
  143. */
  144. public void setObjectsOnCanvas(ArrayList<AbstractCanvasObject> objectsOnCanvas) {
  145. this.objectsOnCanvas = objectsOnCanvas;
  146. }
  147. /**
  148. * Get all Edges on the Canvas.
  149. *
  150. * @return the edgesOnCanvas
  151. */
  152. public ArrayList<Edge> getEdgesOnCanvas() {
  153. return edgesOnCanvas;
  154. }
  155. /**
  156. * Sets the edges on the Canvas.
  157. *
  158. * @param arrayList the edgesOnCanvas to set
  159. */
  160. public void setEdgesOnCanvas(ArrayList<Edge> arrayList) {
  161. this.edgesOnCanvas = arrayList;
  162. }
  163. /**
  164. * Adds an Edge to The Canvas.
  165. *
  166. * @param edge the edgesOnCanvas to add
  167. */
  168. public void addEdgeOnCanvas(Edge edge) {
  169. this.edgesOnCanvas.add(edge);
  170. }
  171. /**
  172. * Remove an edge from the Canvas.
  173. *
  174. * @param edge the edge to remove
  175. */
  176. public void removeEdgesOnCanvas(Edge edge) {
  177. this.edgesOnCanvas.remove(edge);
  178. }
  179. /**
  180. * Returns all selected Objects on the Canvas.
  181. *
  182. * @return The selected Objects
  183. */
  184. public Set<AbstractCanvasObject> getSelectedObjects() {
  185. return selectedObjects;
  186. }
  187. /**
  188. * Returns all selected Objects on the Canvas.
  189. *
  190. * @return The selected Objects
  191. */
  192. public void setSelectedCpsObjects(Set<AbstractCanvasObject> arr) {
  193. this.selectedObjects = arr;
  194. }
  195. /**
  196. * Returns the maximum ITERATIONS.
  197. *
  198. * @return ITERATIONS
  199. */
  200. public int getMaxIterations() {
  201. return iterations;
  202. }
  203. /**
  204. * Returns the current iteration.
  205. *
  206. * @return current iteration
  207. */
  208. public int getActualTimeStep() {
  209. return curIteration;
  210. }
  211. /**
  212. * sets the current Iteration.
  213. *
  214. * @param curIT the current Iteration
  215. */
  216. public void setCurrentIteration(int value) {
  217. this.curIteration = value;
  218. }
  219. /**
  220. * Returns the selected Edge.
  221. *
  222. * @return selectedEdge
  223. */
  224. public Edge getSelectedEdge() {
  225. return selectedEdge;
  226. }
  227. /**
  228. * Set the selected Edge.
  229. *
  230. * @param edge that is selected
  231. */
  232. public void setSelectedEdge(Edge edge) {
  233. this.selectedEdge = edge;
  234. }
  235. /**
  236. * Returns the Categorie Index.
  237. *
  238. * @return the cgIdx
  239. */
  240. public HashMap<String, Integer> getCgIdx() {
  241. return cgIdx;
  242. }
  243. /**
  244. * Sets the Categorie Index.
  245. *
  246. * @param cgIdx the cgIdx to set
  247. */
  248. public void setCgIdx(HashMap<String, Integer> cgIdx) {
  249. this.cgIdx = cgIdx;
  250. }
  251. /**
  252. * Returns the CanvasObject Index.
  253. *
  254. * @return the cvsObjIdx
  255. */
  256. public HashMap<Integer, Integer> getCvsObjIdx() {
  257. return cvsObjIdx;
  258. }
  259. /**
  260. * Sets the CanvasObject Index.
  261. *
  262. * @param cvsObjIdx the cvsObjIdx to set
  263. */
  264. public void setCvsObjIdx(HashMap<Integer, Integer> cvsObjIdx) {
  265. this.cvsObjIdx = cvsObjIdx;
  266. }
  267. /**
  268. * Returns the auto save Number.
  269. *
  270. * @return the auto save Number
  271. */
  272. public int getAutoSaveNr() {
  273. return autoSaveNr;
  274. }
  275. /**
  276. * Sets the auto save Number.
  277. *
  278. * @param autoSaveNr the auto save number
  279. */
  280. public void setAutoSaveNr(int autoSaveNr) {
  281. this.autoSaveNr = autoSaveNr;
  282. }
  283. /**
  284. * Returns the Number of Saves.
  285. *
  286. * @return the numberOfSaves
  287. */
  288. public int getNumberOfSaves() {
  289. return numberOfSaves;
  290. }
  291. /**
  292. * Set the Number of Saves.
  293. *
  294. * @param numberOfSaves the numberOfSaves to set
  295. */
  296. public void setNumberOfSaves(int numberOfSaves) {
  297. this.numberOfSaves = numberOfSaves;
  298. }
  299. /**
  300. * Returns all Objects in the Clipboard.
  301. *
  302. * @return Objects in the Clipboard
  303. */
  304. public Set<AbstractCanvasObject> getClipboradObjects() {
  305. return clipboardObjects;
  306. }
  307. /**
  308. * Sets the ClipboardObjects.
  309. *
  310. * @param c Array of Objects
  311. */
  312. public void setClipboradObjects(Set<AbstractCanvasObject> c) {
  313. this.clipboardObjects = c;
  314. }
  315. /**
  316. * @return the maxCapacity
  317. */
  318. public float getMaxCapacity() {
  319. return maxCapacity;
  320. }
  321. /**
  322. * @param maxCapacity the maxCapacity to set
  323. */
  324. public void setMaxCapacity(float maxCapacity) {
  325. this.maxCapacity = maxCapacity;
  326. }
  327. /**
  328. * get the Interval in ms between each Iteration.
  329. *
  330. * @return timerSpeed speed for the Iterations
  331. */
  332. public int getTimerSpeed() {
  333. return this.timerSpeed;
  334. }
  335. /**
  336. * Sets the Interval in ms between each Iteration.
  337. *
  338. * @param t speed for the Iterations
  339. */
  340. public void setTimerSpeed(int t) {
  341. this.timerSpeed = t;
  342. }
  343. /**
  344. * Get Canvas X Size.
  345. *
  346. * @return the cANVAS_X
  347. */
  348. public int getCanvasX() {
  349. return canvasX;
  350. }
  351. /**
  352. * Set Canvas X Size.
  353. *
  354. * @param canvasX the cANVAS_X to set
  355. */
  356. public void setCanvasX(int canvasX) {
  357. this.canvasX = canvasX;
  358. }
  359. /**
  360. * get Canvas Y size.
  361. *
  362. * @return the cANVAS_Y
  363. */
  364. public int getCanvasY() {
  365. return canvasY;
  366. }
  367. /**
  368. * Set Canvas Y size.
  369. *
  370. * @param canvasY the cANVAS_Y to set
  371. */
  372. public void setCanvasY(int canvasY) {
  373. this.canvasY = canvasY;
  374. }
  375. public List<HolonElement> getAllHolonElements() {
  376. return getAllHolonObjectsOnCanvas().stream().flatMap(hO -> hO.getElements().stream()).collect(Collectors.toList());
  377. }
  378. public List<Flexibility> getAllFlexibilities() {
  379. return getAllHolonObjectsOnCanvas().stream().flatMap(hO -> hO.getElements().stream().flatMap(ele -> ele.flexList.stream())).collect(Collectors.toList());
  380. }
  381. public void reset() {
  382. resetFlexibilities();
  383. resetEdges();
  384. }
  385. private void resetFlexibilities() {
  386. getAllFlexibilities().forEach(flex -> flex.reset());
  387. }
  388. private void resetEdges() {
  389. this.getEdgesOnCanvas().forEach(edge -> edge.reset());
  390. }
  391. public ArrayList<HolonObject> getAllHolonObjectsOnCanvas(){
  392. ArrayList<HolonObject> objectToReturn = new ArrayList<HolonObject>();
  393. objectToReturn.addAll(this.holonObjectsOnCanvas);
  394. //TODO(Tom2021-12-18) create GroupNodeOnCanvas
  395. for(AbstractCanvasObject aCps : objectsOnCanvas) {
  396. if(aCps instanceof GroupNode groupnode){
  397. objectToReturn.addAll(groupnode.getAllHolonObjectsRecursive().toList());
  398. }
  399. }
  400. return objectToReturn;
  401. }
  402. public ArrayList<AbstractCanvasObject> getAllAbstractObjectsOnCanvas(){
  403. ArrayList<AbstractCanvasObject> objectToReturn = new ArrayList<AbstractCanvasObject>();
  404. getAllAsbtractObjectsRecursive(objectToReturn, getObjectsOnCanvas().stream());
  405. return objectToReturn;
  406. }
  407. private void getAllAsbtractObjectsRecursive(ArrayList<AbstractCanvasObject> addObjectsToThisList, Stream<AbstractCanvasObject> listOfObjectsToSearch){
  408. listOfObjectsToSearch.forEach(aCps -> {
  409. if(aCps instanceof GroupNode groupnode){
  410. getAllAsbtractObjectsRecursive(addObjectsToThisList, groupnode.getObjectsInThisLayer());
  411. }
  412. else{
  413. addObjectsToThisList.add(aCps);
  414. }
  415. });
  416. }
  417. /**
  418. * get all Switches
  419. */
  420. public ArrayList<HolonSwitch> getAllSwitches() {
  421. ArrayList<HolonSwitch> switches = new ArrayList<>();
  422. for (AbstractCanvasObject obj : getObjectsOnCanvas()) {
  423. if (obj instanceof HolonSwitch sw) {
  424. switches.add(sw);
  425. } else if (obj instanceof GroupNode groupnode) {
  426. switches.addAll(groupnode.getAllSwitchObjectsRecursive().toList());
  427. }
  428. }
  429. return switches;
  430. }
  431. /**
  432. * Returns the Path for the background Image of the Canvas.
  433. *
  434. * @return imgPath the Path
  435. */
  436. public String getCanvasImagePath() {
  437. return imgPath;
  438. }
  439. /**
  440. * Set the Path for the background Image of the Canvas.
  441. *
  442. * @param path the Path
  443. */
  444. public void setCanvasImagePath(String path) {
  445. imgPath = path;
  446. }
  447. /**
  448. * Returns the mode for the background Image of the Canvas.
  449. * <p>
  450. * 0 take size of the Image 1 stretch the Image 2 Custom Image size
  451. *
  452. * @return backgroundMode the mode
  453. */
  454. public int getCanvasImageMode() {
  455. return backgroundMode;
  456. }
  457. /**
  458. * Set the mode for the background Image of the Canvas.
  459. * <p>
  460. * 0 take size of the Image, 1 stretch the Image, 2 Custom Image size
  461. *
  462. * @param mode the backgroundMode
  463. */
  464. public void setCanvasImageMode(int mode) {
  465. backgroundMode = mode;
  466. }
  467. /**
  468. * Returns the Custom width of the background Image of the Canvas.
  469. *
  470. * @return backgroundWidth the Width
  471. */
  472. public int getCanvasImageWidth() {
  473. return backgroundWidth;
  474. }
  475. /**
  476. * Set the Custom width of the background Image of the Canvas.
  477. *
  478. * @param width the Width
  479. */
  480. public void setCanvasImageWidth(int width) {
  481. backgroundWidth = width;
  482. }
  483. /**
  484. * Returns the Custom height of the background Image of the Canvas.
  485. *
  486. * @return backgroundHeight the height
  487. */
  488. public int getCanvasImageHeight() {
  489. return backgroundHeight;
  490. }
  491. /**
  492. * Set the Custom height of the background Image of the Canvas.
  493. *
  494. * @param height the height
  495. */
  496. public void setCanvasImageHeight(int height) {
  497. backgroundHeight = height;
  498. }
  499. /**
  500. * @return true if SupplyBars should be shown
  501. */
  502. public boolean getShowSupplyBars() {
  503. return showSupplyBars;
  504. }
  505. /**
  506. * @param showSupplyBars true if the SupplyBars should be shown
  507. */
  508. public void setShowSupplyBars(boolean showSupplyBars) {
  509. this.showSupplyBars = showSupplyBars;
  510. }
  511. /**
  512. * @param iterations the number of steps for this simulation
  513. */
  514. public void setIterations(int iterations){
  515. this.iterations=iterations;
  516. }
  517. /**
  518. * @return the fairnessModel
  519. */
  520. public FairnessModel getFairnessModel() {
  521. return fairnessModel;
  522. }
  523. /**
  524. * @param fairnessModel the fairnessModel to set
  525. */
  526. public void setFairnessModel(FairnessModel fairnessModel) {
  527. this.fairnessModel = fairnessModel;
  528. }
  529. public int getGraphIterations(){
  530. return GRAPH_ITERATIONS;
  531. }
  532. /**
  533. * Initialize the Gson with wanted parameters
  534. */
  535. private void initGson() {
  536. GsonBuilder builder = new GsonBuilder();
  537. builder.serializeNulls();
  538. builder.excludeFieldsWithoutExposeAnnotation();
  539. builder.setPrettyPrinting();
  540. builder.registerTypeAdapter(AbstractCanvasObject.class, new AbstractCpsObjectAdapter());
  541. builder.registerTypeAdapter(Vector2Int.class, new PositionAdapter());
  542. builder.registerTypeAdapter(SimpleEntry.class, new PairAdapter());
  543. // use the builder and make a instance of the Gson
  544. this.setGson(builder.create());
  545. }
  546. /**
  547. * @return the gson
  548. */
  549. public Gson getGson() {
  550. return gson;
  551. }
  552. /**
  553. * @param gson the gson to set
  554. */
  555. public void setGson(Gson gson) {
  556. this.gson = gson;
  557. }
  558. /**
  559. * @return the hashcodeMap
  560. */
  561. public HashMap<Integer, GroupNode> getHashcodeMap() {
  562. return hashcodeMap;
  563. }
  564. /**
  565. * @param hashcodeMap the hashcodeMap to set
  566. */
  567. public void setHashcodeMap(HashMap<Integer, GroupNode> hashcodeMap) {
  568. this.hashcodeMap = hashcodeMap;
  569. }
  570. public ArrayList<HolonSwitch> getSwitchsOnCanvas() {
  571. return switchsOnCanvas;
  572. }
  573. public void setSwitchsOnCanvas(ArrayList<HolonSwitch> switchsOnCanvas) {
  574. this.switchsOnCanvas = switchsOnCanvas;
  575. }
  576. public ArrayList<Node> getNodesOnCanvas() {
  577. return nodesOnCanvas;
  578. }
  579. public void setNodesOnCanvas(ArrayList<Node> nodesOnCanvas) {
  580. this.nodesOnCanvas = nodesOnCanvas;
  581. }
  582. public ArrayList<HolonObject> getHolonObjectsOnCanvas() {
  583. return holonObjectsOnCanvas;
  584. }
  585. public void setHolonObjectsOnCanvas(ArrayList<HolonObject> holonObjectsOnCanvas) {
  586. this.holonObjectsOnCanvas = holonObjectsOnCanvas;
  587. }
  588. }