001package gudusoft.gsqlparser.analyzer.v2;
002
003import gudusoft.gsqlparser.analyzer.v2.callgraph.CallGraph;
004import gudusoft.gsqlparser.analyzer.v2.impact.ImpactEngine;
005
006import java.util.Collections;
007
008/**
009 * Complete analysis result from AnalyzerV2.
010 * <p>
011 * Contains the IR program and call graph, and provides query APIs for impact analysis.
012 * Delegates impact computation to {@link ImpactEngine} for real BFS paths and distances.
013 */
014public class AnalysisResult {
015
016    private final IRProgram irProgram;
017    private final CallGraph callGraph;
018    private AnalyzerV2Config config;
019
020    public AnalysisResult(IRProgram irProgram) {
021        this(irProgram, null);
022    }
023
024    public AnalysisResult(IRProgram irProgram, CallGraph callGraph) {
025        this.irProgram = irProgram;
026        this.callGraph = callGraph;
027    }
028
029    public void setConfig(AnalyzerV2Config config) { this.config = config; }
030
031    public IRProgram getIrProgram() { return irProgram; }
032    public CallGraph getCallGraph() { return callGraph; }
033
034    /**
035     * Query routine impact: find all routines and tables transitively
036     * affected by a change to the given routine.
037     * Uses real BFS distances and path reconstruction.
038     */
039    public ImpactResult getRoutineImpact(String routineId, int maxDepth) {
040        return ImpactEngine.getRoutineImpact(callGraph, routineId, maxDepth);
041    }
042
043    /**
044     * Query table impact: find all routines that access the given table,
045     * then trace callers transitively.
046     * Uses real BFS distances and path reconstruction.
047     */
048    public ImpactResult getTableImpact(String tableName, ETableAccessKind accessFilter) {
049        return getTableImpact(tableName, accessFilter, 50);
050    }
051
052    /**
053     * Query table impact with configurable max depth.
054     */
055    public ImpactResult getTableImpact(String tableName, ETableAccessKind accessFilter, int maxDepth) {
056        return ImpactEngine.getTableImpact(callGraph, tableName, accessFilter, maxDepth);
057    }
058
059    /**
060     * Export to JSON format.
061     */
062    public String exportJson() {
063        if (irProgram == null || irProgram.getBoundProgram() == null) {
064            return "{}";
065        }
066        return AnalyzerJsonExporter.export(irProgram, callGraph, config);
067    }
068
069    /**
070     * Export to XML (legacy compatible format).
071     * Phase C implementation.
072     */
073    public String exportXml() {
074        return "<analysis/>";
075    }
076}