SWAD-Europe Deliverable 7.4: Databases, Query, API, Interfaces. Public release of a "strawman" query language implementation incorporating current best practice.

Project name:
Semantic Web Advanced Development for Europe (SWAD-Europe)
Project Number:
Workpackage name:
7: Databases, Query, API, Interfaces
Workpackage description:
Deliverable title:
Public release of a "strawman" query language implementation incorporating current best practice, which may use or build upon existing implementations. Open Source/free software.
Libby Miller
Public release of a "strawman" query language implementation incorporating current best practice, which may use or build upon existing implementations. Open Source/free software.

Document started 2003-08-19; this revision 2003-11-05. This is a completed report.

Comments on this document are welcome and should be sent to the public-esw@w3.org list. An archive of this list is available at http://lists.w3.org/Archives/Public/public-esw/


1. Introduction
2. Sample usage
3. Squish BNF
4. MySQL table layout used
5. References

1. Introduction

This document describes some features of a small RDF query implementation ('Tinkling')[1] written in Java. Its API is described in more detail in a companion document[2]. Other work funded by the SWAD-Europe project in this area includes work on RDF Query testcases[6] undertaken in collaboration with members of the Semantic Web Interest Group. A snapshot of Tinkling may be downloaded[7].

Those interested in RDF and Querying should also look at the work of the W3C Data Access Working Group[8], which is standardising work in this area; and those interested in RDf query in Java might consider using the well-supported Jena code from HP Labs[9].


The Squish query language is an extended version of R.V.Guha's rdfDB query language[3]. Guha's is a simple, conjunctive SQL-like query language, where you SELECT variables FROM a database WHERE (predicate subject object), where predicate subject and object can be uris or variables, or in the case of object, literals (in inverted commas). Guha also has a syntax for insert information into the database.

Squish differs in three ways:

More information is in the BNF. Here is a sample query showing all these features:

select ?name 
from http://swordfish.rdfweb.org/people/libby/rdfweb/webwho.xrdf
(foaf:name ?person ?name)
and ?name ~ 'libby'
using foaf for http://xmlns.com/foaf/0.1/

try this query


Tests and applications are available in the distribution. Tinkling is being used to power two demonstration websites[4], [5] backed with MySQL and containing about 350,000 triples.


The SQL implementation does not return provenance information via query or API, although it is stored. In the in-memory version provenance can be returned like this:

 Graph gr = new Graph(uri, Util.RDFXML);
java.sql.ResultSet r=gr.askSquish(query);

        Node n = (Node)r.getNode("name");
        String source=n.getGraph().getBase();

2. Sample usage

Querying an in-memory Graph with Squish

Create a Graph and populate it:

 Graph gr = new Graph(uri, Util.RDFXML);

Ask a Squish query, returning a ResultSet (see Squish BNF).

java.sql.ResultSet r=gr.askSquish(query);

Querying an in-memory UnionGraph with Squish

Create a UnionGraph and populate it:

 Graph gr = new Graph(uri, Util.RDFXML);
 UnionGraph ug=new UnionGraph();

Ask a Squish query.

java.sql.ResultSet r=ug.askSquish(query);

Querying an SQLGraph with Squish

Create an SQLGraph and populate it:

 Graph gr = new Graph(uri, Util.RDFXML);
 SQLGraph sq = new SQLGraph();       

Ask a Squish query.

java.sql.ResultSet r=sq.askSquish(query);

3. Squish BNF

This was originally generated using javacc and jjdoc. I've made some alterations to make it more readable. Here are some example queries.

The query language is similar to and based on R. V. Guha's RDFDB QL

CompilationUnit ::= Query <EOF>
Query ::= SelectClause ( FromClause )? TriplePatternClause ( ConstraintClause )? ( UsingClause )?
SelectClause ::= 'SELECT' VarList
FromClause ::= 'FROM' UriList
TriplePatternClause ::= 'WHERE' TriplePatternList
ConstraintClause ::= 'AND' ConstraintList
UsingClause ::= 'USING' ( ForList )+
TriplePatternList ::= TriplePattern ( TriplePattern )*
TriplePattern ::= '(' VarOrLiteral VarOrLiteral VarOrLiteral ')'
VarOrLiteral ::= Var
| Literal
Var ::= '?' Identifier
VarList ::= Var ( ( ',' )? Var )*
UriList ::= UriLiteral ( ( "," )? UriLiteral )*
ConstraintList ::= Expression ( 'AND' Expression )*
ForList ::= Identifier 'FOR' UriLiteral
Expression ::= Var SomeFunction
SomeFunction ::= ( NumExpression | StringExpression )+
NumExpression ::= ( '>' | '<' | '==' | '=' | '!=' | '<=' | '>=' ) NumericLiteral
StringExpression ::= ( 'like' | 'ne' | 'eq' | '~' ) Literal
Literal ::= TextLiteral
| UriLiteral
| NumericLiteral
NumericLiteral ::= An integer
| A floating point number
UriLiteral ::= A letter followed by none or more letters, numbers or other characters allowed in RFC 2396
TextLiteral ::= One or more letters or numbers enclosed in inverted commas
Identifier ::= A letter followed by optional numbers and letters.

4. MySQL table layout used

mysql> describe triples;
| Field      | Type         | Null | Key | Default | Extra |
| subject    | int(11)      | YES  | MUL | NULL    |       |
| predicate  | int(11)      | YES  | MUL | NULL    |       |
| object     | int(11)      | YES  | MUL | NULL    |       |
| source     | varchar(255) | YES  | MUL | NULL    |       |
| isresource | tinyint(1)   | YES  |     | NULL    |       |

mysql> describe resources;
| Field   | Type    | Null | Key | Default | Extra |
| keyhash | int(11) | YES  | MUL | NULL    |       |
| value   | text    | YES  | MUL | NULL    |       |

MySQL SQL table creation

libby@bender:/usr/bin$ ./mysqldump test
-- MySQL dump 8.21
-- Host: localhost    Database: test
-- Server version       3.23.49-log

-- Table structure for table 'resources'

CREATE TABLE resources (
  keyhash int(11) default NULL,
  value text,
  UNIQUE KEY khval (keyhash,value(255)),
  KEY kh (keyhash),
  KEY val (value(255))

-- Dumping data for table 'resources'

-- Table structure for table 'triples'

CREATE TABLE triples (
  subject int(11) default NULL,
  predicate int(11) default NULL,
  object int(11) default NULL,
  source varchar(255) default NULL,
  isresource tinyint(1) default NULL,
  UNIQUE KEY spos (subject,predicate,object,source),
  KEY sub (subject),
  KEY pred (predicate),
  KEY obj (object),
  KEY src (source),
  KEY sp (subject,predicate),
  KEY so (subject,object),
  KEY po (predicate,object)

-- Dumping data for table 'triples'

5. References

[1] Tinkling, a small RDF API and Query Language implementation
Libby Miller, November 2003.

[2] SWAD-Europe Deliverable 7.3: Public release of reference implementation of an RDF API
Libby Miller, November 2003.

[3] rdfDB query language
R. V. Guha, 2000.

[4] FOAF (Friend of a friend) database interface
Libby Miller, 2002-2003.

[5] Codepiction image search demonstrator
Libby Miller, 2001-2003.

[6] Summary of RDF query tests work, February-May 2003
Libby Miller, 2003.

[7] RDF Query implementation snapshot, November 2003.

[8] W3C Data Acces Working Group

[9] Jena 2 - A Semantic Web Framework