01: package org.ontoware.rdf2go.util.transform;
02:
03: import java.util.Map;
04:
05: import org.ontoware.aifbcommons.collection.ClosableIterator;
06: import org.ontoware.rdf2go.RDF2Go;
07: import org.ontoware.rdf2go.model.Diff;
08: import org.ontoware.rdf2go.model.Model;
09: import org.ontoware.rdf2go.model.Statement;
10: import org.ontoware.rdf2go.model.impl.DiffImpl;
11: import org.ontoware.rdf2go.model.node.URI;
12: import org.slf4j.Logger;
13: import org.slf4j.LoggerFactory;
14:
15: public class InferenceRule implements TransformerRule {
16:
17: private static Logger log = LoggerFactory
18: .getLogger(InferenceRule.class);
19:
20: String search, add;
21:
22: public InferenceRule(String search, String add) {
23: super ();
24: this .search = search;
25: this .add = add;
26: }
27:
28: public String getSearch() {
29: return search;
30: }
31:
32: public String getAdd() {
33: return add;
34: }
35:
36: public void applyRule(Model model, Map<String, URI> namespaceMap) {
37:
38: Model removeModel = RDF2Go.getModelFactory().createModel();
39: removeModel.open();
40:
41: Model addModel = RDF2Go.getModelFactory().createModel();
42: addModel.open();
43:
44: String query = Transformer.toSparqlConstruct(namespaceMap, add,
45: search);
46:
47: ClosableIterator<Statement> it = model.sparqlConstruct(query)
48: .iterator();
49: while (it.hasNext()) {
50: Statement stmt = it.next();
51: log.debug("rule infers " + stmt);
52: addModel.addStatement(stmt);
53: }
54: it.close();
55:
56: Diff diff = new DiffImpl(addModel.iterator(), removeModel
57: .iterator());
58: addModel.close();
59: removeModel.close();
60: model.update(diff);
61: }
62:
63: }
|