StoreController.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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());getClass();
  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. arr.add(Boolean.compare(ele.getActive(), true) + 1);
  114. json.put("CVSE" + i++, arr);
  115. arr = new JSONArray();
  116. }
  117. }
  118. }
  119. /**
  120. * write all Edges into a JSONObject
  121. *
  122. * @param json
  123. */
  124. public void writeEdges(JSONObject json) {
  125. JSONArray arr = new JSONArray();
  126. int i = 1;
  127. for (CpsEdge edge : MODEL.getEdgesOnCanvas()) {
  128. arr.add(edge.getA().getID());
  129. arr.add(edge.getB().getID());
  130. arr.add(edge.getCapacity());
  131. arr.add(edge.getFlow());
  132. json.put("EDGE" + i++, arr);
  133. arr = new JSONArray();
  134. }
  135. }
  136. /**
  137. * writes the Graph from all Elements into a JSONObject
  138. *
  139. * @param json
  140. */
  141. public void writeElementGraph(JSONObject json) {
  142. ListIterator<Point> iterator;
  143. JSONArray arr = new JSONArray();
  144. int i = 1;
  145. for (Category cats : MODEL.getCategories())
  146. for (CpsObject cps : cats.getObjects())
  147. if (cps instanceof HolonObject)
  148. for (HolonElement ele : ((HolonObject) cps).getElements())
  149. if (!ele.getGraphPoints().isEmpty()) {
  150. iterator = ele.getGraphPoints().listIterator();
  151. arr.add(ele.getSav());
  152. arr.add(ele.getObj());
  153. arr.add(ele.getEleName());
  154. while (iterator.hasNext()) {
  155. Point p = iterator.next();
  156. arr.add((int) p.getX());
  157. arr.add((int) p.getY());
  158. }
  159. json.put("CGGP" + i++, arr);
  160. arr = new JSONArray();
  161. }
  162. i = 1;
  163. for (CpsObject cps : MODEL.getObjectsOnCanvas()) {
  164. if (cps instanceof HolonObject)
  165. for (HolonElement ele : ((HolonObject) cps).getElements())
  166. if (!ele.getGraphPoints().isEmpty()) {
  167. iterator = ele.getGraphPoints().listIterator();
  168. arr.add(ele.getSav());
  169. arr.add(cps.getID());
  170. arr.add(ele.getEleName());
  171. while (iterator.hasNext()) {
  172. Point p = iterator.next();
  173. arr.add((int) p.getX());
  174. arr.add((int) p.getY());
  175. }
  176. json.put("CVSGP" + i++, arr);
  177. arr = new JSONArray();
  178. }
  179. }
  180. }
  181. /**
  182. * Return the Object Type
  183. *
  184. * @param cps
  185. * @return
  186. */
  187. public String getObjectType(CpsObject cps) {
  188. if (cps instanceof HolonObject)
  189. return "HolonObject";
  190. if (cps instanceof HolonTransformer)
  191. return "HolonTransformer";
  192. if (cps instanceof HolonSwitch)
  193. return "HolonSwitch";
  194. else
  195. return "CpsNode";
  196. }
  197. }