Diagrams as code is a term used for storing the source of a diagram image as a text file.

Overview

Diagrams as code

Build Status Gitter

Diagrams as code is a term used for storing the source of a diagram image as a text file. Examples are architecture diagrams, or diagrams showing a system's behavior or design. A generator tool like PlantUML then generates an image from the text, and automatically layouts it. According to the ThoughtWorks Technology Radar, a key benefit is that you can use version control on the text files.

The diagrams as code project presented here has additional advantages.

By representing the diagrams as models in Java source code, you can automatically check if the generated diagrams are modeled correctly. And you get better syntax highlighting and auto-completion, compared to solutions like PlantUML.

Getting started

Diagrams as code is available on Maven Central.

If you are using Maven, include the following in your POM:

<dependency>
  <groupId>org.diagramsascodegroupId>
  <artifactId>diagramsascode-imageartifactId>
  <version>0.1.2version>
dependency>

If you are using Gradle, include the following in your build.gradle:

implementation 'org.diagramsascode:diagramsascode-image:0.1.2'

The jar files are also available in 'Releases'.

At least Java 8 is required to run diagrams as code.

Generate diagram images

Generate a sequence diagram image

The following sequence diagram image has been generated by the source code below:

Image of a sequence diagram

Here's the code (Java 8 syntax here):

// Create the participants (that exchange messages)
var participant1  = new Participant("Client");
var participant2  = new Participant("Server");

// Create the request and response message
var message1 = new Message(participant1, participant2, "Request Message");
var message2 = new Message(participant2, participant1, "Response Message");

// Build the diagram
var diagram = Diagram.builder()
  .withNodes(participant1, participant2)
  .withEdges(message1, message2)
  .withConstraints(new SequenceDiagramConstraints())
  .build();
	
// Create the image of the diagram and write it to a PNG file.
var outputFile = File.createTempFile("sequence", ".png");
SequenceDiagramImage.of(diagram).writeToPngFile(outputFile);

System.out.println("Sequence diagram written to: " + outputFile);

Generate an activity diagram image

The following activity diagram image has been generated by the source code below:

Image of an activity diagram

Here's the code (Java 8 syntax here):

// Create the initial and final node (to define where the flow starts and ends)
var initialNode = new InitialNode();
var finalNode = new FinalNode();
	
// Create the decision and merge node (to split the flow and merge it back together)
var decisionNode = new DecisionNode();
var mergeNode = new MergeNode();
	
// Create actions (for the flow steps)
var action1 = new Action("Action1");
var action2a = new Action("Action2a");
var action2b = new Action("Action2b");
var action3 = new Action("Action3");

// Connect the nodes with control flow edges.
// If they originate from a decision node, the third constructor parameter
// specifies the decision's condition (e.g. "x < 100")
var edge1 = new ControlFlow(initialNode, action1);
var edge2 = new ControlFlow(action1, decisionNode);
var edge3_a = new ControlFlow(decisionNode, action2a, "x < 100");
var edge3_b = new ControlFlow(decisionNode, action2b, "x >= 100");
var edge4_a = new ControlFlow(action2a, mergeNode);
var edge4_b = new ControlFlow(action2b, mergeNode);
var edge5 = new ControlFlow(mergeNode, action3);
var edge6 = new ControlFlow(action3, finalNode);

// Build the diagram
var diagram = Diagram.builder()
  .withNodes(initialNode, finalNode, decisionNode, mergeNode, action1, action2a, action2b, action3)
  .withEdges(edge1, edge2, edge3_a, edge3_b, edge4_a, edge4_b, edge5, edge6)
  .withConstraints(new ActivityDiagramConstraints())
  .build();

// Create the image of the diagram and write it to a PNG file.
var outputFile = File.createTempFile("activity", ".png");
ActivityDiagramImage.of(diagram).writeToPngFile(outputFile);

System.out.println("Activity diagram written to: " + outputFile);

Constraints

Sequence diagram constraints

A sequence diagram has to take the following constraints into account:

  • Only sequence diagram nodes are shown on the diagram, i.e. participants
  • Only sequence diagram edges are shown on the diagram, i.e. messages
  • Each participant has a name

These constraints are defined in the SequenceDiagramConstraints instance.

Activity diagram constraints

An activity diagram has to take the following constraints into account:

  • Only activity diagram nodes are shown on the diagram, i.e. initial/final nodes, decision/merge nodes, and actions
  • Only activity diagram edges are shown on the diagram, i.e. control flow
  • Each action has a name
  • Each decision node has one incoming edge
  • Each decision node has at least one outgoing edge
  • Each merge node has at least one incoming edge
  • Each merge node has one outgoing edge
  • An initial node has no incoming edges
  • A final node has no outgoing edges

