In the past years I have done lots of work in C# and recently came back to work with Java because I am now working in a project that involves MPS. The Meta Programming System from JetBrains. Which is a language workbench with a projectional editor. I got quite used to some of the features C# offers. One of the features I love the most is implicit strong typed variables, expressed with the var keyword as type in a variable declarations. Due to the extensible nature of MPS it is quite easy to extend existing languages. So I decided to build this feature in MPS for Java.

Now you might think this ridiculously crazy since it is hard to change an existing language. But in MPS everything is about modularization, extensibility and composition of languages. And It ships with a Java implementation called baselanguage out of the box. For me it took 30 minutes of work to build this extension. Though I have a lot of experience with MPS and a good knowledge of baselanguage but even if you don’t have that it should take you not longer than an hour to do it on your own. And that is what this tutorial is about. What I will show here is how to extend baselanguage with a new kind of variable declaration that uses its initializer to calculate its type.

Read on →

On Sunday I have given a talk about mbeddr and how we build it at FODESM. You can find the slides at speakerdeck where you can also download them as a PDF. And here you can find the original Keynote 6 files.

The talk was also recorded and I will post the link once it is available.

After some month of low activity in the mbeddr.arduino repository I found more time to work on it in the past week. It is now in a state that it is reasonable to release a first version of it. This version contains a very fist look at the features available by mbeddr in combination with real hardware. If you find any issues please report them on the issue tracker or get in touch with me over the various social media links on the left or via Email. Also contributions and ideas for new features are highly welcome!

You can grab the current release from the github release page. The readme provides some detailed installation instructions for Linux, Mac and Windows.

Have fun hacking your Arduino with mebddr! :–)

Here is a little status update on the mbeddr.arduino project. We are getting closer to the first “release”, which will be an early access version but we want to get it into the hands of people to try it. The main topic on this post is not the release ;–) it is about the opportunity to avoid common pitfalls in Arduino programing with Model Driven Software Development (MDSD). This weekend I have added support for sleep modes. While I was reading the docs for avr-libc I stumbled op on the possibility that race conditions could occur when simply using the macros from avr-libc. This can happen if you just want to put the cpu to sleep if some condition is true and you are sure that the necessary interrupts to wakeup are enabled. To avoid this problem you normally write code like this:

avoid race condition
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <avr/interrupt.h>
#include <avr/sleep.h>

//some code here
set_sleep_mode(<mode>);
cli();
if (some_condition)
{
sleep_enable();
sei();
sleep_cpu();
sleep_disable();
}
sei();

The code disables the all interrupts, then checks the condition and when the condition is meet sets the sleep mode, enables the interrupts again and then sends the cpu to sleep. Since the instruction right behind the sei() macro is guaranteed to be executed before the interrupts are enabled this is a save way to do this. In mbeddr.arduino I wanted to provide a easier way to do this.

But lets first have a look at the basic sleep statement which looks just like the one in C:

simple sleep
1
sleep(Idle Mode);

Except there is some syntactic sugar with blanks instead of using underscores. If you just want to sleep if a certain condition is true you can use a intention to add a condition to the sleep statement:

After you have done this the statements looks a bit different:

sleep with condition
1
sleep(mode: Idle Mode, condition: <expr>);

You can now add you condition when you want to go to sleep mode in the sleep statement and the generator will take care that it is checked with disabled interrupts.

This way the user just has to know that putting the controller into sleep mode under a certain condition is something special, but he doesn’t need to know how to do it. In order to point to him that he might be doing something dangerous the typesystem checks if there is a IfStatement with a single SleepStatement under it and issues a warning. To fix this warning the user can use a simple intention:

I hope this gave a little impression about what goal we are after with mbeddr.arduino and how MDSD can help this.

Good news for all debian and ubuntu users using the Meta Programming System by JetBrains: I have just created a debain package for it. I have also setup a repository on this server to allow easy handling.

In order to use it open a root shell and run the following commands. Those will add the repository and import my public gpg key:

