ottr.base package

Submodules

ottr.base.argument module

class ottr.base.argument.ConcreteArgument(value: Union[rdflib.term.BNode, rdflib.term.Literal, rdflib.term.URIRef, rdflib.term.Variable], position: int)

Bases: ottr.base.argument.InstanceArgument

An argument that evaluates to a constant RDF term, i.e., a RDF term.

Args:
  • value: Argument’s value.

  • position: Argument’s position in the template’s parameters list.

evaluate(bindings: Dict[rdflib.term.Variable, Union[rdflib.term.BNode, rdflib.term.Literal, rdflib.term.URIRef, rdflib.term.Variable, List[Union[rdflib.term.BNode, rdflib.term.Literal, rdflib.term.URIRef, rdflib.term.Variable]]]] = {}, bnode_suffix: Tuple[int, int] = (0, 0), as_nt: bool = False) → Iterable[Union[Tuple[Union[rdflib.term.BNode, rdflib.term.Literal, rdflib.term.URIRef], Union[rdflib.term.BNode, rdflib.term.Literal, rdflib.term.URIRef], Union[rdflib.term.BNode, rdflib.term.Literal, rdflib.term.URIRef]], Tuple[str, str, str]]]

Evaluate the argument using an optional set of bindings.

Args:
  • bindings: set of bindings used for evaluation.

  • bnode_suffix: Pair of suffixes used for creating unique blank nodes.

  • as_nt: True if the RDF triples produced should be in n-triples format, False to use the rdflib format.

Yields:

RDF triples, in rdflib or n-triples format.

property is_bound

Return True if the argument is bound (it is not a Variable), False otherwise

class ottr.base.argument.InstanceArgument(value: Union[rdflib.term.BNode, rdflib.term.Literal, rdflib.term.URIRef, rdflib.term.Variable], position: int)

Bases: abc.ABC

An abstract instance argument, which corresponds to the parameter of a template.

Args:
  • value: Argument’s value.

  • position: Argument’s position in the template’s parameters list.

abstract evaluate(bindings: Dict[rdflib.term.Variable, Union[rdflib.term.BNode, rdflib.term.Literal, rdflib.term.URIRef, rdflib.term.Variable, List[Union[rdflib.term.BNode, rdflib.term.Literal, rdflib.term.URIRef, rdflib.term.Variable]]]] = {}, bnode_suffix: Tuple[int, int] = (0, 0), as_nt: bool = False) → Iterable[Union[Tuple[Union[rdflib.term.BNode, rdflib.term.Literal, rdflib.term.URIRef], Union[rdflib.term.BNode, rdflib.term.Literal, rdflib.term.URIRef], Union[rdflib.term.BNode, rdflib.term.Literal, rdflib.term.URIRef]], Tuple[str, str, str]]]

Evaluate the argument using an optional set of bindings.

Args:
  • bindings: set of bindings used for evaluation.

  • bnode_suffix: Pair of suffixes used for creating unique blank nodes.

  • as_nt: True if the RDF triples produced should be in n-triples format, False to use the rdflib format.

Yields:

RDF triples, in rdflib or n-triples format.

property is_bound

Return True if the argument is bound (it is not a Variable), False otherwise

property position

The argument’s position in the template’s parameters list

property value

The argument’s value

class ottr.base.argument.URIArgument(uri: rdflib.term.URIRef, position: int)

Bases: ottr.base.argument.ConcreteArgument

A ConcreteArgument that always evaluates to an URI.

Args:
  • value: Argument’s value (an URI).

  • position: Argument’s position in the template’s parameters list.

class ottr.base.argument.VariableArgument(value: Union[rdflib.term.BNode, rdflib.term.Literal, rdflib.term.URIRef, rdflib.term.Variable], position: int)

Bases: ottr.base.argument.InstanceArgument

A variable argument, i.e., a SPARQL variable.

Args:
  • value: Argument’s value (a SPARQL variable).

  • position: Argument’s position in the template’s parameters list.

