-
Notifications
You must be signed in to change notification settings - Fork 4
/
ApplyConstructWithChunkedValuesModule.java
139 lines (103 loc) · 4.52 KB
/
ApplyConstructWithChunkedValuesModule.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
package cz.cvut.spipes.modules;
import cz.cvut.spipes.constants.KBSS_MODULE;
import cz.cvut.spipes.constants.SML;
import cz.cvut.spipes.engine.VariablesBinding;
import cz.cvut.spipes.modules.annotations.SPipesModule;
import cz.cvut.spipes.util.QueryUtils;
import java.util.Objects;
import org.apache.jena.query.Query;
import org.apache.jena.query.QueryExecution;
import org.apache.jena.query.QueryExecutionFactory;
import org.apache.jena.query.QuerySolution;
import org.apache.jena.query.ResultSet;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.Property;
import org.apache.jena.rdf.model.ResourceFactory;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.topbraid.spin.arq.ARQFactory;
import org.topbraid.spin.model.Select;
/**
* TODO Order of queries is not enforced.
*/
@SPipesModule(label = "apply construct with chunked values", comment = "Apply construct with chunked values.")
public class ApplyConstructWithChunkedValuesModule extends ApplyConstructAbstractModule {
private static final Logger LOG = LoggerFactory.getLogger(ApplyConstructWithChunkedValuesModule.class);
private static final String TYPE_URI = KBSS_MODULE.uri + "";
private static final String TYPE_PREFIX = TYPE_URI + "/";
private static final int DEFAULT_CHUNK_SIZE = 10;
private static final String VALUES_CLAUSE_MARKER_NAME = "VALUES";
private static final Property P_CHUNK_SIZE = ResourceFactory.createProperty(TYPE_PREFIX + "chunk-size");
@Parameter(urlPrefix = TYPE_PREFIX, name = "chunk-size", comment = "Chunk size. Default is 10.")
private Integer chunkSize = DEFAULT_CHUNK_SIZE;
@Parameter(urlPrefix = SML.uri, name = "selectQuery"
, comment = "The select query that will be used to iterate over construct query templates.")
private Select selectQuery;
private ResultSet selectResultSet;
@Override
public String getTypeURI() {
return TYPE_URI;
}
public int getChunkSize() {
return chunkSize;
}
public void setChunkSize(int chunkSize) {
this.chunkSize = chunkSize;
}
public Select getSelectQuery() {
return selectQuery;
}
public void setSelectQuery(Select selectQuery) {
this.selectQuery = selectQuery;
}
public void initializeQuery() {
Query query = ARQFactory.get().createQuery(selectQuery);
QuerySolution inputBindings = executionContext.getVariablesBinding().asQuerySolution();
QueryExecution execution = QueryExecutionFactory.create(query, executionContext.getDefaultModel(), inputBindings);
LOG.debug("Executing query of chunk provider ...");
selectResultSet = execution.execSelect();
VariablesBinding variablesBinding = new VariablesBinding();
if (! selectResultSet.hasNext()) {
LOG.debug("\"{}\" query did not return any values.", getLabel());
}
}
@Override
protected boolean shouldTerminate(int currentIteration, Model previousInferredModel, Model currentInferredModel) {
if (!parseText) {
throw new IllegalArgumentException("Construct queries with SPIN notations [parseText=false] are not supported as they do not support additions of comments.");
}
if (currentIteration == iterationCount) {
return true;
}
if (! getCurrentResultSetInstance().hasNext()) {
return true;
}
return false;
}
@Override
protected String substituteQueryMarkers(int currentIteration, String queryStr) {
String markerValue = QueryUtils.nextResultsToValuesClause(getCurrentResultSetInstance(), chunkSize);
LOG.debug("Creating query with values clause: \n{}.", markerValue );
return QueryUtils
.substituteMarkers(VALUES_CLAUSE_MARKER_NAME,
"\n" + markerValue + "\n",
queryStr);
}
@Override
public void loadConfiguration() {
super.loadConfiguration();
//iterationCount = this.getPropertyValue(KBSS_MODULE.has_max_iteration_count, 1);
parseText = this.getPropertyValue(KBSS_MODULE.is_parse_text, true);
chunkSize = this.getPropertyValue(P_CHUNK_SIZE, DEFAULT_CHUNK_SIZE);
selectQuery = getPropertyValue(SML.selectQuery).asResource().as(Select.class);
}
@NotNull
private ResultSet getCurrentResultSetInstance() {
if (selectResultSet == null) {
initializeQuery();
}
Objects.nonNull(selectResultSet);
return selectResultSet;
}
}