From 93c99d4203a0da7fb56fdb58b0502a38f4272ed9 Mon Sep 17 00:00:00 2001 From: mouffok Date: Tue, 24 Oct 2023 15:22:39 +0200 Subject: [PATCH] rm shapes and ontology path separation --- kgforge/core/archetypes/model.py | 16 +---- kgforge/specializations/models/demo_model.py | 7 +-- .../models/rdf/pyshacl_shape_wrapper.py | 21 +------ .../models/rdf/rdf_model_directory_service.py | 18 +----- kgforge/specializations/models/rdf_model.py | 13 +---- .../specializations/stores/nexus/service.py | 58 +++++++++---------- 6 files changed, 39 insertions(+), 94 deletions(-) diff --git a/kgforge/core/archetypes/model.py b/kgforge/core/archetypes/model.py index 484b09ef..b9b4e084 100644 --- a/kgforge/core/archetypes/model.py +++ b/kgforge/core/archetypes/model.py @@ -177,16 +177,7 @@ def _initialize_service(self, source: str, **source_config) -> Any: if origin == "directory": - ontology_path = Path(source_config["ontology_path"]) \ - if "ontology_path" in source_config else None - shapes_path = Path(source_config["shapes_path"]) \ - if "shapes_path" in source_config else None - source_path = Path(source) - - return self._service_from_directory( - source_path=source_path, ontologies_path=ontology_path, shapes_path=shapes_path, - context_iri=context_iri - ) + return self._service_from_directory(dir_path=Path(source), context_iri=context_iri) elif origin == "url": return self._service_from_url(source, context_iri) elif origin == "store": @@ -197,10 +188,7 @@ def _initialize_service(self, source: str, **source_config) -> Any: @staticmethod @abstractmethod - def _service_from_directory( - source_path: Optional[Path], ontologies_path: Optional[Path], - shapes_path: Optional[Path], context_iri: Optional[str] - ) -> Any: + def _service_from_directory(dir_path: Path, context_iri: Optional[str]) -> Any: pass @staticmethod diff --git a/kgforge/specializations/models/demo_model.py b/kgforge/specializations/models/demo_model.py index 2cdb89ee..7f834c01 100644 --- a/kgforge/specializations/models/demo_model.py +++ b/kgforge/specializations/models/demo_model.py @@ -96,11 +96,8 @@ def _validate_one(self, resource: Resource, type_: str) -> None: # Utils. @staticmethod - def _service_from_directory( - source_path: Optional[Path], ontologies_path: Optional[Path], - shapes_path: Optional[Path], context_iri: Optional[str] - ): - return ModelLibrary(source_path) + def _service_from_directory(dir_path: Path, context_iri: Optional[str]): + return ModelLibrary(dir_path) class ModelLibrary: diff --git a/kgforge/specializations/models/rdf/pyshacl_shape_wrapper.py b/kgforge/specializations/models/rdf/pyshacl_shape_wrapper.py index da63a8ff..d1a64b10 100644 --- a/kgforge/specializations/models/rdf/pyshacl_shape_wrapper.py +++ b/kgforge/specializations/models/rdf/pyshacl_shape_wrapper.py @@ -1,30 +1,11 @@ -import pyshacl from pyshacl import Shape, ShapesGraph from rdflib import Graph, URIRef from pyshacl.constraints import ALL_CONSTRAINT_PARAMETERS +from typing import List, Optional, Set, Tuple, Dict from kgforge.specializations.models.rdf.collectors import ALL_COLLECTORS -from time import perf_counter -from typing import TYPE_CHECKING, List, Optional, Set, Tuple, Type, Union, Dict - -from rdflib import BNode, Literal, URIRef - -from pyshacl.consts import ( - SH_Info, - SH_resultSeverity, - SH_Warning, -) -from pyshacl.errors import ConstraintLoadError, ConstraintLoadWarning, ReportableRuntimeError, \ - ShapeLoadError - -from pyshacl.pytypes import GraphLike - -if TYPE_CHECKING: - from pyshacl.constraints import ConstraintComponent - from pyshacl.shapes_graph import ShapesGraph - ALL_COLLECTORS_MAP = {c.constraint(): c for c in ALL_COLLECTORS} diff --git a/kgforge/specializations/models/rdf/rdf_model_directory_service.py b/kgforge/specializations/models/rdf/rdf_model_directory_service.py index a5c8badc..0c46287b 100644 --- a/kgforge/specializations/models/rdf/rdf_model_directory_service.py +++ b/kgforge/specializations/models/rdf/rdf_model_directory_service.py @@ -27,20 +27,8 @@ class DirectoryService(RdfService): - def __init__(self, source_path: Path, ontologies_path: Optional[Path], - shapes_path: Optional[Path], context_iri: str) -> None: - - g = Graph() - if ontologies_path is None and shapes_path is None: - if source_path is None: - raise Exception("Must specify source path") - else: - g = load_rdf_files(source_path, g) - else: - g = load_rdf_files(ontologies_path, g) - g = load_rdf_files(shapes_path, g) - - self._graph = g + def __init__(self, dir_path: Path, context_iri: str) -> None: + self._graph = load_rdf_files_into_graph(dir_path, Graph()) self._shapes_graph = ShapesGraphWrapper(self._graph) super().__init__(self._graph, context_iri) @@ -115,7 +103,7 @@ def _build_shapes_map(self) -> Tuple[Dict[URIRef, str], Dict[str, URIRef]]: return schema_to_file, class_being_shaped_id_to_shape_uri -def load_rdf_files(path: Path, memory_graph: Graph) -> Graph: +def load_rdf_files_into_graph(path: Path, memory_graph: Graph) -> Graph: extensions = [".ttl", ".n3", ".json", ".rdf"] for f in path.rglob(os.path.join("*.*")): if f.suffix in extensions: diff --git a/kgforge/specializations/models/rdf_model.py b/kgforge/specializations/models/rdf_model.py index 99758691..5724cf69 100644 --- a/kgforge/specializations/models/rdf_model.py +++ b/kgforge/specializations/models/rdf_model.py @@ -132,17 +132,8 @@ def _validate_one(self, resource: Resource, type_: str) -> None: # Utils. @staticmethod - def _service_from_directory( - source_path: Optional[Path], - ontologies_path: Optional[Path], - shapes_path: Optional[Path], - context_iri: str, - **dir_config - ) -> RdfService: - return DirectoryService( - source_path=source_path, - ontologies_path=ontologies_path, shapes_path=shapes_path, context_iri=context_iri - ) + def _service_from_directory(dir_path: Path, context_iri: str, **dir_config) -> RdfService: + return DirectoryService(dir_path=dir_path, context_iri=context_iri) @staticmethod def _service_from_store(store: Callable, context_config: Optional[Dict], diff --git a/kgforge/specializations/stores/nexus/service.py b/kgforge/specializations/stores/nexus/service.py index 8da5ec34..2f63cf71 100644 --- a/kgforge/specializations/stores/nexus/service.py +++ b/kgforge/specializations/stores/nexus/service.py @@ -248,44 +248,44 @@ def get_project_context(self) -> Dict: return context def resolve_context(self, iri: str, local_only: Optional[bool] = False) -> Dict: - if iri in self.context_cache: - return self.context_cache[iri] - context_to_resolve = ( self.store_local_context if iri == self.store_context else iri ) - url = "/".join((self.url_resolver, "_", quote_plus(context_to_resolve))) - try: - response = requests.get(url, headers=self.headers) - response.raise_for_status() - resource = response.json() - except Exception as e: - if not local_only: - try: - context = Context(context_to_resolve) - except URLError: - raise ValueError(f"{context_to_resolve} is not resolvable") + if context_to_resolve not in self.context_cache: + + url = "/".join((self.url_resolver, "_", quote_plus(context_to_resolve))) + + try: + response = requests.get(url, headers=self.headers) + response.raise_for_status() + resource = response.json() + except Exception as e: + if not local_only: + try: + context = Context(context_to_resolve) + except URLError: + raise ValueError(f"{context_to_resolve} is not resolvable") + else: + document = context.document["@context"] else: - document = context.document["@context"] + raise ValueError(f"{context_to_resolve} is not resolvable") else: - raise ValueError(f"{context_to_resolve} is not resolvable") - else: - # Make sure context is not deprecated - if '_deprecated' in resource and resource['_deprecated']: - raise ConfigurationError(f"Context {context_to_resolve} exists but was deprecated") - document = json.loads(json.dumps(resource["@context"])) + # Make sure context is not deprecated + if '_deprecated' in resource and resource['_deprecated']: + raise ConfigurationError( + f"Context {context_to_resolve} exists but was deprecated" + ) + document = json.loads(json.dumps(resource["@context"])) - if isinstance(document, list): - if self.store_context in document: - document.remove(self.store_context) - if self.store_local_context in document: - document.remove(self.store_local_context) + if isinstance(document, list): + if self.store_context in document: + document.remove(self.store_context) + if self.store_local_context in document: + document.remove(self.store_local_context) - self.context_cache[context_to_resolve] = document + self.context_cache[context_to_resolve] = document - # TODO context_to_resolve may be different from iri. Why is having it in the cache - # already leading to different outcome? (see first 2 lines of function) return self.context_cache[context_to_resolve] def batch_request(