#!/usr/bin/perl -Tw
#
# query.pl - Redland RDF Query demo
#
# $Id: query.pl 10586 2006-03-05 07:49:32Z cmdjb $
#
# Copyright (C) 2004-2006, David Beckett http://purl.org/net/dajobe/
# Copyright (C) 2004-2005, University of Bristol, UK http://www.bristol.ac.uk/
#
# This package is Free Software and part of Redland http://librdf.org/
#
# It is licensed under the following three licenses as alternatives:
# 1. GNU Lesser General Public License (LGPL) V2.1 or any newer version
# 2. GNU General Public License (GPL) V2 or any newer version
# 3. Apache License, V2.0 or any newer version
#
# You may not use this file except in compliance with at least one of
# the above three licenses.
#
# See LICENSE.html or LICENSE.txt at the top of this package for the
# complete terms and further detail along with the license texts for
# the licenses in COPYING.LIB, COPYING and LICENSE-2.0.txt respectively.
#
#
#
# CHANGE THIS FOR YOUR CONFIGURATION
$::ROOT_DIR='/somewhere';
use strict;
# Helps with broken web requests (missing headers)
$ENV{'Content-Length'}||=0;
# Tainting, dontcha know
$ENV{'PATH'}="/bin:/usr/bin:/usr/local/bin";
# Standard perl modules
use CGI;
use LWP::Simple;
use URI::URL;
# Configuration
my(@query_languages)=qw(rdql sparql),
my(%query_language_labels)=('rdql' =>'RDQL',
'sparql' =>'SPARQL');
my $default_query_language='sparql';
my $example_foaf_uri='http://purl.org/net/dajobe/webwho.xrdf';
my $example_rss_uri='http://www.w3.org/2000/08/w3c-synd/home.rss';
my(%query_examples)=(
'rdql' => [
{ Q => <
EOT
, D => $example_foaf_uri
, T => 'A FOAF query that finds the names of all the people in the graph'
} ,
{ Q => <
EOT
, D => $example_foaf_uri
, T => 'A FOAF query that finds all people with a name and an IRC nick'
},
{ Q => <<'EEEEE'
select ?name, ?archives
where
(?list rdf:type doaml:MailingList)
(?list doaml:name ?name)
(?list doaml:archives ?archives)
and ?name =~ /p3p/
using doaml for
EEEEE
, D => 'http://www.doaml.net/doaml/w3ml/Lists.rdf'
, T => 'Print the name and archive URIs of W3C mailing lists about P3P as described by DOAML'
},
],
'sparql' => [
{ Q => <
PREFIX foaf:
SELECT DISTINCT ?name
WHERE {
?x rdf:type foaf:Person .
?x foaf:name ?name
}
ORDER BY ?name
EOT
, D => $example_foaf_uri
, T => 'A FOAF query that finds the names of all the people in the graph'
}
,
{ Q => <
PREFIX foaf:
SELECT ?nick, ?name
WHERE { ?x rdf:type foaf:Person . ?x foaf:nick ?nick . ?x foaf:name ?name }
EOT
, D => $example_foaf_uri
, T => 'A FOAF query that finds all people with a name and an IRC nick'
}
,
{ Q => <
PREFIX dc:
PREFIX rss:
SELECT ?title ?description
WHERE { ?item rdf:type rss:item .
?item rss:title ?title .
?item rss:description ?description }
AAAAA
, D => $example_rss_uri
, T => 'An RSS 1.0 query that finds all the items in the feed'
},
{ Q => <<'CCCCC'
PREFIX rdf:
PREFIX iemsr:
PREFIX rdfs:
SELECT $number $name $description
WHERE {
$r rdf:type iemsr:RootDataElement .
$n iemsr:isChildOf $r .
$n iemsr:refNumber $number .
$n rdfs:label $name .
$n rdfs:comment $description
}
CCCCC
, D => 'http://www.ukoln.ac.uk/projects/iemsr/terms/LOM/'
, T => 'Find all LOM root elements in the LOM encoded for the JISC IE Schema Registry (my current project)'
},
{ Q => <<'DDDDD'
PREFIX rdf:
PREFIX doap:
PREFIX foaf:
SELECT $description $maintainerName
WHERE {
$project rdf:type doap:Project .
$project doap:description $description .
$project doap:maintainer $m .
$m foaf:name $maintainerName
}
DDDDD
, D => 'http://svn.usefulinc.com/svn/repos/trunk/doap/examples/gnome-bluetooth-doap.rdf'
, T => 'Print the description of a project and maintainer(s) using DOAP'
},
{ Q => <<'EEEEE'
PREFIX rdf:
PREFIX doaml:
SELECT ?name ?archives
WHERE {
?list rdf:type doaml:MailingList .
?list doaml:name ?name .
?list doaml:archives ?archives .
FILTER REGEX(?name, "p3p")
}
EEEEE
, D => 'http://www.doaml.net/doaml/w3ml/Lists.rdf'
, T => 'Print the name and archive URIs of W3C mailing lists about P3P as described by DOAML'
},
{ Q => <<'optional-example1'
PREFIX rdf:
PREFIX foaf:
SELECT ?name ?nick
WHERE {
?x rdf:type foaf:Person .
?x foaf:name ?name .
OPTIONAL { ?x foaf:nick ?nick }
}
optional-example1
, D => $example_foaf_uri,
, T => 'Print the names and optional nicks of people in my FOAF file where available'
},
{ Q => <<'podcasts',
PREFIX rdf:
PREFIX dc:
PREFIX rss:
PREFIX enc:
SELECT ?title ?enc ?len
WHERE {
?item rdf:type rss:item .
?item rss:title ?title .
?enclosure rdf:type enc:Enclosure .
?item enc:enclosure ?enclosure .
?enclosure enc:url ?enc .
?enclosure enc:type ?type .
?enclosure enc:length ?len .
FILTER regex(?type, "audio/mpeg")
}
podcasts
,
, D => 'http://B4mad.Net/datenbrei/feed/rdf',
, T => 'What podcasts have you got in your RSS feed? (you will need an RSS feed using the enclosures vocab) ',
},
{ Q => <<'PERIODIC',
PREFIX table:
PREFIX xsd:
SELECT ?name ?symbol ?weight ?number
WHERE {
?element table:group ?group .
?group table:name "Noble gas"^^xsd:string .
?element table:name ?name .
?element table:symbol ?symbol .
?element table:atomicWeight ?weight .
?element table:atomicNumber ?number
}
ORDER BY ASC(?name)
PERIODIC
, D => 'http://www.daml.org/2003/01/periodictable/PeriodicTable.owl',
, T => 'What are the Noble Gases?',
},
{ Q => <<'DAWG',
PREFIX collab:
SELECT ?desc ?R
WHERE {
?issue collab:shortDesc ?desc;
collab:resolveRecord ?R
}
DAWG
, D => 'http://www.w3.org/2000/06/webdata/xslt?xslfile=http%3A%2F%2Fwww.w3.org%2F2003%2F11%2Frdf-in-xhtml-processor&xmlfile=http%3A%2F%2Fwww.w3.org%2F2001%2Fsw%2FDataAccess%2Fissues',
, T => 'What are the RDF DAWG issues?',
},
{ Q => <<'ATOM',
PREFIX rdf:
PREFIX dc:
PREFIX rss:
PREFIX atom:
SELECT ?item ?title ?date
WHERE { ?item rdf:type rss:item .
?item rss:title ?title .
?item atom:updated ?date }
ORDER BY DESC(?date)
LIMIT 10
ATOM
, D => 'http://www.tbray.org/ongoing/ongoing.atom',
, T => 'What are the last 10 updated items in an atom feed?',
},
{ Q => <<'BRUNEL',
PREFIX :
PREFIX bio:
PREFIX foaf:
SELECT ?name ?birthDate ?deathDate
WHERE {
?bridge a :Bridge;
foaf:maker ?person [
foaf:name ?name;
bio:event [
a bio:Birth;
bio:date ?birthDate
];
bio:event [
a bio:Death;
bio:date ?deathDate
]
]
}
BRUNEL
, D => 'http://www.w3.org/2003/01/geo/rdfgml/tests/mixing-eg1.xml',
, T => 'Who made a bridge in Bristol and what birth/death dates did they have?',
},
{ Q => <<'REDLAND-NEWS',
PREFIX dc:
PREFIX rss:
SELECT ?item ?title ?date
WHERE {
?item a rss:item ;
rss:title ?title ;
dc:date ?date
}
ORDER BY DESC(?date)
LIMIT 10
REDLAND-NEWS
D => 'http://librdf.org/NEWS.rdf http://librdf.org/raptor/NEWS.rdf http://librdf.org/rasqal/NEWS.rdf http://librdf.org/bindings/NEWS.rdf',
T => 'Find the most recent 10 news items about Redland from multiple RSS 1.0 feeds',
},
]
);
my(%query_blurbs)=(
'rdql' => <Documentation on RDQL is available in the specification and the Jena RDQL tutorial
See the status of RDQL support in Rasqal
EOT
,
'sparql' => <Documentation on SPARQL is available in the SPARQL Query Language for RDF, W3C Working Draft, 12 October 2004
NOTE Not all of SPARQL is implemented. See the status of SPARQL support in Rasqal. Current unimplemented features include UNION and full XSD datatype comparisons (dates, decimal) and promotions, GRAPH and deeply grouped graph patterns {...}.
EOT
);
my $log_file="$::ROOT_DIR/logs/query.log";
my $max_stream_size=200;
my $max_result_size=200;
my(%namespaces)=(
'rdf' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
'rdfs' => 'http://www.w3.org/2000/01/rdf-schema#',
'dc' => 'http://purl.org/dc/elements/1.1/',
'owl' => 'http://www.w3.org/2002/07/owl#',
# 'xsd' => 'http://www.w3.org/2001/XMLSchema#',
'foaf' => 'http://xmlns.com/foaf/0.1/',
'dcterms' => 'http://purl.org/dc/terms/',
'bot' => 'http://www.w3.org/2001/sw/Europe/200401/bot/terms\#',
);
# Redland perl modules
use RDF::Redland;
# $RDF::Debug=1;
######################################################################
# Subroutines
sub log_action ($$$;$) {
my($host, $db, $message, $now)=@_;
$now ||= time;
return unless open (LOG, ">>$log_file");
my($sec,$min,$hour,$mday,$mon,$year)=gmtime $now;
my $date=sprintf("%04d-%02d-%02dT%02d:%02d:%02dZ",1900+$year,$mon+1,$mday,$hour,$min,$sec);
$message =~ s/[\n\s]+/ /gs
if $message;
print LOG "$host $date $db $message\n";
close(LOG);
}
sub end_page($) {
my $q=shift;
print <<'EOT';
The source code of this demonstration is available in the Redland
bindings distribution as demos/query.pl or from the
Redland website
EOT
print qq{
\n\nDave Beckett
\n\n