1
2
3
4
echo 'deb http://dl.logv.ws/repository ./' >> /etc/apt/sources.list
wget http://logv.ws/files/kdummann.asc
apt-key add kdummann.asc
apt-get update

After that you can simply install MPS by typing;

1
apt-get install jetbrains-mps

The same also applies to mbeddr:

1
apt-get install mbeddr

Update: Currently the mbeddr package has optional dependencies for example the nusmv package which aren’t available on many distributions, we will provides theses packages in the same repository shortly. This will allow us to not depend on any distribution update schedule when we need new versions of this tools. Sadly we will not be able to distribute yices which is used by mbeddr for analyses because their license doesn’t allow is to redistribute their software. You will still have to accept their license manually and install it by hand. But I will post a tutorial how to do that on the mbeddr blog.

Update 2: Some Linux Mint users reported that they had problems with the openJDK on that platform. If you experience any strange problems please switch to the Oracle JDK and see if this fixes your problems. For the german speaking people of you there is a detailed description how to it here.

Introduction

As some of you might already know I have worked on an Arduino extension for mbeddr. Because such system run headless you can’t just use printf for displaying messages. These systems usually ship with a serial or USB port which can be used to communicate with the world. There are actually two ways to deal with this, first we could replace the printf backing file handle with a handle that writes to the serial port, or second use mbeddrs own reporting infrastructure to deal with this. The mebddr approach also give us more flexibility because we could either write messages to the serial port or store critical errors in EPROM for further investigation.

Here I will talk about how to build your own reporting backend into mbeddr, in this case a backend that writes to the serial port.

Architecture

First I will give you an overview about the reporting architecture of mbeddr. It consists of four main parts: MessagesDefinition, MessageDefinitionTable, theReportStatement and a ReportingStrategy.

MessagesDefinition

Messages as the name says define messages. They have a name to reference them, a text that is written out when the message is reported and the can have parameters. These parameters are basically key-value pairs. A message also has additional attributes:

MessageSeverity: similar to a priority it can be ERROR,WARN and INFO

active: a boolean flag that defines if the message is active or not.

count: a boolean flag that defines if the times the messages war reported should be counted.

MessageDefinitionTable

A collection of messages. Any MessagesDefinition belongs to a MessageDefinitionTable. It acts as a kind of namespace for messages.

ReportStatement

The ReportStatement is used to report a message. It references a message and provides the actual values for parameters, if the message defines any. The parameters can be any value matching the type of definition, for instance a local variable or a value obtained from an external sensor. The statement also provides so called checks which is a guard. Only if it evaluates to true the actual message will be reported.

ReportingStrategy

The ReportingStrategy is used inside the build configuration to specify what kind of reporting the project uses. In mbeddr there are already predefined reporting strategies: printf and nothing. As the name suggest the first one uses simple printf statements to report a message and the later does nothing with them.

Implementation

Now that we have a overview about what we need let’s start implementing our own reporting.

Read on →

As some of you might have noticed there where some recent changes in my professional life. I am really happy to announce that I am now working for Itemis, the company behind mbeddr, as a team member of the mbeddr team. Markus already mentioned this on the mbeddr blog. This also implies that I left BKR Software the company that I was working for before. It was an exiting one and a half year where I meet many interesting people and was able to some exciting projects. I would like to thank all of my former collages for this time and I wish you all the best.

Ok but why did I change? Well there were several reasons for that some are private (which I won’t talk about here), but the main reason is that I am now able to work full time on modeling projects which was always a pleasure to me. Until now it was more or less my own passion that motivated me to use model driven or DSLish solutions in projects. Beside this mbeddr is MPS based, a platform that I really like and I think pushed projectional editors to a usable level. On the other hand I am now able to work with Markus and Bernd who pretty much influenced my professional career, with a lot of papers they have published and talks they gave at various conferences, especially in the domain of modeling and product line engineering.

