复制行为树

This commit is contained in:
MHW
2026-04-13 14:19:18 +08:00
parent dafd7115a8
commit 5276a6b16f
4 changed files with 75 additions and 28 deletions

View File

@@ -106,7 +106,8 @@ public class BehaviortreeController extends BaseController
@PostMapping("/copy")
public AjaxResult copy(@RequestBody Behaviortree behaviortree)
{
return toAjax(behaviortreeService.copy(behaviortree));
//return toAjax(behaviortreeService.copy(behaviortree));
return toAjax(behaviortreeProcessor.copy(behaviortree));
}
/**

View File

@@ -8,8 +8,10 @@ package com.solution.web.core;
* that was distributed with this source code.
*/
import cn.hutool.core.util.ObjectUtil;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.solution.common.constant.ExceptionConstants;
import com.solution.system.domain.*;
import com.solution.system.service.IBehaviortreeService;
import com.solution.system.service.INodeconnectionService;
@@ -76,7 +78,59 @@ public class BehaviortreeProcessor {
}
private void processGraph(Behaviortree behaviortree) {
//代码丢失 原:libertyspy 改:MHW
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<>();
Map<String,GraphNode> nodesMap = new HashMap<>();
if (graph.hasNodes()) {
Long index = 0L;
for (GraphNode node : graph.getNodes()) {
nodesMap.put(node.getKey(), node);
Treenodeinstance instance = createNodeInstance(behaviortree, node);
treenodeinstanceService.insertTreenodeinstance(instance);
instanceKeyMap.put(node.getKey(), instance);
if (node.hasParameters()) {
if (node.isMultiable()) {
List<Nodeparameter> nodeparameters = createMultiableNodeparameter(behaviortree, node, instance);
for (Nodeparameter nodeparameter : nodeparameters) {
nodeparameterService.insertNodeparameter(nodeparameter);
}
} else {
// 插入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, nodesMap, instanceKeyMap, nodeKeyIndexMap);
nodeconnectionService.insertNodeconnection(connection);
}
}
}
private Nodeconnection createConnection(Behaviortree behaviortree, GraphEdge edge,
@@ -173,4 +227,23 @@ public class BehaviortreeProcessor {
}
/**
* 复制行为树
* @param behaviortree
* @return
*/
public int copy(Behaviortree behaviortree) {
if(ObjectUtil.isEmpty(behaviortree)){
throw new RuntimeException(ExceptionConstants.PARAMETER_EXCEPTION);
}
String name = behaviortree.getName();
String newName = name + "_" + behaviortree.getId();
String englishName = behaviortree.getEnglishName();
String newEnglishName = englishName + "_" + behaviortree.getId();
behaviortree.setEnglishName(newEnglishName);
behaviortree.setName(newName);
return this.create(behaviortree);
}
}

View File

@@ -62,11 +62,4 @@ public interface IBehaviortreeService
* @return 结果
*/
public int deleteBehaviortreeById(Long id);
/**
* 复制行为树
* @param behaviortree
* @return
*/
int copy(Behaviortree behaviortree);
}

View File

@@ -99,24 +99,4 @@ public class BehaviortreeServiceImpl implements IBehaviortreeService
{
return behaviortreeMapper.deleteBehaviortreeById(id);
}
/**
* 复制行为树
* @param behaviortree
* @return
*/
@Override
public int copy(Behaviortree behaviortree) {
if(ObjectUtil.isEmpty(behaviortree)){
throw new RuntimeException(ExceptionConstants.PARAMETER_EXCEPTION);
}
String name = behaviortree.getName();
String newName = name + "_" + behaviortree.getId();
String englishName = behaviortree.getEnglishName();
String newEnglishName = englishName + "_" + behaviortree.getId();
behaviortree.setEnglishName(newEnglishName);
behaviortree.setName(newName);
return behaviortreeMapper.insertBehaviortree(behaviortree);
}
}