Project

General

Profile

« Previous | Next » 

Revision 1a34bf3a

Added by Andreas Kohlbecker over 8 years ago

first workking implementation of the tinkerpop client for EEA_BDC

View differences:

src/main/java/org/bgbm/biovel/drf/checklist/EEA_BDC_Client.java
1 1
package org.bgbm.biovel.drf.checklist;
2 2

  
3
import java.io.PrintStream;
3 4
import java.net.URI;
5
import java.util.ArrayList;
6
import java.util.Collection;
4 7
import java.util.EnumSet;
8
import java.util.Iterator;
5 9
import java.util.List;
6
import java.util.NoSuchElementException;
7 10

  
8 11
import org.apache.jena.rdf.model.Model;
9
import org.apache.jena.rdf.model.Property;
10
import org.apache.jena.rdf.model.ResIterator;
11 12
import org.apache.jena.rdf.model.Resource;
12
import org.apache.jena.rdf.model.StmtIterator;
13 13
import org.bgbm.biovel.drf.client.ServiceProviderInfo;
14 14
import org.bgbm.biovel.drf.query.IQueryClient;
15 15
import org.bgbm.biovel.drf.query.SparqlClient;
16 16
import org.bgbm.biovel.drf.query.TinkerPopClient;
17 17
import org.bgbm.biovel.drf.store.Neo4jStore;
18
import org.bgbm.biovel.drf.store.Store;
18 19
import org.bgbm.biovel.drf.store.TDBStore;
19
import org.bgbm.biovel.drf.tnr.msg.Classification;
20 20
import org.bgbm.biovel.drf.tnr.msg.NameType;
21 21
import org.bgbm.biovel.drf.tnr.msg.Query;
22 22
import org.bgbm.biovel.drf.tnr.msg.Query.Request;
......
28 28
import org.bgbm.biovel.drf.tnr.msg.TaxonName;
29 29
import org.bgbm.biovel.drf.tnr.msg.TnrMsg;
30 30
import org.bgbm.biovel.drf.utils.IdentifierUtils;
31
import org.bgbm.biovel.drf.utils.Profiler;
31 32
import org.bgbm.biovel.drf.utils.TnrMsgUtils;
33
import org.neo4j.graphdb.Relationship;
34
import org.openrdf.query.MalformedQueryException;
35
import org.openrdf.query.QueryEvaluationException;
36
import org.openrdf.query.QueryLanguage;
37
import org.openrdf.query.TupleQuery;
38
import org.openrdf.query.TupleQueryResult;
39
import org.openrdf.repository.RepositoryException;
40
import org.openrdf.repository.sail.SailRepositoryConnection;
41

  
42
import com.tinkerpop.blueprints.CloseableIterable;
43
import com.tinkerpop.blueprints.Direction;
44
import com.tinkerpop.blueprints.Graph;
45
import com.tinkerpop.blueprints.Index;
46
import com.tinkerpop.blueprints.Vertex;
47
import com.tinkerpop.blueprints.impls.neo4j.Neo4jGraph;
48
import com.tinkerpop.blueprints.impls.neo4j.Neo4jVertex;
49
import com.tinkerpop.blueprints.oupls.sail.GraphSail;
50
import com.tinkerpop.gremlin.java.GremlinPipeline;
51
import com.tinkerpop.pipes.PipeFunction;
52
import com.tinkerpop.pipes.util.FastNoSuchElementException;
32 53

  
33 54
public class EEA_BDC_Client extends AggregateChecklistClient<TinkerPopClient> {
34 55

  
......
39 60
    public static final String LABEL = "European Environment Agency (EEA) Biodiversity data centre (BDC)";
40 61
    public static final String DOC_URL = "http://semantic.eea.europa.eu/documentation";
41 62
    public static final String COPYRIGHT_URL = "http://www.eea.europa.eu/legal/eea-data-policy";
63

  
42 64
    private static final String SPARQL_ENDPOINT_URL = "http://semantic.eea.europa.eu/sparql";
43
    private static final String RDF_FILE_URL = "http://localhost/download/species.rdf.gz"; // http://eunis.eea.europa.eu/rdf/species.rdf.gz
44 65
    private static final boolean USE_REMOTE_SERVICE = false;
66

  
67
    private static final String SPECIES_RDF_FILE_URL = "http://localhost/download/species.rdf.gz"; // http://eunis.eea.europa.eu/rdf/species.rdf.gz
68
    private static final String LEGALREFS_RDF_FILE_URL = "http://localhost/download/legalrefs.rdf.gz"; // http://eunis.eea.europa.eu/rdf/legalrefs.rdf.gz
69
    private static final String REFERENCES_RDF_FILE_URL = "http://localhost/download/references.rdf.gz"; // http://eunis.eea.europa.eu/rdf/references.rdf.gz
45 70
    private static final boolean REFRESH_TDB = false;
71

  
46 72
    private static final Class<? extends IQueryClient> clientClass = TinkerPopClient.class;
47 73

  
48 74
    private static final int MAX_PAGING_LIMIT = 50;
......
76 102
        DWC("dwc", "http://rs.tdwg.org/dwc/terms/"),
77 103
        RDF("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#"),
78 104
        RDFS("rdfs", "http://www.w3.org/2000/01/rdf-schema#"),
79
        SKOS_CORE("scos_core", "http://www.w3.org/2004/02/skos/core#");
105
        SKOS_CORE("scos_core", "http://www.w3.org/2004/02/skos/core#"),
106
        DC("dc", "http://purl.org/dc/terms/source"),
107
        DCTERMS("dcterms", "http://purl.org/dc/terms/");
80 108

  
81 109
        private String schemaUri;
82 110
        private String abbreviation;
......
95 123
            return abbreviation;
96 124
        }
