UPDATE: fk

This commit is contained in:
libertyspy
2026-02-09 14:52:46 +08:00
parent a78781782e
commit 46bead08a0
9 changed files with 523 additions and 15 deletions

View File

@@ -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));
}
/**

View File

@@ -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<NodetemplateVO> vos = new ArrayList<>();
groupedByTemplateType.forEach((key, value) -> {
// 处理逻辑
NodetemplateVO vo = new NodetemplateVO();
vo.setTempleteType(key);
vo.setTemplateType(key);
vo.setDtoList(value);
vos.add(vo);
});

View File

@@ -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;
}
}

View File

@@ -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<NodetemplateDTO> 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<NodetemplateDTO> getDtoList() {

View File

@@ -0,0 +1,160 @@
package com.solution.web.core;
/*
* This file is part of the kernelstudio package.
*
* (c) 2014-2026 zlin <admin@kernelstudio.com>
*
* 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<String, Treenodeinstance> instanceKeyMap = new HashMap<>();
Map<String, Long> 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<String, Treenodeinstance> instanceKeyMap,
Map<String, Long> 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;
}
}

View File

@@ -0,0 +1,52 @@
package com.solution.web.core.graph;
/*
* This file is part of the kernelstudio package.
*
* (c) 2014-2026 zlin <admin@kernelstudio.com>
*
* 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<GraphNode> nodes;
private List<GraphEdge> 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<GraphNode> getNodes() {
return nodes;
}
public void setNodes(List<GraphNode> nodes) {
this.nodes = nodes;
}
public List<GraphEdge> getEdges() {
return edges;
}
public void setEdges(List<GraphEdge> edges) {
this.edges = edges;
}
}

View File

@@ -0,0 +1,85 @@
package com.solution.web.core.graph;
/*
* This file is part of the kernelstudio package.
*
* (c) 2014-2026 zlin <admin@kernelstudio.com>
*
* 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;
}
}

View File

@@ -0,0 +1,142 @@
package com.solution.web.core.graph;
/*
* This file is part of the kernelstudio package.
*
* (c) 2014-2026 zlin <admin@kernelstudio.com>
*
* 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<Templateparameterdef> parameters;
private List<GraphVariable> 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<Templateparameterdef> getParameters() {
return parameters;
}
public void setParameters(List<Templateparameterdef> parameters) {
this.parameters = parameters;
}
public List<GraphVariable> getVariables() {
return variables;
}
public void setVariables(List<GraphVariable> variables) {
this.variables = variables;
}
}

View File

@@ -0,0 +1,65 @@
package com.solution.web.core.graph;
/*
* This file is part of the kernelstudio package.
*
* (c) 2014-2026 zlin <admin@kernelstudio.com>
*
* 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;
}
}