LoadStoreController.java 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. package ui.controller;
  2. import java.io.BufferedReader;
  3. import java.io.File;
  4. import java.io.FileReader;
  5. import java.io.FileWriter;
  6. import java.io.IOException;
  7. import java.util.ArrayList;
  8. import java.util.Iterator;
  9. import org.json.simple.JSONArray;
  10. import org.json.simple.JSONObject;
  11. import org.json.simple.parser.JSONParser;
  12. import org.json.simple.parser.ParseException;
  13. import classes.Category;
  14. import classes.CpsEdge;
  15. import classes.CpsNode;
  16. import classes.CpsObject;
  17. import classes.HolonElement;
  18. import classes.HolonObject;
  19. import classes.HolonSwitch;
  20. import classes.HolonTransformer;
  21. import ui.model.Model;
  22. import ui.model.idCounter;
  23. public class LoadStoreController {
  24. private Model MODEL;
  25. private CategoryController categoryController;
  26. private CanvasController canvasController;
  27. private ObjectController objectController;
  28. public LoadStoreController(Model model, CategoryController cg, CanvasController cvs, ObjectController obj) {
  29. this.MODEL = model;
  30. this.categoryController = cg;
  31. this.canvasController = cvs;
  32. this.objectController = obj;
  33. }
  34. /**
  35. * Writes a Savefile in .json
  36. *
  37. * @throws IOException
  38. */
  39. public void writeJSONFile(String path) throws IOException {
  40. JSONObject json = new JSONObject();
  41. json.put("ID", idCounter.getCounter());
  42. writeCategory(json);
  43. writeObjects(json);
  44. writeElements(json);
  45. writeEdges(json);
  46. FileWriter writer = new FileWriter(path);
  47. writer.write(json.toJSONString());
  48. writer.flush();
  49. writer.close();
  50. }
  51. /**
  52. * writes all Categories into a JSONObject
  53. *
  54. * @param json
  55. * @throws IOException
  56. */
  57. public void writeCategory(JSONObject json) {
  58. JSONArray arr = new JSONArray();
  59. for (Category cat : MODEL.getCategories())
  60. arr.add(cat.getName());
  61. json.put("CG", arr);
  62. }
  63. /**
  64. * writes all Objects in Category into a JSONObject
  65. *
  66. * @param json
  67. */
  68. public void writeObjects(JSONObject json) {
  69. JSONArray arr = new JSONArray();
  70. int i = 1;
  71. for (Category cats : MODEL.getCategories())
  72. for (CpsObject cps : cats.getObjects()) {
  73. arr.add(getObjectType(cps));
  74. arr.add(cps.getSav());
  75. arr.add(cps.getObjName());
  76. arr.add(cps.getImage());
  77. json.put("CGO" + i++, arr);
  78. arr = new JSONArray();
  79. }
  80. i = 1;
  81. for (CpsObject cps : MODEL.getObjectsOnCanvas()) {
  82. arr.add(getObjectType(cps));
  83. arr.add(cps.getObjName());
  84. arr.add(cps.getID());
  85. arr.add(cps.getImage());
  86. arr.add(cps.getPosition().x);
  87. arr.add(cps.getPosition().y);
  88. json.put("CVSO" + i++, arr);
  89. arr = new JSONArray();
  90. ;
  91. }
  92. }
  93. /**
  94. *
  95. * @param cps
  96. * @return
  97. */
  98. public String getObjectType(CpsObject cps) {
  99. if (cps instanceof HolonObject)
  100. return "HolonObject";
  101. if (cps instanceof HolonTransformer)
  102. return "HolonTransformer";
  103. if (cps instanceof HolonSwitch)
  104. return "HolonSwitch";
  105. else
  106. return "CpsNode";
  107. }
  108. /**
  109. * writes all Elements in Objects in Category into a JSONObject
  110. *
  111. * @param json
  112. */
  113. public void writeElements(JSONObject json) {
  114. JSONArray arr = new JSONArray();
  115. int i = 1;
  116. for (Category cats : MODEL.getCategories())
  117. for (CpsObject cps : cats.getObjects())
  118. if (cps instanceof HolonObject)
  119. for (HolonElement ele : ((HolonObject) cps).getElements()) {
  120. arr.add(ele.getSav());
  121. arr.add(ele.getObj());
  122. arr.add(ele.getEleName());
  123. arr.add(ele.getAmount());
  124. arr.add(ele.getEnergy());
  125. json.put("CGE" + i++, arr);
  126. arr = new JSONArray();
  127. }
  128. i = 1;
  129. for (CpsObject cps : MODEL.getObjectsOnCanvas()) {
  130. if (cps instanceof HolonObject)
  131. for (HolonElement ele : ((HolonObject) cps).getElements()) {
  132. arr.add(ele.getSav());
  133. arr.add(cps.getID());
  134. arr.add(ele.getEleName());
  135. arr.add(ele.getAmount());
  136. arr.add(ele.getEnergy());
  137. json.put("CVSE" + i++, arr);
  138. arr = new JSONArray();
  139. }
  140. }
  141. }
  142. /**
  143. * write all Edges into a JSONObject
  144. *
  145. * @param json
  146. */
  147. public void writeEdges(JSONObject json) {
  148. JSONArray arr = new JSONArray();
  149. int i = 1;
  150. for (CpsEdge edge : MODEL.getEdgesOnCanvas()) {
  151. arr.add(edge.getA().getID());
  152. arr.add(edge.getB().getID());
  153. arr.add(edge.getCapacity());
  154. arr.add(edge.getFlow());
  155. json.put("EDGE" + i++, arr);
  156. arr = new JSONArray();
  157. }
  158. }
  159. public void readJSON(String path) throws IOException {
  160. JSONParser parser = new JSONParser();
  161. MODEL.setCategories(new ArrayList<>());
  162. MODEL.setObjectsOnCanvas(new ArrayList<>());
  163. ArrayList<String> obj = new ArrayList<>();
  164. ArrayList<String> ele = new ArrayList<>();
  165. ArrayList<String> edge = new ArrayList<>();
  166. try {
  167. JSONObject json = (JSONObject) parser.parse(new FileReader(path));
  168. for (Object key : json.keySet()) {
  169. if (key.equals("CG"))
  170. readCategory((JSONArray) json.get(key));
  171. else if (key.toString().contains("CGO") || key.toString().contains("CVSO"))
  172. obj.add(key.toString());
  173. else if (key.toString().contains("CGE") || key.toString().contains("CVSE"))
  174. ele.add(key.toString());
  175. else if (key.toString().contains("EDGE"))
  176. edge.add(key.toString());
  177. else idCounter.setCounter(Integer.parseInt(json.get(key.toString()).toString()));
  178. }
  179. dispatch(obj, json);
  180. dispatch(ele, json);
  181. dispatch(edge, json);
  182. } catch (ParseException e) {
  183. // TODO Auto-generated catch block
  184. e.printStackTrace();
  185. }
  186. }
  187. public void dispatch(ArrayList<String> input, JSONObject json) {
  188. for (String str : input) {
  189. if (str.contains("CGO")) {
  190. readCategoryObject((JSONArray) json.get(str));
  191. } else if (str.contains("CVSO")) {
  192. readCanvasObject((JSONArray) json.get(str));
  193. } else if (str.contains("CGE") || str.contains("CVSE")) {
  194. readElement((JSONArray) json.get(str));
  195. } else if (str.contains("EDGE")) {
  196. readEdge((JSONArray) json.get(str));
  197. }
  198. }
  199. }
  200. public void readCategory(JSONArray arr) {
  201. Iterator<Object> i = arr.iterator();
  202. while (i.hasNext()) {
  203. categoryController.addNewCategory(i.next().toString());
  204. }
  205. }
  206. public void readCategoryObject(JSONArray arr) {
  207. Iterator<Object> i = arr.iterator();
  208. String type = i.next().toString();
  209. if (type.equals("HolonObject")) {
  210. categoryController.addNewHolonObject(categoryController.searchCatNode(i.next().toString()),
  211. i.next().toString(), new ArrayList<>(), i.next().toString());
  212. }
  213. if (type.equals("HolonTransformer")) {
  214. categoryController.addNewHolonTransformer(categoryController.searchCatNode(i.next().toString()),
  215. i.next().toString(), i.next().toString());
  216. }
  217. if (type.equals("HolonSwitch")) {
  218. categoryController.addNewHolonSwitch(categoryController.searchCatNode(i.next().toString()),
  219. i.next().toString(), i.next().toString());
  220. }
  221. }
  222. public void readCanvasObject(JSONArray arr) {
  223. Iterator<Object> i = arr.iterator();
  224. CpsObject cps = null;
  225. String type = i.next().toString();
  226. if (type.equals("HolonObject")) {
  227. cps = new HolonObject(i.next().toString());
  228. }
  229. if (type.equals("HolonTransformer")) {
  230. cps = new HolonTransformer(i.next().toString());
  231. }
  232. if (type.equals("HolonSwitch")) {
  233. cps = new HolonSwitch(i.next().toString());
  234. }
  235. if (type.equals("CpsNode")) {
  236. cps = new CpsNode(i.next().toString());
  237. }
  238. cps.setID(Integer.parseInt(i.next().toString()));
  239. cps.setImage(i.next().toString());
  240. cps.setPosition(Integer.parseInt(i.next().toString()), Integer.parseInt(i.next().toString()));
  241. canvasController.addObjectIntoCanvas(cps);
  242. }
  243. public void readElement(JSONArray arr) {
  244. Iterator<Object> i = arr.iterator();
  245. String sav = i.next().toString();
  246. System.out.println(sav);
  247. if (sav.equals("Canvas")) {
  248. objectController.addNewElementIntoCanvasObject(Integer.parseInt(i.next().toString()), i.next().toString(),
  249. Integer.parseInt(i.next().toString()), Float.parseFloat(i.next().toString()));
  250. } else
  251. objectController.addNewElementIntoCategoryObject(sav, i.next().toString(), i.next().toString(),
  252. Integer.parseInt(i.next().toString()), Float.parseFloat(i.next().toString()));
  253. }
  254. public void readEdge(JSONArray arr) {
  255. Iterator<Object> i = arr.iterator();
  256. CpsEdge edge = new CpsEdge(objectController.searchByID(Integer.parseInt(i.next().toString())),
  257. objectController.searchByID(Integer.parseInt(i.next().toString())));
  258. edge.setCapacity(Float.parseFloat(i.next().toString()));
  259. edge.setFlow(Float.parseFloat(i.next().toString()));
  260. canvasController.addEdgeOnCanvas(edge);
  261. }
  262. }