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.

This repository also contains a formal declaration of the functions that this package implements as a SKOS vocabulary. See the Vocabulary section below.

Installation

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

This package's only non-standard 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 a time:TemporalEntity .    
    :b01 a time:TemporalEntity .
    :c01 a time:TemporalEntity .
    :d01 a time:TemporalEntity .
    
    :a01 time:before :c01 .

    :b01 time:after :c01 .
    """

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

q = """
    PREFIX tfun: <https://w3id.org/timefuncs/>

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

The above stript outputs:

http://example.com/a01 is before http://example.com/b01
http://example.com/a01 is before http://example.com/c01
http://example.com/b01 is before http://example.com/c01

The above script is run using an environment that has had the time functions registered with its copy of rdflib (perhaps by running pip install timefuncs) so that there is no need to import anything other than rdfib and the time functions namespace (from timefuncs import TFUN). It may appear that the namespace is not used but it is, internally! No need to re-declare tfun: as a PREFIX in the SPARQL query...

The time function used here, tfun:isBefore, is called as a filter function to return true when the first given object, here ?x is before the second given object, ?y.

This example uses a pretty open-ended graph pattern match (`?x ?p ?y).

Working with Reasoning / Inferencing

These functions don't assume that any reasoning has been carried out on data and will correctly interpret time:before / time:after and similar inverses, transitive relations (chains of properties) and so on. But there are limits: you will always be able to invent highly complex relations between time:TemporalEntity instances that these functions won't correctly work with.

To work with highly complex data, try reasoning over your data first with OWL TIME's axioms before running these functions. To do this, you need a tool that can calculate OWL "RL" inferences, such as rdflib's OWL-RL. Many triplestores have OWL-RL reasoning capability built-in or as add ons.

Functions

Functions in this package 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.

Python implementations are named the same as the SPARQL functions, but in snake_case, not camelCase, e.g. SPARQL's isBefore() is implemented in Python's is_before().

Functions implemented so far, and their corresponding TIME relations:

SPARQL Parameters TIME predicates Notes
tfun:contains(a, b) time:Interval
time:Interval
time:intervalContains
inv. time:intervalDuring
equivalent to tfun:isContainedBy(b, a)
tfun:finishes(a, b) time:Interval
time:Interval
time:intervalFinishes
inv. time:intervalFinishedBy
not time:disjoint
equivalent to tfun:isFinishedBy(b, a)
tfun:hasDuring(a, b) time:Interval
time:Interval
alias for contains(a, b)
tfun:hasInside(a, b) time:Interval
time:Instant
time:inside
not time:before
not time:after
equivalent to tfun:isInside(b, a)
tfun:isAfter(a, b) time:TemporalEntity
time:TemporalEntity
time:after
inv. time:before
equivalent to tfun:isBefore(b, a)
tfun:isBefore(a, b) time:TemporalEntity
time:TemporalEntity
time:before
inv. time:after
equivalent to tfun:isAfter(b, a)
tfun:isContainedBy(a, b) time:Interval
time:Interval
time:intervalDuring
inv. time:intervalContains
equivalent to tfun:contains(b, a)
tfun:isDuring(a, b) time:Interval
time:Interval
alias for isContainedBy(a, b)
tfun:isFinishedBy(a, b) time:Interval
time:Interval
time:intervalFinishedBy
inv. time:intervalFinishes
not time:disjoint
equivalent to tfun:finishes(b, a)
tfun:isInside(a, b) time:Instant
time:Interval
inv. time:inside
not time:after
not time:before
equivalent to tfun:hasInside(b, a)
tfun:isStartedBy(a, b) time:Interval
time:Interval
time:isStartedBy
inv. time:starts
tfun:starts(b, a)
tfun:starts(a, b) time:Interval
time:Interval
time:starts
inv. time:isStartedBy
tfun:isStartedBy(b, a)

These functions are yet to be implemented:

SPARQL Notes
tfun:hasBeginning(a, b)
tfun:hasEnd(a, b)
tfun:isFinishedBy(a, b)
tfun:isBeginningOf(a, b)
tfun:isDisjoint(a, b)
tfun:isEndOf(a, b)
tfun:isEquals(a, b)
tfun:isIn(a, b)
tfun:isMetBy(a, b)
tfun:isNotDisjoint(a, b)
tfun:isOverlappedBy(a, b)
tfun:isStartedBy(a, b)
tfun:meets(a, b)
tfun:overlaps(a, b)

Non-TIME functions

The following proposed functions are inspired by OWL TIME but not directly related to its predicates or classes:

SPARQL Notes
tfun:hasTemporalRelation(a, b) Returns the temporal relation between a & b
May have to run sequences of the other isDisjoint(a, b) functions to determine
tfun:toUNIXTime(a) Returns a UNIX Time representation of a xsd:dateTime or xsd:dateTimeStamp
May be extended for other TRS inputs
tfun:toXSDDateTimeStamp(a) Returns an XSD xsd:dateTimeStamp (UTC) representation of a UNIX time
May be extended for other TRS inputs

Implementation logic

Functions implemented test for every conceivable way that a temporal relation may be found to be true in given 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>

Vocabulary

The time functions, both implemented and to-be implemented, are listed in a SKOS vocabulary, the source files for which are given in the voc/ folder within this repository. The vocabulary is presented online in both RDF (turtle) and HTML (Markdown) formats from these source files, accessible via the namespace IRI:

Each function's IRI resolves to its Concept entry in the vocbaulary, for example, https://w3id.org/timefuncs/isContainedBy --> https://github.com/RDFLib/timefuncs/blob/master/voc/timefuncs.md#is-contained-by.

(The Markdown is auto-generated from the RDF using pyLODE)

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.1.4.tar.gz (13.6 kB view details)

Uploaded Source

Built Distribution

timefuncs-0.1.4-py3-none-any.whl (10.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: timefuncs-0.1.4.tar.gz
  • Upload date:
  • Size: 13.6 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.1.4.tar.gz
Algorithm Hash digest
SHA256 37ac9f569f157e27de09b3bebba98e3945f08e367621b4c7cdb04c3a65b0d2d1
MD5 6be43656f5ccaf14d7e024f06172e72f
BLAKE2b-256 323ba0e60eb1ac7cf174b532baf22eb8e388d4f53665e5aeba48e611a3f9150f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: timefuncs-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 10.2 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.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 2184a43efec45f6e475d490843141be9ab96874f49fe76125f072906b6888c30
MD5 ed5b5da53451d270f35653d28cffe2be
BLAKE2b-256 3aa024a9dbc9268a27bac681921cf013ad5286087a177daf5a521ebef635d177

See more details on using hashes here.

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