-
Notifications
You must be signed in to change notification settings - Fork 4
/
ApplyConstructAbstractModule.java
183 lines (133 loc) · 5.95 KB
/
ApplyConstructAbstractModule.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
171
172
173
174
175
176
177
178
179
180
181
182
183
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.util.JenaUtils;
import cz.cvut.spipes.util.QueryUtils;
import org.apache.jena.query.Query;
import org.apache.jena.query.QueryFactory;
import org.apache.jena.query.QuerySolution;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.ModelFactory;
import org.apache.jena.rdf.model.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.topbraid.spin.arq.ARQFactory;
import org.topbraid.spin.model.Construct;
import org.topbraid.spin.system.SPINModuleRegistry;
import org.topbraid.spin.vocabulary.SP;
import java.util.List;
public abstract class ApplyConstructAbstractModule extends AnnotatedAbstractModule {
private static final Logger LOG = LoggerFactory.getLogger(ApplyConstructAbstractModule.class);
//sml:constructQuery
protected List<Resource> constructQueries;
//sml:replace
protected boolean isReplace;
//kbss:parseText
/**
* Whether the query should be taken from sp:text property instead of from SPIN serialization
*/
protected boolean parseText;
//kbss:iterationCount
/**
* Maximal number of iterations of the whole rule set. 0 means 0 iterations. The actual number of iterations can be smaller,
* if no new inferences are generated any more.
* <p>
* iterationCount = 1:
* - the whole rule set is executed only once.
* iterationCount > 1:
* - the whole rule set is executed at most "iterationCount" times.
* - in each iteration, queries are evaluated on the model merged from the default model and the result of previous iteration
* <p>
* Within each iteration, all queries are evaluated on the same model.
*/
protected int iterationCount = -1;
public ApplyConstructAbstractModule() {
// TODO move elsewhere
SPINModuleRegistry.get().init(); //TODO -- downloads spin from the web (should be cached instead)
}
public boolean isParseText() {
return parseText;
}
public void setParseText(boolean parseText) {
this.parseText = parseText;
}
public boolean isReplace() {
return isReplace;
}
public void setReplace(boolean replace) {
isReplace = replace;
}
public List<Resource> getConstructQueries() {
return constructQueries;
}
public void setConstructQueries(List<Resource> constructQueries) {
this.constructQueries = constructQueries;
}
public int getIterationCount() {
return iterationCount;
}
public void setIterationCount(int iterationCount) {
this.iterationCount = iterationCount;
}
protected QuerySolution generateIterationBinding(int currentIteration, QuerySolution globalBinding) {
return globalBinding;
}
protected boolean shouldTerminate(int currentIteration, Model previousInferredModel, Model currentInferredModel) {
if (currentIteration == iterationCount) {
return true;
}
if ((currentIteration > 0) && (previousInferredModel.size() == currentInferredModel.size())) {
return true;
}
return false;
}
public abstract String getTypeURI();
public ExecutionContext executeSelf() {
Model defaultModel = executionContext.getDefaultModel();
QuerySolution bindings = executionContext.getVariablesBinding().asQuerySolution();
int count = 0;
Model inferredModel = ModelFactory.createDefaultModel();
Model previousInferredModel = ModelFactory.createDefaultModel();
if (!shouldTerminate(0, previousInferredModel, inferredModel)) {
while (!shouldTerminate(++count, previousInferredModel, inferredModel)) {
Model inferredInSingleIterationModel = ModelFactory.createDefaultModel();
QuerySolution currentIterationBindings = generateIterationBinding(count, bindings);
for (Resource constructQueryRes : constructQueries) {
Construct spinConstructRes = constructQueryRes.as(Construct.class);
Query query;
if (parseText) {
String queryStr = spinConstructRes.getProperty(SP.text).getLiteral().getString();
query = QueryFactory.create(substituteQueryMarkers(count, queryStr));
} else {
query = ARQFactory.get().createQuery(spinConstructRes);
}
Model constructedModel = QueryUtils.execConstruct(
query,
JenaUtils.createUnion(defaultModel, inferredModel),
currentIterationBindings
);
inferredInSingleIterationModel = JenaUtils.createUnion(inferredInSingleIterationModel, constructedModel);
}
previousInferredModel = inferredModel;
inferredModel = JenaUtils.createUnion(inferredModel, inferredInSingleIterationModel);
}
}
return createOutputContext(isReplace, inferredModel);
}
protected String substituteQueryMarkers(int currentIteration, String queryStr) {
return queryStr;
}
@Override
public void loadConfiguration() {
// TODO sparql expressions
// TODO load default values from configuration
// TODO does not work with string query as object is not RDF resource ???
constructQueries = getResourcesByProperty(SML.constructQuery);
LOG.debug("Loaded {} spin construct queries.", constructQueries.size());
//TODO default value must be taken from template definition
isReplace = this.getPropertyValue(SML.replace, false);
parseText = this.getPropertyValue(KBSS_MODULE.is_parse_text, false);
iterationCount = this.getPropertyValue(KBSS_MODULE.has_max_iteration_count, -1);
}
}