Skip to content

Commit 026fa95

Browse files
author
Jegors Čemisovs
committed
Add simple application
1 parent 074d8b5 commit 026fa95

File tree

11 files changed

+133
-1
lines changed

11 files changed

+133
-1
lines changed

.idea/gradle.xml

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sample-java/build.gradle

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ java {
66
sourceCompatibility = JavaVersion.VERSION_17
77
}
88

9+
repositories {
10+
mavenCentral()
11+
}
12+
913
dependencies {
1014
implementation project(':algorithm')
1115
}

sample-yaml/build.gradle

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
plugins {
2+
id 'java'
3+
id 'org.springframework.boot' version '2.6.3'
4+
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
5+
}
6+
7+
java {
8+
sourceCompatibility = JavaVersion.VERSION_17
9+
}
10+
11+
repositories {
12+
mavenCentral()
13+
}
14+
15+
dependencies {
16+
implementation project(':algorithm')
17+
implementation 'com.fasterxml.jackson.core:jackson-databind:2.13.1'
18+
implementation 'com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.13.1'
19+
implementation 'org.springframework.shell:spring-shell-starter:2.0.1.RELEASE'
20+
}
21+
22+
group 'lv.id.jc'
23+
version '1.0-SNAPSHOT'
24+
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import com.fasterxml.jackson.dataformat.yaml.YAMLMapper;
2+
import lv.id.jc.algorithm.graph.BreadthFirstSearch;
3+
import lv.id.jc.algorithm.graph.DijkstrasAlgorithm;
4+
import lv.id.jc.algorithm.graph.Graph;
5+
6+
import java.io.IOException;
7+
import java.util.Map;
8+
import java.util.Scanner;
9+
10+
public class GraphCli {
11+
public static void main(String[] args) throws IOException {
12+
var in = GraphCli.class.getClassLoader().getResourceAsStream("complex.yaml");
13+
var schema = new YAMLMapper().readValue(in, Map.class);
14+
var graph = Graph.of(schema);
15+
16+
var scanner = new Scanner(System.in);
17+
18+
System.out.print("Source node: ");
19+
var source = scanner.next().toUpperCase();
20+
21+
System.out.print("Target node:");
22+
var target = scanner.next().toUpperCase();
23+
24+
var shortest = new BreadthFirstSearch<>().findPath(graph, source, target);
25+
var fastest = new DijkstrasAlgorithm<>().findPath(graph, source, target);
26+
27+
System.out.println("The shortest path: " + shortest);
28+
System.out.println("The fastest path: " + fastest);
29+
}
30+
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package lv.id.jc.sample;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class GraphShell {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(GraphShell.class, args);
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
spring.main.allow-circular-references=true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
A: { B: 5, H: 2 }
2+
B: { A: 5, C: 7 }
3+
C: { B: 7, D: 3, G: 4 }
4+
D: { C: 20, E: 4 }
5+
E: { F: 5 }
6+
F: { G: 6 }
7+
G: { C: 4 }
8+
H: { G: 3 }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
A: { B: 5 }
2+
B: { A: 5, C: 10 }
3+
C: { B: 20, D: 5 }
4+
D: { E: 5 }
5+
E: { B: 5 }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
A: { B: 7, C: 2 }
2+
B: { A: 3, C: 5 }
3+
C: { A: 1, B: 3 }

settings.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
rootProject.name = 'algorithms'
2-
include 'algorithm', 'sample-java', 'sample-groovy'
2+
include 'algorithm', 'sample-java', 'sample-groovy', 'sample-yaml'

0 commit comments

Comments
 (0)