简体   繁体   English

如何编写SPARQL查询以从OWL文件中检索数据

[英]How to write a SPARQL query for retrieving data from OWL file

I have generated an owl file using Protege. 我已经使用Protege生成了一个猫头鹰文件。 Now I wish to access all the stationCode property values of all instances of a class RailwayStation from it. 现在,我希望访问所有从它一类火车站有所有实例的stationCode属性值。

My OWL file contains the following format 我的OWL文件包含以下格式

<?xml version="1.0"?>
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:swrlb="http://www.w3.org/2003/11/swrlb#"
xmlns:xsp="http://www.owl-ontologies.com/2005/08/07/xsp.owl#"
xmlns:owl="http://www.w3.org/2002/07/owl#"
xmlns:protege="http://protege.stanford.edu/plugins/owl/protege#"
xmlns:swrl="http://www.w3.org/2003/11/swrl#"
xmlns:swrla="http://swrl.stanford.edu/ontologies/3.3/swrla.owl#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xmlns:sqwrl="http://sqwrl.stanford.edu/ontologies/built-ins/3.4/sqwrl.owl#"
xmlns="http://www.owl-ontologies.com/RailwaysSemantic.owl#"
xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
xml:base="http://www.owl-ontologies.com/RailwaysSemantic.owl">
<owl:Ontology rdf:about="">
<owl:imports rdf:resource="http://swrl.stanford.edu/ontologies/3.3/swrla.owl"/>
<owl:imports rdf:resource="http://sqwrl.stanford.edu/ontologies/built-ins/3.4/sqwrl.owl"/>
</owl:Ontology>
<rdfs:Class rdf:ID="BusStand"/>
<rdfs:Class rdf:ID="Eatery"/>
<rdfs:Class rdf:ID="Train"/>
<rdfs:Class rdf:ID="Hospital"/>
<rdfs:Class rdf:ID="City"/>
<rdfs:Class rdf:ID="RailwayStation"/>
<rdfs:Class rdf:ID="Airport"/>
<rdfs:Class rdf:ID="TouristSpot"/>
<rdfs:Class rdf:ID="Schedule"/>
<RailwayStation rdf:ID="RailwayStation_YPR">
  <stationCode rdf:datatype="http://www.w3.org/2001/XMLSchema#string">
   YPR</stationCode>

</RailwayStation>

How do I write a SPARQL query for this purpose? 为此,我该如何编写SPARQL查询?

My current query is this : 我当前的查询是这样的:

String querystr = "PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>"+
                    "PREFIX owl:<http://www.w3.org/2002/07/owl#>"+
                    "PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#>"+

                    "SELECT * WHERE {"+
                    "?RailwayStation stationCode ?x ."+ 
                    "}";

But it gives an exception like this: 但是它给出了这样的异常:

 Exception in thread "main" com.hp.hpl.jena.query.QueryParseException: Lexical error at line 1, column 194.  Encountered: " " (32), after : "stationCode"
at     com.hp.hpl.jena.sparql.lang.ParserSPARQL11.perform(ParserSPARQL11.java:111)
    at com.hp.hpl.jena.sparql.lang.ParserSPARQL11.parse$(ParserSPARQL11.java:53)
at com.hp.hpl.jena.sparql.lang.SPARQLParser.parse(SPARQLParser.java:37)
at com.hp.hpl.jena.query.QueryFactory.parse(QueryFactory.java:148)
at com.hp.hpl.jena.query.QueryFactory.create(QueryFactory.java:80)
at com.hp.hpl.jena.query.QueryFactory.create(QueryFactory.java:53)
at com.hp.hpl.jena.query.QueryFactory.create(QueryFactory.java:41)
at org.iiitb.jena.Main.sprqltest(Main.java:70)
at org.iiitb.jena.Main.main(Main.java:22)

"?RailwayStation stationCode ?x ." “?RailwayStation stationCode?x。”

The property must be a URI (has <>) or a prefixed name (has a colon). 该属性必须是URI(具有<>)或前缀名称(具有冒号)。

There is some problems with your file and your query. 您的文件和查询存在一些问题。 First, you have to know that when you declare a base namespace in your xml/rdf file, all the tags used without a specific namespace will be added to your base namespace http://www.owl-ontologies.com/RailwaysSemantic.owl . 首先,您必须知道,当在xml / rdf文件中声明base名称空间时,所有不带特定名称空间的标记都将添加到您的基本名称空间http://www.owl-ontologies.com/RailwaysSemantic.owl But in your case I think that Protege genereted this namespace for you. 但是在您的情况下,我认为Protege为您创建了该命名空间。 Concerning your file, add # or / to the end of your base namespace. 关于文件,在基本名称空间的末尾添加# or /

Finally, for your query : Add the prefix PREFIX base: <http://www.owl-ontologies.com/RailwaysSemantic.owl#> just as you did for rdf and rdfs... 最后,对于您的查询:添加前缀PREFIX base: <http://www.owl-ontologies.com/RailwaysSemantic.owl#> ,就像您对rdf和rdfs所做的那样...

After this, you wish to access all the stationCode property values of all instances of a class RailwayStation, the sparql query answering this is : 此后,您希望访问RailwayStation类的所有实例的所有stationCode属性值,回答此问题的sparql查询为:

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX base: <http://www.owl-ontologies.com/RailwaysSemantic.owl#>
select * 
where { ?s a <http://www.owl-ontologies.com/RailwaysSemantic.owl#RailwayStation> .
        ?s <http://www.owl-ontologies.com/RailwaysSemantic.owl#stationCode> ?o}

I tested it in your file and it returns the good result. 我在您的文件中对其进行了测试,并返回了良好的结果。 I deliberately didn't use the namespace in my query so you can immediately test it in your data without changing the namespaces ;) Good luck 我故意不在查询中使用名称空间,因此您可以立即在数据中对其进行测试,而无需更改名称空间;)

We can make it more readable by using prefix as follows: 我们可以通过使用如下前缀来使其更具可读性:

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX base: <http://www.owl-ontologies.com/RailwaysSemantic.owl#>
select * 
where { ?s base:RailwayStation .
    ?s base:stationCode ?o}

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

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