Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 08592390

Přidáno uživatelem Tomáš Šimandl před více než 6 roky(ů)

Used gson library for input file parsing

- used gson instead of net.sf.json (performance improve)

Zobrazit rozdíly:

sources/src/main/java/cz/zcu/kiv/offscreen/api/AttributeDataType.java
24 24
        public String toString() {
25 25
            return "string";
26 26
        }
27
    };
28

  
29
    public static AttributeDataType getEnum(String value){
30
        switch (value) {
31
            case "number": return AttributeDataType.NUMBER;
32
            case "date": return AttributeDataType.DATE;
33
            case "enum": return AttributeDataType.ENUM;
34
            default: return AttributeDataType.STRING;
35
        }
27 36
    }
28 37
}
sources/src/main/java/cz/zcu/kiv/offscreen/graph/loader/GraphJSONDataLoader.java
1 1
package cz.zcu.kiv.offscreen.graph.loader;
2 2

  
3 3
import com.google.common.base.Strings;
4
import com.google.gson.JsonArray;
5
import com.google.gson.JsonElement;
6
import com.google.gson.JsonObject;
7
import com.google.gson.JsonParser;
4 8
import cz.zcu.kiv.offscreen.graph.Attribute;
5 9
import cz.zcu.kiv.offscreen.api.AttributeDataType;
6 10
import cz.zcu.kiv.offscreen.graph.EdgeArchetypeInfo;
7 11
import cz.zcu.kiv.offscreen.graph.GraphManager;
8
import net.sf.json.JSONArray;
9
import net.sf.json.JSONObject;
10 12
import org.apache.commons.io.FileUtils;
11 13

  
12 14
import java.io.File;
......
14 16
import java.math.BigDecimal;
15 17
import java.text.ParsePosition;
16 18
import java.text.SimpleDateFormat;
17
import java.util.ArrayList;
18
import java.util.HashMap;
19
import java.util.List;
20
import java.util.Map;
19
import java.util.*;
21 20

  
22 21
/**
23 22
 * Class loads json file with graph data.
......
65 64
        if(Strings.isNullOrEmpty(loadedJSON)) {
66 65
            return null;
67 66
        }
67
        JsonObject json = new JsonParser().parse(loadedJSON).getAsJsonObject();
68 68

  
69
        JSONObject json = JSONObject.fromObject(loadedJSON);
70

  
71
        JSONArray attributeTypes = getLoadedAttributeTypes(json);
69
        JsonArray attributeTypes = getLoadedAttributeTypes(json);
72 70

  
73 71
        loadVertexArchetypes(json);
74 72

  
......
87 85
     * @param json array of loaded graph
88 86
     * @return - Json array with the attribute types.
89 87
     */
