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:
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.
This page sets out the 3 steps involved in converting from Vensim MDL syntax to Simile Prolog syntax.
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).
As mentioned elsewhere, the conversion from Vensim's MDL language to Simile's Prolog language involves 3 separate steps:
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:
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:
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.