From 40225bd1cbf4210bd940de2e62ed4e3e01cc5275 Mon Sep 17 00:00:00 2001 From: Edmond Chuc Date: Mon, 7 Aug 2023 10:19:10 +1000 Subject: [PATCH 1/2] Add annotations from profile until graph stops expanding --- prez/renderers/renderer.py | 56 ++++++++++++++------------------------ 1 file changed, 21 insertions(+), 35 deletions(-) diff --git a/prez/renderers/renderer.py b/prez/renderers/renderer.py index adb76d22..a4d3f1b5 100644 --- a/prez/renderers/renderer.py +++ b/prez/renderers/renderer.py @@ -72,18 +72,7 @@ async def return_rdf(graph, mediatype, profile_headers): return StreamingResponse(content=obj, media_type=mediatype, headers=profile_headers) -async def return_annotated_rdf( - graph: Graph, - profile_headers, - profile, - predicates_for_link_addition: dict, - mediatype="text/anot+turtle", -): - from prez.cache import tbox_cache - - non_anot_mediatype = mediatype.replace("anot+", "") - - cache = tbox_cache +async def get_annotations_graph(profile, graph, cache): profile_annotation_props = get_annotation_predicates(profile) queries_for_uncached, annotations_graph = await get_annotation_properties( graph, **profile_annotation_props @@ -98,36 +87,33 @@ async def return_annotated_rdf( annotations_graph += anots_from_triplestore cache += anots_from_triplestore - # TODO: this portion of the code requires refactoring. - # Duplicated fragment code below is required to ensure new predicates and values - # defined in the profiles get added to the `graph` and the labels of those - # values also get added to the `graph`. + return annotations_graph - # start of duplicated code fragment - graph += annotations_graph - profile_annotation_props = get_annotation_predicates(profile) - queries_for_uncached, annotations_graph = await get_annotation_properties( - graph, **profile_annotation_props - ) +async def return_annotated_rdf( + graph: Graph, + profile_headers, + profile, + predicates_for_link_addition: dict, + mediatype="text/anot+turtle", +): + from prez.cache import tbox_cache - if queries_for_uncached is None: - anots_from_triplestore = Graph() - else: - anots_from_triplestore = await queries_to_graph([queries_for_uncached]) + non_anot_mediatype = mediatype.replace("anot+", "") - if len(anots_from_triplestore) > 1: - annotations_graph += anots_from_triplestore - cache += anots_from_triplestore - # end duplicated code fragment + cache = tbox_cache + + previous_triples_count = len(graph) + + while True: + graph += await get_annotations_graph(profile, graph, cache) + if len(graph) == previous_triples_count: + break + previous_triples_count = len(graph) generate_prez_links(graph, predicates_for_link_addition) - obj = io.BytesIO( - (graph + annotations_graph).serialize( - format=non_anot_mediatype, encoding="utf-8" - ) - ) + obj = io.BytesIO(graph.serialize(format=non_anot_mediatype, encoding="utf-8")) return StreamingResponse( content=obj, media_type=non_anot_mediatype, headers=profile_headers ) From 0303b6a22312e89be036f7b4f2564cdf35fcdae3 Mon Sep 17 00:00:00 2001 From: Edmond Chuc Date: Mon, 7 Aug 2023 10:19:29 +1000 Subject: [PATCH 2/2] Fix tests for new annotations expansion behaviour --- .../vocprez/expected_responses/collection_listing_anot.ttl | 5 +++++ tests/data/vocprez/expected_responses/vocab_listing_anot.ttl | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/tests/data/vocprez/expected_responses/collection_listing_anot.ttl b/tests/data/vocprez/expected_responses/collection_listing_anot.ttl index ea27d347..3f2a822e 100644 --- a/tests/data/vocprez/expected_responses/collection_listing_anot.ttl +++ b/tests/data/vocprez/expected_responses/collection_listing_anot.ttl @@ -12,6 +12,11 @@ PREFIX xsd: ns1:link "/v/collection/brhl-prps:pggd" ; . +dcterms:description + rdfs:label "Description"@en ; + dcterms:description "Description may include but is not limited to: an abstract, a table of contents, a graphical representation, or a free-text account of the resource."@en ; +. + dcterms:provenance rdfs:label "Provenance"@en ; dcterms:description "The statement may include a description of any changes successive custodians made to the resource."@en ; diff --git a/tests/data/vocprez/expected_responses/vocab_listing_anot.ttl b/tests/data/vocprez/expected_responses/vocab_listing_anot.ttl index 1ede6a7a..ff6a8fa5 100644 --- a/tests/data/vocprez/expected_responses/vocab_listing_anot.ttl +++ b/tests/data/vocprez/expected_responses/vocab_listing_anot.ttl @@ -40,6 +40,11 @@ PREFIX xsd: ns2:link "/v/vocab/def2:borehole-purpose-no-children" ; . +dcterms:description + rdfs:label "Description"@en ; + dcterms:description "Description may include but is not limited to: an abstract, a table of contents, a graphical representation, or a free-text account of the resource."@en ; +. + dcterms:provenance rdfs:label "Provenance"@en ; dcterms:description "The statement may include a description of any changes successive custodians made to the resource."@en ;