From c83613f650b6c4120ce4eb48e23bbf32b0f3162c Mon Sep 17 00:00:00 2001 From: Kevin Stadler Date: Sun, 8 Dec 2024 11:21:54 +0100 Subject: [PATCH] feat: use QueryParameters --- pyproject.toml | 2 +- releven.py | 27 +++++++++------- releven_location.py | 16 ++++++---- releven_person.py | 61 +++++++++++++++++++++++++++-------- releven_person.rq | 69 ++++++++++++++++++++++++++++++++++++--- releven_written_work.py | 71 +++++++---------------------------------- releven_written_work.rq | 31 +----------------- 7 files changed, 150 insertions(+), 127 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 4a5362d..1dd0524 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -6,7 +6,7 @@ authors = ["Kevin Stadler "] [tool.poetry.dependencies] python = "^3.12" -rdfproxy = {git = "https://github.com/acdh-oeaw/rdfproxy.git", branch = "lupl/model-bool-feature"} +rdfproxy = {git = "https://github.com/acdh-oeaw/rdfproxy.git", branch = "lupl/ungrouped-ordering"} fastapi = {extras = ["standard"], version = "^0.115.4"} gitpython = "^3.1.43" diff --git a/releven.py b/releven.py index 99f7759..2365ca5 100644 --- a/releven.py +++ b/releven.py @@ -3,7 +3,7 @@ from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware from git import Repo -from rdfproxy import Page, SPARQLModelAdapter +from rdfproxy import Page, QueryParameters, SPARQLModelAdapter from releven_location import Location from releven_person import Person from releven_written_work import WrittenWork @@ -36,28 +36,31 @@ def location(page: int = 1, size: int = 10) -> Page[Location]: .replace("\n ", " "), model=Location, ) - return adapter.query(page=page, size=size) + parameters = QueryParameters(page=page, size=size) + return adapter.query(parameters) -@app.get("/written_work") -def written_work(page: int = 1, size: int = 10) -> Page[WrittenWork]: +@app.get("/person") +def person(page: int = 1, size: int = 10) -> Page[Person]: adapter = SPARQLModelAdapter( target="https://graphdb.r11.eu/repositories/RELEVEN", - query=open(f"{path.dirname(path.realpath(__file__))}/releven_written_work.rq") + query=open(f"{path.dirname(path.realpath(__file__))}/releven_person.rq") .read() .replace("\n ", " "), - model=WrittenWork, + model=Person, ) - return adapter.query(page=page, size=size) + parameters = QueryParameters(page=page, size=size) + return adapter.query(parameters) -@app.get("/person") -def person(page: int = 1, size: int = 10) -> Page[Person]: +@app.get("/written_work") +def written_work(page: int = 1, size: int = 10) -> Page[WrittenWork]: adapter = SPARQLModelAdapter( target="https://graphdb.r11.eu/repositories/RELEVEN", - query=open(f"{path.dirname(path.realpath(__file__))}/releven_person.rq") + query=open(f"{path.dirname(path.realpath(__file__))}/releven_written_work.rq") .read() .replace("\n ", " "), - model=Person, + model=WrittenWork, ) - return adapter.query(page=page, size=size) + parameters = QueryParameters(page=page, size=size) + return adapter.query(parameters) diff --git a/releven_location.py b/releven_location.py index a149b3a..d3f1db3 100644 --- a/releven_location.py +++ b/releven_location.py @@ -1,15 +1,17 @@ from typing import Annotated -from pydantic import AnyUrl, BaseModel -from rdfproxy import SPARQLBinding +from pydantic import AnyUrl, BaseModel, Field # noqa: F401 +from rdfproxy import ConfigDict, SPARQLBinding class Location(BaseModel): - class Config: - title = "Location" - model_bool = "id" - - id: Annotated[AnyUrl | None, SPARQLBinding("location")] = None + model_config = ConfigDict( + title="Location", + model_bool="id", + ) + id: Annotated[AnyUrl | None, SPARQLBinding("location")] = Field( + default=None, exclude=True + ) location_descriptive_name: Annotated[ str | None, SPARQLBinding("location__location_descriptive_name") ] = None diff --git a/releven_person.py b/releven_person.py index a6bc9e9..8f5bae9 100644 --- a/releven_person.py +++ b/releven_person.py @@ -1,17 +1,17 @@ from typing import Annotated -from pydantic import AnyUrl, BaseModel -from rdfproxy import SPARQLBinding +from pydantic import AnyUrl, BaseModel, Field # noqa: F401 +from rdfproxy import ConfigDict, SPARQLBinding class Person_PersonAppellationAssertion(BaseModel): - class Config: - title = "Name(s) in the sources" - model_bool = "id" - + model_config = ConfigDict( + title="Name(s) in the sources", + model_bool="id", + ) id: Annotated[ AnyUrl | None, SPARQLBinding("person__person_appellation_assertion") - ] = None + ] = Field(default=None, exclude=True) person_appellation_is: Annotated[ str | None, SPARQLBinding("person__person_appellation_assertion__person_appellation_is"), @@ -19,16 +19,51 @@ class Config: class Person(BaseModel): - class Config: - title = "Person" - model_bool = "id" - group_by = "person" - - id: Annotated[AnyUrl | None, SPARQLBinding("person")] = None + model_config = ConfigDict( + title="Person", + model_bool="id", + group_by="id", + ) + id: Annotated[AnyUrl | None, SPARQLBinding("person")] = Field( + default=None, exclude=True + ) + person_id_assignment: Annotated[ + list[AnyUrl], SPARQLBinding("person__person_id_assignment") + ] person_appellation_assertion: Annotated[ list[Person_PersonAppellationAssertion], SPARQLBinding("person__person_appellation_assertion"), ] + person_gender_assertion: Annotated[ + list[AnyUrl], SPARQLBinding("person__person_gender_assertion") + ] + person_ethnicity_assertion: Annotated[ + list[AnyUrl], SPARQLBinding("person__person_ethnicity_assertion") + ] person_descriptive_name: Annotated[ list[str], SPARQLBinding("person__person_descriptive_name") ] + person_possession_assertion: Annotated[ + list[AnyUrl], SPARQLBinding("person__person_possession_assertion") + ] + person_status_assertion: Annotated[ + list[AnyUrl], SPARQLBinding("person__person_status_assertion") + ] + person_religion_assertion: Annotated[ + list[AnyUrl], SPARQLBinding("person__person_religion_assertion") + ] + person_occupation_assertion: Annotated[ + list[AnyUrl], SPARQLBinding("person__person_occupation_assertion") + ] + person_language_assertion: Annotated[ + list[AnyUrl], SPARQLBinding("person__person_language_assertion") + ] + person_kinship_assertion: Annotated[ + list[AnyUrl], SPARQLBinding("person__person_kinship_assertion") + ] + birth_circumstances_claim: Annotated[ + AnyUrl | None, SPARQLBinding("person__birth_circumstances_claim") + ] = None + person_death_assertion: Annotated[ + AnyUrl | None, SPARQLBinding("person__person_death_assertion") + ] = None diff --git a/releven_person.rq b/releven_person.rq index 98a636f..00c6c85 100644 --- a/releven_person.rq +++ b/releven_person.rq @@ -7,24 +7,85 @@ PREFIX r11pros: SELECT ?person + ?person__person_id_assignment ?person__person_appellation_assertion ?person__person_appellation_assertion__person_appellation_is + ?person__person_gender_assertion + ?person__person_ethnicity_assertion ?person__person_descriptive_name + ?person__person_possession_assertion + ?person__person_status_assertion + ?person__person_religion_assertion + ?person__person_occupation_assertion + ?person__person_language_assertion + ?person__person_kinship_assertion + ?person__birth_circumstances_claim + ?person__person_death_assertion WHERE { ?person a crm:E21_Person. + OPTIONAL { + ?person__person_id_assignment a crm:E15_Identifier_Assignment. + ?person ^crm:P140_assigned_attribute_to ?person__person_id_assignment. + } + OPTIONAL { ?person__person_appellation_assertion a star:E13_crm_P1. - ?person__person_appellation_assertion crm:P140_assigned_attribute_to ?person. + ?person ^crm:P140_assigned_attribute_to ?person__person_appellation_assertion. - ?person__person_appellation_assertion___pai crm:P140_assigned_attribute_to ?person__person_appellation_assertion. - ?person__person_appellation_assertion___pai crm:P141_assigned ?person__person_appellation_assertion___pai___pai. - ?person__person_appellation_assertion___pai___pai crm:P190_has_symbolic_content ?person__person_appellation_assertion__person_appellation_is. + ?person__person_appellation_assertion crm:P141_assigned ?person__person_appellation_assertion___pai. + ?person__person_appellation_assertion___pai crm:P190_has_symbolic_content ?person__person_appellation_assertion__person_appellation_is. + } + + OPTIONAL { + ?person__person_gender_assertion a star:E13_crm_P41. + ?person ^crm:P141_assigned ?person__person_gender_assertion. + } + + OPTIONAL { + ?person__person_ethnicity_assertion a star:E13_crm_P107. + ?person ^crm:P141_assigned ?person__person_ethnicity_assertion. } OPTIONAL { ?person crm:P3_has_note ?person__person_descriptive_name. } + + OPTIONAL { + ?person__person_possession_assertion a star:E13_crm_P51. + ?person ^crm:P141_assigned ?person__person_possession_assertion. + } + + OPTIONAL { + ?person__person_status_assertion a star:E13_sdhss_P26. + ?person ^crm:P141_assigned ?person__person_status_assertion. + } + + OPTIONAL { + ?person__person_religion_assertion a star:E13_sdhss_P36. + ?person ^crm:P141_assigned ?person__person_religion_assertion. + } + + OPTIONAL { + ?person__person_occupation_assertion a star:E13_sdhss_P13. + ?person ^crm:P141_assigned ?person__person_occupation_assertion. + } + + OPTIONAL { + ?person__person_language_assertion a star:E13_sdhss_P38. + ?person ^crm:P140_assigned_attribute_to ?person__person_language_assertion. + } + + OPTIONAL { + ?person__person_kinship_assertion a star:E13_sdhss_P17. + ?person ^crm:P141_assigned ?person__person_kinship_assertion. + } + + ?person__birth_circumstances_claim a star:E13_crm_P98. + ?person ^crm:P141_assigned ?person__birth_circumstances_claim. + + ?person__person_death_assertion a star:E13_crm_P100. + ?person ^crm:P141_assigned ?person__person_death_assertion. } diff --git a/releven_written_work.py b/releven_written_work.py index 65e74f5..33dd920 100644 --- a/releven_written_work.py +++ b/releven_written_work.py @@ -1,70 +1,21 @@ from typing import Annotated -from pydantic import AnyUrl, BaseModel -from rdfproxy import SPARQLBinding - - -class WrittenWork_WorkCreationAssertion_WorkCreation(BaseModel): - class Config: - title = "text_creation" - model_bool = "id" - group_by = "written_work__work_creation_assertion__work_creation" - - id: Annotated[ - AnyUrl | None, - SPARQLBinding("written_work__work_creation_assertion__work_creation"), - ] = None - work_creation_6464e7f7408f5: Annotated[ - list[AnyUrl], - SPARQLBinding( - "written_work__work_creation_assertion__work_creation__work_creation_6464e7f7408f5" - ), - ] - text_creation_timeframe_assertio: Annotated[ - AnyUrl | None, - SPARQLBinding( - "written_work__work_creation_assertion__work_creation__text_creation_timeframe_assertio" - ), - ] = None - - -class WrittenWork_WorkCreationAssertion(BaseModel): - class Config: - title = "text_creation_assertion" - model_bool = "id" - - id: Annotated[ - AnyUrl | None, SPARQLBinding("written_work__work_creation_assertion") - ] = None - work_creation: Annotated[ - WrittenWork_WorkCreationAssertion_WorkCreation | None, - SPARQLBinding("written_work__work_creation_assertion__work_creation"), - ] = None - work_creation_assertion_by: Annotated[ - AnyUrl | None, - SPARQLBinding( - "written_work__work_creation_assertion__work_creation_assertion_by" - ), - ] = None - work_creation_assertion_by_64676da87229e: Annotated[ - AnyUrl | None, - SPARQLBinding( - "written_work__work_creation_assertion__work_creation_assertion_by_64676da87229e" - ), - ] = None +from pydantic import AnyUrl, BaseModel, Field # noqa: F401 +from rdfproxy import ConfigDict, SPARQLBinding class WrittenWork(BaseModel): - class Config: - title = "Text Expression" - model_bool = "id" - group_by = "written_work" - - id: Annotated[AnyUrl | None, SPARQLBinding("written_work")] = None + model_config = ConfigDict( + title="Text Expression", + model_bool="id", + group_by="id", + ) + id: Annotated[AnyUrl | None, SPARQLBinding("written_work")] = Field( + default=None, exclude=True + ) written_work_label: Annotated[ str | None, SPARQLBinding("written_work__written_work_label") ] = None work_creation_assertion: Annotated[ - list[WrittenWork_WorkCreationAssertion], - SPARQLBinding("written_work__work_creation_assertion"), + list[AnyUrl], SPARQLBinding("written_work__work_creation_assertion") ] diff --git a/releven_written_work.rq b/releven_written_work.rq index 8beb8c8..b5c2d31 100644 --- a/releven_written_work.rq +++ b/releven_written_work.rq @@ -9,11 +9,6 @@ SELECT ?written_work ?written_work__written_work_label ?written_work__work_creation_assertion - ?written_work__work_creation_assertion__work_creation - ?written_work__work_creation_assertion__work_creation__work_creation_6464e7f7408f5 - ?written_work__work_creation_assertion__work_creation__text_creation_timeframe_assertio - ?written_work__work_creation_assertion__work_creation_assertion_by - ?written_work__work_creation_assertion__work_creation_assertion_by_64676da87229e WHERE { ?written_work a r11:Text_Expression. @@ -22,31 +17,7 @@ WHERE { OPTIONAL { ?written_work__work_creation_assertion a star:E13_lrmoo_R17. - ?written_work__work_creation_assertion crm:P141_assigned ?written_work. - - ?written_work__work_creation_assertion__work_creation a lrmoo:F28_Expression_Creation. - ?written_work__work_creation_assertion___wc crm:P141_assigned ?written_work__work_creation_assertion. - ?written_work__work_creation_assertion___wc crm:P140_assigned_attribute_to ?written_work__work_creation_assertion__work_creation. - - OPTIONAL { - ?written_work__work_creation_assertion__work_creation__work_creation_6464e7f7408f5 a star:E13_crm_P14. - ?written_work__work_creation_assertion__work_creation___wc6 crm:P141_assigned ?written_work__work_creation_assertion__work_creation. - ?written_work__work_creation_assertion__work_creation___wc6 crm:P140_assigned_attribute_to ?written_work__work_creation_assertion__work_creation___wc6___wc6. - ?written_work__work_creation_assertion__work_creation__work_creation_6464e7f7408f5 crm:P140_assigned_attribute_to ?written_work__work_creation_assertion__work_creation___wc6___wc6. - } - - ?written_work__work_creation_assertion__work_creation__text_creation_timeframe_assertio a star:E13_crm_P4. - ?written_work__work_creation_assertion__work_creation___tcta crm:P141_assigned ?written_work__work_creation_assertion__work_creation. - ?written_work__work_creation_assertion__work_creation___tcta crm:P140_assigned_attribute_to ?written_work__work_creation_assertion__work_creation___tcta___tcta. - ?written_work__work_creation_assertion__work_creation__text_creation_timeframe_assertio crm:P140_assigned_attribute_to ?written_work__work_creation_assertion__work_creation___tcta___tcta. - - ?written_work__work_creation_assertion__work_creation_assertion_by a crm:E21_Person. - ?written_work__work_creation_assertion___wcab crm:P141_assigned ?written_work__work_creation_assertion. - ?written_work__work_creation_assertion___wcab crm:P14_carried_out_by ?written_work__work_creation_assertion__work_creation_assertion_by. - - ?written_work__work_creation_assertion__work_creation_assertion_by_64676da87229e a r11:Author_Group. - ?written_work__work_creation_assertion___wcab6 crm:P141_assigned ?written_work__work_creation_assertion. - ?written_work__work_creation_assertion___wcab6 crm:P14_carried_out_by ?written_work__work_creation_assertion__work_creation_assertion_by_64676da87229e. + ?written_work ^crm:P141_assigned ?written_work__work_creation_assertion. } }