Skip to content

Commit

Permalink
complete mvp
Browse files Browse the repository at this point in the history
  • Loading branch information
recalcitrantsupplant committed Nov 14, 2024
1 parent f3984e6 commit c803cdb
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 18 deletions.
39 changes: 33 additions & 6 deletions prez/dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
import httpx
from fastapi import Depends, HTTPException, Request
from pyoxigraph import Store
from rdflib import Dataset, URIRef, SKOS, RDF, DCTERMS
from rdflib import Dataset, URIRef, SKOS, RDF, DCTERMS, Literal
from rdflib import RDF, SKOS, Dataset, URIRef
from sparql_grammar_pydantic import IRI, Var
from sparql_grammar_pydantic import IRI, Var, GroupGraphPattern, GroupGraphPatternSub, TriplesBlock, \
GroupOrUnionGraphPattern

from prez.cache import (
annotations_store,
Expand Down Expand Up @@ -41,7 +42,7 @@
from prez.services.query_generation.cql import CQLParser
from prez.services.query_generation.search_default import SearchQueryRegex
from prez.services.query_generation.search_fuseki_fts import SearchQueryFusekiFTS
from prez.services.query_generation.shacl import NodeShape
from prez.services.query_generation.shacl import NodeShape, PropertyShape
from prez.services.query_generation.sparql_escaping import escape_for_lucene_and_sparql


Expand Down Expand Up @@ -226,17 +227,43 @@ async def generate_search_query(
predicates = predicates if predicates else settings.search_predicates
shacl_shapes = await get_jena_fts_shacl_predicates(system_repo)
shacl_shape_ids = list([str(x) for x in shacl_shapes.objects(subject=None, predicate=DCTERMS.identifier)])
shacl_predicates = []
tssp_lists = []
tss_list = []
non_shacl_predicates = []
i = 0
for pred in predicates:
if pred in shacl_shape_ids:
shacl_predicates.append(pred)
shacl_shape_uri = shacl_shapes.value(subject=None, predicate=DCTERMS.identifier, object=Literal(pred))
shacl_shape_g = shacl_shapes.cbd(shacl_shape_uri)
ps = PropertyShape(
uri=shacl_shape_uri,
graph=shacl_shape_g,
kind="fts",
focus_node=Var(value="focus_node"),
shape_number=i
)
tssp_lists.append(ps.tssp_list)
tss_list.extend(ps.tss_list)
i += 1
else:
non_shacl_predicates.append(pred)

ggp_list = []
for inner_list in tssp_lists:
ggp_list.append(
GroupGraphPattern(
content=GroupGraphPatternSub(
triples_block=TriplesBlock.from_tssp_list(inner_list)
)
)
)
gougp = GroupOrUnionGraphPattern(group_graph_patterns=ggp_list)

return SearchQueryFusekiFTS(
term=escaped_term,
non_shacl_predicates=non_shacl_predicates,
shacl_predicates=shacl_predicates,
shacl_gougp=gougp,
shacl_tss_list=tss_list,
limit=limit,
offset=offset
)
Expand Down
32 changes: 20 additions & 12 deletions prez/services/query_generation/search_fuseki_fts.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ def __init__(
limit: int,
offset: int,
non_shacl_predicates: list[str] | None = None,
shacl_predicates: list[str] | None = None
shacl_gougp: GroupOrUnionGraphPattern | None = None,
shacl_tss_list: list[TriplesSameSubject] | None = None
):
limit += 1 # increase the limit by one, so we know if there are further pages of results.
# join search terms with '+' for better results
Expand Down Expand Up @@ -125,10 +126,20 @@ def __init__(
for p, v in ct_map.items()
]

if shacl_tss_list:
construct_tss_list.extend(shacl_tss_list)

construct_template = ConstructTemplate(
construct_triples=ConstructTriples.from_tss_list(construct_tss_list)
)
gpnt_or_triples_list = [TriplesBlock(

gpnt_or_triples_list = []
if shacl_gougp:
gpnt = GraphPatternNotTriples(
content=shacl_gougp
)
gpnt_or_triples_list.append(gpnt)
gpnt_or_triples_list.extend([TriplesBlock(
triples=TriplesSameSubjectPath(
content=(
TriplesNodePath(
Expand Down Expand Up @@ -225,10 +236,7 @@ def __init__(
),
)
)
)]
# if property_shape:
# pass
# # if property_shape.
)])
where_clause = WhereClause(
group_graph_pattern=GroupGraphPattern(
content=SubSelect(
Expand Down Expand Up @@ -313,7 +321,7 @@ def __init__(
)
)

# logger.debug(f"constructed Fuseki FTS query:\n{self.query}")
# logger.debug(f"constructed Fuseki FTS query:\n{self}")
super().__init__(
construct_template=construct_template,
where_clause=where_clause,
Expand All @@ -330,26 +338,26 @@ def order_by_direction(self):

@property
def limit(self):
return self.query.solution_modifier.limit_offset.limit_clause.limit
return self.solution_modifier.limit_offset.limit_clause.limit

@property
def offset(self):
return self.query.solution_modifier.limit_offset.offset_clause.offset
return self.solution_modifier.limit_offset.offset_clause.offset

@property
def tss_list(self):
return self.query.construct_template.construct_triples.to_tss_list()
return self.construct_template.construct_triples.to_tss_list()

@property
def inner_select_vars(self):
return (
self.query.where_clause.group_graph_pattern.content.select_clause.variables_or_all
self.where_clause.group_graph_pattern.content.select_clause.variables_or_all
)

@property
def inner_select_gpnt(self):
inner_ggp = (
self.query.where_clause.group_graph_pattern.content.where_clause.group_graph_pattern
self.where_clause.group_graph_pattern.content.where_clause.group_graph_pattern
)
return GraphPatternNotTriples(
content=GroupOrUnionGraphPattern(group_graph_patterns=[inner_ggp])
Expand Down

0 comments on commit c803cdb

Please sign in to comment.