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 SearchRemoveAddRule implements TransformerRule {
16:
17: private static Logger log = LoggerFactory
18: .getLogger(SearchRemoveAddRule.class);
19:
20: private String search, remove, add;
21:
22: public SearchRemoveAddRule(String search, String remove, String add) {
23: super ();
24: this .search = search;
25: this .remove = remove;
26: this .add = add;
27: }
28:
29: public SearchRemoveAddRule(String search, String replace) {
30: super ();
31: this .search = search;
32: this .remove = search;
33: this .add = replace;
34: }
35:
36: public void applyRule(Model model, Map<String, URI> namespaceMap) {
37: searchAndReplace(model, namespaceMap, search, remove, add);
38: }
39:
40: public static void searchAndReplace(Model model,
41: Map<String, URI> namespaceMap, String search,
42: String remove, String add) {
43: log.debug("rule remove... ");
44: Model removeModel = RDF2Go.getModelFactory().createModel();
45: removeModel.open();
46: ClosableIterator<Statement> it = model.sparqlConstruct(
47: Transformer.toSparqlConstruct(namespaceMap, remove,
48: search)).iterator();
49: while (it.hasNext()) {
50: Statement stmt = it.next();
51: log.debug("rule removes " + stmt);
52: removeModel.addStatement(stmt);
53: }
54: it.close();
55:
56: log.debug("rule remove... ");
57: Model addModel = RDF2Go.getModelFactory().createModel();
58: addModel.open();
59: it = model.sparqlConstruct(
60: Transformer
61: .toSparqlConstruct(namespaceMap, add, search))
62: .iterator();
63: while (it.hasNext()) {
64: Statement stmt = it.next();
65: log.debug("rule adds " + stmt);
66: addModel.addStatement(stmt);
67: }
68: it.close();
69:
70: Diff diff = new DiffImpl(addModel.iterator(), removeModel
71: .iterator());
72: addModel.close();
73: removeModel.close();
74: model.update(diff);
75: }
76: }
|