Search code examples
c#rdfsparqldotnetrdf

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());
   }
 }

Solution

  • 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");