Projekt

Obecné

Profil

« Předchozí | Další » 

Revize f411c15b

Přidáno uživatelem Vít Mazín před téměř 6 roky(ů)

DOT loader which gives DTO objects for vertices and edges etc.

Zobrazit rozdíly:

sources/imiger-dot-converter/src/main/java/cz/zcu/kiv/imiger/plugin/dot/loader/BaseDOTLoader.java
4 4
import cz.zcu.kiv.imiger.plugin.dot.dto.VertexDTO;
5 5
import cz.zcu.kiv.imiger.vo.AttributeType;
6 6

  
7
import java.util.HashSet;
8 7
import java.util.List;
8
import java.util.Set;
9 9

  
10 10
public abstract class BaseDOTLoader<V extends VertexDTO, E extends EdgeDTO> {
11 11

  
12
    private final String dotInput;
12
    protected final String dotInput;
13 13

  
14 14
    public BaseDOTLoader(String dotInput) {
15 15
        this.dotInput = dotInput;
16 16
    }
17 17

  
18
    public abstract List<V> getVerticies();
18
    public abstract List<V> getVertices();
19 19

  
20 20
    public abstract List<E> getEdges();
21 21

  
22
    public abstract HashSet<AttributeType> getAttributeTypes();
22
    public abstract Set<AttributeType> getAttributeTypes();
23 23
}
sources/imiger-dot-converter/src/main/java/cz/zcu/kiv/imiger/plugin/dot/loader/PaypalDOTLoader.java
1 1
package cz.zcu.kiv.imiger.plugin.dot.loader;
2 2

  
3
import com.paypal.digraph.parser.GraphEdge;
4
import com.paypal.digraph.parser.GraphNode;
5
import com.paypal.digraph.parser.GraphParser;
6
import com.paypal.digraph.parser.GraphParserException;
3 7
import cz.zcu.kiv.imiger.plugin.dot.dto.EdgeDTO;
4 8
import cz.zcu.kiv.imiger.plugin.dot.dto.VertexDTO;
9
import cz.zcu.kiv.imiger.vo.AttributeDataType;
5 10
import cz.zcu.kiv.imiger.vo.AttributeType;
11
import org.apache.logging.log4j.LogManager;
12
import org.apache.logging.log4j.Logger;
6 13

  
7
import java.util.HashSet;
8
import java.util.List;
14
import java.io.ByteArrayInputStream;
15
import java.util.*;
9 16

  
10 17
public class PaypalDOTLoader extends BaseDOTLoader<VertexDTO, EdgeDTO> {
18
    private static Logger logger = LogManager.getLogger();
19

  
20
    private GraphParser parser;
21
    private final List<VertexDTO> vertices;
22
    private final List<EdgeDTO> edges;
23
    private final HashSet<AttributeType> attributeTypes;
24
    private final HashMap<GraphNode, Integer> remappedVertices;
11 25

  
12 26
    public PaypalDOTLoader(String dotInput) {
13 27
        super(dotInput);
28
        parser = null;
29

  
30
        try {
31
            parser = new GraphParser(new ByteArrayInputStream(super.dotInput.getBytes()));
32
        } catch (GraphParserException ex) {
33
            logger.warn("There was a mistake in input DOT file. Exception: " + ex.getMessage());
34
        }
35
        vertices = new ArrayList<>();
36
        edges = new ArrayList<>();
37
        attributeTypes = new HashSet<>();
38
        remappedVertices = new HashMap<>();
39
        processEdges();
40
        processVertices();
14 41
    }
15 42

  
16 43
    @Override
17
    public List<VertexDTO> getVerticies() {
18
        return null;
44
    public List<VertexDTO> getVertices() {
45
        return vertices;
19 46
    }
20 47

  
21 48
    @Override
22 49
    public List<EdgeDTO> getEdges() {
23
        return null;
50
        return edges;
24 51
    }
25 52

  
26 53
    @Override
27
    public HashSet<AttributeType> getAttributeTypes() {
28
        return null;
54
    public Set<AttributeType> getAttributeTypes() {
55
        return attributeTypes;
56
    }
57

  
58
    private void processVertices() {
59
        int id = 0;
60

  
61
        for(GraphNode node : this.parser.getNodes().values()) {
62
            HashMap<String, String> attrs = new HashMap<>();
63
            for(Map.Entry<String, Object> entry : node.getAttributes().entrySet()) {
64
                attrs.put(entry.getKey(), entry.getValue().toString());
65
                attributeTypes.add(new AttributeType(entry.getKey(), AttributeDataType.STRING, ""));
66
            }
67

  
68
            VertexDTO vertex = new VertexDTO(node.getId(), id++, attrs);
69
            this.remappedVertices.put(node, vertex.getId());
70

  
71
            vertices.add(vertex);
72
        }
73
    }
74

  
75
    private void processEdges() {
76
        int id = 0;
77

  
78
        for(GraphEdge edge : this.parser.getEdges().values()) {
79
            HashMap<String, String> attrs = new HashMap<>();
80
            for(Map.Entry<String, Object> entry : edge.getAttributes().entrySet()) {
81
                attrs.put(entry.getKey(), entry.getValue().toString());
82
                attributeTypes.add(new AttributeType(entry.getKey(), AttributeDataType.STRING, ""));
83
            }
84

  
85
            int from = remappedVertices.get(edge.getNode1());
86
            int to = remappedVertices.get(edge.getNode2());
87
            EdgeDTO edgeDTO = new EdgeDTO(edge.getId(), from, to, id++, attrs);
88

  
89
            edges.add(edgeDTO);
90
        }
29 91
    }
30 92
}

Také k dispozici: Unified diff