Jeen, thanks for your reply.  Your suggestion works very well. 

For the benefit of anyone reading this afterwards, Eclipse objected to the line "Literal literal = (Literal)v;", prefering "LiteralImpl literal = (LiteralImpl)v;" because Literal is an interface.

Cliff

On Fri, Feb 4, 2011 at 4:15 PM, Jeen Broekstra <jeen.broekstra@gmail.com> wrote:
On 5/02/2011 07:10, cliff palmer wrote:
> I've created a statement with the subject "elevation" (the URI is local)
> , the predicate is type (the URI is
> http://www.w3.org/1999/02/22-rdf-syntax-ns#type)  and the object is
> datatype (the URI is http://www.w3.org/2002/07/owl#DatatypeProperty).
> The intent is to use "elevation" as a predicate in statements containing
> the elevation of places.

To summarize, and to see if I understand correctly, you have added this
RDF statement:

 :elevation rdf:type owl:DatatypeProperty .

> I've populated statements with the URI for different places as the
> subject, the "elevation URI" for the predicate, and the elevation as a
> literal using the createLiteral method of the ValueFactory class..

So, for example:

 place:London :elevation "10"^^xsd:integer .
 place:Amsterdam :elevation "-1"^^xsd:integer .

(assuming you are using integer values)

> I want to retrieve the elevation from a query using the getStatments
> method of the RepositoryConnection class.  I'm not sure how to retrieve
> a literal value (the subject and predicate are retrieved using the
> getSubject and getPredicate methods of the Statement class).  How do I
> retrieve a literal value from a statement?

You can use Statement.getObject() for this. For example:

// retrieve all RDF statements with a 'elevation' predicate
RepositoryResult<Statement> statements
              = connection.getStatements(null, elevation, null, true);

// print out each literal value
while(statements.hasNext()) {
   Statement st = statements.next();
   Value v = st.getObject();
   if (v instanceof Literal) {
      Literal literal = (Literal)v;

      System.out.println("the literal value is : "
                                             + literal.getLabel());
      System.out.println("the literal datatype is : "
                                             + literal.getDatatype());
   }
}

A statement always consists of three parts: a subject, a predicate, and
an object. The object can be a literal value (as in your example), or it
can be a blank node or URI.

There is no direct way using getStatements() to _only_ retrieve
statements with literal values: you either have to know in advance or
you have to do an instanceof test on the retrieved object Value (as I'm
doing in the above example code).

As Darren suggested, if you want to make sure in the call itself that
only literals are retrieved, you will need to use a SPARQL (or SeRQL)
query on the RepositoryConnection, instead of using getStatements.

Cheers,

Jeen

------------------------------------------------------------------------------
The modern datacenter depends on network connectivity to access resources
and provide services. The best practices for maximizing a physical server's
connectivity to a physical network are well understood - see how these
rules translate into the virtual world?
http://p.sf.net/sfu/oracle-sfdevnlfb
_______________________________________________
Sesame-general mailing list
Sesame-general@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sesame-general