Refactor simulation components for improved functionality and performance
- Update SimulationPlayer to enhance reset handling and progress display - Modify useDelayHandler to incorporate animation speed and improve delay management - Revamp useSpawnHandler to utilize state for active spawns and optimize spawn logic - Integrate reset functionality in Products and Simulator components - Implement execution order and sequence determination in new utility functions - Add trigger extraction logic for better event handling
This commit is contained in:
@@ -40,9 +40,9 @@ const SimulationPlayer: React.FC = () => {
|
|||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (isReset) {
|
if (isReset) {
|
||||||
setTimeout(()=>{
|
setTimeout(() => {
|
||||||
setReset(false);
|
setReset(false);
|
||||||
},0)
|
}, 0)
|
||||||
}
|
}
|
||||||
}, [isReset])
|
}, [isReset])
|
||||||
|
|
||||||
@@ -282,11 +282,10 @@ const SimulationPlayer: React.FC = () => {
|
|||||||
</div>
|
</div>
|
||||||
{index < intervals.length - 1 && (
|
{index < intervals.length - 1 && (
|
||||||
<div
|
<div
|
||||||
className={`line ${
|
className={`line ${progress >= ((index + 1) / totalSegments) * 100
|
||||||
progress >= ((index + 1) / totalSegments) * 100
|
|
||||||
? "filled"
|
? "filled"
|
||||||
: ""
|
: ""
|
||||||
}`}
|
}`}
|
||||||
></div>
|
></div>
|
||||||
)}
|
)}
|
||||||
</React.Fragment>
|
</React.Fragment>
|
||||||
|
|||||||
@@ -1,22 +1,27 @@
|
|||||||
import { useCallback, useEffect, useRef } from "react";
|
import { useCallback, useEffect, useRef } from "react";
|
||||||
import { useFrame } from "@react-three/fiber";
|
import { useFrame } from "@react-three/fiber";
|
||||||
import { usePlayButtonStore, usePauseButtonStore, useResetButtonStore } from "../../../../../store/usePlayButtonStore";
|
import { usePlayButtonStore, usePauseButtonStore, useResetButtonStore, useAnimationPlaySpeed } from "../../../../../store/usePlayButtonStore";
|
||||||
import { useMaterialStore } from "../../../../../store/simulation/useMaterialStore";
|
import { useMaterialStore } from "../../../../../store/simulation/useMaterialStore";
|
||||||
|
|
||||||
interface DelayInstance {
|
interface DelayInstance {
|
||||||
|
initialDelay: number;
|
||||||
delayEndTime: number;
|
delayEndTime: number;
|
||||||
materialId?: string;
|
materialId?: string;
|
||||||
action: ConveyorAction;
|
action: ConveyorAction;
|
||||||
isPaused: boolean;
|
isPaused: boolean;
|
||||||
remainingTime: number;
|
remainingTime: number;
|
||||||
|
lastUpdateTime: number;
|
||||||
|
elapsedTime: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function useDelayHandler() {
|
export function useDelayHandler() {
|
||||||
const { isPlaying } = usePlayButtonStore();
|
const { isPlaying } = usePlayButtonStore();
|
||||||
const { isPaused } = usePauseButtonStore();
|
const { isPaused } = usePauseButtonStore();
|
||||||
const { isReset } = useResetButtonStore();
|
const { isReset } = useResetButtonStore();
|
||||||
|
const { speed } = useAnimationPlaySpeed();
|
||||||
const { setIsPaused } = useMaterialStore();
|
const { setIsPaused } = useMaterialStore();
|
||||||
const activeDelays = useRef<Map<string, DelayInstance>>(new Map());
|
const activeDelays = useRef<Map<string, DelayInstance>>(new Map());
|
||||||
|
const lastUpdateTimeRef = useRef<number>(performance.now());
|
||||||
|
|
||||||
const cleanupDelay = useCallback(() => {
|
const cleanupDelay = useCallback(() => {
|
||||||
activeDelays.current.clear();
|
activeDelays.current.clear();
|
||||||
@@ -30,34 +35,33 @@ export function useDelayHandler() {
|
|||||||
|
|
||||||
const delayLogStatus = (materialUuid: string, status: string) => {
|
const delayLogStatus = (materialUuid: string, status: string) => {
|
||||||
// console.log(`${materialUuid}, ${status}`);
|
// console.log(`${materialUuid}, ${status}`);
|
||||||
}
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
useFrame(() => {
|
||||||
const currentTime = performance.now();
|
const currentTime = performance.now();
|
||||||
|
const deltaTime = currentTime - lastUpdateTimeRef.current;
|
||||||
|
lastUpdateTimeRef.current = currentTime;
|
||||||
|
const completedDelays: string[] = [];
|
||||||
|
|
||||||
activeDelays.current.forEach((delay) => {
|
activeDelays.current.forEach((delay, key) => {
|
||||||
if (isPaused && !delay.isPaused) {
|
if (isPaused && !delay.isPaused) {
|
||||||
delay.remainingTime = Math.max(0, delay.delayEndTime - currentTime);
|
delay.remainingTime = Math.max(0, delay.delayEndTime - currentTime);
|
||||||
delay.isPaused = true;
|
delay.isPaused = true;
|
||||||
} else if (!isPaused && delay.isPaused) {
|
} else if (!isPaused && delay.isPaused) {
|
||||||
delay.delayEndTime = currentTime + delay.remainingTime;
|
delay.delayEndTime = currentTime + delay.remainingTime;
|
||||||
delay.isPaused = false;
|
delay.isPaused = false;
|
||||||
delay.remainingTime = 0;
|
delay.lastUpdateTime = currentTime;
|
||||||
}
|
delay.elapsedTime = 0;
|
||||||
});
|
} else if (!delay.isPaused && isPlaying) {
|
||||||
}, [isPaused]);
|
delay.elapsedTime += deltaTime * speed;
|
||||||
|
|
||||||
useFrame(() => {
|
if (delay.elapsedTime >= delay.initialDelay) {
|
||||||
if (!isPlaying || isPaused) return;
|
if (delay.materialId) {
|
||||||
|
delayLogStatus(delay.materialId, `Delay completed`);
|
||||||
const currentTime = performance.now();
|
setIsPaused(delay.materialId, false);
|
||||||
const completedDelays: string[] = [];
|
}
|
||||||
|
completedDelays.push(key);
|
||||||
activeDelays.current.forEach((delay, key) => {
|
}
|
||||||
if (!delay.isPaused && currentTime >= delay.delayEndTime && delay.materialId) {
|
|
||||||
delayLogStatus(delay.materialId, `Delay completed}`);
|
|
||||||
setIsPaused(delay.materialId, false);
|
|
||||||
completedDelays.push(key);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -74,22 +78,24 @@ export function useDelayHandler() {
|
|||||||
|
|
||||||
const key = materialId ? `${materialId}-${action.actionUuid}` : action.actionUuid;
|
const key = materialId ? `${materialId}-${action.actionUuid}` : action.actionUuid;
|
||||||
|
|
||||||
// If this material already has a delay, cancel it
|
|
||||||
if (activeDelays.current.has(key)) {
|
if (activeDelays.current.has(key)) {
|
||||||
activeDelays.current.delete(key);
|
activeDelays.current.delete(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const now = performance.now();
|
||||||
activeDelays.current.set(key, {
|
activeDelays.current.set(key, {
|
||||||
delayEndTime: performance.now() + delayMs,
|
initialDelay: delayMs,
|
||||||
|
delayEndTime: now + delayMs,
|
||||||
materialId,
|
materialId,
|
||||||
action,
|
action,
|
||||||
isPaused: false,
|
isPaused: false,
|
||||||
remainingTime: 0
|
remainingTime: 0,
|
||||||
|
lastUpdateTime: now,
|
||||||
|
elapsedTime: 0
|
||||||
});
|
});
|
||||||
|
|
||||||
delayLogStatus(materialId, `Started ${delayMs * 1000}s delay`);
|
delayLogStatus(materialId, `Started ${delayMs / 1000}s delay`);
|
||||||
setIsPaused(materialId, true);
|
setIsPaused(materialId, true);
|
||||||
|
|
||||||
}, [isPlaying]);
|
}, [isPlaying]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { useCallback, useEffect, useRef } from "react";
|
import { useCallback, useEffect, useState } from "react";
|
||||||
import * as THREE from 'three';
|
import * as THREE from 'three';
|
||||||
import { useFrame } from "@react-three/fiber";
|
import { useFrame } from "@react-three/fiber";
|
||||||
import { useMaterialStore } from "../../../../../store/simulation/useMaterialStore";
|
import { useMaterialStore } from "../../../../../store/simulation/useMaterialStore";
|
||||||
@@ -32,10 +32,23 @@ export function useSpawnHandler() {
|
|||||||
const { isReset } = useResetButtonStore();
|
const { isReset } = useResetButtonStore();
|
||||||
const { selectedProduct } = useSelectedProduct();
|
const { selectedProduct } = useSelectedProduct();
|
||||||
|
|
||||||
const activeSpawns = useRef<Map<string, SpawnInstance>>(new Map());
|
const [activeSpawns, setActiveSpawns] = useState<Map<string, SpawnInstance>>(new Map());
|
||||||
|
|
||||||
|
const getConveyorPausedState = useCallback((action: ConveyorAction) => {
|
||||||
|
const modelUuid = getModelUuidByActionUuid(selectedProduct.productId, action.actionUuid);
|
||||||
|
if (!modelUuid) return false;
|
||||||
|
|
||||||
|
const conveyor = getConveyorById(modelUuid);
|
||||||
|
if (!conveyor) return false;
|
||||||
|
return conveyor.isPaused;
|
||||||
|
}, [getConveyorById, getModelUuidByActionUuid, selectedProduct.productId]);
|
||||||
|
|
||||||
|
const shouldPauseSpawn = useCallback((action: ConveyorAction) => {
|
||||||
|
return isPaused || getConveyorPausedState(action);
|
||||||
|
}, [isPaused, getConveyorPausedState]);
|
||||||
|
|
||||||
const clearAllSpawns = useCallback(() => {
|
const clearAllSpawns = useCallback(() => {
|
||||||
activeSpawns.current.clear();
|
setActiveSpawns(new Map());
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@@ -84,92 +97,96 @@ export function useSpawnHandler() {
|
|||||||
return newMaterial;
|
return newMaterial;
|
||||||
}, [addMaterial, getModelUuidByActionUuid, getPointUuidByActionUuid, selectedProduct.productId]);
|
}, [addMaterial, getModelUuidByActionUuid, getPointUuidByActionUuid, selectedProduct.productId]);
|
||||||
|
|
||||||
|
useFrame(() => {
|
||||||
const getConveyorPausedState = useCallback((action: ConveyorAction) => {
|
|
||||||
const modelUuid = getModelUuidByActionUuid(selectedProduct.productId, action.actionUuid);
|
|
||||||
if (!modelUuid) return false;
|
|
||||||
|
|
||||||
const conveyor = getConveyorById(modelUuid);
|
|
||||||
return conveyor?.isPaused ?? false;
|
|
||||||
}, [getConveyorById, getModelUuidByActionUuid, selectedProduct.productId]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
const currentTime = performance.now();
|
const currentTime = performance.now();
|
||||||
|
const completedActions: string[] = [];
|
||||||
|
let hasChanges = false;
|
||||||
|
|
||||||
activeSpawns.current.forEach((spawn) => {
|
activeSpawns.forEach(spawn => {
|
||||||
if (isPaused && !spawn.isPaused) {
|
const isPausedNow = shouldPauseSpawn(spawn.params.action);
|
||||||
|
|
||||||
|
if (isPausedNow && !spawn.isPaused) {
|
||||||
if (spawn.lastSpawnTime === null) {
|
if (spawn.lastSpawnTime === null) {
|
||||||
spawn.remainingTime = Math.max(0, spawn.params.intervalMs - (currentTime - spawn.startTime));
|
spawn.remainingTime = Math.max(0, (spawn.params.intervalMs / speed) - (currentTime - spawn.startTime));
|
||||||
} else {
|
} else {
|
||||||
spawn.remainingTime = Math.max(0, spawn.params.intervalMs - (currentTime - spawn.lastSpawnTime));
|
spawn.remainingTime = Math.max(0, (spawn.params.intervalMs / speed) - (currentTime - spawn.lastSpawnTime));
|
||||||
}
|
}
|
||||||
spawn.pauseStartTime = currentTime;
|
spawn.pauseStartTime = currentTime;
|
||||||
spawn.isPaused = true;
|
spawn.isPaused = true;
|
||||||
} else if (!isPaused && spawn.isPaused) {
|
hasChanges = true;
|
||||||
if (spawn.remainingTime > 0) {
|
} else if (!isPausedNow && spawn.isPaused) {
|
||||||
if (spawn.lastSpawnTime === null) {
|
const pauseDuration = currentTime - spawn.pauseStartTime;
|
||||||
spawn.startTime = currentTime - (spawn.params.intervalMs - spawn.remainingTime);
|
if (spawn.lastSpawnTime === null) {
|
||||||
} else {
|
spawn.startTime += pauseDuration;
|
||||||
spawn.lastSpawnTime = currentTime - (spawn.params.intervalMs - spawn.remainingTime);
|
} else {
|
||||||
}
|
spawn.lastSpawnTime += pauseDuration;
|
||||||
}
|
}
|
||||||
spawn.isPaused = false;
|
spawn.isPaused = false;
|
||||||
spawn.pauseStartTime = 0;
|
spawn.pauseStartTime = 0;
|
||||||
spawn.remainingTime = 0;
|
spawn.remainingTime = 0;
|
||||||
|
hasChanges = true;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}, [isPaused]);
|
|
||||||
|
|
||||||
useFrame(() => {
|
if (isPlaying && !isReset && !isPaused) {
|
||||||
if (!isPlaying || isPaused || isReset) return;
|
activeSpawns.forEach((spawn, actionUuid) => {
|
||||||
|
if (spawn.isPaused) return;
|
||||||
|
|
||||||
const currentTime = performance.now();
|
const { material, intervalMs, totalCount, action } = spawn.params;
|
||||||
const completedActions: string[] = [];
|
const adjustedInterval = intervalMs / speed;
|
||||||
|
const isFirstSpawn = spawn.lastSpawnTime === null;
|
||||||
|
|
||||||
activeSpawns.current.forEach((spawn, actionUuid) => {
|
// First spawn
|
||||||
const { material, intervalMs, totalCount, action } = spawn.params;
|
if (isFirstSpawn) {
|
||||||
const isFirstSpawn = spawn.lastSpawnTime === null;
|
const elapsed = currentTime - spawn.startTime;
|
||||||
|
if (elapsed >= adjustedInterval) {
|
||||||
|
const createdMaterial = createNewMaterial(material, action);
|
||||||
|
if (createdMaterial) {
|
||||||
|
spawnLogStatus(createdMaterial.materialId, `[${elapsed.toFixed(2)}ms] Spawned ${material} (1/${totalCount})`);
|
||||||
|
}
|
||||||
|
spawn.lastSpawnTime = currentTime;
|
||||||
|
spawn.spawnCount = 1;
|
||||||
|
hasChanges = true;
|
||||||
|
|
||||||
// First spawn
|
if (totalCount <= 1) {
|
||||||
if (isFirstSpawn) {
|
completedActions.push(actionUuid);
|
||||||
const elapsed = currentTime - spawn.startTime;
|
}
|
||||||
if (elapsed >= intervalMs) {
|
|
||||||
const createdMaterial = createNewMaterial(material, action);
|
|
||||||
if (createdMaterial) {
|
|
||||||
spawnLogStatus(createdMaterial.materialId, `[${elapsed.toFixed(2)}ms] Spawned ${material} (1/${totalCount})`);
|
|
||||||
}
|
}
|
||||||
spawn.lastSpawnTime = currentTime;
|
return;
|
||||||
spawn.spawnCount = 1;
|
}
|
||||||
|
|
||||||
if (totalCount <= 1) {
|
// Subsequent spawns
|
||||||
completedActions.push(actionUuid);
|
if (spawn.lastSpawnTime !== null) {
|
||||||
|
const timeSinceLast = currentTime - spawn.lastSpawnTime;
|
||||||
|
if (timeSinceLast >= adjustedInterval) {
|
||||||
|
const count = spawn.spawnCount + 1;
|
||||||
|
const createdMaterial = createNewMaterial(material, action);
|
||||||
|
if (createdMaterial) {
|
||||||
|
spawnLogStatus(createdMaterial.materialId, `[${timeSinceLast.toFixed(2)}ms] Spawned ${material} (${count}/${totalCount})`);
|
||||||
|
}
|
||||||
|
spawn.lastSpawnTime = currentTime;
|
||||||
|
spawn.spawnCount = count;
|
||||||
|
hasChanges = true;
|
||||||
|
|
||||||
|
if (count >= totalCount) {
|
||||||
|
completedActions.push(actionUuid);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return;
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Subsequent spawns
|
if (hasChanges || completedActions.length > 0) {
|
||||||
if (spawn.lastSpawnTime !== null) {
|
setActiveSpawns(prevSpawns => {
|
||||||
const timeSinceLast = currentTime - spawn.lastSpawnTime;
|
const newSpawns = new Map(prevSpawns);
|
||||||
if (timeSinceLast >= intervalMs) {
|
|
||||||
const count = spawn.spawnCount + 1;
|
|
||||||
const createdMaterial = createNewMaterial(material, action);
|
|
||||||
if (createdMaterial) {
|
|
||||||
spawnLogStatus(createdMaterial.materialId, `[${timeSinceLast.toFixed(2)}ms] Spawned ${material} (${count}/${totalCount})`);
|
|
||||||
}
|
|
||||||
spawn.lastSpawnTime = currentTime;
|
|
||||||
spawn.spawnCount = count;
|
|
||||||
|
|
||||||
if (count >= totalCount) {
|
completedActions.forEach(actionUuid => {
|
||||||
completedActions.push(actionUuid);
|
newSpawns.delete(actionUuid);
|
||||||
}
|
});
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
completedActions.forEach(actionUuid => {
|
return newSpawns;
|
||||||
activeSpawns.current.delete(actionUuid);
|
});
|
||||||
});
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
const handleSpawn = useCallback((action: ConveyorAction) => {
|
const handleSpawn = useCallback((action: ConveyorAction) => {
|
||||||
@@ -178,23 +195,29 @@ export function useSpawnHandler() {
|
|||||||
const { material, spawnInterval = 0, spawnCount = 1, actionUuid } = action;
|
const { material, spawnInterval = 0, spawnCount = 1, actionUuid } = action;
|
||||||
const intervalMs = spawnInterval * 1000;
|
const intervalMs = spawnInterval * 1000;
|
||||||
|
|
||||||
if (activeSpawns.current.has(actionUuid)) {
|
setActiveSpawns(prevSpawns => {
|
||||||
activeSpawns.current.delete(actionUuid);
|
const newSpawns = new Map(prevSpawns);
|
||||||
}
|
|
||||||
|
|
||||||
activeSpawns.current.set(actionUuid, {
|
if (newSpawns.has(actionUuid)) {
|
||||||
lastSpawnTime: null,
|
newSpawns.delete(actionUuid);
|
||||||
startTime: performance.now(),
|
}
|
||||||
spawnCount: 0,
|
|
||||||
params: {
|
newSpawns.set(actionUuid, {
|
||||||
material,
|
lastSpawnTime: null,
|
||||||
intervalMs,
|
startTime: performance.now(),
|
||||||
totalCount: spawnCount,
|
spawnCount: 0,
|
||||||
action: action
|
params: {
|
||||||
},
|
material,
|
||||||
pauseStartTime: 0,
|
intervalMs,
|
||||||
remainingTime: 0,
|
totalCount: spawnCount,
|
||||||
isPaused: false
|
action: action
|
||||||
|
},
|
||||||
|
pauseStartTime: 0,
|
||||||
|
remainingTime: 0,
|
||||||
|
isPaused: false
|
||||||
|
});
|
||||||
|
|
||||||
|
return newSpawns;
|
||||||
});
|
});
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import { getAllProductsApi } from '../../../services/simulation/getallProductsAp
|
|||||||
import { useVehicleStore } from '../../../store/simulation/useVehicleStore';
|
import { useVehicleStore } from '../../../store/simulation/useVehicleStore';
|
||||||
import { useArmBotStore } from '../../../store/simulation/useArmBotStore';
|
import { useArmBotStore } from '../../../store/simulation/useArmBotStore';
|
||||||
import { useConveyorStore } from '../../../store/simulation/useConveyorStore';
|
import { useConveyorStore } from '../../../store/simulation/useConveyorStore';
|
||||||
|
import { useResetButtonStore } from '../../../store/usePlayButtonStore';
|
||||||
|
|
||||||
function Products() {
|
function Products() {
|
||||||
const { products, getProductById, addProduct, setProducts } = useProductStore();
|
const { products, getProductById, addProduct, setProducts } = useProductStore();
|
||||||
@@ -15,6 +16,7 @@ function Products() {
|
|||||||
const { addVehicle, clearvehicles } = useVehicleStore();
|
const { addVehicle, clearvehicles } = useVehicleStore();
|
||||||
const { addArmBot, clearArmBots } = useArmBotStore();
|
const { addArmBot, clearArmBots } = useArmBotStore();
|
||||||
const { addConveyor, clearConveyors } = useConveyorStore();
|
const { addConveyor, clearConveyors } = useConveyorStore();
|
||||||
|
const { isReset } = useResetButtonStore();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const email = localStorage.getItem('email')
|
const email = localStorage.getItem('email')
|
||||||
@@ -45,7 +47,7 @@ function Products() {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, [selectedProduct, products]);
|
}, [selectedProduct, products, isReset]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (selectedProduct.productId) {
|
if (selectedProduct.productId) {
|
||||||
@@ -59,7 +61,7 @@ function Products() {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, [selectedProduct, products]);
|
}, [selectedProduct, products, isReset]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (selectedProduct.productId) {
|
if (selectedProduct.productId) {
|
||||||
@@ -73,7 +75,7 @@ function Products() {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, [selectedProduct, products]);
|
}, [selectedProduct, products, isReset]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
|
|||||||
@@ -0,0 +1,114 @@
|
|||||||
|
import { extractTriggersFromPoint } from "./extractTriggersFromPoint";
|
||||||
|
|
||||||
|
export function determineExecutionOrder(products: productsSchema): PointsScheme[] {
|
||||||
|
// Create maps for all events and points
|
||||||
|
const eventMap = new Map<string, EventsSchema>();
|
||||||
|
const pointMap = new Map<string, PointsScheme>();
|
||||||
|
const allPoints: PointsScheme[] = [];
|
||||||
|
|
||||||
|
// First pass: collect all points
|
||||||
|
products.forEach(product => {
|
||||||
|
product.eventDatas.forEach(event => {
|
||||||
|
eventMap.set(event.modelUuid, event);
|
||||||
|
|
||||||
|
if (event.type === 'transfer') {
|
||||||
|
event.points.forEach(point => {
|
||||||
|
pointMap.set(point.uuid, point);
|
||||||
|
allPoints.push(point);
|
||||||
|
});
|
||||||
|
} else if (event.type === 'vehicle' ||
|
||||||
|
event.type === 'machine' ||
|
||||||
|
event.type === 'storageUnit') {
|
||||||
|
pointMap.set(event.point.uuid, event.point);
|
||||||
|
allPoints.push(event.point);
|
||||||
|
} else if (event.type === 'roboticArm') {
|
||||||
|
pointMap.set(event.point.uuid, event.point);
|
||||||
|
allPoints.push(event.point);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// Build dependency graphs
|
||||||
|
const graph = new Map<string, string[]>();
|
||||||
|
const reverseGraph = new Map<string, string[]>();
|
||||||
|
const allTriggeredPoints = new Set<string>();
|
||||||
|
|
||||||
|
allPoints.forEach(point => {
|
||||||
|
const triggers = extractTriggersFromPoint(point);
|
||||||
|
const dependencies: string[] = [];
|
||||||
|
|
||||||
|
triggers.forEach(trigger => {
|
||||||
|
const targetUuid = trigger.triggeredAsset?.triggeredPoint?.pointUuid;
|
||||||
|
if (targetUuid && pointMap.has(targetUuid)) {
|
||||||
|
dependencies.push(targetUuid);
|
||||||
|
allTriggeredPoints.add(targetUuid);
|
||||||
|
|
||||||
|
if (!reverseGraph.has(targetUuid)) {
|
||||||
|
reverseGraph.set(targetUuid, []);
|
||||||
|
}
|
||||||
|
reverseGraph.get(targetUuid)!.push(point.uuid);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
graph.set(point.uuid, dependencies);
|
||||||
|
});
|
||||||
|
|
||||||
|
// Identify root points (points that trigger others but aren't triggered themselves)
|
||||||
|
const rootPoints = allPoints
|
||||||
|
.filter(point => !allTriggeredPoints.has(point.uuid))
|
||||||
|
.filter(point => {
|
||||||
|
// Only include roots that actually have triggers pointing FROM them
|
||||||
|
const triggers = extractTriggersFromPoint(point);
|
||||||
|
return triggers.some(t => t.triggeredAsset?.triggeredPoint?.pointUuid);
|
||||||
|
});
|
||||||
|
|
||||||
|
// If no root points found but we have triggered points, find the earliest triggers
|
||||||
|
if (rootPoints.length === 0 && allTriggeredPoints.size > 0) {
|
||||||
|
// This handles cases where we have circular dependencies
|
||||||
|
// but still want to include the triggered points
|
||||||
|
const minTriggerCount = Math.min(
|
||||||
|
...Array.from(allTriggeredPoints)
|
||||||
|
.map(uuid => (graph.get(uuid) || []).length)
|
||||||
|
);
|
||||||
|
const potentialRoots = Array.from(allTriggeredPoints)
|
||||||
|
.filter(uuid => (graph.get(uuid) || []).length === minTriggerCount);
|
||||||
|
|
||||||
|
rootPoints.push(...potentialRoots.map(uuid => pointMap.get(uuid)!));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Topological sort only for triggered points
|
||||||
|
const visited = new Set<string>();
|
||||||
|
const temp = new Set<string>();
|
||||||
|
const order: string[] = [];
|
||||||
|
let hasCycle = false;
|
||||||
|
|
||||||
|
function visit(node: string) {
|
||||||
|
if (temp.has(node)) {
|
||||||
|
hasCycle = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (visited.has(node)) return;
|
||||||
|
|
||||||
|
temp.add(node);
|
||||||
|
|
||||||
|
const dependencies = reverseGraph.get(node) || [];
|
||||||
|
for (const dep of dependencies) {
|
||||||
|
visit(dep);
|
||||||
|
}
|
||||||
|
|
||||||
|
temp.delete(node);
|
||||||
|
visited.add(node);
|
||||||
|
order.push(node);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Start processing from root points
|
||||||
|
rootPoints.forEach(root => visit(root.uuid));
|
||||||
|
|
||||||
|
// Convert UUIDs back to points and filter out untriggered points
|
||||||
|
const triggeredPoints = order
|
||||||
|
.map(uuid => pointMap.get(uuid)!)
|
||||||
|
.filter(point => allTriggeredPoints.has(point.uuid) ||
|
||||||
|
rootPoints.some(root => root.uuid === point.uuid));
|
||||||
|
|
||||||
|
return triggeredPoints;
|
||||||
|
}
|
||||||
@@ -0,0 +1,101 @@
|
|||||||
|
import { extractTriggersFromPoint } from "./extractTriggersFromPoint";
|
||||||
|
|
||||||
|
export function determineExecutionSequences(products: productsSchema): PointsScheme[][] {
|
||||||
|
// Create maps for all points
|
||||||
|
const pointMap = new Map<string, PointsScheme>();
|
||||||
|
const allPoints: PointsScheme[] = [];
|
||||||
|
|
||||||
|
// First pass: collect all points
|
||||||
|
products.forEach(product => {
|
||||||
|
product.eventDatas.forEach(event => {
|
||||||
|
if (event.type === 'transfer') {
|
||||||
|
event.points.forEach(point => {
|
||||||
|
pointMap.set(point.uuid, point);
|
||||||
|
allPoints.push(point);
|
||||||
|
});
|
||||||
|
} else if (event.type === 'vehicle' ||
|
||||||
|
event.type === 'machine' ||
|
||||||
|
event.type === 'storageUnit' ||
|
||||||
|
event.type === 'roboticArm') {
|
||||||
|
pointMap.set(event.point.uuid, event.point);
|
||||||
|
allPoints.push(event.point);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// Build complete dependency graph
|
||||||
|
const dependencyGraph = new Map<string, string[]>();
|
||||||
|
const reverseDependencyGraph = new Map<string, string[]>();
|
||||||
|
const triggeredPoints = new Set<string>();
|
||||||
|
|
||||||
|
allPoints.forEach(point => {
|
||||||
|
const triggers = extractTriggersFromPoint(point);
|
||||||
|
const dependencies: string[] = [];
|
||||||
|
|
||||||
|
triggers.forEach(trigger => {
|
||||||
|
const targetUuid = trigger.triggeredAsset?.triggeredPoint?.pointUuid;
|
||||||
|
if (targetUuid && pointMap.has(targetUuid)) {
|
||||||
|
dependencies.push(targetUuid);
|
||||||
|
triggeredPoints.add(targetUuid);
|
||||||
|
|
||||||
|
if (!reverseDependencyGraph.has(targetUuid)) {
|
||||||
|
reverseDependencyGraph.set(targetUuid, []);
|
||||||
|
}
|
||||||
|
reverseDependencyGraph.get(targetUuid)!.push(point.uuid);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
dependencyGraph.set(point.uuid, dependencies);
|
||||||
|
});
|
||||||
|
|
||||||
|
// Identify independent root points (points that trigger others but aren't triggered themselves)
|
||||||
|
const rootPoints = allPoints.filter(point => {
|
||||||
|
const hasOutgoingTriggers = extractTriggersFromPoint(point).some(
|
||||||
|
t => t.triggeredAsset?.triggeredPoint?.pointUuid
|
||||||
|
);
|
||||||
|
return hasOutgoingTriggers && !triggeredPoints.has(point.uuid);
|
||||||
|
});
|
||||||
|
|
||||||
|
// For each root point, build its complete trigger chain
|
||||||
|
const executionSequences: PointsScheme[][] = [];
|
||||||
|
|
||||||
|
function buildSequence(startUuid: string): PointsScheme[] {
|
||||||
|
const sequence: PointsScheme[] = [];
|
||||||
|
const visited = new Set<string>();
|
||||||
|
|
||||||
|
function traverse(uuid: string) {
|
||||||
|
if (visited.has(uuid)) return;
|
||||||
|
visited.add(uuid);
|
||||||
|
|
||||||
|
const point = pointMap.get(uuid);
|
||||||
|
if (point) {
|
||||||
|
sequence.push(point);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Follow forward dependencies
|
||||||
|
const nextPoints = dependencyGraph.get(uuid) || [];
|
||||||
|
nextPoints.forEach(nextUuid => traverse(nextUuid));
|
||||||
|
}
|
||||||
|
|
||||||
|
traverse(startUuid);
|
||||||
|
return sequence;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Build sequences for all root points
|
||||||
|
rootPoints.forEach(root => {
|
||||||
|
executionSequences.push(buildSequence(root.uuid));
|
||||||
|
});
|
||||||
|
|
||||||
|
// Handle any triggered points not reachable from roots (isolated chains)
|
||||||
|
const processedPoints = new Set(
|
||||||
|
executionSequences.flat().map(p => p.uuid)
|
||||||
|
);
|
||||||
|
|
||||||
|
allPoints.forEach(point => {
|
||||||
|
if (triggeredPoints.has(point.uuid) && !processedPoints.has(point.uuid)) {
|
||||||
|
executionSequences.push(buildSequence(point.uuid));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return executionSequences;
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
|
||||||
|
export function extractTriggersFromPoint(point: PointsScheme): TriggerSchema[] {
|
||||||
|
if ('actions' in point) {
|
||||||
|
return point.actions.flatMap(action => action.triggers);
|
||||||
|
} else if ('action' in point) {
|
||||||
|
return point.action.triggers;
|
||||||
|
}
|
||||||
|
return [];
|
||||||
|
}
|
||||||
@@ -2,6 +2,7 @@ import { useEffect } from 'react';
|
|||||||
import { useProductStore } from '../../../store/simulation/useProductStore';
|
import { useProductStore } from '../../../store/simulation/useProductStore';
|
||||||
import { useActionHandler } from '../actions/useActionHandler';
|
import { useActionHandler } from '../actions/useActionHandler';
|
||||||
import { usePlayButtonStore, useResetButtonStore } from '../../../store/usePlayButtonStore';
|
import { usePlayButtonStore, useResetButtonStore } from '../../../store/usePlayButtonStore';
|
||||||
|
import { determineExecutionOrder } from './functions/determineExecutionOrder';
|
||||||
|
|
||||||
function Simulator() {
|
function Simulator() {
|
||||||
const { products } = useProductStore();
|
const { products } = useProductStore();
|
||||||
@@ -17,231 +18,15 @@ function Simulator() {
|
|||||||
const action = 'actions' in point ? point.actions[0] : point.action;
|
const action = 'actions' in point ? point.actions[0] : point.action;
|
||||||
handleAction(action);
|
handleAction(action);
|
||||||
});
|
});
|
||||||
}, [products, handleAction, isPlaying, isReset]);
|
}, [products, isPlaying, isReset]);
|
||||||
|
|
||||||
function determineExecutionOrder(products: productsSchema): PointsScheme[] {
|
return (
|
||||||
// Create maps for all events and points
|
|
||||||
const eventMap = new Map<string, EventsSchema>();
|
|
||||||
const pointMap = new Map<string, PointsScheme>();
|
|
||||||
const allPoints: PointsScheme[] = [];
|
|
||||||
|
|
||||||
// First pass: collect all points
|
<>
|
||||||
products.forEach(product => {
|
|
||||||
product.eventDatas.forEach(event => {
|
|
||||||
eventMap.set(event.modelUuid, event);
|
|
||||||
|
|
||||||
if (event.type === 'transfer') {
|
</>
|
||||||
event.points.forEach(point => {
|
|
||||||
pointMap.set(point.uuid, point);
|
|
||||||
allPoints.push(point);
|
|
||||||
});
|
|
||||||
} else if (event.type === 'vehicle' ||
|
|
||||||
event.type === 'machine' ||
|
|
||||||
event.type === 'storageUnit') {
|
|
||||||
pointMap.set(event.point.uuid, event.point);
|
|
||||||
allPoints.push(event.point);
|
|
||||||
} else if (event.type === 'roboticArm') {
|
|
||||||
pointMap.set(event.point.uuid, event.point);
|
|
||||||
allPoints.push(event.point);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
// Build dependency graphs
|
);
|
||||||
const graph = new Map<string, string[]>();
|
|
||||||
const reverseGraph = new Map<string, string[]>();
|
|
||||||
const allTriggeredPoints = new Set<string>();
|
|
||||||
|
|
||||||
allPoints.forEach(point => {
|
|
||||||
const triggers = extractTriggersFromPoint(point);
|
|
||||||
const dependencies: string[] = [];
|
|
||||||
|
|
||||||
triggers.forEach(trigger => {
|
|
||||||
const targetUuid = trigger.triggeredAsset?.triggeredPoint?.pointUuid;
|
|
||||||
if (targetUuid && pointMap.has(targetUuid)) {
|
|
||||||
dependencies.push(targetUuid);
|
|
||||||
allTriggeredPoints.add(targetUuid);
|
|
||||||
|
|
||||||
if (!reverseGraph.has(targetUuid)) {
|
|
||||||
reverseGraph.set(targetUuid, []);
|
|
||||||
}
|
|
||||||
reverseGraph.get(targetUuid)!.push(point.uuid);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
graph.set(point.uuid, dependencies);
|
|
||||||
});
|
|
||||||
|
|
||||||
// Identify root points (points that trigger others but aren't triggered themselves)
|
|
||||||
const rootPoints = allPoints
|
|
||||||
.filter(point => !allTriggeredPoints.has(point.uuid))
|
|
||||||
.filter(point => {
|
|
||||||
// Only include roots that actually have triggers pointing FROM them
|
|
||||||
const triggers = extractTriggersFromPoint(point);
|
|
||||||
return triggers.some(t => t.triggeredAsset?.triggeredPoint?.pointUuid);
|
|
||||||
});
|
|
||||||
|
|
||||||
// If no root points found but we have triggered points, find the earliest triggers
|
|
||||||
if (rootPoints.length === 0 && allTriggeredPoints.size > 0) {
|
|
||||||
// This handles cases where we have circular dependencies
|
|
||||||
// but still want to include the triggered points
|
|
||||||
const minTriggerCount = Math.min(
|
|
||||||
...Array.from(allTriggeredPoints)
|
|
||||||
.map(uuid => (graph.get(uuid) || []).length)
|
|
||||||
);
|
|
||||||
const potentialRoots = Array.from(allTriggeredPoints)
|
|
||||||
.filter(uuid => (graph.get(uuid) || []).length === minTriggerCount);
|
|
||||||
|
|
||||||
rootPoints.push(...potentialRoots.map(uuid => pointMap.get(uuid)!));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Topological sort only for triggered points
|
|
||||||
const visited = new Set<string>();
|
|
||||||
const temp = new Set<string>();
|
|
||||||
const order: string[] = [];
|
|
||||||
let hasCycle = false;
|
|
||||||
|
|
||||||
function visit(node: string) {
|
|
||||||
if (temp.has(node)) {
|
|
||||||
hasCycle = true;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (visited.has(node)) return;
|
|
||||||
|
|
||||||
temp.add(node);
|
|
||||||
|
|
||||||
const dependencies = reverseGraph.get(node) || [];
|
|
||||||
for (const dep of dependencies) {
|
|
||||||
visit(dep);
|
|
||||||
}
|
|
||||||
|
|
||||||
temp.delete(node);
|
|
||||||
visited.add(node);
|
|
||||||
order.push(node);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Start processing from root points
|
|
||||||
rootPoints.forEach(root => visit(root.uuid));
|
|
||||||
|
|
||||||
// Convert UUIDs back to points and filter out untriggered points
|
|
||||||
const triggeredPoints = order
|
|
||||||
.map(uuid => pointMap.get(uuid)!)
|
|
||||||
.filter(point => allTriggeredPoints.has(point.uuid) ||
|
|
||||||
rootPoints.some(root => root.uuid === point.uuid));
|
|
||||||
|
|
||||||
return triggeredPoints;
|
|
||||||
}
|
|
||||||
|
|
||||||
function determineExecutionSequences(products: productsSchema): PointsScheme[][] {
|
|
||||||
// Create maps for all points
|
|
||||||
const pointMap = new Map<string, PointsScheme>();
|
|
||||||
const allPoints: PointsScheme[] = [];
|
|
||||||
|
|
||||||
// First pass: collect all points
|
|
||||||
products.forEach(product => {
|
|
||||||
product.eventDatas.forEach(event => {
|
|
||||||
if (event.type === 'transfer') {
|
|
||||||
event.points.forEach(point => {
|
|
||||||
pointMap.set(point.uuid, point);
|
|
||||||
allPoints.push(point);
|
|
||||||
});
|
|
||||||
} else if (event.type === 'vehicle' ||
|
|
||||||
event.type === 'machine' ||
|
|
||||||
event.type === 'storageUnit' ||
|
|
||||||
event.type === 'roboticArm') {
|
|
||||||
pointMap.set(event.point.uuid, event.point);
|
|
||||||
allPoints.push(event.point);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
// Build complete dependency graph
|
|
||||||
const dependencyGraph = new Map<string, string[]>();
|
|
||||||
const reverseDependencyGraph = new Map<string, string[]>();
|
|
||||||
const triggeredPoints = new Set<string>();
|
|
||||||
|
|
||||||
allPoints.forEach(point => {
|
|
||||||
const triggers = extractTriggersFromPoint(point);
|
|
||||||
const dependencies: string[] = [];
|
|
||||||
|
|
||||||
triggers.forEach(trigger => {
|
|
||||||
const targetUuid = trigger.triggeredAsset?.triggeredPoint?.pointUuid;
|
|
||||||
if (targetUuid && pointMap.has(targetUuid)) {
|
|
||||||
dependencies.push(targetUuid);
|
|
||||||
triggeredPoints.add(targetUuid);
|
|
||||||
|
|
||||||
if (!reverseDependencyGraph.has(targetUuid)) {
|
|
||||||
reverseDependencyGraph.set(targetUuid, []);
|
|
||||||
}
|
|
||||||
reverseDependencyGraph.get(targetUuid)!.push(point.uuid);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
dependencyGraph.set(point.uuid, dependencies);
|
|
||||||
});
|
|
||||||
|
|
||||||
// Identify independent root points (points that trigger others but aren't triggered themselves)
|
|
||||||
const rootPoints = allPoints.filter(point => {
|
|
||||||
const hasOutgoingTriggers = extractTriggersFromPoint(point).some(
|
|
||||||
t => t.triggeredAsset?.triggeredPoint?.pointUuid
|
|
||||||
);
|
|
||||||
return hasOutgoingTriggers && !triggeredPoints.has(point.uuid);
|
|
||||||
});
|
|
||||||
|
|
||||||
// For each root point, build its complete trigger chain
|
|
||||||
const executionSequences: PointsScheme[][] = [];
|
|
||||||
|
|
||||||
function buildSequence(startUuid: string): PointsScheme[] {
|
|
||||||
const sequence: PointsScheme[] = [];
|
|
||||||
const visited = new Set<string>();
|
|
||||||
|
|
||||||
function traverse(uuid: string) {
|
|
||||||
if (visited.has(uuid)) return;
|
|
||||||
visited.add(uuid);
|
|
||||||
|
|
||||||
const point = pointMap.get(uuid);
|
|
||||||
if (point) {
|
|
||||||
sequence.push(point);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Follow forward dependencies
|
|
||||||
const nextPoints = dependencyGraph.get(uuid) || [];
|
|
||||||
nextPoints.forEach(nextUuid => traverse(nextUuid));
|
|
||||||
}
|
|
||||||
|
|
||||||
traverse(startUuid);
|
|
||||||
return sequence;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Build sequences for all root points
|
|
||||||
rootPoints.forEach(root => {
|
|
||||||
executionSequences.push(buildSequence(root.uuid));
|
|
||||||
});
|
|
||||||
|
|
||||||
// Handle any triggered points not reachable from roots (isolated chains)
|
|
||||||
const processedPoints = new Set(
|
|
||||||
executionSequences.flat().map(p => p.uuid)
|
|
||||||
);
|
|
||||||
|
|
||||||
allPoints.forEach(point => {
|
|
||||||
if (triggeredPoints.has(point.uuid) && !processedPoints.has(point.uuid)) {
|
|
||||||
executionSequences.push(buildSequence(point.uuid));
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return executionSequences;
|
|
||||||
}
|
|
||||||
|
|
||||||
function extractTriggersFromPoint(point: PointsScheme): TriggerSchema[] {
|
|
||||||
if ('actions' in point) {
|
|
||||||
return point.actions.flatMap(action => action.triggers);
|
|
||||||
} else if ('action' in point) {
|
|
||||||
return point.action.triggers;
|
|
||||||
}
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
|
|
||||||
return <></>;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default Simulator;
|
export default Simulator;
|
||||||
Reference in New Issue
Block a user