简体   繁体   中英

RDF SPARQL query multiple types

I am trying to parse a rdf graph using SPARQL. Naturally in a graph my entities can be more than one type at a time.

How does a SPARQL query have to look like so that I can check for both types?

How my graph looks like:

<ns0:TrainArrival rdf:about="http://my.url.com/ontologies/mash-up#TrainArrival-7a60055b-1e7f-464d-b42e-b1839d623b69">
  <ns0:arrivalTime rdf:datatype="&xsd;dateTime">0001-01-01T00:26:00</ns0:arrivalTime>
  <ns0:description rdf:datatype="&xsd;string">Berlin - Düsseldorf - Aachen</ns0:description>
  <ns0:name rdf:datatype="&xsd;string">ICE 123</ns0:name>
  <ns3:primaryTopic xmlns:ns3="http://xmlns.com/foaf/0.1/" rdf:resource="http://my.url.com/ontologies/mash-up#TrainArrival-7a60055b-1e7f-464d-b42e-b1839d623b69" />
</ns0:TrainArrival>

<ns0:TrainArrival rdf:about="http://my.url.com/ontologies/mash-up#TrainArrival-88e52944-7d64-4241-bed0-61a28615e385">
  <ns0:arrivalTime rdf:datatype="&xsd;dateTime">0001-01-01T00:00:00</ns0:arrivalTime>
  <ns0:description rdf:datatype="&xsd;string">Duisburg - Düsseldorf - Köln - Aachen</ns0:description>
  <ns0:name rdf:datatype="&xsd;string">RE 1</ns0:name>
  <rdf:type rdf:resource="http://my.url.com/ontologies/mash-up#PrimaryInformation" />
  <ns4:primaryTopic xmlns:ns4="http://xmlns.com/foaf/0.1/" rdf:resource="http://my.url.com/ontologies/mash-up#TrainArrival-88e52944-7d64-4241-bed0-61a28615e385" />
</ns0:TrainArrival>

The second TrainArrival has an additional type "PrimaryInformation". How can I check if an Entity has both types, TrainArrival + PrimaryInformation?

My Query right now:

public String queryStringTrain =
        "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> "+
                "PREFIX ns0: <http://my.url.com/ontologies/mash-up#> "+
                "PREFIX ns1: <http://xmlns.com/foaf/0.1/> "+
                "SELECT ?object ?type ?type2 ?name ?arrivalTime " +
                "WHERE { " +
                "?object rdf:type ?type . " +
                "?object ns0:name ?name . " +
                "?object ns0:arrivalTime ?arrivalTime . " +
                "OPTIONAL {?object rdf:type ?type2 } " +
                "}";

I then try to check like this:

QuerySolution soln = results.nextSolution();

String type = soln.get("type").toString();
if (soln.contains("type2")) {
   String type2 = soln.getResource("type2").toString();
}

I get exceptions stating:

com.hp.hpl.jena.rdf.model.impl.ResourceImpl cannot be cast to com.hp.hpl.jena.rdf.model.Literal

What am I missing? Thanks in advance!

{
  ?object rdf:type ns0:TrainArrival ;
          rdf:type ns0:PrimaryInformation ;
}

will match having both TrainArrival and PrimaryInformation

Based on the exception you are getting, it seems that one of the variables is not binding properly to a resource. I can't check this, but have you tried rewriting String type2 = soln.getResource("type2").toString(); to String type2 = soln.get("type2").toString();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM