LoadStoreController.java 8.3 KB

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