Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Raji GHAWI
26/01/2009
Outline
Query Execution
Query Analysis
26/01/2009
1. Query Execution
Query Execution
Read a file into a model
26/01/2009
Query Execution
Put the query as a string
PREFIX my:<http://www.something.com/myontology#>
PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT ?stud ?dip
WHERE {
?stud
my:enrolledIn
}
?dip.
String sparqlQuery =
"PREFIX my:<http://www.something.com/myontology#>\n" +
"PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n" +
"\n" +
"SELECT ?stud ?dip \n" +
"WHERE {\n" +
"
?stud
my:enrolledIn
?dip.\n" +
"} ";
26/01/2009
Query Execution
Execute the Query
26/01/2009
Query Execution
Print Query Results
result set
textual format
standard output
----------------------------------| stud
| dip
|
===================================
| my:Simon_Thevenin | my:M2_BDIA |
| my:Raji_Ghawi
| my:Doctorat |
| my:Kamel_Boulil
| my:M2_BDIA |
-----------------------------------
26/01/2009
output
XML format
ResultSetFormatter.outputAsXML(System.out, results);
26/01/2009
<?xml version="1.0"?>
<sparql
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:xs="http://www.w3.org/2001/XMLSchema#"
xmlns="http://www.w3.org/2005/sparql-results#" >
<head>
<variable name="stud"/>
<variable name="dip"/>
</head>
<results ordered="false" distinct="false">
<result>
<binding name="stud">
<uri>http://www.something.com/myontology#Simon_Thevenin</uri>
</binding>
<binding name="dip">
<uri>http://www.something.com/myontology#M2_BDIA</uri>
</binding>
</result>
<result>
<binding name="stud">
<uri>http://www.something.com/myontology#Raji_Ghawi</uri>
</binding>
<binding name="dip">
<uri>http://www.something.com/myontology#Doctorat</uri>
</binding>
</result>
<result>
<binding name="stud">
<uri>http://www.something.com/myontology#Kamel_Boulil</uri>
</binding>
<binding name="dip">
<uri>http://www.something.com/myontology#M2_BDIA</uri>
</binding>
</result>
</results>
</sparql>
8
output
Query Execution
Save Query Results to String
Query Execution
Retrieve Query Solutions
ResultSet results = qe.execSelect();
List vars = results.getResultVars();
while(results.hasNext()) {
QuerySolution qs = results.nextSolution();
System.out.println("--------- solution ---------");
for (int i = 0; i < vars.size(); i++) {
String var = vars.get(i).toString();
RDFNode node = qs.get(var);
System.out.println(var + "\t" + node.toString());
}
}
output
10
output
11
2. Query Analysis
Query Analysis
PREFIX my:<http://www.something.com/myontology#>
PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT ?stud ?modName
WHERE {
?stud
rdf:type
my:Student .
?stud
my:enrolledIn
?dip .
?dip my:hasModule ?mod .
?mod my:moduleName
?modName.
FILTER
(?modName='Databases').
}
26/01/2009
13
Query Analysis
Put the Query in a String
String sparqlQuery =
"PREFIX my:<http://www.something.com/myontology#>\n" +
"PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n" +
"\n" +
"SELECT ?stud ?modName \n" +
"WHERE {\n" +
"
?stud
rdf:type
my:Student .\n" +
"
?stud
my:enrolledIn
?dip .\n" +
"
?dip
my:hasModule
?mod .\n" +
"
?mod
my:moduleName
?modName.\n" +
"
FILTER(?modName='Databases').\n" +
"} ";
26/01/2009
14
Query Analysis
Create the Query
----------");
output
15
Query Analysis
Prefix Mapping
26/01/2009
output
16
Query Analysis
Retrieve Result Variables
output
query.isQueryResultStar()
query.isDistinct()
26/01/2009
17
Query Analysis
Retrieve All Variables
output
26/01/2009
18
Query Analysis
Fetch Query Elements
ElementGroup eg = (ElementGroup) query.getQueryBlock().getPatternElement();
List elemList = eg.getElements();
for(int i=0; i<elemList.size(); i++){
Element elem = (Element) elemList.get(i);
try{
if (elem instanceof ElementOptional) {
ElementOptional elOp = (ElementOptional) elem;
// ....
} else if (elem instanceof ElementFilter) {
ElementFilter elf = (ElementFilter) elem;
// ....
} else if (elem instanceof ElementTriplePattern) {
ElementTriplePattern etp = (ElementTriplePattern) elem;
// ....
}
} catch(ClassCastException e){
e.printStackTrace();
}
}
26/01/2009
19
Query Analysis
ElementOptional
ElementOptional elOp = (ElementOptional) elem;
Iterator iter = elOp.varsMentioned().iterator();
// ....
ElementGroup newElemGrp = (ElementGroup) elOp.getElement();
List elemList = eg.getElements();
for(int i=0; i<elemList.size(); i++){
// ....
}
26/01/2009
20
Query Analysis
ElementFilter
ElementFilter elf = (ElementFilter) elem;
Iterator iter = elf.varsMentioned().iterator();
// ....
Expr expr = elf.getConstraint().getExpr();
// ....
26/01/2009
21
Query Analysis
Expr
E_LogicalAnd
E_LogicalOr
E_Equals
E_NotEquals
E_LessThan
E_LessThanOrEqual
E_GreaterThan
E_GreaterThanOrEqual
getLeft()
getRight()
getSubExpr()
E_LogicalNot
E_Regex
NodeVar
getVarName()
if (expr instanceof E_LogicalAnd) {
E_LogicalAnd and = (E_LogicalAnd) expr;
NodeValueInteger
Expr left = and.getLeft();
NodeValueFloat
Expr right = and.getRight();
asString()
NodeValueDecimal
//
NodeValueString
} else if (expr instanceof E_LogicalOr) {
...
// .. ..
}
// .. ..
else if (expr instanceof E_Regex) {
E_Regex re = (E_Regex) expr;
String pattern = ((NodeValueString) re.getPattern()).asString();
String varName = re.getRegexExpr().getVarName().toString();
// .. ..
}
26/01/2009
22
Query Analysis
ElementTriplePattern
ElementTriplePattern etp = (ElementTriplePattern) elem;
Triple triple = etp.getTriple();
Node subject = triple.getSubject();
Node predicate = triple.getPredicate();
Node object = triple.getObject();
boolean isURI()
boolean isLiteral()
boolean isVariable()
Subject
Variable
26/01/2009
Predicate
URI
Object
Literal
23
?dip.
"BDIA".
Subject
Predicate
Object
URI
Literal
Variable
26/01/2009
24
Query Analysis
OrderBy
if(query.getOrderBy() != null){
Iterator orderBy = query.getOrderBy().iterator();
while (orderBy.hasNext()) {
SortCondition sc = (SortCondition) orderBy.next();
Expr exp = sc.getExpression();
if(exp.isVariable()){
String obv = exp.getVarName();
// ....
}
}
}
26/01/2009
25
References
http://jena.sourceforge.net/ARQ/
http://www.ibm.com/developerworks/xml/library/j-sparql
26/01/2009
26