Stay tuned for further development to the mbeddr arduino project which is now my 20% (yep looking at you Googlers makes me smiles a bit ;–) ) project so I can spent much more time on it. I currently preparing some blog posts and tutorials that should give you some more detailed look inside mebddr and how mebddr helped to make life on embedded hardware easier.

This post is about setting up ant to build an mbeddr project. It enables you to do continues integration builds or other automated build scenarios. I assume that you have already installed mbeddr. If you haven’t, you should follow the instructions in the mbeddr user guide or this tutorial. It also assumes that you are using the source code distribution of mbeddr and have build it. If are using the binary distribution have a look at the end of this tutorial.

First go to you project folder and create a build.xml and build.properties file. The build.properties file should look like this:

1
2
3
mps.home=/path/to/mps
mps.platform.caches=/path/to/cache
mbeddr.github.core.home=/path/to/mbeddr

Obviously you need to set the variable to some sane values for you environmental.

The next step is to create a skeleton for the build.xml:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<project name='mbeddr.arduino' default='build'>
  <property file='build.properties'/>
  <path id='mps.ant.path'>
    <pathelement location='${mps.home}/lib/mps-backend.jar'/>
    <pathelement location='${mps.home}/lib/jdom.jar'/>
    <pathelement location='${mps.home}/lib/log4j.jar'/>
    <pathelement location='${mps.home}/lib/mps-core.jar'/>
  </path>
  <taskdef resource='jetbrains/mps/build/ant/antlib.xml' classpathref='mps.ant.path'/>
  <jvmargs id='myargs'>
    <arg value='-ea'/>
    <arg value='-Xss1024k'/>
    <arg value='-Xmx2048m'/>
    <arg value='-XX:MaxPermSize=128m'/>
    <arg value='-XX:+HeapDumpOnOutOfMemoryError'/>
    <arg value='-Didea.system.path=${mps.platform.caches}/system'/>
    <arg value='-Didea.config.path=${mps.platform.caches}/config'/>
    <arg value='-Didea.plugins.path=${mps.platform.caches}/plugins'/>
  </jvmargs>
  <!--Content goes here -->
</project>

Now you need to fill this skeleton with some actual content. There three kinds of content that are possible:

This tutorial focuses on the first two. If you want to run tests the official MPS docs should give you further information

Building languages 

In order to build a language you add this code at the Content goes here placeholder:

1
2
3
4
5
6
7
8
9
  <target name='build'>
    <mps.generate parallelMode='true' fork='true'>
      <jvmargs refid='myargs'/>
      <project file='path/to/your/mpr'/>
      <modules dir='./languages'/>
      <library name="mbeddr"
         dir="${mbeddr.github.core.home}/code/languages"/>
    </mps.generate>
  </target>

Make sure that the file attribute of the project node points to the .mpr file you want to build. If you have multiple mpr files you will have to have multiple target nodes which you can separate by name. To see how to build them in a single step have a look at the putting it all together section.

The module node is used to specify a subdirectory. You can use it to limit the amount of code that is build. For instance you can prevent building anything else than a single language.

Also make sure that the dir attribute of the mbeddr library is pointing to the code/languages subdirectory since we don’t need the samples in the build.

Building solutions 

After you have build your language you might want to use it. :–)

In order to do so you add this in the Content goes here section:

1
2
3
4
5
6
7
8
9
10
<target name='build'>
    <mps.generate parallelMode='true' fork='true'>
      <jvmargs refid='myargs'/>
      <project file='path/to/your/mpr'/>
      <modules dir='./solutions'/>
      <library name="yourlanguage" dir="./languages"/>
      <library name="mbeddr"
         dir="${mbeddr.github.core.home}/code/languages"/>
    </mps.generate>
</target>

It is quite similar to the one used to build a language, except it’s module attribute refers to the solutions subdirectory. As for the languages you can specify it to be more concrete to just build a subset of your solutions. You also notice an extra library node which references your languages. If you don’t have any languages extending mbeddr and just have solutions using it you can remove this node.

