1

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;
}
Stanislav Kralin
  • 11,070
  • 4
  • 35
  • 58
darrelltw
  • 1,734
  • 1
  • 10
  • 21
  • 1
    Can you clarify a bit about what you're actually doing? TDB is a RDF triple store, so I assume that when you say "rows", that you mean triples. If you're outside of Java code, then using SPARQL is the most natural way to update a triple store, but if you're writing Java code, then you can easily retrieve models for the named graphs in your dataset and add statements to them with Jena. – Joshua Taylor Jul 24 '14 at 15:03
  • Yes , you got my question. But the thing is , After using reasoner . I got an Infmodel which contain my old model and new triples. I am confused about how to put Infmodel to tdb and update because it contain my old model in it. Not just new turtles. Thanks you. – darrelltw Jul 25 '14 at 14:53
  • 1
    Please show some code. What have you done so far? What didn't work the way you expected it to? A TDB dataset can contain multiple graphs, and it's not clear whether you're trying to overwrite one, or just add to it, or what. Please show what you're doing so far, and what doesn't work about it. – Joshua Taylor Jul 25 '14 at 14:58
  • Just update the code and thanks for your response – darrelltw Aug 04 '14 at 02:58

1 Answers1

2

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.

Rob Hall
  • 2,693
  • 16
  • 22
  • Thanks you ! a little question here , when talking about forward inference . Is it like this x -> y and backwards-chaining inferences is like x <- y , right ? – darrelltw Aug 20 '14 at 00:48
  • Exactly. The former are eagerly evaluated based on the content of the base model as well as the results of other rules. The latter are evaluated when a query is executed (or when you ask a model to list statements). Hybrid rules (x -> (y<-z)) exist, depending on your rule set, and will insert new backwards chaining rules into your data. – Rob Hall Aug 20 '14 at 00:56
  • Another question, the infModel.getDeductionsModel() work on genericRule. But is weird at owlReasoner. Is there any better solution with owl reasoner and get the new part of model? – darrelltw Dec 08 '14 at 07:47
  • Are you using a forward-chaining OWL reasoner / rule set? The default OWL reasoner is a hybrid reasoner (so `getDeductionsModel` will only give you the forward part of the rule set). – Rob Hall Dec 08 '14 at 14:14