evaluate(bindings: Dict[rdflib.term.Variable, Union[rdflib.term.BNode, rdflib.term.Literal, rdflib.term.URIRef, rdflib.term.Variable, List[Union[rdflib.term.BNode, rdflib.term.Literal, rdflib.term.URIRef, rdflib.term.Variable]]]] = {}, bnode_suffix: Tuple[int, int] = (0, 0), as_nt: bool = False) → Iterable[Union[Tuple[Union[rdflib.term.BNode, rdflib.term.Literal, rdflib.term.URIRef], Union[rdflib.term.BNode, rdflib.term.Literal, rdflib.term.URIRef], Union[rdflib.term.BNode, rdflib.term.Literal, rdflib.term.URIRef]], Tuple[str, str, str]]]

Evaluate the argument using an optional set of bindings.

Args:
  • bindings: set of bindings used for evaluation.

  • bnode_suffix: Pair of suffixes used for creating unique blank nodes.

  • as_nt: True if the RDF triples produced should be in n-triples format, False to use the rdflib format.

Yields:

RDF triples, in rdflib or n-triples format.

ottr.base.base_templates module

class ottr.base.base_templates.OttrTriple(subject_arg: ottr.base.argument.InstanceArgument, predicate_arg: ottr.base.argument.InstanceArgument, object_arg: ottr.base.argument.InstanceArgument)

Bases: ottr.base.template.AbstractTemplate

The default ottr:Triple base template, which expand to a RDF triple.

Args:
  • subject_arg: Subject argument of the template instance.

  • predicate_arg: Predicate argument of the template instance.

  • object_arg: Object argument of the template instance.

expand(arguments: Dict[rdflib.term.Variable, Union[rdflib.term.BNode, rdflib.term.Literal, rdflib.term.URIRef, rdflib.term.Variable, List[Union[rdflib.term.BNode, rdflib.term.Literal, rdflib.term.URIRef, rdflib.term.Variable]]]], all_templates: Dict[rdflib.term.URIRef, ottr.base.template.AbstractTemplate], bnode_suffix: Tuple[int, int] = (0, 0), as_nt: bool = False) → Iterable[Union[Tuple[Union[rdflib.term.BNode, rdflib.term.Literal, rdflib.term.URIRef], Union[rdflib.term.BNode, rdflib.term.Literal, rdflib.term.URIRef], Union[rdflib.term.BNode, rdflib.term.Literal, rdflib.term.URIRef]], Tuple[str, str, str]]]

Expands the template and yields a single RDF triple.

Args:
  • arguments: Template instantation arguments.

  • all_templates: Map of all templates known at expansion times.

  • bnode_suffix: Pair of suffixes used for creating unique blank nodes.

  • as_nt: True if the RDF triples produced should be in n-triples format, False to use the rdflib format.

Yields:

A RDF triple, in rdflib or n-triples format.

is_base() → bool

Returns True if the template is a base template, False otherwise

ottr.base.expansion module

class ottr.base.expansion.CrossTemplate(name: rdflib.term.URIRef, instance: ottr.base.template.AbstractTemplate, cross_variable: rdflib.term.Variable)

Bases: ottr.base.template.AbstractTemplate

A CrossTemplate expands a template instance using the ‘cross’ expansion mode.

Args:
  • name: Template’s name.

  • instance: Template instance to expand with the ‘cross’ expansion mode.

  • cross_variable: Variable which binds to the list of arguments for the cross-operator.

expand(arguments: Dict[rdflib.term.Variable, Union[rdflib.term.BNode, rdflib.term.Literal, rdflib.term.URIRef, rdflib.term.Variable, List[Union[rdflib.term.BNode, rdflib.term.Literal, rdflib.term.URIRef, rdflib.term.Variable]]]], all_templates: Dict[rdflib.term.URIRef, ottr.base.template.AbstractTemplate], bnode_suffix: Tuple[int, int] = (0, 0), as_nt: bool = False) → Iterable[Union[Tuple[Union[rdflib.term.BNode, rdflib.term.Literal, rdflib.term.URIRef], Union[rdflib.term.BNode, rdflib.term.Literal, rdflib.term.URIRef], Union[rdflib.term.BNode, rdflib.term.Literal, rdflib.term.URIRef]], Tuple[str, str, str]]]

