|
Loading
|
||||
Monitored dining philosophers exampleThis is an example that shows how to use user-defined monitors to establish communication between CPN Tools and an applet via external communication with Comms/CPN. The net is a variation of the net for the Dining philosophers example. In the following it is assumed that you are familiar with the net for the Dining philosophers example. In this net the philosophers pick up one chopstick at a time, whereas in the Dining philosophers example net, the philosophers pick up both the left and right chopsticks at the same time. This net can be found in <cpntoolsdir>/Samples/DiningPhilosophers/DiningPhilosophers_COMM_Mon.cpn where <cpntoolsdir> is the directory in which CPN Tools is installed. The net can also be downloaded here. The appletA Java applet has been created to show how Comms/CPN can be used to establish communication between CPN Tools and an external process. This applet provides an alternative graphical representation of the markings of the net for the dining philosophers system. Each time a transition occurs in the net, the graphics shown by the applet are updated. In the figure below, philosopher number 5 has both chopsticks and is eating, while philosopher number 2 has only picked up the right chopstick. The files for the applet can be found in <cpntoolsdir>/Samples/DiningPhilosophers/applet, or they can be downloaded from the help page for external communication with Comms/CPN. The Java classes for the applet will not be described here. Follow these steps to run the applet with the net described on this page:
Declarations for communication
There are two declarations that are used when communicating with the applet. The function Communication using monitors and Comms/CPN
The two user-defined monitors for this net are named ''AppletComm'' monitor
The Initialization function
The initialization function is called once before a simulation starts. It checks to see if a communication connection exists between CPN Tools and the applet. If there is a connection, then the connection is closed, and the reference variable
fun init () =
if !connected = true
then (ConnManagementLayer.closeConnection("Conn 1");
connected := false)
else ()
Predicate functionThe predicate function is invoked after every step in a simulation, and it returns true each time one of the transitions in the net occurs.
fun pred (bindelem) =
let
fun predBindElem (System'LeftFirst (1, {p})) = true
| predBindElem (System'LeftScnd (1, {p})) = true
| predBindElem (System'PutDown (1, {p})) = true
| predBindElem (System'RightFirst (1, {p})) = true
| predBindElem (System'RightScnd (1, {p})) = true
| predBindElem _ = false
in
predBindElem bindelem
end
Observation function
The observation function is invoked each time the predicate function returns true, and it returns a pair of strings. The first value in the pair is a string representation of the variable
fun obs (bindelem) =
let
fun obsBindElem (System'LeftFirst (1, {p})) = (PH.mkstr(p),"gotleft")
| obsBindElem (System'LeftScnd (1, {p})) = (PH.mkstr(p),"eat")
| obsBindElem (System'PutDown (1, {p})) = (PH.mkstr(p),"think")
| obsBindElem (System'RightFirst (1, {p})) = (PH.mkstr(p),"gotright")
| obsBindElem (System'RightScnd (1, {p})) = (PH.mkstr(p),"eat")
| obsBindElem _ = ("","")
in
obsBindElem bindelem
end
Action function
Whenever the predicate function returns true, the value that is returned by the observation function will be passed to the action function. The action function will check whether a connection to the applet has been established. If there is no connection, then
fun action (s1,s2) =
(if not(!connected)
then (ConnManagementLayer.acceptConnection("Conn 1",9000);
connected:=true)
else ();
send_to_applet(s1,s2))
Stop function
The stop function is invoked when simulation stop criteria are fulfilled. If there is still a connection between CPN Tools and the applet, then the connection will be closed, and the reference variable
fun stop () =
if !connected = true
then (ConnManagementLayer.closeConnection("Conn 1");
connected := false)
else ()
''Deadlock'' monitor
The
All philosophers have their left chopstick if the marking of place
The Initialization functionThe initialization function for this monitor does nothing.
fun init (System'GotLeft_1_mark : PH ms,
System'GotRight_1_mark : PH ms) = ()
Predicate function
The predicate function checks to see whether a dead marking has been reached. The predicate function is invoked when either the
The
fun pred (bindelem,
System'GotLeft_1_mark : PH ms,
System'GotRight_1_mark : PH ms) =
let
fun predBindElem (System'LeftFirst (1, {p})) =
System'GotLeft_1_mark == PH.all()
| predBindElem (System'RightFirst (1, {p})) =
System'GotRight_1_mark == PH.all()
| predBindElem _ = false
in
predBindElem bindelem
end
Observation functionThe observation function for this monitor does nothing, and it returns a unit value.
fun obs (bindelem,
System'GotLeft_1_mark : PH ms,
System'GotRight_1_mark : PH ms) = ()
Action function
The action function is used to close a connection between CPN Tools and the applet when a dead marking has been reached. When the action function is invoked, it will send the string “deadlocked” to the applet, it will close the connection to the applet, and it will set the reference variable
fun action (observedval) =
(ConnManagementLayer.send("Conn 1","deadlocked",stringEncode);
ConnManagementLayer.closeConnection("Conn 1");
connected := false);
Stop functionThe stop function for this monitor does nothing.
fun stop (System'GotLeft_1_mark : PH ms,
System'GotRight_1_mark : PH ms) = ()
|
||||