Petri 網路

關於 Petri 網路

Petri 網路是建模離散事件系統中並行、同步和資源流的基礎形式——由 Carl Adam Petri 於 1962 年發明,此後在電腦科學、控制工程、製造業和業務流程建模領域持續廣泛使用。這種標記法小而直觀:庫所(place)是一個圓,變遷(transition)是一條橫線,弧線帶有權重,系統的動態狀態由散佈在各庫所中的符記(token)表示。

Schematex petri 引擎與拖放式形狀庫的不同之處在於它理解動態語義。它知道什麼是標記(哪些庫所持有多少符記),計算哪些變遷已啟用(每個輸入庫所的符記數 ≥ 弧權重——以綠色標示),並能觸發一個變遷以產生下一個標記。渲染是語義的下游——這與 pert 對排程的立場一致。它不同於**狀態圖(一個活躍狀態,而非符記分佈)、SFC(PLC 的受限安全 Petri 網路),以及BPMN**(其符記語義定義為歸約到工作流程 Petri 網路)。

petri·§
↘ preview
100%
Petri net — Classic 4 places, 2 transitions, 7 arcs. Marking {P1:1, P3:2, P4:1}. Enabled: T1. Classic P1 P2 P3 P4 T1 T2
UTF-8 · LF · 14 lines · 151 chars✓ parsed·5.6 ms·5.1 KB SVG

在初始標記下,T1 已啟用(其輸入 P1 和 P4 各持有一個符記),因此得到綠色環;T2 未啟用(P2 為空)。P4 -> T1 回饋弧被路由為反向弧曲線。


1. 第一張網路

每份文件以 petri 關鍵字和可選標題開頭,然後宣告庫所變遷,再用弧線連接它們:

petri "Minimal"
  place P1 *1
  transition T1
  place P2
  P1 -> T1
  T1 -> P2
  • place <id> — 一個圓。*1 設定初始符記數(標記)。
  • transition <id> — 一條橫線(事件/動作)。
  • <a> -> <b> — 一條有向弧線。弧線是二部的:每條弧線從庫所到變遷,或從變遷到庫所,不能是庫所到庫所或變遷到變遷。若你寫錯了方向,引擎會告訴你是哪一行。

與某些 Schematex 圖表不同,節點不會從弧線中自動宣告——因為無法安全地將未宣告的 ID 確定類型為庫所或變遷。引用未知節點的弧線會產生可讀的錯誤訊息。


2. 標記與符記

標記是每個庫所持有的符記數。有三種等效的設定方式:

place P1 *3            # *n shorthand
place P2 tokens: 3     # explicit
place P3 •••           # literal dots (1–4)

或用 marking: 行一次設定多個:

marking: P1=3, P3=2

符記渲染為點(最多 4 個,排列成網格),超過時顯示為數字。用 tokens: dots | count | auto(預設 auto)強制指定某種樣式。


3. 變遷:立即型 vs. 時態型

transition fast                 # immediate — a solid bar (the default)
transition slow timed rate: 0.8 # timed — a hollow box with a rate label λ

立即型變遷在零時間內觸發,渲染為傳統的實心橫線;時態型變遷(GSPN 慣例)渲染為空心方框,帶有可選的 rate:(λ)。帶有 rate: 的變遷自動視為時態型。prio: n 設定優先級,而 [guard] 渲染為標籤(v0.1 中不進行計算)。


4. 弧線類型

四種弧頭涵蓋標準並行詞彙:

P -> T              # standard arc (filled arrowhead)
P -> T weight: 2    # weight > 1 is labelled (weight: n or *n)
P -o T              # inhibitor — enabled only while the place is empty (hollow-circle head)
P -- T              # read / test — tests presence without consuming (no head)
P => T              # reset — empties the place when the transition fires (double head)

抑制弧和重置弧只能從庫所到變遷——解析器拒絕反方向。


5. 容量

庫所可以設定容量上限。使觸發後溢出的操作被禁用,且庫所以虛線邊框和 K=n 標籤繪製:

place Buffer capacity: 3

6. 動態語義:啟用與觸發

引擎在每次渲染時計算語義:

  • 已啟用的變遷(每個輸入都滿足;抑制弧輸入為空;輸出不會溢出容量)以綠色環標示。
  • 死鎖的變遷——在當前標記下永遠無法觸發——顯示為灰色。
  • fire: 行重播一個觸發序列,並渲染結果標記:
