• Ingen resultater fundet

Implementation in Design/CPN

Web-Based Interfaces for Simulation of Coloured Petri Net Models

6.4 Implementation in Design/CPN

Design/CPN [27] is a widely used tool supporting editing, simulation and ver-ification of CPN models. In this paper, Design/CPN is used to prove the usefulness of the CGI concept for simulating CPN models via a web browser.

In this section we describe some of the design considerations and facilities that are implementation specific for Design/CPN.

According to the description in Sect. 6.3, a CGI script is nothing more than a program that can be executed from a web page and then dynamically produces a new web page. A few modifications are made to Design/CPN in order to make it possible to turn the simulator of Design/CPN into a CGI script, or rather to drive the Design/CPN simulator from a CGI script. To understand why these changes need to be done, we need to describe some of the architecture of the Design/CPN tool.

Design/CPN is divided into two parts: one part implementing the GUI, and another part containing a simulation engine for simulating a CPN model. When a CPN expert has created a CPN model (see Fig. 6.9), simulator code can be generated containing the simulation engine and some model dependent code.

This code contains everything needed to simulate that specific CPN model.

Figure 6.9 differs a little from Fig. 6.1 in that a simulator is generated from the CPN model before being able to perform the actual simulation. However, when the simulator has been generated once and for all, it may be used to run any number of simulations.

In the context of CGI scripts only the simulator of Design/CPN is of in-terest. The reason is that it contains the entire executable simulator which is generated from the CPN model created in the editor of Design/CPN. Thus, the Design/CPN simulator is the only part of Design/CPN that needs to be used when creating a CGI script.

6.4.1 Disabling the GUI of Design/CPN

Due to the fact that the simulator and the graphical user interface (GUI) of Design/CPN communicate with each other, the Standard ML (SML) [83] func-tions contained in the simulator for updating the GUI of Design/CPN should

6.4. Implementation in Design/CPN 53

Create CPN Model

CPN Model

Simulate CPN Model

Simulation Results Design/CPN

GUI

Set Initial Conditions

Initialised Simulator Generate Simulator

Design/CPN Simulator

Show Results

Start

Figure 6.9: Design/CPN approach for creating and simulating CPN models.

be modified in order to not update the GUI while simulating. The reason for this modification is that the GUI of Design/CPN is not present in a CGI script. Only the code constituting the simulator and user-defined functions is contained in a CGI script. Remember that a CGI script is invoked from a form in a browser, while the CGI script itself is running on the web server. Therefore, the web page shown by the web browser can be considered to be the GUI of the CGI script.

6.4.2 Creating CGI Scripts

Another modification of Design/CPN makes it possible to save a CGI script in an executable file. The executable file will contain the entire simulator code for the CPN model and a user-defined function. The simulator code is model dependent SML code which is automatically generated by Design/CPN. This code makes it possible to simulate the CPN model. The user-defined function will be a batch script, as described in Sect. 6.3. Batch scripts in Design/CPN are written in SML.

An example of a batch script can be found in Fig. 6.10. The function getValOfFieldreads the value that the user has typed into the form in the field

fun batch script( , ) =

let fun parse form input() =

{diskField=getValOfField"disk",. . .}; funcycle script () =

(calculate initial marking();

init state(); (* Initialise state of the simulator *) simulate();(* Run the simulation *)

save results();

if not finished()then cycle script () else ());

in

(parse form input ();

cycle script ()) end

Figure 6.10: A simple batch script.

Design/CPN simulator

Save Executable CGI script

Batch Script

CGI script Generate

simulator CPN model

Figure 6.11: Creating a CGI script from Design/CPN.

named by diskwhich is the name of the field with the title “Disk Capacity” in Fig. 6.3. The functionparse form inputis simplified here to only read the field disk. The user may want to do some calculations (calculate initial marking) before initialising the state of the simulator by means of the functioninit state.

The simulation is started using the functionsimulate. The functionsave results saves results in files and/or sends them to the web browser.

