Python 作为用于写 PlantUML 序列图的 DSL
Napkin is a tool to "write" sequence diagrams effectively as Python code.
The sequence diagrams are useful tool to capture the behavioural aspect of the design. PlantUML is a great tool to draw nice sequence diagrams with simple human readable plain text.
However, the syntax of PlantUML is hard to use when there are nested calls, where lifeline with multiple activation/deactivation are involved. Unfortunately, this situation is quite common in sequence diagram for S/W.
For example, consider the following common sequence diagram, which is from Figure 4.2, UML Distilled 3E:
The PlainUML script for the diagram will be as follows:
@startuml
participant User
participant Order
participant OrderLine
participant Product
participant Customer
User -> Order : calculatePrice()
activate Order
Order -> OrderLine : calculatePrice()
activate OrderLine
OrderLine -> Product : getPrice(quantity:number)
OrderLine -> Customer : getDiscountedValue(Order)
activate Customer
Customer -> Order : getBaseValue()
activate Order
Customer <-- Order: value
deactivate Order
OrderLine <-- Customer: discountedValue
deactivate Customer
deactivate OrderLine
deactivate Order
@endumlIt is quite hard to follow especially as there are multiple level of nested activation/deactivation.
What if we express the same thing as the following Python code ?
@napkin.seq_diagram()
def distributed_control(c):
user = c.object('User')
order = c.object('Order')
orderLine = c.object('OrderLine')
product = c.object('Product')
customer = c.object('Customer')
with user:
with order.calculatePrice():
with orderLine.calculatePrice():
product.getPrice('quantity:number')
with customer.getDiscountedValue(order):
order.getBaseValue().ret('value')
c.ret('discountedValue')distributed_control is normal function accepting a context object, c to access APIs.
The function defines objects and the control starts with user object, which then calls orderLine.calculatePrice().
Basically, the sequence diagram is expressed as "almost" normal python code.
There are several advantages in using Python instead of using other special syntax language:
Install and update using pip
$ pip install napkinWrite a simple script called hello.py as follows:
import napkin
@napkin.seq_diagram()
def hello_world(c):
user = c.object('user')
world = c.object('world')
with user:
world.hello()Then, the following command will generate hello_world.puml:
$ napkin hello.py…Instead of passing napkin binary Python files, we can generate diagrams simply by running
the Python source code containing the diagrams as follows:
import napkin
@napkin.seq_diagram()
def hello_world(c):
...
if __name__ == '__main__':
napkin.generate()napkin.generate(output_format='plantuml', output_dir='.') will generate all the diagrams described in the same file.
Napkin can generate PNG/SVG image or ASCII art text files by asking PlantUML server.
In order to generate image file, image format needs to be specified as plantuml_, which will generate image file along with puml file.
$ napkin -f plantuml_png hello.pyAs default, the public server is used and it can be changed by --server-url.
Most usage examples are available here.
A new feature or fix will be avaialbe here.
As Napkin has a functionality to generate image files from PlantUML text file, a
simple script, napkin_plantuml is provided to use PlantUML server to generate
images.
For example, in order to generate hello.png image from hello.puml file:
$ napkin_plantuml hello.puml hello.pngIn order to use napkin as literate programming tool ob-napkin
package is supported for Emacs.
暂无开放 Issues,或尚未同步最近议题。