StoreController.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. package ui.controller;
  2. import java.awt.Point;
  3. import java.io.FileWriter;
  4. import java.io.IOException;
  5. import java.util.ListIterator;
  6. import org.json.simple.JSONArray;
  7. import org.json.simple.JSONObject;
  8. import classes.Category;
  9. import classes.CpsEdge;
  10. import classes.CpsObject;
  11. import classes.HolonElement;
  12. import classes.HolonObject;
  13. import classes.HolonSwitch;
  14. import classes.HolonTransformer;
  15. import ui.model.Model;
  16. import ui.model.idCounter;
  17. public class StoreController {
  18. private Model MODEL;
  19. public StoreController(Model model) {
  20. this.MODEL = model;
  21. }
  22. /**
  23. * Writes the current State of the Modelling into a JSON File which can be
  24. * loaded
  25. *
  26. * @throws IOException
  27. */
  28. public void writeJSONFile(String path) throws IOException {
  29. JSONObject json = new JSONObject();
  30. json.put("ID", idCounter.getCounter());
  31. writeCategory(json);
  32. writeObjects(json);
  33. writeElements(json);
  34. writeEdges(json);
  35. writeElementGraph(json);
  36. FileWriter writer = new FileWriter(path);
  37. writer.write(json.toJSONString());
  38. writer.flush();
  39. writer.close();
  40. }
  41. /**
  42. * writes all Categories into a JSONObject
  43. *
  44. * @param json
  45. * @throws IOException
  46. */
  47. public void writeCategory(JSONObject json) {
  48. JSONArray arr = new JSONArray();
  49. for (Category cat : MODEL.getCategories())
  50. arr.add(cat.getName());
  51. json.put("CG", arr);
  52. }
  53. /**
  54. * writes all Objects in Category into a JSONObject
  55. *
  56. * @param json
  57. */
  58. public void writeObjects(JSONObject json) {
  59. JSONArray arr = new JSONArray();
  60. int i = 1;
  61. for (Category cats : MODEL.getCategories())
  62. for (CpsObject cps : cats.getObjects()) {
  63. arr.add(getObjectType(cps));
  64. arr.add(cps.getSav());
  65. arr.add(cps.getObjName());
  66. arr.add(cps.getImage());
  67. json.put("CGO" + i++, arr);
  68. arr = new JSONArray();
  69. }
  70. i = 1;
  71. for (CpsObject cps : MODEL.getObjectsOnCanvas()) {
  72. arr.add(getObjectType(cps));
  73. arr.add(cps.getObjName());
  74. arr.add(cps.getName());
  75. arr.add(cps.getID());
  76. arr.add(cps.getImage());
  77. arr.add(cps.getPosition().x);
  78. arr.add(cps.getPosition().y);
  79. json.put("CVSO" + i++, arr);
  80. arr = new JSONArray();
  81. ;
  82. }
  83. }
  84. /**
  85. * writes all Elements in Objects in Category into a JSONObject
  86. *
  87. * @param json
  88. */
  89. public void writeElements(JSONObject json) {
  90. JSONArray arr = new JSONArray();
  91. int i = 1;
  92. for (Category cats : MODEL.getCategories())
  93. for (CpsObject cps : cats.getObjects())
  94. if (cps instanceof HolonObject)
  95. for (HolonElement ele : ((HolonObject) cps).getElements()) {
  96. arr.add(ele.getSav());
  97. arr.add(ele.getObj());
  98. arr.add(ele.getEleName());
  99. arr.add(ele.getAmount());
  100. arr.add(ele.getEnergy());
  101. json.put("CGE" + i++, arr);
  102. arr = new JSONArray();
  103. }
  104. i = 1;
  105. for (CpsObject cps : MODEL.getObjectsOnCanvas()) {
  106. if (cps instanceof HolonObject)
  107. for (HolonElement ele : ((HolonObject) cps).getElements()) {
  108. arr.add(ele.getSav());
  109. arr.add(cps.getID());
  110. arr.add(ele.getEleName());
  111. arr.add(ele.getAmount());
  112. arr.add(ele.getEnergy());
  113. json.put("CVSE" + i++, arr);
  114. arr = new JSONArray();
  115. }
  116. }
  117. }
  118. /**
  119. * write all Edges into a JSONObject
  120. *
  121. * @param json
  122. */
  123. public void writeEdges(JSONObject json) {
  124. JSONArray arr = new JSONArray();
  125. int i = 1;
  126. for (CpsEdge edge : MODEL.getEdgesOnCanvas()) {
  127. arr.add(edge.getA().getID());
  128. arr.add(edge.getB().getID());
  129. arr.add(edge.getCapacity());
  130. arr.add(edge.getFlow());
  131. json.put("EDGE" + i++, arr);
  132. arr = new JSONArray();
  133. }
  134. }
  135. /**
  136. * writes the Graph from all Elements into a JSONObject
  137. *
  138. * @param json
  139. */
  140. public void writeElementGraph(JSONObject json) {
  141. ListIterator<Point> iterator;
  142. JSONArray arr = new JSONArray();
  143. int i = 1;
  144. for (Category cats : MODEL.getCategories())
  145. for (CpsObject cps : cats.getObjects())
  146. if (cps instanceof HolonObject)
  147. for (HolonElement ele : ((HolonObject) cps).getElements())
  148. if (!ele.getGraphPoints().isEmpty()) {
  149. iterator = ele.getGraphPoints().listIterator();
  150. arr.add(ele.getSav());
  151. arr.add(ele.getObj());
  152. arr.add(ele.getEleName());
  153. while (iterator.hasNext()) {
  154. Point p = iterator.next();
  155. arr.add((int) p.getX());
  156. arr.add((int) p.getY());
  157. }
  158. json.put("CGGP" + i++, arr);
  159. arr = new JSONArray();
  160. }
  161. i = 1;
  162. for (CpsObject cps : MODEL.getObjectsOnCanvas()) {
  163. if (cps instanceof HolonObject)
  164. for (HolonElement ele : ((HolonObject) cps).getElements())
  165. if (!ele.getGraphPoints().isEmpty()) {
  166. iterator = ele.getGraphPoints().listIterator();
  167. arr.add(ele.getSav());
  168. arr.add(cps.getID());
  169. arr.add(ele.getEleName());
  170. while (iterator.hasNext()) {
  171. Point p = iterator.next();
  172. arr.add((int) p.getX());
  173. arr.add((int) p.getY());
  174. }
  175. json.put("CVSGP" + i++, arr);
  176. arr = new JSONArray();
  177. }
  178. }
  179. }
  180. /**
  181. * Return the Object Type
  182. *
  183. * @param cps
  184. * @return
  185. */
  186. public String getObjectType(CpsObject cps) {
  187. if (cps instanceof HolonObject)
  188. return "HolonObject";
  189. if (cps instanceof HolonTransformer)
  190. return "HolonTransformer";
  191. if (cps instanceof HolonSwitch)
  192. return "HolonSwitch";
  193. else
  194. return "CpsNode";
  195. }
  196. }