muMLE/examples/petrinet/renderer.py

46 lines
1.7 KiB
Python
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from api.od import ODAPI
from concrete_syntax.graphviz.make_url import show_graphviz
try:
import graphviz
HAVE_GRAPHVIZ = True
except ImportError:
HAVE_GRAPHVIZ = False
def render_tokens(num_tokens: int):
if 0 <= num_tokens <= 3:
return ''*num_tokens
if num_tokens == 4:
return '●●\\n●●'
return str(num_tokens)
def render_petri_net(od: ODAPI):
dot = ""
dot += "rankdir=LR;"
dot += "center=true;"
dot += "margin=1;"
dot += "nodesep=1;"
dot += "edge [arrowhead=vee];"
dot += "node[fontname=Arial,fontsize=10];\n"
dot += "subgraph places {"
dot += " node [shape=circle,fixedsize=true,label=\"\", height=.35,width=.35];"
for _, place in od.get_all_instances("PNPlace"):
place_name = od.get_name(place)
try:
place_state = od.get_source(od.get_incoming(place, "pn_of")[0])
num_tokens = od.get_slot_value(place_state, "numTokens")
except IndexError:
num_tokens = 0
dot += f" {place_name} [label=\"{place_name}\\n\\n{render_tokens(num_tokens)}\\n\\n­\"];\n"
dot += "}\n"
dot += "subgraph transitions {"
dot += " node [shape=rect,fixedsize=true,height=.3,width=.12,style=filled,fillcolor=black,color=white];\n"
for transition_name, _ in od.get_all_instances("PNTransition"):
dot += f" {transition_name} [label=\"{transition_name}\\n\\n\\n­\"];\n"
dot += "}\n"
for _, arc in od.get_all_instances("arc"):
src_name = od.get_name(od.get_source(arc))
tgt_name = od.get_name(od.get_target(arc))
dot += f"{src_name} -> {tgt_name};"
show_graphviz(dot, engine="neato")
return ""