Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 90426a44

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

Simplify transformation between JSONs by removing graph.Graph class and use instead api.Graph

Zobrazit rozdíly:

sources/src/main/java/cz/zcu/kiv/offscreen/api/Graph.java
1 1
package cz.zcu.kiv.offscreen.api;
2 2

  
3
import java.util.List;
4
import java.util.Map;
5
import java.util.Set;
3
import java.util.*;
6 4

  
7 5
import org.apache.log4j.Logger;
8 6

  
......
26 24

  
27 25
    private Logger logger = Logger.getLogger(Graph.class);
28 26

  
29
    public Graph(cz.zcu.kiv.offscreen.graph.Graph graph) {
30
        logger.trace("ENTRY");
31
        this.vertices = graph.getVertices();
32
        this.edges = graph.getEdges();
33
        this.vertexArchetypes = graph.getVertexArchetypes();
34
        this.edgeArchetypes = graph.getEdgeArchetypes();
35
        this.attributeTypes = graph.getAttributeTypes();
36
        this.possibleEnumValues = graph.getPossibleEnumValues();
37
        this.groups = graph.getGraphState().getGroups();
38
        this.sideBar = graph.getGraphState().getSideBar();
39
        this.highlightedVertex = graph.getGraphState().getHighlightedVertex();
40
        this.highlightedEdge = graph.getGraphState().getHighlightedEdge();
41
        logger.trace("EXIT");
27
    public Graph() {
28
        vertices = new HashSet<>();
29
        edges = new LinkedList<>();
42 30
    }
43 31

  
44 32
    public List<Edge> getEdges() {
......
84 72
    public String getHighlightedEdge() {
85 73
        return highlightedEdge;
86 74
    }
75

  
76
    public void setVertexArchetypes(List<VertexArchetype> vertexArchetypes) {
77
        this.vertexArchetypes = vertexArchetypes;
78
    }
79

  
80
    public void setEdgeArchetypes(List<EdgeArchetype> edgeArchetypes) {
81
        this.edgeArchetypes = edgeArchetypes;
82
    }
83

  
84
    public void setAttributeTypes(List<AttributeType> attributeTypes) {
85
        this.attributeTypes = attributeTypes;
86
    }
87

  
88
    public void setPossibleEnumValues(Map<Integer, List<String>> possibleEnumValues) {
89
        this.possibleEnumValues = new HashMap<>();
90
        for (Integer index : possibleEnumValues.keySet()) {
91
            this.possibleEnumValues.put("" + index, possibleEnumValues.get(index));
92
        }
93
    }
94

  
95
    public void setGroups(List<Group> groups) {
96
        this.groups = groups;
97
    }
98

  
99
    public void setSideBar(List<SideBar> sideBar) {
100
        this.sideBar = sideBar;
101
    }
102

  
103
    public void setHighlightedVertex(String highlightedVertex) {
104
        this.highlightedVertex = highlightedVertex;
105
    }
106

  
107
    public void setHighlightedEdge(String highlightedEdge) {
108
        this.highlightedEdge = highlightedEdge;
109
    }
87 110
}
sources/src/main/java/cz/zcu/kiv/offscreen/graph/Graph.java
1
package cz.zcu.kiv.offscreen.graph;
2

  
3
import cz.zcu.kiv.offscreen.api.*;
4
import org.apache.log4j.Logger;
5

  
6
import java.util.*;
7

  
8
/**
9
 * Class represents graph which is loaded from input JSON file.
10
 */
11
public class Graph {
12

  
13
    private Set<Vertex> vertices;
14
    private List<Edge> edges;
15
    private List<VertexArchetype> vertexArchetypes;
16
    private List<EdgeArchetype> edgeArchetypes;
17
    private List<AttributeType> attributeTypes;
18
    private Map<String, List<String>> possibleEnumValues;
19
    private List<String> defaultGroupArchetypes;
20

  
21
    private GraphState graphState;
22

  
23
    private Logger logger = Logger.getLogger(Graph.class);
24

  
25
    public Graph() {
26
        logger.trace("ENTRY");
27
        this.vertices = new HashSet<>();
28
        this.edges = new LinkedList<>();
29
        logger.trace("EXIT");
30
    }
31

  
32
    public List<Edge> getEdges() {
33
        logger.trace("ENTRY");
34
        logger.trace("EXIT");
35
        return edges;
36
    }
37

  
38
    public List<VertexArchetype> getVertexArchetypes() {
39
        return vertexArchetypes;
40
    }
41

  
42
    public List<EdgeArchetype> getEdgeArchetypes() {
43
        return edgeArchetypes;
44
    }
45

  
46
    public void setVertexArchetypes(List<VertexArchetype> vertexArchetypes) {
47
        this.vertexArchetypes = vertexArchetypes;
48
    }
49

  
50
    public  void setEdgeArchetypes(List<EdgeArchetype> edgeArchetypes) {
51
        this.edgeArchetypes = edgeArchetypes;
52
    }
53

  
54
    public void setAttributeTypes(List<AttributeType> attributeTypes) {
55
        this.attributeTypes = attributeTypes;
56
    }
57

  
58
    public void setPossibleEnumValues(Map<Integer, List<String>> possibleEnumValues) {
59
        this.possibleEnumValues = new HashMap<>();
60
        for (Integer index : possibleEnumValues.keySet()) {
61
            this.possibleEnumValues.put("" + index, possibleEnumValues.get(index));
62
        }
63
    }
64

  
65
    public List<String> getDefaultGroupArchetypes() {
66
        return defaultGroupArchetypes;
67
    }
68

  
69
    public void setDefaultGroupArchetypes(List<String> defaultGroupArchetypes) {
70
        this.defaultGroupArchetypes = defaultGroupArchetypes;
71
    }
72

  
73
    public List<AttributeType> getAttributeTypes() {
74
        return attributeTypes;
75
    }
76

  
77
    public Map<String, List<String>> getPossibleEnumValues() {
78
        return possibleEnumValues;
79
    }
80

  
81
    public Set<Vertex> getVertices() {
82
        logger.trace("ENTRY");
83
        logger.trace("EXIT");
84
        return vertices;
85
    }
86

  
87
    public void addEdge(Edge edge) {
88
        logger.trace("ENTRY");
89
        this.edges.add(edge);
90
        logger.trace("EXIT");
91
    }
92

  
93
    public void addVertex(Vertex vertex) {
94
        logger.trace("ENTRY");
95
        this.vertices.add(vertex);
96
        logger.trace("EXIT");
97
    }
98

  
99
    public GraphState getGraphState() {
100
        return graphState;
101
    }
102

  
103
    public void setGraphState(GraphState graphState) {
104
        this.graphState = graphState;
105
    }
106
}
sources/src/main/java/cz/zcu/kiv/offscreen/graph/GraphManager.java
239 239
        }
