Skip to content

Commit

Permalink
Update documentation.
Browse files Browse the repository at this point in the history
Remove commented code.
Fix object tests.
  • Loading branch information
recalcitrantsupplant committed Aug 9, 2023
1 parent 554352f commit c61ca27
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 39 deletions.
1 change: 1 addition & 0 deletions prez/reference_data/endpoints/spaceprez_endpoints.ttl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ endpoint:dataset a ont:Endpoint ;
ont:parentEndpoint endpoint:dataset-listing ;
ont:deliversClasses dcat:Dataset ;
ont:endpointTemplate "/s/datasets/$object" ;
ont:FocustoChildRelation rdfs:member ;
.

endpoint:feature-collection-listing a ont:Endpoint ;
Expand Down
18 changes: 9 additions & 9 deletions prez/routers/object.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ async def object_function(
# at present, the behaviour for which should be chosen (or if one should be chosen at all) has not been defined.
object_item.selected_class = None
endpoint_to_relations = get_endpoint_info_for_classes(object_item.classes)

relationship_query = generate_relationship_query(
object_item.uri, endpoint_to_relations
)
Expand Down Expand Up @@ -122,14 +121,15 @@ def get_endpoint_info_for_classes(classes) -> dict:
endpoint_query = get_endpoint_template_queries(classes)
results = endpoints_graph_cache.query(endpoint_query)
endpoint_to_relations = {}
for result in results.bindings:
endpoint_template = result["endpointTemplate"]
relation = result["relation"]
direction = result["direction"]
if endpoint_template not in endpoint_to_relations:
endpoint_to_relations[endpoint_template] = [(relation, direction)]
else:
endpoint_to_relations[endpoint_template].append((relation, direction))
if results.bindings != [{}]:
for result in results.bindings:
endpoint_template = result["endpointTemplate"]
relation = result["relation"]
direction = result["direction"]
if endpoint_template not in endpoint_to_relations:
endpoint_to_relations[endpoint_template] = [(relation, direction)]
else:
endpoint_to_relations[endpoint_template].append((relation, direction))
return endpoint_to_relations


Expand Down
61 changes: 32 additions & 29 deletions prez/sparql/objects_listings.py
Original file line number Diff line number Diff line change
Expand Up @@ -868,35 +868,37 @@ def generate_mediatype_if_statements(requested_mediatypes: list):
def get_endpoint_template_queries(classes: FrozenSet[URIRef]):
query = f"""PREFIX ont: <https://prez.dev/ont/>
SELECT ?classes ?parent_endpoint ?endpoint ?relation ?direction ?endpointTemplate
(count(?intermediate) as ?distance) WHERE {{
SELECT ?classes ?parent_endpoint ?endpoint ?relation ?direction ?endpointTemplate
(count(?intermediate) as ?distance) WHERE {{
VALUES ?classes {{ {" ".join('<' + klass + '>' for klass in classes)} }}
?endpoint a ont:Endpoint ;
ont:endpointTemplate ?endpointTemplate ;
ont:deliversClasses ?classes .
?endpoint ont:parentEndpoint* ?intermediate .
?intermediate ont:parentEndpoint* ?parent_endpoint .
OPTIONAL {{
?parent_endpoint ont:ParentToFocusRelation ?relation .
BIND ("parent_to_focus" AS ?direction)
}}
OPTIONAL {{
?parent_endpoint ont:FocusToParentRelation ?relation .
BIND ("focus_to_parent" AS ?direction)
}}
FILTER (BOUND(?relation))
}} GROUP BY ?endpoint ?parent_endpoint ?relation ?direction ?classes ?endpointTemplate
ORDER BY ?endpoint DESC(?distance)"""
{{
?endpoint a ont:Endpoint ;
ont:endpointTemplate ?endpointTemplate ;
ont:deliversClasses ?classes .
}}
UNION
{{
?endpoint a ont:Endpoint ;
ont:endpointTemplate ?endpointTemplate ;
ont:deliversClasses ?classes .
?endpoint ont:parentEndpoint* ?intermediate .
?intermediate ont:parentEndpoint* ?parent_endpoint .
OPTIONAL {{
?parent_endpoint ont:ParentToFocusRelation ?relation .
BIND ("parent_to_focus" AS ?direction)
}}
OPTIONAL {{
?parent_endpoint ont:FocusToParentRelation ?relation .
BIND ("focus_to_parent" AS ?direction)
}}
FILTER (BOUND(?relation))
}}
}} GROUP BY ?endpoint ?parent_endpoint ?relation ?direction ?classes ?endpointTemplate
ORDER BY ?endpoint DESC(?distance)
"""
return query


def generate_relationship_query(
uri: URIRef, endpoint_to_relations: Dict[URIRef, List[Tuple[URIRef, URIRef]]]
):
for endpoint, relations in endpoint_to_relations.items():
construct_subquery = generate_relationship_query(uri, endpoint, relations)


def generate_relationship_query(
uri: URIRef, endpoint_to_relations: Dict[URIRef, List[Tuple[URIRef, Literal]]]
):
Expand All @@ -908,10 +910,11 @@ def generate_relationship_query(
for i, relation in enumerate(relations):
predicate, direction = relation
parent = "?parent_" + str(i + 1)
if direction == Literal("parent_to_focus"):
subquery += f"{parent} <{predicate}> {previous_uri} .\n"
else: # assuming the direction is "focus_to_parent"
subquery += f"{previous_uri} <{predicate}> {parent} .\n"
if predicate:
if direction == Literal("parent_to_focus"):
subquery += f"{parent} <{predicate}> {previous_uri} .\n"
else: # assuming the direction is "focus_to_parent"
subquery += f"{previous_uri} <{predicate}> {parent} .\n"
previous_uri = parent
subquery += "}}"
subqueries.append(subquery)
Expand Down
7 changes: 7 additions & 0 deletions tests/data/object/expected_responses/fc.ttl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@prefix geo: <http://www.opengis.net/ont/geosparql#> .
@prefix ns1: <https://prez.dev/> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .

<https://test/feature-collection> a geo:FeatureCollection ;
rdfs:member <http://resource.geosciml.org/classifier/cgi/contacttype/alteration_facies_contact> ;
ns1:link "/s/datasets/ns1:dataset/collections/ns1:feature-collection" .
13 changes: 13 additions & 0 deletions tests/data/object/expected_responses/feature.ttl

Large diffs are not rendered by default.

24 changes: 23 additions & 1 deletion tests/object/test_endpoints_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,29 @@ def dataset_uri(test_client):
return g.value(None, RDF.type, DCAT.Dataset)


def test_object_endpoint(test_client, dataset_uri):
def test_object_endpoint_sp_dataset(test_client, dataset_uri):
with test_client as client:
r = client.get(f"/object?uri={dataset_uri}")
assert r.status_code == 200


def test_feature_collection(test_client):
with test_client as client:
r = client.get(f"/object?uri=https://test/feature-collection")
response_graph = Graph().parse(data=r.text)
expected_graph = Graph().parse(
Path(__file__).parent / "../data/object/expected_responses/fc.ttl"
)
assert response_graph.isomorphic(expected_graph)


def test_feature(test_client):
with test_client as client:
r = client.get(
f"/object?uri=https://linked.data.gov.au/datasets/geofabric/hydroid/102208962"
)
response_graph = Graph().parse(data=r.text)
expected_graph = Graph().parse(
Path(__file__).parent / "../data/object/expected_responses/feature.ttl"
)
assert response_graph.isomorphic(expected_graph)

0 comments on commit c61ca27

Please sign in to comment.