// All Scenarios Critical Path Comparison Query // Finds the Critical Path (longest duration path) for each scenario // and returns a comparison table. // Optional: Parameter for a list of scenario IDs to compare // Example: :scenario_ids => ['linear-process', 'parallel-process', 'what-if-parallel'] // 1. Get all Scenario IDs (or filter by provided list) MATCH (s:Scenario) WHERE ($scenario_ids IS NULL OR s.id IN $scenario_ids) WITH COLLECT(s.id) AS scenario_ids // 2. For each scenario, find its Critical Path duration and cost UNWIND scenario_ids AS current_scenario_id // Find start nodes for the current scenario MATCH (start_node:Job) OPTIONAL MATCH (start_node)<-[r_in:PRECEDES {scenario: current_scenario_id}]-() WITH current_scenario_id, start_node, r_in WHERE r_in IS NULL // Find end nodes for the current scenario MATCH (end_node:Job) OPTIONAL MATCH (end_node)-[r_out:PRECEDES {scenario: current_scenario_id}]->() WITH current_scenario_id, start_node, end_node, r_out WHERE r_out IS NULL // Match all valid paths for the current scenario MATCH path = (start_node)-[rels:PRECEDES*]->(end_node) WHERE ALL(r IN rels WHERE r.scenario = current_scenario_id) // For each job in the path, calculate its effective duration and cost WITH current_scenario_id, path, nodes(path) AS jobs_on_path UNWIND jobs_on_path AS job OPTIONAL MATCH (:Scenario {id: current_scenario_id})-[m:MODIFIES]->(job) WITH current_scenario_id, path, COLLECT({ name: job.name, bd: job.base_duration, ed: COALESCE(m.new_duration, job.base_duration), ec: COALESCE(m.new_cost, job.base_cost) }) AS job_data // Calculate totals for each path WITH current_scenario_id, path, job_data, REDUCE(s = 0, x IN job_data | s + x.ed) AS total_duration, REDUCE(s = 0, x IN job_data | s + x.ec) AS total_cost // Find the Critical Path (longest duration) for the current scenario ORDER BY total_duration DESC LIMIT 1 // Return the critical path details for the current scenario RETURN current_scenario_id AS scenario_id, [j IN job_data | j.name] AS critical_path_jobs, [j IN job_data | j.bd] AS critical_path_base_durations, [j IN job_data | j.ed] AS critical_path_effective_durations, total_duration AS critical_path_total_duration, total_cost AS critical_path_total_cost // 3. Order the final comparison table by critical path duration ORDER BY critical_path_total_duration DESC;