[redland-dev] C# Sparql Construct

Dave Beckett dave at dajobe.org
Tue Apr 25 06:06:37 BST 2006


arnoud de jong wrote:
> Hello,
> 
> I'm trying to execute a simple sparql construct query using the 
> following C# code (VS2005).
> 
> Redland.MemoryStorage ms = new MemoryStorage();
> Redland.Model m = new Model(ms);
> m.AddStatement(new Statement(new Node(" http://test.org"),new 
> Node("http://hasTest"), new Node("http://testvalue")));           
> Query q = new Query("CONSTRUCT {?subject ?predicate ?object} WHERE 
> {?subject ?predicate ?object}",null,"sparql",null);
> QueryResults qr = q.Execute(m);
> foreach (object qo in qr)
> {
>    Console.WriteLine(qo.ToString());
> }
> 
> There seems to be no result and I see the following error:Current = 
> 'qr.Current' threw an exception of type ' System.AccessViolationException'
> 
> If I excecute a SELECT query there is not problem. Any suggestions how 
> to solve this?

This is several problems in one.

Firstly, new Node(" http://test.org") and similar make RDF literals
so new Statement(..., ..., ...) makes a non-RDF triple, but redland doesn't 
worry about that at this point and lets you store it.

Then the query runs, and it does execute correctly.

The default Enumeration for a QueryResults is to give variable bindings. 
Since in this case, there is one result with no bindings, the Current() of 
the query result enumeration returns nothing - and there is insufficient 
error checking here - it should return a null.

So what should be executed is something like this:
     QueryResults qr = q.Execute(m);
     Stream stream=qr.AsStream();
     if(stream != null) {
       foreach (object qo in stream)
         {
         if(qo != null)
           Console.WriteLine(qo.ToString());
         }
     }

however this won't work either because the SPARQL triple building *does* 
check that the triple is RDF, so since here it's (literal, literal, 
literal), the triple construction fails so the result is that there are no 
triples and the AsStream() response should return null again (or should be; 
there is a need for more error checking here).

So *if* the stored RDF is legal, then the above should work.  You could try 
something like in the test.cs code:
                 Node subject, predicate, obj;
                 subject = new Node (new Redland.Uri 
("http://purl.org/net/dajobe/"));
                 predicate = new Node (new Redland.Uri 
("http://purl.org/dc/elements/1.1/title"));
                 obj = new Node ("My Home Page");

                 Statement stm = new Statement (subject, predicate, obj);
                 model.AddStatement (stm);

or load RDF from a file


Clearly the C# API needs a bit more love...

There are comments in the code near most of these checks that they should 
throw exceptions on failure, which is probably the right C# answer for 
errors in these cases.

Dave



More information about the redland-dev mailing list