RDF SPARQL 查询多种类型

RDF SPARQL query multiple types

我正在尝试使用 SPARQL 解析 rdf 图。当然,在图表中,我的实体一次可以有多种类型。

SPARQL 查询必须是什么样子才能检查这两种类型?

我的图表的样子:

<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>

第二个 TrainArrival 有一个额外的类型 "PrimaryInformation"。 如何检查实体是否具有两种类型,TrainArrival + PrimaryInformation?

我现在的查询:

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 } " +
                "}";

然后我尝试这样检查:

QuerySolution soln = results.nextSolution();

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

我收到异常说明:

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

我错过了什么? 提前致谢!

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

将匹配同时具有 TrainArrival 和 PrimaryInformation

根据您遇到的异常情况,其中一个变量似乎没有正确绑定到资源。我无法检查这一点,但您是否尝试过将 String type2 = soln.getResource("type2").toString(); 重写为 String type2 = soln.get("type2").toString();