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*
;
  
Type:
  DataType | Entity
;
  
DataType:
  'datatype' name = ID
;
 
Entity:
  'entity' name = ID ('extends' superType = [Entity])? '{'
     features += Feature*
  '}'
;
 
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

test.mydsl

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

 @startuml
class A {
	List<String> names
}

A o--  C : c

class B {
	String something
}

B o-- "*"  A : myA

class C {
}


@enduml

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) '''
	@startuml
	«FOR e : elements.filter(typeof(Entity))»
	«e.toPlantUML»
	«ENDFOR»
	@enduml
	'''
	
	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»
		«ENDFOR»
	}
	
	«FOR f : features.filter[type instanceof Entity]»
		«name» o-- «IF f.many»"*" «ENDIF» «f.type.name» : «f.name»
	«ENDFOR»
	
	'''	
}

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

Bundle-ClassPath: .,
 lib/plantuml.jar
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/

    Grüße
    Boris

  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

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: