-
Notifications
You must be signed in to change notification settings - Fork 4
/
FetchPossibleValuesModule.java
101 lines (81 loc) · 3.73 KB
/
FetchPossibleValuesModule.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
package cz.cvut.spipes.modules;
import cz.cvut.sforms.SFormsVocabularyJena;
import cz.cvut.spipes.constants.KBSS_MODULE;
import cz.cvut.spipes.constants.SML;
import cz.cvut.spipes.engine.ExecutionContext;
import cz.cvut.spipes.form.JenaFormUtils;
import cz.cvut.spipes.modules.annotations.SPipesModule;
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 java.util.HashMap;
import java.util.Map;
/**
* Inputs are forms using Q&A model. Possible values of questions are added to questions that does not have
* any value attached and contains possible value query.
*/
@SPipesModule(label = "fetch possible values", comment =
"Fetches possible values for answers of questions. Inputs are forms using Q&A model. Possible values of " +
"questions are added to questions that does not have any value attached and contains possible value query."
)
public class FetchPossibleValuesModule extends AnnotatedAbstractModule {
private static final Logger LOG = LoggerFactory.getLogger(FetchPossibleValuesModule.class);
private static final String TYPE_URI = KBSS_MODULE.uri + "fetch-possible-values";
@Parameter(urlPrefix = SML.uri, name = "replace", comment = "Replace context flag, default is false.")
private boolean isReplace = false;
@Override
ExecutionContext executeSelf() {
Model inpModel = this.getExecutionContext().getDefaultModel();
Model constructedModel = ModelFactory.createDefaultModel();
Map<String, PossibleValuesQueryProcessor> query2possibleValue = new HashMap<>();
JenaFormUtils.getQuestions(inpModel)
.filterKeep(q -> q.hasProperty(SFormsVocabularyJena.s_p_has_possible_values_query))
.filterKeep(q -> !q.hasProperty(SFormsVocabularyJena.s_p_has_possible_value))
.forEachRemaining(
q -> {
String possibleValuesQuery = getPossibleValuesQuery(q);
PossibleValuesQueryProcessor qProcessor = query2possibleValue.get(possibleValuesQuery);
if (qProcessor == null) {
qProcessor = new PossibleValuesQueryProcessor(possibleValuesQuery);
LOG.debug("Retrieved {} new possible values for question {}.",
qProcessor.getPossibleValuesCount(), q.getURI());
query2possibleValue.put(possibleValuesQuery, qProcessor);
} else {
LOG.debug("Using cache of {} possible values for question {}.",
qProcessor.getPossibleValuesCount(), q.getURI());
}
qProcessor.addQuestion(q);
}
);
query2possibleValue.values().forEach(
qP -> {
constructedModel.add(qP.getPossibleValuesModel());
qP.getRelatedQuestions().forEach(
q -> qP.getPossibleValueResources().forEach(
v -> constructedModel.add(q, SFormsVocabularyJena.s_p_has_possible_value, v)
)
);
}
);
return createOutputContext(isReplace, constructedModel);
}
public String getPossibleValuesQuery(Resource question) {
return question
.getRequiredProperty(SFormsVocabularyJena.s_p_has_possible_values_query)
.getObject()
.asLiteral()
.toString();
}
public boolean isReplace() {
return isReplace;
}
public void setReplace(boolean replace) {
isReplace = replace;
}
@Override
public String getTypeURI() {
return TYPE_URI;
}
}