diff --git a/auto-solution-admin/src/main/java/com/solution/web/controller/behaviour/BehaviortreeController.java b/auto-solution-admin/src/main/java/com/solution/web/controller/behaviour/BehaviortreeController.java index cd6d4f1..9f3f2a8 100644 --- a/auto-solution-admin/src/main/java/com/solution/web/controller/behaviour/BehaviortreeController.java +++ b/auto-solution-admin/src/main/java/com/solution/web/controller/behaviour/BehaviortreeController.java @@ -3,6 +3,7 @@ package com.solution.web.controller.behaviour; import java.util.List; import javax.servlet.http.HttpServletResponse; +import com.solution.web.core.BehaviortreeProcessor; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.security.access.prepost.PreAuthorize; @@ -38,6 +39,9 @@ public class BehaviortreeController extends BaseController @Autowired private IBehaviortreeService behaviortreeService; + @Autowired + private BehaviortreeProcessor behaviortreeProcessor; + /** * 查询行为树主列表 */ @@ -84,7 +88,7 @@ public class BehaviortreeController extends BaseController @PostMapping public AjaxResult add(@RequestBody Behaviortree behaviortree) { - return toAjax(behaviortreeService.insertBehaviortree(behaviortree)); + return toAjax(behaviortreeProcessor.create(behaviortree)); } /** @@ -96,7 +100,7 @@ public class BehaviortreeController extends BaseController @PutMapping public AjaxResult edit(@RequestBody Behaviortree behaviortree) { - return toAjax(behaviortreeService.updateBehaviortree(behaviortree)); + return toAjax(behaviortreeProcessor.update(behaviortree)); } /** diff --git a/auto-solution-admin/src/main/java/com/solution/web/controller/behaviour/NodetemplateController.java b/auto-solution-admin/src/main/java/com/solution/web/controller/behaviour/NodetemplateController.java index 698a361..ce993d8 100644 --- a/auto-solution-admin/src/main/java/com/solution/web/controller/behaviour/NodetemplateController.java +++ b/auto-solution-admin/src/main/java/com/solution/web/controller/behaviour/NodetemplateController.java @@ -103,15 +103,15 @@ public class NodetemplateController extends BaseController { dto.setDescription(template.getDescription()); dto.setEnglishName(template.getEnglishName()); dto.setLogicHandler(template.getLogicHandler()); - dto.setTempleteType(template.getTempleteType()); + dto.setTemplateType(template.getTemplateType()); return dto; }) - .collect(Collectors.groupingBy(NodetemplateDTO::getTempleteType)); + .collect(Collectors.groupingBy(NodetemplateDTO::getTemplateType)); List vos = new ArrayList<>(); groupedByTemplateType.forEach((key, value) -> { // 处理逻辑 NodetemplateVO vo = new NodetemplateVO(); - vo.setTempleteType(key); + vo.setTemplateType(key); vo.setDtoList(value); vos.add(vo); }); diff --git a/auto-solution-admin/src/main/java/com/solution/web/controller/behaviour/vo/NodetemplateDTO.java b/auto-solution-admin/src/main/java/com/solution/web/controller/behaviour/vo/NodetemplateDTO.java index 2954b8f..d240ef7 100644 --- a/auto-solution-admin/src/main/java/com/solution/web/controller/behaviour/vo/NodetemplateDTO.java +++ b/auto-solution-admin/src/main/java/com/solution/web/controller/behaviour/vo/NodetemplateDTO.java @@ -23,7 +23,7 @@ public class NodetemplateDTO { @ApiModelProperty("afsim 中转换的节点名") private String englishName; - private String templeteType; + private String templateType; public Long getId() { return id; @@ -65,11 +65,11 @@ public class NodetemplateDTO { this.englishName = englishName; } - public String getTempleteType() { - return templeteType; + public String getTemplateType() { + return templateType; } - public void setTempleteType(String templeteType) { - this.templeteType = templeteType; + public void setTemplateType(String templateType) { + this.templateType = templateType; } } diff --git a/auto-solution-admin/src/main/java/com/solution/web/controller/behaviour/vo/NodetemplateVO.java b/auto-solution-admin/src/main/java/com/solution/web/controller/behaviour/vo/NodetemplateVO.java index 9a20c04..3f0e8ed 100644 --- a/auto-solution-admin/src/main/java/com/solution/web/controller/behaviour/vo/NodetemplateVO.java +++ b/auto-solution-admin/src/main/java/com/solution/web/controller/behaviour/vo/NodetemplateVO.java @@ -9,17 +9,17 @@ import java.util.List; public class NodetemplateVO { /** 模版类型,节点模版或者条件判断,例如“node”,precondition“ */ @ApiModelProperty("模版类型,节点模版或者条件判断,例如“node”,precondition“") - private String templeteType; + private String templateType; @ApiModelProperty("节点模板数据") private List dtoList; - public String getTempleteType() { - return templeteType; + public String getTemplateType() { + return templateType; } - public void setTempleteType(String templeteType) { - this.templeteType = templeteType; + public void setTemplateType(String templateType) { + this.templateType = templateType; } public List getDtoList() { diff --git a/auto-solution-admin/src/main/java/com/solution/web/core/BehaviortreeProcessor.java b/auto-solution-admin/src/main/java/com/solution/web/core/BehaviortreeProcessor.java new file mode 100644 index 0000000..b857905 --- /dev/null +++ b/auto-solution-admin/src/main/java/com/solution/web/core/BehaviortreeProcessor.java @@ -0,0 +1,160 @@ +package com.solution.web.core; +/* + * This file is part of the kernelstudio package. + * + * (c) 2014-2026 zlin + * + * For the full copyright and license information, please view the LICENSE file + * that was distributed with this source code. + */ + +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.solution.system.domain.*; +import com.solution.system.service.IBehaviortreeService; +import com.solution.system.service.INodeconnectionService; +import com.solution.system.service.INodeparameterService; +import com.solution.system.service.ITreenodeinstanceService; +import com.solution.web.core.graph.Graph; +import com.solution.web.core.graph.GraphEdge; +import com.solution.web.core.graph.GraphNode; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.util.HashMap; +import java.util.Map; + +@Component +public class BehaviortreeProcessor { + + @Autowired + private IBehaviortreeService behaviortreeService; + + @Autowired + private ITreenodeinstanceService treenodeinstanceService; + + @Autowired + private INodeparameterService nodeparameterService; + + @Autowired + private INodeconnectionService nodeconnectionService; + + private ObjectMapper objectMapper = createObjectMapper(); + + public static ObjectMapper createObjectMapper() { + ObjectMapper objectMapper = new ObjectMapper(); + objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + return objectMapper; + } + + public int create(Behaviortree behaviortree) { + int result = behaviortreeService.insertBehaviortree(behaviortree); + processGraph(behaviortree); + return result; + } + + public int update(Behaviortree behaviortree) { + int result = behaviortreeService.updateBehaviortree(behaviortree); + + // 删除节点实例 + treenodeinstanceService.deleteByTreeId(behaviortree.getId()); + + // 删除参数 + nodeparameterService.deleteByTreeId(behaviortree.getId()); + + // 删除连线 + nodeconnectionService.deleteByTreeId(behaviortree.getId()); + + processGraph(behaviortree); + + return result; + } + + private void processGraph(Behaviortree behaviortree) { + + Graph graph = null; + try { + graph = objectMapper.readValue(behaviortree.getXmlContent(), Graph.class); + } catch (Exception e) { + // skip + e.printStackTrace(); + } + + if (null == graph) { + return; + } + + // 插入节点 treenodeinstance + Map instanceKeyMap = new HashMap<>(); + Map nodeKeyIndexMap = new HashMap<>(); + if (graph.hasNodes()) { + Long index = 0L; + for (GraphNode node : graph.getNodes()) { + Treenodeinstance instance = createNodeInstance(behaviortree, node); + treenodeinstanceService.insertTreenodeinstance(instance); + instanceKeyMap.put(node.getKey(), instance); + + if (node.hasParameters()) { + // 插入parameter nodeparameter + for (Templateparameterdef parameter : node.getParameters()) { + Nodeparameter nodeparameter = createNodeParameter(behaviortree, parameter, instance); + nodeparameterService.insertNodeparameter(nodeparameter); + } + } + nodeKeyIndexMap.put(node.getKey(), index); + + index++; + } + } + + // 插入连线 nodeconnection + if (graph.hasEdges()) { + for (GraphEdge edge : graph.getEdges()) { + Nodeconnection connection = createConnection(behaviortree, edge, instanceKeyMap, nodeKeyIndexMap); + nodeconnectionService.insertNodeconnection(connection); + } + } + } + + private Nodeconnection createConnection(Behaviortree behaviortree, GraphEdge edge, + Map instanceKeyMap, + Map nodeKeyIndexMap) { + Nodeconnection connection = new Nodeconnection(); + connection.setTreeId(behaviortree.getId()); + if (null != instanceKeyMap.get(edge.getSource().getCell())) { + Treenodeinstance parent = instanceKeyMap.get(edge.getSource().getCell()); + connection.setParentNodeId(parent.getId()); + } + if (null != instanceKeyMap.get(edge.getTarget().getCell())) { + Treenodeinstance children = instanceKeyMap.get(edge.getTarget().getCell()); + connection.setChildNodeId(children.getId()); + } + if (null != nodeKeyIndexMap.get(edge.getSource().getCell())) { + connection.setOrderIndex(nodeKeyIndexMap.get(edge.getSource().getCell())); + } + return connection; + } + + private Nodeparameter createNodeParameter(Behaviortree behaviortree, Templateparameterdef parameter, + Treenodeinstance instance) { + Nodeparameter nodeparameter = new Nodeparameter(); + nodeparameter.setTreeId(behaviortree.getId()); + nodeparameter.setNodeInstanceId(instance.getId()); + nodeparameter.setParamDefId(parameter.getId()); + nodeparameter.setValue(parameter.getDefaultValue()); + return nodeparameter; + } + + private Treenodeinstance createNodeInstance(Behaviortree behaviortree, GraphNode node) { + Treenodeinstance instance = new Treenodeinstance(); + instance.setTreeId(behaviortree.getId()); + instance.setTemplateId((long) node.getTemplate()); + instance.setInstanceName(node.getName()); + instance.setIsRoot((long) (node.isRoot() ? 1 : 0)); + if ("precondition".equalsIgnoreCase(node.getTemplateType())) { + instance.setPreconditionTempleteId((long) node.getTemplate()); + } + return instance; + } + +} diff --git a/auto-solution-admin/src/main/java/com/solution/web/core/graph/Graph.java b/auto-solution-admin/src/main/java/com/solution/web/core/graph/Graph.java new file mode 100644 index 0000000..6d0f617 --- /dev/null +++ b/auto-solution-admin/src/main/java/com/solution/web/core/graph/Graph.java @@ -0,0 +1,52 @@ +package com.solution.web.core.graph; +/* + * This file is part of the kernelstudio package. + * + * (c) 2014-2026 zlin + * + * For the full copyright and license information, please view the LICENSE file + * that was distributed with this source code. + */ + +import java.io.Serializable; +import java.util.List; + +public class Graph implements Serializable { + + private List nodes; + + private List edges; + + public boolean hasNodes() { + return nodes != null && !nodes.isEmpty(); + } + + public boolean hasEdges() { + return edges != null && !edges.isEmpty(); + } + + @Override + public String toString() { + return "Graph{" + + "nodes=" + nodes + + ", edges=" + edges + + '}'; + } + + public List getNodes() { + return nodes; + } + + public void setNodes(List nodes) { + this.nodes = nodes; + } + + public List getEdges() { + return edges; + } + + public void setEdges(List edges) { + this.edges = edges; + } + +} diff --git a/auto-solution-admin/src/main/java/com/solution/web/core/graph/GraphEdge.java b/auto-solution-admin/src/main/java/com/solution/web/core/graph/GraphEdge.java new file mode 100644 index 0000000..7f7ad8c --- /dev/null +++ b/auto-solution-admin/src/main/java/com/solution/web/core/graph/GraphEdge.java @@ -0,0 +1,85 @@ +package com.solution.web.core.graph; +/* + * This file is part of the kernelstudio package. + * + * (c) 2014-2026 zlin + * + * For the full copyright and license information, please view the LICENSE file + * that was distributed with this source code. + */ + +import org.springframework.util.StringUtils; + +import java.io.Serializable; + +public class GraphEdge implements Serializable { + + private String key; + + private Line source; + + private Line target; + + public boolean hasSource() { + return source != null && StringUtils.hasText(source.cell); + } + + public boolean hasTarget() { + return target != null && StringUtils.hasText(target.cell); + } + + public static class Line implements Serializable { + + private String cell; + + @Override + public String toString() { + return "Line{" + + "cell='" + cell + '\'' + + '}'; + } + + public String getCell() { + return cell; + } + + public void setCell(String cell) { + this.cell = cell; + } + + } + + @Override + public String toString() { + return "GraphEdge{" + + "key='" + key + '\'' + + ", source='" + source + '\'' + + ", target='" + target + '\'' + + '}'; + } + + public String getKey() { + return key; + } + + public void setKey(String key) { + this.key = key; + } + + public Line getSource() { + return source; + } + + public void setSource(Line source) { + this.source = source; + } + + public Line getTarget() { + return target; + } + + public void setTarget(Line target) { + this.target = target; + } + +} diff --git a/auto-solution-admin/src/main/java/com/solution/web/core/graph/GraphNode.java b/auto-solution-admin/src/main/java/com/solution/web/core/graph/GraphNode.java new file mode 100644 index 0000000..e94aab3 --- /dev/null +++ b/auto-solution-admin/src/main/java/com/solution/web/core/graph/GraphNode.java @@ -0,0 +1,142 @@ +package com.solution.web.core.graph; +/* + * This file is part of the kernelstudio package. + * + * (c) 2014-2026 zlin + * + * For the full copyright and license information, please view the LICENSE file + * that was distributed with this source code. + */ + +import com.solution.system.domain.Templateparameterdef; + +import java.io.Serializable; +import java.util.List; + +public class GraphNode implements Serializable { + + private int id; + + private int template; + + private String templateType; + + private String type; + + private String key; + + private String name; + + private String description; + + private String category; + + private List parameters; + + private List variables; + + public boolean hasParameters() { + return parameters != null && !parameters.isEmpty(); + } + + public boolean hasVariables() { + return variables != null && !variables.isEmpty(); + } + + public boolean isRoot() { + return "root".equalsIgnoreCase(this.category); + } + + @Override + public String toString() { + return "GraphNode{" + + "id=" + id + + ", key='" + key + '\'' + + ", name='" + name + '\'' + + ", description='" + description + '\'' + + ", parameters=" + parameters + + ", variables=" + variables + + '}'; + } + + public String getCategory() { + return category; + } + + public void setCategory(String category) { + this.category = category; + } + + public String getTemplateType() { + return templateType; + } + + public void setTemplateType(String templateType) { + this.templateType = templateType; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public int getTemplate() { + return template; + } + + public void setTemplate(int template) { + this.template = template; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + public String getKey() { + return key; + } + + public void setKey(String key) { + this.key = key; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public List getParameters() { + return parameters; + } + + public void setParameters(List parameters) { + this.parameters = parameters; + } + + public List getVariables() { + return variables; + } + + public void setVariables(List variables) { + this.variables = variables; + } + +} diff --git a/auto-solution-admin/src/main/java/com/solution/web/core/graph/GraphVariable.java b/auto-solution-admin/src/main/java/com/solution/web/core/graph/GraphVariable.java new file mode 100644 index 0000000..00eb15b --- /dev/null +++ b/auto-solution-admin/src/main/java/com/solution/web/core/graph/GraphVariable.java @@ -0,0 +1,65 @@ +package com.solution.web.core.graph; +/* + * This file is part of the kernelstudio package. + * + * (c) 2014-2026 zlin + * + * For the full copyright and license information, please view the LICENSE file + * that was distributed with this source code. + */ + +import java.io.Serializable; + +public class GraphVariable implements Serializable { + + private String key; + + private String value; + + private String defaults; + + private String unit; + + @Override + public String toString() { + return "GraphVariable{" + + "key='" + key + '\'' + + ", value='" + value + '\'' + + ", defaults='" + defaults + '\'' + + ", unit='" + unit + '\'' + + '}'; + } + + public String getKey() { + return key; + } + + public void setKey(String key) { + this.key = key; + } + + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } + + public String getDefaults() { + return defaults; + } + + public void setDefaults(String defaults) { + this.defaults = defaults; + } + + public String getUnit() { + return unit; + } + + public void setUnit(String unit) { + this.unit = unit; + } + +}