简体   繁体   English

RDF SPARQL查询多种类型

[英]RDF SPARQL query multiple types

I am trying to parse a rdf graph using SPARQL. 我正在尝试使用SPARQL解析rdf图。 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? SPARQL查询的外观如何,以便我可以检查两种类型?

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". 第二个TrainArrival具有附加类型“ PrimaryInformation”。 How can I check if an Entity has both types, TrainArrival + PrimaryInformation? 如何检查实体是否同时具有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 com.hp.hpl.jena.rdf.model.impl.ResourceImpl无法转换为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 将同时拥有TrainArrival和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(); 我无法检查,但是您是否尝试过重写String type2 = soln.getResource("type2").toString(); to String type2 = soln.get("type2").toString(); String type2 = soln.get("type2").toString();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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