2

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.

Stanislav Kralin
  • 11,070
  • 4
  • 35
  • 58
  • [`ResourceFactory.createResource()`](https://jena.apache.org/documentation/javadoc/jena/org/apache/jena/rdf/model/ResourceFactory.html#createResource--) is the correct approach. Use the [API rather than SPI](https://stackoverflow.com/a/6982965/1371329) for manipulating RDF data. – jaco0646 Jul 26 '17 at 01:12

1 Answers1

2

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]

durschtnase
  • 156
  • 2
  • 7