Search code examples
jena

Can the subjects of all statements in a Model be replaced in Jena?


A tool that I use can output RDF/XML. I want to use that, but based on the contents I want to construct a more meaningful URI. I can edit the input RDF/XML and replace the subject URI there, but by that time I don't know the desired URI yet. All the statements in the resulting Model have the same Subject. Can I replace them in one go after the Model is contructed?


Solution

  • The easiest solution I came up with, was just creating a new Model, and adding all the Statements from the old Model with a different Subject:

        Model model = ...
        Model model2 = ModelFactory.createDefaultModel();
        LocalDate date = getDate(model);
        String uri = toUri(date, filename);
        Resource subject = model.createResource(uri);
        model.listStatements().forEach(stmt -> {
            model2.add(subject, stmt.getPredicate(), stmt.getObject());
        });