In this post we show how to build a Petri net using MindFusion.Diagramming for WinForms. Petri nets are used to model and study distributed systems. A net contains places, transitions and arcs. A place represents possible state of the system, and a transition represents the change from one state to another. Arcs connect places to transitions and show the flow direction.
First, create a new .NET Windows Forms project and add a Model.cs file to it where we’ll define Petri net model classes
public class Net { public List Places { get; set; } public List Transitions { get; set; } public List Arcs { get; set; } public Net() { Places = new List(); Transitions = new List(); Arcs = new List(); } } public class Node { public string Label { get; set; } } public class Place : Node { public int Tokens { get; set; } } public class Transition : Node { } public class Arc { // Arcs run from a place to a transition or vice versa, // never between places or between transitions. public Arc(Place input, Transition output) { Input = input; Output = output; } public Arc(Transition input, Place output) { Input = input; Output = output; } public Node Input { get; private set; } public Node Output { get; private set; } public int Multiplicity { get; set; } }
Now we can create a simple Petri net:
Net CreateSampleNet() { var net = new Net(); var p1 = new Place { Label = "P1", Tokens = 1 }; var p2 = new Place { Label = "P2", Tokens = 0 }; var p3 = new Place { Label = "P3", Tokens = 2 }; var p4 = new Place { Label = "P4", Tokens = 1 }; net.Places.AddRange(new[] { p1, p2, p3, p4 }); var t1 = new Transition { Label = "T1" }; var t2 = new Transition { Label = "T2" }; net.Transitions.AddRange(new[] { t1, t2 }); var a1 = new Arc(p1, t1); var a2 = new Arc(t1, p2); var a3 = new Arc(t1, p3); var a4 = new Arc(p2, t2); var a5 = new Arc(p3, t2); var a6 = new Arc(t2, p4); var a7 = new Arc(t2, p1); net.Arcs.AddRange(new[] { a1, a2, a3, a4, a5, a6, a7 }); return net; }
Next, drop a DiagramView and Diagram objects on the form which we’ll use to visualize the net. Add the following method to create diagram elements representing the model objects, and run LayeredLayout to arrange them:
void BuildDiagram(Net net) { var nodeMap = new Dictionary<node, diagramnode="">(); var placeBounds = new RectangleF(0, 0, 16, 16); var transBounds = new RectangleF(0, 0, 6, 20); foreach (var place in net.Places) { var node = diagram.Factory.CreateShapeNode(placeBounds); node.Text = place.Label; node.TextFormat.LineAlignment = StringAlignment.Far; node.Shape = Shapes.Ellipse; node.Tag = place.Tokens; node.CustomDraw = CustomDraw.Additional; nodeMap[place] = node; } foreach (var trans in net.Transitions) { var node = diagram.Factory.CreateShapeNode(transBounds); node.Text = trans.Label; node.TextFormat.LineAlignment = StringAlignment.Far; node.Shape = Shapes.Rectangle; nodeMap[trans] = node; } foreach (var arc in net.Arcs) { var link = diagram.Factory.CreateDiagramLink( nodeMap[arc.Input], nodeMap[arc.Output]); link.Tag = arc.Multiplicity; link.HeadShape = ArrowHeads.PointerArrow; } var layout = new LayeredLayout(); layout.Orientation = Orientation.Horizontal; layout.StraightenLongLinks = true; layout.Arrange(diagram); }</node,>
We will use the DrawNode custom draw event to render marks associated with each place. Another possibility is to create a custom node class and override its Draw method.
void OnDrawNode(object sender, DrawNodeEventArgs e) { var node = e.Node; var g = e.Graphics; if (node.Tag is int) { var tokens = (int)node.Tag; var cx = node.Bounds.Width / 2; var cy = node.Bounds.Height / 2; if (tokens == 1) { float r = cx / 2; DrawMark(cx, cy, r, g); } else if (tokens == 2) { float r = 2 * cx / 5; DrawMark(cx / 2, cy, r, g); DrawMark(3 * cx / 2, cy, r, g); } else if (tokens == 3) { float r = cx / 3; float y2 = 4 * cy / 3; DrawMark(cx, 2 * cy / 5, r, g); DrawMark(cx / 2, y2, r, g); DrawMark(3 * cx / 2, y2, r, g); } } } void DrawMark(float x, float y, float r, IGraphics g) { g.FillEllipse(Brushes.Black, x - r, y - r, r * 2, r * 2); }
Finally, set some appearance properties and call the methods above to build the diagram:
public MainForm() { InitializeComponent(); diagram.ShadowsStyle = ShadowsStyle.None; diagram.DiagramLinkStyle.Brush = new MindFusion.Drawing.SolidBrush(Color.Black); diagram.ShapeNodeStyle.Brush = new MindFusion.Drawing.SolidBrush(Color.White); diagram.ShapeNodeStyle.FontSize = 10f; diagram.ShapeNodeStyle.FontStyle = FontStyle.Bold; var textAbove = new[] { new LineTemplate(-100, -100, 200, -100), new LineTemplate(200, -100, 200, 0), new LineTemplate(200, 0, -100, 0), new LineTemplate(-100, 0, -100, -100) }; Shapes.Ellipse.TextArea = textAbove; Shapes.Rectangle.TextArea = textAbove; var net = CreateSampleNet(); BuildDiagram(net); }
The final result is displayed below.
The complete sample project is available for download here:
PetriNet.zip
For more information on Petri nets, see this Wikipedia article:
http://en.wikipedia.org/wiki/Petri_net
All MindFusion.Diagramming libraries expose the same programming interface, so most of the sample code shown above will work with only a few modifications in WPF, ASP.NET, Silverlight and Java versions of the control.
Enjoy!