Petri 网

关于 Petri 网

Petri 网是对离散事件系统中并发、同步和资源流进行建模的基础形式语言——由 Carl Adam Petri 于 1962 年发明,此后在计算机科学、控制工程、制造业和业务流程建模中持续应用至今。其表示法简洁、辨识度高:**库所(place)**是圆形,**变迁(transition)是矩形条,弧带有权重,系统的动态状态则由散落在库所中的托肯(token)**表示。

Schematex 的 petri 引擎与拖放式形状库的不同之处在于,它理解动态语义。它了解标识(marking)(哪些库所持有多少个托肯),计算哪些变迁被使能(每个输入库所持有的托肯数至少达到弧权重——以绿色高亮显示),并可触发变迁以产生下一个标识。渲染结果是语义下游的产物——与 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 关键字和可选标题开头,然后在连接弧之前声明库所变迁

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. 变迁:立即型与定时型

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 网标准参考


相关示例

来自示例库的即用场景:

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.