Skip to content

Commit 4477088

Browse files
author
Tihomir Surdilovic
committed
Adding diagram module
Signed-off-by: Tihomir Surdilovic <[email protected]>
1 parent 48b15c3 commit 4477088

File tree

59 files changed

+3116
-2
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+3116
-2
lines changed

README.md

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ With the SDK you can:
99
* Parse workflow JSON and YAML definitions
1010
* Programmatically build workflow definitions
1111
* Validate workflow definitions (both schema and workflow integrity validation)
12+
* Generate workflow diagram (SVG)
1213

1314
Serverless Workflow Java SDK is **not** a workflow runtime implementation but can be used by Java runtime implementations
14-
to parse and validate workflow definitions.
15+
to parse and validate workflow definitions as well as generate the workflow diagram (SVG).
1516

1617
### Status
1718

@@ -62,6 +63,16 @@ Then to use it in your project pom.xml add:
6263
</dependency>
6364
```
6465

66+
* Diagram dependency
67+
68+
```xml
69+
<dependency>
70+
<groupId>io.serverlessworkflow</groupId>
71+
<artifactId>serverlessworkflow-diagram</artifactId>
72+
<version>0.2-SNAPSHOT</version>
73+
</dependency>
74+
```
75+
6576
#### Get dependencies from Nexus
6677

6778
Our SNAPSHOT versions are published to the Sonatype repositories.
@@ -107,6 +118,14 @@ And use the dependencies:
107118
</dependency>
108119
```
109120

121+
```xml
122+
<dependency>
123+
<groupId>io.serverlessworkflow</groupId>
124+
<artifactId>serverlessworkflow-diagram</artifactId>
125+
<version>0.2-SNAPSHOT</version>
126+
</dependency>
127+
```
128+
110129
### How to Use
111130

112131
#### Creating from JSON/YAML source
@@ -247,4 +266,38 @@ Workflow workflow = new Workflow().withId("test-workflow").withVersion("1.0")
247266

