-
Notifications
You must be signed in to change notification settings - Fork 4
/
ConstructFormMetadataModule.java
220 lines (169 loc) · 6.69 KB
/
ConstructFormMetadataModule.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
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 org.apache.commons.codec.digest.DigestUtils;
import org.apache.jena.rdf.model.*;
import org.apache.jena.vocabulary.RDF;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.LinkedList;
import java.util.List;
import static cz.cvut.spipes.form.JenaFormUtils.getAnswerOrigin;
import static cz.cvut.spipes.form.JenaFormUtils.getQuestionOrigin;
/**
* Compute form:has-origin-path and form:has-origin-path-id properties.
*/
public class ConstructFormMetadataModule extends AnnotatedAbstractModule {
private static final Logger LOG = LoggerFactory.getLogger(ConstructFormMetadataModule.class);
private static final String TYPE_URI = KBSS_MODULE.uri + "construct-form-metadata";
private static final String PATH_SEPARATOR = ",";
private static final String INSTANCE_TYPE_SEPARATOR = "|";
@Parameter(urlPrefix = SML.uri, name = "replace")
private boolean isReplace = false;
private enum Origin {
QUESTION_ORIGIN("QO"),
ANSWER_ORIGIN("A0");
String abbr;
Origin(String abbr) {
this.abbr = abbr;
}
@Override
public String toString() {
return abbr;
}
}
@Override
ExecutionContext executeSelf() {
Model inpModel = this.getExecutionContext().getDefaultModel();
List<Resource> rootQuestions = getRootQuestions(inpModel);
LOG.debug("Found {} root questions.", rootQuestions.size());
if (LOG.isTraceEnabled()) {
LOG.trace("Found root questions: {}", rootQuestions);
}
Model constructedModel = ModelFactory.createDefaultModel();
rootQuestions.forEach(
q -> processFormEntity(q, null, constructedModel)
);
return createOutputContext(isReplace, constructedModel);
}
public boolean isReplace() {
return isReplace;
}
public void setReplace(boolean replace) {
isReplace = replace;
}
private void processFormEntity(final Resource formEntity, final String parentPath, Model constructedModel) {
String path = constructPath(parentPath, formEntity);
String pathId = getPathId(path);
getSubEntities(formEntity).forEach(
e -> processFormEntity(e, path, constructedModel)
);
constructedModel.add(formEntity, SFormsVocabularyJena.s_p_has_origin_path, path);
constructedModel.add(formEntity, SFormsVocabularyJena.s_p_has_origin_path_id, pathId);
}
private String getPathId(Resource question) {
return question
.getProperty(SFormsVocabularyJena.s_p_has_origin_path)
.getObject()
.asLiteral()
.getString();
}
@NotNull
private String constructPath(@Nullable String parentPath, @NotNull Resource formEntity) {
StringBuilder sb = new StringBuilder();
if (parentPath != null) {
sb.append(parentPath).append(PATH_SEPARATOR);
}
return sb
.append(getFormEntityOrigin(formEntity))
.append(INSTANCE_TYPE_SEPARATOR)
.append(getFormEntityOriginType(formEntity))
.toString();
}
private String getPathId(String path) {
return DigestUtils.md5Hex(path);
}
private Origin getFormEntityOriginType(Resource formEntity) {
if (isQuestion(formEntity)) {
return Origin.QUESTION_ORIGIN;
}
if (isAnswer(formEntity)) {
return Origin.ANSWER_ORIGIN;
}
throw getExceptionProvidedResourceNotAFormEntity(formEntity);
}
private Resource getFormEntityOrigin(Resource formEntity) {
if (isQuestion(formEntity)) {
return getQuestionOrigin(formEntity);
}
if (isAnswer(formEntity)) {
return getAnswerOrigin(formEntity);
}
throw getExceptionProvidedResourceNotAFormEntity(formEntity);
}
private List<Resource> getRootQuestions(Model formModel) {
return formModel.listSubjects()
.filterKeep(
subj -> subj.hasProperty(
RDF.type,
SFormsVocabularyJena.s_c_question
)
)
.filterDrop(
subj -> formModel.listResourcesWithProperty(
SFormsVocabularyJena.s_p_has_related_question, subj).hasNext()
).toList();
}
private static Resource getOrigin(Resource formEntity) {
if (isQuestion(formEntity)) {
return getQuestionOrigin(formEntity);
}
if (isAnswer(formEntity)) {
return getAnswerOrigin(formEntity);
}
throw getExceptionProvidedResourceNotAFormEntity(formEntity);
}
private static boolean isAnswer(Resource formEntity) {
return formEntity.hasProperty(RDF.type, SFormsVocabularyJena.s_c_answer);
}
private static boolean isQuestion(Resource formEntity) {
return formEntity.hasProperty(RDF.type, SFormsVocabularyJena.s_c_question);
}
private static List<Resource> getRelatedQuestions(Resource question) {
return question.listProperties(SFormsVocabularyJena.s_p_has_related_question).mapWith(st -> st.getObject().asResource()).toList();
}
private static List<Resource> getAnswers(Resource question) {
return question.listProperties(SFormsVocabularyJena.s_p_has_answer).mapWith(st -> st.getObject().asResource()).toList();
}
private static List<Resource> getSubEntities(Resource formEntity) {
List<Resource> resList = new LinkedList<>();
if (isQuestion(formEntity)) {
resList.addAll(getRelatedQuestions(formEntity));
}
resList.addAll(getAnswers(formEntity));
return resList;
}
private static boolean isLeafFormEntity(Resource formEntity) {
if (formEntity.hasProperty(SFormsVocabularyJena.s_p_has_related_question)) {
return false;
}
if (formEntity.hasProperty(SFormsVocabularyJena.s_p_has_answer)) {
return false;
}
return true;
}
private static Property getJenaProperty(String property) {
return ResourceFactory.createProperty(property);
}
private static IllegalArgumentException getExceptionProvidedResourceNotAFormEntity(Resource formEntity) {
return new IllegalArgumentException(String.format("Provided resource %s is not a form entity.", formEntity));
}
@Override
public String getTypeURI() {
return TYPE_URI;
}
}