|
| 1 | +package io.github.randomcodespace.iq.intelligence.extractor; |
| 2 | + |
| 3 | +import io.github.randomcodespace.iq.detector.DetectorContext; |
| 4 | +import io.github.randomcodespace.iq.model.CodeEdge; |
| 5 | +import io.github.randomcodespace.iq.model.CodeNode; |
| 6 | +import org.slf4j.Logger; |
| 7 | +import org.slf4j.LoggerFactory; |
| 8 | +import org.springframework.stereotype.Component; |
| 9 | + |
| 10 | +import java.io.IOException; |
| 11 | +import java.nio.charset.StandardCharsets; |
| 12 | +import java.nio.file.Files; |
| 13 | +import java.nio.file.Path; |
| 14 | +import java.util.ArrayList; |
| 15 | +import java.util.HashMap; |
| 16 | +import java.util.List; |
| 17 | +import java.util.Map; |
| 18 | +import java.util.TreeMap; |
| 19 | + |
| 20 | +/** |
| 21 | + * Runs all {@link LanguageExtractor} beans after {@link io.github.randomcodespace.iq.intelligence.lexical.LexicalEnricher} |
| 22 | + * during the {@code enrich} command. |
| 23 | + * |
| 24 | + * <p>Builds a combined node registry (by id and fqn), groups nodes by source file, |
| 25 | + * reads each file once, and dispatches to matching extractors. Results (edges, type hints) |
| 26 | + * are written back into the in-memory node/edge lists before Neo4j bulk-load. |
| 27 | + * |
| 28 | + * <p>Extraction failures log a warning and are skipped — the pipeline never aborts. |
| 29 | + */ |
| 30 | +@Component |
| 31 | +public class LanguageEnricher { |
| 32 | + |
| 33 | + private static final Logger log = LoggerFactory.getLogger(LanguageEnricher.class); |
| 34 | + |
| 35 | + /** |
| 36 | + * Language alias map: normalises file-extension languages to extractor language keys. |
| 37 | + * e.g. "javascript" nodes are handled by the "typescript" extractor. |
| 38 | + */ |
| 39 | + private static final Map<String, String> LANGUAGE_ALIASES = Map.of( |
| 40 | + "javascript", "typescript" |
| 41 | + ); |
| 42 | + |
| 43 | + private final List<LanguageExtractor> extractors; |
| 44 | + |
| 45 | + public LanguageEnricher(List<LanguageExtractor> extractors) { |
| 46 | + this.extractors = List.copyOf(extractors); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Enrich nodes with language-specific intelligence and add new edges. |
| 51 | + * |
| 52 | + * @param nodes All enriched nodes (post-linker, post-classifier, post-lexical). |
| 53 | + * @param edges Mutable edge list — new edges are appended in place. |
| 54 | + * @param rootPath Absolute root path of the analysed repository (for file reads). |
| 55 | + */ |
| 56 | + public void enrich(List<CodeNode> nodes, List<CodeEdge> edges, Path rootPath) { |
| 57 | + if (extractors.isEmpty()) { |
| 58 | + log.debug("No LanguageExtractor beans registered — skipping language enrichment"); |
| 59 | + return; |
| 60 | + } |
| 61 | + |
| 62 | + // Build combined node registry: id → node, fqn → node |
| 63 | + Map<String, CodeNode> nodeRegistry = buildRegistry(nodes); |
| 64 | + |
| 65 | + // Build extractor lookup: normalised language → extractor |
| 66 | + Map<String, LanguageExtractor> extractorByLanguage = new HashMap<>(); |
| 67 | + for (LanguageExtractor extractor : extractors) { |
| 68 | + extractorByLanguage.put(extractor.getLanguage(), extractor); |
| 69 | + } |
| 70 | + |
| 71 | + // Group nodes by file path (read each file only once). |
| 72 | + // TreeMap guarantees deterministic iteration order (alphabetical by path). |
| 73 | + Map<String, List<CodeNode>> nodesByFile = new TreeMap<>(); |
| 74 | + for (CodeNode node : nodes) { |
| 75 | + if (node.getFilePath() != null) { |
| 76 | + nodesByFile.computeIfAbsent(node.getFilePath(), k -> new ArrayList<>()).add(node); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + int edgesAdded = 0; |
| 81 | + int typeHintsAdded = 0; |
| 82 | + |
| 83 | + for (Map.Entry<String, List<CodeNode>> entry : nodesByFile.entrySet()) { |
| 84 | + String filePath = entry.getKey(); |
| 85 | + List<CodeNode> fileNodes = entry.getValue(); |
| 86 | + |
| 87 | + String language = detectLanguage(filePath); |
| 88 | + if (language == null) continue; |
| 89 | + |
| 90 | + String resolvedLanguage = LANGUAGE_ALIASES.getOrDefault(language, language); |
| 91 | + LanguageExtractor extractor = extractorByLanguage.get(resolvedLanguage); |
| 92 | + if (extractor == null) continue; |
| 93 | + |
| 94 | + String content = readFile(rootPath, filePath); |
| 95 | + if (content == null) continue; |
| 96 | + |
| 97 | + DetectorContext ctx = new DetectorContext(filePath, language, content, nodeRegistry, null); |
| 98 | + |
| 99 | + for (CodeNode node : fileNodes) { |
| 100 | + try { |
| 101 | + LanguageExtractionResult result = extractor.extract(ctx, node); |
| 102 | + edges.addAll(result.callEdges()); |
| 103 | + edges.addAll(result.symbolReferences()); |
| 104 | + edgesAdded += result.callEdges().size() + result.symbolReferences().size(); |
| 105 | + for (Map.Entry<String, String> hint : result.typeHints().entrySet()) { |
| 106 | + node.getProperties().put(hint.getKey(), hint.getValue()); |
| 107 | + typeHintsAdded++; |
| 108 | + } |
| 109 | + } catch (Exception e) { |
| 110 | + log.warn("LanguageExtractor {} failed on node {} in {}: {}", |
| 111 | + extractor.getClass().getSimpleName(), node.getId(), filePath, e.getMessage()); |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + log.info("Language enrichment: {} edges added, {} type hints added across {} extractors", |
| 117 | + edgesAdded, typeHintsAdded, extractorByLanguage.size()); |
| 118 | + } |
| 119 | + |
| 120 | + private Map<String, CodeNode> buildRegistry(List<CodeNode> nodes) { |
| 121 | + Map<String, CodeNode> registry = new HashMap<>(); |
| 122 | + for (CodeNode node : nodes) { |
| 123 | + if (node.getId() != null) { |
| 124 | + registry.put(node.getId(), node); |
| 125 | + } |
| 126 | + if (node.getFqn() != null && !node.getFqn().isEmpty()) { |
| 127 | + registry.put(node.getFqn(), node); |
| 128 | + } |
| 129 | + } |
| 130 | + return registry; |
| 131 | + } |
| 132 | + |
| 133 | + private String readFile(Path rootPath, String filePath) { |
| 134 | + try { |
| 135 | + Path resolved = rootPath.resolve(filePath); |
| 136 | + if (!Files.exists(resolved)) return null; |
| 137 | + return Files.readString(resolved, StandardCharsets.UTF_8); |
| 138 | + } catch (IOException e) { |
| 139 | + log.debug("Could not read file {}: {}", filePath, e.getMessage()); |
| 140 | + return null; |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Map file extension to language string (mirrors FileDiscovery conventions). |
| 146 | + */ |
| 147 | + static String detectLanguage(String filePath) { |
| 148 | + if (filePath == null) return null; |
| 149 | + int dot = filePath.lastIndexOf('.'); |
| 150 | + if (dot < 0) return null; |
| 151 | + return switch (filePath.substring(dot + 1).toLowerCase()) { |
| 152 | + case "java" -> "java"; |
| 153 | + case "ts", "tsx" -> "typescript"; |
| 154 | + case "js", "jsx", "mjs", "cjs" -> "javascript"; |
| 155 | + case "py", "pyw" -> "python"; |
| 156 | + case "go" -> "go"; |
| 157 | + default -> null; |
| 158 | + }; |
| 159 | + } |
| 160 | +} |
0 commit comments