RDF SPARQL查询多种类型

时间:2015-06-02 12:50:48

标签: java rdf sparql

我正在尝试使用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有一个额外的类型&#34; PrimaryInformation&#34;。 如何检查实体是否具有两种类型,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无法强制转换为com.hp.hpl.jena.rdf.model.Literal

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

2 个答案:

答案 0 :(得分:0)

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

将同时具有TrainArrival和PrimaryInformation

答案 1 :(得分:0)

根据您获得的异常,似乎其中一个变量未正确绑定到资源。我无法检查此内容,但您是否尝试将<ons-page class="map" ng-controller="MapController2" sliding-menu-ignore="true"> <ons-toolbar> <div class="left"><ons-back-button>Back</ons-back-button></div> <div class="center page-title">Map MapController</div> </ons-toolbar> <ui-gmap-google-map center="map.center" refresh="true" zoom="map.zoom" draggable="true" data-tap-disabled="true"> <ui-gmap-window show="map.window.show" coords="map.window.model" options="map.window.options" closeClick="map.window.closeClick()"> <div style="color: black"> {{map.window.title}} </div> </ui-gmap-window> <ui-gmap-markers idkey="marker.id" models="map.markers" coords="'self'" doCluster="false" fit="'true'" icon="'icon'" events="map.markersEvents " options="'options'"></ui-gmap-markers> </ui-gmap-google-map> </ons-page> 重写为// Map Controller app.controller('MapController2', function($scope, $rootScope, $http) { var data = {}; data.map = { zoom: 12, center: { latitude: 27.949, longitude: -81.958 }, markers: [ { id: 1, icon: 'img/blue_marker.png', latitude: 27.949, longitude: -81.999, title: 'This is where you are' }, { id: 2, latitude: 27.949, longitude: -81.958, title: 'Job Site' }, { id: 3, icon: 'img/plane.png', latitude: 27.949, longitude: -81.94, title: 'Airport' }], markersEvents: { click: function(marker, eventName, model, arguments) { console.log('Marker was clicked (' + marker + ', ' + eventName);//+', '+mydump(model, 0)+', '+mydump(arguments)+')'); $scope.map.window.model = model; $scope.map.window.model = model; $scope.map.window.title = model.title; $scope.map.window.show = true; } }, window: { marker: {}, show: false, closeClick: function() { this.show = false; }, options: {}, // define when map is ready title: '' } }; //$scope.window = false; $scope.onMarkerClicked = function (m) { //this.windowOptions = !this.windowOptions; console.log('Marker was clicked'); console.log(m); }; $scope.closeClick = function () { this.window = false; }; $scope.map = data.map; });