-
-
Notifications
You must be signed in to change notification settings - Fork 735
Add dot-dsl exercise #2834
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add dot-dsl exercise #2834
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Instructions append | ||
|
||
The DSL uses method chaining. | ||
You can learn more about method chaining [here](https://www.geeksforgeeks.org/method-chaining-in-java-with-examples/). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Instructions | ||
|
||
A [Domain Specific Language (DSL)][dsl] is a small language optimized for a specific domain. | ||
Since a DSL is targeted, it can greatly impact productivity/understanding by allowing the writer to declare _what_ they want rather than _how_. | ||
|
||
One problem area where they are applied are complex customizations/configurations. | ||
|
||
For example the [DOT language][dot-language] allows you to write a textual description of a graph which is then transformed into a picture by one of the [Graphviz][graphviz] tools (such as `dot`). | ||
A simple graph looks like this: | ||
|
||
graph { | ||
graph [bgcolor="yellow"] | ||
a [color="red"] | ||
b [color="blue"] | ||
a -- b [color="green"] | ||
} | ||
|
||
Putting this in a file `example.dot` and running `dot example.dot -T png -o example.png` creates an image `example.png` with red and blue circle connected by a green line on a yellow background. | ||
|
||
Write a Domain Specific Language similar to the Graphviz dot language. | ||
|
||
Our DSL is similar to the Graphviz dot language in that our DSL will be used to create graph data structures. | ||
However, unlike the DOT Language, our DSL will be an internal DSL for use only in our language. | ||
|
||
More information about the difference between internal and external DSLs can be found [here][fowler-dsl]. | ||
|
||
[dsl]: https://en.wikipedia.org/wiki/Domain-specific_language | ||
[dot-language]: https://en.wikipedia.org/wiki/DOT_(graph_description_language) | ||
[graphviz]: https://graphviz.org/ | ||
[fowler-dsl]: https://martinfowler.com/bliki/DomainSpecificLanguage.html |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"authors": [ | ||
"kahgoh" | ||
], | ||
"files": { | ||
"solution": [ | ||
"src/main/java/DotDsl.java" | ||
], | ||
"test": [ | ||
"src/test/java/DotDslTest.java" | ||
], | ||
"example": [ | ||
".meta/src/reference/java/DotDsl.java" | ||
] | ||
}, | ||
"blurb": "Write a Domain Specific Language similar to the Graphviz dot language.", | ||
"source": "Wikipedia", | ||
"source_url": "https://en.wikipedia.org/wiki/DOT_(graph_description_language)" | ||
} |
108 changes: 108 additions & 0 deletions
108
exercises/practice/dot-dsl/.meta/src/reference/java/DotDsl.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class DotDsl { | ||
private static class Element<T> { | ||
private final Map<String, String> attributes = new HashMap<>(); | ||
|
||
public Element() { | ||
} | ||
|
||
public Element(Map<String, String> attributes) { | ||
this.attributes.putAll(attributes); | ||
} | ||
|
||
public Map<String, String> getAttributes() { | ||
return Collections.unmodifiableMap(attributes); | ||
} | ||
} | ||
|
||
public static class Graph extends Element<Graph> { | ||
|
||
private final List<Node> nodes = new ArrayList<>(); | ||
|
||
private final List<Edge> edges = new ArrayList<>(); | ||
|
||
public Graph() { | ||
super(); | ||
} | ||
|
||
public Graph(Map<String, String> attributes) { | ||
super(attributes); | ||
} | ||
|
||
public Collection<Node> getNodes() { | ||
return Collections.unmodifiableList(nodes); | ||
} | ||
|
||
public Collection<Edge> getEdges() { | ||
return Collections.unmodifiableList(edges); | ||
} | ||
|
||
public Graph node(String name) { | ||
nodes.add(new Node(name)); | ||
return this; | ||
} | ||
|
||
public Graph node(String name, Map<String, String> attributes) { | ||
nodes.add(new Node(name, attributes)); | ||
return this; | ||
} | ||
|
||
public Graph edge(String start, String end) { | ||
edges.add(new Edge(start, end)); | ||
return this; | ||
} | ||
|
||
public Graph edge(String start, String end, Map<String, String> attributes) { | ||
edges.add(new Edge(start, end, attributes)); | ||
return this; | ||
} | ||
} | ||
|
||
public static class Node extends Element<Node> { | ||
private final String name; | ||
|
||
public Node(String name) { | ||
this.name = name; | ||
} | ||
|
||
public Node(String name, Map<String, String> attributes) { | ||
super(attributes); | ||
this.name = name; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
} | ||
|
||
public static class Edge extends Element<Edge> { | ||
private final String start; | ||
|
||
private final String end; | ||
|
||
public Edge(String start, String end) { | ||
this.start = start; | ||
this.end = end; | ||
} | ||
|
||
public Edge(String start, String end, Map<String, String> attributes) { | ||
super(attributes); | ||
this.start = start; | ||
this.end = end; | ||
} | ||
|
||
public String getStart() { | ||
return start; | ||
} | ||
|
||
public String getEnd() { | ||
return end; | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
plugins { | ||
id "java" | ||
} | ||
|
||
repositories { | ||
mavenCentral() | ||
} | ||
|
||
dependencies { | ||
testImplementation platform("org.junit:junit-bom:5.10.0") | ||
testImplementation "org.junit.jupiter:junit-jupiter" | ||
testImplementation "org.assertj:assertj-core:3.25.1" | ||
} | ||
|
||
test { | ||
useJUnitPlatform() | ||
|
||
testLogging { | ||
exceptionFormat = "full" | ||
showStandardStreams = true | ||
events = ["passed", "failed", "skipped"] | ||
} | ||
} |
Binary file not shown.
6 changes: 6 additions & 0 deletions
6
exercises/practice/dot-dsl/gradle/wrapper/gradle-wrapper.properties
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
distributionBase=GRADLE_USER_HOME | ||
distributionPath=wrapper/dists | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip | ||
validateDistributionUrl=true | ||
zipStoreBase=GRADLE_USER_HOME | ||
zipStorePath=wrapper/dists |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.