renderer.py 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. from concrete_syntax.common import indent
  2. from concrete_syntax.graphviz.make_url import make_url
  3. from examples.semantics.operational.port.helpers import design_to_state, state_to_design, get_time, get_num_ships
  4. def render_port_to_dot(od,
  5. make_id=lambda name,obj: name # by default, we just use the object name for the graphviz node name
  6. ):
  7. txt = ""
  8. def render_place(place):
  9. name = od.get_name(place)
  10. return f'"{make_id(name,place)}" [ label = "{name}\\n ships = {get_num_ships(od, place)}", style = filled, fillcolor = lightblue ]\n'
  11. for _, cap in od.get_all_instances("CapacityConstraint", include_subtypes=False):
  12. name = od.get_name(cap)
  13. capacity = od.get_slot_value(cap, "shipCapacity")
  14. txt += f'subgraph cluster_{name} {{\n label = "{name}\\n capacity = {capacity}";\n'
  15. for lnk in od.get_outgoing(cap, "capacityOf"):
  16. place = od.get_target(lnk)
  17. txt += f' {render_place(place)}'
  18. txt += f'}}\n'
  19. for _, place_state in od.get_all_instances("PlaceState", include_subtypes=False):
  20. place = state_to_design(od, place_state)
  21. if len(od.get_incoming(place, "capacityOf")) == 0:
  22. txt += render_place(place)
  23. for _, berth_state in od.get_all_instances("BerthState", include_subtypes=False):
  24. berth = state_to_design(od, berth_state)
  25. name = od.get_name(berth)
  26. txt += f'"{make_id(name,berth)}" [ label = "{name}\\n numShips = {get_num_ships(od, berth)}\\n status = {od.get_slot_value(berth_state, "status")}", fillcolor = yellow, style = filled]\n'
  27. for _, gen in od.get_all_instances("Generator", include_subtypes=False):
  28. txt += f'"{make_id(od.get_name(gen),gen)}" [ label = "+", shape = diamond, fillcolor = green, fontsize = 30, style = filled ]\n'
  29. for _, conn in od.get_all_instances("connection"):
  30. src = od.get_source(conn)
  31. tgt = od.get_target(conn)
  32. moved = od.get_slot_value(design_to_state(od, conn), "moved")
  33. src_name = od.get_name(src)
  34. tgt_name = od.get_name(tgt)
  35. txt += f"{make_id(src_name,src)} -> {make_id(tgt_name,tgt)} [color=deepskyblue3, penwidth={1 if moved else 2}];\n"
  36. for _, workers in od.get_all_instances("WorkerSet"):
  37. already_have = []
  38. name = od.get_name(workers)
  39. num_workers = od.get_slot_value(workers, "numWorkers")
  40. txt += f'{make_id(name,workers)} [label="{num_workers} worker(s)", shape=parallelogram, fillcolor=chocolate, style=filled];\n'
  41. for lnk in od.get_outgoing(design_to_state(od, workers), "isOperating"):
  42. berth = od.get_target(lnk)
  43. already_have.append(berth)
  44. txt += f"{make_id(name,workers)} -> {make_id(od.get_name(berth),berth)} [arrowhead=none, color=chocolate];\n"
  45. for lnk in od.get_outgoing(workers, "canOperate"):
  46. berth = od.get_target(lnk)
  47. if berth not in already_have:
  48. txt += f"{make_id(name,workers)} -> {make_id(od.get_name(berth),berth)} [style=dotted, arrowhead=none, color=chocolate];\n"
  49. return txt
  50. def render_port_graphviz(od):
  51. return make_url(render_port_to_dot(od))
  52. def render_port_textual(od):
  53. txt = ""
  54. for _, place_state in od.get_all_instances("PlaceState", include_subtypes=False):
  55. place = state_to_design(od, place_state)
  56. name = od.get_name(place)
  57. txt += f'place "{name}" {"🚢"*get_num_ships(od, place)}\n'
  58. for _, berth_state in od.get_all_instances("BerthState", include_subtypes=False):
  59. berth = state_to_design(od, berth_state)
  60. name = od.get_name(berth)
  61. operated_descr = ""
  62. if len(od.get_incoming(berth, "isOperating")):
  63. operated_descr = " and being operated"
  64. txt += f'berth "{name}" {"🚢"*get_num_ships(od, berth)} {od.get_slot_value(berth_state, "status")}{operated_descr}\n'
  65. return txt