Home > Xtext > Xtext Model Visualization with PlantUML

Xtext Model Visualization with PlantUML

One of my Colleagues recently gave me a hint on PlantUML which is a nice tool to create Graphviz based UML diagrams from a textual input. This blogpost describes how to include PlantUML into Xtext to generate Visualizations from textual models on the fly.

Here is the DSL from Xtexts 15 minutes example

Domainmodel :
  elements += Type*
  DataType | Entity
  'datatype' name = ID
  'entity' name = ID ('extends' superType = [Entity])? '{'
     features += Feature*
  many?='many'? name = ID ':' type = [Type]

The target is to take an input model like

datatype String

entity A {
	many names : String
	c : C

entity B {
	something : String
	many myA : A

entity C {

An generate a nice Diagram like


To make the integration easy we generate the png using the existing Builder/Generator infrastructure

So here is the text input for PlantUML we need to generate

class A {
	List<String> names

A o--  C : c

class B {
	String something

B o-- "*"  A : myA

class C {


and here the generator that does the conversion and feeds Plantuml

class MyDslGenerator implements IGenerator {

	override void doGenerate(Resource resource, IFileSystemAccess fsa) {
		val filename = resource.URI.lastSegment
		for (dm : resource.contents.filter(typeof(Domainmodel))) {
			val plantUML = dm.toPlantUML.toString
			if (fsa instanceof IFileSystemAccessExtension3) {
				val out = new ByteArrayOutputStream()
				new SourceStringReader(plantUML).generateImage(out)
				(fsa as IFileSystemAccessExtension3).generateFile(filename + ".png",
					new ByteArrayInputStream(out.toByteArray))
			} else {
				fsa.generateFile(filename + ".txt", plantUML)

	def dispatch CharSequence toPlantUML(Domainmodel it) '''
	«FOR e : elements.filter(typeof(Entity))»
	def dispatch CharSequence toPlantUML(Entity it) '''
	class «name» {
		«FOR f : features.filter[type instanceof DataType]»
		«IF f.many»List<«f.type.name»>«ELSE»«f.type.name»«ENDIF» «f.name»
	«FOR f : features.filter[type instanceof Entity]»
		«name» o-- «IF f.many»"*" «ENDIF» «f.type.name» : «f.name»

To get PlantUML into the Classpath we add the jar to the project
and add it via the Manifest.MF file

Bundle-ClassPath: .,
About these ads
Categories: Xtext
  1. December 11, 2013 at 08:50 | #1

    Hey Christian, I just blogged about a tighter integration of Xtext & PlantUML: Instead of using an IGenerator, I contributed a diagramTextProvider to an extension provided by PlantUML’s Eclipse plugin. With this approach, PlantUML’s diagram viewer shows/refreshes your diagram even while you are typing: http://borisdevnotes.wordpress.com/2013/12/09/xtextplantuml/


  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Get every new post delivered to your Inbox.

%d bloggers like this: