Projekt

Obecné

Profil

« Předchozí | Další » 

Revize a2dfd926

Přidáno uživatelem Martin Matas před téměř 6 roky(ů)

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

Zobrazit rozdíly:

sources/imiger-dot-converter/src/main/java/cz/zcu/kiv/imiger/plugin/dot/BaseGraphFactory.java
8 8
import java.util.*;
9 9

  
10 10
/**
11
 * Factory provides generic implementation for creating instance of {@link Graph} from
12
 * data retrieved using given instance of {@link BaseDOTLoader}.
13
 *
11 14
 * Date: 31.03.2019
12 15
 *
13 16
 * @author Martin Matas
14 17
 */
15 18
public abstract class BaseGraphFactory<V extends VertexDTO, E extends EdgeDTO> {
16 19

  
20
	/**
21
	 * List of vertex archetypes that must factory initialize.
22
	 */
17 23
	protected List<VertexArchetype> vertexArchetypes;
24

  
25
	/**
26
	 * List of edge archetypes that must factory initialize.
27
	 */
18 28
	protected List<EdgeArchetype> edgeArchetypes;
29

  
30
	/**
31
	 * List of defined attribute types retrieved from {@link BaseDOTLoader}.
32
	 */
19 33
	protected List<AttributeType> attributeTypes;
34

  
35
	/**
36
	 * List of defined vertices retrieved from {@link BaseDOTLoader}. {@link VertexDTO} is
37
	 * specific vertex created with DOTLoader.
38
	 */
20 39
	protected List<VertexDTO> vertices;
21
	protected List<EdgeDTO> edges;
22 40

  
41
	/**
42
	 * List of defined edges retrieved from {@link BaseDOTLoader}. {@link EdgeDTO} is
43
	 * specific vertex created with DOTLoader.
44
	 */
45
	protected List<EdgeDTO> edges;
23 46

  
47
	/**
48
	 * Constructor initialize data that can be retrieved from {@link BaseDOTLoader}, the rest of
49
	 * necessary attributes for creating graph must be initialized in children of this abstract class.
50
	 *
51
	 * @param dotLoader - instance of {@link BaseDOTLoader} which provides parsed data from DOT file
52
	 */
24 53
	protected BaseGraphFactory(BaseDOTLoader<V, E> dotLoader) {
25 54
		vertices = new ArrayList<>();
26 55
		edges = new ArrayList<>();
......
28 57
		loadData(dotLoader);
29 58
	}
30 59

  
60
	/**
61
	 * Must initialize necessary attributes for creating graph from data provided by {@link BaseDOTLoader}.
62
	 *
63
	 * @param dotLoader - instance of {@link BaseDOTLoader} which provides parsed data from DOT file
64
	 */
31 65
	protected abstract void loadData(BaseDOTLoader<V, E> dotLoader);
32 66

  
67
	/**
68
	 * Must create instance of {@link Graph} from created and retrieved data.
69
	 *
70
	 * @return - new graph created from prepared data
71
	 */
33 72
	public abstract Graph createGraph();
34 73

  
35 74
}
sources/imiger-dot-converter/src/main/java/cz/zcu/kiv/imiger/plugin/dot/DOT.java
8 8
import cz.zcu.kiv.imiger.spi.IModule;
9 9
import cz.zcu.kiv.imiger.vo.Graph;
10 10

  
11
/**
12
 * IModule implementation for DOT converter.
13
 */
11 14
public class DOT implements IModule {
15

  
16
    /**
17
     * Returns name of this module.
18
     *
19
     * @return - name of this module
20
     */
12 21
    @Override
13 22
    public String getModuleName() {
14 23
        return "DOT file";
15 24
    }
16 25

  
26
    /**
27
     * Retrieves DOT file which has to be converted to raw JSON that
28
     * IMiGEr support.
29
     *
30
     * @param stringToConvert String to be converted to raw JSON.
31
     * @return - raw JSON as string
32
     */
17 33
    @Override
18 34
    public String getRawJson(String stringToConvert) {
19 35
        BaseDOTLoader<VertexDTO, EdgeDTO> loader = new PaypalDOTLoader(stringToConvert);
sources/imiger-dot-converter/src/main/java/cz/zcu/kiv/imiger/plugin/dot/GraphFactory.java
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));

Také k dispozici: Unified diff