问题
I am using Jena and I want to update the new ontology into my tdb. For example. I have 100 rows in my ontology , after I add some rules and run the reasoner, there are 105 rows now. And I need to update these 5 additional rows in my tdb. How can I get this done ?
I try to google it and I found two ways. One is using sparql to update , another is truncating the tdb and add the new model into it.
Is there any other better solution?
Thanks you
--
code
void after_reasoner(Model m) {
String yago = "http://yago-knowledge.org/resource/";
Reasoner reasoner = new GenericRuleReasoner(
Rule.rulesFromURL("file:./rules/act.rule"));
InfModel inf1 = ModelFactory.createInfModel(reasoner, m);
PrintUtil.registerPrefix("yago", "http://yago-knowledge.org/resource/");
}
So again , my problem is how to deal with the new "infmodel" to my tdb. I want to update only new fact into it.
Here is my method to get model from tdb.
Model tdb_write_return() {
String directory = "./tdb";
Dataset dataset = TDBFactory.createDataset(directory);
dataset.begin(ReadWrite.WRITE);
String ns = "http://www.darrell.com.tw/ontologies/";
Model model = dataset.getNamedModel(ns);
dataset.commit();
dataset.end();
dataset.close();
return model;
}
回答1:
Note that the lifetime of the Datset
that you get from TDBFactory
should be longer than the Model
objects that you get from it. If you call Dataset.close()
on that dataset, then interactions with the returned model may suddenly experience errors.
Materializing Deductions
Regarding your question, if you want to retain your inferred triples, you will need to add them back to the underlying model. This can utilize an existing graph rather than replacing anything.
final String directory = "./tdb";
final String ns = "http://www.darrell.com.tw/ontologies/";
final Dataset dataset = TDBFactory.createDataset(directory);
dataset.begin(ReadWrite.WRITE);
try {
Model model = dataset.getNamedModel(ns);
final Reasoner reasoner = new GenericRuleReasoner(Rule.rulesFromURL("file:./rules/act.rule"));
final InfModel infModel = ModelFactory.createInfModel(reasoner, m);
infModel.prepare()
model.add(infModel.getDeductionsModel()); // #1
dataset.commit();
}
catch(final Exception e) {
dataset.abort();
}
finally {
dataset.end();
}
In the above example the line with comment #1
takes the results of your (forward) deductions and inserts them back into the underlying model. To get all dedudctions you can do something like:
model.add(infModel);
The reasoner could/should consider the new triples and attempt to make new deductions, but the result of that attempted reasoning should be a no-op.
Explanation
If you add the infModel
to the underlying model, you attempt to add the union of your original model, backwards inference, and forward inference, all to the original model. You called this out in your comment. To clarify, due to the set semantics of RDF, attempting to add triples that already exist doesn't result in any changes to the data. In RDF, every quad/triple (or row, considering a relational analogy), is unique.
If you add infModel.getDeductionsModel()
to the original model, all forward-chaining inferences are inserted back into the graph. This should be in general at least a little more efficient than adding infModel
, but it is not applicable if you depend on backwards-chaining inferences.
来源:https://stackoverflow.com/questions/24924684/jena-tdb-after-reason-then-update