【修改NCC更新逻辑】

This commit is contained in:
weizhihong 2023-03-02 17:55:51 +08:00
parent b8cae4edfa
commit 0396b88c2b
6 changed files with 103 additions and 100 deletions

@ -1 +1 @@
Subproject commit 3ff3c0da726665b5bd057c75bd86cd0072c3da31 Subproject commit 12bfb1243559f366ddaa1f36b0a8cbaed3a3c694

View File

@ -5,10 +5,10 @@ import club.joylink.ecs.WorldManage;
import club.joylink.rtss.simulation.ncc.dto.NccMapDataDto; import club.joylink.rtss.simulation.ncc.dto.NccMapDataDto;
import club.joylink.rtss.simulation.ncc.exception.NccExceptionAssertEnum; import club.joylink.rtss.simulation.ncc.exception.NccExceptionAssertEnum;
import club.joylink.rtss.simulation.ncc.module.proto.StorageProto; import club.joylink.rtss.simulation.ncc.module.proto.StorageProto;
import club.joylink.rtss.simulation.ncc.module.proto.TrainStateProto;
import club.joylink.rtss.simulation.ncc.repository.IRtsNccMapDataRepository; import club.joylink.rtss.simulation.ncc.repository.IRtsNccMapDataRepository;
import club.joylink.rtss.simulation.ncc.simulation.event.NCCCreateEvent; import club.joylink.rtss.simulation.ncc.simulation.event.NCCCreateEvent;
import club.joylink.rtss.simulation.ncc.simulation.event.NCCDestroyEvent; import club.joylink.rtss.simulation.ncc.simulation.event.NCCDestroyEvent;
import club.joylink.rtss.simulation.ncc.simulation.event.NCCResetEvent;
import club.joylink.rtss.simulation.ncc.simulation.event.NCCUpdateEvent; import club.joylink.rtss.simulation.ncc.simulation.event.NCCUpdateEvent;
import club.joylink.rtss.simulation.ncc.simulation.system.TrainStateSystem; import club.joylink.rtss.simulation.ncc.simulation.system.TrainStateSystem;
import com.google.protobuf.InvalidProtocolBufferException; import com.google.protobuf.InvalidProtocolBufferException;
@ -40,6 +40,11 @@ public class NCCManagerService {
World world = WorldManage.create(generateWorldId(event.getSimulationId())); World world = WorldManage.create(generateWorldId(event.getSimulationId()));
// 将构建信息放入world // 将构建信息放入world
world.getStorage().insertSingleton(storage); world.getStorage().insertSingleton(storage);
// 构建world实体
storage.getLinesList().forEach(line -> world.getStorage().createEntity(line.getCode(), line));
storage.getStationsList().forEach(station -> world.getStorage().createEntity(station.getCode(), station));
storage.getRoadSectionsList().forEach(roadSection -> world.getStorage().createEntity(roadSection.getCode(), roadSection));
storage.getTrainList().forEach(train -> world.getStorage().createEntity(train.getCode(), train, TrainStateProto.TrainState.newBuilder()));
// 添加系统 // 添加系统
world.getTaskControl().addSystem(new TrainStateSystem()); world.getTaskControl().addSystem(new TrainStateSystem());
world.start(); world.start();
@ -62,20 +67,12 @@ public class NCCManagerService {
} }
} }
/**
* 重置world数据
*/
@EventListener
public void resetWorld(NCCResetEvent event) {
}
/** /**
* 销毁world * 销毁world
*/ */
@EventListener @EventListener
public void destroyWorld(NCCDestroyEvent event) { public void destroyWorld(NCCDestroyEvent event) {
WorldManage.destroy(generateWorldId(event.getSimulationId()));
} }
/** /**

View File

@ -3,11 +3,8 @@ package club.joylink.rtss.simulation.ncc.simulation;
import club.joylink.ecs.World; import club.joylink.ecs.World;
import club.joylink.ecs.api.Entity; import club.joylink.ecs.api.Entity;
import club.joylink.rtss.simulation.ncc.exception.NccExceptionAssertEnum; import club.joylink.rtss.simulation.ncc.exception.NccExceptionAssertEnum;
import club.joylink.rtss.simulation.ncc.module.proto.CommandStatusProto;
import club.joylink.rtss.simulation.ncc.module.proto.RunStatusProto;
import club.joylink.rtss.simulation.ncc.module.proto.TrainStateCommandProto; import club.joylink.rtss.simulation.ncc.module.proto.TrainStateCommandProto;
import club.joylink.rtss.simulation.ncc.module.proto.TrainStateProto; import club.joylink.rtss.simulation.ncc.module.proto.TrainStateProto;
import club.joylink.rtss.simulation.ncc.module.proto.dto.TrainStateDtoProto;
import com.google.protobuf.InvalidProtocolBufferException; import com.google.protobuf.InvalidProtocolBufferException;
import java.util.Optional; import java.util.Optional;
@ -46,7 +43,4 @@ public enum NccStateType {
} }
public abstract void handle(World world, byte[] data); public abstract void handle(World world, byte[] data);
} }

View File

@ -1,14 +0,0 @@
package club.joylink.rtss.simulation.ncc.simulation.event;
import lombok.Getter;
/**
* NCC重置事件
*/
@Getter
public class NCCResetEvent extends NCCCommEvent {
public NCCResetEvent(Object source, String simulationId) {
super(source, simulationId);
}
}

