2

If I have connection cxn to an empty triplestore and I say:

val CheckSparql = "select (count(?s) as ?scount) where { ?s ?p ?o . }"

val PreparedSparql = cxn.prepareTupleQuery(QueryLanguage.SPARQL, CheckSparql)
val Result = PreparedSparql.evaluate()
val ResBindSet = Result.next()
val TripleCount = ResBindSet.getValue("scount")

println(TripleCount.toString())

I get

"0"^^<http://www.w3.org/2001/XMLSchema#integer>

Is there an RDF4J method for retrieving just the value of that literal?

I just want 0 or "0", without the datatype

I figured I should try to do this without string manipulation/regular expressions if possible.

Mark Miller
  • 3,011
  • 1
  • 14
  • 34
  • 1
    `getValue` returns an instance of type [`Value`](http://docs.rdf4j.org/javadoc/2.2/org/eclipse/rdf4j/model/Value.html). I guess you have to check for the type and cast it to the appropriate type. In your case, it should be a [Literal](http://docs.rdf4j.org/javadoc/2.2/org/eclipse/rdf4j/model/Literal.html) which provides a method called `integerValue()` to get the integer value. Untested: `TripleCount.asInstanceOf[Literal].integerValue()` – UninformedUser Oct 25 '17 at 20:29
  • @AKSW yeah, that works. thanks. this also seems to work: `val tripleCountInt = tripleCount.stringValue().toInt` can you think of any reason not to do that? – Mark Miller Oct 25 '17 at 20:38
  • I guess for a COUNT query this should always work, so I think your solution would be safe. Otherwise, the answer of @JeenBroekstra shows another solution. Feel free to choose your favorite solution, all of them work as expected :D – UninformedUser Oct 26 '17 at 07:43

1 Answers1

2

The Literals util class is your friend:

int scount = Literals.getIntValue(ResBindSet.getValue("scount"))

or if you prefer a string:

String scount =  Literals.getLabel(ResBindSet.getValue("scount"))

You can also optionally add a fallback argument, for example if the supplied argument turns out not to be a literal.

Jeen Broekstra
  • 21,642
  • 4
  • 51
  • 73