2.6: Petri nets

With the slightly updated version of the BarPort and BarNode example classes in Demo1, you can easily create Petri nets, such as:

The code to create this diagram is:

    public void MakePetriNet(GoView view) {
      view.StartTransaction();
      GoDocument doc = view.Document;
      doc.Clear();
      BarNode t41 = MakePetriTransition(doc, "T41", 200, 200, 300, 10);
      BarNode t43 = MakePetriTransition(doc, "T43", 410, 300, 30, 10);
      BarNode t44 = MakePetriTransition(doc, "T44", 200, 400, 300, 10);
      NetworkNode p41 = MakePetriNode(doc, "Begin",     null, t41,  350, 150);
      NetworkNode p42 = MakePetriNode(doc, "Turn on",    t41, t44,  250, 300);
      NetworkNode p43 = MakePetriNode(doc, "Move down",  t41, t43,  425, 250);
      NetworkNode p44 = MakePetriNode(doc, "Move up",    t43, t44,  425, 350);
      NetworkNode p45 = MakePetriNode(doc, "End",        t44, null, 350, 450);
      view.FinishTransaction("created Petri net");
    }
    public BarNode MakePetriTransition(GoDocument doc, String s, float x, float y, float w, float h) {
      BarNode t = new BarNode();
      t.Bounds = new RectangleF(x, y, w, h);
      t.LabelSpot = GoObject.MiddleLeft;
      t.Text = s;
      doc.Add(t);
      return t;
    }
    public NetworkNode MakePetriNode(GoDocument doc, String s, BarNode b1, BarNode b2, float x, float y) {
      NetworkNode n = new NetworkNode();
      n.LabelSpot = GoObject.MiddleRight;
      n.Text = s;
      n.Shape = new GoEllipse();
      n.Shape.Size = new SizeF(30, 30);
      n.Port.Size = new SizeF(15, 15);
      n.Location = new PointF(x, y);
      doc.Add(n);
      ConnectNetworkNode(doc, b1, n);
      ConnectNetworkNode(doc, n, b2);
      return n;
    }
    public GoLink ConnectNetworkNode(GoDocument doc, GoBasicNode a, GoBasicNode b) {
      if (a == null) return null;
      if (b == null) return null;
      GoLink l = new GoLink();
      l.Orthogonal = true;
      l.ToArrow = true;
      l.FromPort = a.Port;
      l.ToPort = b.Port;
      doc.Add(l);
      l.UpdateRoute();
      return l;
    }

Some day maybe we’ll make a real sample application out of this. We could add a label to each node and animate the operation of a Petri net.