8 |
8 |
import java.util.*;
|
9 |
9 |
|
10 |
10 |
/**
|
|
11 |
* Implements necessary methods for creating graph.
|
|
12 |
*
|
11 |
13 |
* Date: 31.03.2019
|
12 |
14 |
*
|
13 |
15 |
* @author Martin Matas
|
14 |
16 |
*/
|
15 |
17 |
public class GraphFactory extends BaseGraphFactory<VertexDTO, EdgeDTO> {
|
16 |
18 |
|
|
19 |
/**
|
|
20 |
* Name of default edge archetype name field.
|
|
21 |
*/
|
17 |
22 |
private static final String EDGE_ARCHETYPE_NAME = "Edge";
|
|
23 |
|
|
24 |
/**
|
|
25 |
* Name of default vertex archetype name field.
|
|
26 |
*/
|
18 |
27 |
private static final String VERTEX_ARCHETYPE_NAME = "Vertex";
|
|
28 |
|
|
29 |
/**
|
|
30 |
* Value of default archetype text field.
|
|
31 |
*/
|
19 |
32 |
private static final String ARCHETYPE_TEXT = "";
|
|
33 |
|
|
34 |
/**
|
|
35 |
* Index of default archetype in created archetype list.
|
|
36 |
*/
|
20 |
37 |
private static final int DEFAULT_ARCHETYPE_INDEX = 0;
|
21 |
38 |
|
|
39 |
/**
|
|
40 |
* Retrieves data from {@link BaseDOTLoader} and defines archetypes for vertices and edges.
|
|
41 |
*
|
|
42 |
* @param dotLoader - instance of {@link BaseDOTLoader} which provides parsed data from DOT file
|
|
43 |
*/
|
22 |
44 |
public GraphFactory(BaseDOTLoader<VertexDTO,EdgeDTO> dotLoader) {
|
23 |
45 |
super(dotLoader);
|
24 |
46 |
prepareEdgeArchetypes();
|
25 |
47 |
prepareVertexArchetypes();
|
26 |
48 |
}
|
27 |
49 |
|
|
50 |
/**
|
|
51 |
* Initialize attributes that can be filled with data from {@link BaseDOTLoader}. In case when
|
|
52 |
* DOTLoader returns uninitialized lists, attributes will be initialized empty.
|
|
53 |
*
|
|
54 |
* @param dotLoader - instance of {@link BaseDOTLoader} which provides parsed data from DOT file
|
|
55 |
*/
|
28 |
56 |
@Override
|
29 |
57 |
protected void loadData(BaseDOTLoader<VertexDTO,EdgeDTO> dotLoader) {
|
30 |
58 |
List<VertexDTO> loadedVertices = dotLoader.getVertices();
|
... | ... | |
44 |
72 |
}
|
45 |
73 |
}
|
46 |
74 |
|
|
75 |
/**
|
|
76 |
* Creates new graph from data which were prepared when instance was created.
|
|
77 |
*
|
|
78 |
* @return - new instance of graph
|
|
79 |
*/
|
47 |
80 |
@Override
|
48 |
81 |
public Graph createGraph() {
|
49 |
82 |
Graph graph = new Graph();
|
... | ... | |
57 |
90 |
return graph;
|
58 |
91 |
}
|
59 |
92 |
|
|
93 |
/**
|
|
94 |
* Iterates through the list of vertices retrieved from {@link BaseDOTLoader} and for each of them
|
|
95 |
* creates new graph vertex.
|
|
96 |
*
|
|
97 |
* @param graph - reference to the instance of new graph
|
|
98 |
* @param vertices - vertices retrieved from {@link BaseDOTLoader}
|
|
99 |
*/
|
60 |
100 |
private void addVerticesToGraph(Graph graph, List<VertexDTO> vertices) {
|
61 |
101 |
for (VertexDTO v : vertices) {
|
62 |
102 |
|
63 |
|
List<String[]> attributes = sortAttributes(v.getAttributes(), attributeTypes);
|
|
103 |
List<String[]> attributes = checkAttributes(v.getAttributes(), attributeTypes);
|
64 |
104 |
Vertex vertex = new Vertex(v.getId(), v.getName(), DEFAULT_ARCHETYPE_INDEX, "", attributes);
|
65 |
105 |
graph.getVertices().add(vertex);
|
66 |
106 |
}
|
67 |
107 |
}
|
68 |
108 |
|
|
109 |
/**
|
|
110 |
* Iterates through the list of edges retrieved from {@link BaseDOTLoader} and for each of them
|
|
111 |
* creates new graph edge.
|
|
112 |
*
|
|
113 |
* @param graph - reference to the instance of new graph
|
|
114 |
* @param edges - edges retrieved from {@link BaseDOTLoader}
|
|
115 |
*/
|
69 |
116 |
private void addEdgesToGraph(Graph graph, List<EdgeDTO> edges) {
|
70 |
117 |
for (EdgeDTO e : edges) {
|
71 |
118 |
|
72 |
|
List<String[]> attributes = sortAttributes(e.getAttributes(), attributeTypes);
|
|
119 |
List<String[]> attributes = checkAttributes(e.getAttributes(), attributeTypes);
|
73 |
120 |
SubedgeInfo subedgeInfo = new SubedgeInfo(e.getId(), DEFAULT_ARCHETYPE_INDEX, attributes);
|
74 |
121 |
|
75 |
122 |
List<SubedgeInfo> subedgeInfos = new ArrayList<>();
|
... | ... | |
80 |
127 |
}
|
81 |
128 |
}
|
82 |
129 |
|
83 |
|
private List<String[]> sortAttributes(Map<String, String> unsortedAttributes, List<AttributeType> attributeTypes) {
|
84 |
|
HashMap<String, List<String>> attributes = new HashMap<>();
|
85 |
|
|
86 |
|
attributeTypes.forEach(type -> {
|
87 |
|
attributes.put(type.getName(), new ArrayList<>());
|
88 |
|
});
|
89 |
|
|
90 |
|
for (Map.Entry<String, String> e: unsortedAttributes.entrySet()) {
|
91 |
|
if (attributes.containsKey(e.getKey())) {
|
92 |
|
attributes.get(e.getKey()).add(e.getValue());
|
|
130 |
/**
|
|
131 |
* Iterates through the list of attributes and for each of them checks if the attribute type exists. Then for the
|
|
132 |
* attribute creates key-value pair using {@link String[]}, where key is the attribute type and value is the
|
|
133 |
* value of attribute.
|
|
134 |
*
|
|
135 |
* @param uncheckedAttributes - map of attributes retrieved from {@link BaseDOTLoader}, where key is unchecked
|
|
136 |
* attribute type and value is the value of attribute
|
|
137 |
* @param attributeTypes - list of attribute types retrieved from {@link BaseDOTLoader} against which will be unchecked
|
|
138 |
* attributes validated
|
|
139 |
* @return - list of checked attributes
|
|
140 |
*/
|
|
141 |
private List<String[]> checkAttributes(Map<String, String> uncheckedAttributes, List<AttributeType> attributeTypes) {
|
|
142 |
List<String[]> attributes = new ArrayList<>();
|
|
143 |
Set<String> attributesHashSet = new HashSet<>();
|
|
144 |
|
|
145 |
attributeTypes.forEach(t -> attributesHashSet.add(t.getName()));
|
|
146 |
|
|
147 |
for (Map.Entry<String, String> e: uncheckedAttributes.entrySet()) {
|
|
148 |
if (attributesHashSet.contains(e.getKey())) {
|
|
149 |
attributes.add(new String[]{e.getKey(), e.getValue()});
|
93 |
150 |
}
|
94 |
151 |
}
|
95 |
152 |
|
96 |
|
List<String[]> finalAttributes = new ArrayList<>();
|
97 |
|
attributes.forEach((k, v) -> {
|
98 |
|
finalAttributes.add(v.toArray(new String[0]));
|
99 |
|
});
|
100 |
|
|
101 |
|
return finalAttributes;
|
|
153 |
return attributes;
|
102 |
154 |
}
|
103 |
155 |
|
|
156 |
/**
|
|
157 |
* Defines default edge archetype for edges.
|
|
158 |
*/
|
104 |
159 |
private void prepareEdgeArchetypes() {
|
105 |
160 |
edgeArchetypes= new ArrayList<>();
|
106 |
161 |
edgeArchetypes.add(new EdgeArchetype(EDGE_ARCHETYPE_NAME, ARCHETYPE_TEXT));
|
107 |
162 |
}
|
108 |
163 |
|
|
164 |
/**
|
|
165 |
* Defines default vertex archetypes for vertices.
|
|
166 |
*/
|
109 |
167 |
private void prepareVertexArchetypes() {
|
110 |
168 |
vertexArchetypes = new ArrayList<>();
|
111 |
169 |
vertexArchetypes.add(new VertexArchetype(VERTEX_ARCHETYPE_NAME, ARCHETYPE_TEXT));
|
Fixed creation of attribute structure, documentation
- structure of attributes was adjusted to produces string array, where first value is type of attribute and the second value is the value of attribute
- documentation comments were added into implemented files