Discussion:
How to remove an element from a list?
Dmitri Pisarenko
2017-03-16 09:11:20 UTC
Permalink
Hello!

I have an application, which creates an individual called batch. A batch has a list of company IDs (strings). Add the property like this:

open fun addCompanyIdsList(
batch: Resource,
companyIds: List<String>,
model: Model) {
val prop = model.createProperty(Bp2BatchCompanyIds)
val list = model.createList()
companyIds
.filter { it.isNumeric() }
.map { it.toInt() }
.map { id -> model.createTypedLiteral(id) }
.forEach { literal -> list.add(literal) }
batch.addProperty(prop, list)
}

In another operation I want to remove the first element of the list so that this change is saved in Jena. For primitive data types like integers, there is the method

org.apache.jena.rdf.model.Statement#changeLiteralObject(boolean)

and its analogs.

How do I change the list? Is it sufficient to just remove an element from the list like in the code snippet below?

ds.begin(ReadWrite.WRITE)
val query = createQuery("""SELECT ?x
WHERE { ?x $Bp2BatchNumber $batchId }""")
val qexec = createQueryExecution(ds, query)
val rs = qexec.execSelect()
val sol = rs.nextSolution()
val rec = sol["x"]
val prop = findStatement(Bp2BatchCompanyIds, rec as Resource)
val companyIds = prop.`object` as RDFList
val head = companyIds.head
companyIds.remove(result)
ds.end()

Thanks in advance

Dmitri Pisarenko
Lorenz B.
2017-03-16 09:17:52 UTC
Permalink
Post by Dmitri Pisarenko
Hello!
open fun addCompanyIdsList(
batch: Resource,
companyIds: List<String>,
model: Model) {
val prop = model.createProperty(Bp2BatchCompanyIds)
val list = model.createList()
companyIds
.filter { it.isNumeric() }
.map { it.toInt() }
.map { id -> model.createTypedLiteral(id) }
.forEach { literal -> list.add(literal) }
batch.addProperty(prop, list)
}
In another operation I want to remove the first element of the list so that this change is saved in Jena. For primitive data types like integers, there is the method
org.apache.jena.rdf.model.Statement#changeLiteralObject(boolean)
and its analogs.
How do I change the list? Is it sufficient to just remove an element from the list like in the code snippet below?
ds.begin(ReadWrite.WRITE)
val query = createQuery("""SELECT ?x
WHERE { ?x $Bp2BatchNumber $batchId }""")
val qexec = createQueryExecution(ds, query)
val rs = qexec.execSelect()
val sol = rs.nextSolution()
val rec = sol["x"]
val prop = findStatement(Bp2BatchCompanyIds, rec as Resource)
val companyIds = prop.`object` as RDFList
val head = companyIds.head
companyIds.remove(result)
ds.end()
Which programming language is this?
Does your code compile? I'm asking because there is just a getHead()
method on an RDFList object. And this one returns an RDFNode object,
thus, remove should be impossible here.
Post by Dmitri Pisarenko
Thanks in advance
Dmitri Pisarenko
--
Lorenz Bühmann
AKSW group, University of Leipzig
Group: http://aksw.org - semantic web research center
Dmitri Pisarenko
2017-03-16 09:20:51 UTC
Permalink
Post by Lorenz B.
Which programming language is this?
Kotlin.

"companyIds.head" is equivalent to "companyIds.getHead()".
Post by Lorenz B.
Does your code compile? I'm asking because there is just a getHead()
method on an RDFList object. And this one returns an RDFNode object,
thus, remove should be impossible here.
It does compile, but I haven't tested it manually yet.

Could you point me to an analog of org.apache.jena.rdf.model.Statement#changeLiteralObject(...) for lists?

Dmitri
Post by Lorenz B.
 Hello!
     open fun addCompanyIdsList(
             batch: Resource,
             companyIds: List<String>,
             model: Model) {
         val prop = model.createProperty(Bp2BatchCompanyIds)
         val list = model.createList()
         companyIds
                 .filter { it.isNumeric() }
                 .map { it.toInt() }
                 .map { id -> model.createTypedLiteral(id) }
                 .forEach { literal -> list.add(literal) }
         batch.addProperty(prop, list)
     }
 In another operation I want to remove the first element of the list so that this change is saved in Jena. For primitive data types like integers, there is the method
 org.apache.jena.rdf.model.Statement#changeLiteralObject(boolean)
 and its analogs.
 How do I change the list? Is it sufficient to just remove an element from the list like in the code snippet below?
         ds.begin(ReadWrite.WRITE)
         val query = createQuery("""SELECT ?x
                                                 WHERE { ?x $Bp2BatchNumber $batchId }""")
         val qexec = createQueryExecution(ds, query)
         val rs = qexec.execSelect()
         val sol = rs.nextSolution()
         val rec = sol["x"]
         val prop = findStatement(Bp2BatchCompanyIds, rec as Resource)
         val companyIds = prop.`object` as RDFList
         val head = companyIds.head
         companyIds.remove(result)
         ds.end()
