-
Notifications
You must be signed in to change notification settings - Fork 4
/
ApplyConstructWithChunkedValuesAndScrollableCursorModule.java
136 lines (104 loc) · 5.08 KB
/
ApplyConstructWithChunkedValuesAndScrollableCursorModule.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
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.recursion.ChunkedValuesProvider;
import cz.cvut.spipes.recursion.CombinedQueryTemplateRecursionProvider;
import cz.cvut.spipes.recursion.QueryTemplateRecursionProvider;
import cz.cvut.spipes.recursion.ScrollableCursorProvider;
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.
* TODO issue with redundant call {@link ScrollableCursorProvider}
* TODO supports only one CONSTRUCT query
*/
@SPipesModule(label = "apply construct with chunked values and scrollable cursor", comment = "Apply construct with chunked values and scrollable cursor")
public class ApplyConstructWithChunkedValuesAndScrollableCursorModule extends ApplyConstructAbstractModule {
private static final Logger LOG = LoggerFactory.getLogger(ApplyConstructWithChunkedValuesAndScrollableCursorModule.class);
private static final String TYPE_URI = KBSS_MODULE.uri + "apply-construct-with-chunked-values-and-scrollable-cursor";
private static final String TYPE_PREFIX = TYPE_URI + "/";
private static final int DEFAULT_CHUNK_SIZE = 10;
private static final int DEFAULT_PAGE_SIZE = 10000;
private static final Property P_CHUNK_SIZE = ResourceFactory.createProperty(TYPE_PREFIX + "chunk-size");
private static final Property P_PAGE_SIZE = ResourceFactory.createProperty(TYPE_PREFIX + "page-size");
@Parameter(urlPrefix = TYPE_PREFIX, name = "chunk-size")
private Integer chunkSize = DEFAULT_CHUNK_SIZE;
@Parameter(urlPrefix = TYPE_PREFIX, name = "page-size")
private Integer pageSize = DEFAULT_PAGE_SIZE;
@Parameter(urlPrefix = SML.uri, name = "selectQuery")
private Select selectQuery;
private QueryTemplateRecursionProvider recursionProvider;
@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;
}
private ResultSet getSelectQueryResultSet() {
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 ...");
ResultSet selectResultSet = execution.execSelect();
VariablesBinding variablesBinding = new VariablesBinding();
if (!selectResultSet.hasNext()) {
LOG.debug("\"{}\" query did not return any values.", getLabel());
}
return selectResultSet;
}
@Override
protected boolean shouldTerminate(int currentIteration, Model previousIterationModel, Model currentIterationModel) {
if (!parseText) {
throw new IllegalArgumentException("Construct queries with SPIN notations [parseText=false] are not supported as they do not support additions of comments.");
}
return getRecursionProvider().shouldTerminate(currentIteration, previousIterationModel, currentIterationModel);
}
@Override
protected String substituteQueryMarkers(int currentIteration, String queryStr) {
return getRecursionProvider().substituteQueryMarkers(currentIteration, 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);
pageSize = this.getPropertyValue(P_PAGE_SIZE, DEFAULT_PAGE_SIZE);
}
@NotNull
private QueryTemplateRecursionProvider getRecursionProvider() {
if (recursionProvider == null) {
ResultSet rs = getSelectQueryResultSet();
recursionProvider = new CombinedQueryTemplateRecursionProvider(
iterationCount,
new ChunkedValuesProvider(rs, chunkSize, iterationCount),
new ScrollableCursorProvider(pageSize, iterationCount)
);
}
return recursionProvider;
}
}