Control.java 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051
  1. package ui.controller;
  2. import api.CpsAlgorithm;
  3. import classes.*;
  4. import com.google.gson.JsonParseException;
  5. import interfaces.CategoryListener;
  6. import org.apache.commons.compress.archivers.ArchiveException;
  7. import ui.model.Model;
  8. import ui.view.CreateTemplatePopUp;
  9. import ui.view.FlexiblePane;
  10. import ui.view.MyCanvas;
  11. import ui.view.StatisticGraphPanel;
  12. import java.awt.*;
  13. import java.awt.datatransfer.UnsupportedFlavorException;
  14. import java.io.File;
  15. import java.io.IOException;
  16. import java.util.ArrayList;
  17. import java.util.Hashtable;
  18. import java.util.stream.Collectors;
  19. import javax.swing.JFrame;
  20. /**
  21. * The Class represents the controller in the model, controller view Pattern.
  22. *
  23. * @author Gruppe14
  24. */
  25. public class Control {
  26. private final ConsoleController consoleController;
  27. private final MultiPurposeController multiPurposeController;
  28. private final CategoryController categoryController;
  29. private final ObjectController objectController;
  30. private final CanvasController canvasController;
  31. private final GlobalController globalController;
  32. private final SaveController saveController;
  33. private final LoadController loadController;
  34. private final AutoSaveController autoSaveController;
  35. private final StatsController statsController;
  36. private final NodeController nodeController;
  37. private final ClipboardController clipboardController;
  38. private final HolonCanvasController holonCanvasController;
  39. private Model model;
  40. private SimulationManager simulationManager;
  41. private String autosaveDir = "";
  42. private String categoryDir = "";
  43. private String otherDir = "";
  44. private String dimensionsFileName = "dimensions";
  45. private int rand;
  46. /**
  47. * Constructor.
  48. *
  49. * @param model the Model
  50. */
  51. public Control(Model model) {
  52. this.model = model;
  53. this.multiPurposeController = new MultiPurposeController(model);
  54. this.categoryController = new CategoryController(model, multiPurposeController);
  55. this.objectController = new ObjectController(model, multiPurposeController);
  56. this.canvasController = new CanvasController(model, multiPurposeController);
  57. this.globalController = new GlobalController(model);
  58. this.saveController = new SaveController(model);
  59. this.nodeController = new NodeController(model, canvasController, multiPurposeController);
  60. this.loadController = new LoadController(model, categoryController, canvasController, objectController,
  61. nodeController, multiPurposeController);
  62. this.simulationManager = new SimulationManager(model);
  63. this.autoSaveController = new AutoSaveController(model);
  64. this.consoleController = new ConsoleController(model);
  65. this.statsController = new StatsController(model);
  66. this.clipboardController = new ClipboardController(model, saveController, loadController, canvasController,
  67. objectController, nodeController, multiPurposeController);
  68. this.holonCanvasController = new HolonCanvasController(model);
  69. autosaveDir = System.getProperty("user.home") + "/.config/HolonGUI/Autosave/";
  70. categoryDir = System.getProperty("user.home") + "/.config/HolonGUI/Category/";
  71. otherDir = System.getProperty("user.home") + "/.config/HolonGUI/Other/";
  72. File autoSave = new File(autosaveDir);
  73. File category = new File(categoryDir);
  74. File other = new File(otherDir);
  75. // deleteDirectory(dest);
  76. autoSave.mkdirs();
  77. category.mkdirs();
  78. other.mkdirs();
  79. createAutoRandom();
  80. try {
  81. autoSave();
  82. } catch (IOException e) {
  83. e.printStackTrace();
  84. }
  85. }
  86. /**
  87. * Generate random number, so that every instance of the program has unique
  88. * save files
  89. */
  90. private void createAutoRandom() {
  91. rand = (int) (Math.random() * 1000);
  92. while (new File(autosaveDir + rand + (autoSaveController.getAutoSaveNr())).exists()) {
  93. rand = (int) Math.random() * 1000;
  94. }
  95. }
  96. /**
  97. * Delete a Directory.
  98. *
  99. * @param path to delete
  100. */
  101. public void deleteDirectory(File path) {
  102. if (path.exists()) {
  103. File[] files = path.listFiles();
  104. for (File file : files) {
  105. if (file.isDirectory()) {
  106. deleteDirectory(file);
  107. } else {
  108. if (file.getName().contains("" + rand))
  109. file.delete();
  110. }
  111. }
  112. // path.delete();
  113. }
  114. }
  115. /* Operations for searching */
  116. /**
  117. * Search for Object by ID.
  118. *
  119. * @param id the id of the Object
  120. * @return the CpsObject
  121. */
  122. public AbstractCpsObject searchByID(int id) {
  123. return multiPurposeController.searchByID(id);
  124. }
  125. /**
  126. * Search for Object by ID in upperNode
  127. *
  128. * @param id the id of the Object
  129. * @return the CpsObject
  130. */
  131. public AbstractCpsObject searchByIDUpperNode(int id, CpsUpperNode upperNode) {
  132. return multiPurposeController.searchByIDUpperNode(id, upperNode);
  133. }
  134. public AbstractCpsObject searchTracked(int id) {
  135. return multiPurposeController.searchByID(id);
  136. }
  137. /**
  138. * Search for Object in a Category.
  139. *
  140. * @param category name of the Category
  141. * @param object Name of the Object
  142. * @return The Object
  143. */
  144. public AbstractCpsObject searchCategoryObject(String category, String object) {
  145. return multiPurposeController.searchCatObj(multiPurposeController.searchCat(category), object);
  146. }
  147. /**
  148. * search for category.
  149. *
  150. * @param cat name of the Category
  151. * @return the Category
  152. */
  153. public Category searchCategory(String cat) {
  154. return multiPurposeController.searchCat(cat);
  155. }
  156. /* Operations for Categories and Objects */
  157. /**
  158. * init default category and objects.
  159. *
  160. * @throws IOException
  161. */
  162. public void resetCategorys() throws IOException {
  163. categoryController.initCategories();
  164. objectController.initHolonElements();
  165. saveCategory();
  166. }
  167. /**
  168. * Adds New Category into Model.
  169. *
  170. * @param cat name of the new Category
  171. * @throws IOException
  172. */
  173. public void addCategory(String cat) throws IOException {
  174. categoryController.addNewCategory(cat);
  175. saveCategory();
  176. }
  177. /**
  178. * Gives all Category as String
  179. * @return a array of strings from all Categorys
  180. */
  181. public String[] getCategoriesStrings()
  182. {
  183. return ((ArrayList<String>) categoryController.getCategories().stream().map(c -> c.getName()).collect(Collectors.toList())).toArray(new String[categoryController.getCategories().size()]);
  184. }
  185. /**
  186. * Add new Holon Object to a Category.
  187. *
  188. * @param cat Category
  189. * @param obj New Object Name
  190. * @param ele Array of Elements
  191. * @param img the image Path
  192. * @throws IOException
  193. */
  194. public void addObject(Category cat, String obj, ArrayList<HolonElement> ele, String img) throws IOException {
  195. categoryController.addNewHolonObject(cat, obj, ele, img);
  196. saveCategory();
  197. }
  198. /**
  199. * Add new Holon Transformer to a Category.
  200. *
  201. * @param cat Category
  202. * @param obj New Object Name
  203. */
  204. public void addTransformer(Category cat, String obj) {
  205. categoryController.addNewHolonTransformer(cat, obj, "/Images/transformer-1.png");
  206. }
  207. /**
  208. * Add new Holon Switch to a Category.
  209. *
  210. * @param cat Category
  211. * @param obj New Object Name
  212. * @throws IOException
  213. */
  214. public void addSwitch(Category cat, String obj) throws IOException {
  215. categoryController.addNewHolonSwitch(cat, obj, "/Images/switch-on.png");
  216. saveCategory();
  217. }
  218. /**
  219. * Add new HolonBattery to a Category
  220. * @param cat
  221. * @param obj
  222. * @throws IOException
  223. */
  224. public void addBattery(Category cat, String obj) throws IOException {
  225. categoryController.addNewHolonBattery(cat, obj, "/Images/battery.png");
  226. saveCategory();
  227. }
  228. //TODO make good
  229. public void addBattery(Category cat, HolonBattery holonbat) throws IOException {
  230. categoryController.addNewHolonBattery(cat, holonbat, "/Images/battery.png");
  231. saveCategory();
  232. }
  233. /**
  234. * delete a given Category.
  235. *
  236. * @param cat the Category
  237. * @throws IOException
  238. */
  239. public void deleteCategory(String cat) throws IOException {
  240. categoryController.deleteCategory(cat);
  241. saveCategory();
  242. }
  243. /**
  244. * Delete an Object from a Category.
  245. *
  246. * @param cat the Category
  247. * @param obj the Object
  248. * @throws IOException
  249. */
  250. public void delObjectCategory(String cat, String obj) throws IOException {
  251. categoryController.deleteObject(cat, obj);
  252. saveCategory();
  253. }
  254. /**
  255. * deletes a selectedObject.
  256. *
  257. * @param obj Cpsobject
  258. */
  259. public void deleteSelectedObject(AbstractCpsObject obj) {
  260. objectController.deleteSelectedObject(obj);
  261. }
  262. /**
  263. * add an Object to selectedObject.
  264. *
  265. * @param obj AbstractCpsobject
  266. */
  267. public void addSelectedObject(AbstractCpsObject obj) {
  268. objectController.addSelectedObject(obj);
  269. }
  270. /* Operations for Canvas */
  271. /**
  272. * Add a new Object.
  273. *
  274. * @param object the Object
  275. */
  276. public void addObjectCanvas(AbstractCpsObject object) {
  277. canvasController.addNewObject(object);
  278. simulationManager.calculateStateForTimeStep(model.getCurIteration());
  279. if (!(object instanceof CpsNode)) {
  280. try {
  281. autoSave();
  282. } catch (IOException e) {
  283. e.printStackTrace();
  284. }
  285. }
  286. }
  287. /**
  288. * Deletes an CpsObject on the Canvas and its connections.
  289. *
  290. * @param obj AbstractCpsObject
  291. * @param save
  292. */
  293. public void delCanvasObject(AbstractCpsObject obj, boolean save) {
  294. canvasController.deleteObjectOnCanvas(obj);
  295. calculateStateForCurrentTimeStep();
  296. if (obj instanceof CpsUpperNode)
  297. canvasController.bfsNodeCleaner((CpsUpperNode) obj);
  298. if (save)
  299. try {
  300. autoSave();
  301. } catch (IOException e) {
  302. e.printStackTrace();
  303. }
  304. }
  305. /**
  306. * Replaces {@code toBeReplaced} by {@code by} on the canvas
  307. * @param toBeReplaced the object that will be replaced
  308. * @param by the object that will replace it
  309. */
  310. public void replaceCanvasObject(AbstractCpsObject toBeReplaced, AbstractCpsObject by) {
  311. canvasController.replaceObjectOnCanvas(toBeReplaced, by);
  312. }
  313. /**
  314. * Add an edge to the Canvas.
  315. *
  316. * @param edge the edge
  317. */
  318. public void addEdgeOnCanvas(CpsEdge edge) {
  319. canvasController.addEdgeOnCanvas(edge);
  320. try {
  321. autoSave();
  322. } catch (IOException e) {
  323. e.printStackTrace();
  324. }
  325. }
  326. /**
  327. * Removes an Edge from the Canvas.
  328. *
  329. * @param edge the edge to remove
  330. */
  331. public void removeEdgesOnCanvas(CpsEdge edge) {
  332. canvasController.removeEdgesOnCanvas(edge);
  333. try {
  334. autoSave();
  335. } catch (IOException e) {
  336. e.printStackTrace();
  337. }
  338. }
  339. /**
  340. * Set the selected Edge.
  341. *
  342. * @param edge that is selected
  343. */
  344. public void setSelecteEdge(CpsEdge edge) {
  345. model.setSelectedEdge(edge);
  346. }
  347. /**
  348. * Returns the ID of the selected Object 0 = no Object is selected.
  349. *
  350. * @param id the ID of the selected Object
  351. */
  352. public void setSelectedObjectID(int id) {
  353. objectController.setSelectedObjectID(id);
  354. }
  355. /* Operations for Objects and Elements */
  356. /**
  357. * Add a new Element into a Object on the Canvas.
  358. *
  359. * @param objectId the Object ID
  360. * @param ele the Name of the Element
  361. * @param amount the Amount
  362. * @param energy the Energy
  363. * @param elementId the Element ID
  364. */
  365. public void addElementCanvasObject(int objectId, String ele, int amount, float energy, int elementId) {
  366. objectController.addNewElementIntoCanvasObject(objectId, ele, amount, energy, elementId);
  367. try {
  368. autoSave();
  369. } catch (IOException e) {
  370. e.printStackTrace();
  371. }
  372. }
  373. /**
  374. * Add a new Element into a Object in Category.
  375. *
  376. * @param catName the Category
  377. * @param objName the Object
  378. * @param eleName the Element Name
  379. * @param amount the amount
  380. * @param energy the Energy
  381. */
  382. public void addElementCategoryObject(String catName, String objName, String eleName, int amount, float energy) {
  383. objectController.addNewElementIntoCategoryObject(catName, objName, eleName, amount, energy);
  384. }
  385. /**
  386. * deletes a Element from a given Canvas Object.
  387. *
  388. * @param id the ID
  389. * @param elementid the Element ID
  390. */
  391. public void deleteElementCanvas(int id, int elementid) {
  392. objectController.deleteElementInCanvas(id, elementid);
  393. try {
  394. autoSave();
  395. } catch (IOException e) {
  396. e.printStackTrace();
  397. }
  398. }
  399. /* Global Operations */
  400. /**
  401. * Add a SubNetColor.
  402. *
  403. * @param c the Color
  404. */
  405. public void addSubNetColor(Color c) {
  406. globalController.addSubNetColor(c);
  407. }
  408. /**
  409. * Returns SCALE.
  410. *
  411. * @return SCALE
  412. */
  413. public int getScale() {
  414. return globalController.getScale();
  415. }
  416. /**
  417. * Changes the value of SCALE and SCALEDIV2.
  418. *
  419. * @param s Scale
  420. */
  421. public void setScale(int s) {
  422. globalController.setScale(s);
  423. }
  424. /**
  425. * Returns SCALE Divided by 2.
  426. *
  427. * @return SCALE Divided by 2
  428. */
  429. public int getScaleDiv2() {
  430. return globalController.getScaleDiv2();
  431. }
  432. /**
  433. * sets the current Iteration.
  434. *
  435. * @param curit the current Iteration
  436. */
  437. public void setCurIteration(int curit) {
  438. globalController.setCurIteration(curit);
  439. }
  440. /**
  441. * Writes the current State of the Modelling into a JSON File which can be
  442. * loaded.
  443. *
  444. * @param path the Path
  445. * @throws IOException exception
  446. */
  447. public void saveFile(String path) throws IOException, ArchiveException {
  448. saveController.writeSave(path);
  449. }
  450. /**
  451. * Reads the the Save File and load the state into the Model.
  452. *
  453. * @param path the Path
  454. * @throws IOException exception
  455. * @throws ArchiveException
  456. */
  457. public void loadFile(String path) throws IOException, ArchiveException {
  458. loadController.readSave(path);
  459. saveCategory();
  460. autoSave();
  461. }
  462. /**
  463. * Reads the Json File from Autosave
  464. *
  465. * @param path
  466. * @throws IOException
  467. */
  468. public void loadAutoSave(String path) throws IOException {
  469. loadController.readJson(path);
  470. }
  471. public ArrayList<Integer> loadSavedWindowDimensionsIfExistent() {
  472. try {
  473. return loadController.readWindowDimensions(otherDir + dimensionsFileName);
  474. } catch (Exception e) {
  475. return new ArrayList<>();
  476. }
  477. }
  478. /**
  479. * Init the CategoryListener.
  480. *
  481. * @param catLis the CategoryListener
  482. */
  483. public void initListener(CategoryListener catLis) {
  484. categoryController.addCategoryListener(catLis);
  485. }
  486. /**
  487. * calculates the flow of the edges and the supply for objects for the
  488. * current Timestep.
  489. */
  490. public void calculateStateForCurrentTimeStep() {
  491. // simulationManager.reset();
  492. simulationManager.calculateStateForTimeStep(model.getCurIteration());
  493. }
  494. /**
  495. * calculates the flow of the edges and the supply for objects.
  496. *
  497. * @param x current Iteration
  498. */
  499. public void calculateStateForTimeStep(int x) {
  500. // simulationManager.reset();
  501. simulationManager.calculateStateForTimeStep(x);
  502. }
  503. /**
  504. * resets the whole State of the simulation including a reset of all Edges
  505. * to the default "is working" state
  506. */
  507. public void resetSimulation() {
  508. setIsSimRunning(false);
  509. simulationManager.resetSimulation();
  510. }
  511. /**
  512. * Set the Canvas.
  513. *
  514. * @param can the Canvas
  515. */
  516. public void setCanvas(MyCanvas can) {
  517. simulationManager.setCanvas(can);
  518. }
  519. /**
  520. * make an autosave.
  521. *
  522. * @throws IOException Exception
  523. */
  524. public void autoSave() throws IOException {
  525. autoSaveController.increaseAutoSaveNr();
  526. saveController.writeAutosave(autosaveDir + rand + autoSaveController.getAutoSaveNr());
  527. if (autoSaveController.allowed()) {
  528. new File(autosaveDir + rand + (autoSaveController.getAutoSaveNr() - globalController.getNumbersOfSaves()))
  529. .delete();
  530. }
  531. }
  532. /**
  533. * find all old auto save files (with a file-name, that does not contain the current rand)
  534. *
  535. * @return a list of files, that are not from the current run
  536. */
  537. public ArrayList<File> filterOldAutoSaveFiles() {
  538. File[] files = new File(autosaveDir).listFiles();
  539. ArrayList<File> oldAutoSaves = new ArrayList<>();
  540. for (File file : files) {
  541. if (!file.getName().contains(String.valueOf(rand)))
  542. oldAutoSaves.add(file);
  543. }
  544. return oldAutoSaves;
  545. }
  546. /**
  547. * deletes the old autosave files
  548. */
  549. public void deleteObsoleteAutoSaveFiles() {
  550. for (File file : filterOldAutoSaveFiles()) {
  551. file.delete();
  552. }
  553. }
  554. public void saveCategory() throws IOException {
  555. saveController.writeCategory(categoryDir + "Category.json");
  556. }
  557. public void savePosAndSizeOfWindow(int x, int y, int width, int height) throws IOException, ArchiveException {
  558. saveController.writeWindowStatus(otherDir + dimensionsFileName, x, y, width, height);
  559. }
  560. /**
  561. * Returns the undo save.
  562. *
  563. * @return the undo save
  564. */
  565. public String getUndoSave() {
  566. autoSaveController.decreaseAutoSaveNr();
  567. if (!new File(autosaveDir + rand + (autoSaveController.getAutoSaveNr())).exists()) {
  568. autoSaveController.increaseAutoSaveNr();
  569. }
  570. return autosaveDir + rand + (autoSaveController.getAutoSaveNr());
  571. }
  572. /**
  573. * Returns the redo save.
  574. *
  575. * @return the redo save
  576. */
  577. public String getRedoSave() {
  578. autoSaveController.increaseAutoSaveNr();
  579. if (!new File(autosaveDir + rand + (autoSaveController.getAutoSaveNr())).exists()) {
  580. autoSaveController.decreaseAutoSaveNr();
  581. // if it still does not exist, try old autosaves
  582. if (!new File(autosaveDir + rand + (autoSaveController.getAutoSaveNr())).exists()) {
  583. ArrayList<File> oldAutoSaves = filterOldAutoSaveFiles();
  584. if (oldAutoSaves.size() > 0) {
  585. return autosaveDir + oldAutoSaves.get(oldAutoSaves.size() - 1).getName();
  586. }
  587. }
  588. }
  589. return autosaveDir + rand + (autoSaveController.getAutoSaveNr());
  590. }
  591. /**
  592. * Getter for Model.
  593. *
  594. * @return the Model
  595. */
  596. public Model getModel() {
  597. return model;
  598. }
  599. /**
  600. * get the Simulation Manager.
  601. *
  602. * @return the Simulation Manager
  603. */
  604. public SimulationManager getSimManager() {
  605. return simulationManager;
  606. }
  607. /**
  608. * Getter for selected CpsObject.
  609. *
  610. * @param text String the Text
  611. * @param color the color of the Text
  612. * @param p size of the Text
  613. * @param bold bold or not
  614. * @param italic italic or not
  615. * @param nl new line or not
  616. */
  617. public void addTextToConsole(String text, Color color, int p, boolean bold, boolean italic, boolean nl) {
  618. consoleController.addTextToConsole(text, color, p, bold, italic, nl);
  619. }
  620. /**
  621. * Print Text on the console in black and font size 12.
  622. *
  623. * @param text String the Text
  624. */
  625. public void addTextToConsole(String text) {
  626. consoleController.addTextToConsole(text);
  627. }
  628. /**
  629. * Clears the console.
  630. */
  631. public void clearConsole() {
  632. consoleController.clearConsole();
  633. }
  634. /**
  635. * Set the timerSpeed.
  636. *
  637. * @param t interval in ms
  638. */
  639. public void setTimerSpeed(int t) {
  640. globalController.setTimerSpeed(t);
  641. }
  642. /**
  643. * Set the Canvas X Size.
  644. *
  645. * @param canvasX the cANVAS_X to set
  646. */
  647. public void setCanvasX(int canvasX) {
  648. globalController.setCanvasX(canvasX);
  649. try {
  650. autoSave();
  651. } catch (IOException e) {
  652. e.printStackTrace();
  653. }
  654. }
  655. /**
  656. * Set the Canvas Y Size.
  657. *
  658. * @param canvasY the cANVAS_Y to set
  659. */
  660. public void setCanvasY(int canvasY) {
  661. globalController.setCanvasY(canvasY);
  662. try {
  663. autoSave();
  664. } catch (IOException e) {
  665. e.printStackTrace();
  666. }
  667. }
  668. public void setMaxCapacity(float cap) {
  669. globalController.setMaxCapacity(cap);
  670. }
  671. /**
  672. * Set the Algorithm.
  673. *
  674. * @param obj the Algorithm
  675. */
  676. public void setAlgorithm(Object obj) {
  677. multiPurposeController.setAlgorithm(obj);
  678. }
  679. /**
  680. * Run the Algorithm.
  681. */
  682. public void runAlgorithm(Model model, Control controller) {
  683. if (model.getAlgorithm() != null) {
  684. ((CpsAlgorithm) model.getAlgorithm()).runAlgorithm(model, controller);
  685. }
  686. }
  687. // ========================= MANAGING TRACKED OBJECTS ====================
  688. public ArrayList<AbstractCpsObject> getTrackingObj() {
  689. return statsController.getTrackingObj();
  690. }
  691. public void setTrackingObj(ArrayList<AbstractCpsObject> objArr) {
  692. statsController.setTrackingObj(objArr);
  693. }
  694. public void addTrackingObj(AbstractCpsObject obj) {
  695. statsController.addTrackingObj(obj);
  696. try {
  697. autoSave();
  698. } catch (IOException e) {
  699. e.printStackTrace();
  700. }
  701. }
  702. public void removeTrackingObj(AbstractCpsObject obj) {
  703. statsController.removeTrackingObj(obj);
  704. try {
  705. autoSave();
  706. } catch (IOException e) {
  707. e.printStackTrace();
  708. }
  709. }
  710. // ========================== MANAGING TRACKED OBJECTS END ================
  711. /**
  712. * Controlling Nodes of Nodes
  713. */
  714. public void addUpperNode(String nodeName, CpsUpperNode upperNode, ArrayList<AbstractCpsObject> toGroup) {
  715. nodeController.doUpperNode(nodeName, upperNode, toGroup);
  716. try {
  717. autoSave();
  718. } catch (IOException e) {
  719. e.printStackTrace();
  720. }
  721. }
  722. public void delUpperNode(CpsUpperNode node, CpsUpperNode upperNode) {
  723. nodeController.undoUpperNode(node, upperNode);
  724. try {
  725. autoSave();
  726. } catch (IOException e) {
  727. e.printStackTrace();
  728. }
  729. }
  730. public void addObjUpperNode(AbstractCpsObject object, CpsUpperNode upperNode) {
  731. nodeController.addObjectInUpperNode(object, upperNode, true);
  732. try {
  733. autoSave();
  734. } catch (IOException e) {
  735. e.printStackTrace();
  736. }
  737. }
  738. public void delObjUpperNode(AbstractCpsObject object, CpsUpperNode upperNode) {
  739. nodeController.deleteObjectInUpperNode(object, upperNode);
  740. if (object instanceof CpsUpperNode)
  741. canvasController.bfsNodeCleaner((CpsUpperNode) object);
  742. try {
  743. autoSave();
  744. } catch (IOException e) {
  745. e.printStackTrace();
  746. }
  747. }
  748. /**
  749. * Replaces {@code toBePlaced} by {@code by} in {@code upperNode}
  750. * @param toBeReplaced
  751. * @param by
  752. * @param upperNode
  753. */
  754. public void replaceObjUpperNode(AbstractCpsObject toBeReplaced,
  755. AbstractCpsObject by, CpsUpperNode upperNode) {
  756. nodeController.replaceObjectInUpperNode(toBeReplaced, by, upperNode);
  757. }
  758. public void addEdgeUpperNode(CpsEdge edge, CpsUpperNode upperNode) {
  759. nodeController.addEdge(edge, upperNode);
  760. try {
  761. autoSave();
  762. } catch (IOException e) {
  763. e.printStackTrace();
  764. }
  765. }
  766. public void delEdgeUpperNode(CpsEdge edge, CpsUpperNode upperNode) {
  767. nodeController.deleteEdge(edge, upperNode);
  768. try {
  769. autoSave();
  770. } catch (IOException e) {
  771. e.printStackTrace();
  772. }
  773. }
  774. public void connectNodes(CpsEdge edge, CpsUpperNode upperNode) {
  775. nodeController.connectNodes(edge, upperNode);
  776. try {
  777. autoSave();
  778. } catch (IOException e) {
  779. e.printStackTrace();
  780. }
  781. }
  782. public void disconnectNodes(CpsEdge edge, CpsUpperNode upperNode) {
  783. nodeController.disconnectNodes(edge, upperNode);
  784. try {
  785. autoSave();
  786. } catch (IOException e) {
  787. e.printStackTrace();
  788. }
  789. }
  790. /**
  791. * Get the number of HolonObjects in the given List
  792. *
  793. * @param list
  794. */
  795. public int getNumberHolonObjects(ArrayList<AbstractCpsObject> list) {
  796. return objectController.getNumberHolonObjects(list);
  797. }
  798. /**
  799. * Get the number of HolonObjects with the given state in the given List
  800. *
  801. * @param list
  802. * @param state
  803. */
  804. public int getNumberStateObjects(ArrayList<AbstractCpsObject> list, int state) {
  805. return objectController.getNumberStateObjects(list, state);
  806. }
  807. /**
  808. * Returns HolonBodySCALE.
  809. *
  810. * @return SCALE
  811. */
  812. public int getHolonBodyScale() {
  813. return globalController.getHolonBodyScale();
  814. }
  815. /**
  816. * Changes the value of HolonBodySCALE
  817. *
  818. * @param s HolonBodyScale
  819. */
  820. public void setHolonBodyScale(int s) {
  821. globalController.setHolonBodyScale(s);
  822. }
  823. /**
  824. * Sets the ID of the selected HolonBody
  825. *
  826. * @param i ID of the selected HolonBody
  827. */
  828. public void addSelectedHolonBody(int i) {
  829. objectController.addSelectedHolonBody(i);
  830. }
  831. /**
  832. * Copy all Selected Objects.
  833. */
  834. public void copy(CpsUpperNode upperNode) {
  835. clipboardController.copy(upperNode);
  836. }
  837. public void paste(CpsUpperNode upperNode, Point point)
  838. throws JsonParseException, UnsupportedFlavorException, IOException {
  839. clipboardController.paste(upperNode, point);
  840. try {
  841. autoSave();
  842. } catch (IOException e) {
  843. e.printStackTrace();
  844. }
  845. }
  846. public void cut(CpsUpperNode upperNode) {
  847. clipboardController.cut(upperNode);
  848. try {
  849. autoSave();
  850. } catch (IOException e) {
  851. e.printStackTrace();
  852. }
  853. }
  854. /**
  855. * creates a new Template for the given cps Object
  856. * @param cps Object, which should become a template
  857. * @param parentFrame
  858. */
  859. public void createTemplate(HolonObject cps, JFrame parentFrame) {
  860. CreateTemplatePopUp t = new CreateTemplatePopUp(cps,model,parentFrame,this);
  861. t.setVisible(true);
  862. }
  863. public void getObjectsInDepth() {
  864. clipboardController.getObjectsInDepth();
  865. }
  866. public float getTotalProduction(ArrayList<AbstractCpsObject> arrayList) {
  867. return holonCanvasController.getTotalProduction(arrayList);
  868. }
  869. public float getTotalConsumption(ArrayList<AbstractCpsObject> arrayList) {
  870. return holonCanvasController.getTotalConsumption(arrayList);
  871. }
  872. public int getTotalElements(ArrayList<AbstractCpsObject> arrayList) {
  873. return holonCanvasController.getTotalElements(arrayList);
  874. }
  875. public int getTotalProducers(ArrayList<AbstractCpsObject> arrayList) {
  876. return holonCanvasController.getTotalProducers(arrayList);
  877. }
  878. public int getActiveElements(ArrayList<AbstractCpsObject> arrayList) {
  879. return holonCanvasController.getActiveElements(arrayList);
  880. }
  881. /**
  882. * Set the Background Image;
  883. *
  884. * @param imagePath Image Path
  885. * @param mode Image Mode
  886. * @param width Image custom width
  887. * @param height Image custom height
  888. */
  889. public void setBackgroundImage(String imagePath, int mode, int width, int height) {
  890. canvasController.setBackgroundImage(imagePath, mode, width, height);
  891. }
  892. public void setFlexiblePane(FlexiblePane fp) {
  893. simulationManager.setFlexiblePane(fp);
  894. }
  895. public Hashtable<String, StatisticGraphPanel> getGraphTable() {
  896. return model.getGraphTable();
  897. }
  898. public void setGraphTable(Hashtable<String, StatisticGraphPanel> gT) {
  899. model.setGraphTable(gT);
  900. }
  901. /**
  902. * Sets if the Simulation is running
  903. */
  904. public void setIsSimRunning(boolean isRunning) {
  905. globalController.setIsSimRunning(isRunning);
  906. }
  907. /**
  908. * Sets showConsoleLog.
  909. *
  910. * @param showConsoleLog
  911. */
  912. public void setShowConsoleLog(boolean showConsoleLog) {
  913. globalController.setShowConsoleLog(showConsoleLog);
  914. }
  915. /**
  916. * Sets show
  917. * @param showSupplyBars wether the bars should be shown
  918. */
  919. public void setShowSupplyBars(boolean showSupplyBars) {
  920. globalController.setShowSupplyBars(showSupplyBars);
  921. }
  922. /**
  923. * Sets fairness Model
  924. * @param fairnessModel that should be used.
  925. */
  926. public void setFairnessModel(short fairnessModel) {
  927. globalController.setFairnessModel(fairnessModel);
  928. }
  929. }