Skip to content

Commit

Permalink
rm shapes and ontology path separation
Browse files Browse the repository at this point in the history
  • Loading branch information
ssssarah committed Oct 24, 2023
1 parent fe7747c commit 93c99d4
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 94 deletions.
16 changes: 2 additions & 14 deletions kgforge/core/archetypes/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand All @@ -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
Expand Down
7 changes: 2 additions & 5 deletions kgforge/specializations/models/demo_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
21 changes: 1 addition & 20 deletions kgforge/specializations/models/rdf/pyshacl_shape_wrapper.py
Original file line number Diff line number Diff line change
@@ -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}

Expand Down
18 changes: 3 additions & 15 deletions kgforge/specializations/models/rdf/rdf_model_directory_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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:
Expand Down
13 changes: 2 additions & 11 deletions kgforge/specializations/models/rdf_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
58 changes: 29 additions & 29 deletions kgforge/specializations/stores/nexus/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 93c99d4

Please sign in to comment.