SQWRL

Martin O'Connor edited this page Aug 12, 2016 · 24 revisions

SQWRL (Semantic Query-Enhanced Web Rule Language; pronounced squirrel) is a SWRL-based query language that provides SQL-like operators for extracting information from OWL ontologies.

Table of Contents

SQWRL Language Features

The language provides two sets of query operators:

  • Core Operators The core language uses a SWRL rule antecedent as a pattern specification and replaces the rule consequent by SQWRL selection operators. These operators are documented here.
  • Collection Operators To support more advanced querying capabilities, SQWRL has a selection of collection operators that provide grouping and aggregation functionality, and limited forms of negation as failure and disjunction. These operators are documented here.

This document assumes that the reader is already familiar with SWRL. An introduction can be found here. A quick introduction to SQWRL can be found in:

Running SQWRL Queries

Two mechanisms are provided by the SWRLAPI to execute SQWRL queries: (1) a Java API that provides a JDBC-like interface, called the SQWRL Query API, which can be used to execute queries and retrieve query results in Java applications, and (2) a graphical user interface called the SQWRL Query Tab that supports interactive querying and results display.

Example SQWRL Queries

The following SQWRL queries use built-ins from the SWRLAPI built-in libraries.

Calculate the circumference of a circle:

    Circle(?c)  ^ hasRadius(?c, ?r) ^
    swrlm:eval(?circumference, "2 * pi * r", ?r)
    -> sqwrl:select(?r, ?circumference) 

Calculate how many days have passed since the 1st of January, 2016:

    temporal:duration(?d, "2015-01-01", "now", "days") 
    -> sqwrl:select(?d)

List all individuals in an ontology:

    owl:Thing(?i) -> sqwrl:select(?i)

List all declared classes:

    tbox:cd(?c) -> sqwrl:select(?c)

List all OWL asserted superclasses of class Person:

    tbox:sca(Person, ?c) -> sqwrl:select(?c) ^ sqwrl:orderBy(?c)

List all pairs of disjoint data properties:

    rbox:djdpa(?dp1, ?dp2) -> sqwrl:select(?dp1, ?dp2) ^ sqwrl:orderBy(?dp1, ?dp2)

List all irreflexive object properties:

    rbox:iropa(?p) -> sqwrl:select(?p) ^ sqwrl:orderBy(?p)

List all data properties with a boolean value of true:

    abox:dpaa(?s, ?p, true) -> sqwrl:select(?p)

List all values of data property hasAge:

    abox:dpaa(?s, hasAge, ?v) -> sqwrl:select(?v)

List all object properties with a domain of Person:

    tbox:opda(?p, Person) -> sqwrl:select(?p)

Return a random number between 0 and 1:

    swrlm:eval(?r, "rand()") -> sqwrl:select(?r) 

Other examples of SQWRL queries can be found here.

Queries and Rules

SQWRL queries can operate in conjunction with SWRL and can thus be used to retrieve knowledge inferred by SWRL rules.

Assume, for example, the following rule that classifies persons older than 17 as adults:

 Person(?p) ^ hasAge(?p, ?age) ^ swrlb:greaterThan(?age, 17) -> Adult(?p)

A query to list all the adults in an ontology can be written:

 Adult(?p) -> sqwrl:select(?p)

Interoperation with other Built-In Libraries

SQWRL queries can freely use any SWRL built-in libraries. The list of built-in libraries provided by the SWRLAPI can be found here. Users may also develop their own built-in libraries for use in SQWRL queries using the SWRLTab's built-in bridge mechanism.

The ability to freely define and use built-ins in a query provides a means of continuously expanding the power of the query language.

Semantics of SQWRL

SQWRL queries operate on known individuals in the currently loaded OWL ontology. It is very important to note that SQWRL provides no way of accessing the information it accumulates from within a rule so query results cannot be written back to the ontology. There is no way, for example, to insert the result of a computed aggregate count back into the ontology. Such a mechanism could invalidate OWL's open world assumption and lead to nonmonotonicity.

While SQWRL's operators are implemented as SWRL built-ins, they do not operate like standard built-ins. Unlike most built-ins, they do not evaluate their arguments and return true if the arguments satisfy some predicate. Instead, they always return true and act as accumulators and build up table-based data structures outside of an ontology. However, they are side-effect free in terms of this ontology - they do not perform any ontology modifications. Crucially, they do not violate SWRL's semantics.

Unlike OWL and SWRL, SQWRL adopts the unique name assumption when querying.