Skip to content

Commit

Permalink
chore: renaming functions of ontology checks in xmlupload (#817)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nora-Olivia-Ammann committed Feb 20, 2024
1 parent 3f869d9 commit e6b0e7d
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 98 deletions.
28 changes: 14 additions & 14 deletions src/dsp_tools/commands/xmlupload/check_consistency_with_ontology.py
Expand Up @@ -10,7 +10,7 @@
InvalidOntologyElementsInData,
)
from dsp_tools.commands.xmlupload.ontology_client import OntologyClient
from dsp_tools.models.exceptions import UserError
from dsp_tools.models.exceptions import InputError

defaultOntologyColon: Pattern[str] = regex.compile(r"^:\w+$")
knoraUndeclared: Pattern[str] = regex.compile(r"^\w+$")
Expand All @@ -22,30 +22,30 @@ def do_xml_consistency_check_with_ontology(onto_client: OntologyClient, root: et
This function takes an OntologyClient and the root of an XML.
It retrieves the ontologies from the server.
It iterates over the root.
If it finds any invalid properties or classes, they are printed out and a UserError is raised.
If it finds any invalid properties or classes, they are printed out and a InputError is raised.
Args:
onto_client: client for the ontology retrieval
root: root of the XML
Raises:
UserError: if there are any invalid properties or classes
InputError: if there are any invalid properties or classes
"""
onto_check_info = ProjectOntosInformation(
default_ontology_prefix=onto_client.default_ontology,
onto_lookup=onto_client.get_all_ontologies_from_server(),
)
classes_in_data, properties_in_data = _get_all_classes_and_properties_from_data(root)
_find_if_all_classes_and_properties_exist_in_onto(classes_in_data, properties_in_data, onto_check_info)
_find_all_classes_and_properties_in_onto(classes_in_data, properties_in_data, onto_check_info)


def _find_if_all_classes_and_properties_exist_in_onto(
def _find_all_classes_and_properties_in_onto(
classes_in_data: dict[str, list[str]],
properties_in_data: dict[str, list[str]],
onto_check_info: ProjectOntosInformation,
) -> None:
class_problems = _check_if_all_class_types_exist(classes_in_data, onto_check_info)
property_problems = _check_if_all_properties_exist(properties_in_data, onto_check_info)
class_problems = _find_all_class_types_in_onto(classes_in_data, onto_check_info)
property_problems = _find_all_properties_in_onto(properties_in_data, onto_check_info)
if not class_problems and not property_problems:
return None
problems = InvalidOntologyElementsInData(
Expand All @@ -59,7 +59,7 @@ def _find_if_all_classes_and_properties_exist_in_onto(
"\n\n---------------------------------------\n\n"
f"\nAll the problems are listed in the file: '{Path.cwd()}/{csv_file}'"
)
raise UserError(msg)
raise InputError(msg)


def _get_all_classes_and_properties_from_data(
Expand Down Expand Up @@ -96,17 +96,17 @@ def _get_all_property_names_and_resource_ids_one_resource(
return prop_dict


def _check_if_all_class_types_exist(
def _find_all_class_types_in_onto(
classes: dict[str, list[str]], onto_check_info: ProjectOntosInformation
) -> list[tuple[str, list[str], str]]:
problem_list = []
for cls_type, ids in classes.items():
if problem := _check_if_one_class_type_exists(cls_type, onto_check_info):
if problem := _find_one_class_type_in_onto(cls_type, onto_check_info):
problem_list.append((cls_type, ids, problem))
return problem_list


def _check_if_one_class_type_exists(cls_type: str, onto_check_info: ProjectOntosInformation) -> str | None:
def _find_one_class_type_in_onto(cls_type: str, onto_check_info: ProjectOntosInformation) -> str | None:
prefix, cls_ = _get_separate_prefix_and_iri_from_onto_prop_or_cls(cls_type, onto_check_info.default_ontology_prefix)
if not prefix:
return "Class name does not follow a known ontology pattern"
Expand All @@ -116,17 +116,17 @@ def _check_if_one_class_type_exists(cls_type: str, onto_check_info: ProjectOntos
return "Unknown ontology prefix"


def _check_if_all_properties_exist(
def _find_all_properties_in_onto(
properties: dict[str, list[str]], onto_check_info: ProjectOntosInformation
) -> list[tuple[str, list[str], str]]:
problem_list = []
for prop_name, ids in properties.items():
if problem := _check_if_one_property_exists(prop_name, onto_check_info):
if problem := _find_one_property_in_onto(prop_name, onto_check_info):
problem_list.append((prop_name, ids, problem))
return problem_list


def _check_if_one_property_exists(prop_name: str, onto_check_info: ProjectOntosInformation) -> str | None:
def _find_one_property_in_onto(prop_name: str, onto_check_info: ProjectOntosInformation) -> str | None:
prefix, prop = _get_separate_prefix_and_iri_from_onto_prop_or_cls(
prop_name, onto_check_info.default_ontology_prefix
)
Expand Down
24 changes: 12 additions & 12 deletions src/dsp_tools/commands/xmlupload/models/ontology_lookup_models.py
Expand Up @@ -18,39 +18,39 @@ class ProjectOntosInformation:
onto_lookup: dict[str, OntoInfo]


def extract_classes_properties_from_onto(onto_graph: list[dict[str, Any]]) -> OntoInfo:
def extract_classes_and_properties_from_onto(onto_json: list[dict[str, Any]]) -> OntoInfo:
"""
This function takes an ontology graph from the DSP-API.
It extracts the classes and properties.
And saves them in an instance of the class Ontology.
Args:
onto_graph: graph from DSP-API
onto_json: graph from DSP-API
Returns:
Ontology instance with the classes and properties
"""
classes = _get_all_cleaned_classes_from_graph(onto_graph)
properties = _get_all_cleaned_properties_from_graph(onto_graph)
classes = _get_all_cleaned_classes_from_json(onto_json)
properties = _get_all_cleaned_properties_from_json(onto_json)
return OntoInfo(classes, properties)


def _get_all_cleaned_classes_from_graph(onto_graph: list[dict[str, Any]]) -> list[str]:
classes = _get_all_classes_from_graph(onto_graph)
def _get_all_cleaned_classes_from_json(onto_json: list[dict[str, Any]]) -> list[str]:
classes = _get_all_classes_from_json(onto_json)
return _remove_prefixes(classes)


def _get_all_classes_from_graph(onto_graph: list[dict[str, Any]]) -> list[str]:
return [elem["@id"] for elem in onto_graph if elem.get("knora-api:isResourceClass")]
def _get_all_classes_from_json(onto_json: list[dict[str, Any]]) -> list[str]:
return [elem["@id"] for elem in onto_json if elem.get("knora-api:isResourceClass")]


def _get_all_cleaned_properties_from_graph(onto_graph: list[dict[str, Any]]) -> list[str]:
props = _get_all_properties_from_graph(onto_graph)
def _get_all_cleaned_properties_from_json(onto_json: list[dict[str, Any]]) -> list[str]:
props = _get_all_properties_from_json(onto_json)
return _remove_prefixes(props)


def _get_all_properties_from_graph(onto_graph: list[dict[str, Any]]) -> list[str]:
return [elem["@id"] for elem in onto_graph if not elem.get("knora-api:isResourceClass")]
def _get_all_properties_from_json(onto_json: list[dict[str, Any]]) -> list[str]:
return [elem["@id"] for elem in onto_json if not elem.get("knora-api:isResourceClass")]


def _remove_prefixes(ontology_elements: list[str]) -> list[str]:
Expand Down
8 changes: 6 additions & 2 deletions src/dsp_tools/commands/xmlupload/ontology_client.py
@@ -1,7 +1,10 @@
from dataclasses import dataclass, field
from typing import Any, Protocol

from dsp_tools.commands.xmlupload.models.ontology_lookup_models import OntoInfo, extract_classes_properties_from_onto
from dsp_tools.commands.xmlupload.models.ontology_lookup_models import (
OntoInfo,
extract_classes_and_properties_from_onto,
)
from dsp_tools.models.exceptions import BaseError, UserError
from dsp_tools.utils.connection import Connection
from dsp_tools.utils.create_logger import get_logger
Expand Down Expand Up @@ -43,7 +46,8 @@ def get_all_ontologies_from_server(self) -> dict[str, OntoInfo]:
"""
ontologies = self._get_all_ontology_jsons_from_server()
return {
onto_name: extract_classes_properties_from_onto(onto_graph) for onto_name, onto_graph in ontologies.items()
onto_name: extract_classes_and_properties_from_onto(onto_graph)
for onto_name, onto_graph in ontologies.items()
}

def _get_all_ontology_jsons_from_server(self) -> dict[str, list[dict[str, Any]]]:
Expand Down
1 change: 1 addition & 0 deletions src/dsp_tools/commands/xmlupload/xmlupload.py
Expand Up @@ -65,6 +65,7 @@ def xmlupload(
Raises:
BaseError: in case of permanent network or software failure
UserError: in case of permanent network or software failure, or if the XML file is invalid
InputError: in case of permanent network or software failure, or if the XML file is invalid
Returns:
True if all resources could be uploaded without errors; False if one of the resources could not be
Expand Down
Expand Up @@ -8,7 +8,7 @@

from dsp_tools.commands.xmlupload.check_consistency_with_ontology import do_xml_consistency_check_with_ontology
from dsp_tools.commands.xmlupload.ontology_client import OntologyClientLive
from dsp_tools.models.exceptions import BaseError, UserError
from dsp_tools.models.exceptions import BaseError, InputError, UserError


@dataclass
Expand Down Expand Up @@ -95,7 +95,7 @@ def test_error_on_nonexistent_onto_name() -> None:
" - the_only_resource\n\n"
"---------------------------------------\n\n"
)
with pytest.raises(UserError, match=expected):
with pytest.raises(InputError, match=expected):
do_xml_consistency_check_with_ontology(ontology_client, root)


Expand Down

0 comments on commit e6b0e7d

Please sign in to comment.