Which programming language is this?
Does your code compile? I'm asking because there is just a getHead()
method on an RDFList object. And this one returns an RDFNode object,
thus, remove should be impossible here.
 Thanks in advance
 Dmitri Pisarenko
--
Lorenz Bühmann
AKSW group, University of Leipzig
Group: http://aksw.org - semantic web research center
Lorenz B.
2017-03-16 09:26:54 UTC
Permalink
As far as I know, all Jena RDFNodes and Statements are immutable.
The methods on Statements objects or convenience methods that remove the
old statement and add the new one to the underlying model.

In your case it's just changeObject(RDFNode val), see [1]

where val denotes the new RDFNode object, in your case the new list.
RDFList::Rmeove will return a new RDFList object, thus, you can use this
one as argument.

[1]
https://jena.apache.org/documentation/javadoc/jena/org/apache/jena/rdf/model/Statement.html#changeObject-org.apache.jena.rdf.model.RDFNode-
Post by Dmitri Pisarenko
Post by Lorenz B.
Which programming language is this?
Kotlin.
"companyIds.head" is equivalent to "companyIds.getHead()".
Post by Lorenz B.
Does your code compile? I'm asking because there is just a getHead()
method on an RDFList object. And this one returns an RDFNode object,
thus, remove should be impossible here.
It does compile, but I haven't tested it manually yet.
Could you point me to an analog of org.apache.jena.rdf.model.Statement#changeLiteralObject(...) for lists?
Dmitri
Post by Lorenz B.
Post by Dmitri Pisarenko
Hello!
open fun addCompanyIdsList(
batch: Resource,
companyIds: List<String>,
model: Model) {
val prop = model.createProperty(Bp2BatchCompanyIds)
val list = model.createList()
companyIds
.filter { it.isNumeric() }
.map { it.toInt() }
.map { id -> model.createTypedLiteral(id) }
.forEach { literal -> list.add(literal) }
batch.addProperty(prop, list)
}
In another operation I want to remove the first element of the list so that this change is saved in Jena. For primitive data types like integers, there is the method
org.apache.jena.rdf.model.Statement#changeLiteralObject(boolean)
and its analogs.
How do I change the list? Is it sufficient to just remove an element from the list like in the code snippet below?
ds.begin(ReadWrite.WRITE)
val query = createQuery("""SELECT ?x
WHERE { ?x $Bp2BatchNumber $batchId }""")
val qexec = createQueryExecution(ds, query)
val rs = qexec.execSelect()
val sol = rs.nextSolution()
val rec = sol["x"]
val prop = findStatement(Bp2BatchCompanyIds, rec as Resource)
val companyIds = prop.`object` as RDFList
val head = companyIds.head
companyIds.remove(result)
ds.end()
Which programming language is this?
Does your code compile? I'm asking because there is just a getHead()
method on an RDFList object. And this one returns an RDFNode object,
thus, remove should be impossible here.
Post by Dmitri Pisarenko
Thanks in advance
Dmitri Pisarenko
--
Lorenz BÃŒhmann
AKSW group, University of Leipzig
Group: http://aksw.org - semantic web research center
--
Lorenz BÃŒhmann
AKSW group, University of Leipzig
Group: http://aksw.org - semantic web research center
Dmitri Pisarenko
2017-03-16 09:31:40 UTC
Permalink
Many thanks for your answer!

This is part of a bigger change, which I intend to test during the weekend. I'll write back to you, if I find out something interesting.

Thanks again

