1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package cz.zcu.mre.sparkle.data.resourceStorage;
21
22 import cz.zcu.mre.sparkle.Messages;
23 import cz.zcu.mre.sparkle.SparklePreferences;
24 import cz.zcu.mre.sparkle.data.DataAgent;
25 import cz.zcu.mre.sparkle.data.dataAgentFactory.DataAgentFactory;
26 import cz.zcu.mre.sparkle.gui.dialogs.other.ErrorDialog;
27 import cz.zcu.mre.sparkle.gui.dialogs.other.ProgressDialog;
28 import cz.zcu.mre.sparkle.tools.CancellableConsumer;
29 import javafx.beans.property.SimpleBooleanProperty;
30 import javafx.beans.value.ChangeListener;
31 import javafx.beans.value.ObservableBooleanValue;
32 import javafx.concurrent.Task;
33 import javafx.stage.Stage;
34 import org.apache.jena.query.QuerySolution;
35 import org.apache.jena.rdf.model.Property;
36 import org.apache.jena.rdf.model.RDFNode;
37 import org.apache.jena.rdf.model.Resource;
38 import org.apache.jena.vocabulary.OWL;
39 import org.apache.jena.vocabulary.RDF;
40 import java.io.File;
41 import java.io.Serializable;
42 import java.lang.reflect.Field;
43 import java.util.List;
44 import java.util.Set;
45 import java.util.concurrent.ExecutionException;
46 import java.util.logging.Level;
47 import java.util.logging.Logger;
48
49
50
51
52
53 class ResourceStorageUpdater
54 implements Serializable {
55
56 private static final long serialVersionUID = 324354987984654654L;
57
58 private static final Logger LOG = Logger.getLogger(ResourceStorageUpdater.class.getName());
59
60 private final ResourcesStorage resourcesStorage;
61
62 public ResourceStorageUpdater(ResourcesStorage resourcesStorage) {
63 this.resourcesStorage = resourcesStorage;
64 }
65
66
67
68
69
70
71
72
73
74
75
76
77 public final int addFromStorage(
78 final String namespace, final boolean includeNamedIndividuals, final ObservableBooleanValue cancelled) {
79
80 return addFromStorage(namespace, includeNamedIndividuals, cancelled, resourcesStorage.getDataAgent());
81 }
82
83
84
85
86
87
88
89
90
91
92
93
94
95 public int addFromStorage(final String namespace, final boolean includeNamedIndividuals,
96 final ObservableBooleanValue cancelled, final DataAgent dataAgent) {
97 final int[] result = {0};
98
99 List<String> langs = ResourceQueryBuilder.getCommaSeparatedPreferences(SparklePreferences.TITLES_LANG);
100 List<String> titles = ResourceQueryBuilder.getCommaSeparatedPreferences(SparklePreferences.TITLES);
101 List<String> langVars = ResourceQueryBuilder.getVarsNames(langs, titles);
102
103 final CancellableConsumer<QuerySolution> consumer = new CancellableConsumer<QuerySolution>() {
104 @Override
105 public void accept(final QuerySolution qs) {
106 if (addFromQuerySolution(qs, "r", langVars)) {
107 result[0]++;
108 }
109 }
110 };
111
112 final ChangeListener<Boolean> cancellationListener = (observable, oldValue, newValue) -> {
113 if (newValue) {
114 consumer.cancel();
115 }
116 };
117
118 cancelled.addListener(cancellationListener);
119
120 String query = ResourceQueryBuilder.getQuery(namespace, includeNamedIndividuals, langs, titles, langVars,
121 resourcesStorage.getPrefixesStorage().getPrefixToIri());
122 dataAgent.select(query, consumer);
123
124 cancelled.removeListener(cancellationListener);
125
126 return result[0];
127 }
128
129
130
131
132
133
134
135
136
137
138
139 @SuppressWarnings("SameParameterValue")
140 private boolean addFromQuerySolution(final QuerySolution qs, final String varName,
141 List<String> titles) {
142
143 if (!qs.contains(varName)) {
144 return false;
145 }
146 String title = "";
147 for (String lang : titles) {
148 if (!qs.contains(lang)) {
149 continue;
150 }
151 title = qs.get(lang).asLiteral().getString();
152 if (!title.isEmpty()) {
153 break;
154 }
155 }
156
157 final RDFNode resourceNode = qs.get(varName);
158 if (!resourceNode.isResource()) {
159 return false;
160 }
161
162 final Resource resource = resourceNode.asResource();
163 return addPropertyOrResource(resource, title);
164 }
165
166
167
168
169
170
171
172
173
174
175
176
177 boolean addFromField(final Field field)
178 throws IllegalArgumentException, IllegalAccessException {
179 if (!java.lang.reflect.Modifier.isStatic(field.getModifiers())
180 || !Resource.class.isAssignableFrom(field.getType())) {
181 return false;
182 }
183
184 final Object o = field.get(null);
185 if (o == null) {
186 return false;
187 }
188
189 final Resource r = Resource.class.cast(field.get(null));
190 if (r.getLocalName().isEmpty()) {
191 return false;
192 }
193
194 return addPropertyOrResource(r, "");
195 }
196
197
198
199
200
201
202
203
204
205
206
207 private boolean addPropertyOrResource(Resource r, String title) {
208 if (r instanceof Property) {
209 return addPropertyOrResource((Property) r, title);
210 }
211 if (r.getModel() == null) {
212 r = resourcesStorage.getDataAgent().inModel(r);
213 }
214
215 boolean hasOWLProperty = r.hasProperty(RDF.type, OWL.ObjectProperty)
216 || r.hasProperty(RDF.type, OWL.FunctionalProperty)
217 || r.hasProperty(RDF.type, OWL.DatatypeProperty)
218 || r.hasProperty(RDF.type, OWL.OntologyProperty)
219 || r.hasProperty(RDF.type, OWL.AnnotationProperty)
220 || r.hasProperty(RDF.type, OWL.SymmetricProperty)
221 || r.hasProperty(RDF.type, OWL.TransitiveProperty);
222 boolean hasRdfProperty = r.hasProperty(RDF.type, RDF.Property);
223
224 return resourcesStorage.addTerm(hasRdfProperty || hasOWLProperty
225 ? new PropertyStorageEntry(r.as(Property.class), title)
226 : new ResourceStorageEntry(r, title));
227 }
228
229
230
231
232
233
234
235
236 private boolean addPropertyOrResource(Property p, String title) {
237 if (p.getModel() == null) {
238 p = resourcesStorage.getDataAgent().inModel(p);
239 }
240 return resourcesStorage.addTerm(new PropertyStorageEntry(p, title));
241 }
242
243
244
245
246
247
248
249
250 public void updateResourceStorage(Stage stage, List<File> files, Set<String> namespaces) {
251
252 final Task<Boolean> task = new Task<Boolean>() {
253 @Override
254 protected Boolean call() throws Exception {
255
256 updateMessage(Messages.getString("TERMS_LOADING_IN_PROGRESS"));
257
258 final SimpleBooleanProperty cancelled = new SimpleBooleanProperty(false);
259
260 int index = 1;
261 updateProgress(0, files.size());
262
263 for (File file : files) {
264 DataAgent tempDA = DataAgentFactory.createTemp();
265 tempDA.addToModel(file);
266
267 for (String namespace : namespaces) {
268 updateTitle(Messages.getString("UPDATING") + ": " + index + "/"
269 + (files.size() * namespaces.size()));
270 index = updateStorageFromFile(cancelled, index, file, tempDA, namespace);
271 }
272 }
273 return true;
274 }
275
276
277
278
279
280
281
282
283
284
285
286
287 private int updateStorageFromFile(SimpleBooleanProperty cancelled, int index, File file, DataAgent tempDA,
288 String namespace) {
289 try {
290 updateMessage(file.getName() + "\n" + namespace + "\n"
291 + Messages.getString("TERMS_LOADING_IN_PROGRESS"));
292
293 addFromStorage(namespace, true, cancelled, tempDA);
294 updateProgress(++index, files.size());
295
296 } catch (Error e) {
297 ErrorDialog.open(stage, Messages.getString("ERROR"),
298 Messages.getString("OPERATION_FAILED"), e);
299 LOG.log(Level.SEVERE, "Exception: ", e);
300 }
301 return index;
302 }
303
304 };
305
306 performUpdateStorageFromFIle(stage, task, true);
307 }
308
309
310
311
312
313
314
315
316
317 public int updateResourceStorage(Stage stage, String namespace, int size, int index) {
318 return updateResourceStorage(stage, namespace, size, index, true);
319 }
320
321
322
323
324
325
326
327
328
329
330 public int updateResourceStorage(Stage stage, String namespace, int size, int index, boolean includeIndividuals) {
331
332 int updated = index;
333
334 final Task<Boolean> task = new Task<Boolean>() {
335 @Override
336 protected Boolean call() {
337
338 LOG.log(Level.INFO, "Updating {0}: {1}/{2}", new Object[]{namespace, updated, size});
339
340 updateTitle(Messages.getString("UPDATING") + ": " + updated + "/" + size);
341 String prefix = resourcesStorage.getPrefixesStorage().getNsURIPrefix(namespace);
342 updateMessage(prefix + ": " + namespace + "\n" + Messages.getString("TERMS_LOADING_IN_PROGRESS"));
343
344 final SimpleBooleanProperty cancelled = new SimpleBooleanProperty(false);
345
346 updateProgress(0, 1);
347
348 DataAgent tempDA = DataAgentFactory.createTemp();
349
350 try {
351 tempDA.getFullModel().read(namespace);
352
353 addFromStorage(namespace, includeIndividuals, cancelled, tempDA);
354 updateProgress(1, 1);
355
356 } catch (Exception | Error e) {
357 LOG.log(Level.INFO, "Exception: {0}\n{1}", new Object[]{namespace, e.getCause().getLocalizedMessage()});
358 return false;
359 }
360
361 return true;
362 }
363
364 };
365
366 if (!performUpdateStorageFromFIle(stage, task, false)) {
367 return index;
368 }
369
370 return ++index;
371 }
372
373
374
375
376
377
378
379 private boolean performUpdateStorageFromFIle(Stage stage, Task<Boolean> task, boolean showDialog) {
380 try {
381 return ProgressDialog.performTask(stage, task);
382
383 } catch (final OutOfMemoryError e) {
384 ErrorDialog.open(stage, Messages.getString("ERROR"),
385 Messages.getString("OUT_OF_MEMORY_FILE_LARGE"), e);
386
387 return false;
388
389 } catch (InterruptedException | ExecutionException | Error e) {
390 LOG.log(Level.INFO, "Exception: {0}", e.getCause().getLocalizedMessage());
391 if (showDialog) {
392 ErrorDialog.open(stage, Messages.getString("ERROR"), Messages.getString("IMPORT_FAILED"), e);
393 }
394 return false;
395 }
396 }
397
398 }