Ravindu Kottahachchi
Ravindu Kottahachchi

Reputation: 91

sparql query in dotNetRDF

I have coverted the well known pizza.owl ontology to an RDF file pizza.rdf through the Manchester OWL syntax converter. I have written this code, but I get no results, but no error, neither. How can I get the triples with predicate MushroomTopping?

 TripleStore store = new TripleStore();
 store.LoadFromFile(@"C:\pizza.rdf");
 Object results = store.ExecuteQuery("PREFIX pizza:<http://example.org/> SELECT *  WHERE { ?X ?Y pizza:MushroomTopping  . }
 if (results is SparqlResultSet)
 {
   //Print out the Results
   //Console.WriteLine("working up to this ");
   SparqlResultSet rset = (SparqlResultSet)results;
   foreach (SparqlResult result in rset.Results)
   {
     Console.WriteLine(result.ToString());
   }
 }

Upvotes: 3

Views: 4099

Answers (2)

Joshua Taylor
Joshua Taylor

Reputation: 85913

RobV's answer is important, and you may run into it eventually, but I think the most likely problem is that your prefix in the query is wrong. This is your query:

PREFIX pizza:<http://example.org/>
SELECT *  WHERE {
  ?X ?Y pizza:MushroomTopping .
}

If you look at some of the triples in the data, though, you'll see, for instance:

<http://www.co-ode.org/ontologies/pizza/pizza.owl#RocketTopping> <http://www.w3.org/2002/07/owl#disjointWith> <http://www.co-ode.org/ontologies/pizza/pizza.owl#MushroomTopping> .

The pizza: prefix should probably be http://www.co-ode.org/ontologies/pizza/pizza.owl#. If you make that change, then you'll probably get more meaningful results, like these (but I ran this in Jena, so the results will probably look a little bit different):

prefix pizza: <http://www.co-ode.org/ontologies/pizza/pizza.owl#>
select ?s ?p {
  ?s ?p pizza:MushroomTopping
}
-------------------------------------------------------------------------------
| s                      | p                                                  |
===============================================================================
| _:b0                   | <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> |
| pizza:CaperTopping     | <http://www.w3.org/2002/07/owl#disjointWith>       |
| pizza:PepperTopping    | <http://www.w3.org/2002/07/owl#disjointWith>       |
| _:b1                   | <http://www.w3.org/2002/07/owl#someValuesFrom>     |
| pizza:RocketTopping    | <http://www.w3.org/2002/07/owl#disjointWith>       |
| _:b2                   | <http://www.w3.org/2002/07/owl#someValuesFrom>     |
| _:b3                   | <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> |
| pizza:OnionTopping     | <http://www.w3.org/2002/07/owl#disjointWith>       |
| _:b4                   | <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> |
| _:b5                   | <http://www.w3.org/2002/07/owl#someValuesFrom>     |
| pizza:TomatoTopping    | <http://www.w3.org/2002/07/owl#disjointWith>       |
| pizza:GarlicTopping    | <http://www.w3.org/2002/07/owl#disjointWith>       |
| _:b6                   | <http://www.w3.org/2002/07/owl#someValuesFrom>     |
| _:b7                   | <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> |
| pizza:OliveTopping     | <http://www.w3.org/2002/07/owl#disjointWith>       |
| pizza:ArtichokeTopping | <http://www.w3.org/2002/07/owl#disjointWith>       |
-------------------------------------------------------------------------------

How can I get the triples with predicate MushroomTopping?

Note that RDF triples are of the form (subject, predicate, object). When you write a SPARQL query, you'd write:

select ?subject ?predicate ?object where {
  ?subject ?predicate ?object
}

The query that you wrote has pizza:MushroomTopping in the object position. Fortunately, that probably makes more sense in this case, since pizza:MushroomTopping is an OWL class, not a property, so you're very unlikely to see it appear in the predicate position.

Upvotes: 0

RobV
RobV

Reputation: 28675

See the Querying with SPARQL documentation specifically the section on Common Errors which says:

A common error with making queries is that queries by default typically operate only over the unnamed default graph in the store (depending on your query processor). Therefore executing queries may yield no results depending on what graphs your data is in and whether you configured your dataset correctly. Please see the SPARQL Datasets page for discussions of configuring different kinds of dataset. You can also look at Debugging SPARQL Queries for a method to debug what is happening with your query when using the in-memory SPARQL engine.

The typical cause of this is that when you call LoadFromFile() or LoadFromUri() the library automatically assigns the graph a name based on the data source so when you add it a store instance it is a named graph rather than the default graph. The easiest way to resolve this is to simply set the BaseUri property of your graph instance to null after loading it and before you execute queries with it.

Emphasis added is mine as this describes exactly what is happening in your case. The loaded graph is given a name based on the file it came from and so is a named graph and the default graph of your store remains empty.

Please also note that the ExecuteQuery() method you use is specifically deprecated because of this problem and you would have received a compiler warning about use of an obsolete method. This method may be removed in future releases and so should be avoided.

There are two ways to fix this, firstly as noted in the above quoted documentation we can remove the name from the graph so it is treated as the default graph:

Graph g = new Graph();
g.LoadFromFile(@"C:\pizza.rdf");
g.BaseUri = null;
store.Add(g);

Or alternatively you can avoid using the deprecated method completely by creating a query processor and a dataset which gives you direct control over which graph is used as the default:

Graph g = new Graph();
g.LoadFromFile(@"C:\pizza.rdf");
ISparqlDataset ds = new InMemoryDataset(g);
LeviathanQueryProcessor processor = new LeviathanQueryProcessor(ds);
Object results = processor.ProcessQuery("# Your query");

Upvotes: 4

Related Questions