14.6.11

Railroad Diagrams in Redmine Wiki

I recently needed to show the grammar of our XText based UML guard and action language in the Azmun Wiki. I decided to use railroad diagrams for that purpose, since I remembered that the XText documentation contains such cool diagrams in the MWE2 sub-chapter. For example, here is the railroad diagram for Module definitions:



I don't know how the XText documentation is generated, but I know that the rail package for LaTeX is able to generate such diagarams. So I wanted to integrate such diagrams in our wiki using rail.

We host all the projects of our research training group METRIK in a Redmine instance. One of the Redmine Plug-Ins is the Wiki External Filter, which allows defining macros that process macro argument using external filter program and render its result in Redmine wiki. The Plug-In is shipped with support for PlantUML to draw UML diagrams, Graphviz for abritrary diagrams, ritex for MathML, and ffmpeg to embed videos.

In order to add a new filter, I extended the redmine/config/wiki_external_filter.yml file with the following entry:

rail:
    description: "Constructs railroad diagrams for (E)BNF grammars, see http://notendur.hi.is/snorri/091263/rail/rail.html"
    template: image
    outputs:
      - command: "SOME_PATH/rail.sh"
        content_type: "image/png"
        prolog: "\documentclass{article} \n \usepackage{rail} \n \pagestyle{empty} \n \\begin{document} \n \\begin{figure} \n"
        epilog: "\n \\end{figure} \n \\end{document} \n"

Our filter is named rail and calls the shell script rail.sh, which I will show in a second. It defines the needed prolog and epilog LaTeX commands including the usage of the rail package and the definition of a figure, so that the user only needs to specify the rail commands.

Here now the code of the rail.sh shell script:

#!/bin/sh

# pipe STDIN to file
cat - > input.tex

# run first time with latex
latex input.tex 1> /dev/null 2> /dev/null

# run rail
rail input 1> /dev/null 2> /dev/null

# run second time with latex
latex input.tex 1> /dev/null 2> /dev/null

# convert DVI file to PNG
dvipng -q -Ttight -M -pp1 --noghostscript -D150 -o out.png input.dvi 1> /dev/null 2> /dev/null

# remove temporary files
rm input.*

# pipe contents of PNG to STDOUT
cat out.png -

This script has following prerequesites:
(Note that there are precompiled Debian and RPM packages for LaTeX and dvipng available.)

Having all the pieces together, we now can use rail scripts to create nice railroad diagarams in Redmine. Here is an example taken from Azmun:

{{rail(
\railalias{IMPLIES}{->}
\railalias{EQUIVALENCE}{<>}
\railalias{OR}{||}
\railalias{XOR}{\textasciicircum}
\railalias{AND}{\&\&}
\railalias{EQ}{==}
\railalias{NEQ}{!=}
\railalias{LT}{<}
\railalias{GT}{>}
\railalias{LTE}{<=}
\railalias{GTE}{>=}
\railalias{SHIFTLEFT}{<<}
\railalias{SHIFTRIGHT}{>>}
\railalias{MUL}{*}
\railalias{DIV}{/}
\railalias{MOD}{\%}
\railalias{PLUS}{+}
\railalias{MINUS}{-}
\railalias{NOT}{!}
\railalias{PO}{(}
\railalias{PC}{)}
\railalias{DOT}{.}
\railalias{FALSE}{false}
\railalias{TRUE}{true}
\railalias{INT}{0..9}

\railterm{IMPLIES,EQUIVALENCE,OR,XOR,AND,EQ,NEQ,LT,GT,LTE,GTE,SHIFTLEFT,SHIFTRIGHT,MUL,DIV,MOD,PLUS,MINUS,NOT,PO,PC,DOT,FALSE,TRUE,INT}

\begin{rail}  
  BasicExpression :
    [constants] ( BooleanConstant  
        | IntegerConstant )
    | [attribute reference] AttributeReference
    | PO BasicExpression PC
    | [logical NOT] NOT BasicExpression
    | ( [integer multiplication] BasicExpression MUL BasicExpression
        | [integer division] BasicExpression DIV BasicExpression 
        | [integer remainder] BasicExpression MOD BasicExpression )
    | ( [integer addition] BasicExpression PLUS BasicExpression
        | [integer substraction] BasicExpression MINUS BasicExpression )
    | ( [bit shift left] BasicExpression SHIFTLEFT BasicExpression
        | [bit shift right] BasicExpression SHIFTRIGHT BasicExpression )
    | ( [equality] BasicExpression EQ BasicExpression
        | [inequality] BasicExpression NEQ BasicExpression
        | [less than] BasicExpression LT BasicExpression
        | [greater than] BasicExpression GT BasicExpression
        | [less than or equal] BasicExpression LTE BasicExpression
        | [greater than or equal] BasicExpression GTE BasicExpression )
    | [logical AND] BasicExpression AND BasicExpression
    | ( [logical OR] BasicExpression OR BasicExpression
        | [logical exclusive OR] BasicExpression XOR BasicExpression )
    | [logical equivalence] BasicExpression EQUIVALENCE BasicExpression
    | [logical implication] BasicExpression IMPLIES BasicExpression
  ;

  AttributeReference : UMLPropertyReference ( DOT AttributeReference )?;
  BooleanConstant : FALSE | TRUE ;
  IntegerConstant : ( MINUS | PLUS )? (INT+) ;   
\end{rail}  

)}}

