Skip to main content

OWL TIME functions implemented as SPARQL extension functions in rdflib

Project description

RDFlib OWL TIME Functions

This Python package implements functions to ascertain the temporal relations of objects in RDF graphs.

This package invents SPARQL functions that are based on logic provided in OWL TIME and are named similarly to TIME's Temporal Entity's predicates, for example, the function isBefore() correlates to time:before, i.e. isBefore(a, b) will test to see if time:before should be applied between object a & b.

The functions are made available as functions in RDFlib's SPARQL implementation where they can be called by IRI, e.g. isBefore(a, b) is called tfun:isBefore(?a, ?b). All functions are posed as questions, "is before", and return an RDF literal true or false.

Installation

Normal installation e.g. from PyPI: pip install timefuncs.

This package's only dependency is RDFlib.

Use

These functions are implemented in RDFlib Python in the file timefuncts/funcs.py and are imported into timefuncs/__init__.py and registered there in RDFlib as SPARQL extension functions with their IRIs.

This means they can be used like this (full working script):

from rdflib import Graph
from timefuncs import TFUN

data = """
    PREFIX : <http://example.com/>
    PREFIX time: <http://www.w3.org/2006/time#>    
    
    :a01 time:before :b01 . 
    
    :a02 time:after :b02 .
    """

g = Graph().parse(data=data)

q = """
    SELECT *
    WHERE {
        ?x ?p ?y .
        FILTER tfun:isBefore(?x, ?y)
    }
    """
for r in g.query(q, initNs={"tfun": TFUN}):
    print(f"{r['x']} is before {r['y']}")

# prints: http://example.com/a01 is before http://example.com/b0

Functions

These functions are implemented as SPARQL extension functions with the namespace https://w3id.org/timefuncs/, e.g. isBefore()'s full IRI is https://w3id.org/timefuncs/isBefore.

Functions implemented so far, and their corresponding TIME relations:

  • tfun:contains()
    • time:intervalBefore
  • tfun:hasDuring()
    • alias for contains()
  • tfun:hasInside()
    • time:intervalBefore
  • tfun:isAfter()
    • time:after
  • tfun:isBefore()
    • time:before
  • tfun:isContainedBy()
    • time:intervalBefore
  • tfun:isDuring()
    • alias for isContainedBy()
  • tfun:isInside()
    • time:inside

These functions are yet to be implemented:

  • tfun:isDisjoint()
    • time:intervalDisjoint
  • tfun:isIn()
    • time:intervalIn

Implementation logic

Functions are implemented to test for every conceivable way that they may be found to be true in data. For example, isBefore(a, b) will return true if:

  • the predicate time:before is given between a & b, or any chain of objects a ... n ... b
  • the predicate time:after is given between b & a, or any chain of objects b ... n ... a
  • any chain of time:before & time:after links a & b, all pointing in the right directions
  • the end of a is declared as being before b or the start of b
    • i.e. for <a> time:hasEnd <a_end> . and <b> time:hasBeginning <b_beginning> ., isBefore(a, b) is true if <a_end> time:before <b_beginning>
  • the start of b is declared as being after a or the end of a
  • a can be calculated as being before b, based on their instantaneous times or start and end times
    • i.e. for <a> time:inXSDDateTimeStamp <a_xsd> . and <b> time:inXSDDateTimeStamp <b_xsd> . or <a> time:hasEnd/time:inXSDDateTimeStamp <a_xsd> . and <b> time:hasEnd/time:inXSDDateTimeStamp <b_xsd> ., isBefore(a, b) is true if <a_xsd> <b_xsd>

Testing

All tests are in tests/ and implemented using pytest.

There are individual tests for each function, e.g. tests/test_is_before.py for isBefore() as well as a test file to rn all tests againts OWL TIME's test suite): tests/test_test_suite.py.

Contributing

Via GitHub, Issues & Pull Requests:

License

This code is licensed with the BSD 3-clause license as per LICENSE which is the same license as used for rdflib.

Citation

@software{https://github.com/rdflib/timefuncs,
  author = {{Nicholas J. Car}},
  title = {RDFlib OWL TIME Functions},
  version = {0.0.2},
  date = {2021},
  url = {https://github.com/rdflib/timefuncs}
}

Contact

Creator & maintainer:
Nicholas J. Car
Data System Architect
SURROUND Australia Pty Ltd
nicholas.car@surroundaustrlaia.com
and
Adjunct Senior Lecturer
College of Engineering & Computer Science
Australian National University
nicholas.car@anu.edu.au

https://orcid.org/0000-0002-8742-7730

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

timefuncs-0.0.2.tar.gz (8.9 kB view details)

Uploaded Source

Built Distribution

timefuncs-0.0.2-py3-none-any.whl (7.5 kB view details)

Uploaded Python 3

File details

Details for the file timefuncs-0.0.2.tar.gz.

File metadata

  • Download URL: timefuncs-0.0.2.tar.gz
  • Upload date:
  • Size: 8.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.1

File hashes

Hashes for timefuncs-0.0.2.tar.gz
Algorithm Hash digest
SHA256 94a87d7910f1a154aaec4d98afbf022f9a3b2ebca92bf709d6b2d4fb2520db43
MD5 f2d618e52a0118acbff93f0ad3c36d65
BLAKE2b-256 9b8e6b4b500b0a9a17c58e14b7eaa464b8a32a0d5660457709515885fb58de0e

See more details on using hashes here.

Provenance

File details

Details for the file timefuncs-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: timefuncs-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 7.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.1

File hashes

Hashes for timefuncs-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 aaab5715a9fed57fbb43f5fe6cd6a19c8732bb6bb96b0741abf34a1ae43f2b7b
MD5 d52aa5f6bcafcad49f7f525385b86ccb
BLAKE2b-256 3e276eb556b35b48008e7730b46b0787803d547fefb34a0269ec3cccecda58a0

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page