Search code examples
javajenablank-nodes

Adding blank nodes to a Jena model


I'm trying to populate a Jena ontology model with an existing set of triples, some of which contain blank nodes. I want to maintain these blank nodes inside this new model faithfully but I can't work out a way of adding them into a Jena model.

I have been using:

Statement s = ResourceFactory.createStatement(subject, predicate, object);

To add new statements to the model:

private OntModel model = ModelFactory.createOntologyModel();
model.add(s);

but this only allows for certain types as subject, predicate, and object; Resource subject, Property predicate, RDFNode object. None of these types allow for the adding of a blanknode as subject or object such as through:

Node subject =  NodeFactory.createBlankNode(subjectValue);

Any suggestions? I've tried just using the blanknodes as resources and creating a Resource object but that breaks everything as they become classes then and not blank nodes.

Any help would be much appreciated, been pulling my hair out with this.


Solution

  • well, if you already have an existing set of triples you can easily read them from file by using:

    OntModel model = ModelFactory.createOntologyModel();
    model.read(new FileInputStream("data.ttl"), null, "TTL");
    

    this will take care of blank nodes, see the jena documentation

    you can create a blank node by hand like this:

    Resource subject = model.createResource("s");
    Property predicate = model.createProperty("p");
    Resource object = model.createResource();
    model.add(subject, predicate, object);
    

    which will result in something like:

    [s, p, aad22737-ce84-4564-a9c5-9bdfd49b55de]