This script results in the following diagram:



So, what is missing? Yes, an automatic conversion of Xtext grammars to rail scripts.
I am also looking forward to have the new Xtext Syntax Graph View.

Happy grammar hacking!

29.3.11

New screencast from nusmv-tools

I created a screencast which shows the features of the nusmv-tools Eclipse editor. The editor is build using Xtext, so many thanks to the itemis AG team!

You can watch the video also in HD.

nusmv-tools Eclipse editor in action from Siamak Haschemi on Vimeo.


Happy model editing!

22.3.11

Access NuSMV from Java

NuSMV is a symbolic model checker, which I use as part of my PhD thesis. However, NuSMV is written in C and I could not found any Java API to use the model checker from Java. Luckily, creating Java libraries out from C-headers becomes very easy by using JNA and JNAerator:

JNA stands for Java Native Access and is a alternative to JNI, the official way to access shared native libraries. The advantage of JNA is the dynamic binding to shared libraries, whithout writing anything but Java code. Principally, you write a Java Interface with methods machting the corresponding C-header of the library and provide that Java Interface to JNA. More info‘s and examples can be found on the project‘s website.

While JNA provides the dynamic bridging between calls to the java interface, you still have to write the Java Interface and therefore you also need the knowledge about the conversion conventions from C to Java. Here comes JNAerator into the game. JNAerator is a tool to parse C headers and generate the corresponding JNA Java Interfaces. It comes with a Graphical User Interface, but can also be used on the command-line.

To create a Java API for NuSMV, first a dynamic library has to be created out of the sources. Unfortunately, this library is not created by the make files. I host some scripts for the current NuSMV 2.5.2 to help you out with windows, linux, and osx.
Having the shared library for NuSMV (nusmv.dll, libnusmv.so, libnusmv.jnilib), we can go an create the Java API using JNAerator (I also host the script for that). What we get is a java interface which now gives us access to NuSMV.

Accessing NuSMV becomes now as easy as:

import static org.eclipselabs.nusmvtools.nusmv4j.NusmvLibraryUtil.toByteBuffer;

import java.io.File;
import java.io.PrintStream;

import org.eclipselabs.nusmvtools.nusmv4j.NuSMV4J;
import org.eclipselabs.nusmvtools.nusmv4j.NusmvLibrary;

public class Main {
  public static void main(final String[] args) throws Exception {
    final File file = new File("testModel.nusmv");
    final PrintStream ps = new PrintStream(file);
    ps.println("MODULE main");
    ps.println("  VAR");
    ps.println("    x : boolean;");
    ps.flush();
    ps.close();

    final NusmvLibrary nusmvLibrary = NuSMV4J.getNusmvLibrary();
    nusmvLibrary.Cmd_CommandExecute(toByteBuffer("reset"));
    nusmvLibrary.Cmd_CommandExecute(toByteBuffer("set default_trace_plugin 4"));
    nusmvLibrary.Cmd_CommandExecute(toByteBuffer("read_model -i testModel.nusmv"));
    nusmvLibrary.Cmd_CommandExecute(toByteBuffer("go"));
    nusmvLibrary.Cmd_CommandExecute(toByteBuffer("check_ctlspec -p \"!x\""));
    nusmvLibrary.Cmd_CommandExecute(toByteBuffer("show_traces"));

    file.delete();
  }
}

When you run the above class, you get the following line on the console:

-- specification !x  is false
-- as demonstrated by the following execution sequence
<?xml version="1.0" encoding="UTF-8"?>
<counter-example type="0" desc="CTL Counterexample" >
  <node>
    <state id="1">
      <value variable="x">TRUE</value>
    </state>
  </node>
  <loops> </loops>
</counter-example>

If you don't want to take the above steps on your own, you should take a look at my project nusmv-tools, which contains some Eclipse based tools like an Eclipse editor for NuSMV files (see blow), an API to read the NuSMV counterexamples in java, a model advisor, and the libraries for accessing NuSMV from java, which I presented in this post.



Happy model checking!