You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@jena.apache.org by "Martin G. Skjæveland" <m....@gmail.com> on 2017/04/20 19:46:39 UTC

Problems replacing RDFLists

Dear list,

I want to replace all the RDF lists in a model which have contents equal 
to given input RDF list 'old' with a copies of an RDF list 'fresh'.

Following the advise in 
https://jena.apache.org/documentation/javadoc/jena/org/apache/jena/rdf/model/RDFList.html#removeList--, 
I remove all lists equal to 'old' and update old's heads to new copies 
of fresh.

However, running the code has two results, either it fails with the 
exception:

Exception in thread "main" 
org.apache.jena.shared.PropertyNotFoundException: 
http://www.w3.org/1999/02/22-rdf-syntax-ns#first
     at 
org.apache.jena.rdf.model.impl.ModelCom.getRequiredProperty(ModelCom.java:1231)
     at 
org.apache.jena.rdf.model.impl.ResourceImpl.getRequiredProperty(ResourceImpl.java:172)
     at 
org.apache.jena.rdf.model.impl.RDFListImpl.sameListAs(RDFListImpl.java:876)
     at ListTest.substituteNonEmptyRDFList(ListTest.java:39)
     at ListTest.main(ListTest.java:65)


or it outputs a model where the replaced list seems OK, but does not 
seem to be recognized as a proper RDF list (due to the lack of terse 
list formatting):

@prefix ex:    <http://example.com#> .
@prefix rdf:   <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .

ex:fresh1  ex:hasList  ( ex:B1 ex:B2 ) .

ex:old1  ex:hasList  [ a          rdf:List ;
                        rdf:first  ex:B1 ;
                        rdf:rest   [ a          rdf:List ;
                                     rdf:first  ex:B2 ;
                                     rdf:rest   ()
                                   ]
                      ] .

Running code example and input data attached.

Grateful for all help.

Regards,
Martin G. Skj�veland

Re: Problems replacing RDFLists

Posted by "Martin G. Skjæveland" <m....@gmail.com>.
Suggested solution to own problem.

Hi Martin,

seems there are two problems.

The first is that you with your call RDFList.sameListAs(RDFList) are 
comparing against lists that you might already have deleted from the 
model (in a previous iteration of the for loop). A solution is to 
"safekeep" the contents of the 'old' list in a java list and compare the 
model's lists as java lists:

public static void substituteNonEmptyRDFList (Model model, RDFList old, 
RDFList fresh) {

   List<RDFNode> oldContent = old.asJavaList();
   List<RDFNode> oldHeads = new ArrayList<>();

   for (RDFList l : getNonEmptyRDFLists(model)) {
     if (oldContent.equals(l.asJavaList())){
       l.removeList();
       oldHeads.add(l);
    }
  }

   // replace pointers to old lists to new copies of fresh lists:
   for (RDFNode n : oldHeads) {
     RDFList freshCopy = fresh.copy();				
     for (Statement s: model.listStatements(null, null, n).toList()) {
       s.changeObject(freshCopy);
     }
   }
}

The second problem seems to be with how Jena's RDFListImpl copies 
RDFLists, adding rdf:type rdf:List to all list items. In 
RDFListImpl.newListCell the following comment

   Note: following the RDF WG decision, we no longer assert rdf:type 
rdf:List for list cells

suggests that this is not necessary.

In RDFListImpl.copy, replacing

1121  Resource cell = getModel().createResource( cellType );

with

1121  Resource cell = getModel().createResource();

seems to fix the problem.

Cheers,
Martin

On 20/04/17 21:46, Martin G. Skj�veland wrote:
> Dear list,
>
> I want to replace all the RDF lists in a model which have contents equal
> to given input RDF list 'old' with a copies of an RDF list 'fresh'.
>
> Following the advise in
> https://jena.apache.org/documentation/javadoc/jena/org/apache/jena/rdf/model/RDFList.html#removeList--,
> I remove all lists equal to 'old' and update old's heads to new copies
> of fresh.
>
> However, running the code has two results, either it fails with the
> exception:
>
> Exception in thread "main"
> org.apache.jena.shared.PropertyNotFoundException:
> http://www.w3.org/1999/02/22-rdf-syntax-ns#first
>     at
> org.apache.jena.rdf.model.impl.ModelCom.getRequiredProperty(ModelCom.java:1231)
>
>     at
> org.apache.jena.rdf.model.impl.ResourceImpl.getRequiredProperty(ResourceImpl.java:172)
>
>     at
> org.apache.jena.rdf.model.impl.RDFListImpl.sameListAs(RDFListImpl.java:876)
>     at ListTest.substituteNonEmptyRDFList(ListTest.java:39)
>     at ListTest.main(ListTest.java:65)
>
>
> or it outputs a model where the replaced list seems OK, but does not
> seem to be recognized as a proper RDF list (due to the lack of terse
> list formatting):
>
> @prefix ex:    <http://example.com#> .
> @prefix rdf:   <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
>
> ex:fresh1  ex:hasList  ( ex:B1 ex:B2 ) .
>
> ex:old1  ex:hasList  [ a          rdf:List ;
>                        rdf:first  ex:B1 ;
>                        rdf:rest   [ a          rdf:List ;
>                                     rdf:first  ex:B2 ;
>                                     rdf:rest   ()
>                                   ]
>                      ] .
>
> Running code example and input data attached.
>
> Grateful for all help.
>
> Regards,
> Martin G. Skj�veland