TemplatableSparqlQueryLoader
What it is
Loads “templatable” SPARQL queries from a TripleStoreService, builds Pydantic argument models for each query, and returns a list of GenericWorkflow instances ready to execute those templates with validated inputs.
Public API
async_asyncio_thread_jobs(jobs)- Runs a list of thread jobs concurrently using
asyncio.to_thread. - Returns gathered results.
- Runs a list of thread jobs concurrently using
asyncio_thread_job(jobs)- Synchronous wrapper around
async_asyncio_thread_jobsusingasyncio.run.
- Synchronous wrapper around
class TemplatableSparqlQueryLoader__init__(triple_store_service: TripleStoreService)- Stores the triple store service used to query metadata.
templatable_queries() -> tuple[dict, dict]- Queries the triple store for:
intentMapping:TemplatableSparqlQueryitems (label, description, template, argument URIs)intentMapping:QueryArgumentitems (name, description, validation pattern/format)
- Returns:
queries: mapping of query URI → metadata dict includinghasArgumentlistarguments: mapping of argument URI → argument metadata
- Queries the triple store for:
load_workflows() -> list- For each templatable query:
- Creates a Pydantic model via
create_model(...)with string fields for each argument. - Applies
Field(..., description=..., pattern=..., example=...)based on argument metadata. - Instantiates
GenericWorkflow[ArgumentsModel](label, description, sparqlTemplate, ArgumentsModel, triple_store_service).
- Creates a Pydantic model via
- Returns a list of workflows; logs a warning and skips on per-query errors.
- For each templatable query:
Configuration/Dependencies
- Requires an instance of
naas_abi_core.services.triple_store.TripleStoreService.TripleStoreServiceproviding.query(str). - Uses:
pydantic(Field,create_model)rdflib(Graph,RDF,URIRef) to locally query argument metadatanaas_abi_core.loggerfor warnings- Local
GenericWorkflowclass (imported from.GenericWorkflow)
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) # each is a GenericWorkflow parameterized by a generated Pydantic args modelCaveats
load_workflows()swallows any exception per query and only logs a warning; problematic queries may be silently skipped.- Argument fields are always typed as
strand marked required (Field(...)), with validation enforced via regexpatternfrom the triple store. - The helper functions
async_asyncio_thread_jobs/asyncio_thread_jobare defined but not used byTemplatableSparqlQueryLoaderin this file.