我正在尝试使用 R 的 bpmnR 包开发符合 bpmn 的流程图。我已经设置了一个基本流程,但在渲染中遇到了问题。
有没有办法以线性方式显示图表?
我使用的代码是:
library(dplyr)
library(bpmnR)
# Create data for BPMN elements
nodes <- tibble(id = "task", name = "Task name",
objectType = "task", gatewayDirection = NA)
events <- tibble(id = c("start", "end"),
name = c("Start event", "End event"),
objectType = c("startEvent", "endEvent"))
flows <- tibble(id = c("flow1", "flow2"),
name = c("flow1", "flow2"),
sourceRef = c("start", "task"),
targetRef = c("task", "end"),
objectType = c("sequenceFlow", "sequenceFlow"))
# Create the BPMN model
model <- create_bpmn(nodes, flows, events)
# Render the BPMN diagram
render_bpmn(
model, # Use 'model' here
viewer.suppress = FALSE,
width = NULL,
height = NULL,
elementId = NULL,
xml_version_number = "1.0",
xml_encoding_declaration = "UTF-8"
)
如评论中所述,没有现成的方法可以实现此目的,但可以使用
xml2
包和一些试验和错误来修改模型对象。
下面的示例使用坐标 0,0 作为锚点,以便更容易确定在何处绘制每个元素。请注意,x 和 y 偏移量基于每个对象的默认大小:
如果您的模型对象的这些不同,您可以检查
model$xml
以查找宽度和高度变量并调整偏移量以适应。
library(dplyr)
library(bpmnR)
library(xml2)
# Create data for BPMN elements
nodes <- tibble(id = "task", name = "Task name",
objectType = "task", gatewayDirection = NA)
events <- tibble(id = c("start", "end"),
name = c("Start event", "End event"),
objectType = c("startEvent", "endEvent"))
flows <- tibble(id = c("flow1", "flow2"),
name = c("flow1", "flow2"),
sourceRef = c("start", "task"),
targetRef = c("task", "end"),
objectType = c("sequenceFlow", "sequenceFlow"))
# Create the BPMN model
model <- create_bpmn(nodes, flows, events)
# Convert model xml as an xml object
mod_xml <- read_xml(model$xml)
# Return name and id values for all mod_xml elements (your id values will differ)
nodes <- xml_find_all(mod_xml, "//*[@id and @name]")
nodes <- data.frame(
id = sapply(nodes, function(x) xml_attr(x, "id")),
name = sapply(nodes, function(x) xml_attr(x, "name"))
)
nodes
# id name
# 1 id_050c2a4c-6205-4f2b-a1e1-45ce40c2584c Task name
# 2 id_d863e666-fd07-4778-bfef-e517d686234d Start event
# 3 id_1967ee21-3f1c-46db-b944-9a949a46b757 End event
# 4 id_af62389a-4f4b-4cbc-9e33-8b77f615a3cf flow1
# 5 id_1eefc447-26a0-4543-a655-a93ca9eefcd2 flow2
# Path to Task name
t_xy <- paste0(
".//bpmndi:BPMNShape[@bpmnElement='", nodes[nodes$name == "Task name", "id"], "']"
)
# Return Task name BPMNShape
shape_node <- xml_find_first(mod_xml, t_xy)
# Get Task name coordinates
bounds_node <- xml_find_first(shape_node, ".//dc:Bounds")
# Adjust coordinates
xml_set_attr(bounds_node, "x", "200")
xml_set_attr(bounds_node, "y", "0")
# Path to Start event
s_xy <- paste0(
".//bpmndi:BPMNShape[@bpmnElement='", nodes[nodes$name == "Start event", "id"], "']"
)
# Return BPMNShape
shape_node <- xml_find_first(mod_xml, s_xy)
# Get Start event coordinates
bounds_node <- xml_find_first(shape_node, ".//dc:Bounds")
# Adjust coordinates
xml_set_attr(bounds_node, "x", "0")
xml_set_attr(bounds_node, "y", "0")
# Path to End event
e_xy <- paste0(
".//bpmndi:BPMNShape[@bpmnElement='", nodes[nodes$name == "End event", "id"], "']"
)
# Return End event BPMNShape
shape_node <- xml_find_first(mod_xml, e_xy)
# Get plot coordinates
bounds_node <- xml_find_first(shape_node, ".//dc:Bounds")
# Adjust coordinates
xml_set_attr(bounds_node, "x", "464")
xml_set_attr(bounds_node, "y", "40")
# Path to flow1
f1_xy <- paste0(
".//bpmndi:BPMNEdge[@bpmnElement='", nodes[nodes$name == "flow1", "id"], "']"
)
# Return BPMNEdge
shape_node <- xml_find_first(mod_xml, f1_xy)
# Get plot coordinates
bounds_node <- xml_find_all(shape_node, ".//di:waypoint")
# Adjust coordinates
xml_set_attr(bounds_node[1], "x", "36")
xml_set_attr(bounds_node[1], "y", "18")
xml_set_attr(bounds_node[2:3], "x", "200")
xml_set_attr(bounds_node[2:3], "y", "40")
# Path to flo2
f2_xy <- paste0(
".//bpmndi:BPMNEdge[@bpmnElement='", nodes[nodes$name == "flow2", "id"], "']"
)
# Return BPMNEdge
shape_node <- xml_find_first(mod_xml, f2_xy)
# Get plot coordinates
bounds_node <- xml_find_all(shape_node, ".//di:waypoint")
# Adjust coordinates
xml_set_attr(bounds_node[1], "x", "300")
xml_set_attr(bounds_node[1], "y", "40")
xml_set_attr(bounds_node[2:3], "x", "464")
xml_set_attr(bounds_node[2:3], "y", "58")
# Update model xml
model$xml <- mod_xml
# Render updated model
render_bpmn(
model,
viewer.suppress = FALSE,
width = NULL,
height = NULL,
elementId = NULL,
xml_version_number = "1.0",
xml_encoding_declaration = "UTF-8"
)