View File

@ -19,80 +19,88 @@ public class TrainStateSystem implements WorldSystem<Tuple1<TrainStateProto.Trai
@Override @Override
public void update(WorldQuery<Tuple1<TrainStateProto.TrainState.Builder>> query, Tuple1<StorageProto.Storage> storageTuple1) { public void update(WorldQuery<Tuple1<TrainStateProto.TrainState.Builder>> query, Tuple1<StorageProto.Storage> storageTuple1) {
// 所有资源
StorageProto.Storage storage = storageTuple1.val1().get();
// 查找需要更新的车状态信息 // 查找需要更新的车状态信息
query.forEach(tuple -> { query.forEach(tuple -> {
TrainStateProto.TrainState.Builder trainState = tuple.val1().get(); TrainStateProto.TrainState.Builder trainState = tuple.val1().get();
TrainStateCommandProto.TrainStateCommand.Builder command = trainState.getCommandBuilder(); TrainStateCommandProto.TrainStateCommand.Builder command = trainState.getCommandBuilder();
if (command != null && Objects.equals(command.getStatus(), CommandStatusProto.CommandStatus.UNDO)) { CommandStatusProto.CommandStatus status = command.getStatus();
if (Objects.equals(status, CommandStatusProto.CommandStatus.UNDO)
|| Objects.equals(status, CommandStatusProto.CommandStatus.RESET)) {
// 标识状态
command.setStatus(CommandStatusProto.CommandStatus.DOING); command.setStatus(CommandStatusProto.CommandStatus.DOING);
// 更新车次信息 if (CommandStatusProto.CommandStatus.RESET.equals(status)) {
if (StringUtils.isNoneBlank(command.getTripNumber())) { trainState.clear();
trainState.setTripNumber(command.getTripNumber()); } else {
} update(trainState, command, storageTuple1.val1().get());
// 更新位置信息
if (StringUtils.isNoneBlank(command.getStationCode())) {
Optional<StationProto.Station> stationOptional = storage.getStationsList().stream()
.filter(station -> Objects.equals(station.getCode(), command.getStationCode()))
.findFirst();
if (stationOptional.isPresent()) {
trainState.setStartStation(stationOptional.get());
log.info("列车进入车站:" + trainState.getStartStation().getName());
} else {
throw NccExceptionAssertEnum.DataNotExist.exception();
}
}
// 更新终点站
if (StringUtils.isNoneBlank(command.getTerminusCode())) {
Optional<StationProto.Station> stationOptional = storage.getStationsList().stream()
.filter(station -> Objects.equals(station.getCode(), command.getTerminusCode()))
.findFirst();
if (stationOptional.isPresent()) {
trainState.setTerminus(stationOptional.get());
} else {
throw NccExceptionAssertEnum.DataNotExist.exception();
}
}
// 行驶距离
if (command.getDistance() >= 0) {
trainState.setDistance(command.getDistance());
}
// 行驶状态
trainState.setRunStatus(command.getRunStatus());
// 行驶方向
if (command.getDirection() != 0) {
trainState.setDirection(command.getDirection());
}
// 行驶线路
if (StringUtils.isNoneBlank(command.getLineCode())) {
Optional<LineProto.Line> lineOptional = storage.getLinesList().stream()
.filter(line -> Objects.equals(line.getCode(), command.getLineCode()) )
.findFirst();
if (lineOptional.isPresent()) {
trainState.setLine(lineOptional.get());
} else {
throw NccExceptionAssertEnum.DataNotExist.exception();
}
}
// 更新位置 方向一致的路段
Optional<RoadSectionProto.RoadSection> roadSectionOptional = storage.getRoadSectionsList().stream()
.filter(rs -> Objects.equals(trainState.getLine().getCode(), rs.getLineId())
&& Objects.equals(trainState.getStartStation().getCode(), rs.getStartStationId()))
.filter(rs -> (rs.getMaxDistance() - rs.getMinDistance()) * trainState.getDirection() > 0)
.findFirst();
if (roadSectionOptional.isPresent()) {
RoadSectionProto.RoadSection roadSection = roadSectionOptional.get();
trainState.setRoadSection(roadSection);
double totalDistance = Math.abs(roadSection.getMaxDistance() - roadSection.getMinDistance());
double distanceRatio = command.getDistance() / totalDistance;
trainState.setDistanceRatio(distanceRatio);
log.info("列车行驶至车站:" + trainState.getStartStation().getName() + "距离 " + trainState.getDistance() + ",行驶比例:" + distanceRatio);
} }
// 完成标识 // 完成标识
command.setStatus(CommandStatusProto.CommandStatus.DONE); command.setStatus(CommandStatusProto.CommandStatus.DONE);
} }
}); });
} }
private void update(TrainStateProto.TrainState.Builder trainState, TrainStateCommandProto.TrainStateCommand.Builder command, StorageProto.Storage storage) {
// 更新车次信息
if (StringUtils.isNoneBlank(command.getTripNumber())) {
trainState.setTripNumber(command.getTripNumber());
}
// 更新位置信息
if (StringUtils.isNoneBlank(command.getStationCode())) {
Optional<StationProto.Station> stationOptional = storage.getStationsList().stream()
.filter(station -> Objects.equals(station.getCode(), command.getStationCode()))
.findFirst();
if (stationOptional.isPresent()) {
trainState.setStartStation(stationOptional.get());
log.info("列车进入车站:" + trainState.getStartStation().getName());
} else {
throw NccExceptionAssertEnum.DataNotExist.exception();
}
}
// 更新终点站
if (StringUtils.isNoneBlank(command.getTerminusCode())) {
Optional<StationProto.Station> stationOptional = storage.getStationsList().stream()
.filter(station -> Objects.equals(station.getCode(), command.getTerminusCode()))
.findFirst();
if (stationOptional.isPresent()) {
trainState.setTerminus(stationOptional.get());
} else {
throw NccExceptionAssertEnum.DataNotExist.exception();
}
}
// 行驶距离
if (command.getDistance() >= 0) {
trainState.setDistance(command.getDistance());
}
// 行驶状态
trainState.setRunStatus(command.getRunStatus());
// 行驶方向
if (command.getDirection() != 0) {
trainState.setDirection(command.getDirection());
}
// 行驶线路
if (StringUtils.isNoneBlank(command.getLineCode())) {
Optional<LineProto.Line> lineOptional = storage.getLinesList().stream()
.filter(line -> Objects.equals(line.getCode(), command.getLineCode()) )
.findFirst();
if (lineOptional.isPresent()) {
trainState.setLine(lineOptional.get());
} else {
throw NccExceptionAssertEnum.DataNotExist.exception();
}
}
// 更新位置 方向一致的路段
Optional<RoadSectionProto.RoadSection> roadSectionOptional = storage.getRoadSectionsList().stream()
.filter(rs -> Objects.equals(trainState.getLine().getCode(), rs.getLineId())
&& Objects.equals(trainState.getStartStation().getCode(), rs.getStartStationId()))
.filter(rs -> (rs.getMaxDistance() - rs.getMinDistance()) * trainState.getDirection() > 0)
.findFirst();
if (roadSectionOptional.isPresent()) {
RoadSectionProto.RoadSection roadSection = roadSectionOptional.get();
trainState.setRoadSection(roadSection);
double totalDistance = Math.abs(roadSection.getMaxDistance() - roadSection.getMinDistance());
double distanceRatio = command.getDistance() / totalDistance;
trainState.setDistanceRatio(distanceRatio);
log.info("列车行驶至车站:" + trainState.getStartStation().getName() + "距离 " + trainState.getDistance() + ",行驶比例:" + distanceRatio);
}
}
} }

