code to diagram logo

Code To Diagram

Converting Code Execution into Sequence Diagrams

Sequence Diagramas

The sequence diagram it is probably the must intuitive way to understand some code execution.

Use it in the pre-project phase it is very common to describe the excepted behavior from some software. From the sequence diagram it is possible to developer test diagram, as check if all the features used on it was describe into the another phase of the planning as the use cases, for example.

Sequence Diagramas Weaknesses

When the sequence diagram it is used to describe the features into the code level, as the interaction of objects, using methods, it is possible to plan how the classes will be. But, as the code development evolves, been caused by the analysis fail or by requirement change, the diagrams will losing the link with the reality losing consequently the descriptive function to one analysis history function.

Also, to require the existence of one diagram to each test methods of some project will bring a huge spent of time, into it's creation as is in the maintenance. This is because each change of some method name or attribute name will require a change into all diagrams what involve it.

Code to Diagram Propose

The solution proposed here it is the automatic generation of sequence diagrams based on the PHP code execution. One full code execution or some stretch of execution it is monitored by classes of code instrumentation what, based on the in formations received, creating the diagram of that code execution. This diagram can be seen as HTML, saved as HTML, and also to be saved as XML and in the future exported as a jpeg image.

The pattern used here was the factorys and printers. There is many factorys what builds uml sequence diagram objects and many options of printers what create a different result based on the uml sequence diagram object received.

if you have some special case, when any of the avaliable printers and factorys solve your problem, everthing it is extremly well doc to help to you create new classes and append new features.

code to diagram flow
How it Works ?

All this process must be simple and enough to be used simply append one call, but flexible also to deal with all the special cases. The big idea it is to create the diagram of the execution been less invader as possible into the source code what the execution will run. You can see the runing examples here.

Preview of Diagram from Code Execution

In a short preview this code:

<?php

require_once('../../public/codetodiagram.php');

CodeToDiagram::getInstance()->start();

require_once('Wolf.class.php');

require_once('House.class.php');

require_once('Pig.class.php');

require_once('History.class.php');

new History();

?>

Will bring to you this:

three little pigs diagram automatically generated

Click here to know how to use this project into your application

Draw your diagram

One small component was created in the process of generation of this software what can be used separately. It is the generator of sequence diagrams from one XML. This application can be test here where one XML of example it is used but can be change freely.

The target of this project it is make possible the generation of a sequence diagram from each test method. Been each generated diagram saved with an easy to find name. Some new features should be appending to as show the code of the caller, make improvements into the parameters descriptions and many more.

Preview of Diagram from Xml Iterpretation

In a short preview this xml:

  <  ?xml  version= "1.0"  encoding= "UTF-8"?>   <  sequence>  <  actors>  <  actor  id= "user"              type= "user"> User<  /actor>  <  actor  id= "view"              type= "boundary"> :View<  /actor>  <  actor  id= "controller"  type= "controller"> :Controller<  /actor>  <  actor  id= "model"            type= "entity"> :Model<  /actor>  <  /actors>  <  messages>  <  message  type= "call"  from= "user"  to= "view"  text= "request()"/>   <  message  type= "call"  from= "view"  to= "controller"  text= "handleEvent()"/>   <  message  type= "call"  from= "controller"  to= "model"  text= "queryInformation()"/>   <  message  type= "call"  from= "controller"  to= "controller"  text= "doValidation()"/>   <  message  type= "return"  from= "controller"  to= "view"  text= "updateView()"/>   <  message  type= "return"  from= "view"  to= "user"  text= "notifyUser()"/>   <  /messages>  <  /sequence> 

Will bring to you this:

model view controller sequence diagram automatically generated by the code to diagram

Download

You can download the last version of this project just downloading from it's SVN . But, if you just want to take a look into the code, it is better view by this link.

Features

While still not fully working on Internet Explorer, the generator already makes life easier for those responsible in maintaining this kind of diagrams. We welcome anyone with the patience and desire to make the CSS changes need for it to work on IE.

Also, fully compliance with UML 2.0 is still under development. Anyone interested in working in these fields is more than welcome to join the team. And if you have a patch on add-on to send, feel free to do so.And remember, this is free software, and as such, I can give you no warranty. Use it at your own risk. It's not for the faint of heart.

Who build this stuff ? And Why ?

This project was buid to create a high quality documentation in our other project, called Coruja PHP Framework. That aims to do everything with the highest quality and, of course, the documentation could not be out of this.

Click here to see the team responsible for the development of these projects

Links Related

Click IBM - UML's Sequence Diagram to know more about sequence diagrams.

Click The Tom Kyte Blog talk about Code Instrumentation or Java(tm) Source Code Instrumentation to know more about code instrumentation.