TemplatableSparqlQueryLoader
What it is
- Loads templatable SPARQL query definitions and their argument metadata from a triple store.
- Produces a list of
GenericWorkflowinstances, each paired with a generated Pydantic model that validates the query arguments.
Public API
Functions
async_asyncio_thread_jobs(jobs)- Asynchronously runs a list of
asyncio.to_thread(*job)calls and gathers results.
- Asynchronously runs a list of
asyncio_thread_job(jobs)- Synchronous wrapper that runs
async_asyncio_thread_jobs(jobs)viaasyncio.run.
- Synchronous wrapper that runs
Class: TemplatableSparqlQueryLoader
__init__(triple_store_service: TripleStoreService)- Stores a
TripleStoreServiceused to query the triple store.
- Stores a
templatable_queries() -> tuple[dict, dict]- Queries the triple store for:
intentMapping:TemplatableSparqlQueryentries (label, description, template, and argument URIs)intentMapping:QueryArgumententries (name, description, validation pattern/format)
- Returns:
queries: mapping{query_uri: {label, description, sparqlTemplate, hasArgument: [argument_uri...]}}arguments: mapping{argument_uri: {name, description, validationPattern, validationFormat}}
- Queries the triple store for:
load_workflows() -> list- Builds one
GenericWorkflow[...]per templatable query. - For each query:
- Dynamically creates a Pydantic model named
"{LabelCapitalized}Arguments". - Each argument becomes a required
strfield with:descriptionfromargumentDescriptionpatternfromvalidationPatternexamplefromvalidationFormat
- Dynamically creates a Pydantic model named
- Returns a list of created workflows; failures are logged as warnings and skipped.
- Builds one
Configuration/Dependencies
- Requires a working
TripleStoreServiceinstance providing.query(sparql: str)returning iterable rows. - SPARQL schema expectations (must exist in the triple store):
intentMapping:TemplatableSparqlQueryrdfs:labelintentMapping:intentDescriptionintentMapping:sparqlTemplateintentMapping:hasArgument
intentMapping:QueryArgumentintentMapping:argumentNameintentMapping:argumentDescriptionintentMapping:validationPatternintentMapping:validationFormat
- Uses:
pydantic.create_model,pydantic.Fieldfor runtime model generationrdflib.Graphfor local querying of argument metadatanaas_abi_core.loggerfor warnings
Usage
from naas_abi_core.services.triple_store.TripleStoreService import TripleStoreService
from naas_abi_core.modules.templatablesparqlquery.workflows.TemplatableSparqlQueryLoader import (
TemplatableSparqlQueryLoader,
)
triple_store = TripleStoreService(...) # must be configured for your environment
loader = TemplatableSparqlQueryLoader(triple_store)
workflows = loader.load_workflows()
for wf in workflows:
print(wf)
Caveats
- If any query/argument is missing expected properties (e.g.,
validationPattern), workflow creation can fail and will be skipped with a warning. - The generated Pydantic fields are all typed as
strand marked required (Field(...)), regardless ofvalidationFormat. async_asyncio_thread_jobs/asyncio_thread_jobare defined but not used byload_workflows().