petri
  place P1 *1
  transition T1
  place P2
  transition T2
  place P3
  P1 -> T1
  T1 -> P2
  P2 -> T2
  T2 -> P3
  fire: T1

觸發 T1 後,符記從 P1 移動到 P2,此時 T2 成為已啟用的變遷。SVG 的 <desc> 記錄標記、已啟用集合,以及偵測到的任何子類(狀態機 / 標記圖 / 工作流程網路)。


7. 版面與主題

layout: lr     # left-to-right (default)
layout: tb     # top-to-bottom

庫所和變遷自動落在交替的層上;循環被偵測後,其回饋弧路由為反向弧曲線。三種主題:

  • default — 主題藍灰色,綠色保留給已啟用,紅色保留給抑制弧
  • monochrome — 忠實還原 Murata-1989 教科書外觀;已啟用以雙重黑色環表示(顏色降級為形狀)。
  • dark — Catppuccin Mocha。

CJK 標籤和 「…」 / "…" 引號可正常解析:

petri "生产流程"
  place 原料 *2 「原材料」
  transition 加工
  place 成品
  原料 -> 加工 weight: 2
  加工 -> 成品

完整規格:Petri Net Standard Reference


相關範例

來自範例庫的即用情境:

petri·§ Murata 1989 + ISO/IEC 15909-1 (place/transition net)
Petri net — Classic 4 places, 2 transitions, 7 arcs. Marking {P1:1, P3:2, P4:1}. Enabled: T1. Classic P1 P2 P3 P4 T1 T2
The classic Petri net — concurrency with feedback
Murata's canonical place/transition net — one transition forks a token into two concurrent branches that a second transition joins, with a feedback place closing the loop. The engine marks which transitions are enabled in the current marking.
education
petri·§ Murata 1989 / ISO-IEC 15909-1 (place/transition net)
Petri net — Mutual Exclusion — two processes, one resource 5 places, 4 transitions, 12 arcs. Marking {idleA:1, idleB:1, mutex:1}. Enabled: enterA, enterB. Mutual Exclusion — two processes, one resource idleA A idle idleB B idle mutex resource critA A critical critB B critical enterA exitA enterB exitB
Mutual exclusion with a shared resource
The canonical concurrency pattern as a Petri net — two processes competing for a single Mutex token. Either process may enter its critical section, but the shared token guarantees they never do so at the same time. Both entry transitions show as enabled until one fires and consumes the resource.
industrial & process
petri·§ Murata 1989 (place/transition net firing rule)
Petri net — After fire: T1 3 places, 2 transitions, 4 arcs. Marking {P2:1}. Enabled: T2. Class: state machine, workflow net. After fire: T1 P1 P2 P3 T1 T2
Firing a transition advances the marking
The same three-place net before and after firing — the `fire:` directive replays a transition, so the rendered marking is the state *after* the token moved from P1 to P2, and the engine now highlights T2 as the newly enabled transition.
education
petri·§ van der Aalst 1998 workflow nets (a Murata place/transition net)
Petri net — Order workflow 6 places, 4 transitions, 10 arcs. Marking {in:1}. Enabled: split. Class: workflow net. Order workflow in received pick invoice packed billed out shipped split pack bill ship
Order-fulfilment workflow net (WF-net)
A van der Aalst workflow net — a single source place and single sink place bracket an AND-split/join that runs picking and invoicing concurrently before shipping. The engine detects the WF-net structure and reports it in the SVG description.
business & operations
petri·§ Murata 1989 + Marsan 1995 (GSPN timed transitions)
Petri net — Producer / Consumer (bounded buffer) 4 places, 2 transitions, 8 arcs. Marking {pReady:1, free:3, cReady:1}. Enabled: produce. Class: marked graph. Producer / Consumer (bounded buffer) pReady producer ready free free slots used used slots K=3 cReady consumer ready produce deposit consume withdraw λ=0.8
Producer / consumer with a bounded buffer
The bounded-buffer producer/consumer pattern as a Petri net — complementary free/used slot places enforce the buffer size, a timed withdraw transition models the consumer's rate, and place capacity caps the buffer at three slots. Tokens flowing through free→used→free show backpressure as a structural property.
industrial & process

Found this useful?

Schematex is free, fully open source, and zero-dependency. A star helps other developers discover it.