Skip to content

Commit 162f3d7

Browse files
author
Jegors Čemisovs
committed
Updated README.md
1 parent 85c9731 commit 162f3d7

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

graph-shell/src/main/java/lv/id/jc/graph/cli/Commands.java

+17-10
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,22 @@
77
import lv.id.jc.algorithm.graph.SearchAlgorithm;
88
import org.jline.utils.AttributedString;
99
import org.jline.utils.AttributedStyle;
10-
import org.springframework.beans.factory.InitializingBean;
1110
import org.springframework.beans.factory.annotation.Value;
11+
import org.springframework.core.io.ClassPathResource;
1212
import org.springframework.shell.jline.PromptProvider;
1313
import org.springframework.shell.standard.ShellComponent;
1414
import org.springframework.shell.standard.ShellMethod;
1515

16+
import javax.annotation.PostConstruct;
1617
import javax.validation.ConstraintValidator;
1718
import javax.validation.ConstraintValidatorContext;
1819
import javax.validation.constraints.NotEmpty;
20+
import java.io.IOException;
1921
import java.util.List;
2022
import java.util.Map;
2123

2224
@ShellComponent
23-
public class Commands implements InitializingBean, PromptProvider, ConstraintValidator<Vertex, String> {
25+
public class Commands implements PromptProvider, ConstraintValidator<Vertex, String> {
2426
private final SearchAlgorithm<String> bfgAlgorithm = new BreadthFirstSearch<>();
2527
private final SearchAlgorithm<String> dijkstrasAlgorithm = new DijkstrasAlgorithm<>();
2628

@@ -29,10 +31,10 @@ public class Commands implements InitializingBean, PromptProvider, ConstraintVal
2931

3032
private Graph<String> graph;
3133

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);
3638
graph = Graph.of(schema);
3739
}
3840

@@ -46,22 +48,27 @@ public boolean isValid(String vertex, ConstraintValidatorContext context) {
4648
return graph.schema().containsKey(vertex);
4749
}
4850

49-
@ShellMethod("finds the shortest path by using Breadth First Search Algorithm")
51+
@ShellMethod("Find the shortest path by using Breadth First Search Algorithm.")
5052
public List<String> shortest(@Vertex String source, @Vertex String target) {
5153
return bfgAlgorithm.findPath(graph, source, target);
5254
}
5355

54-
@ShellMethod("finds the fastest path by using Dijkstra's Algorithm")
56+
@ShellMethod("Find the fastest path by using Dijkstra's Algorithm.")
5557
public List<String> fastest(@Vertex String source, @Vertex String target) {
5658
return dijkstrasAlgorithm.findPath(graph, source, target);
5759
}
5860

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.")
6067
public Map<String, Map<String, Number>> schema() {
6168
return graph.schema();
6269
}
6370

64-
@ShellMethod("prints distance for the path")
71+
@ShellMethod("Calculate the distance for the given path.")
6572
public double distance(@NotEmpty List<String> path) {
6673
return graph.getDistance(path);
6774
}

graph-shell/src/main/java/lv/id/jc/graph/cli/Vertex.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
@Retention(RetentionPolicy.RUNTIME)
1313
@Constraint(validatedBy = {Commands.class})
1414
public @interface Vertex {
15-
String message() default "this vertex was not found in the graph diagram";
15+
String message() default "must be present in the graph scheme";
1616

1717
Class<?>[] groups() default {};
1818

0 commit comments

Comments
 (0)