Control.java 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064
  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. try {
  313. autoSave();
  314. } catch (IOException e) {
  315. System.out.println("Error by Replacing "+toBeReplaced.toString() + " by " + by.toString());
  316. e.printStackTrace();
  317. }
  318. }
  319. /**
  320. * Add an edge to the Canvas.
  321. *
  322. * @param edge the edge
  323. */
  324. public void addEdgeOnCanvas(CpsEdge edge) {
  325. canvasController.addEdgeOnCanvas(edge);
  326. try {
  327. autoSave();
  328. } catch (IOException e) {
  329. e.printStackTrace();
  330. }
  331. }
  332. /**
  333. * Removes an Edge from the Canvas.
  334. *
  335. * @param edge the edge to remove
  336. */
  337. public void removeEdgesOnCanvas(CpsEdge edge) {
  338. canvasController.removeEdgesOnCanvas(edge);
  339. try {
  340. autoSave();
  341. } catch (IOException e) {
  342. e.printStackTrace();
  343. }
  344. }
  345. /**
  346. * Set the selected Edge.
  347. *
  348. * @param edge that is selected
  349. */
  350. public void setSelecteEdge(CpsEdge edge) {
  351. model.setSelectedEdge(edge);
  352. }
  353. /**
  354. * Returns the ID of the selected Object 0 = no Object is selected.
  355. *
  356. * @param id the ID of the selected Object
  357. */
  358. public void setSelectedObjectID(int id) {
  359. objectController.setSelectedObjectID(id);
  360. }
  361. /* Operations for Objects and Elements */
  362. /**
  363. * Add a new Element into a Object on the Canvas.
  364. *
  365. * @param objectId the Object ID
  366. * @param ele the Name of the Element
  367. * @param amount the Amount
  368. * @param energy the Energy
  369. * @param elementId the Element ID
  370. */
  371. public void addElementCanvasObject(int objectId, String ele, int amount, float energy, int elementId) {
  372. objectController.addNewElementIntoCanvasObject(objectId, ele, amount, energy, elementId);
  373. try {
  374. autoSave();
  375. } catch (IOException e) {
  376. e.printStackTrace();
  377. }
  378. }
  379. /**
  380. * Add a new Element into a Object in Category.
  381. *
  382. * @param catName the Category
  383. * @param objName the Object
  384. * @param eleName the Element Name
  385. * @param amount the amount
  386. * @param energy the Energy
  387. */
  388. public void addElementCategoryObject(String catName, String objName, String eleName, int amount, float energy) {
  389. objectController.addNewElementIntoCategoryObject(catName, objName, eleName, amount, energy);
  390. }
  391. /**
  392. * deletes a Element from a given Canvas Object.
  393. *
  394. * @param id the ID
  395. * @param elementid the Element ID
  396. */
  397. public void deleteElementCanvas(int id, int elementid) {
  398. objectController.deleteElementInCanvas(id, elementid);
  399. try {
  400. autoSave();
  401. } catch (IOException e) {
  402. e.printStackTrace();
  403. }
  404. }
  405. /* Global Operations */
  406. /**
  407. * Add a SubNetColor.
  408. *
  409. * @param c the Color
  410. */
  411. public void addSubNetColor(Color c) {
  412. globalController.addSubNetColor(c);
  413. }
  414. /**
  415. * Returns SCALE.
  416. *
  417. * @return SCALE
  418. */
  419. public int getScale() {
  420. return globalController.getScale();
  421. }
  422. /**
  423. * Changes the value of SCALE and SCALEDIV2.
  424. *
  425. * @param s Scale
  426. */
  427. public void setScale(int s) {
  428. globalController.setScale(s);
  429. }
  430. /**
  431. * Returns SCALE Divided by 2.
  432. *
  433. * @return SCALE Divided by 2
  434. */
  435. public int getScaleDiv2() {
  436. return globalController.getScaleDiv2();
  437. }
  438. /**
  439. * sets the current Iteration.
  440. *
  441. * @param curit the current Iteration
  442. */
  443. public void setCurIteration(int curit) {
  444. globalController.setCurIteration(curit);
  445. }
  446. /**
  447. * Writes the current State of the Modelling into a JSON File which can be
  448. * loaded.
  449. *
  450. * @param path the Path
  451. * @throws IOException exception
  452. */
  453. public void saveFile(String path) throws IOException, ArchiveException {
  454. saveController.writeSave(path);
  455. }
  456. /**
  457. * Reads the the Save File and load the state into the Model.
  458. *
  459. * @param path the Path
  460. * @throws IOException exception
  461. * @throws ArchiveException
  462. */
  463. public void loadFile(String path) throws IOException, ArchiveException {
  464. loadController.readSave(path);
  465. saveCategory();
  466. autoSave();
  467. }
  468. /**
  469. * Reads the Json File from Autosave
  470. *
  471. * @param path
  472. * @throws IOException
  473. */
  474. public void loadAutoSave(String path) throws IOException {
  475. loadController.readJson(path);
  476. }
  477. public ArrayList<Integer> loadSavedWindowDimensionsIfExistent() {
  478. try {
  479. return loadController.readWindowDimensions(otherDir + dimensionsFileName);
  480. } catch (Exception e) {
  481. return new ArrayList<>();
  482. }
  483. }
  484. /**
  485. * Init the CategoryListener.
  486. *
  487. * @param catLis the CategoryListener
  488. */
  489. public void initListener(CategoryListener catLis) {
  490. categoryController.addCategoryListener(catLis);
  491. }
  492. /**
  493. * calculates the flow of the edges and the supply for objects for the
  494. * current Timestep.
  495. */
  496. public void calculateStateForCurrentTimeStep() {
  497. // simulationManager.reset();
  498. simulationManager.calculateStateForTimeStep(model.getCurIteration());
  499. }
  500. /**
  501. * calculates the flow of the edges and the supply for objects.
  502. *
  503. * @param x current Iteration
  504. */
  505. public void calculateStateForTimeStep(int x) {
  506. // simulationManager.reset();
  507. simulationManager.calculateStateForTimeStep(x);
  508. }
  509. /**
  510. * resets the whole State of the simulation including a reset of all Edges
  511. * to the default "is working" state
  512. */
  513. public void resetSimulation() {
  514. setIsSimRunning(false);
  515. simulationManager.resetSimulation();
  516. }
  517. /**
  518. * Set the Canvas.
  519. *
  520. * @param can the Canvas
  521. */
  522. public void setCanvas(MyCanvas can) {
  523. simulationManager.setCanvas(can);
  524. }
  525. /**
  526. * make an autosave.
  527. *
  528. * @throws IOException Exception
  529. */
  530. public void autoSave() throws IOException {
  531. autoSaveController.increaseAutoSaveNr();
  532. saveController.writeAutosave(autosaveDir + rand + autoSaveController.getAutoSaveNr());
  533. if (autoSaveController.allowed()) {
  534. new File(autosaveDir + rand + (autoSaveController.getAutoSaveNr() - globalController.getNumbersOfSaves()))
  535. .delete();
  536. }
  537. }
  538. /**
  539. * find all old auto save files (with a file-name, that does not contain the current rand)
  540. *
  541. * @return a list of files, that are not from the current run
  542. */
  543. public ArrayList<File> filterOldAutoSaveFiles() {
  544. File[] files = new File(autosaveDir).listFiles();
  545. ArrayList<File> oldAutoSaves = new ArrayList<>();
  546. for (File file : files) {
  547. if (!file.getName().contains(String.valueOf(rand)))
  548. oldAutoSaves.add(file);
  549. }
  550. return oldAutoSaves;
  551. }
  552. /**
  553. * deletes the old autosave files
  554. */
  555. public void deleteObsoleteAutoSaveFiles() {
  556. for (File file : filterOldAutoSaveFiles()) {
  557. file.delete();
  558. }
  559. }
  560. public void saveCategory() throws IOException {
  561. saveController.writeCategory(categoryDir + "Category.json");
  562. }
  563. public void savePosAndSizeOfWindow(int x, int y, int width, int height) throws IOException, ArchiveException {
  564. saveController.writeWindowStatus(otherDir + dimensionsFileName, x, y, width, height);
  565. }
  566. /**
  567. * Returns the undo save.
  568. *
  569. * @return the undo save
  570. */
  571. public String getUndoSave() {
  572. autoSaveController.decreaseAutoSaveNr();
  573. if (!new File(autosaveDir + rand + (autoSaveController.getAutoSaveNr())).exists()) {
  574. autoSaveController.increaseAutoSaveNr();
  575. }
  576. return autosaveDir + rand + (autoSaveController.getAutoSaveNr());
  577. }
  578. /**
  579. * Returns the redo save.
  580. *
  581. * @return the redo save
  582. */
  583. public String getRedoSave() {
  584. autoSaveController.increaseAutoSaveNr();
  585. if (!new File(autosaveDir + rand + (autoSaveController.getAutoSaveNr())).exists()) {
  586. autoSaveController.decreaseAutoSaveNr();
  587. // if it still does not exist, try old autosaves
  588. if (!new File(autosaveDir + rand + (autoSaveController.getAutoSaveNr())).exists()) {
  589. ArrayList<File> oldAutoSaves = filterOldAutoSaveFiles();
  590. if (oldAutoSaves.size() > 0) {
  591. return autosaveDir + oldAutoSaves.get(oldAutoSaves.size() - 1).getName();
  592. }
  593. }
  594. }
  595. return autosaveDir + rand + (autoSaveController.getAutoSaveNr());
  596. }
  597. /**
  598. * Getter for Model.
  599. *
  600. * @return the Model
  601. */
  602. public Model getModel() {
  603. return model;
  604. }
  605. /**
  606. * get the Simulation Manager.
  607. *
  608. * @return the Simulation Manager
  609. */
  610. public SimulationManager getSimManager() {
  611. return simulationManager;
  612. }
  613. /**
  614. * Getter for selected CpsObject.
  615. *
  616. * @param text String the Text
  617. * @param color the color of the Text
  618. * @param p size of the Text
  619. * @param bold bold or not
  620. * @param italic italic or not
  621. * @param nl new line or not
  622. */
  623. public void addTextToConsole(String text, Color color, int p, boolean bold, boolean italic, boolean nl) {
  624. consoleController.addTextToConsole(text, color, p, bold, italic, nl);
  625. }
  626. /**
  627. * Print Text on the console in black and font size 12.
  628. *
  629. * @param text String the Text
  630. */
  631. public void addTextToConsole(String text) {
  632. consoleController.addTextToConsole(text);
  633. }
  634. /**
  635. * Clears the console.
  636. */
  637. public void clearConsole() {
  638. consoleController.clearConsole();
  639. }
  640. /**
  641. * Set the timerSpeed.
  642. *
  643. * @param t interval in ms
  644. */
  645. public void setTimerSpeed(int t) {
  646. globalController.setTimerSpeed(t);
  647. }
  648. /**
  649. * Set the Canvas X Size.
  650. *
  651. * @param canvasX the cANVAS_X to set
  652. */
  653. public void setCanvasX(int canvasX) {
  654. globalController.setCanvasX(canvasX);
  655. try {
  656. autoSave();
  657. } catch (IOException e) {
  658. e.printStackTrace();
  659. }
  660. }
  661. /**
  662. * Set the Canvas Y Size.
  663. *
  664. * @param canvasY the cANVAS_Y to set
  665. */
  666. public void setCanvasY(int canvasY) {
  667. globalController.setCanvasY(canvasY);
  668. try {
  669. autoSave();
  670. } catch (IOException e) {
  671. e.printStackTrace();
  672. }
  673. }
  674. public void setMaxCapacity(float cap) {
  675. globalController.setMaxCapacity(cap);
  676. }
  677. /**
  678. * Set the Algorithm.
  679. *
  680. * @param obj the Algorithm
  681. */
  682. public void setAlgorithm(Object obj) {
  683. multiPurposeController.setAlgorithm(obj);
  684. }
  685. /**
  686. * Run the Algorithm.
  687. */
  688. public void runAlgorithm(Model model, Control controller) {
  689. if (model.getAlgorithm() != null) {
  690. ((CpsAlgorithm) model.getAlgorithm()).runAlgorithm(model, controller);
  691. }
  692. }
  693. // ========================= MANAGING TRACKED OBJECTS ====================
  694. public ArrayList<AbstractCpsObject> getTrackingObj() {
  695. return statsController.getTrackingObj();
  696. }
  697. public void setTrackingObj(ArrayList<AbstractCpsObject> objArr) {
  698. statsController.setTrackingObj(objArr);
  699. }
  700. public void addTrackingObj(AbstractCpsObject obj) {
  701. statsController.addTrackingObj(obj);
  702. try {
  703. autoSave();
  704. } catch (IOException e) {
  705. e.printStackTrace();
  706. }
  707. }
  708. public void removeTrackingObj(AbstractCpsObject obj) {
  709. statsController.removeTrackingObj(obj);
  710. try {
  711. autoSave();
  712. } catch (IOException e) {
  713. e.printStackTrace();
  714. }
  715. }
  716. // ========================== MANAGING TRACKED OBJECTS END ================
  717. /**
  718. * Controlling Nodes of Nodes
  719. */
  720. public void addUpperNode(String nodeName, CpsUpperNode upperNode, ArrayList<AbstractCpsObject> toGroup) {
  721. nodeController.doUpperNode(nodeName, upperNode, toGroup);
  722. try {
  723. autoSave();
  724. } catch (IOException e) {
  725. e.printStackTrace();
  726. }
  727. }
  728. public void delUpperNode(CpsUpperNode node, CpsUpperNode upperNode) {
  729. nodeController.undoUpperNode(node, upperNode);
  730. try {
  731. autoSave();
  732. } catch (IOException e) {
  733. e.printStackTrace();
  734. }
  735. }
  736. public void addObjUpperNode(AbstractCpsObject object, CpsUpperNode upperNode) {
  737. nodeController.addObjectInUpperNode(object, upperNode, true);
  738. try {
  739. autoSave();
  740. } catch (IOException e) {
  741. e.printStackTrace();
  742. }
  743. }
  744. public void delObjUpperNode(AbstractCpsObject object, CpsUpperNode upperNode) {
  745. nodeController.deleteObjectInUpperNode(object, upperNode);
  746. if (object instanceof CpsUpperNode)
  747. canvasController.bfsNodeCleaner((CpsUpperNode) object);
  748. try {
  749. autoSave();
  750. } catch (IOException e) {
  751. e.printStackTrace();
  752. }
  753. }
  754. /**
  755. * Replaces {@code toBePlaced} by {@code by} in {@code upperNode}
  756. * @param toBeReplaced
  757. * @param by
  758. * @param upperNode
  759. */
  760. public void replaceObjUpperNode(AbstractCpsObject toBeReplaced,
  761. AbstractCpsObject by, CpsUpperNode upperNode) {
  762. nodeController.replaceObjectInUpperNode(toBeReplaced, by, upperNode);
  763. try {
  764. autoSave();
  765. } catch (IOException e) {
  766. System.out.println("Error by Replacing "+toBeReplaced.toString()
  767. + " by " + by.toString() + " in UpperNode " + upperNode.toString());
  768. e.printStackTrace();
  769. }
  770. }
  771. public void addEdgeUpperNode(CpsEdge edge, CpsUpperNode upperNode) {
  772. nodeController.addEdge(edge, upperNode);
  773. try {
  774. autoSave();
  775. } catch (IOException e) {
  776. e.printStackTrace();
  777. }
  778. }
  779. public void delEdgeUpperNode(CpsEdge edge, CpsUpperNode upperNode) {
  780. nodeController.deleteEdge(edge, upperNode);
  781. try {
  782. autoSave();
  783. } catch (IOException e) {
  784. e.printStackTrace();
  785. }
  786. }
  787. public void connectNodes(CpsEdge edge, CpsUpperNode upperNode) {
  788. nodeController.connectNodes(edge, upperNode);
  789. try {
  790. autoSave();
  791. } catch (IOException e) {
  792. e.printStackTrace();
  793. }
  794. }
  795. public void disconnectNodes(CpsEdge edge, CpsUpperNode upperNode) {
  796. nodeController.disconnectNodes(edge, upperNode);
  797. try {
  798. autoSave();
  799. } catch (IOException e) {
  800. e.printStackTrace();
  801. }
  802. }
  803. /**
  804. * Get the number of HolonObjects in the given List
  805. *
  806. * @param list
  807. */
  808. public int getNumberHolonObjects(ArrayList<AbstractCpsObject> list) {
  809. return objectController.getNumberHolonObjects(list);
  810. }
  811. /**
  812. * Get the number of HolonObjects with the given state in the given List
  813. *
  814. * @param list
  815. * @param state
  816. */
  817. public int getNumberStateObjects(ArrayList<AbstractCpsObject> list, int state) {
  818. return objectController.getNumberStateObjects(list, state);
  819. }
  820. /**
  821. * Returns HolonBodySCALE.
  822. *
  823. * @return SCALE
  824. */
  825. public int getHolonBodyScale() {
  826. return globalController.getHolonBodyScale();
  827. }
  828. /**
  829. * Changes the value of HolonBodySCALE
  830. *
  831. * @param s HolonBodyScale
  832. */
  833. public void setHolonBodyScale(int s) {
  834. globalController.setHolonBodyScale(s);
  835. }
  836. /**
  837. * Sets the ID of the selected HolonBody
  838. *
  839. * @param i ID of the selected HolonBody
  840. */
  841. public void addSelectedHolonBody(int i) {
  842. objectController.addSelectedHolonBody(i);
  843. }
  844. /**
  845. * Copy all Selected Objects.
  846. */
  847. public void copy(CpsUpperNode upperNode) {
  848. clipboardController.copy(upperNode);
  849. }
  850. public void paste(CpsUpperNode upperNode, Point point)
  851. throws JsonParseException, UnsupportedFlavorException, IOException {
  852. clipboardController.paste(upperNode, point);
  853. try {
  854. autoSave();
  855. } catch (IOException e) {
  856. e.printStackTrace();
  857. }
  858. }
  859. public void cut(CpsUpperNode upperNode) {
  860. clipboardController.cut(upperNode);
  861. try {
  862. autoSave();
  863. } catch (IOException e) {
  864. e.printStackTrace();
  865. }
  866. }
  867. /**
  868. * creates a new Template for the given cps Object
  869. * @param cps Object, which should become a template
  870. * @param parentFrame
  871. */
  872. public void createTemplate(HolonObject cps, JFrame parentFrame) {
  873. CreateTemplatePopUp t = new CreateTemplatePopUp(cps,model,parentFrame,this);
  874. t.setVisible(true);
  875. }
  876. public void getObjectsInDepth() {
  877. clipboardController.getObjectsInDepth();
  878. }
  879. public float getTotalProduction(ArrayList<AbstractCpsObject> arrayList) {
  880. return holonCanvasController.getTotalProduction(arrayList);
  881. }
  882. public float getTotalConsumption(ArrayList<AbstractCpsObject> arrayList) {
  883. return holonCanvasController.getTotalConsumption(arrayList);
  884. }
  885. public int getTotalElements(ArrayList<AbstractCpsObject> arrayList) {
  886. return holonCanvasController.getTotalElements(arrayList);
  887. }
  888. public int getTotalProducers(ArrayList<AbstractCpsObject> arrayList) {
  889. return holonCanvasController.getTotalProducers(arrayList);
  890. }
  891. public int getActiveElements(ArrayList<AbstractCpsObject> arrayList) {
  892. return holonCanvasController.getActiveElements(arrayList);
  893. }
  894. /**
  895. * Set the Background Image;
  896. *
  897. * @param imagePath Image Path
  898. * @param mode Image Mode
  899. * @param width Image custom width
  900. * @param height Image custom height
  901. */
  902. public void setBackgroundImage(String imagePath, int mode, int width, int height) {
  903. canvasController.setBackgroundImage(imagePath, mode, width, height);
  904. }
  905. public void setFlexiblePane(FlexiblePane fp) {
  906. simulationManager.setFlexiblePane(fp);
  907. }
  908. public Hashtable<String, StatisticGraphPanel> getGraphTable() {
  909. return model.getGraphTable();
  910. }
  911. public void setGraphTable(Hashtable<String, StatisticGraphPanel> gT) {
  912. model.setGraphTable(gT);
  913. }
  914. /**
  915. * Sets if the Simulation is running
  916. */
  917. public void setIsSimRunning(boolean isRunning) {
  918. globalController.setIsSimRunning(isRunning);
  919. }
  920. /**
  921. * Sets showConsoleLog.
  922. *
  923. * @param showConsoleLog
  924. */
  925. public void setShowConsoleLog(boolean showConsoleLog) {
  926. globalController.setShowConsoleLog(showConsoleLog);
  927. }
  928. /**
  929. * Sets show
  930. * @param showSupplyBars wether the bars should be shown
  931. */
  932. public void setShowSupplyBars(boolean showSupplyBars) {
  933. globalController.setShowSupplyBars(showSupplyBars);
  934. }
  935. /**
  936. * Sets fairness Model
  937. * @param fairnessModel that should be used.
  938. */
  939. public void setFairnessModel(short fairnessModel) {
  940. globalController.setFairnessModel(fairnessModel);
  941. }
  942. }