fastrdf.config

fastrdf’s configuration as triples, in the model itself.

The configuration of a model, what it virtualizes, which indexes it has and how it caches query results, is kept in a named graph of its own, CONFIG_GRAPH, in the namespace CONFIG (https://datatreehouse.github.io/fastrdf/config#), together with its common questions (saved SPARQL queries, CommonQuestion) and its prefixes (PrefixConfig, SHACL prefix declarations). It travels with the model: written, serialized and served like any other graph, and applied with apply_config. See CONFIG_GRAPH.md for the vocabulary.

Secrets are never stored: a secret parameter names the environment variable that holds it, read when the configuration is applied.

>>> from fastrdf.config import VirtualizationConfig, VirtualizationResource, write_virtualization, apply_config
>>> write_virtualization(m, VirtualizationConfig(
...     iri="urn:example:readings",
...     database="snowflake",
...     parameters={"account": "acme", "user": "fastrdf", "warehouse": "wh", "database": "meters", "schema": "public"},
...     secrets={"password": "SNOWFLAKE_PASSWORD"},
...     resources=[VirtualizationResource(name="readings", sql="SELECT id, ts AS timestamp, value FROM readings",
...                                       template=READINGS_TEMPLATE)],
... ))
>>> apply_config(m)  # connects and virtualizes

Attributes

CHRONTEXT

Deprecated alias of FASTRDF: the time series virtualization vocabulary moved from the chrontext

CONFIG

The namespace of fastrdf's configuration vocabulary.

CONFIG_GRAPH

The named graph that holds the configuration of a model.

DATABASES

DATABASE_PARAMETERS

DATABASE_SECRETS

DCTERMS

FASTRDF

The fastrdf vocabulary, which holds the time series virtualization vocabulary (fastrdf:hasTimeseries,

GENERAL_SQL

GENERATED_PREFIXES

KQL_DATABASES

LEGACY_CHRONTEXT

The former namespace of the time series virtualization vocabulary. Deprecated: its IRIs are still

OWL

PREFIXES

QUERY_FORMS

QUERY_LANGUAGES

QUESTION_DOMAINS

QUESTION_DOMAIN_LABELS

QUESTION_IRI_PREFIX

RDFS_LABEL

RDF_TYPE

RESOURCE_OPTIONS

SELF_DESCRIBING

SHACL

SQL_DATABASES

SQL_DIALECTS

TSQL_DATABASES

XSD

Classes

CommonQuestion

A saved SPARQL query with a name and a description: a question people ask the model often.

IndexingConfig

The indexes of the model.

PrefixConfig

A prefix of the model: a short name for a namespace, used to write and show IRIs (ex:Pump for

QueryCacheConfig

The query result cache of the model (Model.set_query_cache). Results are kept until a

SQLAlchemyDatabase

Any database SQLAlchemy connects to, by URL, queried with the given SQL dialect.

VirtualizationConfig

A virtualized database: how to connect to it, and the resources it holds.

VirtualizationResource

A resource of a virtualized database: its rows (SQL) and the template they instantiate.

Functions

apply_config(m[, environ]) → List[str]

Applies the configuration in the model's config graph: makes the configured prefixes known to

apply_prefixes(m[, prefixes])

Makes the configured prefixes (read from the config graph when not given) known to the

apply_properties(m, config)

Writes the static properties of the resources (VirtualizationResource.properties) on the

apply_query_cache(m[, config])

Sets the model's query result cache to the configuration (read from the config graph when

apply_virtualization(m, config[, environ])

Connects to a configured database and virtualizes its resources in the model.

generate_prefixes(m[, graph, max_generated, write]) → List[PrefixConfig]

Makes prefixes for the namespaces the data uses: the usual prefix of each well-known vocabulary

namespace_of(iri) → Optional[str]

The namespace of an IRI: up to its last '#' or '/' (for http and https IRIs), or its last ':'

prefix_iri(name) → str

The IRI of the declaration of a prefix in the config graph.

prefix_name_for(namespace) → str

A short prefix name for a namespace: its last telling path segment (http://example.org/plant/

probe(config[, sql, environ]) → dict

Checks a configuration against the database itself, without changing any model: connects and,

query_form(query) → str

The form of a SPARQL query (select, construct, ask or describe), after its prologue and

question_iri(name) → str

A new IRI for a common question, from its name and a random part.

read_indexing(m) → IndexingConfig

The indexes in the configuration (none when it does not say).

read_prefixes(m) → List[PrefixConfig]

The prefixes in the configuration (the SHACL prefix declarations in the config graph), by name.

read_query_cache(m) → Optional[QueryCacheConfig]

The query result cache in the configuration, None when it does not say.

read_questions(m[, domain]) → List[CommonQuestion]

The common questions in the configuration, in their order (sh:order, then name). With a domain

read_virtualizations(m) → List[VirtualizationConfig]

The virtualized databases in the configuration.

remove_prefix(m, name)

Removes a prefix from the configuration.

remove_query_cache(m)

Removes the query result cache from the configuration (the model's setting stays).

remove_question(m, iri)

Removes a common question from the configuration.

remove_virtualization(m, iri)

Removes a virtualized database from the configuration (a virtualization already applied stays).

well_known_prefixes() → Dict[str, str]

fastrdf's well-known prefixes, from namespace to name (fastrdf.well_known_prefixes(), the

write_indexing(m, config)

Sets the indexes of the model in the configuration.

write_prefix(m, prefix)

Adds a prefix to the configuration, or replaces the one with its name.

write_prefixes(m, prefixes)

Adds prefixes to the configuration, replacing those with the same names. A namespace has one

write_query_cache(m, config)

Sets the query result cache of the model in the configuration.

write_question(m, question)

Adds a common question to the configuration, or replaces the one with its IRI.

write_virtualization(m, config)

Adds a virtualized database to the configuration, or replaces the one with its IRI.

Module Contents

class fastrdf.config.CommonQuestion

A saved SPARQL query with a name and a description: a question people ask the model often. In the config graph a mcfg:CommonQuestion, which is a SHACL-AF SPARQL executable (sh:select, sh:construct or sh:ask holds the query).

applies_to: str | None = None

mcfg:appliesTo: the vocabulary (namespace IRI) of the data it is about, e.g. one of QUESTION_DOMAINS; None for a question about any model.

bundled: bool = False

mcfg:bundled: one of the questions fastrdf ships (fastrdf.bundled_questions).

description: str = ''

dcterms:description: what the question answers.

property domain: str | None

The name of its domain in QUESTION_DOMAINS (e.g. “cim”), if it is one of those.

property form: str

The query form: select, construct, ask or describe.

iri: str
name: str

dcterms:title (and rdfs:label).

order: int | None = None

sh:order: where it is listed, lowest first (then by name).

query: str

The SPARQL query, with its PREFIX declarations: SELECT, CONSTRUCT, ASK or DESCRIBE.

class fastrdf.config.IndexingConfig

The indexes of the model.

any() → bool

Whether an index beyond the defaults is configured.

options() → fastrdf.IndexingOptions

The IndexingOptions that create these indexes.

fts: bool = False
geo: bool = False
object_sort_all: bool = False
object_sort_some: List[str] = []
class fastrdf.config.PrefixConfig

A prefix of the model: a short name for a namespace, used to write and show IRIs (ex:Pump for http://example.org/Pump). In the config graph a SHACL prefix declaration (sh:prefix, sh:namespace), declared (sh:declare) by the owl:Ontology PREFIXES.

generated: bool = False

mcfg:generated: made by generate_prefixes, not by a person.

name: str

sh:prefix, e.g. ex.

namespace: str

sh:namespace, e.g. http://example.org/.

class fastrdf.config.QueryCacheConfig

The query result cache of the model (Model.set_query_cache). Results are kept until a write changes the triples they read, per named graph and predicate.

options() → fastrdf.QueryCacheOptions

The QueryCacheOptions for Model.set_query_cache.

cache_virtualized: bool = False
enabled: bool = True
max_bytes: int = 268435456
max_entries: int = 1024
max_entry_bytes: int = 16777216
min_run_time: float = 0.0
ttl: float | None = None
class fastrdf.config.SQLAlchemyDatabase(url: str, sql_dialect: str, password: str | None = None)

Any database SQLAlchemy connects to, by URL, queried with the given SQL dialect.

query(sql: str)
engine
sql_dialect
class fastrdf.config.VirtualizationConfig

A virtualized database: how to connect to it, and the resources it holds.

database: str

One of DATABASES.

enabled: bool = True
iri: str
label: str | None = None
parameters: Dict[str, str]

Connection parameters, e.g. account and warehouse for Snowflake.

resources: List[VirtualizationResource] = []
secrets: Dict[str, str]

Secret connection parameters: the parameter name and the environment variable holding it.

sql_dialect: str | None = None

Overrides the database’s dialect, one of SQL_DIALECTS.

class fastrdf.config.VirtualizationResource

A resource of a virtualized database: its rows (SQL) and the template they instantiate.

name: str

The resource name, the value of fastrdf:hasResource on what it virtualizes.

options: Dict[str, str]

Further settings of the resource, e.g. for CDF id_type (external_id, id or instance_id).

properties: Dict[str, str]

Static properties of the objects it virtualizes: predicate IRI to object IRI, e.g. the unit of a measurement (cim:Measurement.unitSymbol to cim:UnitSymbol.W). Applying the virtualization writes them as ordinary triples on each object with fastrdf:hasResource the resource name (in the default graph), replacing the values those objects had for the predicate. Constant triples in a virtualized template are not supported by chrontext, so this is where such facts go.

sql: str | None = None

For an SQL database: the SQL giving its rows, with columns named as the template’s parameters.

template: str

The template, as a stOTTR document, or the IRI of a template already in the model.

fastrdf.config.apply_config(m: fastrdf.Model, environ: Mapping[str, str] | None = None) → List[str]

Applies the configuration in the model’s config graph: makes the configured prefixes known to the model, creates the configured indexes, sets the query result cache and virtualizes the enabled databases.

Parameters:
  • m – The model.

  • environ – Where the secrets are read from, by default the environment variables.

Returns:

The IRIs of the virtualizations applied.

fastrdf.config.apply_prefixes(m: fastrdf.Model, prefixes: List[PrefixConfig] | None = None)

Makes the configured prefixes (read from the config graph when not given) known to the model, for parsing SPARQL, Datalog and OTTR (Model.add_prefixes).

fastrdf.config.apply_properties(m: fastrdf.Model, config: VirtualizationConfig)

Writes the static properties of the resources (VirtualizationResource.properties) on the objects they virtualize, those with fastrdf:hasResource the resource name, in the default graph. A property replaces the values the objects had for its predicate, so changing a unit and applying again leaves only the new one.

fastrdf.config.apply_query_cache(m: fastrdf.Model, config: QueryCacheConfig | None = None)

Sets the model’s query result cache to the configuration (read from the config graph when not given). Does nothing when the configuration has none.

fastrdf.config.apply_virtualization(m: fastrdf.Model, config: VirtualizationConfig, environ: Mapping[str, str] | None = None)

Connects to a configured database and virtualizes its resources in the model.

fastrdf.config.generate_prefixes(m: fastrdf.Model, graph: None | str | List[str | None] = None, max_generated: int = GENERATED_PREFIXES, write: bool = True) → List[PrefixConfig]

Makes prefixes for the namespaces the data uses: the usual prefix of each well-known vocabulary in it (fastrdf.well_known_prefixes(): rdf, rdfs, xsd, owl, sh, skos, dcterms, prov, geo, cim, opc, …), and a short name (prefix_name_for) for the max_generated namespaces it uses most besides these. Namespaces that have a prefix already (in the config graph, or declared with sh:prefix in any graph) keep it, and names in use are not given again.

Parameters:
  • m – The model.

  • graph – The named graph whose data is looked at, by default the default graph; or a list of graphs (None in it for the default graph), whose uses of each namespace are added up.

  • max_generated – How many namespaces that are not well known get a prefix at most.

  • write – Writes the new prefixes to the config graph (as generated, mcfg:generated true).

Returns:

The new prefixes.

fastrdf.config.namespace_of(iri: str) → str | None

The namespace of an IRI: up to its last ‘#’ or ‘/’ (for http and https IRIs), or its last ‘:’ for other schemes (urn:example:pump:1 has urn:example:pump:). None when there is no local name.

fastrdf.config.prefix_iri(name: str) → str

The IRI of the declaration of a prefix in the config graph.

fastrdf.config.prefix_name_for(namespace: str) → str

A short prefix name for a namespace: its last telling path segment (http://example.org/plant/ is plant), else the name of its host (http://data.example.org/ is example).

fastrdf.config.probe(config: VirtualizationConfig, sql: str | None = None, environ: Mapping[str, str] | None = None) → dict

Checks a configuration against the database itself, without changing any model: connects and, for an SQL database, runs the SQL (LIMIT 5, in KQL: take 5) and returns its columns and a sample; for Cognite Data Fusion inspects the token and lists some time series; for InfluxDB without SQL, and InfluxDB 2, lists the measurements; for OPC UA connects to the endpoint; for IP.21 searches for some tags.

Returns:

A dict with “message”, and “columns” (name and polars type) and “sample” (rows) when there are rows to show.

fastrdf.config.query_form(query: str) → str

The form of a SPARQL query (select, construct, ask or describe), after its prologue and comments. Raises ValueError for an update or anything else that is not a query.

fastrdf.config.question_iri(name: str) → str

A new IRI for a common question, from its name and a random part.

fastrdf.config.read_indexing(m: fastrdf.Model) → IndexingConfig

The indexes in the configuration (none when it does not say).

fastrdf.config.read_prefixes(m: fastrdf.Model) → List[PrefixConfig]

The prefixes in the configuration (the SHACL prefix declarations in the config graph), by name.

fastrdf.config.read_query_cache(m: fastrdf.Model) → QueryCacheConfig | None

The query result cache in the configuration, None when it does not say.

fastrdf.config.read_questions(m: fastrdf.Model, domain: str | None = None) → List[CommonQuestion]

The common questions in the configuration, in their order (sh:order, then name). With a domain (one of QUESTION_DOMAINS, or a vocabulary IRI), only those that apply to it and those that apply to any model.

fastrdf.config.read_virtualizations(m: fastrdf.Model) → List[VirtualizationConfig]

The virtualized databases in the configuration.

fastrdf.config.remove_prefix(m: fastrdf.Model, name: str)

Removes a prefix from the configuration.

fastrdf.config.remove_query_cache(m: fastrdf.Model)

Removes the query result cache from the configuration (the model’s setting stays).

fastrdf.config.remove_question(m: fastrdf.Model, iri: str)

Removes a common question from the configuration.

fastrdf.config.remove_virtualization(m: fastrdf.Model, iri: str)

Removes a virtualized database from the configuration (a virtualization already applied stays).

fastrdf.config.well_known_prefixes() → Dict[str, str]

fastrdf’s well-known prefixes, from namespace to name (fastrdf.well_known_prefixes(), the one table of default prefixes, kept in Rust): the names generate_prefixes gives the vocabularies it finds. cim is CIM100; CIM16 is cim16.

fastrdf.config.write_indexing(m: fastrdf.Model, config: IndexingConfig)

Sets the indexes of the model in the configuration.

fastrdf.config.write_prefix(m: fastrdf.Model, prefix: PrefixConfig)

Adds a prefix to the configuration, or replaces the one with its name.

fastrdf.config.write_prefixes(m: fastrdf.Model, prefixes: List[PrefixConfig])

Adds prefixes to the configuration, replacing those with the same names. A namespace has one prefix: writing a prefix for a namespace that has another one raises ValueError.

fastrdf.config.write_query_cache(m: fastrdf.Model, config: QueryCacheConfig)

Sets the query result cache of the model in the configuration.

fastrdf.config.write_question(m: fastrdf.Model, question: CommonQuestion)

Adds a common question to the configuration, or replaces the one with its IRI.

fastrdf.config.write_virtualization(m: fastrdf.Model, config: VirtualizationConfig)

Adds a virtualized database to the configuration, or replaces the one with its IRI.

fastrdf.config.CHRONTEXT = 'https://datatreehouse.github.io/fastrdf/vocab#'

Deprecated alias of FASTRDF: the time series virtualization vocabulary moved from the chrontext namespace (LEGACY_CHRONTEXT) to the fastrdf vocabulary.

fastrdf.config.CONFIG = 'https://datatreehouse.github.io/fastrdf/config#'

The namespace of fastrdf’s configuration vocabulary.

fastrdf.config.CONFIG_GRAPH = 'https://datatreehouse.github.io/fastrdf/config#ConfigGraph'

The named graph that holds the configuration of a model.

fastrdf.config.DATABASES: Dict[str, str]
fastrdf.config.DATABASE_PARAMETERS: Dict[str, List[str]]
fastrdf.config.DATABASE_SECRETS: Dict[str, List[str]]
fastrdf.config.DCTERMS = 'http://purl.org/dc/terms/'
fastrdf.config.FASTRDF = 'https://datatreehouse.github.io/fastrdf/vocab#'

The fastrdf vocabulary, which holds the time series virtualization vocabulary (fastrdf:hasTimeseries, fastrdf:hasDataPoint, fastrdf:hasTimestamp, fastrdf:hasValue, fastrdf:hasResource, fastrdf:hasExternalId, fastrdf:FloorDateTimeToSecondsInterval, fastrdf:dateBin, …).

fastrdf.config.GENERAL_SQL = 'sql'
fastrdf.config.GENERATED_PREFIXES = 8
fastrdf.config.KQL_DATABASES = ('kusto',)
fastrdf.config.LEGACY_CHRONTEXT = 'https://github.com/DataTreehouse/chrontext#'

The former namespace of the time series virtualization vocabulary. Deprecated: its IRIs are still accepted in queries, in the predicates of data and in virtualization templates, and are read as the IRI with the same local name in FASTRDF.

fastrdf.config.OWL = 'http://www.w3.org/2002/07/owl#'
fastrdf.config.PREFIXES = 'https://datatreehouse.github.io/fastrdf/config#prefixes'
fastrdf.config.QUERY_FORMS: Dict[str, tuple]
fastrdf.config.QUERY_LANGUAGES: Dict[str, str]
fastrdf.config.QUESTION_DOMAINS: Dict[str, str]
fastrdf.config.QUESTION_DOMAIN_LABELS: Dict[str, str]
fastrdf.config.QUESTION_IRI_PREFIX = 'urn:fastrdf:config:question:'
fastrdf.config.RDFS_LABEL = 'http://www.w3.org/2000/01/rdf-schema#label'
fastrdf.config.RDF_TYPE = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type'
fastrdf.config.RESOURCE_OPTIONS: Dict[str, List[str]]
fastrdf.config.SELF_DESCRIBING = ('osipi', 'pi_points', 'ip21', 'insights_hub')
fastrdf.config.SHACL = 'http://www.w3.org/ns/shacl#'
fastrdf.config.SQL_DATABASES = ('sql', 'duckdb', 'snowflake', 'bigquery', 'databricks', 'timescale', 'fabric_lakehouse',...
fastrdf.config.SQL_DIALECTS = ('duckdb', 'postgres', 'snowflake', 'bigquery', 'databricks')
fastrdf.config.TSQL_DATABASES = ('fabric_warehouse',)
fastrdf.config.XSD = 'http://www.w3.org/2001/XMLSchema#'