Hi,

I would like a method Model clone (Model) that creates a copy of a model such that any blank nodes in the copy are kept apart from the source.

The test case below illustrates the intended use: adding the clone of a model back into the model should increase the size of the model, if the model contains blank nodes.

Is there such a method in the API, or need I invent my own? If so, any suggestion of an optimal way will be greatly appreciated.

I have tried by adding a model into a fresh model. My other suggestions are to iterate over all blank nodes in a model and replace all instances with a fresh node, or to use a SPARQL query CONSTRUCT { ?s ?p ?o } WHERE { ?s ?p ?o }.

Thanks!
Martin


import static org.junit.Assert.*;

import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.ModelFactory;
import org.apache.jena.rdf.model.ResourceFactory;
import org.apache.jena.vocabulary.OWL;
import org.apache.jena.vocabulary.RDF;
import org.junit.Test;

public class ModelsTest {

  @Test
  public void shouldCloneModel () {

    // create the model:  [] a Thing .
    Model source = ModelFactory.createDefaultModel();
    source.add(ResourceFactory.createResource(), RDF.type, OWL.Thing);

    // OK
    assertEquals(1, source.size());

    // clone
    Model copy = clone(source);

    // OK
    assertEquals(1, copy.size());


    // adding cloned copy back into source
    source.add(copy);

    // Fails with current implementation
    assertEquals(2, source.size());
  }

  public Model clone (Model source) {
    Model copy = ModelFactory.createDefaultModel();
    copy.add(source);
    return copy;
  }
}

Reply via email to