97 125

  
126
        public String propertyURI(String name) {
127
            return schemaUri + name;
128
        }
129

  
98 130
    }
99 131

  
100 132
    public enum SubCheckListId {
......
132 164
                    throw new RuntimeException("Creation of TripleStore failed",  e1);
133 165
                }
134 166
                if(REFRESH_TDB) {
135
                    // use downloadable rdf
136
                    try {
137
                        tripleStore.loadIntoStore(RDF_FILE_URL);
138
                    } catch (Exception e) {
139
                        logger.error("Loading " + RDF_FILE_URL + " into TripleStore failed",  e);
140
                    }
167
                    updateStore(tripleStore);
141 168
                }
142 169
              //FIXME queryClient = new SparqlClient(tripleStore);
143 170

  
......
153 180
                    throw new RuntimeException("Creation of Neo4jStore failed",  e1);
154 181
                }
155 182
                if(REFRESH_TDB) {
156
                    // use downloadable rdf
157
                    try {
158
                        neo4jStore.loadIntoStore(RDF_FILE_URL);
159
                    } catch (Exception e) {
160
                        throw new RuntimeException("Loading " + RDF_FILE_URL + " into Neo4jStore failed",  e);
161
                    }
183
                    updateStore(neo4jStore);
162 184
                }
163 185
                queryClient = new TinkerPopClient(neo4jStore);
164 186

  
......
169 191
        }
170 192
    }
171 193

  
194
    /**
195
     * @param neo4jStore
196
     */
197
    private void updateStore(Store neo4jStore) {
198
        try {
199
            neo4jStore.loadIntoStore(
200
                    //SPECIES_RDF_FILE_URL,
201
                    LEGALREFS_RDF_FILE_URL,
202
                    REFERENCES_RDF_FILE_URL
203
                    );
204
        } catch (Exception e) {
205
            throw new RuntimeException("Loading "
206
                    + SPECIES_RDF_FILE_URL + ", "
207
                    + LEGALREFS_RDF_FILE_URL + ", "
208
                    + REFERENCES_RDF_FILE_URL +
209
                    " into Neo4jStore failed",  e);
210
        }
211
    }
212

  
172 213
    @Override
173 214
    public ServiceProviderInfo buildServiceProviderInfo() {
174 215

  
......
203 244
        return queryString;
204 245
    }