Also make sure that the dir attribute of the mbeddr library is pointing to the code/languages subdirectory since we don’t need the samples in the build.

Putting it all together 

Now that you know how to build languages and solutions we can put all these together in order to build a hole project. This is not really mbeddr or MPS specific, it’s just plain ant stuff to do.

But first let me explain why separating this into two steps. Yes you could simply build a hole mpr file, but most of the time you want to have some more fine grained control about what is build and when. If the languages build would fail the solutions would fail anyway and it would be hard to figure out what went wrong, except you seek though all the logs. If you separate them you can easily see that there is a problem with the languages and simply ignore the solution build errors. This is extremely handy when running continuous integration builds against your check-ins.

What you need to do is, in contrast to name the target just build, you create two different like this:

1
2
3
4
5
6
  <target name='build-languages'>
      <!-- language build description goes here -->
  </target>
  <target name='build-solution'>
      <!-- language build description goes here -->
  </target>

Now you can type ant build-languages to build the languages or ant build-solutions to build the solutions. But what if you would like to build everything with a single command?

You create a new target named build with two antcall elements:

1
2
3
4
  <target name="build">
    <antcall target="build-languages"/>
    <antcall target="build-solutions"/>
  </target>

Be aware that the order matters here. So you need to build the languages first an then the solutions.

What if I don’t use mbeddr from source? 

If you want to use the mbeddr binary distribution and you have installed it already you simply remove the mbeddr library from your build files. A solution would look like this:

1
2
3
4
5
6
7
8
<target name='build'>
    <mps.generate parallelMode='true' fork='true'>
      <jvmargs refid='myargs'/>
      <project file='path/to/your/mpr'/>
      <modules dir='./solutions'/>
      <library name="yourlanguage" dir="./languages"/>
    </mps.generate>
</target>

Today a gave a little talk about my spare time project ‘mbeddr.arduino’. I gave brief overview what’s the goal and it current state. The slides can be found here.

This guide covers how to build and install mbeddr from source, it does not include how to install the fancy model checking tools.

After you have installed Ubuntu 13.04 you will notice that it already ships with Java 7. Since MPS only works with Java 6 you need to downgrade it. The problem is that the official package repository of Ubuntu does not contain a Oracle Java version anymore, there is OpenJDK 6 but it will not work with properly MPS. You can go to Oracles download page and install it on your own but that is pain in the ass. Luckily there is a unofficial repository that can do all the that for you. To add the repository and install Java run this commands:

sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update
sudo apt-get install oracle-java6-installer

This gives you a working Java 6 but you need to make it the default Java on your machine. This is done with:

sudo apt-get install oracle-java6-set-default

Now you can install MPS from the JetBrains page. Extract the tar.gz to your favorite location. In order to be able to build and run mbeddr you will need some additional stuff: ant, gcc, git, make and graphvis. To install them run:

sudo apt-get install ant git-core graphviz build-essential

After that go to the location where you want to put the mbeddr source code and clone the git repository with:

git clone https://github.com/mbeddr/mbeddr.core.git

Once that is finished set up the build properties file in /path/to/mbeddr.core/code/languages/ there is an example file in the same location that should give you a good point to start from. The finial configuration should look something like this:

mbeddr.github.core.home=/build/mbeddr.core/
mps.platform.caches=/build/mps-ant-caches/

# MPS installation
mps.home=/build/mps/

To build mbeddr run:

./rebuildLanguagesAfterCheckout.sh

in the same location.

When that is finished start your MPS and navigate to File -> Settings, there you select GlobalLibraries and create a new variable called mbeddr.core which points to path/to/mbeddr.core/code, save the setting and you are done.

This should give you a working basic mbeddr setup, to verify that it works you can open the tutorial project from the mbeddr page and run some of the samples.

Note: You will not be able to run any of the formal verification stuff, since we did not install them yet. I will extend the guide in the near future to cover that too.