Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 80ed28fb

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

Alert with information about error in source file (#7268)

Zobrazit rozdíly:

sources/imiger-core/src/main/java/cz/zcu/kiv/offscreen/servlets/api/GetSessionDiagram.java
53 53
                if(optional.isPresent()){
54 54
                    rawJson = optional.get();
55 55
                } else {
56
                    response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
56
                    response.setStatus(HttpServletResponse.SC_NOT_ACCEPTABLE);
57 57
                    return;
58 58
                }
59 59
            }
sources/imiger-core/src/main/webapp/js/showGraphApp.js
263 263
					case 401:
264 264
						alert('You are not allowed to view the diagram.');
265 265
						break;
266
					case 406:
267
						alert('There was a problem in graph source file.');
268
						break;
266 269
					default:
267 270
						console.error(error);
268 271
						alert('Something went wrong. Check console for more details.');
sources/imiger-dot-converter/src/main/java/cz/zcu/kiv/imiger/plugin/dot/DOT.java
4 4
import cz.zcu.kiv.imiger.plugin.dot.dto.EdgeDTO;
5 5
import cz.zcu.kiv.imiger.plugin.dot.dto.VertexDTO;
6 6
import cz.zcu.kiv.imiger.plugin.dot.loader.BaseDOTLoader;
7
import cz.zcu.kiv.imiger.plugin.dot.loader.PaypalDOTLoader;
7
import cz.zcu.kiv.imiger.plugin.dot.loader.DigraphDOTLoader;
8 8
import cz.zcu.kiv.imiger.spi.IModule;
9 9
import cz.zcu.kiv.imiger.vo.Graph;
10 10

  
......
32 32
     */
33 33
    @Override
34 34
    public String getRawJson(String stringToConvert) {
35
        BaseDOTLoader<VertexDTO, EdgeDTO> loader = new PaypalDOTLoader(stringToConvert);
35
        BaseDOTLoader<VertexDTO, EdgeDTO> loader = new DigraphDOTLoader(stringToConvert);
36 36
        GraphFactory graphFactory = new GraphFactory(loader);
37 37
        Graph graph = graphFactory.createGraph();
38 38

  
39
        Gson gson = new Gson();
40
        return gson.toJson(graph, Graph.class);
39
        if(!graph.getVertices().isEmpty()) {
40
            Gson gson = new Gson();
41
            return gson.toJson(graph, Graph.class);
42
        }
43

  
44
        else {
45
            return "";
46
        }
41 47
    }
42 48
}
sources/imiger-dot-converter/src/main/java/cz/zcu/kiv/imiger/plugin/dot/loader/DigraphDOTLoader.java
1
package cz.zcu.kiv.imiger.plugin.dot.loader;
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;
7
import cz.zcu.kiv.imiger.plugin.dot.dto.EdgeDTO;
8
import cz.zcu.kiv.imiger.plugin.dot.dto.VertexDTO;
9
import cz.zcu.kiv.imiger.vo.AttributeDataType;
10
import cz.zcu.kiv.imiger.vo.AttributeType;
11
import org.apache.logging.log4j.LogManager;
12
import org.apache.logging.log4j.Logger;
13

  
14
import java.io.ByteArrayInputStream;
15
import java.util.*;
16

  
17
public class DigraphDOTLoader 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 HashSet<String> attributeNames;
25
    private final HashMap<GraphNode, Integer> remappedVertices;
26

  
27
    /**
28
     * DOT loader which uses Digraph library
29
     *
30
     * @param dotInput - Source DOT file
31
     */
32
    public DigraphDOTLoader(String dotInput) {
33
        super(dotInput);
34
        parser = null;
35

  
36
        try {
37
            parser = new GraphParser(new ByteArrayInputStream(super.dotInput.getBytes()));
38
        } catch (GraphParserException ex) {
39
            logger.warn("There was a mistake in input DOT file. Exception: " + ex.getMessage());
40
        }
41
        vertices = new ArrayList<>();
42
        edges = new ArrayList<>();
43
        attributeTypes = new HashSet<>();
44
        remappedVertices = new HashMap<>();
45
        attributeNames = new HashSet<>();
46

  
47
        if(parser != null) {
48
            processVertices();
49
            processEdges();
50
        }
51
    }
52

  
53
    /**
54
     * Returns list of vertices DTO
55
     * @return List of vertices DTO
56
     */
57
    @Override
58
    public List<VertexDTO> getVertices() {
59
        return vertices;
60
    }
61

  
62
    /**
63
     * Returns list of edges DTO
64
     * @return List of edges DTO
65
     */
66
    @Override
67
    public List<EdgeDTO> getEdges() {
68
        return edges;
69
    }
