function petriNetTInvariantsGui % PETRINETTINVARIANTSGUI GUI for illustrating % cycles (T-invariants) in Petri nets. % % Transition invariant condition: % A * x = 0 % % State equation: % Mk = M0 + A * x % % A valid T-invariant is a nonnegative integer % firing vector x that returns % the net to its original marking. % placeNames = ["P1: Buffer A"; "P2: Buffer B"; "P3: Buffer C"]; transitionNames = ["T1: A -> B"; "T2: B -> C"; "T3: C -> A"]; Pre = eye(3); Post = [0 0 1; 1 0 0; 0 1 0]; A = Post - Pre; sampleM0 = [1; 0; 0]; sampleM = sampleM0; sampleX = [1; 1; 1]; fig = uifigure('Name', 'Petri Net T-Invariants', 'Position', [100 100 1340 800]); mainGrid = uigridlayout(fig, [2 2]); mainGrid.RowHeight = {'1x', 28}; mainGrid.ColumnWidth = {'0.95x', '1.05x'}; mainGrid.Padding = [10 10 10 10]; mainGrid.RowSpacing = 8; mainGrid.ColumnSpacing = 10; inputPanel = uipanel(mainGrid, 'Title', 'Controls'); inputPanel.Layout.Row = 1; inputPanel.Layout.Column = 1; outputPanel = uipanel(mainGrid, 'Title', 'T-Invariant Analysis'); 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, [9 2]); inputGrid.RowHeight = {22, 28, 22, 28, 22, 28, 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'); uilabel(inputGrid, 'Text', 'Firing count vector x'); m0Field = uieditfield(inputGrid, 'text', ... 'Value', vectorToString(sampleM0), ... 'ValueChangedFcn', @refreshAll); m0Field.Layout.Row = 2; m0Field.Layout.Column = 1; xField = uieditfield(inputGrid, 'text', ... 'Value', vectorToString(sampleX), ... 'ValueChangedFcn', @refreshAll); xField.Layout.Row = 2; xField.Layout.Column = 2; uilabel(inputGrid, 'Text', 'Current marking M'); uilabel(inputGrid, 'Text', 'Transition to fire'); mField = uieditfield(inputGrid, 'text', ... 'Value', vectorToString(sampleM), ... 'ValueChangedFcn', @refreshAll); mField.Layout.Row = 4; mField.Layout.Column = 1; transitionDropDown = uidropdown(inputGrid, ... 'Items', transitionNames, ... 'Value', transitionNames(1), ... 'ValueChangedFcn', @refreshAll); transitionDropDown.Layout.Row = 4; transitionDropDown.Layout.Column = 2; fireButton = uibutton(inputGrid, ... 'Text', 'Fire Selected Transition', ... 'ButtonPushedFcn', @fireTransition); fireButton.Layout.Row = 5; fireButton.Layout.Column = [1 2]; resetButton = uibutton(inputGrid, ... 'Text', 'Reset M = M0', ... 'ButtonPushedFcn', @resetCurrentMarking); resetButton.Layout.Row = 6; resetButton.Layout.Column = [1 2]; checkButton = uibutton(inputGrid, ... 'Text', 'Check T-Invariant', ... 'ButtonPushedFcn', @refreshAll); checkButton.Layout.Row = 7; checkButton.Layout.Column = [1 2]; hintLabel = uilabel(inputGrid, ... 'Text', 'A valid T-invariant has x >= 0, integer, and A*x = 0.'); hintLabel.Layout.Row = 8; hintLabel.Layout.Column = [1 2]; cycleLabel = uilabel(inputGrid, ... 'Text', 'Example cycle: T1, T2, T3 each fire once.'); cycleLabel.Layout.Row = 9; cycleLabel.Layout.Column = [1 2]; outputGrid = uigridlayout(outputPanel, [6 1]); outputGrid.RowHeight = {92, '1x', 300, 28, 60, '1x'}; outputGrid.Padding = [8 8 8 8]; formulaText = uitextarea(outputGrid, ... 'Editable', 'off', ... 'Value', [ "Transition invariant condition:" "A * x = 0" "State equation:" "Mk = M0 + A * x" "If x is a nonnegative integer T-invariant, the net returns to M0." "This represents a complete cyclic work path." ]); formulaText.Layout.Row = 1; uilabel(outputGrid, 'Text', 'Incidence matrix A = Post - Pre'); incidenceTable = uitable(outputGrid, 'Data', A); incidenceTable.Layout.Row = 2; netAxes = uiaxes(outputGrid); netAxes.Layout.Row = 3; netAxes.Box = 'on'; netAxes.Visible = 'off'; netAxes.XTick = []; netAxes.YTick = []; resultLabel = uilabel(outputGrid, 'Text', ''); resultLabel.Layout.Row = 4; basisText = uitextarea(outputGrid, ... 'Editable', 'off', ... 'Value', strings(0,1)); basisText.Layout.Row = 5; detailText = uitextarea(outputGrid, ... 'Editable', 'off', ... 'Value', strings(0,1)); detailText.Layout.Row = 6; refreshAll(); function refreshAll(~, ~) try M0 = parseVector(m0Field.Value, 3, 'M0'); M = parseVector(mField.Value, 3, 'M'); x = parseVector(xField.Value, 3, 'x'); requireNonnegativeIntegerVector(M0, 'M0'); requireNonnegativeIntegerVector(M, 'M'); requireNonnegativeVector(x, 'x'); A = Post - Pre; incidenceTable.Data = A; residual = A * x; Mk = M0 + residual; selectedTransition = find(strcmp(transitionNames, string(transitionDropDown.Value)), 1); if isempty(selectedTransition) selectedTransition = 1; end enabled = all(M >= Pre, 1); drawPetriNet(netAxes, M, enabled, selectedTransition); isIntegerX = all(abs(x - round(x)) <= 1e-12); isTInvariant = norm(residual, inf) <= 1e-12 && isIntegerX && all(x >= 0); if isTInvariant resultLabel.Text = 'x is a T-invariant: A*x = 0, so the net returns to M0.'; elseif norm(residual, inf) <= 1e-12 resultLabel.Text = 'A*x = 0, but x is not a valid nonnegative integer vector.'; else resultLabel.Text = 'x is not a T-invariant: A*x is nonzero.'; end basis = null(A, 'r'); if isempty(basis) basisText.Value = [ "No basis vector found." "The null space may be trivial." ]; else for k = 1:size(basis, 2) basis(:, k) = normalizeBasisVector(basis(:, k)); end lines = strings(size(basis, 2) + 1, 1); lines(1) = "Candidate basis vectors for null(A):"; for k = 1:size(basis, 2) lines(k + 1) = "v" + string(k) + " = " + vectorToString(basis(:, k)); end basisText.Value = lines; end detailText.Value = [ "M0 = " + vectorToString(M0) "Current M = " + vectorToString(M) "x = " + vectorToString(x) "A*x = " + vectorToString(residual) "Mk = M0 + A*x = " + vectorToString(Mk) "If A*x = 0, then Mk = M0 and the cycle closes." ]; statusLabel.Text = 'Ready.'; catch ME statusLabel.Text = "Error: " + string(ME.message); resultLabel.Text = ''; basisText.Value = strings(0,1); detailText.Value = strings(0,1); end end function fireTransition(~, ~) try M = parseVector(mField.Value, 3, 'M'); requireNonnegativeIntegerVector(M, '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); mField.Value = vectorToString(M); refreshAll(); statusLabel.Text = "Fired " + string(transitionNames(t)) + '.'; catch ME statusLabel.Text = "Error: " + string(ME.message); end end function resetCurrentMarking(~, ~) mField.Value = vectorToString(sampleM0); transitionDropDown.Value = transitionNames(1); refreshAll(); end function drawPetriNet(ax, M, enabled, selectedTransition) cla(ax); hold(ax, 'on'); axis(ax, 'equal'); axis(ax, [0 10 7 0]); 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(M(i)), ... 'HorizontalAlignment', 'center'); drawTokens(ax, placePos(i,:), M(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 Cyclic Work Path (T-Invariant)', ... '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 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', [0.85 0.25 0.25], ... 'EdgeColor', [0.85 0.25 0.25]); end end function v = parseVector(textValue, expectedLength, name) if isstring(textValue) textValue = char(textValue); end textValue = strtrim(textValue); textValue = regexprep(textValue, '[\[\];,]', ' '); parts = split(string(textValue)); parts(parts == "") = []; if isempty(parts) error('Invalid %s.', name); end v = str2double(parts); if any(isnan(v)) error('Invalid %s.', name); end v = v(:); if numel(v) ~= expectedLength error('The %s vector must have %d elements.', name, expectedLength); end end function requireNonnegativeIntegerVector(v, name) tol = 1e-12; if any(v(:) < 0) error('%s must be nonnegative.', name); end if any(abs(v(:) - round(v(:))) > tol) error('%s must contain integers.', name); end end function requireNonnegativeVector(v, name) if any(v(:) < 0) error('%s must be nonnegative.', name); end end function w = normalizeBasisVector(v) v = real(v); idx = find(abs(v) > 1e-12, 1); if isempty(idx) w = v; return; end if v(idx) < 0 v = -v; end w = v ./ max(1, max(abs(v))); end function s = vectorToString(v) v = v(:).'; if isempty(v) s = "[]"; return; end s = "[" + join(string(compose('%g', v)), " ") + "]"; end end