When the user has finished creating the batch script to be included in the CGI script, the actual CGI script can be generated. The CGI script is generated and saved in a file by simply invoking a SML function. The overall approach for creating a CGI script using Design/CPN is illustrated in Fig. 6.11. First the model is created and the simulator is generated using Design/CPN. Then a batch script is created by the CPN expert to control the actions of the CGI script. Finally, the CGI script containing the batch script and the Design/CPN simulator is generated and saved in a file.

6.4. Implementation in Design/CPN 55

funprint header n =

(print("<B><FONT SIZE=+2>Results from running "^

(Int.toString n)^

" simulations</FONT></B>"));

Figure 6.12: Print HTML code to a web browser.

6.4.3 High-level Functions

Due to the fact that SML is the language used for specifying CGI scripts which contain the Design/CPN simulator, or in particular that the batch script con-tained in the CGI script is written in SML, the CPN expert can use the full power of the language SML for getting input and producing output from the CGI script. This section describes some auxiliary and high-level functions that may be useful when creating batch scripts to be used in CGI scripts. In par-ticular we will focus on functions for reading input from HTML forms and generating HTML code as simulation output.

We will now describe how to read the parameters entered in the browser from the CGI script. When discussing the general structure of a batch script in Fig. 6.10, we introduced the SML functiongetValOfField. This function is very useful when reading a value that an application user has input in a form.

The function can be used to read any input field in a form, thus it is very general. The only parameter given to the function is the name of the field in the form. The function retrieves the input sent from the form and returns the value contained in the field.

When we want to produce an HTML document as output from running a CGI script, the CGI script needs to print some HTML code to standard output. SML provides the function printfor printing to standard output. By printing HTML code to standard output it is possible to create complex web pages. The HTML document may even include embedded graphics, so-called Java scripts [95], and Java applets which allow programs to be executed directly on the client machine holding the web browser. Figure 6.12 illustrates a simple SML function which prints some simple HTML code.

It may also be useful to save HTML documents as files. This is particularly useful if several data files and figures are generated by the CGI script. In this way it is possible to divide the results of running the CGI script over several HTML documents. The page printed directly to the browser could simply be a kind of index page for the rest of the HTML documents. In this way the results of executing the CGI script may be shown in a structured manner.

SML contains several functions for creating files and directories. Thus, it is immediately possible to save several HTML documents, to be referred to from other HTML documents.

Results or raw data obtained during or after a simulation can also be saved in files. Collecting data can, e.g. be done using the Design/CPN Performance Tool described in [58]. By printing HTML code which provides an URL link to the data file (see Fig. 6.13) to the web browser, the user can download the

fun print url(URL,description) =

(print ("<A HREF=\""^URL^"\">"^description^"</A>"));

print url ("www.daimi.au.dk/res1.txt",

"Results from simulation");

Figure 6.13: Print URL to a web browser.

fun gen gnuplots

{filenamesxtitles: (string *string) list, title: string,

xlabel : string, ylabel : string, dest filename : string, gnuplot path :string}

Figure 6.14: Interface to Gnuplot function.

result files when the CGI script (or simulation) ends. After downloading the file, the user can analyse the results produced using his favourite analysis tool.

As an alternative to downloading the raw data, the CGI script can post-process the data itself.

In Sect. 6.3we discussed generating graphics using external programs di-rectly from the CGI script. We said that Gnuplot [32] is a tool that can be controlled from a CGI script. To make it as easy as possible to create plots using Gnuplot, and because Gnuplot is a non-commercial product, we provide a SML function for plotting graphs using Gnuplot. Figure 6.14 contains the interface of the Gnuplot SML function. The function is simply called with a list of titles and file names of the raw data files and some textual information to be included in the graph. Finally, the user also needs to specify a destination file name where the plot is supposed to be saved. This SML function implies that users not familiar with Gnuplot are also able to easily create plots using the tool. The plot in Fig. 6.4 was generated using this function.