1
|
package org.cybertaxonomy.utis.checklist;
|
2
|
|
3
|
import java.io.PrintStream;
|
4
|
import java.net.URI;
|
5
|
import java.util.ArrayList;
|
6
|
import java.util.EnumSet;
|
7
|
import java.util.Iterator;
|
8
|
import java.util.List;
|
9
|
|
10
|
import org.apache.lucene.queryParser.QueryParser;
|
11
|
import org.cybertaxonomy.utis.client.ServiceProviderInfo;
|
12
|
import org.cybertaxonomy.utis.query.TinkerPopClient;
|
13
|
import org.cybertaxonomy.utis.store.Neo4jStore;
|
14
|
import org.cybertaxonomy.utis.store.Neo4jStoreManager;
|
15
|
import org.cybertaxonomy.utis.tnr.msg.Classification;
|
16
|
import org.cybertaxonomy.utis.tnr.msg.NameType;
|
17
|
import org.cybertaxonomy.utis.tnr.msg.Query;
|
18
|
import org.cybertaxonomy.utis.tnr.msg.Query.Request;
|
19
|
import org.cybertaxonomy.utis.tnr.msg.Response;
|
20
|
import org.cybertaxonomy.utis.tnr.msg.Source;
|
21
|
import org.cybertaxonomy.utis.tnr.msg.Synonym;
|
22
|
import org.cybertaxonomy.utis.tnr.msg.Taxon;
|
23
|
import org.cybertaxonomy.utis.tnr.msg.TaxonBase;
|
24
|
import org.cybertaxonomy.utis.tnr.msg.TaxonName;
|
25
|
import org.cybertaxonomy.utis.tnr.msg.TnrMsg;
|
26
|
import org.cybertaxonomy.utis.utils.IdentifierUtils;
|
27
|
import org.cybertaxonomy.utis.utils.Profiler;
|
28
|
import org.cybertaxonomy.utis.utils.TnrMsgUtils;
|
29
|
import org.neo4j.graphdb.Relationship;
|
30
|
|
31
|
import com.tinkerpop.blueprints.Graph;
|
32
|
import com.tinkerpop.blueprints.Vertex;
|
33
|
import com.tinkerpop.blueprints.impls.neo4j2.Neo4j2Vertex;
|
34
|
import com.tinkerpop.blueprints.oupls.sail.GraphSail;
|
35
|
import com.tinkerpop.gremlin.java.GremlinPipeline;
|
36
|
import com.tinkerpop.pipes.util.FastNoSuchElementException;
|
37
|
import com.tinkerpop.pipes.util.structures.Table;
|
38
|
|
39
|
public class EEA_BDC_Client extends AggregateChecklistClient<TinkerPopClient> implements UpdatableStoreInfo {
|
40
|
|
41
|
/**
|
42
|
*
|
43
|
*/
|
44
|
public static final String ID = "eea_bdc";
|
45
|
public static final String LABEL = "European Environment Agency (EEA) Biodiversity data centre (BDC)";
|
46
|
public static final String DOC_URL = "http://semantic.eea.europa.eu/documentation";
|
47
|
public static final String COPYRIGHT_URL = "http://www.eea.europa.eu/legal/eea-data-policy";
|
48
|
|
49
|
// private static final String DOWNLOAD_BASE_URL = "http://localhost/download/";
|
50
|
private static final String DOWNLOAD_BASE_URL = "http://eunis.eea.europa.eu/rdf/";
|
51
|
|
52
|
private static final String SPECIES_RDF_FILE_URL = DOWNLOAD_BASE_URL + "species.rdf.gz";
|
53
|
private static final String TAXONOMY_RDF_FILE_URL = DOWNLOAD_BASE_URL + "taxonomy.rdf.gz";
|
54
|
private static final String LEGALREFS_RDF_FILE_URL = DOWNLOAD_BASE_URL + "legalrefs.rdf.gz";
|
55
|
private static final String REFERENCES_RDF_FILE_URL = DOWNLOAD_BASE_URL + "references.rdf.gz";
|
56
|
|
57
|
/**
|
58
|
* check for updates once a day
|
59
|
*/
|
60
|
private static final int CHECK_UPDATE_MINUTES = 60 * 24;
|
61
|
|
62
|
public static final EnumSet<SearchMode> SEARCH_MODES = EnumSet.of(
|
63
|
SearchMode.scientificNameExact,
|
64
|
SearchMode.scientificNameLike,
|
65
|
SearchMode.vernacularNameExact,
|
66
|
SearchMode.vernacularNameLike,
|
67
|
SearchMode.findByIdentifier);
|
68
|
|
69
|
public static enum RdfSchema {
|
70
|
|
71
|
/*
|
72
|
* xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
|
73
|
xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"
|
74
|
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
|
75
|
xmlns:dcterms="http://purl.org/dc/terms/"
|
76
|
xmlns:dc="http://purl.org/dc/elements/1.1/"
|
77
|
xmlns:dwc="http://rs.tdwg.org/dwc/terms/"
|
78
|
xmlns:owl="http://www.w3.org/2002/07/owl#"
|
79
|
xmlns="http://eunis.eea.europa.eu/rdf/species-schema.rdf#"
|
80
|
xmlns:sioc="http://rdfs.org/sioc/ns#"
|
81
|
xmlns:skos="http://www.w3.org/2004/02/skos/core#"
|
82
|
xmlns:bibo="http://purl.org/ontology/bibo/"
|
83
|
xmlns:cc="http://creativecommons.org/ns#"
|
84
|
xmlns:foaf="http://xmlns.com/foaf/0.1/"
|
85
|
*/
|
86
|
EUNIS_SPECIES("es","http://eunis.eea.europa.eu/rdf/species-schema.rdf#"),
|
87
|
EUNIS_TAXONOMY("et", "http://eunis.eea.europa.eu/rdf/taxonomies-schema.rdf#"),
|
88
|
DWC("dwc", "http://rs.tdwg.org/dwc/terms/"),
|
89
|
RDF("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#"),
|
90
|
RDFS("rdfs", "http://www.w3.org/2000/01/rdf-schema#"),
|
91
|
SKOS_CORE("scos_core", "http://www.w3.org/2004/02/skos/core#"),
|
92
|
DC("dc", "http://purl.org/dc/terms/source"),
|
93
|
DCTERMS("dcterms", "http://purl.org/dc/terms/");
|
94
|
|
95
|
private String schemaUri;
|
96
|
private String abbreviation;
|
97
|
RdfSchema(String abbreviation, String schemaUri) {
|
98
|
this.abbreviation = abbreviation;
|
99
|
this.schemaUri = schemaUri;
|
100
|
}
|
101
|
|
102
|
public String schemaUri() {
|
103
|
|
104
|
return schemaUri;
|
105
|
}
|
106
|
|
107
|
public String abbreviation() {
|
108
|
|
109
|
return abbreviation;
|
110
|
}
|
111
|
|
112
|
public String property(String name) {
|
113
|
return schemaUri + name;
|
114
|
}
|
115
|
|
116
|
}
|
117
|
|
118
|
public enum SubCheckListId {
|
119
|
|
120
|
eunis, natura_2000;
|
121
|
}
|
122
|
|
123
|
private enum RankLevel{
|
124
|
|
125
|
Kingdom, Phylum, Clazz, Order, Family, Genus;
|
126
|
}
|
127
|
|
128
|
public EEA_BDC_Client() {
|
129
|
|
130
|
super();
|
131
|
}
|
132
|
|
133
|
public EEA_BDC_Client(String checklistInfoJson) throws DRFChecklistException {
|
134
|
|
135
|
super(checklistInfoJson);
|
136
|
}
|
137
|
|
138
|
/**
|
139
|
* {@inheritDoc}
|
140
|
*/
|
141
|
@Override
|
142
|
public boolean isStatelessClient() {
|
143
|
return true;
|
144
|
}
|
145
|
|
146
|
/**
|
147
|
* {@inheritDoc}
|
148
|
*/
|
149
|
@Override
|
150
|
public String getTestUrl() {
|
151
|
return SPECIES_RDF_FILE_URL;
|
152
|
}
|
153
|
|
154
|
/**
|
155
|
* {@inheritDoc}
|
156
|
*/
|
157
|
@Override
|
158
|
public int pollIntervalMinutes() {
|
159
|
return CHECK_UPDATE_MINUTES;
|
160
|
}
|
161
|
|
162
|
/**
|
163
|
* {@inheritDoc}
|
164
|
*/
|
165
|
@Override
|
166
|
public String[] updatableResources() {
|
167
|
return new String[] {SPECIES_RDF_FILE_URL, TAXONOMY_RDF_FILE_URL, LEGALREFS_RDF_FILE_URL, REFERENCES_RDF_FILE_URL};
|
168
|
}
|
169
|
|
170
|
@Override
|
171
|
public void initQueryClient() {
|
172
|
|
173
|
Neo4jStore neo4jStore = Neo4jStoreManager.provideStoreFor(this);
|
174
|
queryClient = new TinkerPopClient(neo4jStore);
|
175
|
}
|
176
|
|
177
|
@Override
|
178
|
public ServiceProviderInfo buildServiceProviderInfo() {
|
179
|
|
180
|
ServiceProviderInfo checklistInfo = new ServiceProviderInfo(ID, LABEL, DOC_URL, COPYRIGHT_URL, getSearchModes());
|
181
|
checklistInfo.addSubChecklist(new ServiceProviderInfo(SubCheckListId.eunis.name(), "EUNIS",
|
182
|
"http://www.eea.europa.eu/themes/biodiversity/eunis/eunis-db#tab-metadata",
|
183
|
"http://www.eea.europa.eu/legal/copyright", SEARCH_MODES));
|
184
|
return checklistInfo;
|
185
|
}
|
186
|
|
187
|
|
188
|
/**
|
189
|
* @param queryString
|
190
|
* @throws DRFChecklistException
|
191
|
*/
|
192
|
private void addPrexfixes(StringBuilder queryString) throws DRFChecklistException {
|
193
|
|
194
|
for(RdfSchema schema : RdfSchema.values()) {
|
195
|
queryString.append(String.format("PREFIX %s: <%s>\n", schema.abbreviation(), schema.schemaUri()));
|
196
|
}
|
197
|
}
|
198
|
|
199
|
/**
|
200
|
* @param checklistInfo
|
201
|
* @return
|
202
|
* @throws DRFChecklistException
|
203
|
*/
|
204
|
private StringBuilder prepareQueryString() throws DRFChecklistException {
|
205
|
|
206
|
StringBuilder queryString = new StringBuilder();
|
207
|
addPrexfixes(queryString);
|
208
|
return queryString;
|
209
|
}
|
210
|
|
211
|
private Taxon createTaxon(Vertex v) {
|
212
|
|
213
|
Taxon taxon = new Taxon();
|
214
|
|
215
|
TaxonName taxonName = createTaxonName(v);
|
216
|
|
217
|
// Taxon
|
218
|
taxon.setTaxonName(taxonName);
|
219
|
taxon.setIdentifier(v.getId().toString());
|
220
|
taxon.setUrl(v.getProperty(GraphSail.VALUE).toString());
|
221
|
taxon.setAccordingTo(queryClient.relatedVertexValue(v, RdfSchema.DWC, "nameAccordingToID"));
|
222
|
URI typeUri = queryClient.vertexURI(v, RdfSchema.RDF, "type");
|
223
|
taxon.setTaxonomicStatus(typeUri.getFragment());
|
224
|
|
225
|
createSources(v, taxon);
|
226
|
|
227
|
// classification
|
228
|
Classification c = null;
|
229
|
Vertex parentV= null;
|
230
|
try {
|
231
|
parentV = queryClient.relatedVertex(v, RdfSchema.EUNIS_SPECIES, "taxonomy");
|
232
|
} catch (Exception e) {
|
233
|
logger.error("No taxonomy information for " + v.toString());
|
234
|
}
|
235
|
|
236
|
while (parentV != null) {
|
237
|
logger.debug("parent taxon: " + parentV.toString());
|
238
|
String level = queryClient.relatedVertexValue(parentV, RdfSchema.EUNIS_TAXONOMY, "level");
|
239
|
String parentTaxonName = queryClient.relatedVertexValue(parentV, RdfSchema.EUNIS_TAXONOMY, "name");
|
240
|
|
241
|
RankLevel rankLevel = null;
|
242
|
try {
|
243
|
rankLevel = RankLevel.valueOf(level);
|
244
|
} catch (Exception e) {
|
245
|
// IGNORE
|
246
|
}
|
247
|
if(rankLevel != null) {
|
248
|
if(c == null) {
|
249
|
c = new Classification();
|
250
|
}
|
251
|
switch(rankLevel) {
|
252
|
case Clazz:
|
253
|
c.setClazz(parentTaxonName);
|
254
|
break;
|
255
|
case Family:
|
256
|
c.setFamily(parentTaxonName);
|
257
|
break;
|
258
|
case Genus:
|
259
|
c.setGenus(parentTaxonName);
|
260
|
break;
|
261
|
case Kingdom:
|
262
|
c.setKingdom(parentTaxonName);
|
263
|
break;
|
264
|
case Order:
|
265
|
c.setOrder(parentTaxonName);
|
266
|
break;
|
267
|
case Phylum:
|
268
|
c.setPhylum(parentTaxonName);
|
269
|
break;
|
270
|
default:
|
271
|
break;
|
272
|
}
|
273
|
}
|
274
|
Vertex lastParentV = parentV;
|
275
|
parentV = queryClient.relatedVertex(parentV, RdfSchema.EUNIS_TAXONOMY, "parent");
|
276
|
if(lastParentV.equals(parentV)) {
|
277
|
// avoid endless looping when data is not correct
|
278
|
break;
|
279
|
}
|
280
|
}
|
281
|
if(c != null) {
|
282
|
taxon.setClassification(c);
|
283
|
}
|
284
|
return taxon;
|
285
|
}
|
286
|
|
287
|
/**
|
288
|
* @param model
|
289
|
* @param taxonR
|
290
|
* @param taxonBase
|
291
|
*/
|
292
|
private void createSources(Vertex v, TaxonBase taxonBase) {
|
293
|
|
294
|
// Sources are source references, re there others like data bases?
|
295
|
|
296
|
GremlinPipeline<Graph, Vertex> taxonPipe = new GremlinPipeline<Graph, Vertex>(v);
|
297
|
|
298
|
try {
|
299
|
List<Vertex> titleVs = taxonPipe
|
300
|
.outE(RdfSchema.EUNIS_SPECIES.property("hasLegalReference")).inV()
|
301
|
.outE(RdfSchema.DCTERMS.property("source")).inV().dedup()
|
302
|
.outE(RdfSchema.DCTERMS.property("title")).inV()
|
303
|
.toList();
|
304
|
for(Vertex tv : titleVs) {
|
305
|
Source source = new Source();
|
306
|
logger.debug(tv.toString());
|
307
|
source.setName(tv.getProperty(GraphSail.VALUE).toString());
|
308
|
taxonBase.getSources().add(source);
|
309
|
}
|
310
|
} catch (FastNoSuchElementException e) {
|
311
|
logger.debug("No sources found");
|
312
|
}
|
313
|
}
|
314
|
|
315
|
/**
|
316
|
* @param taxonR
|
317
|
* @return
|
318
|
*/
|
319
|
private TaxonName createTaxonName(Vertex v) {
|
320
|
|
321
|
TaxonName taxonName = new TaxonName();
|
322
|
// TaxonName
|
323
|
taxonName.setFullName(queryClient.relatedVertexValue(v, RdfSchema.RDFS, "label"));
|
324
|
taxonName.setCanonicalName(queryClient.relatedVertexValue(v, RdfSchema.EUNIS_SPECIES, "binomialName"));
|
325
|
taxonName.setRank(queryClient.relatedVertexValue(v, RdfSchema.EUNIS_SPECIES, "taxonomicRank"));
|
326
|
return taxonName;
|
327
|
}
|
328
|
|
329
|
|
330
|
private void createSynonyms(Vertex taxonV, Response tnrResponse) {
|
331
|
|
332
|
|
333
|
GremlinPipeline<Graph, Vertex> taxonPipe = new GremlinPipeline<Graph, Vertex>(taxonV);
|
334
|
|
335
|
try {
|
336
|
List<Vertex> synonymVs = taxonPipe
|
337
|
.inE(RdfSchema.EUNIS_SPECIES.property("eunisPrimaryName")).outV().dedup()
|
338
|
.toList();
|
339
|
for(Vertex synonymV : synonymVs) {
|
340
|
// http://www.w3.org/1999/02/22-rdf-syntax-ns#type is used inconsistently, accepted taxa can have type SpeciesSynonym
|
341
|
// and are their own synonym in this case !
|
342
|
// using http://eunis.eea.europa.eu/rdf/species-schema.rdf#taxonomicRank is the recommended way to detect synonyms and to avoid
|
343
|
// adding the accepted taxon as its own synonym
|
344
|
String taxonomicRank = queryClient.relatedVertexValue(synonymV, RdfSchema.EUNIS_SPECIES, "taxonomicRank");
|
345
|
|
346
|
|
347
|
if (taxonomicRank != null && taxonomicRank.equals("Synonym")) {
|
348
|
|
349
|
Synonym synonym = new Synonym();
|
350
|
|
351
|
TaxonName taxonName = createTaxonName(synonymV);
|
352
|
synonym.setTaxonomicStatus(taxonomicRank);
|
353
|
synonym.setUrl(synonymV.getProperty(GraphSail.VALUE).toString());
|
354
|
synonym.setTaxonName(taxonName);
|
355
|
synonym.setAccordingTo(queryClient.relatedVertexValue(synonymV, RdfSchema.DWC, "nameAccordingToID"));
|
356
|
|
357
|
createSources(synonymV, synonym);
|
358
|
|
359
|
tnrResponse.getSynonym().add(synonym);
|
360
|
}
|
361
|
}
|
362
|
} catch (FastNoSuchElementException e) {
|
363
|
logger.debug("No sources found");
|
364
|
}
|
365
|
|
366
|
}
|
367
|
|
368
|
@Override
|
369
|
public void resolveScientificNamesExact(TnrMsg tnrMsg) throws DRFChecklistException {
|
370
|
|
371
|
for (ServiceProviderInfo checklistInfo : getServiceProviderInfo().getSubChecklists()) {
|
372
|
|
373
|
// FIXME query specific subchecklist
|
374
|
|
375
|
// selecting one request as representative, only
|
376
|
// the search mode and addSynonmy flag are important
|
377
|
// for the further usage of the request object
|
378
|
Query query = singleQueryFrom(tnrMsg);
|
379
|
|
380
|
String queryString = query.getRequest().getQueryString();
|
381
|
logger.debug("original queryString: "+ queryString);
|
382
|
queryString = QueryParser.escape(queryString);
|
383
|
queryString = queryString.replace(" ", "\\ ");
|
384
|
if(query.getRequest().getSearchMode().equals(SearchMode.scientificNameLike.name())) {
|
385
|
queryString += "*";
|
386
|
}
|
387
|
logger.debug("prepared queryString: "+ queryString);
|
388
|
|
389
|
GremlinPipeline<Graph, Vertex> pipe = null;
|
390
|
|
391
|
// Profiler profiler = Profiler.newCpuProfiler(false);
|
392
|
|
393
|
logger.debug("Neo4jINDEX");
|
394
|
|
395
|
ArrayList<Vertex> hitVs = queryClient.vertexIndexQuery("value:" + queryString);
|
396
|
pipe = new GremlinPipeline<Graph, Vertex>(hitVs);
|
397
|
|
398
|
List<Vertex> vertices = new ArrayList<Vertex>();
|
399
|
pipe.in(RdfSchema.EUNIS_SPECIES.property("binomialName"),
|
400
|
RdfSchema.DWC.property("subgenus"), // EUNIS has no subgenera but this is added for future compatibility
|
401
|
RdfSchema.DWC.property("genus")
|
402
|
// no taxa for higher ranks in EUNIS
|
403
|
).fill(vertices);
|
404
|
|
405
|
updateQueriesWithResponse(vertices, null, null, checklistInfo, query);
|
406
|
// profiler.end(System.err);
|
407
|
}
|
408
|
}
|
409
|
|
410
|
@Override
|
411
|
public void resolveScientificNamesLike(TnrMsg tnrMsg) throws DRFChecklistException {
|
412
|
// delegate to resolveScientificNamesExact,
|
413
|
resolveScientificNamesExact(tnrMsg);
|
414
|
|
415
|
}
|
416
|
|
417
|
@Override
|
418
|
public void resolveVernacularNamesExact(TnrMsg tnrMsg) throws DRFChecklistException {
|
419
|
List<Query> queryList = tnrMsg.getQuery();
|
420
|
|
421
|
for (ServiceProviderInfo checklistInfo : getServiceProviderInfo().getSubChecklists()) {
|
422
|
|
423
|
// FIXME query specific subchecklist
|
424
|
|
425
|
// selecting one request as representative, only
|
426
|
// the search mode and addSynonmy flag are important
|
427
|
// for the further usage of the request object
|
428
|
Query query = singleQueryFrom(tnrMsg);
|
429
|
|
430
|
String queryString = query.getRequest().getQueryString();
|
431
|
logger.debug("original queryString: "+ queryString);
|
432
|
queryString = QueryParser.escape(queryString);
|
433
|
queryString = queryString.replace(" ", "\\ ");
|
434
|
if(query.getRequest().getSearchMode().equals(SearchMode.vernacularNameLike.name())) {
|
435
|
queryString = "*" + queryString + "*";
|
436
|
}
|
437
|
|
438
|
logger.debug("prepared queryString: "+ queryString);
|
439
|
|
440
|
GremlinPipeline<Graph, Vertex> pipe = null;
|
441
|
|
442
|
Profiler profiler = Profiler.newCpuProfiler(false);
|
443
|
|
444
|
// by using the Neo4j index directly it is possible to
|
445
|
// take full advantage of the underlying Lucene search engine
|
446
|
ArrayList<Vertex> hitVs = queryClient.vertexIndexQuery("value:" + queryString);
|
447
|
|
448
|
// List<String> matchingNames = new ArrayList<String>(hitVs.size());
|
449
|
// for(Vertex v : hitVs) {
|
450
|
// String matchValue = v.getProperty(GraphSail.VALUE).toString();
|
451
|
// matchingNames.add(matchValue);
|
452
|
// logger.debug("matchingName " + matchValue);
|
453
|
// }
|
454
|
|
455
|
List<Vertex> vertices = new ArrayList<Vertex>();
|
456
|
pipe = new GremlinPipeline<Graph, Vertex>(hitVs);
|
457
|
Table table = new Table();
|
458
|
pipe.as("match").in(RdfSchema.DWC.property("vernacularName")).as("taxon").table(table).iterate();
|
459
|
|
460
|
updateQueriesWithResponse(
|
461
|
table.getColumn("taxon"), table.getColumn("match"),
|
462
|
NameType.VERNACULAR_NAME, checklistInfo, query);
|
463
|
profiler.end(System.err);
|
464
|
}
|
465
|
}
|
466
|
|
467
|
@Override
|
468
|
public void resolveVernacularNamesLike(TnrMsg tnrMsg) throws DRFChecklistException {
|
469
|
resolveVernacularNamesExact(tnrMsg);
|
470
|
}
|
471
|
|
472
|
@Override
|
473
|
public void findByIdentifier(TnrMsg tnrMsg) throws DRFChecklistException {
|
474
|
|
475
|
for (ServiceProviderInfo checklistInfo : getServiceProviderInfo().getSubChecklists()) {
|
476
|
|
477
|
// FIXME query specific subchecklist
|
478
|
Query query = singleQueryFrom(tnrMsg);
|
479
|
String queryString = query.getRequest().getQueryString();
|
480
|
|
481
|
// by using the Neo4j index directly it is possible to
|
482
|
// take full advantage of the underlying Lucene search engine
|
483
|
queryString = QueryParser.escape(queryString);
|
484
|
ArrayList<Vertex> hitVs = queryClient.vertexIndexQuery("value:" + queryString);
|
485
|
if(hitVs.size() > 0) {
|
486
|
Response response = tnrResponseFromResource(hitVs.get(0), query.getRequest(), null, null, checklistInfo);
|
487
|
query.getResponse().add(response);
|
488
|
} else if(hitVs.size() > 1) {
|
489
|
throw new DRFChecklistException("More than one node with the id '" + queryString + "' found");
|
490
|
}
|
491
|
}
|
492
|
}
|
493
|
|
494
|
private void updateQueriesWithResponse(List<Vertex> taxonNodes, List<Vertex> matchNodes, NameType matchType, ServiceProviderInfo ci, Query query){
|
495
|
|
496
|
if (taxonNodes == null) {
|
497
|
return;
|
498
|
}
|
499
|
|
500
|
logger.debug("matching taxon nodes:");
|
501
|
int i = -1;
|
502
|
for (Vertex v : taxonNodes) {
|
503
|
i++;
|
504
|
logger.debug(" " + v.toString());
|
505
|
if(logger.isTraceEnabled()) {
|
506
|
logger.trace("updateQueriesWithResponse() : printing propertyKeys to System.err");
|
507
|
printPropertyKeys(v, System.err);
|
508
|
}
|
509
|
if(v.getProperty("kind").equals("url")) {
|
510
|
logger.error("vertex of type 'url' expected, but was " + v.getProperty("type").equals("url"));
|
511
|
continue;
|
512
|
}
|
513
|
Vertex matchNode = null;
|
514
|
if(matchNodes != null) {
|
515
|
matchNode = matchNodes.get(i);
|
516
|
}
|
517
|
Response tnrResponse = tnrResponseFromResource(v, query.getRequest(), matchNode, matchType, ci);
|
518
|
if(tnrResponse != null) {
|
519
|
query.getResponse().add(tnrResponse);
|
520
|
}
|
521
|
}
|
522
|
}
|
523
|
|
524
|
/**
|
525
|
* @param model
|
526
|
* @param taxonR
|
527
|
* @param request
|
528
|
* @param matchType
|
529
|
* @param matchNode
|
530
|
* @param ci
|
531
|
* @return
|
532
|
*/
|
533
|
private Response tnrResponseFromResource(Vertex taxonV, Request request, Vertex matchNode, NameType matchType, ServiceProviderInfo ci) {
|
534
|
|
535
|
Response tnrResponse = TnrMsgUtils.tnrResponseFor(ci);
|
536
|
|
537
|
GremlinPipeline<Graph, Vertex> pipe = new GremlinPipeline<Graph, Vertex>(taxonV);
|
538
|
|
539
|
String validName = queryClient.relatedVertexValue(taxonV, RdfSchema.EUNIS_SPECIES, "validName");
|
540
|
|
541
|
boolean isAccepted = validName != null && validName.equals("true");
|
542
|
|
543
|
logger.debug("processing " + (isAccepted ? "accepted taxon" : "synonym or other") + " " + taxonV.getId());
|
544
|
|
545
|
//
|
546
|
if(matchNode != null) {
|
547
|
String matchingName = matchNode.getProperty(GraphSail.VALUE).toString();
|
548
|
tnrResponse.setMatchingNameString(matchingName);
|
549
|
tnrResponse.setMatchingNameType(matchType);
|
550
|
}
|
551
|
|
552
|
// case when accepted name
|
553
|
if(isAccepted) {
|
554
|
Taxon taxon = createTaxon(taxonV);
|
555
|
tnrResponse.setTaxon(taxon);
|
556
|
if(matchNode == null) {
|
557
|
tnrResponse.setMatchingNameType(NameType.TAXON);
|
558
|
String matchingName = taxon.getTaxonName().getCanonicalName();
|
559
|
tnrResponse.setMatchingNameString(matchingName);
|
560
|
}
|
561
|
|
562
|
}
|
563
|
else {
|
564
|
// case when synonym
|
565
|
Vertex synonymV = taxonV;
|
566
|
taxonV = null;
|
567
|
try {
|
568
|
taxonV = queryClient.relatedVertex(synonymV, RdfSchema.EUNIS_SPECIES, "eunisPrimaryName");
|
569
|
} catch(Exception e) {
|
570
|
logger.error("No accepted taxon found for " + synonymV.toString() + " (" + synonymV.getProperty(GraphSail.VALUE) + ")");
|
571
|
}
|
572
|
|
573
|
if(taxonV != null) {
|
574
|
Taxon taxon = createTaxon(taxonV);
|
575
|
tnrResponse.setTaxon(taxon);
|
576
|
} else {
|
577
|
}
|
578
|
if(matchNode == null) {
|
579
|
tnrResponse.setMatchingNameType(NameType.SYNONYM);
|
580
|
String matchingName = queryClient.relatedVertexValue(synonymV, RdfSchema.EUNIS_SPECIES, "binomialName");
|
581
|
tnrResponse.setMatchingNameString(matchingName);
|
582
|
}
|
583
|
}
|
584
|
|
585
|
if(request.isAddSynonymy()) {
|
586
|
createSynonyms(taxonV, tnrResponse);
|
587
|
}
|
588
|
|
589
|
logger.debug("processing " + (isAccepted ? "accepted taxon" : "synonym or other") + " " + taxonV.getId() + " DONE");
|
590
|
return tnrResponse;
|
591
|
}
|
592
|
|
593
|
/**
|
594
|
* @param vertex
|
595
|
*/
|
596
|
private void printEdges(Neo4j2Vertex vertex) {
|
597
|
Iterable<Relationship> rels = vertex.getRawVertex().getRelationships();
|
598
|
Iterator<Relationship> iterator = rels.iterator();
|
599
|
if(iterator.hasNext()) {
|
600
|
Relationship rel = iterator.next();
|
601
|
System.err.println(rel.toString() + ": " + rel.getStartNode().toString() + "-[" + rel.getType() + "]-" + rel.getEndNode().toString());
|
602
|
}
|
603
|
}
|
604
|
|
605
|
private void printPropertyKeys(Vertex v, PrintStream ps) {
|
606
|
StringBuilder out = new StringBuilder();
|
607
|
out.append(v.toString());
|
608
|
for(String key : v.getPropertyKeys()) {
|
609
|
out.append(key).append(": ").append(v.getProperty(key)).append(" ");
|
610
|
}
|
611
|
ps.println(out.toString());
|
612
|
}
|
613
|
|
614
|
@Override
|
615
|
public EnumSet<SearchMode> getSearchModes() {
|
616
|
return SEARCH_MODES;
|
617
|
}
|
618
|
|
619
|
@Override
|
620
|
public boolean isSupportedIdentifier(String value) {
|
621
|
return IdentifierUtils.checkURI(value);
|
622
|
}
|
623
|
|
624
|
|
625
|
}
|