1 introduction

definition cited from SPARQL 1.1 Overview:

SPARQL 1.1 is a set of specifications that provide languages and protocols to query and manipulate RDF graph content on the Web or in an RDF store.

I roughly divide W3C's SPARQL 1.1 Specifications into 5 parts:

This note is dedicated to record SPARQL languages(query, update), and Jena's ARQ(an implementation) supports.

2 language

No one really want to read the specifications directly, especially the unpatients, including myself. So I choose to stand on the shoulders of giants. Section 2.1 Query is mostly cited from [1].

2.1 Query

At first glance, SPARQL Query's key words seems to be copyed from Relational Storage Query language SQL. Be careful, although the key words may be familiar, the underlying modeling method and storage are different.

2.1.1 SELECT

(1) a sample with projection and modifier:

# prefixes
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>

# select clause, with projection
SELECT ?sub ?prop ?obj
WHERE {
    ?sub ?prop ?obj.
}
LIMIT 0, 10 # using query modifier

The WHERE caluse is a specification of graph pattern, and each line in WHERE caluse is a triple, and should end with .. The notations that begin with ? are variables, and when SAPRQL endpoint generate a query answer, these variables should bind to a specific IRI resource.

(2) a sample cited from "Linked Data in Action" with additional RDF dataset and abbreviation of triple:

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX pos: <http://www.w3.org/2003/01/geo/wgs84_pos#>

SELECT ?name ?latitude ?longitude
# named graph
FROM <http://3roundstones.com/dave/me.rdf>
FROM <http://semanticweb.org/wiki/Special:ExportRDF/Michael_Hausenblas>
WHERE {
   ?person foaf:name ?name ;
      foaf:based_near ?near .# abbreviation
   ?near pos:lat ?latitude ;
      pos:long ?longitude .
}

?person foaf:name ?name ;foaf:based_near ?near. uses an abbreviation of turtle syntax. Here are some forms of turtle syntax abbreviations:

a :b c.  ==   a :b c;
a :e f.         :e f.

a :b c.  ==  a :b c;  ==  a :b c,d
a :b d.        :b d.

The FROM caluses define so-called additional RDF dataset, without FROM, the default graph of SPARQL endpoint is used.

(3) queries with named graph and blank nodes

PREFIX tbl: <http://www.w3.org/People/Berners-Lee/card#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT *
FROM NAMED <http://www.koalie.net/foaf.rdf>
FROM NAMED <http://heddley.com/edd/foaf.rdf>
FROM NAMED <http://www.cs.umd.edu/∼hendler/2003/foaf.rdf>
FROM NAMED <http://www.dajobe.org/foaf.rdf>
FROM NAMED <http://www.isi.edu/∼gil/foaf.rdf>
FROM NAMED <http://www.ivan-herman.net/foaf.rdf>
FROM NAMED <http://www.kjetil.kjernsmo.net/foaf>
FROM NAMED <http://www.lassila.org/ora.rdf>
FROM NAMED <http://www.mindswap.org/2004/owl/mindswappers>
WHERE {
    GRAPH ?originGraph {
        _:blank1 foaf:knows _:blank2.
        _:blank2 rdf:type foaf:Person;
                foaf:nick ?nickname;
                foaf:name ?realname
    }
}

The FROM NAMED <IRI> caluses denote named graphs, they are used to indentify Resource's data sources. The GRAPH ?originGraph {<graph-pattern>} is used to define named graphs' patterns, ?originGraph is called the variable of named graph. Also we can explicitly assign a particular in the GRAPH caluse, here is a example:

PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?nickname
WHERE {
    GRAPH <http://www.w3.org/People/Berners-Lee/card> {
        :blank3 foaf:nick ?nickname
    }
}

Notations like _:blank1 are blank nodes, they play the same role just like Resource variables, but they could not be included in the query answers. Blank nodes has 2 forms of notaions:

_:blank :prop obj.   == [] :prop obj. or
                         [ :prop obj]

_:blank :prop1 obj1. == [:prop1 obj1]
_:blank :prop2 obj2.      :prop2 obj2.

sub :prop1 _:blank.   == sub :prop1 [:prop2 obj].
_:blank :prop2 obj. 

_:blank :prop1 obj1; == [:prop1 obj1;
        :prop2 obj2.       :prop2 obj2].

_:blank :prop obj1,  == [:prop obj1, obj2]
              obj2.

Query Modifiers

(1) DISTINCT

DISTINCT is used to remove duplicated result in query answers.

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dbprop: <http://dbpedia.org/property/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT DISTINCT ?picture
WHERE {
    ?person rdfs:label "George Washington"@en;
        dbprop:occupation ?job;
        dbprop:birthPlace ?birthLoc;
        foaf:img ?picture
}
(2) REDUCED

Just like DISTINCT, excepte that SPARQL endpoint can return any number of duplicated results.

(3) ORDER BY

