Skip to content

Commit

Permalink
bugfix: 修复环问题
Browse files Browse the repository at this point in the history
  • Loading branch information
hanshuaikang committed Oct 30, 2023
1 parent 6b5ee4e commit f0f74ef
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 20 deletions.
44 changes: 26 additions & 18 deletions bamboo_engine/builder/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ def _get_next_node(node, pipeline_tree):

out_goings = node["outgoing"]

# 说明曾经去除过环,此时没有out_goings
if out_goings == "":
return []

# 当只有一个输出时,
if not isinstance(out_goings, list):
out_goings = [out_goings]
Expand Down Expand Up @@ -211,7 +215,7 @@ def inject_pipeline_token(node, pipeline_tree, node_token_map, token):
if node["type"] in ["ParallelGateway", "ExclusiveGateway", "ConditionalParallelGateway"]:
next_nodes = _get_next_node(node, pipeline_tree)
node_token = unique_id("t")
target_node = None
target_nodes = {}
for next_node in next_nodes:
# 分支网关各个分支token相同
node_token_map[next_node["id"]] = node_token
Expand All @@ -220,36 +224,40 @@ def inject_pipeline_token(node, pipeline_tree, node_token_map, token):
node_token = unique_id("t")
node_token_map[next_node["id"]] = node_token

# 如果是网关,沿着路径向内搜索,最终遇到对应的分支网关会返回
# 如果是并行网关,沿着路径向内搜索,最终遇到对应的汇聚网关会返回
target_node = inject_pipeline_token(next_node, pipeline_tree, node_token_map, node_token)

if target_node is None:
return

# 汇聚网关可以直连结束节点,所以可能会存在找不到对应的汇聚网关的情况
if target_node["type"] in ["EmptyEndEvent", "ExecutableEndEvent"]:
if target_node:
target_nodes[target_node["id"]] = target_node

for target_node in target_nodes.values():
# 汇聚网关可以直连结束节点,所以可能会存在找不到对应的汇聚网关的情况
if target_node["type"] in ["EmptyEndEvent", "ExecutableEndEvent"]:
node_token_map[target_node["id"]] = token
continue
# 汇聚网关的token等于对应的网关的token
node_token_map[target_node["id"]] = token
return
# 汇聚网关的token等于对应的网关的token
node_token_map[target_node["id"]] = token
# 到汇聚网关之后,此时继续向下遍历
next_node = _get_next_node(target_node, pipeline_tree)[0]
# 汇聚网关只会有一个出度
node_token_map[next_node["id"]] = token
return inject_pipeline_token(next_node, pipeline_tree, node_token_map, token)
# 到汇聚网关之后,此时继续向下遍历
next_node = _get_next_node(target_node, pipeline_tree)[0]
# 汇聚网关只会有一个出度
node_token_map[next_node["id"]] = token
inject_pipeline_token(next_node, pipeline_tree, node_token_map, token)

# 如果是汇聚网关,并且id等于converge_id,说明此时遍历在某个单元
if node["type"] == "ConvergeGateway":
return node

# 如果是普通的节点,说明只有一个出度,此时直接向下遍历就好
if node["type"] in ["ServiceActivity", "EmptyStartEvent"]:
next_node = _get_next_node(node, pipeline_tree)[0]
next_node_list = _get_next_node(node, pipeline_tree)
# 此时有可能遇到一个去环的节点,该节点没有
if not next_node_list:
return
next_node = next_node_list[0]
node_token_map[next_node["id"]] = token
return inject_pipeline_token(next_node, pipeline_tree, node_token_map, token)

# 如果遇到结束节点,直接返回
if node["type"] == ["EmptyEndEvent", "ExecutableEndEvent"]:
if node["type"] in ["EmptyEndEvent", "ExecutableEndEvent"]:
return node

if node["type"] == "SubProcess":
Expand Down
35 changes: 33 additions & 2 deletions tests/builder/test_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ def test_inject_pipeline_token_normal():

start.extend(act).extend(end)
pipeline = build_tree(start)

node_token_map = generate_pipeline_token(pipeline)

assert get_node_token(pipeline, "act_1", node_token_map) == get_node_token(pipeline, "start_event", node_token_map)
Expand All @@ -54,6 +53,39 @@ def test_inject_pipeline_token_normal():
)


def test_inject_pipeline_token_with_complex_cycle():
start = EmptyStartEvent()
end = EmptyEndEvent()

write_document = ServiceActivity(name="act_1", component_code="example_component")
review_document = ServiceActivity(name="act_2", component_code="example_component")
rework_document = ServiceActivity(name="act_3", component_code="example_component")

release_document = ServiceActivity(name="act_4", component_code="example_component")
gateway = ExclusiveGateway(
conditions={0: "${act_1_output} < 0", 1: "${act_1_output} >= 0", 2: "${act_1_output} >= 0"},
name="ExclusiveGateway",
)

start.extend(write_document).extend(review_document).extend(gateway).connect(
release_document, end, rework_document
).to(release_document).extend(end)
rework_document.extend(review_document)
pipeline = build_tree(start)
node_token_map = generate_pipeline_token(pipeline)

assert (
get_node_token(pipeline, "start_event", node_token_map)
== get_node_token(pipeline, "act_1", node_token_map)
== get_node_token(pipeline, "act_2", node_token_map)
== get_node_token(pipeline, "ExclusiveGateway", node_token_map)
== get_node_token(pipeline, "end_event", node_token_map)
!= get_node_token(pipeline, "act_3", node_token_map)
)

assert get_node_token(pipeline, "act_4", node_token_map) == get_node_token(pipeline, "act_3", node_token_map)


def test_inject_pipeline_token_parallel_gateway():
start = EmptyStartEvent()
pg = ParallelGateway(name="ParallelGateway")
Expand All @@ -67,7 +99,6 @@ def test_inject_pipeline_token_parallel_gateway():

pipeline = build_tree(start)
node_token_map = generate_pipeline_token(pipeline)

assert (
get_node_token(pipeline, "start_event", node_token_map)
== get_node_token(pipeline, "ParallelGateway", node_token_map)
Expand Down

0 comments on commit f0f74ef

Please sign in to comment.