Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enh/attributes in schema validation #822

Merged
merged 9 commits into from
Oct 13, 2022
22 changes: 19 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,24 @@ 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
# instanciated or imported from a file
MBueschelberger marked this conversation as resolved.
Show resolved Hide resolved
if rel_entity in origin_cuds.get_attributes():
actual_cardinality = 1
else:
actual_cardinality = 0
MBueschelberger marked this conversation as resolved.
Show resolved Hide resolved
# TODO: how to apply in Simphony >= v4 for multiple data properties?
MBueschelberger marked this conversation as resolved.
Show resolved Hide resolved
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)
kysrpex marked this conversation as resolved.
Show resolved Hide resolved

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