/**
* APPLICATION ORCHESTRATOR (Vue 3)
* --------------------------------
* Binds the single reactive state to the DOM, handles user inputs, and routes
* data between the Event Log, Domain Actions, Ontologies (SMIM), Storage (SMS),
* Viewpoints (URMSM), and Renderers.
*/
import { createApp, computed, watch, onMounted, ref, reactive, nextTick } from 'vue';
import { state, advanceTime, loadState, setPerspective, nextSequence } from './state.js';
import { i18n } from './i18n.js';
import {
createRegisterPartEvent,
createAssembleEvent,
createReportDefectEvent,
createRevokeDefectEvent,
createDismantleEvent
} from './smrm/actions.js';
import { ENTITY_CLASSES, RELATION_TYPES, INTERNAL_PROPERTY_KEYS, inferEntityClass } from './smrm/definitions.js';
import * as bfoAdapter from './smim/bfo.js';
import * as isoAdapter from './smim/iso15926.js';
import * as naiveAdapter from './smim/naive.js';
import { applyQdikwInference } from './smim/inference-engine.js';
import { initVisNetwork, updateVisNetwork } from './renderers/vis-adapter.js';
import { updateMermaidNetwork } from './renderers/mermaid-adapter.js';
import { generateBomTrees } from './urmsm/bom-view.js';
import { reduceGraphForSMRM } from './urmsm/smrm-view.js';
import {
generateLpgNodeStorage,
generateLpgStorage,
generateLpgQuery,
evaluateLiveQuery
} from './sms/lpg-generator.js';
import { generateRdfStorage, generateRdfQuery } from './sms/rdf-generator.js';
import { generatePhysicalGraph } from './sms/sms-graph-generator.js';
import {
vanishingCrackScenario,
onTimeUpdate as dispatchVanishingCrackTimeline,
resetTimeline as resetVanishingCrackTimeline
} from './scenarios/scenario-vanishing-crack.js';
import {
sandboxTourScenario,
onSandboxTourTimeUpdate,
resetSandboxTourTimeline
} from './scenarios/scenario-sandbox-tour.js';
import {
qdikwScenario,
QDIKW_STAGE_TIMESTAMPS,
onQdikwTimeUpdate,
resetQdikwTimeline
} from './scenarios/scenario-qdikw.js';
const BomNode = {
name: 'BomNode',
props: {
node: { type: Object, required: true }
},
template: `
{{ node.id }}
{{ node.entityClass }}
β οΈ {{ def }}
`
};
const App = {
components: { BomNode },
setup() {
const visContainer = ref(null);
const mermaidContainer = ref(null);
const fileInput = ref(null);
// Business UI inputs
const bizRegType = ref('Part');
const bizRegId = ref('');
const bizInstSource = ref('');
const bizInstTarget = ref('');
const bizDefTarget = ref('');
const bizDefType = ref('');
const bizDefVal = ref('');
const bizRevDefect = ref('');
const bizDismPart = ref('');
const bizDeleteNodeId = ref('');
const bizStorePart = ref('');
const bizStoreTarget = ref('');
// Scenario Player State
const activeScenarioId = ref('QDIKW_PIPELINE');
const scenarioAudio = ref(null);
const isPlaying = ref(false);
const audioCurrentTime = ref(0);
const audioDuration = ref(0);
const showSubtitles = ref(true);
const currentSubtitleText = ref('');
let virtualTimerInterval = null;
const usingVirtualTimer = ref(false);
const activeScenarioObj = computed(() => {
if (activeScenarioId.value === 'SANDBOX_TOUR') return sandboxTourScenario;
if (activeScenarioId.value === 'QDIKW_PIPELINE') return qdikwScenario;
return vanishingCrackScenario;
});
const activeAudioSrc = computed(() => activeScenarioObj.value.audioSrc);
const layout = reactive({ leftWidth: 64, bomHeight: 24, storageHeight: 42 });
const t = computed(() => i18n[state.language] || i18n.en);
const baseNodes = computed(() => {
const nodeMap = new Map();
state.eventLog.forEach(event => {
if (event.valid_from !== undefined && event.valid_from > state.currentTime) return;
if (event.type === 'REGISTER_PART' && event.partId) {
const cls = inferEntityClass(event.partId, event.entityClass);
nodeMap.set(event.partId, { id: event.partId, label: event.partId, type: cls, entityClass: cls });
} else if (event.type === 'ASSEMBLE') {
if (event.parentId && !nodeMap.has(event.parentId)) {
const pCls = inferEntityClass(event.parentId);
nodeMap.set(event.parentId, { id: event.parentId, label: event.parentId, type: pCls, entityClass: pCls });
}
if (event.childId && !nodeMap.has(event.childId)) {
const cCls = inferEntityClass(event.childId);
nodeMap.set(event.childId, { id: event.childId, label: event.childId, type: cCls, entityClass: cCls });
}
}
});
return Array.from(nodeMap.values());
});
const availableNodes = computed(() => baseNodes.value.map(n => n.id));
const activeParts = computed(() => baseNodes.value.filter(n => n.entityClass !== 'Slot' && n.entityClass !== 'Warehouse').map(n => n.id));
const activeWarehouses = computed(() => baseNodes.value.filter(n => n.entityClass === 'Warehouse').map(n => n.id));
const bizInstallSources = computed(() => baseNodes.value.filter(n => n.entityClass === 'Part' || n.entityClass === 'Vehicle').map(n => n.id));
const bizInstallTargets = computed(() => baseNodes.value.filter(n => n.entityClass === 'Vehicle' || n.entityClass === 'Part' || n.entityClass === 'Slot' || n.entityClass === 'Warehouse').map(n => n.id));
function dispatch(event) {
if (!event) return;
state.eventLog.push(event);
}
const semanticGraph = computed(() => {
const opts = { ontology: state.ontology, showHistory: state.showHistory };
let rawGraph;
if (state.ontology === 'BFO') {
rawGraph = bfoAdapter.generateSemanticGraph(state.eventLog, baseNodes.value, state.currentTime, opts);
} else if (state.ontology === 'ISO15926') {
rawGraph = isoAdapter.generateSemanticGraph(state.eventLog, baseNodes.value, state.currentTime, opts);
} else {
rawGraph = naiveAdapter.generateSemanticGraph(state.eventLog, baseNodes.value, state.currentTime, opts);
}
return applyQdikwInference(rawGraph, state);
});
const smrmReducedGraph = computed(() => reduceGraphForSMRM(semanticGraph.value));
const installedComponents = computed(() => {
const comps = new Set();
smrmReducedGraph.value.edges.forEach(e => {
if (e.properties?.isCurrent === false) return;
if (e.label === 'has_part') comps.add(e.to);
else if (e.label === 'mounted_in' || e.label === 'stored_at') comps.add(e.from);
});
return Array.from(comps).filter(id => !id.startsWith('Slot_'));
});
const activeDefectsList = computed(() => {
const activeMap = new Map();
state.eventLog.forEach((e, index) => {
if (e.valid_from !== undefined && e.valid_from > state.currentTime) return;
if (e.type === 'REPORT_DEFECT') {
if (e.valid_to !== null && e.valid_to !== undefined && e.valid_to <= state.currentTime) return;
const key = `${e.targetId}::${e.defectName}`;
activeMap.set(key, {
id: e.id || `def_${index}_${e.targetId}_${e.defectName}`,
logIndex: index,
targetId: e.targetId,
defectName: e.defectName,
defectValue: e.defectValue,
label: `${e.targetId} β’ ${e.defectName}=${e.defectValue}`
});
} else if (e.type === 'REVOKE_DEFECT') {
const key = `${e.targetId}::${e.defectName}`;
activeMap.delete(key);
}
});
return Array.from(activeMap.values());
});
watch(bizInstallSources, (sources) => { if (!sources.includes(bizInstSource.value)) bizInstSource.value = sources[0] || ''; }, { immediate: true });
watch(bizInstallTargets, (targets) => { if (!targets.includes(bizInstTarget.value)) bizInstTarget.value = targets[0] || ''; }, { immediate: true });
watch(activeParts, (parts) => { if (!parts.includes(bizStorePart.value)) bizStorePart.value = parts[0] || ''; }, { immediate: true });
watch(activeWarehouses, (warehouses) => { if (!warehouses.includes(bizStoreTarget.value)) bizStoreTarget.value = warehouses[0] || ''; }, { immediate: true });
watch(installedComponents, (comps) => { if (!comps.includes(bizDismPart.value)) bizDismPart.value = comps[0] || ''; }, { immediate: true });
watch(activeDefectsList, (defects) => { if (!defects.some(d => d.id === bizRevDefect.value)) bizRevDefect.value = defects[0]?.id || ''; }, { immediate: true });
watch(availableNodes, (nodes) => {
if (!nodes.includes(bizDefTarget.value)) bizDefTarget.value = nodes[0] || '';
if (!nodes.includes(bizDeleteNodeId.value)) bizDeleteNodeId.value = nodes[0] || '';
}, { immediate: true });
const bomTrees = computed(() => generateBomTrees(semanticGraph.value));
const lpgNodeRecords = computed(() => state.storage === 'LPG' ? generateLpgNodeStorage(semanticGraph.value) : []);
const lpgRecords = computed(() => state.storage === 'LPG' ? generateLpgStorage(semanticGraph.value) : []);
const rdfTtl = computed(() => state.storage === 'RDF' ? generateRdfStorage(semanticGraph.value) : '');
function formatProperties(props) {
if (!props) return '';
const entries = Object.entries(props).filter(
([key, val]) => !INTERNAL_PROPERTY_KEYS.includes(key) || key === 'validFrom' || key === 'validTo' || key === 'atTime'
? (key !== 'isCurrent' && val !== undefined) : false
);
if (entries.length === 0) return '';
return entries.map(([k, v]) => {
const valStr = v === null ? 'null' : (typeof v === 'number' ? `T${v}` : `'${v}'`);
return `${k}: ${valStr}`;
}).join(', ');
}
const queryCode = computed(() => {
if (state.storage === 'LPG') return generateLpgQuery(semanticGraph.value, state.currentTime, state.ontology);
return generateRdfQuery(semanticGraph.value, state.currentTime, state.ontology);
});
const liveQueryResult = computed(() => evaluateLiveQuery(semanticGraph.value, state.currentTime, state.ontology));
const activeGraph = computed(() => {
if (state.perspective === 'SMS_LPG') return generatePhysicalGraph(semanticGraph.value, 'LPG');
if (state.perspective === 'SMS_RDF') return generatePhysicalGraph(semanticGraph.value, 'RDF');
if (state.perspective === 'SMRM') return smrmReducedGraph.value;
return semanticGraph.value;
});
const graphCaption = computed(() => {
let ontoText = 'BFO (3D)';
if (state.ontology === 'ISO15926') ontoText = 'ISO 15926 (4D)';
if (state.ontology === 'NAIVE_NODE') ontoText = t.value.ontoNaiveNode;
if (state.ontology === 'NAIVE_EDGE') ontoText = t.value.ontoNaiveEdge;
let viewText = t.value.viewLogicalCaption;
if (state.perspective === 'SMRM') viewText = t.value.viewSmrmCaption;
if (state.perspective === 'SMS_LPG') viewText = t.value.viewPhysicalLpgCaption;
if (state.perspective === 'SMS_RDF') viewText = t.value.viewPhysicalRdfCaption;
return `${ontoText} | ${viewText}`;
});
const graphLegendItems = computed(() => {
const nodes = []; const edges = []; const dict = t.value.legend;
if (state.perspective === 'SMRM' || state.ontology === 'NAIVE_NODE' || state.ontology === 'NAIVE_EDGE') {
nodes.push({ id: 'naiveVehicle', label: dict.naiveVehicle, swatch: 'swatch-box-vehicle' });
if (state.perspective !== 'SMRM') nodes.push({ id: 'naiveSlot', label: dict.naiveSlot, swatch: 'swatch-box-slot' });
nodes.push({ id: 'naivePart', label: dict.naivePart, swatch: 'swatch-box-part' });
nodes.push({ id: 'naiveDefect', label: dict.naiveDefect, swatch: 'swatch-diamond-defect' });
nodes.push({ id: 'naiveWarehouse', label: dict.naiveWarehouse, swatch: 'swatch-box-warehouse' });
if (state.perspective === 'SMS_RDF') {
nodes.push({ id: 'rdfStmt', label: dict.rdfStmt, swatch: 'swatch-diamond' });
nodes.push({ id: 'rdfLit', label: dict.rdfLit, swatch: 'swatch-literal' });
edges.push({ id: 'rdfPred', label: dict.rdfPred, swatch: 'swatch-line-past' });
} else {
if (state.perspective !== 'SMRM') edges.push({ id: 'relHasSlot', label: dict.relHasSlot, swatch: 'swatch-line-slot' });
edges.push({ id: 'relMountedIn', label: dict.relMountedIn, swatch: 'swatch-line-mount' });
edges.push({ id: 'relStoredAt', label: dict.relStoredAt, swatch: 'swatch-line-store' });
edges.push({ id: 'relHasDefect', label: dict.relHasDefect, swatch: 'swatch-line-defect' });
}
} else if (state.ontology === 'BFO') {
nodes.push({ id: 'bfoSys', label: dict.bfoSys, swatch: 'swatch-box-sys' });
nodes.push({ id: 'bfoElem', label: dict.bfoElem, swatch: 'swatch-box-elem' });
nodes.push({ id: 'bfoSlot', label: dict.bfoSlot || 'BFO:Site (Functional Slot)', swatch: 'swatch-box-slot' });
nodes.push({ id: 'bfoQuality', label: dict.bfoQuality, swatch: 'swatch-pill-quality' });
if (state.perspective === 'SMS_RDF') {
nodes.push({ id: 'rdfStmt', label: dict.rdfStmt, swatch: 'swatch-hex-statement' });
nodes.push({ id: 'rdfLit', label: dict.rdfLit, swatch: 'swatch-literal' });
edges.push({ id: 'rdfPred', label: dict.rdfPred, swatch: 'swatch-line-past' });
} else {
edges.push({ id: 'bfoActive', label: dict.bfoActiveEdge, swatch: 'swatch-line-active' });
edges.push({ id: 'bfoInheres', label: dict.bfoInheres, swatch: 'swatch-line-transition' });
if (state.showHistory) edges.push({ id: 'bfoPast', label: dict.bfoPastEdge, swatch: 'swatch-line-past' });
}
} else {
nodes.push({ id: 'iso4d', label: dict.iso4d, swatch: 'swatch-box-4d' });
nodes.push({ id: 'isoState', label: dict.isoState, swatch: 'swatch-box-state' });
if (state.showHistory) {
nodes.push({ id: 'isoStatePast', label: dict.isoStatePast || 'Historical State Slice', swatch: 'swatch-box-state-past' });
}
nodes.push({ id: 'isoDefect', label: dict.isoDefect, swatch: 'swatch-diamond-defect' });
if (state.perspective === 'SMS_RDF') nodes.push({ id: 'rdfLit', label: dict.rdfLit, swatch: 'swatch-literal' });
edges.push({ id: 'isoPart', label: dict.isoPartRel, swatch: 'swatch-line-active' });
edges.push({ id: 'isoSlice', label: dict.isoSliceRel, swatch: 'swatch-line-past' });
edges.push({ id: 'isoDefectRel', label: dict.isoDefectRel, swatch: 'swatch-line-defect' });
if (state.perspective === 'SMS_LPG') edges.push({ id: 'isoTrans', label: dict.isoTransitionRel, swatch: 'swatch-line-transition' });
}
const gNodes = activeGraph.value.nodes || [];
const gEdges = activeGraph.value.edges || [];
if (gNodes.some(n => n.type === 'InferredStress')) {
nodes.push({ id: 'leg_inf_stress', label: dict.infStress, swatch: 'swatch-inf-stress' });
}
if (gNodes.some(n => n.type === 'InferredHazard')) {
nodes.push({ id: 'leg_inf_hazard', label: dict.infHazard, swatch: 'swatch-inf-hazard' });
}
if (gNodes.some(n => n.type === 'WisdomDirective')) {
nodes.push({ id: 'leg_wisdom_dir', label: dict.wisdomDir, swatch: 'swatch-wisdom-dir' });
}
if (gNodes.some(n => n.type === 'BlindSpot')) {
nodes.push({ id: 'leg_blind_spot', label: dict.blindSpot, swatch: 'swatch-blind-spot' });
}
if (gEdges.some(e => e.type === 'inference')) {
edges.push({ id: 'leg_inf_edge', label: dict.infEdge, swatch: 'swatch-line-inference' });
}
return { nodes, edges };
});
watch([activeGraph, () => state.graphEngine], () => { renderGraph(); }, { deep: true });
function renderGraph() {
if (state.currentView === 'MENU') return;
const graphToRender = activeGraph.value;
if (state.graphEngine === 'VIS') {
const isComplex = state.ontology === 'ISO15926' || state.perspective.startsWith('SMS_');
updateVisNetwork(graphToRender, isComplex);
} else if (mermaidContainer.value) {
updateMermaidNetwork(graphToRender, mermaidContainer.value);
}
}
function seedInitialSandboxData() {
state.eventLog.splice(0, state.eventLog.length);
state.currentTime = 0;
state.edgeSequence = 1;
state.qdikw.active = false;
state.figureOverlay.visible = false;
dispatch(createRegisterPartEvent('System_A', 0, 'Vehicle', nextSequence()));
dispatch(createRegisterPartEvent('SubAssembly_1', 0, 'Part', nextSequence()));
dispatch(createAssembleEvent('System_A', 'SubAssembly_1', 'mounted_in', 0, nextSequence()));
dispatch(createRegisterPartEvent('Part_A', 0, 'Part', nextSequence()));
dispatch(createAssembleEvent('System_A', 'Part_A', 'mounted_in', 0, nextSequence()));
dispatch(createRegisterPartEvent('Sensor_X', 0, 'Part', nextSequence()));
dispatch(createAssembleEvent('SubAssembly_1', 'Sensor_X', 'mounted_in', 0, nextSequence()));
dispatch(createRegisterPartEvent('Warehouse_1', 0, 'Warehouse', nextSequence()));
}
function resetActiveScenarioState() {
state.userRole = 'UNI_ENGINEER';
state.eventLog.splice(0, state.eventLog.length);
state.currentTime = 0;
state.ontology = 'NAIVE_NODE';
state.qdikw.active = false;
state.figureOverlay.visible = false;
if (activeScenarioId.value === 'SANDBOX_TOUR') {
resetSandboxTourTimeline(0);
setPerspective('SMRM');
state.graphEngine = 'MERMAID';
audioDuration.value = sandboxTourScenario.fallbackDuration;
onSandboxTourTimeUpdate(0, state, renderGraph);
} else if (activeScenarioId.value === 'QDIKW_PIPELINE') {
resetQdikwTimeline(0);
setPerspective('SMRM');
state.graphEngine = 'MERMAID';
audioDuration.value = qdikwScenario.fallbackDuration;
onQdikwTimeUpdate(0, state, renderGraph, null);
} else {
resetVanishingCrackTimeline(0);
setPerspective('SMIM');
state.graphEngine = 'MERMAID';
audioDuration.value = vanishingCrackScenario.fallbackDuration || 148;
}
}
function launchScenario(scenarioId) {
activeScenarioId.value = scenarioId;
stopAudio(false);
if (state.currentView === 'SCENARIO') {
resetActiveScenarioState();
nextTick(() => renderGraph());
} else {
state.currentView = 'SCENARIO';
}
}
function jumpToQdikwStage(stageKey) {
const targetTime = QDIKW_STAGE_TIMESTAMPS[stageKey];
if (targetTime === undefined) return;
if (scenarioAudio.value && !usingVirtualTimer.value) {
scenarioAudio.value.currentTime = targetTime;
}
audioCurrentTime.value = targetTime;
resetActiveScenarioState();
processTimelineAt(targetTime);
}
function toggleSlotRegime() {
if (!state.qdikw.slotMetadata.SLOT_FRONT_SUSPENSION) {
state.qdikw.slotMetadata.SLOT_FRONT_SUSPENSION = { loadRegime: 'DYNAMIC_CYCLIC', safetyFunction: 'STEERING_CONTROL' };
} else {
const cur = state.qdikw.slotMetadata.SLOT_FRONT_SUSPENSION.loadRegime;
state.qdikw.slotMetadata.SLOT_FRONT_SUSPENSION.loadRegime = cur === 'DYNAMIC_CYCLIC' ? 'STATIC_LOAD' : 'DYNAMIC_CYCLIC';
}
renderGraph();
}
watch(() => state.currentView, async (newView) => {
if (newView !== 'SCENARIO') stopAudio(false);
if (newView === 'SCENARIO') {
resetActiveScenarioState();
} else if (newView === 'SANDBOX' && state.eventLog.length === 0) {
seedInitialSandboxData();
}
if (newView !== 'MENU') {
await nextTick();
if (visContainer.value) initVisNetwork(visContainer.value);
renderGraph();
}
});
onMounted(() => {
seedInitialSandboxData();
if (state.currentView !== 'MENU' && visContainer.value) {
initVisNetwork(visContainer.value);
renderGraph();
}
});
function clearVirtualTimer() {
if (virtualTimerInterval) { clearInterval(virtualTimerInterval); virtualTimerInterval = null; }
}
function startVirtualTimer() {
clearVirtualTimer();
usingVirtualTimer.value = true;
isPlaying.value = true;
if (!audioDuration.value) audioDuration.value = activeScenarioObj.value.fallbackDuration || 108;
virtualTimerInterval = setInterval(() => {
if (audioCurrentTime.value < audioDuration.value) {
audioCurrentTime.value = Math.min(audioDuration.value, audioCurrentTime.value + 0.25);
processTimelineAt(audioCurrentTime.value);
} else {
clearVirtualTimer();
onAudioEnded();
}
}, 250);
}
function goHome() {
stopAudio(false);
state.currentView = 'MENU';
state.qdikw.active = false;
state.figureOverlay.visible = false;
state.eventLog.splice(0, state.eventLog.length);
state.currentTime = 0;
state.edgeSequence = 1;
}
function playAudio() {
if (usingVirtualTimer.value) { startVirtualTimer(); return; }
if (scenarioAudio.value) {
scenarioAudio.value.play().catch(() => { startVirtualTimer(); });
} else {
startVirtualTimer();
}
}
function pauseAudio() {
clearVirtualTimer();
isPlaying.value = false;
if (scenarioAudio.value && !usingVirtualTimer.value) scenarioAudio.value.pause();
}
function togglePlayAudio() {
if (isPlaying.value) pauseAudio(); else playAudio();
}
function stopAudio(resetScenarioGraph = true) {
clearVirtualTimer();
usingVirtualTimer.value = false;
if (scenarioAudio.value) {
scenarioAudio.value.pause();
scenarioAudio.value.currentTime = 0;
}
isPlaying.value = false;
audioCurrentTime.value = 0;
currentSubtitleText.value = '';
if (activeScenarioId.value === 'SANDBOX_TOUR') resetSandboxTourTimeline(0);
else if (activeScenarioId.value === 'QDIKW_PIPELINE') resetQdikwTimeline(0);
else resetVanishingCrackTimeline(0);
if (resetScenarioGraph) {
resetActiveScenarioState();
nextTick(() => { renderGraph(); });
}
}
function processTimelineAt(currentTime) {
const scenario = activeScenarioObj.value;
const activeSubtitle = scenario.subtitles.find(sub => currentTime >= sub.start && currentTime <= sub.end);
currentSubtitleText.value = activeSubtitle ? activeSubtitle.text : '';
const uiRefs = {
bizRegType, bizRegId, bizInstSource, bizInstTarget, bizDefTarget, bizDefType, bizDefVal
};
if (activeScenarioId.value === 'SANDBOX_TOUR') {
onSandboxTourTimeUpdate(currentTime, state, renderGraph);
} else if (activeScenarioId.value === 'QDIKW_PIPELINE') {
onQdikwTimeUpdate(currentTime, state, renderGraph, uiRefs);
} else {
dispatchVanishingCrackTimeline(currentTime, state, renderGraph);
}
}
function onTimeUpdate() {
if (usingVirtualTimer.value) return;
if (scenarioAudio.value) audioCurrentTime.value = scenarioAudio.value.currentTime;
processTimelineAt(audioCurrentTime.value);
}
function onLoadedMetadata() {
if (scenarioAudio.value && scenarioAudio.value.duration) {
usingVirtualTimer.value = false;
audioDuration.value = scenarioAudio.value.duration;
}
}
function onAudioEnded() {
clearVirtualTimer();
isPlaying.value = false;
currentSubtitleText.value = '';
}
function onSeek(event) {
const targetTime = parseFloat(event.target.value);
if (scenarioAudio.value && !usingVirtualTimer.value) scenarioAudio.value.currentTime = targetTime;
audioCurrentTime.value = targetTime;
resetActiveScenarioState();
processTimelineAt(targetTime);
}
function formatTime(seconds) {
if (!seconds || isNaN(seconds) || seconds < 0) return '00:00';
const mins = Math.floor(seconds / 60);
const secs = Math.floor(seconds % 60);
return `${mins.toString().padStart(2, '0')}:${secs.toString().padStart(2, '0')}`;
}
function handleBizRegister() {
if (!bizRegId.value) return;
const event = createRegisterPartEvent(bizRegId.value, state.currentTime, bizRegType.value, nextSequence());
if (event) { dispatch(event); bizRegId.value = ''; }
}
function handleBizInstall() {
if (!bizInstSource.value || !bizInstTarget.value) return;
if (bizInstSource.value === bizInstTarget.value) return;
if (installedComponents.value.includes(bizInstSource.value)) {
dispatch(createDismantleEvent(bizInstSource.value, state.currentTime, nextSequence()));
}
const event = createAssembleEvent(bizInstTarget.value, bizInstSource.value, 'mounted_in', state.currentTime, nextSequence());
if (event) dispatch(event);
}
function handleBizReportDefect() {
if (!bizDefTarget.value || !bizDefType.value) return;
const event = createReportDefectEvent(bizDefTarget.value, bizDefType.value, bizDefVal.value || 'present', state.currentTime, nextSequence());
if (event) { dispatch(event); bizDefType.value = ''; bizDefVal.value = ''; }
}
function handleBizRevokeDefect() {
if (!bizRevDefect.value) return;
const item = activeDefectsList.value.find(d => d.id === bizRevDefect.value);
if (item) {
const event = createRevokeDefectEvent(item.targetId, item.defectName, state.currentTime, item.id, nextSequence());
if (event) { dispatch(event); bizRevDefect.value = ''; }
}
}
function handleBizDeleteDefect() {
if (!bizRevDefect.value) return;
const item = activeDefectsList.value.find(d => d.id === bizRevDefect.value);
if (item) {
for (let i = state.eventLog.length - 1; i >= 0; i--) {
const ev = state.eventLog[i];
if ((ev.type === 'REPORT_DEFECT' || ev.type === 'REVOKE_DEFECT') &&
ev.targetId === item.targetId &&
ev.defectName === item.defectName) {
state.eventLog.splice(i, 1);
}
}
bizRevDefect.value = '';
renderGraph();
}
}
function handleBizDismantle() {
if (!bizDismPart.value) return;
const event = createDismantleEvent(bizDismPart.value, state.currentTime, nextSequence());
if (event) { dispatch(event); bizDismPart.value = ''; }
}
function handleBizDeleteNode() {
const target = bizDeleteNodeId.value;
if (!target) return;
for (let i = state.eventLog.length - 1; i >= 0; i--) {
const ev = state.eventLog[i];
if (ev.partId === target || ev.parentId === target || ev.childId === target || ev.targetId === target) {
state.eventLog.splice(i, 1);
}
}
renderGraph();
}
function handleBizStore() {
if (!bizStorePart.value || !bizStoreTarget.value) return;
if (installedComponents.value.includes(bizStorePart.value)) {
dispatch(createDismantleEvent(bizStorePart.value, state.currentTime, nextSequence()));
}
const event = createAssembleEvent(bizStoreTarget.value, bizStorePart.value, 'stored_at', state.currentTime, nextSequence());
if (event) dispatch(event);
}
function startResizeH(e) {
const resizerEl = e.currentTarget; resizerEl?.classList.add('dragging');
const startX = e.clientX; const startWidth = layout.leftWidth;
const containerWidth = document.querySelector('.main-container').getBoundingClientRect().width;
const onMouseMove = (evt) => {
const newWidth = startWidth + ((evt.clientX - startX) / containerWidth) * 100;
if (newWidth > 25 && newWidth < 78) layout.leftWidth = newWidth;
};
const onMouseUp = () => {
resizerEl?.classList.remove('dragging');
document.removeEventListener('mousemove', onMouseMove); document.removeEventListener('mouseup', onMouseUp);
document.body.classList.remove('no-select');
};
document.addEventListener('mousemove', onMouseMove); document.addEventListener('mouseup', onMouseUp);
document.body.classList.add('no-select');
}
function startResizeV(e, property, containerSelector) {
const resizerEl = e.currentTarget; resizerEl?.classList.add('dragging');
const startY = e.clientY; const startHeight = layout[property];
const containerHeight = document.querySelector(containerSelector).getBoundingClientRect().height;
const onMouseMove = (evt) => {
const newHeight = startHeight + ((evt.clientY - startY) / containerHeight) * 100;
if (newHeight > 12 && newHeight < 75) layout[property] = newHeight;
};
const onMouseUp = () => {
resizerEl?.classList.remove('dragging');
document.removeEventListener('mousemove', onMouseMove); document.removeEventListener('mouseup', onMouseUp);
document.body.classList.remove('no-select');
};
document.addEventListener('mousemove', onMouseMove); document.addEventListener('mouseup', onMouseUp);
document.body.classList.add('no-select');
}
function saveJson() {
const exportData = {
version: '2.0', savedAt: new Date().toISOString(), userRole: state.userRole, ontology: state.ontology,
perspective: state.perspective, storage: state.storage, graphEngine: state.graphEngine, language: state.language,
showHistory: state.showHistory, currentTime: state.currentTime, edgeSequence: state.edgeSequence,
eventLog: JSON.parse(JSON.stringify(state.eventLog))
};
const blob = new Blob([JSON.stringify(exportData, null, 2)], { type: 'application/json' });
const url = URL.createObjectURL(blob); const a = document.createElement('a'); a.href = url;
a.download = `workbench-state-t${state.currentTime}.json`;
document.body.appendChild(a); a.click(); document.body.removeChild(a); URL.revokeObjectURL(url);
}
function triggerLoadJson() { if (fileInput.value) { fileInput.value.value = ''; fileInput.value.click(); } }
function onFileLoaded(e) {
const file = e.target.files?.[0]; if (!file) return;
const reader = new FileReader();
reader.onload = (event) => {
try {
const parsed = JSON.parse(event.target.result);
if (Array.isArray(parsed)) loadState({ eventLog: parsed });
else if (parsed && typeof parsed === 'object') loadState(parsed);
nextTick(() => { renderGraph(); });
} catch (err) { console.error('Failed to parse state JSON:', err); alert('Invalid JSON file format.'); }
};
reader.readAsText(file);
}
return {
state, t, layout, advanceTime, setPerspective, dispatch, semanticGraph,
fileInput, saveJson, triggerLoadJson, onFileLoaded, renderGraph,
entityClasses: ENTITY_CLASSES, relationTypes: RELATION_TYPES, availableNodes, goHome,
launchScenario, jumpToQdikwStage, toggleSlotRegime, activeScenarioId, activeAudioSrc,
activeParts, activeWarehouses, bizRegType, bizRegId, handleBizRegister,
bizInstSource, bizInstTarget, handleBizInstall, bizInstallSources, bizInstallTargets,
bizDefTarget, bizDefType, bizDefVal, handleBizReportDefect,
bizRevDefect, handleBizRevokeDefect, handleBizDeleteDefect, activeDefectsList,
bizDismPart, handleBizDismantle, installedComponents,
bizDeleteNodeId, handleBizDeleteNode,
bizStorePart, bizStoreTarget, handleBizStore,
bomTrees, lpgNodeRecords, lpgRecords, rdfTtl, formatProperties, queryCode, liveQueryResult,
visContainer, mermaidContainer, graphCaption, graphLegendItems,
startResizeH, startResizeV, scenarioAudio, isPlaying, audioCurrentTime, audioDuration,
showSubtitles, currentSubtitleText, playAudio, pauseAudio, togglePlayAudio, stopAudio,
onTimeUpdate, onLoadedMetadata, onAudioEnded, onSeek, formatTime
};
},
template: `
{{ t.qdikwStepQ }}
{{ t.qdikwStepD }}
{{ t.qdikwStepI }}
{{ t.qdikwStepK }}
{{ t.qdikwStepW }}
{{ state.qdikw.activeRole }}
{{ state.qdikw.roleBadge }}
IEC 63339 / SMIM
{{ t.ontoTitle }}
IEC 63339
ποΈ {{ t.viewTitle }}
π¨ {{ t.engineTitle }}
T = {{ state.currentTime }}
β
{{ currentSubtitleText }}
{{ formatTime(audioCurrentTime) }} / {{ formatTime(audioDuration) }}
`
};
createApp(App).mount('#app');