70

  
71
    /**
72
     * Returns list of attribute types
73
     * @return List of attribute types
74
     */
75
    @Override
76
    public Set<AttributeType> getAttributeTypes() {
77
        if(attributeTypes.size() == 0 && attributeNames.size() > 0) {
78
            for(String attr : attributeNames) {
79
                attributeTypes.add(new AttributeType(attr, AttributeDataType.STRING, ""));
80
            }
81
        }
82
        return attributeTypes;
83
    }
84

  
85
    /**
86
     * Method transforms vertices from GraphNode object to vertex DTO and stores attribute type.
87
     * It has to be called before processing edges.
88
     */
89
    private void processVertices() {
90
        int id = 0;
91

  
92
        for(GraphNode node : this.parser.getNodes().values()) {
93
            HashMap<String, String> attrs = new HashMap<>();
94

  
95
            if(!node.getAttributes().entrySet().isEmpty()) {
96
                for (Map.Entry<String, Object> entry : node.getAttributes().entrySet()) {
97
                    String key = entry.getKey().replaceAll("\\\\n|\\\\l|\"", "");
98
                    String value = entry.getValue().toString().replaceAll("\\\\n|\\\\l|\"", "");
99
                    attrs.put(key, value);
100
                    attributeNames.add(key);
101
                }
102
            }
103

  
104
            VertexDTO vertex = new VertexDTO(node.getId().replaceAll("\\\\n|\\\\l|\"", ""), id++, attrs);
105
            this.remappedVertices.put(node, vertex.getId());
106

  
107
            vertices.add(vertex);
108
        }
109
    }
110

  
111
    /**
112
     * Method transforms vertices from GraphNode object to vertex DTO and stores attribute type.
113
     * It has to be called after processing vertices.
114
     */
115
    private void processEdges() {
116
        int id = 0;
117

  
118
        for(GraphEdge edge : this.parser.getEdges().values()) {
119
            HashMap<String, String> attrs = new HashMap<>();
120

  
121
            if(!edge.getAttributes().entrySet().isEmpty()) {
122
                for (Map.Entry<String, Object> entry : edge.getAttributes().entrySet()) {
123
                    String key = entry.getKey().replaceAll("\\\\n|\\\\l|\"", "");
124
                    String value = entry.getValue().toString().replaceAll("\\\\n|\\\\l|\"", "");
125
                    attrs.put(key, value);
126
                    attributeNames.add(key);
127
                }
128
            }
129

  
130
            int from = remappedVertices.get(edge.getNode1());
131
            int to = remappedVertices.get(edge.getNode2());
132
            EdgeDTO edgeDTO = new EdgeDTO(edge.getId().replaceAll("\\\\n|\\\\l|\"", ""), from, to, id++, attrs);
133
            
134
            edges.add(edgeDTO);
135
        }
136
    }
137
}
sources/imiger-dot-converter/src/main/java/cz/zcu/kiv/imiger/plugin/dot/loader/PaypalDOTLoader.java
1
package cz.zcu.kiv.imiger.plugin.dot.loader;
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;
7
import cz.zcu.kiv.imiger.plugin.dot.dto.EdgeDTO;
8
import cz.zcu.kiv.imiger.plugin.dot.dto.VertexDTO;
9
import cz.zcu.kiv.imiger.vo.AttributeDataType;
10
import cz.zcu.kiv.imiger.vo.AttributeType;
11
import org.apache.logging.log4j.LogManager;
12
import org.apache.logging.log4j.Logger;
13

  
14
import java.io.ByteArrayInputStream;
15
import java.util.*;
16

  
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 HashSet<String> attributeNames;
25
    private final HashMap<GraphNode, Integer> remappedVertices;
26

  
27
    /**
28
     * DOT loader which uses Digraph library
29
     *
30
     * @param dotInput - Source DOT file
31
     */
32
    public PaypalDOTLoader(String dotInput) {
33
        super(dotInput);
34
        parser = null;
35

  
36
        try {
37
            parser = new GraphParser(new ByteArrayInputStream(super.dotInput.getBytes()));
38
        } catch (GraphParserException ex) {
39
            logger.warn("There was a mistake in input DOT file. Exception: " + ex.getMessage());
40
        }
41
        vertices = new ArrayList<>();
42
        edges = new ArrayList<>();
43
        attributeTypes = new HashSet<>();
44
        remappedVertices = new HashMap<>();
45
        attributeNames = new HashSet<>();
46

  
47
        if(parser != null) {
48
            processVertices();
49
            processEdges();
50
        }
51
    }
52

  
53
    /**
54
     * Returns list of vertices DTO
55
     * @return List of vertices DTO
56
     */
57
    @Override
