Adding blank nodes to a Jena model

ε祈祈猫儿з 提交于 2019-12-02 14:56:52

问题


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.


回答1:


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]



来源:https://stackoverflow.com/questions/45309944/adding-blank-nodes-to-a-jena-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!