Coverage for lode / reader / modules.py: 21%
28 statements
« prev ^ index » next coverage.py v7.13.0, created at 2026-03-25 15:05 +0000
« prev ^ index » next coverage.py v7.13.0, created at 2026-03-25 15:05 +0000
1# modules.py - Moduli di arricchimento del grafo RDF
2from rdflib import Graph, OWL
3from typing import Optional
5def apply_imported(graph: Graph) -> Graph:
6 """Arricchisce il grafo con le triple delle ontologie direttamente importate (profondita 1)."""
7 return _expand_owl_imports(graph, max_depth=1)
10def apply_closure(graph: Graph) -> Graph:
11 """Arricchisce il grafo con la chiusura transitiva completa di owl:imports."""
12 return _expand_owl_imports(graph, max_depth=None)
14def _expand_owl_imports(graph: Graph, max_depth: Optional[int]) -> Graph:
15 visited: set = set()
16 for _, _, uri in graph.triples((None, OWL.imports, None)):
17 _load_into(graph, str(uri), depth=1, max_depth=max_depth, visited=visited)
18 return graph
21def _load_into(graph: Graph, source: str, depth: int, max_depth: Optional[int], visited: set) -> None:
22 if source in visited:
23 return
24 if max_depth is not None and depth > max_depth:
25 return
27 visited.add(source)
29 # Riusa il Loader per tutto il caricamento (content negotiation, formati, ecc.)
30 # I moduli non vengono propagati: ogni ontologia importata viene caricata as-is
31 from lode.reader.loader import Loader
32 try:
33 imported_loader = Loader(source)
34 except Exception:
35 print(f" [modules] Warning: could not load {source}")
36 return
38 imported_graph = imported_loader.get_graph()
39 print(f" [modules] Imported {len(imported_graph)} triples from {source}")
40 graph += imported_graph
42 for _, _, nested_uri in imported_graph.triples((None, OWL.imports, None)):
43 _load_into(graph, str(nested_uri), depth + 1, max_depth, visited)