58
    public List<VertexDTO> getVertices() {
59
        return vertices;
60
    }
61

  
62
    /**
63
     * Returns list of edges DTO
64
     * @return List of edges DTO
65
     */
66
    @Override
67
    public List<EdgeDTO> getEdges() {
68
        return edges;
69
    }
70

  
71
    /**
72
     * Returns list of attribute types
73
     * @return List of attribute types
74
     */
75
    @Override
76
    public Set<AttributeType> getAttributeTypes() {
77
        if(attributeTypes.size() == 0 && attributeNames.size() > 0) {
78
            for(String attr : attributeNames) {
79
                attributeTypes.add(new AttributeType(attr, AttributeDataType.STRING, ""));
80
            }
81
        }
82
        return attributeTypes;
83
    }
84

  
85
    /**
86
     * Method transforms vertices from GraphNode object to vertex DTO and stores attribute type.
87
     * It has to be called before processing edges.
88
     */
89
    private void processVertices() {
90
        int id = 0;
91

  
92
        for(GraphNode node : this.parser.getNodes().values()) {
93
            HashMap<String, String> attrs = new HashMap<>();
94

  
95
            if(!node.getAttributes().entrySet().isEmpty()) {
96
                for (Map.Entry<String, Object> entry : node.getAttributes().entrySet()) {
97
                    String key = entry.getKey().replaceAll("\\\\n|\\\\l|\"", "");
98
                    String value = entry.getValue().toString().replaceAll("\\\\n|\\\\l|\"", "");
99
                    attrs.put(key, value);
100
                    attributeNames.add(key);
101
                }
102
            }
103

  
104
            VertexDTO vertex = new VertexDTO(node.getId().replaceAll("\\\\n|\\\\l|\"", ""), id++, attrs);
105
            this.remappedVertices.put(node, vertex.getId());
106

  
107
            vertices.add(vertex);
108
        }
109
    }
110

  
111
    /**
112
     * Method transforms vertices from GraphNode object to vertex DTO and stores attribute type.
113
     * It has to be called after processing vertices.
114
     */
115
    private void processEdges() {
116
        int id = 0;
117

  
118
        for(GraphEdge edge : this.parser.getEdges().values()) {
119
            HashMap<String, String> attrs = new HashMap<>();
120

  
121
            if(!edge.getAttributes().entrySet().isEmpty()) {
122
                for (Map.Entry<String, Object> entry : edge.getAttributes().entrySet()) {
123
                    String key = entry.getKey().replaceAll("\\\\n|\\\\l|\"", "");
124
                    String value = entry.getValue().toString().replaceAll("\\\\n|\\\\l|\"", "");
125
                    attrs.put(key, value);
126
                    attributeNames.add(key);
127
                }
128
            }
129

  
130
            int from = remappedVertices.get(edge.getNode1());
131
            int to = remappedVertices.get(edge.getNode2());
132
            EdgeDTO edgeDTO = new EdgeDTO(edge.getId().replaceAll("\\\\n|\\\\l|\"", ""), from, to, id++, attrs);
133
            
134
            edges.add(edgeDTO);
135
        }
136
    }