205 246

  
206
    private Taxon createTaxon(Model model, Resource taxonR) {
247
    private Taxon createTaxon(Vertex v) {
207 248

  
208 249
        Taxon taxon = new Taxon();
209 250

  
210
        TaxonName taxonName = createTaxonName(taxonR);
251
        TaxonName taxonName = createTaxonName(v);
211 252

  
212 253
        // Taxon
213 254
        taxon.setTaxonName(taxonName);
214
        taxon.setIdentifier(taxonR.getURI());
215
        taxon.setAccordingTo(queryClient.objectAsString(taxonR, RdfSchema.DWC, "nameAccordingToID"));
216
        URI typeUri = queryClient.objectAsURI(taxonR, RdfSchema.RDF, "type");
255
        taxon.setIdentifier(v.getId().toString());
256
        taxon.setAccordingTo(queryClient.relatedVertexValue(v, RdfSchema.DWC, "nameAccordingToID"));
257
        URI typeUri = queryClient.relatedVertexURI(v, RdfSchema.RDF, "type");
217 258
        taxon.setTaxonomicStatus(typeUri.getFragment());
218 259

  
219
        createSources(model, taxonR, taxon);
260
        createSources(v, taxon);
261

  
262
        /*
220 263

  
221 264
        // classification
222 265
        Classification c = null;
......
269 312
        if(c != null) {
270 313
            taxon.setClassification(c);
271 314
        }
315
        */
272 316
        return taxon;
273 317
    }
274 318

  
......
277 321
     * @param taxonR
278 322
     * @param taxonBase
279 323
     */
280
    private void createSources(Model model, Resource taxonR, TaxonBase taxonBase) {
324
    private void createSources(Vertex v, TaxonBase taxonBase) {
325

  
281 326
        // Sources are source references, re there others like data bases?
282
        for ( StmtIterator refIt = taxonR.listProperties(model.getProperty(RdfSchema.EUNIS_SPECIES.schemaUri, "hasLegalReference")); refIt.hasNext();) {
283
            try {
284
            Source source = new Source();
285
            Resource sourceR = refIt.next().getObject().asResource();
286
            String sourceName = queryClient.objectAsString(sourceR, RdfSchema.RDFS, "source");
287
            source.setName(sourceName);
288
            taxonBase.getSources().add(source);
289
            } catch (NoSuchElementException e) {
290
                logger.debug("No statements for rdf:hasLegalReference" , e);
327

  
328
        GremlinPipeline<Graph, Vertex> taxonPipe = new GremlinPipeline<Graph, Vertex>(v);
329

  
330
        try {
331
            List<Vertex> titleVs = taxonPipe
332
                    .outE(RdfSchema.EUNIS_SPECIES.propertyURI("hasLegalReference")).inV()
333
                    .outE(RdfSchema.DCTERMS.propertyURI("source")).inV().dedup()
334
                    .outE(RdfSchema.DCTERMS.propertyURI("title")).inV()
335
                    .toList();
336
            for(Vertex tv : titleVs) {
337
                Source source = new Source();
338
                logger.error(tv.toString());
339
                source.setName(tv.getProperty(GraphSail.VALUE).toString());
340
                taxonBase.getSources().add(source);
291 341
            }
342
        } catch (FastNoSuchElementException e) {
343
            logger.debug("No sources found");
292 344
        }
293 345
    }
294 346

  
......
296 348
     * @param taxonR
297 349
     * @return
298 350
     */
299
    private TaxonName createTaxonName(Resource taxonR) {
351
    private TaxonName createTaxonName(Vertex v) {
352

  
300 353
        TaxonName taxonName = new TaxonName();
301 354
        // TaxonName
302
        taxonName.setFullName(queryClient.objectAsString(taxonR, RdfSchema.RDFS, "label"));
355
        taxonName.setFullName(queryClient.relatedVertexValue(v, RdfSchema.RDFS, "label"));
303 356
        // TODO rename CanonicalName to scientificName? compare with dwc:scientificName
304
        taxonName.setCanonicalName(queryClient.objectAsString(taxonR, RdfSchema.EUNIS_SPECIES, "binomialName"));
305
        taxonName.setRank(queryClient.objectAsString(taxonR, RdfSchema.EUNIS_SPECIES, "taxonomicRank"));
357
        taxonName.setCanonicalName(queryClient.relatedVertexValue(v, RdfSchema.EUNIS_SPECIES, "binomialName"));
358
        taxonName.setRank(queryClient.relatedVertexValue(v, RdfSchema.EUNIS_SPECIES, "taxonomicRank"));
306 359
        return taxonName;
307 360
    }
308 361

  
309 362

  
363
    private void createSynonyms(Vertex taxonV, Response tnrResponse) {
310 364

  
311 365

  
312
    private void createSynonyms(Resource taxonR, Response tnrResponse) {
313

  
314
        List<Resource> synonymRList = queryForSynonyms(taxonR);
315

  
316
        for (Resource synonymR  : synonymRList) {
317

  
318
            URI typeUri = queryClient.objectAsURI(synonymR, RdfSchema.RDF, "type");
319
            String status = typeUri.getFragment();
366
        GremlinPipeline<Graph, Vertex> taxonPipe = new GremlinPipeline<Graph, Vertex>(taxonV);
320 367

  
368
        try {
369
            List<Vertex> synonymVs = taxonPipe
370
                    .inE(RdfSchema.EUNIS_SPECIES.propertyURI("eunisPrimaryName")).outV().dedup()
371
                    .toList();
372
            for(Vertex synonymV : synonymVs) {
373
                String typeUri = queryClient.relatedVertexValue(synonymV, RdfSchema.RDF, "type");
374
                String status = null;
375
                try {
376
                    status = URI.create(typeUri).getFragment();
377
                } catch (Exception e) {
321 378

  
322
            if (status != null && status.equals("SpeciesSynonym")) {
379
                }
323 380

  
324
                Synonym synonym = new Synonym();
381
                if (status != null && status.equals("SpeciesSynonym")) {
325 382

  
326
                TaxonName taxonName = createTaxonName(synonymR);
383
                    Synonym synonym = new Synonym();
327 384

  
328
                synonym.setTaxonomicStatus(status);
329
                synonym.setTaxonName(taxonName);
330
                synonym.setAccordingTo(queryClient.objectAsString(synonymR, RdfSchema.DWC, "nameAccordingToID"));
385
                    TaxonName taxonName = createTaxonName(synonymV);
386
                    synonym.setTaxonomicStatus(status);
387
                    synonym.setTaxonName(taxonName);
388
                    synonym.setAccordingTo(queryClient.relatedVertexValue(synonymV, RdfSchema.DWC, "nameAccordingToID"));
331 389

  
332
                createSources(synonymR.getModel(), synonymR, synonym);
390
                    createSources(synonymV, synonym);
333 391

  
334
                tnrResponse.getSynonym().add(synonym);
392
                    tnrResponse.getSynonym().add(synonym);
393
                }
335 394
            }
395
        } catch (FastNoSuchElementException e) {
396
            logger.debug("No sources found");
336 397
        }
398

  
337 399
    }
338 400

  
339 401
    /**
......
374 436
     */
375 437
    private List<Resource> listSynonymResources(Model model, Resource taxonR) {
376 438
        List<Resource> synonymRList;
439
        /*
377 440
        Property filterProperty = model.createProperty(RdfSchema.EUNIS_SPECIES.schemaUri, "eunisPrimaryName");
378 441
        synonymRList = queryClient.listResources(model, filterProperty, null, taxonR);
379 442
        return synonymRList;
443
         */
444
        return null;
380 445
    }
381 446

  
382 447
    @Override
......
391 456
        for (ServiceProviderInfo checklistInfo : getServiceProviderInfo().getSubChecklists()) {
392 457

  
393 458
            Query query = singleQueryFrom(tnrMsg);
394
            StringBuilder queryString = prepareQueryString();
459

  
460
            boolean  TUPLEQUERY = false;
461
            boolean Neo4jINDEX = true;
395 462

  
396 463
            String filter;
464
            String queryString = query.getRequest().getQueryString();
465
            logger.debug("queryString: "+ queryString);
466
            PipeFunction<Vertex, Boolean> matchFilter;
397 467
            if(query.getRequest().getSearchMode().equals(SearchMode.scientificNameLike.name())) {
398
                filter = "(regex(?name, \"^" + query.getRequest().getQueryString() + "\"))";
468
                filter = "(regex(?name, \"^" + queryString + "\"))";
469
                matchFilter = queryClient.createStarttWithFilter(queryString);
470
                queryString = queryString + "*";
399 471
            } else {
400
                filter = "(?name = \"" + query.getRequest().getQueryString() + "\")";
472
                filter = "(?name = \"" + queryString + "\")";
473
                matchFilter = queryClient.createEqualsFilter(queryString);
401 474
            }
402 475

  
403
            queryString.append(
404
                    "DESCRIBE ?eunisurl \n"
405
                    + "WHERE {\n"
406
                    + "     ?eunisurl es:binomialName ?name . \n"
407
                    + "     FILTER " + filter  + " \n"
408
                    + "} \n"
409
                    + "LIMIT " + MAX_PAGING_LIMIT + " OFFSET 0"
410
                    );
476
            if(TUPLEQUERY) {
477
                StringBuilder sparql = prepareQueryString();
478
                sparql.append(
479
                        "SELECT ?eunisurl \n"
480
                        + "WHERE {\n"
481
                        + "     ?eunisurl es:binomialName ?name . \n"
482
                        + "     FILTER " + filter  + " \n"
483
                        + "}"
484
                        );
485

  
486
                Neo4jGraph neo4jGraph = (Neo4jGraph)queryClient.graph();
487
                Vertex v = neo4jGraph.getVertex(2);
488

  
489
                SailRepositoryConnection connection = null;
490
                try {
491

  
492
                    Profiler profiler = Profiler.newCpuProfiler(true);
493

  
494
                    connection = queryClient.connection();
495
                    TupleQuery tquery = connection.prepareTupleQuery(QueryLanguage.SPARQL, sparql.toString());
496
                    TupleQueryResult tqresult = tquery.evaluate();
497
                    queryClient.showResults(tqresult);
498

  
499
                    profiler.end(System.err);
500

  
501
                } catch (MalformedQueryException | RepositoryException | QueryEvaluationException e1) {
502
                    // TODO Auto-generated catch block
503
                    e1.printStackTrace();
504
                } catch (Exception e1) {
505
                    // yourkit
506
                    e1.printStackTrace();
507
                } finally {
508
                    try {
509
                        connection.close();
510
                    } catch (RepositoryException e1) {
511
                        // IGNORE //
512
                    }
513
                    connection = null;
514
                }
515

  
516
            }
517
            GremlinPipeline<Graph, Vertex> pipe = null;
518

  
519
            if(Neo4jINDEX) {
520

  
521
                Profiler profiler = Profiler.newCpuProfiler(false);
522

  
523
                logger.debug("Neo4jINDEX");
524
                Collection<Vertex> resultSet = new ArrayList<Vertex>();
525

  
526
                // Theoretically it should be sufficient to use a Gremlin Pipe to find
527
                // the species nodes but gremlin is obviously not using the Neo4j index
528
                // to it is far too slow ...
529
                //
530
                //GremlinPipeline<Graph, Object> pipe2 = new GremlinPipeline<Graph, Object>(
531
                //        graph.getVertices("http://eunis.eea.europa.eu/rdf/species-schema.rdf#binomialName", queryString)
532
                //        );
533

  
534
                // ... therefore the Neo4j index is used directly
535
                Neo4jGraph graph = (Neo4jGraph)queryClient.graph();
536
                Index<Neo4jVertex> vertexAutoIndex = graph.getIndex("node_auto_index", Neo4jVertex.class);
537
                CloseableIterable<Neo4jVertex> nodes = vertexAutoIndex.query("value", "\"" + queryString + "\"");
538

  
539
                pipe = new GremlinPipeline<Graph, Vertex>(nodes);
540

  
541
                List<Vertex> vertices = new ArrayList<Vertex>();
542
                pipe.in("http://eunis.eea.europa.eu/rdf/species-schema.rdf#binomialName").fill(vertices);
543

  
544

  
545
                for(Vertex v : vertices) {
546
                    logger.debug("  " + v.toString());
547
                }
548

  
549
//                for (Iterator<Neo4jVertex> it = nodes.iterator(); it.hasNext();) {
550
//                    Vertex v = it.next();
551
//                    logger.debug("  " + v.toString());
552
//                    vertices.add(graph.getVertex(v.getId()));
553
//                }
554

  
555

  
556
                nodes.close();
557

  
558
                profiler.end(System.err);
559
                updateQueriesWithResponse(vertices, checklistInfo, query);
560
            }
411 561

  
412
            logger.debug("\n" + queryString.toString());
413
            /* FIXME
414
            Model model = queryClient.describe(queryString.toString());
415
            updateQueriesWithResponse(model, checklistInfo, query); */
416 562
        }
417 563
    }
418 564

  
......
426 572

  
427 573
    @Override
428 574
    public void resolveVernacularNamesExact(TnrMsg tnrMsg) throws DRFChecklistException {
575
        /*
429 576
        List<Query> queryList = tnrMsg.getQuery();
430 577

  
431 578
        // selecting one request as representative, only
......
455 602
                    );
456 603

  
457 604
            logger.debug("\n" + queryString.toString());
605
         */
458 606
 /* FIXME
459 607
            Model model = queryClient.describe(queryString.toString());
460 608
            updateQueriesWithResponse(model, checklistInfo, query); */
461
        }
462

  
609
//        }
463 610
    }
464 611

  
465 612
    @Override
......
469 616

  
470 617
    @Override
471 618
    public void findByIdentifier(TnrMsg tnrMsg) throws DRFChecklistException {
472
        for (ServiceProviderInfo checklistInfo : getServiceProviderInfo().getSubChecklists()) {
619
        /*for (ServiceProviderInfo checklistInfo : getServiceProviderInfo().getSubChecklists()) {
473 620

  
474 621
            Query query = singleQueryFrom(tnrMsg);
475 622
            Resource taxonR = queryClient.getFromUri(query.getRequest().getQueryString());
......
477 624
            Response response = tnrResponseFromResource(taxonR.getModel(), taxonR, query.getRequest());
478 625
            query.getResponse().add(response);
479 626
        }
627
        */
480 628
    }
481 629

  
482
    private void updateQueriesWithResponse(Model model, ServiceProviderInfo ci, Query query)
483
            throws DRFChecklistException {
630
    private void updateQueriesWithResponse(List<Vertex> nodes, ServiceProviderInfo ci, Query query){
484 631

  
485
        if (model == null) {
632
        if (nodes == null) {
486 633
            return;
487 634
        }
488 635

  
489
        ResIterator subjectIt = model.listSubjects();
490

  
491
        while (subjectIt.hasNext()) {
492
            Resource subject = subjectIt.next();
493
            Resource taxonR;
494
            StmtIterator exactMatches = subject.listProperties(subject.getModel().getProperty(RdfSchema.SKOS_CORE.schemaUri, "exactMatch"));
495
            if(exactMatches.hasNext()) {
496
                // need to follow the exactMatch uri in this case
497
                taxonR = queryClient.getFromUri(exactMatches.next().getResource().getURI());
498
            } else {
499
                // the subject is already a species
500
                taxonR = subject;
636
        logger.debug("matching nodes:");
637
        for (Vertex v : nodes) {
638
            logger.debug("  " + v.toString());
639
            printPropertyKeys(v, System.err);
640
            if(v.getProperty("kind").equals("url")) {
641
                logger.error("vertex of type 'url' expected, but was " + v.getProperty("type").equals("url"));
642
                continue;
501 643
            }
502

  
503
            Response tnrResponse = tnrResponseFromResource(model, taxonR, query.getRequest());
644
            Response tnrResponse = tnrResponseFromResource(v, query.getRequest());
504 645
            if(tnrResponse != null) {
505 646
                query.getResponse().add(tnrResponse);
506 647
            }
......
513 654
     * @param request
514 655
     * @return
515 656
     */
516
    private Response tnrResponseFromResource(Model model, Resource taxonR, Request request) {
657
    @SuppressWarnings("unused")
658
    private Response tnrResponseFromResource(Vertex taxonV, Request request) {
517 659

  
518 660
        Response tnrResponse = TnrMsgUtils.tnrResponseFor(getServiceProviderInfo());
519 661

  
520 662
        SearchMode searchMode = SearchMode.valueOf(request.getSearchMode());
521 663

  
522
        // Check for type to ignore the triple pointing from synonyms to accepted taxonUris
523
        // only complete descriptions of taxa and synonym are relevant.
524
        boolean isCompleteResource = taxonR.hasProperty(taxonR.getModel().getProperty(RdfSchema.RDF.schemaUri, "type"));
525
        if(!isCompleteResource) {
526
            return null;
527
        }
664
        GremlinPipeline<Graph, Vertex> pipe = new GremlinPipeline<Graph, Vertex>(taxonV);
665

  
666
        String validName = queryClient.relatedVertexValue(taxonV, RdfSchema.EUNIS_SPECIES, "validName");
528 667

  
529
        String validName = queryClient.objectAsString(taxonR, RdfSchema.EUNIS_SPECIES, "validName");
530
        boolean isAccepted = validName != null && validName.equals("true^^http://www.w3.org/2001/XMLSchema#boolean");
668
        boolean isAccepted = validName != null && validName.equals("true");
531 669
        boolean skipThis = false;
532 670

  
533
        logger.debug("processing " + (isAccepted ? "accepted taxon" : "synonym or other")  + " " + taxonR.getURI());
671
        logger.debug("processing " + (isAccepted ? "accepted taxon" : "synonym or other")  + " " + taxonV.getId());
534 672

  
535 673
        // case when accepted name
536 674
        if(isAccepted) {
537
            Taxon taxon = createTaxon(model, taxonR);
675
            Taxon taxon = createTaxon(taxonV);
538 676
            tnrResponse.setTaxon(taxon);
539 677
            tnrResponse.setMatchingNameType(NameType.TAXON);
540 678
            String matchingName = taxon.getTaxonName().getCanonicalName();
541 679
            tnrResponse.setMatchingNameString(matchingName);
542 680

  
543
        } else {
681
        }
682
        else {
544 683
            // case when synonym
545
            Resource synonymR = taxonR;
546
            URI taxonUri = queryClient.objectAsURI(taxonR, RdfSchema.EUNIS_SPECIES, "eunisPrimaryName");
547
            if(taxonUri == null) {
548
                logger.error("no taxon uri found");
684
            Vertex synonymV = taxonV;
685
            taxonV = null;
686
            try {
687
                taxonV = synonymV.getEdges(Direction.OUT, RdfSchema.EUNIS_SPECIES.propertyURI("eunisPrimaryName")).iterator().next().getVertex(Direction.IN);
688
            } catch(Exception e) {
689
                logger.error("No accepted taxon found for " + synonymV.toString() + " (" + synonymV.getProperty(GraphSail.VALUE) + ")");
549 690
            }
550 691

  
551
            taxonR = queryClient.getFromUri(taxonUri);
552
            if(taxonR != null) {
553
                Taxon taxon = createTaxon(model, taxonR);
692
            if(taxonV != null) {
693
                Taxon taxon = createTaxon(taxonV);
554 694
                tnrResponse.setTaxon(taxon);
555 695
            } else {
556
                logger.error("No accepted taxon found for " + synonymR.getURI());
557 696
            }
558 697
            tnrResponse.setMatchingNameType(NameType.SYNONYM);
559
            String matchingName = queryClient.objectAsString(synonymR, RdfSchema.EUNIS_SPECIES, "binomialName");
698
            String matchingName = queryClient.relatedVertexValue(synonymV, RdfSchema.EUNIS_SPECIES, "binomialName");
560 699
            tnrResponse.setMatchingNameString(matchingName);
561 700
        }
562 701

  
563 702
        if(!skipThis && request.isAddSynonymy()) {
564
            createSynonyms(taxonR, tnrResponse);
703
            createSynonyms(taxonV, tnrResponse);
565 704
        }
566
        logger.debug("processing " + (isAccepted ? "accepted taxon" : "synonym or other")  + " " + taxonR.getURI() + " DONE");
567 705

  
706
        logger.debug("processing " + (isAccepted ? "accepted taxon" : "synonym or other")  + " " + taxonV.getId() + " DONE");
568 707
        return tnrResponse;
569 708
    }
570 709

  
710
    /**
711
     * @param vertex
712
     */
713
    private void printEdges(Neo4jVertex vertex) {
714
        Iterable<Relationship> rels = vertex.getRawVertex().getRelationships();
715
        Iterator<Relationship> iterator = rels.iterator();
716
        if(iterator.hasNext()) {
717
            Relationship rel = iterator.next();
718
            System.err.println(rel.toString() + ": " + rel.getStartNode().toString() + "-[" +  rel.getType() + "]-" + rel.getEndNode().toString());
719
        }
720
    }
721

  
722
    private void printPropertyKeys(Vertex v, PrintStream ps) {
723
        StringBuilder out = new StringBuilder();
724
        out.append(v.toString());
725
        for(String key : v.getPropertyKeys()) {
726
            out.append(key).append(": ").append(v.getProperty(key)).append(" ");
727
        }
728
        ps.println(out.toString());
729
    }
730

  
571 731
    @Override
572 732
    public EnumSet<SearchMode> getSearchModes() {
573 733
        return SEARCH_MODES;

Also available in: Unified diff