Skip to content

DerwenAI/pyOTTR

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

76 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pyOTTR

Build Status

Manipulate OTTR Reasonable Ontology Templates in Python.

Package documentation

OTTR documentation

Supported features:

🔧 In development:

Installation

Using pip (recommended)

pip install ottr

Manual installation

Requirement: poetry (v0.12 or higher).

git clone https://github.com/Callidon/pyOTTR.git
cd pyOTTR/
poetry install

Getting started

The main class to manipulate is OttrGenerator, which is used to load OTTR templates and expand template instances. So, in practice, you only need to create a new generator, load some templates and then execute your instances to produce RDF triples. Otherwise, everything else is done using classic OTTR syntax!

By default, all templates from the OTTR template library are loaded when the generator is created.

# an OttrGenerator is used to load templates and expand instances

import ottr

template: str = """
@prefix ex: <http://example.org#> .

ex:FirstName [ ottr:IRI ?uri, ?firstName ] :: {
  ottr:Triple ( ?uri, foaf:firstName, ?firstName )
} .

ex:Person[ ?firstName ] :: {
  ottr:Triple ( _:person, rdf:type, foaf:Person ),
  ex:FirstName ( _:person, ?firstName )
} .
""".strip()

# load a simple OTTR template definition

generator: ottr.OttrGenerator = ottr.OttrGenerator()
generator.load_templates(template)

# parse and prepare an instance for execution

rdf_data: str = """
@prefix ex: <http://example.org#> .

ex:Person("Ann") .
""".strip()

instances: ottr.generator.OttrInstances = generator.instanciate(rdf_data)

# execute the instance, which yield RDF triples
# the following prints (_:person0, rdf:type, foaf:Person) and (_:person0, foaf:firstName, "Ann")

for s, p, o in instances.execute(as_nt = True):
    print("# ----- RDF triple ----- #")
    print((s, p, o)

Addendum

Updated for more recent releases of RDFlib by Derwen

About

Manipulate OTTR Reasonable Ontology Templates in Python

Resources

Stars

Watchers

Forks

Releases

Packages

Used by

Contributors

Languages