Skip to content

Commit

Permalink
Enh/attributes in schema validation (#822)
Browse files Browse the repository at this point in the history
* add checking of data properties (attributes) in schema validation

* Use FOAF from web archive
  • Loading branch information
MBueschelberger authored Oct 13, 2022
1 parent 0c9d944 commit 231d72c
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 3 deletions.
20 changes: 17 additions & 3 deletions osp/core/utils/schema_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import yaml

from osp.core.namespaces import get_entity
from osp.core.ontology import OntologyAttribute, OntologyRelationship

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -97,9 +98,22 @@ def _load_data_model_from_yaml(data_model_file):

def _check_cuds_object_cardinality(origin_cuds, dest_oclass, rel, constraints):

actual_cardinality = len(
origin_cuds.get(rel=get_entity(rel), oclass=get_entity(dest_oclass))
)
rel_entity = get_entity(rel)

if type(rel_entity) == OntologyRelationship:
actual_cardinality = len(
origin_cuds.get(rel=rel_entity, oclass=get_entity(dest_oclass))
)
elif type(rel_entity) == OntologyAttribute:
# No datatype checking since this is already done when Cuds are
# instantiated or imported from a file
actual_cardinality = (
1 if rel_entity in origin_cuds.get_attributes() else 0
)
else:
raise ConsistencyError(
f"Relation '{rel}' not supported for {origin_cuds.oclass}."
)

min, max = _interpret_cardinality_value_from_constraints(constraints)
if actual_cardinality < min or actual_cardinality > max:
Expand Down
7 changes: 7 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@ def test_validate_tree_against_schema(self):
"test_validation_schema_city_with_optional_subtree.yml",
)

schema_file_with_attribute = os.path.join(
os.path.dirname(__file__),
"test_validation_schema_city_with_attribute.yml",
)

c = city.City(name="freiburg")

# empty city is not valid
Expand Down Expand Up @@ -188,6 +193,8 @@ def test_validate_tree_against_schema(self):
schema_file_with_missing_entity,
)

validate_tree_against_schema(c, schema_file_with_attribute)

def test_branch(self):
"""Test the branch function."""
x = branch(
Expand Down
33 changes: 33 additions & 0 deletions tests/test_validation_schema_city_with_attribute.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
version: "1.0.0"

oclass: city.City

model:
city.City:
city.name:
STRING:
cardinality: 1
city.hasInhabitant:
city.Citizen:
cardinality: 1-2
city.hasPart:
city.Neighborhood:
cardinality: 1

city.Neighborhood:
city.name:
STRING:
cardinality: 1
city.hasPart:
city.Street:
cardinality: 1+

city.Street:
city.name:
STRING:
cardinality: 1

city.Citizen:
city.name:
STRING:
cardinality: 1

0 comments on commit 231d72c

Please sign in to comment.