These constraints are defined in the ActivityDiagramConstraints instance.

General notes on constraints

The constraints are validated implicitly when you create an ImageSource instance. You can also validate them explicitly by calling diagram.validate().

You can omit validating constraints by skipping the part .withConstraints(...) part when building the diagram. While this is not recommended in general since it allows building invalid diagrams, it may be useful for prototyping.

You can enforce more or less strict constraints by implementing the DiagramConstraints interface and provide your own constraints, or reuse existing ones.

Sub projects

The diagrams as code project consists of the following sub projects:

  • diagramsascode-core: defines the core modeling elements, like nodes, edges and constraints. It is not intended to be used directly. Instead, it needs to be extended for each diagram type to be generated.
  • diagramsascode-sequence: builds on core to represent UML sequence diagrams with participants and messages. Adds sequence diagram specific constraints.
  • diagramsascode-activity: builds on core to represent UML activity diagrams with initial/final nodes, actions, decision/merge nodes and control flow. Adds activity diagram specific constraints.
  • diagramsascode-image: enables you to generate automatically layouted images for the diagrams.
You might also like...

Usign ascii characters, recreate an image following a local path

CMDImage Usign ascii characters, recreate an image following a local path README El código toma una imagen desde una ruta local para convertirlo en ca

Aug 30, 2022

This project allows the exchange of files between your local disk and a D64 image (Commodore 64 image disk) . Ce projet permet l'échange de fichiers entre votre disque local et une image D64 (Image de disquette du Commodore 64).

DiskToolC64 Ce projet permet l'échange de fichiers entre votre disque local et une image D64 (Image de disquette du Commodore 64). Introduction Les fi

Oct 12, 2022

A simple program that is realized by entering data, storing it in memory (in a file) and reading from a file to printing that data.

A simple program that is realized by entering data, storing it in memory (in a file) and reading from a file to printing that data.

Pet project A simple program that is realized by entering data, storing it in memory (in a file) and reading from a file to printing that data. It can

Apr 28, 2022

Twitter Text Libraries. This code is used at Twitter to tokenize and parse text to meet the expectations for what can be used on the platform.

twitter-text This repository is a collection of libraries and conformance tests to standardize parsing of Tweet text. It synchronizes development, tes

Jan 8, 2023

This is the term project for level-1 term -2

This is the term project for level-1 term -2

Java-Fx-Term_Project This is the term project for level-1 term -2 . It's a server-client playerDatabase system for some clubs of EPL, although sign up

Apr 1, 2022

Facsimile - Copy Your Most Used Text to Clipboard Easily with Facsimile!. It Helps You to Store You Most Used Text as a Key, Value Pair and Copy it to Clipboard with a Shortcut.

Facsimile - Copy Your Most Used Text to Clipboard Easily with Facsimile!. It Helps You to Store You Most Used Text as a Key, Value Pair and Copy it to Clipboard with a Shortcut.

Facsimile An exact copy of Your Information ! Report Bug · Request Feature Table of Contents About The Project Built With Getting Started Installation

Sep 12, 2022

A gradle plugin based on ANTLR to generate UML diagrams from kotlin source code.

A gradle plugin based on ANTLR to generate UML diagrams from kotlin source code.

A gradle plugin based on ANTLR to generate UML diagrams from kotlin source code.

Oct 26, 2022

Text Object Java Objects (TOJOs): an object representation of a multi-line structured text file like CSV

It's a simple manager of "records" in a text file of CSV, JSON, etc. format. It's something you would use when you don't want to run a full database,

Dec 27, 2022

A library for creating and editing graph-like diagrams in JavaFX.

A library for creating and editing graph-like diagrams in JavaFX.

Graph Editor A library for creating and editing graph-like diagrams in JavaFX. This project is a fork of tesis-dynaware/graph-editor 1.3.1, which is n

Jan 1, 2023

Open source Picture to text, text to Picture app

Open source Picture to text, text to Picture app

Pic SMS App Pic SMS is a free open source app. With Pic SMS, you can: convert pictures into text parts and send as SMS convert text parts into a pictu

Feb 8, 2022

Simple Java image-scaling library implementing Chris Campbell's incremental scaling algorithm as well as Java2D's "best-practices" image-scaling techniques.

imgscalr - Java Image-Scaling Library http://www.thebuzzmedia.com/software/imgscalr-java-image-scaling-library/ Changelog --------- 4.2 * Added sup

Jan 5, 2023

