Skip to content

Commit 93c99d4

Browse files
committed
rm shapes and ontology path separation
1 parent fe7747c commit 93c99d4

File tree

6 files changed

+39
-94
lines changed

6 files changed

+39
-94
lines changed

kgforge/core/archetypes/model.py

+2-14
Original file line numberDiff line numberDiff line change
@@ -177,16 +177,7 @@ def _initialize_service(self, source: str, **source_config) -> Any:
177177

178178
if origin == "directory":
179179

180-
ontology_path = Path(source_config["ontology_path"]) \
181-
if "ontology_path" in source_config else None
182-
shapes_path = Path(source_config["shapes_path"]) \
183-
if "shapes_path" in source_config else None
184-
source_path = Path(source)
185-
186-
return self._service_from_directory(
187-
source_path=source_path, ontologies_path=ontology_path, shapes_path=shapes_path,
188-
context_iri=context_iri
189-
)
180+
return self._service_from_directory(dir_path=Path(source), context_iri=context_iri)
190181
elif origin == "url":
191182
return self._service_from_url(source, context_iri)
192183
elif origin == "store":
@@ -197,10 +188,7 @@ def _initialize_service(self, source: str, **source_config) -> Any:
197188

198189
@staticmethod
199190
@abstractmethod
200-
def _service_from_directory(
201-
source_path: Optional[Path], ontologies_path: Optional[Path],
202-
shapes_path: Optional[Path], context_iri: Optional[str]
203-
) -> Any:
191+
def _service_from_directory(dir_path: Path, context_iri: Optional[str]) -> Any:
204192
pass
205193

206194
@staticmethod

kgforge/specializations/models/demo_model.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,8 @@ def _validate_one(self, resource: Resource, type_: str) -> None:
9696
# Utils.
9797

9898
@staticmethod
99-
def _service_from_directory(
100-
source_path: Optional[Path], ontologies_path: Optional[Path],
101-
shapes_path: Optional[Path], context_iri: Optional[str]
102-
):
103-
return ModelLibrary(source_path)
99+
def _service_from_directory(dir_path: Path, context_iri: Optional[str]):
100+
return ModelLibrary(dir_path)
104101

105102

106103
class ModelLibrary:

kgforge/specializations/models/rdf/pyshacl_shape_wrapper.py

+1-20
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,11 @@
1-
import pyshacl
21
from pyshacl import Shape, ShapesGraph
32
from rdflib import Graph, URIRef
43
from pyshacl.constraints import ALL_CONSTRAINT_PARAMETERS
54

5+
from typing import List, Optional, Set, Tuple, Dict
66

77
from kgforge.specializations.models.rdf.collectors import ALL_COLLECTORS
88

9-
from time import perf_counter
10-
from typing import TYPE_CHECKING, List, Optional, Set, Tuple, Type, Union, Dict
11-
12-
from rdflib import BNode, Literal, URIRef
13-
14-
from pyshacl.consts import (
15-
SH_Info,
16-
SH_resultSeverity,
17-
SH_Warning,
18-
)
19-
from pyshacl.errors import ConstraintLoadError, ConstraintLoadWarning, ReportableRuntimeError, \
20-
ShapeLoadError
21-
22-
from pyshacl.pytypes import GraphLike
23-
24-
if TYPE_CHECKING:
25-
from pyshacl.constraints import ConstraintComponent
26-
from pyshacl.shapes_graph import ShapesGraph
27-
289

2910
ALL_COLLECTORS_MAP = {c.constraint(): c for c in ALL_COLLECTORS}
3011

kgforge/specializations/models/rdf/rdf_model_directory_service.py

+3-15
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,8 @@
2727

2828
class DirectoryService(RdfService):
2929

30-
def __init__(self, source_path: Path, ontologies_path: Optional[Path],
31-
shapes_path: Optional[Path], context_iri: str) -> None:
32-
33-
g = Graph()
34-
if ontologies_path is None and shapes_path is None:
35-
if source_path is None:
36-
raise Exception("Must specify source path")
37-
else:
38-
g = load_rdf_files(source_path, g)
39-
else:
40-
g = load_rdf_files(ontologies_path, g)
41-
g = load_rdf_files(shapes_path, g)
42-
43-
self._graph = g
30+
def __init__(self, dir_path: Path, context_iri: str) -> None:
31+
self._graph = load_rdf_files_into_graph(dir_path, Graph())
4432
self._shapes_graph = ShapesGraphWrapper(self._graph)
4533
super().__init__(self._graph, context_iri)
4634

