|
| 1 | +package lv.id.jc.sample; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.dataformat.yaml.YAMLMapper; |
| 4 | +import lv.id.jc.algorithm.graph.BreadthFirstSearch; |
| 5 | +import lv.id.jc.algorithm.graph.Graph; |
| 6 | +import lv.id.jc.algorithm.graph.SearchAlgorithm; |
| 7 | +import org.jline.utils.AttributedString; |
| 8 | +import org.jline.utils.AttributedStyle; |
| 9 | +import org.springframework.beans.factory.InitializingBean; |
| 10 | +import org.springframework.beans.factory.annotation.Value; |
| 11 | +import org.springframework.shell.jline.PromptProvider; |
| 12 | +import org.springframework.shell.standard.ShellComponent; |
| 13 | +import org.springframework.shell.standard.ShellMethod; |
| 14 | + |
| 15 | +import java.util.List; |
| 16 | +import java.util.Map; |
| 17 | + |
| 18 | +@ShellComponent |
| 19 | +public class Commands implements PromptProvider, InitializingBean { |
| 20 | + |
| 21 | + @Value("${graph.file:complex}") |
| 22 | + private String file; |
| 23 | + |
| 24 | + private Graph<String> graph; |
| 25 | + private SearchAlgorithm<String> bfgAlgorithm = new BreadthFirstSearch<>(); |
| 26 | + |
| 27 | + @Override |
| 28 | + public void afterPropertiesSet() throws Exception { |
| 29 | + var in = Commands.class.getClassLoader().getResourceAsStream(file + ".yaml"); |
| 30 | + var schema = new YAMLMapper().readValue(in, Map.class); |
| 31 | + graph = Graph.of(schema); |
| 32 | + } |
| 33 | + |
| 34 | + @ShellMethod("finds the shortest path by using Breadth First Search Algorithm") |
| 35 | + public List<String> shortest(String source, String target) { |
| 36 | + return bfgAlgorithm.findPath(graph, source, target); |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + public AttributedString getPrompt() { |
| 41 | + return new AttributedString(file + ":>", AttributedStyle.DEFAULT.foreground(AttributedStyle.YELLOW)); |
| 42 | + } |
| 43 | +} |
0 commit comments