248267
WorkflowValidator workflowValidator = new WorkflowValidatorImpl();
249268
List<ValidationError> validationErrors = workflowValidator.setWorkflow(workflow).validate();
250-
```
269+
```
270+
271+
#### Building Workflow Diagram
272+
273+
Given a valid workflow source or a Workflow object you can build the workflow Diagram SVG.
274+
Diagrams are built using [PlantUML](https://plantuml.com/) and can be embedded inside your
275+
tooling or web pages, or any SVG viewer.
276+
277+
You can build the workflow diagram SVG the the following code:
278+
279+
``` java
280+
Workflow workflow = Workflow.fromSource(source);
281+
282+
WorkflowDiagram workflowDiagram = new WorkflowDiagramImpl();
283+
workflowDiagram.setWorkflow(workflow);
284+
285+
String diagramSVG = workflowDiagram.getSvgDiagram();
286+
```
287+
288+
`diagramSVG` includes the diagram SVG which you can then decide to save to a file,
289+
print, or process further.
290+
291+
Here are some generated diagrams from the specification examples:
292+
293+
1. [Job Monitoring Example](https://github.com/serverlessworkflow/specification/blob/master/examples/examples.md#Monitor-Job-Example)
294+
<p align="center">
295+
<img src="img/jobmonitoring.png" alt="Job Monitoring Example Diagram"/>
296+
</p>
297+
298+
299+
2. [Send CloudEvent on Workflow completion Example](https://github.com/serverlessworkflow/specification/blob/master/examples/examples.md#send-cloudevent-on-workfow-completion-example)
300+
<p align="center">
301+
<img src="img/provisionorders.png" alt="Send Cloud Event on Workflow complation"/>
302+
</p>
303+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright 2020-Present The Serverless Workflow Specification Authors
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
package io.serverlessworkflow.api.interfaces;
18+
19+
import io.serverlessworkflow.api.Workflow;
20+
21+
import java.io.File;
22+
23+
public interface WorkflowDiagram {
24+
WorkflowDiagram setWorkflow(Workflow workflow);
25+
26+
WorkflowDiagram setSource(String source);
27+
28+
String getSvgDiagram() throws Exception;
29+
}

diagram/.gitignore

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
HELP.md
2+
target/
3+
!.mvn/wrapper/maven-wrapper.jar
4+
!**/src/main/**
5+
!**/src/test/**
6+
7+
### STS ###
8+
.apt_generated
9+
.classpath
10+
.factorypath
11+
.project
12+
.settings
13+
.springBeans
14+
.sts4-cache
15+
16+
### IntelliJ IDEA ###
17+
.idea
18+
*.iws
19+
*.iml
20+
*.ipr
21+
22+
### NetBeans ###
23+
/nbproject/private/
24+
/nbbuild/
25+
/dist/
26+
/nbdist/
27+
/.nb-gradle/
28+
build/
29+
30+
### VS Code ###
31+
.vscode/

diagram/pom.xml

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<parent>
7+
<groupId>io.serverlessworkflow</groupId>
8+
<artifactId>serverlessworkflow-parent</artifactId>
9+
<version>0.2-SNAPSHOT</version>
10+
</parent>
11+
12+
<artifactId>serverlessworkflow-diagram</artifactId>
13+
<name>Serverless Workflow :: Diagram</name>
14+
<description>Java SDK for Serverless Workflow Specification</description>
15+
16+
<dependencies>
17+
<dependency>
18+
<groupId>org.slf4j</groupId>
19+
<artifactId>slf4j-api</artifactId>
20+
</dependency>
21+
22+
<dependency>
23+
<groupId>io.serverlessworkflow</groupId>
24+
<artifactId>serverlessworkflow-api</artifactId>
25+
<version>${project.version}</version>
26+
</dependency>
27+
28+
<dependency>
29+
<groupId>org.apache.commons</groupId>
30+
<artifactId>commons-lang3</artifactId>
31+
</dependency>
32+
33+
<dependency>
34+
<groupId>org.thymeleaf</groupId>
35+
<artifactId>thymeleaf</artifactId>
36+
</dependency>
37+
<dependency>
38+
<groupId>net.sourceforge.plantuml</groupId>
39+
<artifactId>plantuml</artifactId>
40+
</dependency>
41+
<dependency>
42+
<groupId>guru.nidi</groupId>
43+
<artifactId>graphviz-java</artifactId>
44+
</dependency>
45+
46+
<!-- test -->
47+
<dependency>
48+
<groupId>org.junit.jupiter</groupId>
49+
<artifactId>junit-jupiter-api</artifactId>
50+
<scope>test</scope>
51+
</dependency>
52+
<dependency>
53+
<groupId>org.junit.jupiter</groupId>
54+
<artifactId>junit-jupiter-engine</artifactId>
55+
<scope>test</scope>
56+
</dependency>
57+
<dependency>
58+
<groupId>org.junit.jupiter</groupId>
59+
<artifactId>junit-jupiter-params</artifactId>
60+
<scope>test</scope>
61+
</dependency>
62+
<dependency>
63+
<groupId>org.mockito</groupId>
64+
<artifactId>mockito-core</artifactId>
65+
<scope>test</scope>
66+
</dependency>
67+
<dependency>
68+
<groupId>ch.qos.logback</groupId>
69+
<artifactId>logback-classic</artifactId>
70+
<scope>test</scope>
71+
</dependency>
72+
<dependency>
73+
<groupId>org.assertj</groupId>
74+
<artifactId>assertj-core</artifactId>
75+
<scope>test</scope>
76+
</dependency>
77+
<dependency>
78+
<groupId>org.hamcrest</groupId>
79+
<artifactId>hamcrest-library</artifactId>
80+
<scope>test</scope>
81+
</dependency>
82+
<dependency>
83+
<groupId>org.skyscreamer</groupId>
84+
<artifactId>jsonassert</artifactId>
85+
<scope>test</scope>
86+
</dependency>
87+
</dependencies>
88+
</project>
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright 2020-Present The Serverless Workflow Specification Authors
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
package io.serverlessworkflow.diagram;
18+
19+
import io.serverlessworkflow.api.Workflow;
20+
import io.serverlessworkflow.api.interfaces.WorkflowDiagram;
21+
import io.serverlessworkflow.diagram.utils.WorkflowToPlantuml;
22+
import net.sourceforge.plantuml.FileFormat;
23+
import net.sourceforge.plantuml.FileFormatOption;
24+
import net.sourceforge.plantuml.SourceStringReader;
25+
import org.slf4j.Logger;
26+
import org.slf4j.LoggerFactory;
27+
28+
import java.io.ByteArrayOutputStream;
29+
import java.nio.charset.Charset;
30+
31+
public class WorkflowDiagramImpl implements WorkflowDiagram {
32+
33+
private static final Logger logger = LoggerFactory.getLogger(WorkflowDiagramImpl.class);
34+
35+
private String source;
36+
private Workflow workflow;
37+
38+
@Override
39+
public WorkflowDiagram setWorkflow(Workflow workflow) {
40+
this.workflow = workflow;
41+
this.source = Workflow.toJson(workflow);
42+
return this;
43+
}
44+
45+
@Override
46+
public WorkflowDiagram setSource(String source) {
47+
this.source = source;
48+
this.workflow = Workflow.fromSource(source);
49+
50+
return this;
51+
}
52+
53+
@Override
54+
public String getSvgDiagram() throws Exception {
55+
if(workflow == null) {
56+
throw new IllegalAccessException("Unable to get diagram - no workflow set.");
57+
}
58+
SourceStringReader reader = new SourceStringReader(WorkflowToPlantuml.convert(workflow));
59+
final ByteArrayOutputStream os = new ByteArrayOutputStream();
60+
String desc = reader.generateImage(os, new FileFormatOption(FileFormat.SVG));
61+
os.close();
62+
return new String(os.toByteArray(), Charset.forName("UTF-8"));
63+
}
64+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2020-Present The Serverless Workflow Specification Authors
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
package io.serverlessworkflow.diagram.config;
18+
19+
import org.thymeleaf.TemplateEngine;
20+
import org.thymeleaf.templatemode.TemplateMode;
21+
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;
22+
import org.thymeleaf.templateresolver.ITemplateResolver;
23+
24+
public class ThymeleafConfig {
25+
public static TemplateEngine templateEngine;
26+
27+
static {
28+
templateEngine = new TemplateEngine();
29+
templateEngine.addTemplateResolver(plantUmlTemplateResolver());
30+
}
31+
32+
private static ITemplateResolver plantUmlTemplateResolver() {
33+
ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
34+
templateResolver.setPrefix("/templates/plantuml/");
35+
templateResolver.setSuffix(".txt");
36+
templateResolver.setTemplateMode(TemplateMode.TEXT);
37+
templateResolver.setCharacterEncoding("UTF8");
38+
templateResolver.setCheckExistence(true);
39+
templateResolver.setCacheable(false);
40+
return templateResolver;
41+
}
42+
}

0 commit comments

Comments
 (0)