Sparql query in dotNetRDF

I covered the well known ontology pizza.owl

to an RDF file pizza.rdf

via the Manchester OWL parser. I wrote this code, but I am not getting any results, but there are no errors. How can I get triples with a 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());
   }
 }

      

+3


source to share


2 answers


See Querying with SPARQL , in particular the Common Errors section , which says:

A common mistake when creating queries is that default queries usually only work on an unnamed default schedule in the store (depending on your query processor). Therefore, the execution of queries may not produce any results, depending on which graphs your data is on and you have correctly configured your dataset. Please see the SPARQL Datasets page for a discussion of setting different data types. You can also see Debugging SPARQL Queries for a Method for Debugging Happens to Your Query when using the in-memory SPARQL engine.

The typical reason for this is that when you call LoadFromFile () or LoadFromUri (), the library automatically assigns a name to the chart based on the data source, so when you add a storage instance it is named by the name of the chart, not the default chart. The easiest way to solve this problem is to simply set the BaseUri property of your chart instance to null after loading it and before making queries with it.

Emphasis added mine, as it describes exactly what is happening in your case. The loaded graph is given a name based on the file it comes from and a so-called graph, and your storage's default graph is left blank.

Also note that the method ExecuteQuery()

you are using is deliberately deprecated due to this issue and you received a compiler warning about using the deprecated method. This method may be removed in a future version, so it should be avoided.



There are two ways to fix this, firstly, as stated in the above documentation, we can remove the name from the graph so that 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 ditch the legacy method entirely by creating a query processor and dataset that gives you direct control over which graph is 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");

      

+2


source


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

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

      

If you look at some of the triplets in the data, you see, for example:

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

      

Pizza Prefix : Must be http://www.co-ode.org/ontologies/pizza/pizza.owl# . If you make this change, then you will probably get more meaningful results like these (but I ran into Jena, so the results will probably be slightly 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 triplets with the MushroomTopping predicate?

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

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

      

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

0


source







All Articles