View File

@ -43,6 +43,14 @@ public final class CommandStatusProto {
* <code>DONE = 2;</code> * <code>DONE = 2;</code>
*/ */
DONE(2), DONE(2),
/**
* <pre>
* 重置
* </pre>
*
* <code>RESET = 3;</code>
*/
RESET(3),
UNRECOGNIZED(-1), UNRECOGNIZED(-1),
; ;
@ -70,6 +78,14 @@ public final class CommandStatusProto {
* <code>DONE = 2;</code> * <code>DONE = 2;</code>
*/ */
public static final int DONE_VALUE = 2; public static final int DONE_VALUE = 2;
/**
* <pre>
* 重置
* </pre>
*
* <code>RESET = 3;</code>
*/
public static final int RESET_VALUE = 3;
public final int getNumber() { public final int getNumber() {
@ -99,6 +115,7 @@ public final class CommandStatusProto {
case 0: return UNDO; case 0: return UNDO;
case 1: return DOING; case 1: return DOING;
case 2: return DONE; case 2: return DONE;
case 3: return RESET;
default: return null; default: return null;
} }
} }
@ -164,10 +181,11 @@ public final class CommandStatusProto {
descriptor; descriptor;
static { static {
java.lang.String[] descriptorData = { java.lang.String[] descriptorData = {
"\n\024command_status.proto\022\005proto*.\n\rCommand" + "\n\024command_status.proto\022\005proto*9\n\rCommand" +
"Status\022\010\n\004UNDO\020\000\022\t\n\005DOING\020\001\022\010\n\004DONE\020\002BC\n" + "Status\022\010\n\004UNDO\020\000\022\t\n\005DOING\020\001\022\010\n\004DONE\020\002\022\t\n" +
"-club.joylink.rtss.simulation.ncc.module" + "\005RESET\020\003BC\n-club.joylink.rtss.simulation" +
".protoB\022CommandStatusProtob\006proto3" ".ncc.module.protoB\022CommandStatusProtob\006p" +
"roto3"
}; };
descriptor = com.google.protobuf.Descriptors.FileDescriptor descriptor = com.google.protobuf.Descriptors.FileDescriptor
.internalBuildGeneratedFileFrom(descriptorData, .internalBuildGeneratedFileFrom(descriptorData,