-
Notifications
You must be signed in to change notification settings - Fork 4
/
Rdf4jUpdateModule.java
170 lines (144 loc) · 6.05 KB
/
Rdf4jUpdateModule.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package cz.cvut.spipes.modules;
import cz.cvut.spipes.constants.KBSS_MODULE;
import cz.cvut.spipes.constants.SML;
import cz.cvut.spipes.engine.ExecutionContext;
import cz.cvut.spipes.exceptions.RepositoryAccessException;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.Property;
import org.apache.jena.rdf.model.Resource;
import org.apache.jena.rdf.model.ResourceFactory;
import org.apache.jena.vocabulary.RDF;
import org.eclipse.rdf4j.query.MalformedQueryException;
import org.eclipse.rdf4j.query.QueryLanguage;
import org.eclipse.rdf4j.query.Update;
import org.eclipse.rdf4j.query.UpdateExecutionException;
import org.eclipse.rdf4j.repository.Repository;
import org.eclipse.rdf4j.repository.RepositoryConnection;
import org.eclipse.rdf4j.repository.RepositoryException;
import org.eclipse.rdf4j.repository.sparql.SPARQLRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.topbraid.spin.vocabulary.SP;
import java.util.List;
import java.util.stream.Collectors;
public class Rdf4jUpdateModule extends AbstractModule {
private static final Logger LOG = LoggerFactory.getLogger(Rdf4jUpdateModule.class.getName());
private static final String TYPE_URI = KBSS_MODULE.getURI() + "rdf4j-update";
private static final String PROPERTY_PREFIX_URI = KBSS_MODULE.getURI() + "rdf4j";
/**
* URL of the Rdf4j server
*/
static final Property P_RDF4J_SERVER_URL = getParameter("p-rdf4j-server-url");
private String rdf4jServerURL;
/**
* Rdf4j repository ID
*/
static final Property P_RDF4J_REPOSITORY_NAME = getParameter("p-rdf4j-repository-name");
private String rdf4jRepositoryName;
private List<String> updateQueries;
private Repository updateRepository;
public void setUpdateQueries(List<String> updateQueries) {
this.updateQueries = updateQueries;
}
public List<String> getUpdateQueries() {
return updateQueries;
}
private int iterationCount;
private boolean onlyIfTripleCountChanges;
public int getIterationCount() {
return iterationCount;
}
public void setIterationCount(int iterationCount) {
this.iterationCount = iterationCount;
}
public String getRdf4jServerURL() {
return rdf4jServerURL;
}
public void setRdf4jServerURL(String rdf4jServerURL) {
this.rdf4jServerURL = rdf4jServerURL;
}
public boolean isOnlyIfTripleCountChanges() {
return onlyIfTripleCountChanges;
}
public void setOnlyIfTripleCountChanges(boolean onlyIfTripleCountChanges) {
this.onlyIfTripleCountChanges = onlyIfTripleCountChanges;
}
public String getRdf4jRepositoryName() {
return rdf4jRepositoryName;
}
public void setRdf4jRepositoryName(String rdf4jRepositoryName) {
this.rdf4jRepositoryName = rdf4jRepositoryName;
}
void setUpdateRepository(Repository updateRepository) {
this.updateRepository = updateRepository;
}
public static Resource createUpdateQueryResource(Model model, String updateQuery) {
return
model.createResource()
.addProperty(RDF.type, SML.updateQuery)
.addProperty(SP.text, ResourceFactory.createPlainLiteral(updateQuery));
}
private static Property getParameter(final String name) {
return ResourceFactory.createProperty(PROPERTY_PREFIX_URI + "/" + name);
}
@Override
ExecutionContext executeSelf() {
try (RepositoryConnection updateConnection = updateRepository.getConnection()) {
LOG.debug("Connected to {}", rdf4jRepositoryName);
for(int i = 0;i < iterationCount;i++) {
long oldTriplesCount = updateConnection.size();
for (String updateQuery : updateQueries) {
makeUpdate(updateQuery, updateConnection);
}
long newTriplesCount = updateConnection.size();
if(onlyIfTripleCountChanges && (newTriplesCount == oldTriplesCount) )break;
}
} catch (RepositoryException e) {
throw new RepositoryAccessException(rdf4jRepositoryName, e);
}
return this.executionContext;
}
void makeUpdate(String updateString, RepositoryConnection updateConnection) {
Update prepareUpdate;
try {
prepareUpdate = updateConnection.prepareUpdate(QueryLanguage.SPARQL, updateString);
} catch (MalformedQueryException e) {
LOG.error("Malformed Query, query text:\n{}",
updateString);
return;
} catch (RepositoryException e) {
LOG.error("Repository exception\n{}",
e.getMessage());
return;
}
try {
assert prepareUpdate != null;
prepareUpdate.execute();
LOG.debug("Update successful");
} catch (UpdateExecutionException e) {
LOG.error("Update execution exception, query text:\n{}\n{}",
updateString,
e.getMessage());
}
}
@Override
public String getTypeURI() {
return TYPE_URI;
}
@Override
public void loadConfiguration() {
rdf4jServerURL = getEffectiveValue(P_RDF4J_SERVER_URL).asLiteral().getString();
rdf4jRepositoryName = getEffectiveValue(P_RDF4J_REPOSITORY_NAME).asLiteral().getString();
iterationCount = getPropertyValue(KBSS_MODULE.has_max_iteration_count,1);
onlyIfTripleCountChanges = getPropertyValue(KBSS_MODULE.stop_iteration_on_stable_triple_count,false);
LOG.debug("Iteration count={}\nOnlyIf...Changes={}"
,iterationCount
,onlyIfTripleCountChanges);
if(updateRepository == null)setUpdateRepository(new SPARQLRepository(rdf4jServerURL + "repositories/" + rdf4jRepositoryName + "/statements"));
updateQueries = loadUpdateQueries();
}
private List<String> loadUpdateQueries() {
return getResourcesByProperty(SML.updateQuery).stream().map(
r -> r.getProperty(SP.text).getLiteral().getString()).collect(Collectors.toList());
}
}