@@ -115,7 +103,7 @@ def _build_shapes_map(self) -> Tuple[Dict[URIRef, str], Dict[str, URIRef]]:
115103
return schema_to_file, class_being_shaped_id_to_shape_uri
116104

117105

118-
def load_rdf_files(path: Path, memory_graph: Graph) -> Graph:
106+
def load_rdf_files_into_graph(path: Path, memory_graph: Graph) -> Graph:
119107
extensions = [".ttl", ".n3", ".json", ".rdf"]
120108
for f in path.rglob(os.path.join("*.*")):
121109
if f.suffix in extensions:

kgforge/specializations/models/rdf_model.py

+2-11
Original file line numberDiff line numberDiff line change
@@ -132,17 +132,8 @@ def _validate_one(self, resource: Resource, type_: str) -> None:
132132
# Utils.
133133

134134
@staticmethod
135-
def _service_from_directory(
136-
source_path: Optional[Path],
137-
ontologies_path: Optional[Path],
138-
shapes_path: Optional[Path],
139-
context_iri: str,
140-
**dir_config
141-
) -> RdfService:
142-
return DirectoryService(
143-
source_path=source_path,
144-
ontologies_path=ontologies_path, shapes_path=shapes_path, context_iri=context_iri
145-
)
135+
def _service_from_directory(dir_path: Path, context_iri: str, **dir_config) -> RdfService:
136+
return DirectoryService(dir_path=dir_path, context_iri=context_iri)
146137

147138
@staticmethod
148139
def _service_from_store(store: Callable, context_config: Optional[Dict],

kgforge/specializations/stores/nexus/service.py

+29-29
Original file line numberDiff line numberDiff line change
@@ -248,44 +248,44 @@ def get_project_context(self) -> Dict:
248248
return context
249249

250250
def resolve_context(self, iri: str, local_only: Optional[bool] = False) -> Dict:
251-
if iri in self.context_cache:
252-
return self.context_cache[iri]
253-
254251
context_to_resolve = (
255252
self.store_local_context if iri == self.store_context else iri
256253
)
257-
url = "/".join((self.url_resolver, "_", quote_plus(context_to_resolve)))
258254

259-
try:
260-
response = requests.get(url, headers=self.headers)
261-
response.raise_for_status()
262-
resource = response.json()
263-
except Exception as e:
264-
if not local_only:
265-
try:
266-
context = Context(context_to_resolve)
267-
except URLError:
268-
raise ValueError(f"{context_to_resolve} is not resolvable")
255+
if context_to_resolve not in self.context_cache:
256+
257+
url = "/".join((self.url_resolver, "_", quote_plus(context_to_resolve)))
258+
259+
try:
260+
response = requests.get(url, headers=self.headers)
261+
response.raise_for_status()
262+
resource = response.json()
263+
except Exception as e:
264+
if not local_only:
265+
try:
266+
context = Context(context_to_resolve)
267+
except URLError:
268+
raise ValueError(f"{context_to_resolve} is not resolvable")
269+
else:
270+
document = context.document["@context"]
269271
else:
270-
document = context.document["@context"]
272+
raise ValueError(f"{context_to_resolve} is not resolvable")
271273
else:
272-
raise ValueError(f"{context_to_resolve} is not resolvable")
273-
else:
274-
# Make sure context is not deprecated
275-
if '_deprecated' in resource and resource['_deprecated']:
276-
raise ConfigurationError(f"Context {context_to_resolve} exists but was deprecated")
277-
document = json.loads(json.dumps(resource["@context"]))
274+
# Make sure context is not deprecated
275+
if '_deprecated' in resource and resource['_deprecated']:
276+
raise ConfigurationError(
277+
f"Context {context_to_resolve} exists but was deprecated"
278+
)
279+
document = json.loads(json.dumps(resource["@context"]))
278280

279-
if isinstance(document, list):
280-
if self.store_context in document:
281-
document.remove(self.store_context)
282-
if self.store_local_context in document:
283-
document.remove(self.store_local_context)
281+
if isinstance(document, list):
282+
if self.store_context in document:
283+
document.remove(self.store_context)
284+
if self.store_local_context in document:
285+
document.remove(self.store_local_context)
284286

285-
self.context_cache[context_to_resolve] = document
287+
self.context_cache[context_to_resolve] = document
286288

287-
# TODO context_to_resolve may be different from iri. Why is having it in the cache
288-
# already leading to different outcome? (see first 2 lines of function)
289289
return self.context_cache[context_to_resolve]
290290

291291
def batch_request(

0 commit comments

Comments
 (0)