Skip to content

Commit

Permalink
change pyshacl engine when validating
Browse files Browse the repository at this point in the history
  • Loading branch information
gtfierro committed Jan 15, 2025
1 parent bc17408 commit 43aa1b9
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions buildingmotif/api/views/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from rdflib.plugins.parsers.notation3 import BadSyntax
from sqlalchemy.orm.exc import NoResultFound

from buildingmotif import get_building_motif
from buildingmotif.api.serializers.model import serialize
from buildingmotif.dataclasses import Library, Model, ShapeCollection

Expand Down Expand Up @@ -163,9 +164,13 @@ def validate_model(models_id: int) -> flask.Response:
except NoResultFound:
return {"message": f"No model with id {models_id}"}, status.HTTP_404_NOT_FOUND

# we will read the shape collections from the input
shape_collections = []

# no body provided -- default to model manifest and default SHACL engine
# get shacl_engine from the query params, default to pyshacl
shacl_engine = request.args.get("shacl_engine", "pyshacl")

# no body provided -- default to model manifest
if request.content_length is None:
shape_collections = [model.get_manifest()]
else:
Expand All @@ -182,7 +187,6 @@ def validate_model(models_id: int) -> flask.Response:

if body is not None and not isinstance(body, dict):
return {"message": "body is not dict"}, status.HTTP_400_BAD_REQUEST
shape_collections = []
body = body if body is not None else {}
nonexistent_libraries = []
for library_id in body.get("library_ids", []):
Expand All @@ -196,9 +200,17 @@ def validate_model(models_id: int) -> flask.Response:
"message": f"Libraries with ids {nonexistent_libraries} do not exist"
}, status.HTTP_400_BAD_REQUEST

# if shape_collections is empty, model.validate will default
# to the model's manifest
vaildation_context = model.validate(shape_collections)
# temporarily change the SHACL engine
bm = get_building_motif()
old_shacl_engine = bm.shacl_engine
bm.shacl_engine = shacl_engine

# if shape_collections is empty, model.validate will default to the model's manifest
vaildation_context = model.validate(
shape_collections, error_on_missing_imports=False
)
# change the SHACL engine back
bm.shacl_engine = old_shacl_engine

return {
"message": vaildation_context.report_string,
Expand Down

0 comments on commit 43aa1b9

Please sign in to comment.