240 240

  
241 241

  
242
        List<String> defaultGroupArchetypes = configLoader.loadGroupArchetypesStrings();
243
        graph.setDefaultGroupArchetypes(defaultGroupArchetypes);
244

  
245 242
        Set<VertexImpl> resultVertices = getVerticesByArchetypeFilter(filter);
246 243
        resultVertices = getVerticesByAttributeFilter(filter, resultVertices);
247 244
        List<EdgeImpl> resultEdges = getEdgesByArchetypeFilter(filter, resultVertices);
......
257 254
        graph.setAttributeTypes(attributeTypes);
258 255
        graph.setPossibleEnumValues(possibleEnumValues);
259 256

  
260
        graph.setGraphState(createGraphState(graph));
257
        List<String> defaultGroupArchetypes = configLoader.loadGroupArchetypesStrings();
258
        addDefaultGroups(graph, defaultGroupArchetypes);
261 259

  
262 260
        return graph;
263 261
    }
......
454 452
        int idCounter = 1;
455 453
        for (EdgeImpl edgeImpl : edgeSet.keySet()) {
456 454
            Edge edge = new Edge(idCounter++, edgeImpl.getFrom(), edgeImpl.getTo(), edgeImpl.getText(), edgeSet.get(edgeImpl));
457
            graph.addEdge(edge);
455
            graph.getEdges().add(edge);
458 456
        }
459 457
    }
460 458

  
......
488 486

  
489 487
            List<String[]> attributes = getAttributesAsArray(vertexImpl.getSortedAttributes());
490 488
            Vertex vertex = new Vertex(vertexImpl,attributes);
491
            graph.addVertex(vertex);
489
            graph.getVertices().add(vertex);
492 490
        }
493 491
    }
494 492

  
......
540 538
    }
541 539

  
542 540
    /**
543
     * Method is used for initialization of default graph status based on input graph.
544
     *
545
     * @param graph graph where are set vertices, defaultGroupArchetypes and vertexArchetypes.
546
     * @return instance of created GraphState
547
     */
548
    private GraphState createGraphState(Graph graph) {
549
        GraphState state = new GraphState();
550

  
551
        state.addGroupsAll(createDefaultGroups(graph));
552

  
553

  
554
        return state;
555
    }
556

  
557
    /**
558
     * Method create list of groups based on defaultGroupArchetypes whose are defined in graph. Vertices are taken
559
     * from graph too.
541
     * Method add list of groups whose are defined in defaultGroupArchetypes to graph.
542
     * Vertices are taken from graph.
560 543
     *
561
     * @param graph graph from which are groups created.
562
     * @return created list of groups or empty list.
544
     * @param graph graph to which are groups added.
545
     * @param defaultGroupArchetypes list of group archetypes
563 546
     */
