|
28 | 28 | class RdfModelServiceFromDirectory(RdfModelService): |
29 | 29 |
|
30 | 30 | def __init__(self, dir_path: Path, context_iri: str) -> None: |
31 | | - self._graph = load_rdf_files_into_graph(dir_path, Graph()) |
32 | | - self._shapes_graph = ShapesGraphWrapper(self._graph) |
33 | | - super().__init__(self._graph, context_iri) |
| 31 | + |
| 32 | + graph, shape_to_source, class_to_shape = self._build_shapes_map(dir_path=dir_path) |
| 33 | + self._shapes_graph = ShapesGraphWrapper(graph) |
| 34 | + |
| 35 | + super().__init__( |
| 36 | + graph=graph, context_iri=context_iri, shape_to_source=shape_to_source, |
| 37 | + class_to_shape=class_to_shape |
| 38 | + ) |
34 | 39 |
|
35 | 40 | def materialize(self, iri: URIRef) -> NodeProperties: |
36 | 41 | sh = self._shapes_graph.lookup_shape_from_node(iri) |
@@ -58,34 +63,53 @@ def resolve_context(self, iri: str) -> Dict: |
58 | 63 | def generate_context(self) -> Dict: |
59 | 64 | return self._generate_context() |
60 | 65 |
|
61 | | - def _build_shapes_map(self) -> Tuple[Dict[URIRef, str], Dict[str, URIRef]]: |
62 | | - query = """ |
63 | | - PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> |
64 | | - PREFIX sh: <http://www.w3.org/ns/shacl#> |
65 | | - SELECT ?type ?shape WHERE { |
66 | | - { ?shape sh:targetClass ?type .} |
67 | | - UNION { |
68 | | - SELECT (?shape as ?type) ?shape WHERE { |
69 | | - ?shape a sh:NodeShape . |
70 | | - ?shape a rdfs:Class |
71 | | - } |
72 | | - } |
73 | | - } ORDER BY ?type |
74 | | - """ |
75 | | - res = self._graph.query(query) |
76 | | - |
77 | | - class_to_shape: Dict[str, URIRef] = { |
78 | | - row["type"]: URIRef(row["shape"]) |
79 | | - for row in res |
80 | | - } |
81 | | - |
82 | | - # FIXME should return the file path where the schema is in |
83 | | - shape_to_file = dict( |
84 | | - (e, "") # TODO file source |
85 | | - for e in class_to_shape.values() |
86 | | - ) |
| 66 | + def _build_shapes_map( |
| 67 | + self, dir_path: Path |
| 68 | + ) -> Tuple[Graph, Dict[URIRef, str], Dict[str, URIRef]]: |
87 | 69 |
|
88 | | - return shape_to_file, class_to_shape |
| 70 | + query = """ |
| 71 | + PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> |
| 72 | + PREFIX sh: <http://www.w3.org/ns/shacl#> |
| 73 | + SELECT ?type ?shape WHERE { |
| 74 | + { ?shape sh:targetClass ?type .} |
| 75 | + UNION { |
| 76 | + SELECT (?shape as ?type) ?shape WHERE { |
| 77 | + ?shape a sh:NodeShape . |
| 78 | + ?shape a rdfs:Class |
| 79 | + } |
| 80 | + } |
| 81 | + } ORDER BY ?type |
| 82 | + """ |
| 83 | + |
| 84 | + class_to_shape: Dict[str, URIRef] = dict() |
| 85 | + shape_to_file: Dict[URIRef, str] = dict() |
| 86 | + graph = Graph() |
| 87 | + |
| 88 | + extensions = [".ttl", ".n3", ".json", ".rdf"] |
| 89 | + for f in dir_path.rglob(os.path.join("*.*")): |
| 90 | + graph_i = Graph() |
| 91 | + if f.suffix in extensions: |
| 92 | + file_format = guess_format(f.name) |
| 93 | + if file_format is None: |
| 94 | + file_format = "json-ld" |
| 95 | + graph_i.parse(f.as_posix(), format=file_format) |
| 96 | + |
| 97 | + res = graph_i.query(query) |
| 98 | + |
| 99 | + class_to_shape_i = dict( |
| 100 | + (row["type"], URIRef(row["shape"])) |
| 101 | + for row in res |
| 102 | + ) |
| 103 | + class_to_shape.update(class_to_shape_i) |
| 104 | + |
| 105 | + shape_to_file.update(dict( |
| 106 | + (e, f.as_posix()) |
| 107 | + for e in class_to_shape_i.values() |
| 108 | + )) |
| 109 | + |
| 110 | + graph += graph_i |
| 111 | + |
| 112 | + return graph, shape_to_file, class_to_shape |
89 | 113 |
|
90 | 114 |
|
91 | 115 | def load_rdf_files_into_graph(path: Path, memory_graph: Graph) -> Graph: |
|
0 commit comments