ORDER BY sort results using alphabetical order and numberic order, and uses ASC() order by default. Use DESC() to specify descented order.

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dbprop: <http://dbpedia.org/property/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?job ?birthLoc ?picture
WHERE {
    ?person rdfs:label "George Washington"@en;
        dbprop:occupation ?job;
        dbprop:birthPlace ?birthLoc;
        foaf:img ?picture
}
ORDER BY ?birthLoc DESC(?job)
(4) OFFSET, LIMIT

OFFSET and LIMIT should play with ORDER BY, used to generate streaming results.

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dbprop: <http://dbpedia.org/property/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?job ?birthLoc ?picture
WHERE {
    ?person rdfs:label "George Washington"@en;
        dbprop:occupation ?job;
        dbprop:birthPlace ?birthLoc;
        foaf:img ?picture
}
ORDER BY ?birthLoc DESC(?job)
OFFSET 0 LIMIT 10
(5) FILTER

FILTER is used to filter query answer result through some boolean conditions.

These conditions can be specified using: (a) a subset of XQuery, XPath operators and functions, or (b) SPARQL specific operators.

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dbprop: <http://dbpedia.org/property/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
SELECT ?prop ?object
WHERE {
    ?person rdfs:label "George Washington"@en;
        dbprop:presidentStart ?start;
        ?prop ?object.
    FILTER(xsd:integer(?start) + 4 <= xsd:integer(?object))
}

The position of FILTER in WHERE clause is not important.

(6) OPTIONAL

OPTIONAL is used to add more graph patterns in WHERE caluses, while does not restrict results when there are no query bindings for these patterns.

PREFIX ex: <http://www.example.com/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dbprop: <http://dbpedia.org/property/>
SELECT ?l1 ?l2 ?l3 ?l4
WHERE {
    ?person rdfs:label "George Washington"@en.
    ?l1 dbprop:namedFor ?person.
    OPTIONAL { ?l2 dbprop:blankInfo ?person }
    OPTIONAL { ?l3 ex:isNamedAfter ?person }
    OPTIONAL { ?person ex:wasFamousAndGaveNameTo ?l4 }
}
(7) UNION

UNION is used to aggregate results of two or more graph patterns to generate a new result.

PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT *
WHERE {
    { ?unknown foaf:gender "male" }
    UNION
    { ?unknown foaf:gender "female" } .

    { ?unknown foaf:interest <http://www.iamhuman.com> }
    UNION
    { ?unknown foaf:interest <http://lovebeinghuman.org> }
}

CAUTION: No examples are expressed in CONSTRUCT, ASK and DESCRIBE.

2.1.2 CONSTRUCT

CONSTRUCT is used to transform datas from RDF datasets to datas in another RDF dataset.

2.1.3 ASK

ASK is used when you want to confirm whether a given graph pattern is exist in the SPARQL endpoint, it returns a boolean result.

2.1.4 DESCRIBE

DESCRIBE is used when query clients do not know the details of the structure of data, the SPARQL endpoint decides how to organize the data.

2.2 Update

TODO

3 language implementation: Jena ARQ

Using Jena with Apache Maven:

<groupId>org.apache.jena</groupId>
<artifactId>apache-jena-libs</artifactId>
<type>pom</type>
<version>2.13.0</version>
</dependency>

3.1 API

3.1.1 query from model

see SelectQueryUsingModel.java

3.1.2 query from dataset

see SelectQueryUsingDataset.java

or paly with existed models:

Dataset dataset = DatasetFactory.create() ;
dataset.setDefaultModel(model) ;
dataset.addNamedModel("http://example/named-1", modelX) ;
dataset.addNamedModel("http://example/named-2", modelY) ;
 try(QueryExecution qExec = QueryExecutionFactory.create(query, dataset)) {
    ...
}

3.1.3 query from remote service

see SelectQueryUsingRemoteService.java

3.1.4 store resultset for later use

ResultSet results = qexec.execSelect() ;
results = ResultSetFactory.copyResults(results) ;
return results;

3.2 command line utilities

Classes are located in package arq.

(1) run a remote service query

java -cp .. arq.rsparql --service 'http://www.sparql.org/books/sparql'  \
   'PREFIX books:   <http://example.org/book/>  \
    PREFIX dc:      <http://purl.org/dc/elements/1.1/>  \
       SELECT ?book ?title WHERE   { ?book dc:title ?title }'

(2) run a remote service query with a local query file

java -cp .. arq.rsparql --service='http://www.sparql.org/books/sparql' \
 --file=query/books.rq

here query directory specified in --file arguments is in the CLASSPATH.

Other command line classes refer ARQ - Command Line Applications for more details.

(3) run with an OS script

cd $JENA_HOME/bin
./rsparql --service 'http://www.sparql.org/books/sparql' \
     'PREFIX books:   <http://example.org/book/> \
      PREFIX dc:      <http://purl.org/dc/elements/1.1/> \
      SELECT ?book ?title WHERE   { ?book dc:title ?title }'

# or
./rsparql --service='http://www.sparql.org/books/sparql' --file=./query/books.rq

3.3 other queries except SELECT

CONSTRUCT

QueryExecution qexec = ...;
Model resultModel = qexec.execConstruct() ;

DESCRIBE

QueryExecution qexec = ...;
Model resultModel = qexec.execDescribe() ;

ASK

QueryExecution qexec = ...;
boolean result = qexec.execAsk() 

resources and references

[1] Hebeler J, Fisher M, et al.Web 3.0与Semantic Web编程[M]. 清华大学出版社, 北京.2010

[2] SPARQLer - SPARQL online tools and resources collection

SPARQL1.1 101 Language and Jena support的更多相关文章

  1. Outline of Apache Jena Notes

    1 description 这篇是语义网应用框架Apache Jena学习记录的索引. 初始动机见Apache Jena - A Bootstrap 2 Content 内容组织基本上遵循Jena首页 ...

  2. Should You Build Your Own Backtester?

    By Michael Halls-Moore on August 2nd, 2016 This post relates to a talk I gave in April at QuantCon 2 ...

  3. OSCP Learning Notes - Privilege Escalation

    Privilege Escalation Download the Basic-pentesting vitualmation from the following website: https:// ...

  4. Spring.Net在Mvc4.0中应用的说明

    案例Demo:http://yunpan.cn/cJ5aZrm7Uybi3 访问密码 414b Spring.Net在Mvc4.0中应用的说明 1.引用dll 2.修改Global文件 (Spring ...

  5. 【腾讯Bugly干货分享】React Native项目实战总结

    本文来自于腾讯bugly开发者社区,非经作者同意,请勿转载,原文地址:http://dev.qq.com/topic/577e16a7640ad7b4682c64a7 “8小时内拼工作,8小时外拼成长 ...

  6. F#之旅2 - 我有特别的学F#技巧

    原文地址:https://swlaschin.gitbooks.io/fsharpforfunandprofit/content/learning-fsharp/ Learning F#Functio ...

  7. 【Java EE 学习 25 下】【网上图书商城js小技术点总结】

    1.日历控件的使用 日历控件源代码: /** * add auto hide when mouse moveout * * @version 1.0.1 * @date 2010-11-23 * @a ...

  8. Ubuntu 14.04 编译安装 boost 1.58

    简介 Boost is a set of libraries for the C++ programming language that provide support for tasks and s ...

  9. linux Centos 6.5 安装桌面环境GNOME

    在某种场合之下,我们使用的Linux还是要选择安装桌面环境的,所以在这里介绍一下如何给没有安装桌面环境的系统安装桌面环境.以Centos 6.5 为例演示一下如何安装桌面环境. 工具/原料 Linux ...

随机推荐

  1. How to generate a random number in R

    Generate a random number between 5.0 and 7.5x1 <- runif(1, 5.0, 7.5) # 参数1表示产生一个随机数x2 <- runif ...

  2. 激活、复制、使用R/3标准数据源(RSA5、RSA6、RSA1)

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  3. iscroll 使用及遇到的问题

    介绍: iscroll.js 是滑动事件.在手机上可以快速的滑动,用户体验很好.在线例子: 选择套餐 iScroll必须在调用之前实例化---用法 <script src="iscro ...

  4. hdu4987A simple probability problem.(凸包)

    链接 多校的最后一场,当时没看懂题意,看题目还以为是概率问题就没深看. 官方题解 对于他说的第一种,考虑长为L的线段 概率为2L/(pi*d), 可以理解,下面的就不知道在说啥了.. 按我初始的想法想 ...

  5. 字符串去空格 java , js和Jquery 方法

    1.  java方式 String.trim(); 2.js方式 function Trim(str) { return str.replace(/(^\s*)|(\s*$)/g, "&qu ...

  6. SSH框架流程详解

    解图: 由图可见,有三个框架{ ①. Struts_2 ②. Spring ③. Hibernate } 框架 作用 本质 同等于 Struts_2 实现MVC / 控制.跳转 过滤器(Filter) ...

  7. JVM实用参数——新生代垃圾回收

    JVM实用参数目录 JVM实用参数——新生代垃圾回收 概述 第1部分  新生代垃圾回收介绍 第2部分 参数介绍 参考 第1部分  新生代垃圾回收介绍 本部分,我们将关注堆(heap) 中一个主要区域, ...

  8. sqlserver存储过程批量插入数据

    在系统中经常会遇到向数据库中批量插入数据情况,存储过程中没有数组,只有通过字符串分割循环插入,下面是一个本人研究的一个例子: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 c ...

  9. Xcode6.1标准Framework静态库制作方法。工程转Framework,静态库加xib和图片。完美解决方案。

    http://www.cocoachina.com/bbs/read.php?tid-282490.html Xcode6.1标准Framework静态库制作方法.工程转Framework,静态库加x ...

  10. 关于edoc程序tomcat post提交中文乱码解决办法

    1修改源码代码中页面部分指定转码格式为UTF-8替换原来的gb2312或GBK 2源码新增 water.edoc.service.FileFilter 过滤器并在web.xml首行添加制定格式 3to ...