Expands the template and yields RDF triples.

Args:
  • arguments: Template instantation arguments.

  • all_templates: Map of all templates known at expansion times.

  • bnode_suffix: Pair of suffixes used for creating unique blank nodes.

  • as_nt: True if the RDF triples produced should be in n-triples format, False to use the rdflib format.

Yields:

RDF triples, in rdflib or n-triples format.

ottr.base.template module

class ottr.base.template.AbstractTemplate(name: rdflib.term.URIRef)

Bases: abc.ABC

An abstract OTTR Template.

Argument: The template’s name as an RDF URI.

add_parameter(name: rdflib.term.Variable, position: int, param_type: rdflib.term.URIRef = rdflib.term.URIRef('http://www.w3.org/2000/01/rdf-schema#Resource'), optional: bool = False, nonblank: bool = False, default: Union[rdflib.term.BNode, rdflib.term.Literal, rdflib.term.URIRef] = None) → None

Register a new parameter for this template.

Args:
  • name: The parameter’s name.

  • position: The parameter’s position in the template definition.

  • param_type: The parameter’s type (an URI). Defaults to rdfs:Resource.

  • optional: If the parameter is optional or not. Defaults to False.

  • nonblank: If the parameter accepts blank nodes or not. Defaults to False.

  • default: The parameter’s default value if it is set to ottr:None.

Example:
>>> from rdflib import Literal, URIRref, Variable
>>> from rdflib.namespace import Namespace, XSD
>>> template.add_parameter(Variable("?uri"), 0, param_type=URIRef("http://ns.ottr.xyz/0.4/IRI"), nonblank=True)
>>> template.add_parameter(Variable("?name"), 1, param_type=XSD.string, nonblank=True)
abstract expand(arguments: Dict[rdflib.term.Variable, Union[rdflib.term.BNode, rdflib.term.Literal, rdflib.term.URIRef, rdflib.term.Variable, List[Union[rdflib.term.BNode, rdflib.term.Literal, rdflib.term.URIRef, rdflib.term.Variable]]]], all_templates: Dict[rdflib.term.URIRef, Any], bnode_suffix: Tuple[int, int] = (0, 0), as_nt: bool = False) → Iterable[Union[Tuple[Union[rdflib.term.BNode, rdflib.term.Literal, rdflib.term.URIRef], Union[rdflib.term.BNode, rdflib.term.Literal, rdflib.term.URIRef], Union[rdflib.term.BNode, rdflib.term.Literal, rdflib.term.URIRef]], Tuple[str, str, str]]]

Expands the template and yields RDF triples.

Args:
  • arguments: Template instantation arguments.

  • all_templates: Map of all templates known at expansion times.

  • bnode_suffix: Pair of suffixes used for creating unique blank nodes.

  • as_nt: True if the RDF triples produced should be in n-triples format, False to use the rdflib format.

Yields:

RDF triples, in rdflib or n-triples format.

format_arguments(arguments: List[Tuple[int, Union[rdflib.term.BNode, rdflib.term.Literal, rdflib.term.URIRef]]]) → Dict[rdflib.term.Variable, Union[rdflib.term.BNode, rdflib.term.Literal, rdflib.term.URIRef]]

Format a list of expansion arguments so that they can be used for template expansion using the expand() method.

Args: List of expansion arguments.

Returns: Formatted list of expansion arguments, to be used with the expand() method.

Example:
>>> from rdflib import Literal, URIRef
>>> from rdflib.namespace import XSD
>>> raw_arguments = [ (0, URIRef("http://example.org#Anna")), (1, Literal("Anna")) ]
>>> arguments = template.format_arguments(raw_arguments)
>>> for triple in template.expand(arguments):
>>>   print(triple)
is_base() → bool

Returns True if the template is a base template, False otherwise

property name
class ottr.base.template.MainTemplate(name: rdflib.term.URIRef, instances: List[ottr.base.template.AbstractTemplate])

Bases: ottr.base.template.AbstractTemplate

An OTTR template definition, which contains several instances to expand.

