From 1576b6edc624741fa406a631a4238c000d1208c3 Mon Sep 17 00:00:00 2001 From: wangqi Date: Fri, 6 Sep 2024 21:43:55 +0800 Subject: [PATCH] feature: add workflow definition graph and service --- .../carp/framework/dag/algorithm/DAG.java | 27 +++++-- .../dag/algorithm/DefaultDagEdge.java | 33 ++++++++ .../WorkflowDefinitionAttrs.java} | 6 +- .../api/graph/WorkflowDefinitionGraph.java | 9 ++- .../api/graph/WorkflowDefinitionMeta.java | 26 ++++++ .../api/graph/WorkflowExecutionGraph.java | 9 ++- .../api/graph/WorkflowTaskDefinition.java | 36 +++++++++ .../graph/WorkflowTaskDefinitionAttrs.java | 26 ++++++ .../api/graph/WorkflowTaskDefinitionMeta.java | 26 ++++++ .../WorkflowTaskInstance.java} | 34 ++++---- .../api/manager/WorkflowInstanceManager.java | 30 +++++++ .../manager/WorkflowTaskInstanceManager.java | 30 +++++++ .../api/service/WorkflowDagService.java | 2 + .../service/WorkflowDefinitionService.java | 29 +++++++ .../WorkflowTaskDefinitionConvert.java | 54 +++++++++++++ .../convert/WorkflowTaskInstanceConvert.java | 45 +++++++++++ .../service/impl/WorkflowDagServiceImpl.java | 9 +++ .../impl/WorkflowDefinitionServiceImpl.java | 80 +++++++++++++++++++ 18 files changed, 476 insertions(+), 35 deletions(-) create mode 100644 carp-framework/carp-framework-dag/src/main/java/cn/sliew/carp/framework/dag/algorithm/DefaultDagEdge.java rename carp-modules/carp-module-workflow/carp-module-workflow-api/src/main/java/cn/sliew/carp/module/workflow/api/{service/dto/WorkflowDefinitionDagMeta.java => graph/WorkflowDefinitionAttrs.java} (85%) create mode 100644 carp-modules/carp-module-workflow/carp-module-workflow-api/src/main/java/cn/sliew/carp/module/workflow/api/graph/WorkflowDefinitionMeta.java create mode 100644 carp-modules/carp-module-workflow/carp-module-workflow-api/src/main/java/cn/sliew/carp/module/workflow/api/graph/WorkflowTaskDefinition.java create mode 100644 carp-modules/carp-module-workflow/carp-module-workflow-api/src/main/java/cn/sliew/carp/module/workflow/api/graph/WorkflowTaskDefinitionAttrs.java create mode 100644 carp-modules/carp-module-workflow/carp-module-workflow-api/src/main/java/cn/sliew/carp/module/workflow/api/graph/WorkflowTaskDefinitionMeta.java rename carp-modules/carp-module-workflow/carp-module-workflow-api/src/main/java/cn/sliew/carp/module/workflow/api/{service/dto/WorkflowDefinitionDTO.java => graph/WorkflowTaskInstance.java} (57%) create mode 100644 carp-modules/carp-module-workflow/carp-module-workflow-api/src/main/java/cn/sliew/carp/module/workflow/api/manager/WorkflowInstanceManager.java create mode 100644 carp-modules/carp-module-workflow/carp-module-workflow-api/src/main/java/cn/sliew/carp/module/workflow/api/manager/WorkflowTaskInstanceManager.java create mode 100644 carp-modules/carp-module-workflow/carp-module-workflow-api/src/main/java/cn/sliew/carp/module/workflow/api/service/WorkflowDefinitionService.java create mode 100644 carp-modules/carp-module-workflow/carp-module-workflow-api/src/main/java/cn/sliew/carp/module/workflow/api/service/convert/WorkflowTaskDefinitionConvert.java create mode 100644 carp-modules/carp-module-workflow/carp-module-workflow-api/src/main/java/cn/sliew/carp/module/workflow/api/service/convert/WorkflowTaskInstanceConvert.java create mode 100644 carp-modules/carp-module-workflow/carp-module-workflow-api/src/main/java/cn/sliew/carp/module/workflow/api/service/impl/WorkflowDefinitionServiceImpl.java diff --git a/carp-framework/carp-framework-dag/src/main/java/cn/sliew/carp/framework/dag/algorithm/DAG.java b/carp-framework/carp-framework-dag/src/main/java/cn/sliew/carp/framework/dag/algorithm/DAG.java index 6121da0e..3cfd3c77 100644 --- a/carp-framework/carp-framework-dag/src/main/java/cn/sliew/carp/framework/dag/algorithm/DAG.java +++ b/carp-framework/carp-framework-dag/src/main/java/cn/sliew/carp/framework/dag/algorithm/DAG.java @@ -21,7 +21,6 @@ import org.jgrapht.Graph; import org.jgrapht.GraphPath; import org.jgrapht.alg.shortestpath.AllDirectedPaths; -import org.jgrapht.graph.DefaultEdge; import org.jgrapht.graph.builder.GraphTypeBuilder; import org.jgrapht.traverse.BreadthFirstIterator; import org.jgrapht.traverse.TopologicalOrderIterator; @@ -32,11 +31,27 @@ public class DAG { - private Graph jgrapht = GraphTypeBuilder.directed() + private Graph> jgrapht = GraphTypeBuilder.>directed() .allowingSelfLoops(false) .weighted(false) .buildGraph(); + public void addNode(N node) { + jgrapht.addVertex(node); + } + + public void addEdge(N source, N target) { + jgrapht.addEdge(source, target, new DefaultDagEdge<>(source, target)); + } + + public Set nodes() { + return jgrapht.vertexSet(); + } + + public Set> edges() { + return jgrapht.edgeSet(); + } + public Set getSources() { return jgrapht.vertexSet().stream() .filter(node -> jgrapht.inDegreeOf(node) == 0) @@ -50,7 +65,7 @@ public Set getSinks() { } public Integer getMaxDepth() { - AllDirectedPaths paths = new AllDirectedPaths<>(jgrapht); + AllDirectedPaths> paths = new AllDirectedPaths<>(jgrapht); return paths.getAllPaths(getSources(), getSinks(), true, null) .stream().map(GraphPath::getLength) .sorted() @@ -58,14 +73,14 @@ public Integer getMaxDepth() { } public void topologyTraversal(Consumer consumer) { - TopologicalOrderIterator iterator = new TopologicalOrderIterator<>(jgrapht); + TopologicalOrderIterator> iterator = new TopologicalOrderIterator<>(jgrapht); while (iterator.hasNext()) { consumer.accept(iterator.next()); } } public void breadthFirstTraversal(Consumer consumer) { - BreadthFirstIterator iterator = new BreadthFirstIterator<>(jgrapht); + BreadthFirstIterator> iterator = new BreadthFirstIterator<>(jgrapht); while (iterator.hasNext()) { N node = iterator.next(); int depth = iterator.getDepth(node); @@ -82,7 +97,7 @@ public void executeBFS(Consumer consumer) { public void doExecuteBFS(Integer depth, Integer maxDepth, Consumer consumer) { // 使用 BFS,计算每个节点所在的层级 - BreadthFirstIterator iterator = new BreadthFirstIterator<>(jgrapht); + BreadthFirstIterator> iterator = new BreadthFirstIterator<>(jgrapht); while (iterator.hasNext()) { N node = iterator.next(); if (iterator.getDepth(node) == depth) { diff --git a/carp-framework/carp-framework-dag/src/main/java/cn/sliew/carp/framework/dag/algorithm/DefaultDagEdge.java b/carp-framework/carp-framework-dag/src/main/java/cn/sliew/carp/framework/dag/algorithm/DefaultDagEdge.java new file mode 100644 index 00000000..2e741142 --- /dev/null +++ b/carp-framework/carp-framework-dag/src/main/java/cn/sliew/carp/framework/dag/algorithm/DefaultDagEdge.java @@ -0,0 +1,33 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package cn.sliew.carp.framework.dag.algorithm; + +import lombok.Getter; + +@Getter +public class DefaultDagEdge { + + private final N source; + private final N target; + + public DefaultDagEdge(N source, N target) { + this.source = source; + this.target = target; + } +} diff --git a/carp-modules/carp-module-workflow/carp-module-workflow-api/src/main/java/cn/sliew/carp/module/workflow/api/service/dto/WorkflowDefinitionDagMeta.java b/carp-modules/carp-module-workflow/carp-module-workflow-api/src/main/java/cn/sliew/carp/module/workflow/api/graph/WorkflowDefinitionAttrs.java similarity index 85% rename from carp-modules/carp-module-workflow/carp-module-workflow-api/src/main/java/cn/sliew/carp/module/workflow/api/service/dto/WorkflowDefinitionDagMeta.java rename to carp-modules/carp-module-workflow/carp-module-workflow-api/src/main/java/cn/sliew/carp/module/workflow/api/graph/WorkflowDefinitionAttrs.java index 22987507..c6b503a1 100644 --- a/carp-modules/carp-module-workflow/carp-module-workflow-api/src/main/java/cn/sliew/carp/module/workflow/api/service/dto/WorkflowDefinitionDagMeta.java +++ b/carp-modules/carp-module-workflow/carp-module-workflow-api/src/main/java/cn/sliew/carp/module/workflow/api/graph/WorkflowDefinitionAttrs.java @@ -16,13 +16,11 @@ * limitations under the License. */ -package cn.sliew.carp.module.workflow.api.service.dto; +package cn.sliew.carp.module.workflow.api.graph; import lombok.Data; @Data -public class WorkflowDefinitionDagMeta { - - private Long scheduleJobInstanceId; +public class WorkflowDefinitionAttrs { } diff --git a/carp-modules/carp-module-workflow/carp-module-workflow-api/src/main/java/cn/sliew/carp/module/workflow/api/graph/WorkflowDefinitionGraph.java b/carp-modules/carp-module-workflow/carp-module-workflow-api/src/main/java/cn/sliew/carp/module/workflow/api/graph/WorkflowDefinitionGraph.java index 5116194a..5dac6c64 100644 --- a/carp-modules/carp-module-workflow/carp-module-workflow-api/src/main/java/cn/sliew/carp/module/workflow/api/graph/WorkflowDefinitionGraph.java +++ b/carp-modules/carp-module-workflow/carp-module-workflow-api/src/main/java/cn/sliew/carp/module/workflow/api/graph/WorkflowDefinitionGraph.java @@ -18,10 +18,11 @@ package cn.sliew.carp.module.workflow.api.graph; -import cn.sliew.carp.framework.dag.service.dto.DagConfigStepDTO; -import com.google.common.graph.Graph; +import cn.sliew.carp.framework.dag.algorithm.DAG; +import lombok.Data; -public interface WorkflowDefinitionGraph { +@Data +public class WorkflowDefinitionGraph { - Graph getGraph(); + private DAG dag; } diff --git a/carp-modules/carp-module-workflow/carp-module-workflow-api/src/main/java/cn/sliew/carp/module/workflow/api/graph/WorkflowDefinitionMeta.java b/carp-modules/carp-module-workflow/carp-module-workflow-api/src/main/java/cn/sliew/carp/module/workflow/api/graph/WorkflowDefinitionMeta.java new file mode 100644 index 00000000..a0e20eb2 --- /dev/null +++ b/carp-modules/carp-module-workflow/carp-module-workflow-api/src/main/java/cn/sliew/carp/module/workflow/api/graph/WorkflowDefinitionMeta.java @@ -0,0 +1,26 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package cn.sliew.carp.module.workflow.api.graph; + +import lombok.Data; + +@Data +public class WorkflowDefinitionMeta { + +} diff --git a/carp-modules/carp-module-workflow/carp-module-workflow-api/src/main/java/cn/sliew/carp/module/workflow/api/graph/WorkflowExecutionGraph.java b/carp-modules/carp-module-workflow/carp-module-workflow-api/src/main/java/cn/sliew/carp/module/workflow/api/graph/WorkflowExecutionGraph.java index b7b98419..db813c5d 100644 --- a/carp-modules/carp-module-workflow/carp-module-workflow-api/src/main/java/cn/sliew/carp/module/workflow/api/graph/WorkflowExecutionGraph.java +++ b/carp-modules/carp-module-workflow/carp-module-workflow-api/src/main/java/cn/sliew/carp/module/workflow/api/graph/WorkflowExecutionGraph.java @@ -18,10 +18,11 @@ package cn.sliew.carp.module.workflow.api.graph; -import cn.sliew.carp.framework.dag.service.dto.DagStepDTO; -import com.google.common.graph.Graph; +import cn.sliew.carp.framework.dag.algorithm.DAG; +import lombok.Data; -public interface WorkflowExecutionGraph { +@Data +public class WorkflowExecutionGraph { - Graph getGraph(); + private DAG dag; } diff --git a/carp-modules/carp-module-workflow/carp-module-workflow-api/src/main/java/cn/sliew/carp/module/workflow/api/graph/WorkflowTaskDefinition.java b/carp-modules/carp-module-workflow/carp-module-workflow-api/src/main/java/cn/sliew/carp/module/workflow/api/graph/WorkflowTaskDefinition.java new file mode 100644 index 00000000..ada05730 --- /dev/null +++ b/carp-modules/carp-module-workflow/carp-module-workflow-api/src/main/java/cn/sliew/carp/module/workflow/api/graph/WorkflowTaskDefinition.java @@ -0,0 +1,36 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package cn.sliew.carp.module.workflow.api.graph; + +import cn.sliew.carp.framework.common.model.BaseDTO; +import lombok.Data; + +@Data +public class WorkflowTaskDefinition extends BaseDTO { + + private Long dagId; + + private String stepId; + + private String name; + + private WorkflowTaskDefinitionMeta meta; + + private WorkflowTaskDefinitionAttrs attrs; +} diff --git a/carp-modules/carp-module-workflow/carp-module-workflow-api/src/main/java/cn/sliew/carp/module/workflow/api/graph/WorkflowTaskDefinitionAttrs.java b/carp-modules/carp-module-workflow/carp-module-workflow-api/src/main/java/cn/sliew/carp/module/workflow/api/graph/WorkflowTaskDefinitionAttrs.java new file mode 100644 index 00000000..b3d67c00 --- /dev/null +++ b/carp-modules/carp-module-workflow/carp-module-workflow-api/src/main/java/cn/sliew/carp/module/workflow/api/graph/WorkflowTaskDefinitionAttrs.java @@ -0,0 +1,26 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package cn.sliew.carp.module.workflow.api.graph; + +import lombok.Data; + +@Data +public class WorkflowTaskDefinitionAttrs { + +} diff --git a/carp-modules/carp-module-workflow/carp-module-workflow-api/src/main/java/cn/sliew/carp/module/workflow/api/graph/WorkflowTaskDefinitionMeta.java b/carp-modules/carp-module-workflow/carp-module-workflow-api/src/main/java/cn/sliew/carp/module/workflow/api/graph/WorkflowTaskDefinitionMeta.java new file mode 100644 index 00000000..e6dcbe45 --- /dev/null +++ b/carp-modules/carp-module-workflow/carp-module-workflow-api/src/main/java/cn/sliew/carp/module/workflow/api/graph/WorkflowTaskDefinitionMeta.java @@ -0,0 +1,26 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package cn.sliew.carp.module.workflow.api.graph; + +import lombok.Data; + +@Data +public class WorkflowTaskDefinitionMeta { + +} diff --git a/carp-modules/carp-module-workflow/carp-module-workflow-api/src/main/java/cn/sliew/carp/module/workflow/api/service/dto/WorkflowDefinitionDTO.java b/carp-modules/carp-module-workflow/carp-module-workflow-api/src/main/java/cn/sliew/carp/module/workflow/api/graph/WorkflowTaskInstance.java similarity index 57% rename from carp-modules/carp-module-workflow/carp-module-workflow-api/src/main/java/cn/sliew/carp/module/workflow/api/service/dto/WorkflowDefinitionDTO.java rename to carp-modules/carp-module-workflow/carp-module-workflow-api/src/main/java/cn/sliew/carp/module/workflow/api/graph/WorkflowTaskInstance.java index a7d8ddbd..292ce682 100644 --- a/carp-modules/carp-module-workflow/carp-module-workflow-api/src/main/java/cn/sliew/carp/module/workflow/api/service/dto/WorkflowDefinitionDTO.java +++ b/carp-modules/carp-module-workflow/carp-module-workflow-api/src/main/java/cn/sliew/carp/module/workflow/api/graph/WorkflowTaskInstance.java @@ -16,34 +16,34 @@ * limitations under the License. */ -package cn.sliew.carp.module.workflow.api.service.dto; +package cn.sliew.carp.module.workflow.api.graph; -import cn.sliew.carp.framework.common.dict.workflow.WorkflowType; import cn.sliew.carp.framework.common.model.BaseDTO; -import cn.sliew.carp.framework.dag.service.dto.DagConfigComplexDTO; +import cn.sliew.carp.framework.dag.service.dto.DagConfigStepDTO; +import com.fasterxml.jackson.databind.JsonNode; import io.swagger.v3.oas.annotations.media.Schema; import lombok.Data; -import java.util.Map; +import java.util.Date; @Data -public class WorkflowDefinitionDTO extends BaseDTO { +public class WorkflowTaskInstance extends BaseDTO { - @Schema(description = "workflow type") - private WorkflowType type; + private Long dagInstanceId; - @Schema(description = "workflow name") - private String name; + @Schema(description = "步骤") + private WorkflowTaskDefinition definition; - @Schema(description = "workflow param") - private Map param; + private String uuid; - @Schema(description = "备注") - private String remark; + private JsonNode inputs; - @Schema(description = "schedule") - private WorkflowScheduleDTO schedule; + private JsonNode outputs; + + private String status; + + private Date startTime; + + private Date endTime; - @Schema(description = "dag") - private DagConfigComplexDTO dag; } diff --git a/carp-modules/carp-module-workflow/carp-module-workflow-api/src/main/java/cn/sliew/carp/module/workflow/api/manager/WorkflowInstanceManager.java b/carp-modules/carp-module-workflow/carp-module-workflow-api/src/main/java/cn/sliew/carp/module/workflow/api/manager/WorkflowInstanceManager.java new file mode 100644 index 00000000..21b26cc4 --- /dev/null +++ b/carp-modules/carp-module-workflow/carp-module-workflow-api/src/main/java/cn/sliew/carp/module/workflow/api/manager/WorkflowInstanceManager.java @@ -0,0 +1,30 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package cn.sliew.carp.module.workflow.api.manager; + +public interface WorkflowInstanceManager { + + void deploy(Long workflowDefinitionId); + + void shutdown(Long id); + + void suspend(Long id); + + void resume(Long id); +} diff --git a/carp-modules/carp-module-workflow/carp-module-workflow-api/src/main/java/cn/sliew/carp/module/workflow/api/manager/WorkflowTaskInstanceManager.java b/carp-modules/carp-module-workflow/carp-module-workflow-api/src/main/java/cn/sliew/carp/module/workflow/api/manager/WorkflowTaskInstanceManager.java new file mode 100644 index 00000000..af491afe --- /dev/null +++ b/carp-modules/carp-module-workflow/carp-module-workflow-api/src/main/java/cn/sliew/carp/module/workflow/api/manager/WorkflowTaskInstanceManager.java @@ -0,0 +1,30 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package cn.sliew.carp.module.workflow.api.manager; + +public interface WorkflowTaskInstanceManager { + + void deploy(Long id); + + void shutdown(Long id); + + void suspend(Long id); + + void resume(Long id); +} diff --git a/carp-modules/carp-module-workflow/carp-module-workflow-api/src/main/java/cn/sliew/carp/module/workflow/api/service/WorkflowDagService.java b/carp-modules/carp-module-workflow/carp-module-workflow-api/src/main/java/cn/sliew/carp/module/workflow/api/service/WorkflowDagService.java index d66a8225..f871b864 100644 --- a/carp-modules/carp-module-workflow/carp-module-workflow-api/src/main/java/cn/sliew/carp/module/workflow/api/service/WorkflowDagService.java +++ b/carp-modules/carp-module-workflow/carp-module-workflow-api/src/main/java/cn/sliew/carp/module/workflow/api/service/WorkflowDagService.java @@ -20,6 +20,7 @@ import cn.sliew.carp.framework.dag.service.dto.DagConfigComplexDTO; import cn.sliew.carp.framework.dag.service.dto.DagConfigStepDTO; +import cn.sliew.carp.framework.dag.service.dto.DagInstanceComplexDTO; import cn.sliew.carp.framework.dag.x6.graph.DagGraphVO; public interface WorkflowDagService { @@ -34,4 +35,5 @@ public interface WorkflowDagService { void update(Long dagId, DagGraphVO graph); + DagInstanceComplexDTO getDagInstance(Long dagInstanceId); } diff --git a/carp-modules/carp-module-workflow/carp-module-workflow-api/src/main/java/cn/sliew/carp/module/workflow/api/service/WorkflowDefinitionService.java b/carp-modules/carp-module-workflow/carp-module-workflow-api/src/main/java/cn/sliew/carp/module/workflow/api/service/WorkflowDefinitionService.java new file mode 100644 index 00000000..a6ab6f85 --- /dev/null +++ b/carp-modules/carp-module-workflow/carp-module-workflow-api/src/main/java/cn/sliew/carp/module/workflow/api/service/WorkflowDefinitionService.java @@ -0,0 +1,29 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package cn.sliew.carp.module.workflow.api.service; + +import cn.sliew.carp.module.workflow.api.graph.WorkflowDefinitionGraph; +import cn.sliew.carp.module.workflow.api.graph.WorkflowExecutionGraph; + +public interface WorkflowDefinitionService { + + WorkflowDefinitionGraph get(Long dagId); + + WorkflowExecutionGraph getExecutionGraph(Long dagInstanceId); +} diff --git a/carp-modules/carp-module-workflow/carp-module-workflow-api/src/main/java/cn/sliew/carp/module/workflow/api/service/convert/WorkflowTaskDefinitionConvert.java b/carp-modules/carp-module-workflow/carp-module-workflow-api/src/main/java/cn/sliew/carp/module/workflow/api/service/convert/WorkflowTaskDefinitionConvert.java new file mode 100644 index 00000000..d6f2eb03 --- /dev/null +++ b/carp-modules/carp-module-workflow/carp-module-workflow-api/src/main/java/cn/sliew/carp/module/workflow/api/service/convert/WorkflowTaskDefinitionConvert.java @@ -0,0 +1,54 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package cn.sliew.carp.module.workflow.api.service.convert; + +import cn.sliew.carp.framework.common.convert.BaseConvert; +import cn.sliew.carp.framework.dag.service.dto.DagConfigStepDTO; +import cn.sliew.carp.module.workflow.api.graph.WorkflowTaskDefinition; +import cn.sliew.carp.module.workflow.api.graph.WorkflowTaskDefinitionAttrs; +import cn.sliew.carp.module.workflow.api.graph.WorkflowTaskDefinitionMeta; +import cn.sliew.milky.common.util.JacksonUtil; +import org.mapstruct.Mapper; +import org.mapstruct.ReportingPolicy; +import org.mapstruct.factory.Mappers; +import org.springframework.beans.BeanUtils; + +@Mapper(unmappedTargetPolicy = ReportingPolicy.IGNORE) +public interface WorkflowTaskDefinitionConvert extends BaseConvert { + WorkflowTaskDefinitionConvert INSTANCE = Mappers.getMapper(WorkflowTaskDefinitionConvert.class); + + @Override + default DagConfigStepDTO toDo(WorkflowTaskDefinition dto) { + throw new UnsupportedOperationException(); + } + + @Override + default WorkflowTaskDefinition toDto(DagConfigStepDTO entity) { + WorkflowTaskDefinition dto = new WorkflowTaskDefinition(); + BeanUtils.copyProperties(entity, dto); + dto.setName(entity.getStepName()); + if (entity.getStepMeta() != null) { + dto.setMeta(JacksonUtil.toObject(entity.getStepMeta(), WorkflowTaskDefinitionMeta.class)); + } + if (entity.getStepAttrs() != null) { + dto.setAttrs(JacksonUtil.toObject(entity.getStepAttrs(), WorkflowTaskDefinitionAttrs.class)); + } + return dto; + } +} diff --git a/carp-modules/carp-module-workflow/carp-module-workflow-api/src/main/java/cn/sliew/carp/module/workflow/api/service/convert/WorkflowTaskInstanceConvert.java b/carp-modules/carp-module-workflow/carp-module-workflow-api/src/main/java/cn/sliew/carp/module/workflow/api/service/convert/WorkflowTaskInstanceConvert.java new file mode 100644 index 00000000..cddd3255 --- /dev/null +++ b/carp-modules/carp-module-workflow/carp-module-workflow-api/src/main/java/cn/sliew/carp/module/workflow/api/service/convert/WorkflowTaskInstanceConvert.java @@ -0,0 +1,45 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package cn.sliew.carp.module.workflow.api.service.convert; + +import cn.sliew.carp.framework.common.convert.BaseConvert; +import cn.sliew.carp.framework.dag.service.dto.DagStepDTO; +import cn.sliew.carp.module.workflow.api.graph.WorkflowTaskInstance; +import org.mapstruct.Mapper; +import org.mapstruct.ReportingPolicy; +import org.mapstruct.factory.Mappers; +import org.springframework.beans.BeanUtils; + +@Mapper(unmappedTargetPolicy = ReportingPolicy.IGNORE) +public interface WorkflowTaskInstanceConvert extends BaseConvert { + WorkflowTaskInstanceConvert INSTANCE = Mappers.getMapper(WorkflowTaskInstanceConvert.class); + + @Override + default DagStepDTO toDo(WorkflowTaskInstance dto) { + throw new UnsupportedOperationException(); + } + + @Override + default WorkflowTaskInstance toDto(DagStepDTO entity) { + WorkflowTaskInstance dto = new WorkflowTaskInstance(); + BeanUtils.copyProperties(entity, dto); + dto.setDefinition(WorkflowTaskDefinitionConvert.INSTANCE.toDto(entity.getDagConfigStep())); + return dto; + } +} diff --git a/carp-modules/carp-module-workflow/carp-module-workflow-api/src/main/java/cn/sliew/carp/module/workflow/api/service/impl/WorkflowDagServiceImpl.java b/carp-modules/carp-module-workflow/carp-module-workflow-api/src/main/java/cn/sliew/carp/module/workflow/api/service/impl/WorkflowDagServiceImpl.java index 035a1a8b..db8bc0fd 100644 --- a/carp-modules/carp-module-workflow/carp-module-workflow-api/src/main/java/cn/sliew/carp/module/workflow/api/service/impl/WorkflowDagServiceImpl.java +++ b/carp-modules/carp-module-workflow/carp-module-workflow-api/src/main/java/cn/sliew/carp/module/workflow/api/service/impl/WorkflowDagServiceImpl.java @@ -20,8 +20,10 @@ import cn.sliew.carp.framework.dag.service.DagConfigComplexService; import cn.sliew.carp.framework.dag.service.DagConfigStepService; +import cn.sliew.carp.framework.dag.service.DagInstanceComplexService; import cn.sliew.carp.framework.dag.service.dto.DagConfigComplexDTO; import cn.sliew.carp.framework.dag.service.dto.DagConfigStepDTO; +import cn.sliew.carp.framework.dag.service.dto.DagInstanceComplexDTO; import cn.sliew.carp.framework.dag.service.param.DagConfigSimpleAddParam; import cn.sliew.carp.framework.dag.x6.graph.DagGraphVO; import cn.sliew.carp.module.workflow.api.service.WorkflowDagService; @@ -35,6 +37,8 @@ public class WorkflowDagServiceImpl implements WorkflowDagService { private DagConfigComplexService dagConfigComplexService; @Autowired private DagConfigStepService dagConfigStepService; + @Autowired + private DagInstanceComplexService dagInstanceComplexService; @Override public Long initialize(String name, String remark) { @@ -64,4 +68,9 @@ public DagConfigStepDTO getStep(Long stepId) { public void update(Long dagId, DagGraphVO graph) { dagConfigComplexService.replace(dagId, graph); } + + @Override + public DagInstanceComplexDTO getDagInstance(Long dagInstanceId) { + return dagInstanceComplexService.selectOne(dagInstanceId); + } } diff --git a/carp-modules/carp-module-workflow/carp-module-workflow-api/src/main/java/cn/sliew/carp/module/workflow/api/service/impl/WorkflowDefinitionServiceImpl.java b/carp-modules/carp-module-workflow/carp-module-workflow-api/src/main/java/cn/sliew/carp/module/workflow/api/service/impl/WorkflowDefinitionServiceImpl.java new file mode 100644 index 00000000..a5c0a315 --- /dev/null +++ b/carp-modules/carp-module-workflow/carp-module-workflow-api/src/main/java/cn/sliew/carp/module/workflow/api/service/impl/WorkflowDefinitionServiceImpl.java @@ -0,0 +1,80 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package cn.sliew.carp.module.workflow.api.service.impl; + +import cn.sliew.carp.framework.dag.algorithm.DAG; +import cn.sliew.carp.framework.dag.algorithm.DefaultDagEdge; +import cn.sliew.carp.framework.dag.service.dto.*; +import cn.sliew.carp.module.workflow.api.graph.WorkflowDefinitionGraph; +import cn.sliew.carp.module.workflow.api.graph.WorkflowExecutionGraph; +import cn.sliew.carp.module.workflow.api.graph.WorkflowTaskDefinition; +import cn.sliew.carp.module.workflow.api.graph.WorkflowTaskInstance; +import cn.sliew.carp.module.workflow.api.service.WorkflowDagService; +import cn.sliew.carp.module.workflow.api.service.WorkflowDefinitionService; +import cn.sliew.carp.module.workflow.api.service.convert.WorkflowTaskDefinitionConvert; +import cn.sliew.carp.module.workflow.api.service.convert.WorkflowTaskInstanceConvert; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.HashMap; +import java.util.Map; + +@Service +public class WorkflowDefinitionServiceImpl implements WorkflowDefinitionService { + + @Autowired + private WorkflowDagService workflowDagService; + + @Override + public WorkflowDefinitionGraph get(Long dagId) { + DagConfigComplexDTO complexDTO = workflowDagService.getDag(dagId); + WorkflowDefinitionGraph graph = new WorkflowDefinitionGraph(); + DAG dag = new DAG<>(); + Map taskDefinitionMap = new HashMap<>(); + for (DagConfigStepDTO stepDTO : complexDTO.getSteps()) { + WorkflowTaskDefinition taskDefinition = WorkflowTaskDefinitionConvert.INSTANCE.toDto(stepDTO); + taskDefinitionMap.put(taskDefinition.getStepId(), taskDefinition); + dag.addNode(taskDefinition); + } + for (DagConfigLinkDTO linkDTO : complexDTO.getLinks()) { + dag.addEdge(taskDefinitionMap.get(linkDTO.getFromStepId()), taskDefinitionMap.get(linkDTO.getToStepId())); + } + graph.setDag(dag); + return graph; + } + + @Override + public WorkflowExecutionGraph getExecutionGraph(Long dagInstanceId) { + DagInstanceComplexDTO complexDTO = workflowDagService.getDagInstance(dagInstanceId); + WorkflowDefinitionGraph definitionGraph = get(complexDTO.getDagConfig().getId()); + WorkflowExecutionGraph executionGraph = new WorkflowExecutionGraph(); + DAG dag = new DAG<>(); + Map taskInstanceMap = new HashMap<>(); + for (DagStepDTO stepDTO : complexDTO.getSteps()) { + WorkflowTaskInstance taskInstance = WorkflowTaskInstanceConvert.INSTANCE.toDto(stepDTO); + taskInstanceMap.put(taskInstance.getDefinition().getStepId(), taskInstance); + dag.addNode(taskInstance); + } + for (DefaultDagEdge edge : definitionGraph.getDag().edges()) { + dag.addEdge(taskInstanceMap.get(edge.getSource().getStepId()), taskInstanceMap.get(edge.getTarget().getStepId())); + } + executionGraph.setDag(dag); + return executionGraph; + } +}