90
    private JSONArray getLoadedAttributeTypes(JSONObject json) {
91
        JSONArray attributeTypes = json.getJSONArray("attributeTypes");
92
        for (int i = 0; i < attributeTypes.size(); i++) {
93
            JSONObject attributeType = attributeTypes.getJSONObject(i);
94
            String dataTypeString = attributeType.getString("dataType");
95
            AttributeDataType dataType;
96
            switch (dataTypeString) {
97
                case "number": dataType = AttributeDataType.NUMBER; break;
98
                case "date": dataType = AttributeDataType.DATE; break;
99
                case "enum": dataType = AttributeDataType.ENUM; break;
100
                default: dataType = AttributeDataType.STRING; break;
101
            }
102
            graphManager.addAttributeType(attributeType.getString("name"), dataType, attributeType.getString("text"));
88
    private JsonArray getLoadedAttributeTypes(JsonObject json) {
89

  
90
        JsonArray attributeTypes = json.getAsJsonArray("attributeTypes");
91
        for (JsonElement attributeTypeElement : attributeTypes) {
92

  
93
            JsonObject attributeType = attributeTypeElement.getAsJsonObject();
94
            String dataTypeString = attributeType.get("dataType").getAsString();
95

  
96
            AttributeDataType dataType = AttributeDataType.getEnum(dataTypeString);
97

  
98
            graphManager.addAttributeType(attributeType.get("name").getAsString(), dataType, attributeType.get("text").getAsString());
103 99
        }
104 100

  
105 101
        return attributeTypes;
......
109 105
     * Loads the vertex archetypes from the json and adds them to all vertex archetypes.
110 106
     * @param json array of loaded graph
111 107
     */
112
    private void loadVertexArchetypes(JSONObject json) {
113
        JSONArray vertexArchetypes = json.getJSONArray("vertexArchetypes");
114
        for (int i = 0; i < vertexArchetypes.size(); i++) {
115
            JSONObject vertexArchetype = vertexArchetypes.getJSONObject(i);
108
    private void loadVertexArchetypes(JsonObject json) {
109
        JsonArray vertexArchetypes = json.getAsJsonArray("vertexArchetypes");
110

  
111
        for (JsonElement vertexArchetypeElement : vertexArchetypes){
116 112

  
117
            graphManager.addVertexArchetype(vertexArchetype.getString("name"), vertexArchetype.getString("text"));
113
            JsonObject vertexArchetype = vertexArchetypeElement.getAsJsonObject();
114
            graphManager.addVertexArchetype(vertexArchetype.get("name").getAsString(), vertexArchetype.get("text").getAsString());
118 115
        }
119 116
    }
120 117

  
......
122 119
     * Loads the edge archetypes from the json and adds them to all edge archetypes.
123 120
     * @param json array of loaded graph
124 121
     */
125
    private void loadEdgeArchetypes(JSONObject json) {
126
        JSONArray edgeArchetypes = json.getJSONArray("edgeArchetypes");
127
        for (int i = 0; i < edgeArchetypes.size(); i++) {
128
            JSONObject edgeArchetype = edgeArchetypes.getJSONObject(i);
122
    private void loadEdgeArchetypes(JsonObject json) {
123
        JsonArray edgeArchetypes = json.getAsJsonArray("edgeArchetypes");
124
        for (JsonElement edgeArchetypeElement : edgeArchetypes) {
125
            JsonObject edgeArchetype = edgeArchetypeElement.getAsJsonObject();
129 126

  
130
            graphManager.addEdgeArchetype(edgeArchetype.getString("name"), edgeArchetype.getString("text"));
127
            graphManager.addEdgeArchetype(edgeArchetype.get("name").getAsString(), edgeArchetype.get("text").getAsString());
131 128
        }
132 129
    }
133 130

  
......
136 133
     * @param attributeTypes - json array with the attribute types
137 134
     * @param json array of loaded graph
138 135
     */
139
    private void loadVertices(JSONArray attributeTypes, JSONObject json) {
140
        JSONArray verticesObject = json.getJSONArray("vertices");
141
        for (int i = 0; i < verticesObject.size(); i++) {
142
            JSONObject vertexObject = verticesObject.getJSONObject(i);
143
            String name = vertexObject.getString("title");
144
            String text = vertexObject.getString("text");
145
            int archetypeIndex = vertexObject.getInt("archetype");
146
            int vertexId = vertexObject.getInt("id");
136
    private void loadVertices(JsonArray attributeTypes, JsonObject json) {
137
        JsonArray verticesObject = json.getAsJsonArray("vertices");
138
        for (JsonElement vertexElement : verticesObject) {
139
            JsonObject vertexObject = vertexElement.getAsJsonObject();
140

  
141
            String name = vertexObject.get("title").getAsString();
142
            String text = vertexObject.get("text").getAsString();
143
            int archetypeIndex = vertexObject.get("archetype").getAsShort();
144
            int vertexId = vertexObject.get("id").getAsInt();
145

  
147 146
            vertexArchetypes.put(vertexId, archetypeIndex);
148 147

  
149
            JSONObject attributesObject = vertexObject.getJSONObject("attributes");
148
            JsonObject attributesObject = vertexObject.getAsJsonObject("attributes");
150 149
            Map<Integer, Attribute> attributes = loadObjectAttributes(attributesObject, attributeTypes);
151 150

  
152 151
            graphManager.addVertex(vertexId, name, text, archetypeIndex, attributes);
......
158 157
     * @param attributeTypes - List of attributes
159 158
     * @param json array of loaded graph
160 159
     */
161
    private void loadEdges(JSONArray attributeTypes, JSONObject json) {
162
        JSONArray edgesObject = json.getJSONArray("edges");
163
        for (int i = 0; i < edgesObject.size(); i++) {
164
            JSONObject edgeObject = edgesObject.getJSONObject(i);
165
            int edgeId = edgeObject.getInt("id");
166
            int from = edgeObject.getInt("from");
167
            int to = edgeObject.getInt("to");
168
            String text = edgeObject.getString("text");
169
            int archetypeIndex = edgeObject.getInt("archetype");
160
    private void loadEdges(JsonArray attributeTypes, JsonObject json) {
161
        JsonArray edgesObject = json.getAsJsonArray("edges");
162

  
163
        for (JsonElement edgeElement : edgesObject) {
164
            JsonObject edgeObject = edgeElement.getAsJsonObject();
165

  
166
            int edgeId = edgeObject.get("id").getAsInt();
167
            int from = edgeObject.get("from").getAsInt();
168
            int to = edgeObject.get("to").getAsInt();
169
            String text = edgeObject.get("text").getAsString();
170
            int archetypeIndex = edgeObject.get("archetype").getAsInt();
171

  
170 172
            int fromArchetypeIndex = vertexArchetypes.get(from);
171 173
            int toArchetypeIndex = vertexArchetypes.get(to);
172 174

  
173
            JSONObject attributesObject = edgeObject.getJSONObject("attributes");
175
            JsonObject attributesObject = edgeObject.getAsJsonObject("attributes");
174 176
            Map<Integer, Attribute> attributes = loadObjectAttributes(attributesObject, attributeTypes);
175 177

  
176 178
            EdgeArchetypeInfo archetypeInfo = new EdgeArchetypeInfo(fromArchetypeIndex, archetypeIndex, toArchetypeIndex);
......
184 186
     * @param attributeTypes - All types of attributes
185 187
     * @return Map of attributes for the json object
186 188
     */
187
    private Map<Integer, Attribute> loadObjectAttributes(JSONObject attributesObject, JSONArray attributeTypes) {
189
    private Map<Integer, Attribute> loadObjectAttributes(JsonObject attributesObject, JsonArray attributeTypes) {
188 190
        Map<Integer, Attribute> attributes = new HashMap<>();
189
        for (Object attributeKey : attributesObject.keySet()) {
190
            Object attrValue = loadAttributeFromObject((String)attributeKey, attributeTypes, attributesObject);
191 191

  
192
            int attributeTypeIndex = Integer.parseInt((String)attributeKey);
192
        for (Map.Entry<String, JsonElement> attribute : attributesObject.entrySet()) {
193
            Object attrValue = loadAttributeFromObject(attribute, attributeTypes);
194

  
195
            int attributeTypeIndex = Integer.parseInt(attribute.getKey());
193 196
            attributes.put(attributeTypeIndex, new Attribute(attributeTypeIndex, attrValue));
194 197
        }
195 198

  
......
198 201

  
199 202
    /**
200 203
     * Loads single attribute from the json object
201
     * @param attributeIdString - Attribute id as key
204
     * @param attribute - Attribute
202 205
     * @param attributeTypes - All the attributes types
203
     * @param attributesObject - Object containing all the attributes for the current object
204 206
     * @return Value of the attribute
205 207
     */
206
    private Object loadAttributeFromObject(String attributeIdString, JSONArray attributeTypes, JSONObject attributesObject) {
207
        int attributeId = Integer.parseInt(attributeIdString);
208
        String attrType = attributeTypes.getJSONObject(attributeId).getString("dataType");
208
    private Object loadAttributeFromObject(Map.Entry<String, JsonElement> attribute, JsonArray attributeTypes) {
209

  
210
        int attributeId = Integer.parseInt(attribute.getKey());
211
        String attrType = attributeTypes.get(attributeId).getAsJsonObject().get("dataType").getAsString();
209 212
        Object attrValue;
210 213
        switch (attrType) {
211 214
            case "number":
212
                String val = attributesObject.getString(attributeIdString);
215
                String val = attribute.getValue().getAsString();
213 216
                attrValue = new BigDecimal(val);
214 217
                break;
215 218
            case "date":
216
                String dateStr = attributesObject.getString(attributeIdString);
219
                String dateStr = attribute.getValue().getAsString();
217 220
                SimpleDateFormat format = new SimpleDateFormat(GraphJSONDataLoader.DATETIME_FORMAT);
218 221
                attrValue = format.parse(dateStr, new ParsePosition(0));
219 222
                break;
220 223
            case "enum":
221 224
                List<String> enumValues = new ArrayList<>();
222
                JSONArray enumValuesArray = attributesObject.optJSONArray(attributeIdString);
223
                if (enumValuesArray == null) {
224
                    enumValues.add(attributesObject.getString(attributeIdString)); // Only one value present
225
                } else {
226
                    for (int j = 0; j < enumValuesArray.size(); j++) {
227
                        enumValues.add(enumValuesArray.getString(j));
225

  
226
                if (attribute.getValue().isJsonArray()) {
227

  
228
                    JsonArray enumValuesArray = attribute.getValue().getAsJsonArray();
229
                    for (JsonElement element : enumValuesArray) {
230
                            enumValues.add(element.isJsonNull() ? "" : element.getAsString());
228 231
                    }
232

  
233
                } else {
234
                    enumValues.add(attribute.getValue().getAsString()); // Only one value present
229 235
                }
230 236

  
231 237
                graphManager.addUniquePossibleAttributeValues(attributeId, enumValues);
......
233 239

  
234 240
                break;
235 241
            default:
236
                attrValue = attributesObject.getString(attributeIdString);
242
                StringBuilder elements = new StringBuilder();
243
                if (attribute.getValue().isJsonArray()){
244

  
245
                    for (JsonElement element : attribute.getValue().getAsJsonArray()){
246
                        if (elements.length() != 0) {
247
                            elements.append(", ");
248
                        }
249
                        elements.append(element.getAsString());
250
                    }
251
                    attrValue = elements.toString();
252

  
253
                } else {
254
                    attrValue = attribute.getValue().getAsString();
255
                }
237 256
                break;
238 257
        }
239 258

  

Také k dispozici: Unified diff