[Pellet-users] Help to construct a complex SPARQL query
João Paulo Sousa
jpaulo at ipb.pt
Mon Jan 7 11:33:50 UTC 2008
Hi,
I'm having some difilcuties to make a complex SPARQL query. I already
consult several documents like the W3C, Jena Documents and examples
that I found in this list and different sites.
I have two ontologies (actor.owl and spatial.owl) the first describe a
actor like a user and the second describes a spatial location. The
actor has objectproperty "currentLocation" that told here is the user.
The two ontologies with data are published and on-line.
My objective is to make a sparql query that tell to me the current
location of the user that Eva knows. In this case the user Eva knows
the user Paulo and his current location is Room Multimedia.
The actual query only gives to me the individual that Eva knows.
Thanks in advance,
/jpaulo.
package icasv01;
import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntModelSpec;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.query.ResultSetFormatter;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.sparql.util.IndentedWriter;
import org.mindswap.pellet.jena.PelletQueryExecution;
import org.mindswap.pellet.jena.PelletReasonerFactory;
/**
*
* @author Administrator
*/
public class NewMain1 {
static Boolean debug = true;
static String queryBegin = "PREFIX actor: <http://icas.ipb.pt/myontologies/actor.owl#
> \r\n" +
"PREFIX space: <http://icas.ipb.pt/myontologies/spatial.owl#
> \r\n" +
"PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-
ns#>" ;
static String actordata = "http://icas.ipb.pt/myontologies/icasactordata.owl#
";
static String spatialdata = "http://icas.ipb.pt/myontologies/spatial.owl#
";
static String ns = "http://icas.ipb.pt/myontologies/actor.owl";
static OntModelSpec REASONER = PelletReasonerFactory.THE_SPEC;
static OntModel model;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
FriendsAroundMe();
// jenaquery();
}
public static void FriendsAroundMe() {
model =
ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC);
model.read(actordata);
model.read(spatialdata);
String actorName = "Eva";
String queryStr = queryBegin + "SELECT ?knows \r\n" +
" WHERE {\r\n" +
"?x actor:knows ?knows . \r\n" +
"?x actor:name ?name . \r\n" +
"FILTER regex(?name, \"" + actorName + "\")\r\n" +
"}";
RunQuery(queryStr);
}
public static void RunQuery(String queryStr) {
Query query = QueryFactory.create(queryStr);
query.serialize(new IndentedWriter(System.out, true));
QueryExecution queryExec = new PelletQueryExecution(query,
model);
ResultSet results = queryExec.execSelect();
if (debug) {
try {
printQueryResults("Query result: ", queryExec, query);
} catch (Exception e) {
System.out.println(e);
}
}
}
public static void printQueryResults(String header,
QueryExecution qe, Query query) throws Exception {
System.out.println("\n" + header);
ResultSet results = qe.execSelect();
ResultSetFormatter.out(System.out, results, query);
System.out.println();
}
}
More information about the Pellet-users
mailing list