15.Using CSharp - larics/Petri.Net GitHub Wiki

Don't like Python ? No problem. Starting from version 2.1.5626.11484 Petri.NET simulator allow you to using of C# programming language instead of Python. To use C# simply type your C# code in code editor.

Petri.NET simulator use C# code on following way:

  • Your first line must start with a //C# (using this simulator will know that you are using C# instead of Python)
  • Before each step, a C# function public int Step(int k) will be called, where k is time information
  • At the end of simulation, a C# function public void Reset() will be called. Existence of this function isn’t mandatory and simulation will work well with or without it.
  • To access place you must use function FindPlace() which will return object. You can't access places using direct variable like in Python.

So, this is OK:

//C#
// This is OK !!!!
public void Step(int k)
{
    Place p = FindPlace("P2");
    Print("P2 has " + p.Tokens + " tokens");
    if(p.Tokens < 5)
      p.Tokens = 10;
}

and this isn't OK

//C#
// This is wrong !!!!
public void Step(int k)
{
    Print("P2 has " + P2 + " tokens");
    if(P2 < 5)
      P2 = 10;
}

How to use namespace ?

In normal C# code used namespace will be placed on the top of the file. Here you have two alternatives. First is to type entire namespace like we did with System.IO.Ports in sample below:

//C#
System.IO.Ports.SerialPort sp = null;
public void Step(int k)
{
    if(sp == null)
    {
       sp = new System.IO.Ports.SerialPort();
       sp.PortName = "COM1";
       sp.BaudRate = 9600;
       sp.Parity = System.IO.Ports.Parity.None;
       sp.DataBits = 8;
       sp.StopBits = System.IO.Ports.StopBits.One;
       sp.Open();
       Print("Open COM port\n");
    } 

    Place p = FindPlace("P2");
    sp.Write("P2 has " + p.Tokens + " tokens\r\n");
}

public void Reset()
{
    if(sp != null && sp.IsOpen)
    {
       Print("Close com port\n");
       sp.Close();
       sp = null;
    }
}

Second is to declare used namespace using line that start with "//using", as shown in sample below:

//C#
//using System.IO.Ports

SerialPort sp = null;
public void Step(int k)
{
    if(sp == null)
    {
       sp = new SerialPort();
       sp.PortName = "COM1";
       sp.BaudRate = 9600;
       sp.Parity = Parity.None;
       sp.DataBits = 8;
       sp.StopBits = StopBits.One;
       sp.Open();
       Print("Open COM port\n");
    } 

    Place p = FindPlace("P2");
    sp.Write("P2 has " + p.Tokens + " tokens\r\n");
}

public void Reset()
{
    if(sp != null && sp.IsOpen)
    {
       Print("Close com port\n");
       sp.Close();
       sp = null;
    }
}

How to add DLL as used reference ?

Generated C# script will reference System.dll and System.Windows.Forms.dll class libraries. This mean you can use classes defined in those libraries without any problem. However, problem occur if you want to use some class from some library that isn't referenced. To reference some other library simple use line with "//ref:" as shown in sample below:

//C#
//ref:C:\library\MyDll.dll

public void Step(int k)
{
   ClassFromMyDll c = new ClassFromMyDll ();
   c.DoSomething();  
}

Note that if you want to reference some standard library from GAC you don't need to specify a path. Notice that custom libraries must be compiled to use .NET 2.0