564
    private Collection<Group> createDefaultGroups(Graph graph){
547
    private void addDefaultGroups(Graph graph, List<String> defaultGroupArchetypes){
565 548

  
566 549
        Map<Integer, Group> groups = new HashMap<>();
567 550
        int groupId = 1;
......
569 552
        // find index of vertex archetypes names
570 553
        int index = 0;
571 554
        for(VertexArchetype archetype : graph.getVertexArchetypes()){
572
            if(graph.getDefaultGroupArchetypes().contains(archetype.name)){
555
            if(defaultGroupArchetypes.contains(archetype.name)){
573 556
                groups.put(index, new Group(groupId++, archetype.name));
574 557
            }
575 558
            index++;
......
590 573
            }
591 574
        }
592 575

  
593
        return groupList;
576
        graph.setGroups(groupList);
594 577
    }
595 578
}
sources/src/main/java/cz/zcu/kiv/offscreen/graph/GraphState.java
1
package cz.zcu.kiv.offscreen.graph;
2

  
3
import cz.zcu.kiv.offscreen.api.Group;
4
import cz.zcu.kiv.offscreen.api.SideBar;
5

  
6
import java.util.ArrayList;
7
import java.util.Collection;
8
import java.util.List;
9

  
10
public class GraphState {
11

  
12
    private List<Group> groups = new ArrayList<>();
13
    private List<SideBar> sideBar = new ArrayList<>();
14
    private String highlightedVertex;
15
    private String highlightedEdge;
16

  
17

  
18
    public List<Group> getGroups() {
19
        return groups;
20
    }
21

  
22
    public List<SideBar> getSideBar() {
23
        return sideBar;
24
    }
25

  
26
    public String getHighlightedVertex() {
27
        return highlightedVertex;
28
    }
29

  
30
    public String getHighlightedEdge() {
31
        return highlightedEdge;
32
    }
33

  
34
    public void addGroup(Group group){
35
        this.groups.add(group);
36
    }
37

  
38
    public void addSideBar(SideBar sideBar){
39
        this.sideBar.add(sideBar);
40
    }
41

  
42
    public void addGroupsAll(Collection<Group> collection){
43
        this.groups.addAll(collection);
44
    }
45

  
46
    public void addSideBarsAll(Collection<SideBar> collection){
47
        this.sideBar.addAll(collection);
48
    }
49

  
50
    public void setHighlightedVertex(String highlightedVertex) {
51
        this.highlightedVertex = highlightedVertex;
52
    }
53

  
54
    public void setHighlightedEdge(String highlightedEdge) {
55
        this.highlightedEdge = highlightedEdge;
56
    }
57
}
sources/src/main/java/cz/zcu/kiv/offscreen/servlets/api/LoadGraphData.java
53 53

  
54 54
        if (!Strings.isNullOrEmpty(jsonToDisplay)) {
55 55
            GraphManager graphManager = new GraphJSONDataLoader(jsonToDisplay).LoadData();
56

  
56 57
            String configLocation = ConfigurationLoader.getConfigLocation(request.getServletContext());
57 58
            JSONConfigLoader configLoader = new JSONConfigLoader(graphManager, configLocation);
58
            cz.zcu.kiv.offscreen.graph.Graph graph = graphManager.createGraph(configLoader);
59
            Graph export = new Graph(graph);
60
            JSONObject json = JSONObject.fromObject(export);
59

  
60
            Graph graph = graphManager.createGraph(configLoader);
61
            JSONObject json = JSONObject.fromObject(graph);
61 62

  
62 63
            response.setStatus(HttpServletResponse.SC_OK);
63 64
            response.getWriter().write(json.toString());
sources/src/test/java/cz/zcu/kiv/imiger/tests/backend/GraphFilterTest.java
1 1
package cz.zcu.kiv.imiger.tests.backend;
2 2

  
3 3
import cz.zcu.kiv.offscreen.api.Edge;
4
import cz.zcu.kiv.offscreen.api.Graph;
4 5
import cz.zcu.kiv.offscreen.api.SubedgeInfo;
5 6
import cz.zcu.kiv.offscreen.api.Vertex;
6
import cz.zcu.kiv.offscreen.graph.Graph;
7 7
import cz.zcu.kiv.offscreen.graph.GraphManager;
8 8
import cz.zcu.kiv.offscreen.graph.loader.GraphJSONDataLoader;
9 9
import cz.zcu.kiv.offscreen.graph.loader.JSONConfigLoader;
......
12 12
import org.junit.jupiter.api.Test;
13 13

  
14 14
import java.io.File;
15
import java.io.IOException;
16 15
import java.util.ArrayList;
17 16
import java.util.List;
18 17

  

Také k dispozici: Unified diff