Real Time and Transition Priorities in CPN Tools

This post has 122 words. Reading it will take less than one minute.

Some months ago, Eric Verbeek and I worked together on adding a couple features to CPN Tools with the primary focus of getting Eric started with maintaining CPN Tools.  Today I released a new version of CPN Tools with those features, namely real time stamps and transition priorities.  See the demo here:

Note that whenever I mention resources p and q in the first example, I really mean processes p and q. The resources are named R, S, and T.

Just to make it painfully clear: Real time: real, wall clock time/star trek time/bullshit time: fake, transition priorities: real.  If you are interested, below is the code I used to generate the fake time stamps using Access/CPN:

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import dk.au.daimi.ascoveco.cpn.engine.highlevel.HighLevelSimulator;
import dk.au.daimi.ascoveco.cpn.engine.proxy.ProxyDaemon;
import dk.au.daimi.ascoveco.cpn.engine.proxy.ProxySimulator;

public class SillyTime {
	private static final String[] approx = new String[] { "1", "2",
	"Threeish", "Four, I think", "Five or six",
	"Not too many", "Not too many", "Think it's about ten",
	"Think it's about ten", "Think it's about ten",
	"Lost count several steps ago",
	"Lost count several steps ago",
	"Lost count several steps ago", "Lets say it's 37",
	"Lets say it's 37", "Fifty?", "Fifty?",
	"Over nine thousaaand!" };

	public static void main(final String[] args) throws Exception {
		System.out.println("Starting proxy");
		final ProxyDaemon pd = ProxyDaemon.getDefaultInstance();
		while (true) {
			System.out.println("Waiting for CPN Tools");
			final ProxySimulator ps = pd.getNext();
			System.out.println("Waiting for syntax check");
			while (ps.getPetriNet() == null) Thread.sleep(500);
			new Thread() {
				public void run() {
					final HighLevelSimulator simulator = ps.getSimulator();
					try {
						simulator.evaluate("val SILLY'step = ref \"nothing\";"
						+ "fun setWallClockTime() = (SILLY'step := \"wall\");"
						+ "fun setInternationalTime() = (SILLY'step := \"star\");"
						+ "fun setApproximateTime() = (SILLY'step := \"approx\")");
						int last = -1, count = 0;
						final Random r = new Random();
						while (true) {
							final String result = simulator.evaluate("!SILLY'step");
							int level = last;
							if (result.indexOf("wall") >= 0) level = 1;
							if (result.indexOf("star") >= 0) level = 2;
							if (result.indexOf("approx") >= 0) level = 3;
							count++;
							if (level != last) count = 0;
							last = level;
							if (level == 1) {
								simulator.execute();
								final SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
								final String s = formatter.format(new Date());
								simulator.refreshViews(s + " CEDT", simulator.getStep());
							}
							if (level == 2) {
								simulator.execute();
								final int main = r.nextInt(10000) + 40000;
								final int minor = r.nextInt(10);
								simulator.refreshViews("Stardate " + main + "." + minor, simulator.getStep());
							}
							if (level == 3) {
								simulator.execute();
								simulator.refreshViews(
									approx[Math.min(count, approx.length - 1)], simulator.getStep());
								}
								Thread.sleep(1000);
							}
						} catch (final Exception e) {
							}
						}
					}.start();
				}
			}
		}
	}
}

Time person of the year 2006, Nobel Peace Prize winner 2012.

8 thoughts on “Real Time and Transition Priorities in CPN Tools

  1. Thank you for this great tutorial.

    Id o have a question though :

    How to get the current time when we have have real time stamp?

    Actually time() returns the Int type.

  2. With the new version of CPN (the latest version available on the Download page), I tried to implement the proxy simulator. However, it results in some sort of blocking queue and CPN tools seems to freeze as soon as I load any model.

    I tried searching for the port in the default and the CPN Tools is configured to run on the default port.

    I would like to access the GUI, and change the marking of a place during simulation.

    1. Hi, if you refer to the ProxySimulator of Access/CPN, this is not compatible with CPN Tools 4 as the protocol has changed. You can instead use simulator extensions and the protocol debugger to get much of the same functionality.

      1. Hi Micheal,

        Thanks for your input. It really worked out.

        My use case is: at the time of simulation I detect when a transition has occurred and then I modify the markings of few places. This I could achieve using the simulator extension and the debugger protocol.

        However, when I run the state space tool, (I assume that it calculates the states by running an ML function), when I use addSubscription, I don’t get any calls to my handle method.

        I was wondering if I could modify the markings during state space generation? I need to explicitly update places on the run time.

        1. Using the addSubscription command I could get the control only when I start drawing the graph, with the display predecessor or successor tool. However, but when the state space tool is run and the markings are calculated, is there some method through which I could obtain the marking and modify it in my extension?

  3. Hi Micheal,

    Thanks for your reply. Since extensions don’t work with state-space analysis, I was wondering to create my own extension, which will calculate the state spaces. I tried to create an HighLevelSimulator (since CPN Tools 4 don’t support proxy simulator) and using DFS algorithm, I am looking to create the state space of the given model.

    In my model, I have transitions which use Comms/CPN API and in the code segment of the transition, I define a variable which receives an answer from Comms/CPN and appears only on the outgoing arc of the transition. However, when I try to execute the transition using the HighLevelSimulator executeAndGet(Instance) function, it doesn’t evaluate the code segment of the transition and returns an empty binding.
    I also tried to use the evaluate function with the provided transition code, but it throws an error stating that the variable in the transition code segment is not bounded.
    I was wondering if there is some way to execute the transition (using the HighLevel Simulator) along with its code evaluation.

    1. Hi Micheal,

      Is there a way to execute a transition (along with its code segment) from the CPN Tools extension and get the bindings?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.