Skip to content

Commit

Permalink
Merge pull request #45 from ternaustralia/edmond/optimise-resource-json
Browse files Browse the repository at this point in the history
Optimise resource json view
  • Loading branch information
edmondchuc authored Jan 5, 2023
2 parents f01c72e + a5c8d4f commit d912e0d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 42 deletions.
2 changes: 1 addition & 1 deletion src/linkeddata_api/domain/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class SubjectPredicates(BaseModel):

class PredicateObjects(BaseModel):
predicate: URI
objects: list[Union[URI, Literal]]
objects: set[Union[URI, Literal]]


class Resource(BaseModel):
Expand Down
77 changes: 36 additions & 41 deletions src/linkeddata_api/domain/viewer/resource/json/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import logging
from typing import Union
from collections import defaultdict

from rdflib import RDF

Expand Down Expand Up @@ -130,36 +132,29 @@ def get(uri: str, sparql_endpoint: str) -> domain.schema.Resource:

result = data.sparql.post(query, sparql_endpoint).json()

try:
result = _add_rows_for_rdf_list_items(result, uri, sparql_endpoint)
label = domain.label.get(uri, sparql_endpoint) or uri
types, properties = _get_types_and_properties(result, uri, sparql_endpoint)

profile = ""
if exists_uri("https://w3id.org/tern/ontologies/tern/MethodCollection", types):
profile = "https://w3id.org/tern/ontologies/tern/MethodCollection"
properties = method_profile(properties)
elif exists_uri("https://w3id.org/tern/ontologies/tern/Method", types):
profile = "https://w3id.org/tern/ontologies/tern/Method"
properties = method_profile(properties)

# incoming_properties = _get_incoming_properties(uri, sparql_endpoint)

return domain.schema.Resource(
uri=uri,
profile=profile,
label=label,
types=types,
properties=properties,
# incoming_properties=incoming_properties,
incoming_properties=[], # TODO:
)
except data.exceptions.SPARQLNotFoundError as err:
raise err
except Exception as err:
raise data.exceptions.SPARQLResultJSONError(
f"Unexpected SPARQL result.\n{result}\n{err}"
) from err
result = _add_rows_for_rdf_list_items(result, uri, sparql_endpoint)
label = domain.label.get(uri, sparql_endpoint) or uri
types, properties = _get_types_and_properties(result, uri, sparql_endpoint)

profile = ""
if exists_uri("https://w3id.org/tern/ontologies/tern/MethodCollection", types):
profile = "https://w3id.org/tern/ontologies/tern/MethodCollection"
properties = method_profile(properties)
elif exists_uri("https://w3id.org/tern/ontologies/tern/Method", types):
profile = "https://w3id.org/tern/ontologies/tern/Method"
properties = method_profile(properties)

# incoming_properties = _get_incoming_properties(uri, sparql_endpoint)

return domain.schema.Resource(
uri=uri,
profile=profile,
label=label,
types=types,
properties=properties,
# incoming_properties=incoming_properties,
incoming_properties=[], # TODO:
)


@log_time
Expand Down Expand Up @@ -231,7 +226,9 @@ def _get_types_and_properties(
) -> tuple[list[domain.schema.URI], list[domain.schema.PredicateObjects]]:

types: list[domain.schema.URI] = []
properties: list[domain.schema.PredicateObjects] = []
properties: dict[
str, set[Union[domain.schema.URI, domain.schema.Literal]]
] = defaultdict(set)

# An index of URIs with label values.
uri_label_index = _get_uri_label_index(result, uri, sparql_endpoint)
Expand Down Expand Up @@ -318,17 +315,14 @@ def _get_types_and_properties(
f"Expected type to be uri or literal but got {row['o']['type']}"
)

found = False
for p in properties:
if p.predicate.value == predicate.value:
found = True
if item not in p.objects:
p.objects.append(item)
# Use dict and set for performance
properties[predicate].add(item)

if not found:
properties.append(
domain.schema.PredicateObjects(predicate=predicate, objects=[item])
)
# Convert to a list of PredicateObjects
properties = [
domain.schema.PredicateObjects(predicate=k, objects=v)
for k, v in properties.items()
]

# Duplicates may occur due to processing RDF lists.
# Remove duplicates, if any.
Expand All @@ -341,6 +335,7 @@ def _get_types_and_properties(
# Sort all property objects by label.
properties.sort(key=lambda x: x.predicate.label)
for property_ in properties:
property_.objects = list(property_.objects)
property_.objects.sort(key=sort_property_objects)

return types, properties

0 comments on commit d912e0d

Please sign in to comment.