Classpy is a GUI tool for investigating Java class file, Lua binary chunk, Wasm binary code, and other binary file formats.

Classpy is a GUI tool for investigating Java class file, Lua binary chunk, Wasm binary code, and other binary file formats.

Classpy Classpy is a GUI tool for investigating Java class file, Lua binary chunk, Wasm binary code, and other binary file formats. Inspiration This t

Dec 17, 2022

Tencent Kona JDK11 is a no-cost, production-ready distribution of the Open Java Development Kit (OpenJDK), Long-Term Support(LTS) with quarterly updates. Tencent Kona JDK11 is certified as compatible with the Java SE standard.

Tencent Kona JDK11 is a no-cost, production-ready distribution of the Open Java Development Kit (OpenJDK), Long-Term Support(LTS) with quarterly updates. Tencent Kona JDK11 is certified as compatible with the Java SE standard.

Tencent Kona JDK11 Tencent Kona JDK11 is a no-cost, production-ready distribution of the Open Java Development Kit (OpenJDK), Long-Term Support(LTS) w

Dec 16, 2022

Tencent Kona JDK17 is a no-cost, production-ready distribution of the Open Java Development Kit (OpenJDK), Long-Term Support(LTS) with quarterly updates.

Tencent Kona JDK17 is a no-cost, production-ready distribution of the Open Java Development Kit (OpenJDK), Long-Term Support(LTS) with quarterly updates.

Tencent Kona JDK17 Tencent Kona JDK17 is a no-cost, production-ready distribution of the Open Java Development Kit (OpenJDK), Long-Term Support(LTS) w

Nov 30, 2022

Uses modified Jaro distance to find closest result to search term

Uses modified Jaro distance to find closest result to search term

Jan 17, 2022
Comments
  • Make diagram image creation more convenient for users

    Make diagram image creation more convenient for users

    Right now, it takes two steps to create a diagram image, once you've built the diagram. For example:

    Diagram diagram = ...
    ImageSource source = ImageSource.ofSequenceDiagram(diagram);
        
    Image image = Image.fromSource(source);
    File outputFile = File.createTempFile("sequence", ".png");
    image.writeToPngFile(outputFile);
    

    Implementing this issue will simplify it to:

    Diagram diagram = ...
    File outputFile = File.createTempFile("sequence", ".png");
    SequenceDiagramImage.of(diagram).writeToPngFile(outputFile);
    

    Apart from the SequenceDiagramImageclass, there will be an ActivityDiagramImage class for activity diagrams.

    enhancement 
    opened by diagramsascode 0
Releases(v0.1.1)
Owner
null
Open source Picture to text, text to Picture app

Pic SMS App Pic SMS is a free open source app. With Pic SMS, you can: convert pictures into text parts and send as SMS convert text parts into a pictu

Kaung Khant Kyaw 17 Feb 8, 2022
Simple Java image-scaling library implementing Chris Campbell's incremental scaling algorithm as well as Java2D's "best-practices" image-scaling techniques.

imgscalr - Java Image-Scaling Library http://www.thebuzzmedia.com/software/imgscalr-java-image-scaling-library/ Changelog --------- 4.2 * Added sup

Riyad Kalla 1.1k Jan 5, 2023
Roman Beskrovnyi 250 Jan 9, 2023
A VisionCamera Frame Processor Plugin to preform text detection on images using MLKit Vision Text Recognition

vision-camera-ocr A VisionCamera Frame Processor Plugin to preform text detection on images using MLKit Vision Text Recognition. Installation yarn add

Aaron Grider 133 Dec 19, 2022
Fast computer vision library for SFM, calibration, fiducials, tracking, image processing, and more.

Table of Contents Introduction Cloning Repository Quick Start Gradle and Maven Building from Source Dependencies Help/Contact Introduction BoofCV is a

Peter Abeles 916 Jan 6, 2023
Custom Image Server for use with ShareX

rImageServer How to use: Normal Instalation: Download .jar Start: javar -jar [filename].jar Edit: imageserver.propierties Check if web works -> Go to

Ryzeon 11 May 21, 2022
A well-designed local image and video selector for Android

Matisse Matisse is a well-designed local image and video selector for Android. You can Use it in Activity or Fragment Select images including JPEG, PN

Zhihu 12.4k Dec 29, 2022
Luban—Image compression with efficiency very close to WeChat Moments

Luban—Image compression with efficiency very close to WeChat Moments

郑梓斌 13.1k Dec 29, 2022
Provide image storage and access services.

Provide image storage and access services.

shiyq 2 Jan 23, 2022