137
}
sources/imiger-dot-converter/src/test/java/cz/zcu/kiv/imiger/plugin/dot/backend/DigraphDOTLoaderTest.java
1
package cz.zcu.kiv.imiger.plugin.dot.backend;
2

  
3
import cz.zcu.kiv.imiger.plugin.dot.dto.EdgeDTO;
4
import cz.zcu.kiv.imiger.plugin.dot.dto.VertexDTO;
5
import cz.zcu.kiv.imiger.plugin.dot.loader.BaseDOTLoader;
6
import cz.zcu.kiv.imiger.plugin.dot.loader.DigraphDOTLoader;
7
import cz.zcu.kiv.imiger.vo.AttributeType;
8
import org.junit.Before;
9
import org.junit.Test;
10
import org.apache.commons.io.*;
11

  
12
import java.io.IOException;
13
import java.util.List;
14
import java.util.Set;
15

  
16
import static org.junit.Assert.*;
17

  
18
public class DigraphDOTLoaderTest {
19

  
20
    BaseDOTLoader<VertexDTO, EdgeDTO> loader;
21

  
22
    @Before
23
    public void setup() {
24
        loader = null;
25
    }
26

  
27
    private String getFileAsString(String fileName) {
28
        try {
29
            return IOUtils.toString(getClass().getClassLoader().getResourceAsStream(fileName), "UTF-8");
30
        } catch (IOException e) {
31
            return null;
32
        }
33
    }
34

  
35
    @Test
36
    public void testOkGraph() {
37
        String input = getFileAsString("complete1.dot");
38

  
39
        loader = new DigraphDOTLoader(input);
40

  
41
        List<VertexDTO> vertices = loader.getVertices();
42
        List<EdgeDTO> edges = loader.getEdges();
43
        Set<AttributeType> attrs = loader.getAttributeTypes();
44

  
45
        assertEquals(13, vertices.size());
46
        assertEquals(12, edges.size());
47
        assertEquals(5, attrs.size());
48
    }
49

  
50
    @Test
51
    public void testIncompleteGraph() {
52
        String input = getFileAsString("incomplete1.dot");
53

  
54
        loader = new DigraphDOTLoader(input);
55

  
56
        List<VertexDTO> vertices = loader.getVertices();
57
        List<EdgeDTO> edges = loader.getEdges();
58
        Set<AttributeType> attrs = loader.getAttributeTypes();
59

  
60
        assertEquals(0, vertices.size());
61
        assertEquals(0, edges.size());
62
        assertEquals(0, attrs.size());
63
    }
64

  
65
    @Test
66
    public void missingVertexDefinitionTest() {
67
        String input = getFileAsString("incomplete2.dot");
68

  
69
        loader = new DigraphDOTLoader(input);
70

  
71
        List<VertexDTO> vertices = loader.getVertices();
72
        List<EdgeDTO> edges = loader.getEdges();
73
        Set<AttributeType> attrs = loader.getAttributeTypes();
74

  
75
        assertEquals(13, vertices.size());
76
        assertEquals(12, edges.size());
77
        assertEquals(5, attrs.size());
78
    }
79
}
sources/imiger-dot-converter/src/test/java/cz/zcu/kiv/imiger/plugin/dot/backend/PaypalDOTLoaderTest.java
1
package cz.zcu.kiv.imiger.plugin.dot.backend;
2

  
3
import cz.zcu.kiv.imiger.plugin.dot.dto.EdgeDTO;
4
import cz.zcu.kiv.imiger.plugin.dot.dto.VertexDTO;
5
import cz.zcu.kiv.imiger.plugin.dot.loader.BaseDOTLoader;
6
import cz.zcu.kiv.imiger.plugin.dot.loader.PaypalDOTLoader;
7
import cz.zcu.kiv.imiger.vo.AttributeType;
8
import org.junit.Before;
9
import org.junit.Test;
10
import org.apache.commons.io.*;
11

  
12
import java.io.IOException;
13
import java.util.List;
14
import java.util.Set;
15

  
16
import static org.junit.Assert.*;
17

  
18
public class PaypalDOTLoaderTest {
19

  
20
    BaseDOTLoader<VertexDTO, EdgeDTO> loader;
21

  
22
    @Before
23
    public void setup() {
24
        loader = null;
25
    }
26

  
27
    private String getFileAsString(String fileName) {
28
        try {
29
            return IOUtils.toString(getClass().getClassLoader().getResourceAsStream(fileName), "UTF-8");
30
        } catch (IOException e) {
31
            return null;
32
        }
33
    }
34

  
35
    @Test
36
    public void testOkGraph() {
37
        String input = getFileAsString("complete1.dot");
38

  
39
        loader = new PaypalDOTLoader(input);
40

  
41
        List<VertexDTO> vertices = loader.getVertices();
42
        List<EdgeDTO> edges = loader.getEdges();
43
        Set<AttributeType> attrs = loader.getAttributeTypes();
44

  
45
        assertEquals(13, vertices.size());
46
        assertEquals(12, edges.size());
47
        assertEquals(5, attrs.size());
48
    }
49

  
50
    @Test
51
    public void testIncompleteGraph() {
52
        String input = getFileAsString("incomplete1.dot");
53

  
54
        loader = new PaypalDOTLoader(input);
55

  
56
        List<VertexDTO> vertices = loader.getVertices();
57
        List<EdgeDTO> edges = loader.getEdges();
58
        Set<AttributeType> attrs = loader.getAttributeTypes();
59

  
60
        assertEquals(0, vertices.size());
61
        assertEquals(0, edges.size());
62
        assertEquals(0, attrs.size());
63
    }
64

  
65
    @Test
66
    public void missingVertexDefinitionTest() {
67
        String input = getFileAsString("incomplete2.dot");
68

  
69
        loader = new PaypalDOTLoader(input);
70

  
71
        List<VertexDTO> vertices = loader.getVertices();
72
        List<EdgeDTO> edges = loader.getEdges();
73
        Set<AttributeType> attrs = loader.getAttributeTypes();
74

  
75
        assertEquals(13, vertices.size());
76
        assertEquals(12, edges.size());
77
        assertEquals(5, attrs.size());
78
    }
79
}

Také k dispozici: Unified diff