40383

Jena TDB after reason then update

Question:

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; }

Answer1:

<strong>Note</strong> 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.

<strong>Materializing Deductions</strong>

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.

<pre class="lang-java prettyprint-override">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.

<strong>Explanation</strong>

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 <em>least</em> a little more efficient than adding infModel, but it is not applicable if you depend on backwards-chaining inferences.

Recommend

  • Ho to achieve Mapping between namespaces in Apache Jena thru Reasoning?
  • ASP.NET Gridview Paging Problem
  • Fancy dynamic list in Android: TableLayout vs ListView
  • Keep same scale in different graphs ggplot2
  • Stacked Bar Chart with percentage composition inside the Bar and total above the Bar in JFreeChart
  • IDX10503: Signature validation failed
  • Can you use DataSet and DataTables in a Portable Class Library
  • Mockery and Laravel constructor injection
  • Eloquent Relationship on Partial Match
  • d3.js selection conditional rendering
  • How can I reset dropdown data if modal closed on vue component?
  • Is it better to have roles as a column on my users table, or do it through join tables (Roles &
  • How to merge keras sequential models with same input?
  • Returning the auto incrementing value after an insert using slick
  • Django model for a Postgres view
  • Activation Function choice for Neural network
  • How to getText() from the input field of an angularjs Application
  • How to return DataSet (xsd) in WCF
  • Combining two different ActiveRecord collections into one
  • Angularjs pass function from Controller to Directive (or call controller function from directive) -
  • Django invalid literal for int() with base 10
  • Eloquent update method change created_at timestamp
  • WPF - CanExecute dosn't fire when raising Commands from a UserControl
  • Checking free space on FTP server
  • Change Inet root folder for iis 7
  • The plugin 'org.apache.maven.plugins:maven-jboss-as-plugin' does not exist or no valid ver
  • Spring security and special characters
  • What is the “return” in scheme?
  • Trying to switch camera back to front but getting exception
  • Updated Ionic CLI but shows previous version (Windows)
  • json Serialization in asp
  • Free memory of cv::Mat loaded using FileStorage API
  • Angular 2 constructor injection vs direct access
  • What are the advantages and disadvantages of reading an entire file into a single String as opposed
  • MATLAB: Piecewise function in curve fitting toolbox using fittype
  • Programmatically clearing map cache
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Binding checkboxes to object values in AngularJs
  • How can I use `wmic` in a Windows PE script?