Search code examples
javasparqljenaowlprotege

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


I have generated an owl file using Protege. Now I wish to access all the stationCode property values of all instances of a class RailwayStation from it.

My OWL file contains the following format

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

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)

Solution

  • 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. But in your case I think that Protege genereted this namespace for you. Concerning your file, add # or / to the end of your base namespace.

    Finally, for your query : Add the prefix PREFIX base: <http://www.owl-ontologies.com/RailwaysSemantic.owl#> just as you did for rdf and 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 :

    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