Tips'n'Tricks


Binding of Variables in Simulator

A CPN variable which cannot be bound from the values of input token, guard, or output patterns can be expanded - all values are tried out - if at least one of the following conditions are fulfilled:
  1. The color set of the variable has few values.
  2. The color set of the variable has declare clause mentioning ms.
re 1) A color set has few values if it has less than the value of the reference called CPN'ExpandLimit. The default value is 100. If you want another limit, e.g., 500, add the following statement in the beginning of your global declaration node:
  CPN'ExpandLimit := 500;

re 2) If the color set has a finite number of elements you can declare ms. The following color sets are finite:

  • unit
  • bool
  • index
  • enumeration
  • integer with min..max
  • string with ch_min..ch_max and min_len..max_len
  • products built from limited color sets
  • records built from limited color sets
  • unions built from limited color sets
  • list built from a limited color set and with limit on length
  • subset either built from a limited color set or with enumeration of all elements


Random Value Generators

There exist several techniques for getting random values in Design/CPN.
  1. Variables not bound in the guard, input arcs and output in code segments are given random values in the simulator determined by the colour sets of the variables. Example:
      color R = int with 1..10 declare ms ;
      var rand:R ;
    
               rand
      [T1] -------------> (P1)  P1 is of colour set R
    When the transition T1 occurs the place P1 will get a random integer between 1 and 10.

    The part declare ms is for this case used to ensure that Design/CPN regards R as a "small" set. Design/CPN can only determine random values of colour sets which are small.

  2. The declaration of R above has a side effect: The function named ran'R becomes available. The expression:
      ran'R()
    is then used to get a random integer between 1 and 10. (If you colour set is called FOO instead, then the function name ran'FOO becomes available.)
  3. There are two built-in random number generators in Design/CPN:
      CPN'randint  : int  * int  -> int
      CPN'randreal : real * real -> real
    Example use:
      CPN'randint(1,10)
    returns a random integer number in the range [1,10].
      CPN'randreal(1.0,10.0);
    returns a random number (floating point) in the range [1,10[ (10 not included).

How to Speed Up Your Syntax Check

When you change a declaration in the Global Declaration Node of a CPN diagram, Design/CPN 3.0 must recheck and reswitch the entire diagram from scratch. To save time you can use the Temporary Declaration Node and the ML Evaluate command.

The Temporary Declaration Node allow you to add new declarations to your CPN diagram without rechecking and reswitching the entire diagram. This is, e.g., very useful if you want to add a NEW colour set or a variable/function which you forgot. When the contents of the Temporary Declaration Node becomes to large it is time to move the most stable declarations into the Global Declaration Node. See also Trouble with the Declaration Nodes on the Common Problems page.

The ML Evaluate command allows you to evaluate Standard ML code in your diagram. The Standard ML code is usually written in an auxiliary node, but it ML Evaluate can also be used for arc expressions and guards (provided that you give the variables a value before you evaluate the net inscriptions). ML Evaluate is excellent for making complex ML code. It allows you to syntax check and debug ML code, without changing the Global Declaration Node, i.e., without making a full recheck and reswitch. This means that you can save a lot of time, because errors can be detected and corrected in seconds. Like Temporary Declaration Nodes you should use ML Evaluate with great care. NEVER use ML Evaluate to change an existing declaration (Design/CPN will NOT recheck and reswitch the net inscriptions where the declaration is used). If you make a syntax check from scratch, the results of using ML Evaluate is lost, and hence you must reevaluate the declarations which you made by means of ML Evaluate

It can often be difficult, even for experienced users, to find when it is safe to use Temporary Declaration Nodes and the ML Evaluate command. If your model starts to "behave strange", we recommend to move everything from the Temporary Declaration Node and everything which you declared by means of ML Evaluate into the Global Declaration Node and make a new syntax check and switch -- from scratch.


Overlay Graphics

It is possible to create a special diagram page, the so called Master page, where its graphics will appear on every printed page. Furthermore, there are simple macros for printing out the page name, page number, date, and time on each page. This is useful if you wish to have, e.g., a fancy header on every page printed. (The graphics on the Master page is not visible in the Design/CPN editor, only on printed pages.)

A Master page is created as follows:

  • Create a New Page (the Page menu).
  • Invoke the Page Attributes dialog (the Set menu).
  • In the dialog set the page number to 10000.
  • Set the page name to, e.g., "Master", and close the dialog.
You can now create your fancy overlay graphics on the Master page. Macros are typically used as follows: On the Master page create 4 Labels (the Aux menu) each with the following text respectively (you can have at most one macro in each graphical object):
  Page <<p>>
  Name <<n>>
  Date <<d>>
  Time <<t>>
On each printed page you will now see the current page number, page name, date, and timestamp. Note that on the Mac "<<" and ">>" are each one single character.

The Backup Diagram Files

When Design/CPN saves a diagram, a number of backup files are created in order to handle fatal situations during a save (e.g., crash or full disk). Assume that you save a diagram and name it "myDiagram". When save is done, successful or not, then the following situations are possible:

Files you seeExplanationRecovery files
myDiagram
myDiagram~
myDiagram.DB
myDiagram.DB~
Successful savemyDiagram~
myDiagram.BD~
myDiagram
myDiagram~
myDiagram.DB~
Error during save of .DB filemyDiagram~
myDiagram.DB~
myDiagram
myDiagram~
myDiagram.DB
Error before save of .DB filemyDiagram~
myDiagram.DB
myDiagram.tmp
myDiagram.DB
Error during save of diagram (graphics part)myDiagram.tmp
myDiagram.DB

The table column "Recovery files" shows which files you need in order to recover the old version of the diagram. E.g., if recovery is "myDiagram.tmp" and "myDiagram.DB" (last row), then you need to rename "myDiagram.tmp" to "myDiagram" and "myDiagram.DB" to "myDiagram.DB" (i.e., do nothing), because a diagram consists of a graphics part ("myDiagram") and a database part ("myDiagram.DB").


Updated

How to Produce PostScript Output

This tip is for UNIX users. On the Macintosh you just need check the option "Print to File" which is standard in most print dialogs.

Instead of printing your diagram on a PostScript printer you can create a PostScript file of your diagram. The shell environment variable called DesignPrintCommand determines which command that the PostScript output file name should be prefixed with. E.g., try the following. In a shell set the DesignPrintCommand environment variable (assuming that your home directory is "/users/foo"):

  setenv DesignPrintCommand "cat > /users/foo/CPNPrintOutput.ps"
From the same shell window start Design/CPN (while running, the tool does not identify changes of the environment variables). The next time you print, the output will be saved in the file CPNPrintOutput.ps in your home directory.

You may wish to look through the File->Page Setup dialog in order to setup some printing parameters. If you just want to print one diagram page, then uncheck the option Print Hierarchy Page.

If you have "printed" only one diagram page, then the PostScript file can be converted to Encapsulated Postscript by means of many different utilities. One example is to use the ps2epsi utility. Another possibility is to calculate the bounding box of your PostScript output (by some program or by hand using a ruler) and then insert the bounding box information into the second line of the PostScript file. (See your PostScript language reference manual for more information.)

Be careful that your diagram graphics is within the page boundary. Otherwise parts of your figure will be clipped (in normal output form).


Arbitrary ML Data-types as Colour Sets

The colour set declarations of Design/CPN, as described in the reference manual, are a subset of Standard ML's types. Design/CPN does not support directly in the syntax, e.g., recursive types. However, if the user provides a few extra functions, it is possible to declare such advanced colour sets. In the following we describe what is required in order to use the full expressiveness of the Standard ML type declaration system.

User declared advanced colour sets must fulfil the following requirements:

  1. Equality, "=", must be meaningful for the equivalent Standard ML type. Thus, e.g., function types and abstract data-types cannot be used as colour sets.
  2. The user must declare the following three functions (AUtype is the name of the user declared type):
    • ran : unit -> AUtype
    • lt : AUtype * AUtype -> bool
    • mkst_col : AUtype -> string
ran is used in interactive simulations when binding free variables of a type with many elements. If neither free variables of the type nor interactive simulations are used, then just define a dummy ran function - remember that it still needs to be of the type above.

lt is used to order elements of multi sets,e.g., before showing the current marking of a place.

mkst_col is used to map a value to a string. This is necessary for showing the current marking in the simulator. It is also used inside the "Bind" and "Change Marking" dialogs, and this means that it is important that the string representation of a value, returned by the function, will evaluate to the same value when it is given to the ML system.

Below find an example where the user declares an advanced colour set, which is the recursive tree type, Tree:

Contents of the global declaration node:

color I = int;
color elm = record value1 :  I * value2 : I
            declare ran, lt, mkst_col;

datatype T = Branch of T * T | Leaf of elm | Cut_off;

color one_to_three = int with 1..3
                     declare ran;

fun my_random ()
    = case (ran'one_to_three ())
        of 1 => Branch (my_random (), my_random ())
         | 2 => Leaf (ran'elm ())
         | _ => Cut_off;

fun my_lt (Branch (t1,t2), Branch (t3,t4))
    = if my_lt (t1,t3) then true
      else ((t1 = t3) andalso (my_lt (t2,t4)))
  | my_lt (Branch _, _) = false
  | my_lt (_, Branch _) = true
  | my_lt (Leaf e1, Leaf e2) = lt'elm (e1,e2)
  | my_lt (Leaf _, _) = false
  | my_lt (_, Leaf _) = true
  | my_lt (Cut_off, Cut_off) = false;

fun my_mkst_col (Branch (t1,t2))
    = "Branch ("^(my_mkst_col t1)^", "^(my_mkst_col t2)^")"
  | my_mkst_col (Leaf e1) = "Leaf ("^(mkst_col'elm e1)^")"
  | my_mkst_col Cut_off   = "Cut_off";

color Tree = T with (my_lt, my_random, my_mkst_col);

var x,y,z : Tree;

The colour set Tree can now be used in colour set regions in Design/CPN diagrams.

If a declare clause is added as:

  color Tree = T with (my_lt, my_random, my_mkst_col)
               declare ran,lt,mkst_col,mkst_ms;
then the following function names will be declared by Design/CPN and made available for the user:
  ran'Tree
  lt'Tree
  mkst_col'Tree
  mkst_ms'Tree

CPN FAM Memory Usage on Macintosh

As there is no initial dynamic memory allocation for applications on the Macintosh it can sometimes be hard to determine the most appropriate application start-up memory size.

For the Design/CPN ML engine on the Macintosh - the CPN FAM - it can especially be difficult as its memory usage is highly dependent on the complexity of the model. Often you may see the message "CPN FAM could not be started" or experience surprisingly slow simulations.

If you have succeeded in starting the ML engine, e.g., for syntax checking or simulation, then you can look at the output from the CPN FAM in order to get indications about whether the CPN FAM needs more memory. To see this output you should bring the CPN FAM window to the front. Observer text that looks approximately as follows (generated by the garbage collector):

[GC: [Using compacting GC] Marking...relocating...moving...72%
(2186/3016 Kbytes) used.
Space used (in bytes)
States 64, Processes 48, Rec(1) 467656, Rec(2) 400404,
Rec(N) 548784, Strings/Numbers 221592, Variants 177344,
Bytecodes 327228, Stacks 96000]
In this example the interesting number is "72%". If you see this number go above 90% it is a good indication of that you need to increase the memory size of the CPN FAM.

In general we recommend that the CPN FAM should have at least 4-5000 kb - and more if available - typically 6-8000 kb.


Identification of Page Instances

In the CPN book of Jensen (volume 1) it is described how to use an ML-function, inst(), in order to identify a page instance. Unfortunately, this function is not implemented in Design/CPN.

As an alternative, one can make a socket place on the super-page which contains an integer value. On the sub-page, the corresponding port node can be used to make a copy of the integer token, increment the value, and put the integer token back again on the port place. In this way, each instance of the sub-page will get a unique identifier value for further use.

Instead of the port-socket technique, one can also use a global (or page) fusion place for the same purpose. One should just ensure that the page instances in question can access and update a shared value atomically.


Deleting Arc-selection Points

You probably already know that arc-selection points can be added by dragging a selection point on straight segments. (This holds also for connectors, polygons, and lines.) But how is an arc-selection point deleted? With the mouse pointer, select an arc-selection point and keep the button down while pressing the space bar on the keyboard. Then the currently selected point will disappear.

How to Get Fresh Defaults

Should you need to reset you defaults you can always erase the file called ".CPNSettings" in your home directory (or system folder on the Mac). The next time you start Design/CPN you'll see the dialog message ".CPNSettings file not found (or unreadable). Create new .CPNSettings file?". If you press "OK" the tool will create a fresh CPNSettings file with reset defaults.

Note that existing diagrams will not automatically inherit new defaults. If you need to transfer these defaults to your diagram you also have to load your diagram and then invoke Copy Defaults via the Set menu and then select System-> Diagram.


Reading multi-sets from a file

Try the following example for reading multi-set parameters from a file (see also the Simple Protocol example):
 Global declaration:

  color MYCOLOR=with aa | bb | cc declare input_ms;
  var result:MYCOLOR ms;

 Code segment:

  output result;
  action
  let
   val infile=TextIO.openIn(DSFile_NameDialog
                            {prompt="Select file",
                             okButtonLabel="Load",
                             path="",
                             writeBox=false});
   val message=input_ms'MYCOLOR(infile)
  in
   TextIO.closeIn(infile);
   message
  end handle _ => empty;

 Input file:

  % This is a comment which is ignored
  1`aa++
  2`bb++
  5`cc
There are also functions for output of colours (token values) and multi-sets. For more information see the online Reference Manual on page 37-9, 41-5, 41-8, and 41-9.

Last modified: Tue Jun 3 09:57:10 2003 -- Design/CPN Online admin