123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388 |
- package ui.controller;
- import java.awt.Color;
- import java.awt.Point;
- import java.io.FileReader;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.List;
- import java.util.stream.Collector;
- import java.util.stream.Collectors;
- import org.json.simple.parser.JSONParser;
- import org.json.simple.parser.ParseException;
- import com.google.gson.Gson;
- import com.google.gson.GsonBuilder;
- import com.google.gson.JsonElement;
- import com.google.gson.JsonObject;
- import com.google.gson.JsonParser;
- import com.google.gson.stream.JsonReader;
- import TypeAdapter.AbstractCpsObjectAdapter;
- import TypeAdapter.ColorAdapter;
- import TypeAdapter.PositionAdapter;
- import classes.Category;
- import classes.CpsEdge;
- import classes.CpsNode;
- import classes.AbstractCpsObject;
- import classes.HolonElement;
- import classes.HolonObject;
- import classes.HolonSwitch;
- import classes.HolonTransformer;
- import classes.IdCounter;
- import classes.Position;
- import ui.model.Model;
- /**
- * Controller for the Loading.
- *
- * @author Gruppe14
- */
- public class LoadController {
- /**
- * enum Mode.
- */
- public enum MODE {
- COMPLETE, PARTIAL
- }
- public enum TYPE {
- CATEGORY, CANVAS
- }
- public enum EDGETYPE {
- CANVAS, CONNECTION, NODE, OLD
- }
- public enum NUMTYPE {
- CATEGORY, OBJECT, ELEMENT, EDGE, CONNECTION, NODEEDGE, OLDEDGE, UNITGRAPH
- }
- public enum GRAPHTYPE {
- SWITCH, ELEMENT
- }
- private Model model;
- private CategoryController cgC;
- private CanvasController cvsC;
- private ObjectController objC;
- private MultiPurposeController mpC;
- private Gson gson;
- // uninitialized arrays only init when needed
- ArrayList<String> jCat, jObj, jEle, jEdge, jConn, jNodeEdge, jOldEdge, jUnitGraph;
- /**
- * Constructor.
- *
- * @param model
- * Model
- * @param cg
- * CategoryController
- * @param cvs
- * CanvasController
- * @param obj
- * ObjectController
- * @param mp
- * MultiPurposeController
- */
- public LoadController(Model model, CategoryController cg, CanvasController cvs, ObjectController obj, MultiPurposeController mp) {
- this.model = model;
- this.cgC = cg;
- this.cvsC = cvs;
- this.objC = obj;
- this.mpC = mp;
- initGson();
- }
- /**
- * Reads the the JSON File and load the state into the Model.
- *
- * @param path
- * the Path
- * @throws IOException
- * exception
- */
- public void readJson(String path) throws IOException {
- JsonParser parser = new JsonParser();
- JsonObject json = (JsonObject) parser.parse(new FileReader(path));
- // get all keys via stream
- List<String> keys = json.entrySet().stream().map(i -> i.getKey())
- .collect(Collectors.toCollection(ArrayList::new));
- initialize(keys, json);
- distribute(keys, json, parser);
- // for (Object key : keys) {
- // if (key.equals("CG"))
- // readCategory((JSONArray) json.get((String) key));
- // else if (key.equals("ID"))
- // IdCounter.setCounter(Integer.parseInt(json.get(key.toString()).toString()));
- // // else if (key.equals("SIZEX"))
- // // global.setCanvasX(Integer.parseInt(json.get(key.toString()).toString()));
- // // else if (key.equals("SIZEY"))
- // // global.setCanvasY(Integer.parseInt(json.get(key.toString()).toString()));
- // else if (key.toString().contains("CGO") || key.toString().contains("CVSO"))
- // obj.add(key.toString());
- // else if (key.toString().contains("CGE") || key.toString().contains("CVSE"))
- // ele.add(key.toString());
- // else if (key.toString().contains("EDGE"))
- // edge.add(key.toString());
- // else if (key.toString().contains("CGGP") || key.toString().contains("CVSGP"))
- // gp.add(key.toString());
- //
- // }
- //
- // dispatch(obj, json);
- // dispatch(ele, json);
- // dispatch(edge, json);
- // dispatch(gp, json);
- }
- private void distribute(List<String> keys, JsonObject json, JsonParser parser) {
- // TODO Auto-generated method stub
- for (String key : keys) {
- if(key.contains("CATEGORY"))
- loadCategory(json.get(key));
- if(key.contains("CGOBJECT"))
- loadCategoryObject(TYPE.CATEGORY, json.get(key).getAsJsonObject());
- }
- }
- private void loadCategoryObject(TYPE type, JsonObject jsonElement) {
- // TODO Auto-generated method stub
- AbstractCpsObject temp = gson.fromJson(jsonElement., AbstractCpsObject.class);
- cgC.addObject(mpC.searchCat(temp.getSav()), temp);
- }
- private void loadCategory(JsonElement jsonElement) {
- // TODO Auto-generated method stub
- cgC.addCategory(new Category(jsonElement.getAsString()));
- }
- private void initialize(List<String> keys, JsonObject json) {
- switch (MODE.valueOf(json.get("MODE").getAsString())) {
- case COMPLETE:
- model.setCgIdx(new HashMap<String, Integer>());
- model.setCategories(new ArrayList<Category>());
- model.setCvsObjIdx(new HashMap<Integer, Integer>());
- model.setObjectsOnCanvas(new ArrayList<AbstractCpsObject>());
- model.setEdgesOnCanvas(new ArrayList<CpsEdge>());
- model.setCanvasX(json.get("CANVAS_SIZE_X").getAsInt());
- model.setCanvasX(json.get("CANVAS_SIZE_Y").getAsInt());
- break;
- case PARTIAL:
- model.setCvsObjIdx(new HashMap<Integer, Integer>());
- model.setObjectsOnCanvas(new ArrayList<AbstractCpsObject>());
- model.setEdgesOnCanvas(new ArrayList<CpsEdge>());
- break;
- default:
- break;
- }
- IdCounter.setCounter(json.get("IDCOUNTER").getAsInt());
- }
- /**
- * dispatch the Keys into the right processing.
- *
- * @param input
- * input
- * @param json
- * Json
- */
- public void dispatch(ArrayList<String> input, JSONObject json) {
- for (String str : input) {
- if (str.contains("CGO")) {
- readCategoryObject((JSONArray) json.get(str));
- } else if (str.contains("CVSO")) {
- readCanvasObject((JSONArray) json.get(str));
- } else if (str.contains("CGE") || str.contains("CVSE")) {
- readElement((JSONArray) json.get(str));
- } else if (str.contains("EDGE")) {
- readEdge((JSONArray) json.get(str));
- } else if (str.contains("CGGP") || str.contains("CVSGP")) {
- readElementGraph((JSONArray) json.get(str));
- }
- }
- }
- /**
- * Read all Categories from the JSON file.
- *
- * @param arr
- * JSONArray
- */
- public void readCategory(JSONArray arr) {
- Iterator<Object> i = arr.iterator();
- while (i.hasNext()) {
- cgC.addNewCategory(next(i));
- }
- }
- /**
- * Read all Objects in Category from the JSON File.
- *
- * @param arr
- * JSON Array
- */
- public void readCategoryObject(JSONArray arr) {
- Iterator<Object> i = arr.iterator();
- String type = next(i);
- if (type.equals("HolonObject")) {
- cgC.addNewHolonObject(mpC.searchCat(next(i)), next(i), new ArrayList<HolonElement>(), next(i));
- } else if (type.equals("HolonTransformer")) {
- cgC.addNewHolonTransformer(mpC.searchCat(next(i)), next(i), next(i));
- } else if (type.equals("HolonSwitch")) {
- cgC.addNewHolonSwitch(mpC.searchCat(next(i)), next(i), next(i));
- }
- }
- /**
- * Read all Objects in Canvas from JSON File.
- *
- * @param arr
- * JSON Array
- */
- public void readCanvasObject(JSONArray arr) {
- Iterator<Object> i = arr.iterator();
- AbstractCpsObject cps = null;
- String type = next(i);
- if (type.equals("HolonObject")) {
- cps = new HolonObject(next(i));
- } else if (type.equals("HolonTransformer")) {
- cps = new HolonTransformer(next(i));
- } else if (type.equals("HolonSwitch")) {
- cps = new HolonSwitch(next(i));
- } else if (type.equals("CpsNode")) {
- cps = new CpsNode(next(i));
- }
- cps.setName(next(i));
- cps.setID(Integer.parseInt(next(i)));
- cps.setImage(next(i));
- cps.setPosition(Integer.parseInt(next(i)), Integer.parseInt(next(i)));
- cvsC.addNewObject(cps);
- }
- /**
- * Read all Elements in Category and Canvas from JSON File.
- *
- * @param arr
- * JSON Array
- */
- public void readElement(JSONArray arr) {
- Iterator<Object> i = arr.iterator();
- String sav = next(i);
- String obj = next(i);
- HolonElement ele = new HolonElement(next(i), Integer.parseInt(next(i)), Float.parseFloat(next(i)));
- if (sav.equals("CVS")) {
- ele.setActive(convert(Integer.parseInt(next(i))));
- objC.addElementIntoCanvasObject((HolonObject) mpC.searchByID(Integer.parseInt(obj)), ele);
- } else
- objC.addElementIntoCategoryObject(sav, obj, ele);
- }
- /**
- * Read.
- *
- * @param arr
- * JSON Array
- */
- public void readEdge(JSONArray arr) {
- Iterator<Object> i = arr.iterator();
- CpsEdge edge = new CpsEdge(mpC.searchByID(Integer.parseInt(next(i))),
- mpC.searchByID(Integer.parseInt(next(i))));
- edge.setCapacity(Float.parseFloat(next(i)));
- edge.setFlow(Float.parseFloat(next(i)));
- cvsC.addEdgeOnCanvas(edge);
- }
- /**
- * Read the Element Graph.
- *
- * @param arr
- * JSON Array
- */
- public void readElementGraph(JSONArray arr) {
- Iterator<Object> i = arr.iterator();
- String sav = next(i);
- HolonElement ele;
- if (sav.equals("CVS")) {
- ele = mpC.searchEle((HolonObject) mpC.searchByID(Integer.parseInt(next(i))), next(i));
- while (i.hasNext())
- ele.getGraphPoints().add(new Point(Integer.parseInt(next(i)), Integer.parseInt(next(i))));
- } else {
- ele = mpC.searchEle((HolonObject) mpC.searchCatObj(mpC.searchCat(sav), next(i)), next(i));
- while (i.hasNext())
- ele.getGraphPoints().add(new Point(Integer.parseInt(next(i)), Integer.parseInt(next(i))));
- }
- }
- /**
- * Get the Next Element from an Iterator.
- *
- * @param i
- * Iterator
- * @return next Element from the Iterator
- */
- public String next(Iterator<Object> i) {
- return i.next().toString();
- }
- /**
- * converting saved Integers into Booleans.
- *
- * @param x
- * integer
- * @return boolean
- */
- public boolean convert(int x) {
- return x == 1 ? true : false;
- }
-
- /**
- * Initialize the Gson with wanted parameters
- */
- private void initGson() {
- // TODO Auto-generated method stub
- GsonBuilder builder = new GsonBuilder();
- builder.excludeFieldsWithoutExposeAnnotation().serializeNulls().setPrettyPrinting();
- builder.registerTypeAdapter(AbstractCpsObject.class, new AbstractCpsObjectAdapter());
- builder.registerTypeAdapter(Position.class, new PositionAdapter());
- builder.registerTypeAdapter(Color.class, new ColorAdapter());
- // use the builder and make a instance of the Gson
- this.gson = builder.create();
- }
-
- private void initArrays() {
-
- }
- }
|