7
7
import lv .id .jc .algorithm .graph .SearchAlgorithm ;
8
8
import org .jline .utils .AttributedString ;
9
9
import org .jline .utils .AttributedStyle ;
10
- import org .springframework .beans .factory .InitializingBean ;
11
10
import org .springframework .beans .factory .annotation .Value ;
11
+ import org .springframework .core .io .ClassPathResource ;
12
12
import org .springframework .shell .jline .PromptProvider ;
13
13
import org .springframework .shell .standard .ShellComponent ;
14
14
import org .springframework .shell .standard .ShellMethod ;
15
15
16
+ import javax .annotation .PostConstruct ;
16
17
import javax .validation .ConstraintValidator ;
17
18
import javax .validation .ConstraintValidatorContext ;
18
19
import javax .validation .constraints .NotEmpty ;
20
+ import java .io .IOException ;
19
21
import java .util .List ;
20
22
import java .util .Map ;
21
23
22
24
@ ShellComponent
23
- public class Commands implements InitializingBean , PromptProvider , ConstraintValidator <Vertex , String > {
25
+ public class Commands implements PromptProvider , ConstraintValidator <Vertex , String > {
24
26
private final SearchAlgorithm <String > bfgAlgorithm = new BreadthFirstSearch <>();
25
27
private final SearchAlgorithm <String > dijkstrasAlgorithm = new DijkstrasAlgorithm <>();
26
28
@@ -29,10 +31,10 @@ public class Commands implements InitializingBean, PromptProvider, ConstraintVal
29
31
30
32
private Graph <String > graph ;
31
33
32
- @ Override
33
- public void afterPropertiesSet () throws Exception {
34
- var in = Commands . class . getClassLoader (). getResourceAsStream (graphName + ".yaml" );
35
- var schema = new YAMLMapper ().readValue (in , Map .class );
34
+ @ PostConstruct
35
+ public void loadGraph () throws IOException {
36
+ var resource = new ClassPathResource (graphName + ".yaml" );
37
+ var schema = new YAMLMapper ().readValue (resource . getInputStream () , Map .class );
36
38
graph = Graph .of (schema );
37
39
}
38
40
@@ -46,22 +48,27 @@ public boolean isValid(String vertex, ConstraintValidatorContext context) {
46
48
return graph .schema ().containsKey (vertex );
47
49
}
48
50
49
- @ ShellMethod ("finds the shortest path by using Breadth First Search Algorithm" )
51
+ @ ShellMethod ("Find the shortest path by using Breadth First Search Algorithm. " )
50
52
public List <String > shortest (@ Vertex String source , @ Vertex String target ) {
51
53
return bfgAlgorithm .findPath (graph , source , target );
52
54
}
53
55
54
- @ ShellMethod ("finds the fastest path by using Dijkstra's Algorithm" )
56
+ @ ShellMethod ("Find the fastest path by using Dijkstra's Algorithm. " )
55
57
public List <String > fastest (@ Vertex String source , @ Vertex String target ) {
56
58
return dijkstrasAlgorithm .findPath (graph , source , target );
57
59
}
58
60
59
- @ ShellMethod ("prints schema of the graph" )
61
+ @ ShellMethod ("Print the edges of the given vertex." )
62
+ public Map <String , Number > edges (@ Vertex String vertex ) {
63
+ return graph .edges (vertex );
64
+ }
65
+
66
+ @ ShellMethod ("Print schema of the graph." )
60
67
public Map <String , Map <String , Number >> schema () {
61
68
return graph .schema ();
62
69
}
63
70
64
- @ ShellMethod ("prints distance for the path" )
71
+ @ ShellMethod ("Calculate the distance for the given path. " )
65
72
public double distance (@ NotEmpty List <String > path ) {
66
73
return graph .getDistance (path );
67
74
}
0 commit comments