function petriNetRobotArmsGui2 %PETRINETROBOTARMSGUI GUI for a 3-robot-arm Petri % net example. % % Places: % P1 = Idle Robots % P2 = Robots Working % P3 = Robots Repairing % % P-invariant: % y = [1; 1; 1] % y^T * A = 0 % y^T * M = 3 % placeNames = ["Idle Robots"; "Working Robots"; "Repairing Robots"]; transitionNames = ["T1: Idle -> Working"; "T2: Working -> Repairing"; "T3: Repairing -> Idle"]; Pre = eye(3); Post = [0 0 1; 1 0 0; 0 1 0]; A = Post - Pre; M0 = [3; 0; 0]; M = M0; y = [1; 1; 1]; fig = uifigure('Name', '3 Robot Arms Petri Net', 'Position', [100 100 1300 760]); mainGrid = uigridlayout(fig, [2 2]); mainGrid.RowHeight = {'1x', 28}; mainGrid.ColumnWidth = {'0.9x', '1.1x'}; mainGrid.Padding = [10 10 10 10]; mainGrid.RowSpacing = 8; mainGrid.ColumnSpacing = 10; inputPanel = uipanel(mainGrid, 'Title', 'Control Panel'); inputPanel.Layout.Row = 1; inputPanel.Layout.Column = 1; outputPanel = uipanel(mainGrid, 'Title', 'Petri Net Representation'); outputPanel.Layout.Row = 1; outputPanel.Layout.Column = 2; statusLabel = uilabel(mainGrid, 'Text', 'Ready.'); statusLabel.Layout.Row = 2; statusLabel.Layout.Column = [1 2]; inputGrid = uigridlayout(inputPanel, [7 2]); inputGrid.RowHeight = {24, 24, 24, 30, 30, 30, 30}; inputGrid.ColumnWidth = {'1x', '1x'}; inputGrid.Padding = [8 8 8 8]; inputGrid.RowSpacing = 8; inputGrid.ColumnSpacing = 8; uilabel(inputGrid, 'Text', 'Initial marking M0 = [3; 0; 0]'); uilabel(inputGrid, 'Text', 'P-invariant y = [1; 1; 1]'); uilabel(inputGrid, 'Text', 'P1: Idle'); uilabel(inputGrid, 'Text', 'Transition to fire'); p1Field = uieditfield(inputGrid, 'numeric', ... 'Value', M(1), ... 'Limits', [0 inf], ... 'RoundFractionalValues', 'on', ... 'ValueChangedFcn', @refreshAll); p1Field.Layout.Row = 3; p1Field.Layout.Column = 1; transitionDropDown = uidropdown(inputGrid, ... 'Items', transitionNames, ... 'Value', transitionNames(1), ... 'ValueChangedFcn', @refreshAll); transitionDropDown.Layout.Row = 3; transitionDropDown.Layout.Column = 2; uilabel(inputGrid, 'Text', 'P2: Working'); fireButton = uibutton(inputGrid, ... 'Text', 'Fire Selected Transition', ... 'ButtonPushedFcn', @fireTransition); fireButton.Layout.Row = 4; fireButton.Layout.Column = 2; p2Field = uieditfield(inputGrid, 'numeric', ... 'Value', M(2), ... 'Limits', [0 inf], ... 'RoundFractionalValues', 'on', ... 'ValueChangedFcn', @refreshAll); p2Field.Layout.Row = 4; p2Field.Layout.Column = 1; uilabel(inputGrid, 'Text', 'P3: Repairing'); resetButton = uibutton(inputGrid, ... 'Text', 'Reset to 3 Idle Robots', ... 'ButtonPushedFcn', @resetExample); resetButton.Layout.Row = 5; resetButton.Layout.Column = 2; p3Field = uieditfield(inputGrid, 'numeric', ... 'Value', M(3), ... 'Limits', [0 inf], ... 'RoundFractionalValues', 'on', ... 'ValueChangedFcn', @refreshAll); p3Field.Layout.Row = 5; p3Field.Layout.Column = 1; refreshButton = uibutton(inputGrid, ... 'Text', 'Refresh', ... 'ButtonPushedFcn', @refreshAll); refreshButton.Layout.Row = 6; refreshButton.Layout.Column = [1 2]; sumLabel = uilabel(inputGrid, 'Text', 'Total robots should stay at 3.'); sumLabel.Layout.Row = 7; sumLabel.Layout.Column = [1 2]; outputGrid = uigridlayout(outputPanel, [4 1]); outputGrid.RowHeight = {90, '1x', 36, 84}; outputGrid.Padding = [8 8 8 8]; formulaText = uitextarea(outputGrid, ... 'Editable', 'off', ... 'Value', [ "Petri net model:" "P1 = Idle Robots, P2 = Working Robots, P3 = Repairing Robots" "P-invariant: y = [1 1 1]^T" "y^T * A = 0" "y^T * M = y^T * M0 = 3" ]); formulaText.Layout.Row = 1; netAxes = uiaxes(outputGrid); netAxes.Layout.Row = 2; netAxes.Box = 'on'; netAxes.XTick = []; netAxes.YTick = []; netAxes.Visible = 'off'; resultLabel = uilabel(outputGrid, 'Text', ''); resultLabel.Layout.Row = 3; detailText = uitextarea(outputGrid, ... 'Editable', 'off', ... 'Value', strings(0,1)); detailText.Layout.Row = 4; refreshAll(); function refreshAll(~, ~) try M = getMarking(); validateMarking(M); A = Post - Pre; fireIndex = find(strcmp(transitionNames, string(transitionDropDown.Value)), 1); if isempty(fireIndex) fireIndex = 1; end enabled = all(M >= Pre, 1); totalTokens = y.' * M; totalInitial = y.' * M0; invariantResidual = y.' * A; drawPetriNet(netAxes, M, enabled, fireIndex); if norm(invariantResidual, inf) <= 1e-12 resultLabel.Text = 'P-invariant holds: the total number of robots is conserved.'; else resultLabel.Text = 'P-invariant does not hold for this model.'; end detailText.Value = [ "Current marking M = " + vectorToString(M) "Total robots y^T * M = " + scalarToString(totalTokens) "Initial total y^T * M0 = " + scalarToString(totalInitial) "y^T * A = " + vectorToString(invariantResidual) "Structural meaning: the robot pool is bounded by 3." ]; statusLabel.Text = 'Ready.'; catch ME statusLabel.Text = "Error: " + string(ME.message); resultLabel.Text = ''; detailText.Value = strings(0,1); end end function fireTransition(~, ~) try M = getMarking(); validateMarking(M); t = find(strcmp(transitionNames, string(transitionDropDown.Value)), 1); if isempty(t) error('Select a valid transition.'); end if ~all(M >= Pre(:, t)) statusLabel.Text = "Transition " + string(transitionNames(t)) + ' is not enabled.'; return; end M = M + A(:, t); setMarking(M); refreshAll(); statusLabel.Text = "Fired " + string(transitionNames(t)) + '.'; catch ME statusLabel.Text = "Error: " + string(ME.message); end end function resetExample(~, ~) setMarking(M0); transitionDropDown.Value = transitionNames(1); refreshAll(); end function Mvec = getMarking() Mvec = [p1Field.Value; p2Field.Value; p3Field.Value]; end function setMarking(Mvec) p1Field.Value = Mvec(1); p2Field.Value = Mvec(2); p3Field.Value = Mvec(3); end function validateMarking(Mvec) if any(Mvec < 0) error('Robot counts must be nonnegative.'); end if any(abs(Mvec - round(Mvec)) > 1e-12) error('Robot counts must be integers.'); end end function drawPetriNet(ax, Mvec, enabled, selectedTransition) cla(ax); hold(ax, 'on'); axis(ax, 'equal'); axis(ax, [0 10 0 7]); ax.Visible = 'off'; placePos = [2 5; 8 5; 5 1.8]; transPos = [5 5; 6.9 3.0; 3.1 3.0]; placeR = 0.65; transW = 0.35; transH = 1.0; % Arcs drawArc(ax, placePos(1,:), transPos(1,:)); drawArc(ax, transPos(1,:), placePos(2,:)); drawArc(ax, placePos(2,:), transPos(2,:)); drawArc(ax, transPos(2,:), placePos(3,:)); drawArc(ax, placePos(3,:), transPos(3,:)); drawArc(ax, transPos(3,:), placePos(1,:)); % Places for i = 1:3 rectangle(ax, ... 'Position', [placePos(i,1)-placeR, placePos(i,2)-placeR, 2*placeR, 2*placeR], ... 'Curvature', [1 1], ... 'FaceColor', [0.95 0.97 1.0], ... 'EdgeColor', [0.1 0.2 0.6], ... 'LineWidth', 2); text(ax, placePos(i,1), placePos(i,2)+1.05, placeNames(i), ... 'HorizontalAlignment', 'center', ... 'FontWeight', 'bold'); text(ax, placePos(i,1), placePos(i,2)-1.05, "M = " + string(Mvec(i)), ... 'HorizontalAlignment', 'center'); drawTokens(ax, placePos(i,:), Mvec(i), placeR); end % Transitions for j = 1:3 if enabled(j) faceColor = [0.65 0.9 0.65]; else faceColor = [0.85 0.85 0.85]; end if j == selectedTransition edgeColor = [0.0 0.3 0.8]; lineWidth = 2.5; else edgeColor = [0 0 0]; lineWidth = 1.5; end rectangle(ax, ... 'Position', [transPos(j,1)-transW/2, transPos(j,2)-transH/2, transW, transH], ... 'FaceColor', faceColor, ... 'EdgeColor', edgeColor, ... 'LineWidth', lineWidth); text(ax, transPos(j,1), transPos(j,2)-0.95, transitionNames(j), ... 'HorizontalAlignment', 'center', ... 'FontSize', 9); end text(ax, 5, 6.6, 'Petri Net for Conserving 3 Robot Arms', ... 'HorizontalAlignment', 'center', ... 'FontWeight', 'bold', ... 'FontSize', 12); end function drawArc(ax, p1, p2) plot(ax, [p1(1) p2(1)], [p1(2) p2(2)], 'k-', 'LineWidth', 1.4); v = p2 - p1; mid = (p1 + p2) / 2; quiver(ax, mid(1), mid(2), 0.18*v(1), 0.18*v(2), 0, ... 'Color', 'k', ... 'LineWidth', 1.0, ... 'MaxHeadSize', 0.8, ... 'AutoScale', 'off'); end function drawTokens(ax, center, count, radius) if count <= 0 return; end tokenColors = [0.85 0.25 0.25]; tokenRadius = 0.11; offsets = [0 0; -0.18 0.14; 0.18 0.14; -0.18 -0.14; 0.18 -0.14]; for k = 1:count idx = min(k, size(offsets, 1)); pos = center + offsets(idx, :) * radius; rectangle(ax, ... 'Position', [pos(1)-tokenRadius, pos(2)-tokenRadius, 2*tokenRadius, 2*tokenRadius], ... 'Curvature', [1 1], ... 'FaceColor', tokenColors, ... 'EdgeColor', tokenColors); end end function s = vectorToString(v) v = v(:).'; s = "[" + join(string(compose('%g', v)), " ") + "]"; end function s = scalarToString(x) s = string(compose('%g', x)); end end