Vensim to Simile conversion using XSugar and Prolog

The main aim of this project is to make it possible to run Vensim models in Simile.   This involves writing a converter from Vensim syntax to Simile syntax.   A subsidiary aim is develop the converter so that it can work in the reverse direction: i.e. that Simile models (at least, that subset of models which conform to Vensim's limited expressiveness) can be converted into Vensim syntax using the same converter run backwards.

This method for converting Vensim MDL files into Simile Prolog is based on 3 steps:

  1. converting the Vensim MDL to a corresponding XML format ("Vensim XML") using an XSugar stylesheet;
  2. converting the Vensim XML into a corresponding Prolog format ("Vensim Prolog") using an XSugar stylesheet;
  3. converting the Vensim Prolog into Simile Prolog using a Prolog program.

To most readers this will seem like a fairly complicated way of undertaking this task, all the more so because of unfamiliarity with the two technologies used (XSugar and Prolog).  The aim of this section is to explain how this approach works, and why it was adopted.

 

Description of the conversion process

This page sets out the 3 steps involved in converting from Vensim MDL syntax to Simile Prolog syntax.

Converting from Vensim Prolog to Simile Prolog

This oage...

Converting from Vensim MDL to Vensim XML

This page...

Converting from Vensim XML to Vensim Prolog

This page...

Description of the technologies used (XSugar and Prolog)

The Vensim-to-Simile conversion process makes use of two technologies: XSugar and Prolog.

XSugar (http://www.brics.dk/xsugar/) has been developed to enable a plain-text (non-XML) syntax to be transformed into an XML syntax, and vice-versa.   It does this by enabling you to specify a set of grammar rules, which define the language concerned.  The special thing about XSugar is that each rule has tow matching parts: the rule for the non-XML syntax and the rule for the XML syntax.  This enables the XSugar rule-set to be used in either direction: i.e. converting from plain text to XML or from XML to plain text.  

In the present context, the use of XSugar opens up the possibility that a single converter can be used to convert from Vensim to Simile or the other way around, without havingto write a separate converter for each direction.

Prolog (Programming in Logic) is a logic-based programming language developed in the 1970s.   Unlike most programming languages, a Prolog program does not consist of a set of instructions to the computer.  Rather, a Prolog program consists of facts and rules, each of which is 'true' in its own right.  The equivalent of 'running  program' is to ask the Prolog interpreter to find a solution to a problem, by searching through all the facts and rules it has at its disposal.  

In the epresent context, the Prolog program can define complex relationships between one data structure (the model in vensim's data model) and another (the same model in Simile's data structure).  

Prolog

This page describes Prolog in more detail.

XSugar

This page explains XSugar in more detail.

Justification of the approach

As mentioned elsewhere, the conversion from Vensim's MDL language to Simile's Prolog language involves 3 separate steps:

  1. conversion from Vensim's MDL to a corresponding XML (Vensim XML);
  2. conversion of Vensim XML to a corresponding Prolog (Vensim Prolog);
  3. conversion of Vensim Prolog to Simile Prolog.

This page sets out that thinkingthat led to this approach.

The conversion between other model-representation lanugaes and Simile operates at two levels:  

First, there are fairly minor syntactic dfferences.  For example, in Vensim a variable name (as used in an equation) may contain spaces; whereas in Simile the variable name in the equation cannot contain spaces, and these are replaced by underscores when the label provided by the user does contain spaces.   This type of conversion can usually be done on a line-by-line basis.

Second, there are much more substantial structural differences in how the models are represented.   In Vensim, the mathematical part of the model is eqpresented by a set of equation statements, followed by one or more "views", which give information defining how the model or part of it is actually displayed.   In Simile, the mathematical and diagrammatic information is represented by two statements which immediately follow each other, with a separate link statement to join them together.   This type of conversion is much more complex.

So, let's start with the hardest part first.  What is an appropriate computing approach to use to convert from one symbolic representation to another, when both contain the same information but arranged in quite different ways?   I decided to use the logic-programming language Prolog ("Programming in Logic") for this task.   I hesitate to say "it's the best", because that implies that I have thoroughly researched the field and chosen it from various competing approaches, which I haven't.  However, I can say that it is very well suited to this task, for a number of reasons:

  1. It is a language specifically designed for reasoning with symbolically-represented information, which is what a model representation is, whether it's a Vensim or Simile model.
  2. It is a declarative language. You don't tell the computer what operations to perform, as you do with a conventional programming language.  Rather, you make statements about relationships, in the form of rules, and it's up to the prolog interpreter to solve a problem by using these rules.  In the present context, 'solving a problem' means finding a Simile representation which corresponds to teh Vensim representation.
  3. One benefit of the previous feature is that a Prolog program is blissfully free of many of the procedural structures you need in a conventional programming language - for example, loops to iterate over many possibilities.   This makes the Prolog program much more light-weight, and therefore easier ti write and maintain.
  4. Any part of the Prolog program can be tested by itself.   The full program for transforming from one language to another will consist of many rules.  However, any one rule can be tested to see if it's working - you do not need to write special code to exercise some small part of the program, as you would normally need to do.
  5. Finally, because Prolog is a declarative language, it has the potential for writing the transformation rules so that they can be used in either direction - from Vensim to Simile or from Simile to Vensim.   This has not been done in the present Prolog context (though it has for the XSugar parts - see below), but the potential is there.   This would mean that the rule set has to be written only once, ratherthan writing two totally separate converters.

One implication of deciding to use Prolog for the main structural conversion is that the Vensim model must be represented in Prolog syntax.   It doesn't matter what Prolog syntax, just anything that the Prolog interpreter can interpret as valid Prolog facts.   However, Vensim's MDL format is not in Prolog syntax.  It is therefore necessary to convert from the MDL into Prolog.   Unlike the structural conversion considered above, this can be on a direct, one-to-one basis, merely marking up one syntax (MDL) with the conventions of another (Prolog).   What is the best way of doing this?

This is where XSugar comes in.  XSugar was specifically designed to allow for the conversion between one syntax and another.  Moreover, it has been designed so that (subject to certain conditions) the conversion is reversible: the same rule-set can be used to convert in either direction.   So it seems ideally suited to our needs. 

The problem is that XSugar is specifically designed to convert between a non-XML ("plain text") syntax and XML  For example, a simple XSugar rule could specify the mapping between

Charles Darwin; The Origin of Species

and

<book><author>Charles Darwin></author><title<The Origin of Species</title></book>

So, if we want to go from Vensim's MDL to a Vensim model in Prolog, we would need two XSugar conversions:

  • from Vensim MDL to an XML syntax for Vensim models ("Vensim XML); and
  • from Vensim XML to a Prolog syntax for Vensim models ("Vensim Prolog") (using an XSugar stylesheet in the reverse direction, i.e. from XML to Prolog text).

This sounds arduous.   In fact, once you have the first stylesheet, it is pretty easy to make the second, because it will have the same number of conversion rules, and the XML part is the same in both cases.   So it's actually a small part of the whole exercise.

The complete conversion process thus consists of three steps:

Vensim MDL --> Vensim XML --> Vensim Prolog --> Simile Prolog.

of which the first two have guaranteed reversibility.  If one subsequently wants to convert in the reverse direction, one need only write the Simile Prolog --> Vensim Prolog converter; or re-write the Vensim Prolog --> Simile Prolog converter so that it too is reversible.