Args:
  • name: The template name.

  • instances: Instances declared in the template.

expand(arguments: Dict[rdflib.term.Variable, Union[rdflib.term.BNode, rdflib.term.Literal, rdflib.term.URIRef, rdflib.term.Variable, List[Union[rdflib.term.BNode, rdflib.term.Literal, rdflib.term.URIRef, rdflib.term.Variable]]]], all_templates: Dict[rdflib.term.URIRef, Any], bnode_suffix: Tuple[int, int] = (0, 0), as_nt: bool = False) → Iterable[Union[Tuple[Union[rdflib.term.BNode, rdflib.term.Literal, rdflib.term.URIRef], Union[rdflib.term.BNode, rdflib.term.Literal, rdflib.term.URIRef], Union[rdflib.term.BNode, rdflib.term.Literal, rdflib.term.URIRef]], Tuple[str, str, str]]]

Expands the template and yields RDF triples.

Args:
  • arguments: Template instantation arguments.

  • all_templates: Map of all templates known at expansion times.

  • bnode_suffix: Pair of suffixes used for creating unique blank nodes.

  • as_nt: True if the RDF triples produced should be in n-triples format, False to use the rdflib format.

Yields:

RDF triples, in rdflib or n-triples format.

class ottr.base.template.NonBaseInstance(name: rdflib.term.URIRef, instance_arguments: List[ottr.base.argument.InstanceArgument])

Bases: ottr.base.template.AbstractTemplate

A non-base OTTR Template instance.

Args:
  • name: The template name.

  • instance_arguments: Arguments of the template.

expand(arguments: Dict[rdflib.term.Variable, Union[rdflib.term.BNode, rdflib.term.Literal, rdflib.term.URIRef, rdflib.term.Variable, List[Union[rdflib.term.BNode, rdflib.term.Literal, rdflib.term.URIRef, rdflib.term.Variable]]]], all_templates: Dict[rdflib.term.URIRef, Any], bnode_suffix: Tuple[int, int] = (0, 0), as_nt: bool = False) → Iterable[Union[Tuple[Union[rdflib.term.BNode, rdflib.term.Literal, rdflib.term.URIRef], Union[rdflib.term.BNode, rdflib.term.Literal, rdflib.term.URIRef], Union[rdflib.term.BNode, rdflib.term.Literal, rdflib.term.URIRef]], Tuple[str, str, str]]]

Expands the template and yields RDF triples.

Args:
  • arguments: Template instantation arguments.

  • all_templates: Map of all templates known at expansion times.

  • bnode_suffix: Pair of suffixes used for creating unique blank nodes.

  • as_nt: True if the RDF triples produced should be in n-triples format, False to use the rdflib format.

Yields: RDF triples, in rdflib or n-triples format.

Throws: Exception when an undefined OTTR template is encountered.

class ottr.base.template.TemplateParameter(name: rdflib.term.Variable, param_type: str, optional: bool, nonblank: bool, default: Union[rdflib.term.BNode, rdflib.term.Literal, rdflib.term.URIRef] = None)

Bases: object

The parameter of an OTTR Template.

Args:
  • name: The parameter’s name.

  • param_type: The parameter’s type.

  • optional: if the parameter is optional or not.

  • nonblank: if the parameter accepts blank node or not.

  • default: the parameter’s default value if it is set to ottr:None.

property name

Get the parameter name, i.e., a SPARQL variable

validate(value: Union[rdflib.term.BNode, rdflib.term.Literal, rdflib.term.URIRef]) → Tuple[bool, Union[rdflib.term.BNode, rdflib.term.Literal, rdflib.term.URIRef, None], Optional[str]]

Assert that a RDF Term can be used as argument for this parameter, i.e., its value is compatible with the parameter definition (type, optional, non blank, etc).

Argument: The value to validate.

Returns: A tuple (is_valid, value, error_reason) where:
  • is_valid is a boolean which indicates the outcome of the validation.

  • value is the RDF value to use for the argument. Set to None if the validation was not successfull.

  • error_reason is an error message that indicates why the validation has failed. Set to None if the validation was successfull.

ottr.base.utils module

Module contents