import type { Ablauf, AblaufSource } from './ablaeufe.dto'; import type { FlowUnit, GraphProposal, ProposedStep } from './plan.types'; export function proposeGraph(flow: Ablauf, units: FlowUnit[], sources: AblaufSource[]): GraphProposal { const g = flow.graph!, warnings: string[] = [], catalog = new Map(units.map(u => [u.id, u])); let blocked = false; const nodeMap = new Map(g.nodes.map(n => [n.unitId, n])); const storage = (id: string) => catalog.get(id)?.settings.kind === 'storage'; const effective = g.edges.filter(e => e.active && !(storage(e.from) && nodeMap.get(e.from)?.storageUse === 'fill') && !(storage(e.to) && nodeMap.get(e.to)?.storageUse === 'empty')); const roots = g.nodes.filter(n => storage(n.unitId) && n.storageUse !== 'fill' || !g.edges.some(e => e.to === n.unitId) && (!g.edges.some(e => e.from === n.unitId) || effective.some(e => e.from === n.unitId))).map(n => n.unitId); const active = new Set(roots), queue = [...roots]; for (let i = 0; i < queue.length; i++) for (const e of effective.filter(e => e.from === queue[i])) if (!active.has(e.to)) { active.add(e.to); queue.push(e.to); } const edges = effective.filter(e => active.has(e.from) && active.has(e.to)); if (!g.nodes.length) warnings.push('Noch keine Units im Verbindungsplan.'); for (const n of g.nodes) { const u = catalog.get(n.unitId); if (!u) { warnings.push(`Unit ${n.unitId} fehlt in der ausgewählten Anlage.`); blocked = true; continue; } const incoming = g.edges.filter(e => e.to === n.unitId), outgoing = g.edges.filter(e => e.from === n.unitId); if (!u.configured) warnings.push(`${u.name}: Zu-/Abgänge sind an der Unit noch nicht gepflegt.`); if (u.settings.inputCount !== null && incoming.length !== u.settings.inputCount) warnings.push(`${u.name}: ${u.settings.inputCount} Zuführungen vorgesehen, ${incoming.length} verbunden.`); if (u.settings.outputCount !== null && new Set(outgoing.map(e => e.outletId)).size !== u.settings.outputCount) warnings.push(`${u.name}: ${u.settings.outputCount} Abgänge vorgesehen, ${new Set(outgoing.map(e => e.outletId)).size} verbunden.`); for (const e of outgoing) if (e.outletId && !u.settings.outlets.some(p => p.id === e.outletId)) warnings.push(`${u.name}: Ein verwendeter Abgang wurde entfernt oder ist unbekannt.`); if (u.settings.outputCount !== null && outgoing.some(e => e.outletId === null)) warnings.push(`${u.name}: Verbindung einem benannten Abgang zuordnen.`); if (outgoing.some(e => e.outletId && outgoing.filter(other => other.outletId === e.outletId).length > 1)) warnings.push(`${u.name}: Ein Abgang ist mehrfach verbunden.`); if (!active.has(n.unitId)) continue; const ain = edges.filter(e => e.to === n.unitId), aout = edges.filter(e => e.from === n.unitId); if (u.settings.inputMode === 'selection' && ain.length > 1) { warnings.push(`${u.name}: Mehrere Zuführungen aktiv, obwohl eine Auswahl vorgesehen ist.`); blocked = true; } if (u.settings.outputMode === 'selection' && aout.length > 1) { warnings.push(`${u.name}: Mehrere Abgänge aktiv, obwohl eine Auswahl vorgesehen ist.`); blocked = true; } if (u.settings.inputMode === 'all' && incoming.some(e => !e.active) && !storage(n.unitId)) { warnings.push(`${u.name}: Nicht alle gleichzeitig benötigten Zuführungen sind aktiv.`); blocked = true; } if (u.settings.outputMode === 'all' && outgoing.some(e => !e.active) && !storage(n.unitId)) { warnings.push(`${u.name}: Nicht alle gleichzeitig benötigten Abgänge sind aktiv.`); blocked = true; } if (storage(n.unitId)) { if (n.storageUse === 'both') warnings.push(`${u.name}: Befüllung und Entnahme sind unabhängig geplant; Voll-/Leermeldungen getrennt berücksichtigen.`); } else if (!sources.some(s => s.unitId === n.unitId)) warnings.push(`${u.name}: Kein separat steuerbares, gespeichertes Aggregat vorhanden (eventuell nur Slaves).`); } // A store's receiving side does not require the extraction drive to run. const dependencies = edges.filter(e => !storage(e.to)); const remaining = new Set(active), layers: string[][] = []; while (remaining.size) { const sinks = [...remaining].filter(id => !dependencies.some(e => e.from === id && remaining.has(e.to))); if (!sinks.length) break; layers.push(sinks); sinks.forEach(id => remaining.delete(id)); } const cycle = remaining.size > 0 || g.nodes.some(n => !active.has(n.unitId)) && hasCycle(g.nodes.map(n => n.unitId), effective.filter(e => !storage(e.to))); if (cycle) warnings.push('Zyklische Einschalt-Abhängigkeit ohne Speichertrennung. Verbindungen prüfen; kein Schrittvorschlag möglich.'); const steps: ProposedStep[] = []; if (!cycle) for (const layer of layers) { const drives = sources.filter(s => s.unitId && layer.includes(s.unitId) && (!storage(s.unitId) || nodeMap.get(s.unitId)?.storageUse !== 'fill')); if (!drives.length) continue; const directions: ProposedStep['directions'] = {}; for (const d of drives) { const u = catalog.get(d.unitId!)!; const dirs = [...new Set(edges.filter(e => e.from === d.unitId).map(e => u.settings.outlets.find(p => p.id === e.outletId)?.direction).filter((v): v is 'left' | 'right' => v === 'left' || v === 'right'))]; if (dirs.length > 1) { warnings.push(`${u.name}: Gleichzeitig aktive Abgänge erfordern unterschiedliche Laufrichtungen.`); blocked = true; } const desired = dirs.length === 1 ? dirs[0]! : 'forward'; if (desired !== 'forward' && !d.reversible) { warnings.push(`${d.name}: Abgang verlangt eine Richtung, aber Linkslauf ist an der Unit-Komponente nicht eingerichtet.`); blocked = true; } directions[d.id] = desired !== 'forward' && d.reversible ? desired : 'forward'; } steps.push({ id: `graph_${steps.length + 1}`, aggregateIds: drives.map(d => d.id), directions, wait: { kind: 'running', seconds: null } }); } blocked ||= cycle; return { warnings: [...new Set(warnings)], aggregateIds: steps.flatMap(s => s.aggregateIds), steps: blocked ? [] : steps, cycle, blocked }; } function hasCycle(ids: string[], edges: { from: string; to: string }[]) { const remaining = new Set(ids); while (remaining.size) { const leaves = [...remaining].filter(id => !edges.some(e => e.from === id && remaining.has(e.to))); if (!leaves.length) return true; leaves.forEach(id => remaining.delete(id)); } return false; }