Dmitri
Post by Lorenz B.
As far as I know, all Jena RDFNodes and Statements are immutable.
The methods on Statements objects or convenience methods that remove the
old statement and add the new one to the underlying model.
In your case it's just changeObject(RDFNode val), see [1]
where val denotes the new RDFNode object, in your case the new list.
RDFList::Rmeove will return a new RDFList object, thus, you can use this
one as argument.
[1]
https://jena.apache.org/documentation/javadoc/jena/org/apache/jena/rdf/model/Statement.html#changeObject-org.apache.jena.rdf.model.RDFNode-
 Which programming language is this?
 Kotlin.
 "companyIds.head" is equivalent to "companyIds.getHead()".
 Does your code compile? I'm asking because there is just a getHead()
 method on an RDFList object. And this one returns an RDFNode object,
 thus, remove should be impossible here.
 It does compile, but I haven't tested it manually yet.
 Could you point me to an analog of org.apache.jena.rdf.model.Statement#changeLiteralObject(...) for lists?
 Dmitri
  Hello!
      open fun addCompanyIdsList(
              batch: Resource,
              companyIds: List<String>,
              model: Model) {
          val prop = model.createProperty(Bp2BatchCompanyIds)
          val list = model.createList()
          companyIds
                  .filter { it.isNumeric() }
                  .map { it.toInt() }
                  .map { id -> model.createTypedLiteral(id) }
                  .forEach { literal -> list.add(literal) }
          batch.addProperty(prop, list)
      }
  In another operation I want to remove the first element of the list so that this change is saved in Jena. For primitive data types like integers, there is the method
  org.apache.jena.rdf.model.Statement#changeLiteralObject(boolean)
  and its analogs.
  How do I change the list? Is it sufficient to just remove an element from the list like in the code snippet below?
          ds.begin(ReadWrite.WRITE)
          val query = createQuery("""SELECT ?x
                                                  WHERE { ?x $Bp2BatchNumber $batchId }""")
          val qexec = createQueryExecution(ds, query)
          val rs = qexec.execSelect()
          val sol = rs.nextSolution()
          val rec = sol["x"]
          val prop = findStatement(Bp2BatchCompanyIds, rec as Resource)
          val companyIds = prop.`object` as RDFList
          val head = companyIds.head
          companyIds.remove(result)
          ds.end()
 Which programming language is this?
 Does your code compile? I'm asking because there is just a getHead()
 method on an RDFList object. And this one returns an RDFNode object,
 thus, remove should be impossible here.
  Thanks in advance
  Dmitri Pisarenko
 --
 Lorenz Bühmann
 AKSW group, University of Leipzig
 Group: http://aksw.org - semantic web research center
--
Lorenz Bühmann
AKSW group, University of Leipzig
Group: http://aksw.org - semantic web research center
Lorenz B.
2017-03-16 09:22:22 UTC
Permalink
Ah sorry, I was wrong. You call remove on the RDFList object.

According to the implementation, it should modify the current list
object. Have you tried it out? Does it not work?
Post by Lorenz B.
Post by Dmitri Pisarenko
Hello!
open fun addCompanyIdsList(
batch: Resource,
companyIds: List<String>,
model: Model) {
val prop = model.createProperty(Bp2BatchCompanyIds)
val list = model.createList()
companyIds
.filter { it.isNumeric() }
.map { it.toInt() }
.map { id -> model.createTypedLiteral(id) }
.forEach { literal -> list.add(literal) }
batch.addProperty(prop, list)
}
In another operation I want to remove the first element of the list so that this change is saved in Jena. For primitive data types like integers, there is the method
org.apache.jena.rdf.model.Statement#changeLiteralObject(boolean)
and its analogs.
How do I change the list? Is it sufficient to just remove an element from the list like in the code snippet below?
ds.begin(ReadWrite.WRITE)
val query = createQuery("""SELECT ?x
WHERE { ?x $Bp2BatchNumber $batchId }""")
val qexec = createQueryExecution(ds, query)
val rs = qexec.execSelect()
val sol = rs.nextSolution()
val rec = sol["x"]
val prop = findStatement(Bp2BatchCompanyIds, rec as Resource)
val companyIds = prop.`object` as RDFList
val head = companyIds.head
companyIds.remove(result)
ds.end()
Which programming language is this?
Does your code compile? I'm asking because there is just a getHead()
method on an RDFList object. And this one returns an RDFNode object,
thus, remove should be impossible here.
Post by Dmitri Pisarenko
Thanks in advance
Dmitri Pisarenko
--
Lorenz Bühmann
AKSW group, University of Leipzig
Group: http://aksw.org - semantic web research center
Loading...