Project

General

Profile

« Previous | Next » 

Revision 7413db32

Added by Andreas Kohlbecker over 8 years ago

implementing MatchingNameString and MatchingNameType

View differences:

src/main/java/org/bgbm/biovel/drf/checklist/EEA_BDC_Client.java
31 31
import org.bgbm.biovel.drf.utils.Profiler;
32 32
import org.bgbm.biovel.drf.utils.TnrMsgUtils;
33 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 34

  
42 35
import com.tinkerpop.blueprints.Direction;
43 36
import com.tinkerpop.blueprints.Graph;
44 37
import com.tinkerpop.blueprints.Vertex;
45
import com.tinkerpop.blueprints.impls.neo4j2.Neo4j2Graph;
46 38
import com.tinkerpop.blueprints.impls.neo4j2.Neo4j2Vertex;
47 39
import com.tinkerpop.blueprints.oupls.sail.GraphSail;
48 40
import com.tinkerpop.gremlin.java.GremlinPipeline;
49
import com.tinkerpop.pipes.PipeFunction;
50 41
import com.tinkerpop.pipes.util.FastNoSuchElementException;
42
import com.tinkerpop.pipes.util.structures.Table;
51 43

  
52 44
public class EEA_BDC_Client extends AggregateChecklistClient<TinkerPopClient> {
53 45

  
......
455 447

  
456 448
            Query query = singleQueryFrom(tnrMsg);
457 449

  
458
            boolean  TUPLEQUERY = false;
459
            boolean Neo4jINDEX = true;
460

  
461
            String filter;
462 450
            String queryString = query.getRequest().getQueryString();
451
            logger.debug("original queryString: "+ queryString);
463 452
            queryString = QueryParser.escape(queryString);
464 453
            queryString = queryString.replace(" ", "\\ ");
465
            logger.debug("original queryString: "+ queryString);
466

  
467
            PipeFunction<Vertex, Boolean> matchFilter;
468 454
            if(query.getRequest().getSearchMode().equals(SearchMode.scientificNameLike.name())) {
469
                filter = "(regex(?name, \"^" + queryString + "\"))";
470
                matchFilter = queryClient.createStarttWithFilter(queryString);
471
                // need to escape white space and add wildcard to the end
472 455
                queryString += "*";
473
            } else {
474
                filter = "(?name = \"" + queryString + "\")";
475
                matchFilter = queryClient.createEqualsFilter(queryString);
476 456
            }
477

  
478 457
            logger.debug("prepared queryString: "+ queryString);
479 458

  
480
            if(TUPLEQUERY) {
481
                StringBuilder sparql = prepareQueryString();
482
                sparql.append(
483
                        "SELECT ?eunisurl \n"
484
                        + "WHERE {\n"
485
                        + "     ?eunisurl es:binomialName ?name . \n"
486
                        + "     FILTER " + filter  + " \n"
487
                        + "}"
488
                        );
489

  
490
                Neo4j2Graph neo4jGraph = (Neo4j2Graph)queryClient.graph();
491
                Vertex v = neo4jGraph.getVertex(2);
492

  
493
                SailRepositoryConnection connection = null;
494
                try {
495

  
496
                    Profiler profiler = Profiler.newCpuProfiler(true);
497

  
498
                    connection = queryClient.connection();
499
                    TupleQuery tquery = connection.prepareTupleQuery(QueryLanguage.SPARQL, sparql.toString());
500
                    TupleQueryResult tqresult = tquery.evaluate();
501
                    queryClient.showResults(tqresult);
502

  
503
                    profiler.end(System.err);
504

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

  
520
            }
521 459
            GremlinPipeline<Graph, Vertex> pipe = null;
522 460

  
523
            if(Neo4jINDEX) {
524

  
525
                Profiler profiler = Profiler.newCpuProfiler(false);
461
            Profiler profiler = Profiler.newCpuProfiler(false);
526 462

  
527
                logger.debug("Neo4jINDEX");
463
            logger.debug("Neo4jINDEX");
528 464

  
529
                ArrayList<Vertex> hitVs = queryClient.vertexIndexQuery("value:" + queryString);
530
                pipe = new GremlinPipeline<Graph, Vertex>(hitVs);
465
            ArrayList<Vertex> hitVs = queryClient.vertexIndexQuery("value:" + queryString);
466
            pipe = new GremlinPipeline<Graph, Vertex>(hitVs);
531 467

  
532
                List<Vertex> vertices = new ArrayList<Vertex>();
533
                pipe.in(RdfSchema.EUNIS_SPECIES.propertyURI("binomialName")).fill(vertices);
468
            List<Vertex> vertices = new ArrayList<Vertex>();
469
            pipe.in(RdfSchema.EUNIS_SPECIES.propertyURI("binomialName")).fill(vertices);
534 470

  
535
                updateQueriesWithResponse(vertices, checklistInfo, query);
536
                profiler.end(System.err);
537
            }
471
            updateQueriesWithResponse(vertices, null, null, checklistInfo, query);
472
            profiler.end(System.err);
538 473
        }
539 474
    }
540 475

  
......
557 492
            Query query = singleQueryFrom(tnrMsg);
558 493

  
559 494
            String queryString = query.getRequest().getQueryString();
495
            logger.debug("original queryString: "+ queryString);
560 496
            queryString = QueryParser.escape(queryString);
561 497
            queryString = queryString.replace(" ", "\\ ");
562
            logger.debug("original queryString: "+ queryString);
563 498
            if(query.getRequest().getSearchMode().equals(SearchMode.vernacularNameLike.name())) {
564 499
                queryString = "*" + queryString + "*";
565 500
            }
......
583 518

  
584 519
            List<Vertex> vertices = new ArrayList<Vertex>();
585 520
            pipe = new GremlinPipeline<Graph, Vertex>(hitVs);
586
            pipe.in(RdfSchema.DWC.propertyURI("vernacularName")).fill(vertices);
521
            Table table = new Table();
522
            pipe.as("match").in(RdfSchema.DWC.propertyURI("vernacularName")).as("taxon").table(table).iterate();
587 523

  
588
            updateQueriesWithResponse(vertices, checklistInfo, query);
524
            updateQueriesWithResponse(
525
                    table.getColumn("taxon"), table.getColumn("match"),
526
                    NameType.VERNACULAR_NAME, checklistInfo, query);
589 527
            profiler.end(System.err);
590 528
        }
591 529
    }
......
609 547
            queryString = QueryParser.escape(queryString);
610 548
            ArrayList<Vertex> hitVs = queryClient.vertexIndexQuery("value:" + queryString);
611 549
            if(hitVs.size() > 0) {
612
                Response response = tnrResponseFromResource(hitVs.get(0), query.getRequest());
550
                Response response = tnrResponseFromResource(hitVs.get(0), query.getRequest(), null, null);
613 551
                query.getResponse().add(response);
614 552
            } else if(hitVs.size() > 1) {
615 553
                throw new DRFChecklistException("More than one node with the id '" + queryString + "' found");
......
617 555
        }
618 556
    }
619 557

  
620
    private void updateQueriesWithResponse(List<Vertex> nodes, ServiceProviderInfo ci, Query query){
558
    private void updateQueriesWithResponse(List<Vertex> taxonNodes, List<Vertex> matchNodes, NameType matchType, ServiceProviderInfo ci, Query query){
621 559

  
622
        if (nodes == null) {
560
        if (taxonNodes == null) {
623 561
            return;
624 562
        }
625 563

  
626 564
        logger.debug("matching taxon nodes:");
627
        for (Vertex v : nodes) {
565
        int i = -1;
566
        for (Vertex v : taxonNodes) {
567
            i++;
628 568
            logger.debug("  " + v.toString());
629 569
            printPropertyKeys(v, System.err);
630 570
            if(v.getProperty("kind").equals("url")) {
631 571
                logger.error("vertex of type 'url' expected, but was " + v.getProperty("type").equals("url"));
632 572
                continue;
633 573
            }
634
            Response tnrResponse = tnrResponseFromResource(v, query.getRequest());
574
            Vertex matchNode = null;
575
            if(matchNodes != null) {
576
                matchNode = matchNodes.get(i);
577
            }
578
            Response tnrResponse = tnrResponseFromResource(v, query.getRequest(), matchNode, matchType);
635 579
            if(tnrResponse != null) {
636 580
                query.getResponse().add(tnrResponse);
637 581
            }
......
642 586
     * @param model
643 587
     * @param taxonR
644 588
     * @param request
589
     * @param matchType
590
     * @param matchNode
645 591
     * @return
646 592
     */
647 593
    @SuppressWarnings("unused")
648
    private Response tnrResponseFromResource(Vertex taxonV, Request request) {
594
    private Response tnrResponseFromResource(Vertex taxonV, Request request, Vertex matchNode, NameType matchType) {
649 595

  
650 596
        Response tnrResponse = TnrMsgUtils.tnrResponseFor(getServiceProviderInfo());
651 597

  
......
656 602
        String validName = queryClient.relatedVertexValue(taxonV, RdfSchema.EUNIS_SPECIES, "validName");
657 603

  
658 604
        boolean isAccepted = validName != null && validName.equals("true");
659
        boolean skipThis = false;
660 605

  
661 606
        logger.debug("processing " + (isAccepted ? "accepted taxon" : "synonym or other")  + " " + taxonV.getId());
662 607

  
608
        //
609
        if(matchNode != null) {
610
            String matchingName = matchNode.getProperty(GraphSail.VALUE).toString();
611
            tnrResponse.setMatchingNameString(matchingName);
612
            tnrResponse.setMatchingNameType(matchType);
613
        }
614

  
663 615
        // case when accepted name
664 616
        if(isAccepted) {
665 617
            Taxon taxon = createTaxon(taxonV);
666 618
            tnrResponse.setTaxon(taxon);
667
            tnrResponse.setMatchingNameType(NameType.TAXON);
668
            String matchingName = taxon.getTaxonName().getCanonicalName();
669
            tnrResponse.setMatchingNameString(matchingName);
619
            if(matchNode == null) {
620
                tnrResponse.setMatchingNameType(NameType.TAXON);
621
                String matchingName = taxon.getTaxonName().getCanonicalName();
622
                tnrResponse.setMatchingNameString(matchingName);
623
            }
670 624

  
671 625
        }
672 626
        else {
......
684 638
                tnrResponse.setTaxon(taxon);
685 639
            } else {
686 640
            }
687
            tnrResponse.setMatchingNameType(NameType.SYNONYM);
688
            String matchingName = queryClient.relatedVertexValue(synonymV, RdfSchema.EUNIS_SPECIES, "binomialName");
689
            tnrResponse.setMatchingNameString(matchingName);
641
            if(matchNode == null) {
642
                tnrResponse.setMatchingNameType(NameType.SYNONYM);
643
                String matchingName = queryClient.relatedVertexValue(synonymV, RdfSchema.EUNIS_SPECIES, "binomialName");
644
                tnrResponse.setMatchingNameString(matchingName);
645
            }
690 646
        }
691 647

  
692
        if(!skipThis && request.isAddSynonymy()) {
648
        if(request.isAddSynonymy()) {
693 649
            createSynonyms(taxonV, tnrResponse);
694 650
        }
695 651

  
src/test/java/org/bgbm/biovel/drf/checklist/EEA_BDC_ClientTest.java
3 3
import static org.junit.Assert.assertEquals;
4 4
import static org.junit.Assert.assertTrue;
5 5

  
6
import org.bgbm.biovel.drf.tnr.msg.NameType;
6 7
import org.bgbm.biovel.drf.tnr.msg.Response;
7 8
import org.bgbm.biovel.drf.tnr.msg.TnrMsg;
8 9
import org.bgbm.biovel.drf.utils.TnrMsgException;
......
97 98
        String outputXML = TnrMsgUtils.convertTnrMsgToXML(tnrMsg);
98 99
        System.out.println(outputXML);
99 100
        assertTrue(tnrMsg.getQuery().get(0).getResponse().size() > 0);
100
        tnrMsg.getQuery().get(0).getResponse().get(0).getTaxon().getTaxonName().getCanonicalName().equals("Cichorium intybus");
101
        Response response = tnrMsg.getQuery().get(0).getResponse().get(0);
102
        assertEquals("Cichorium intybus", response.getTaxon().getTaxonName().getCanonicalName());
103
        assertEquals("Wegwarte", response.getMatchingNameString());
104
        assertEquals(NameType.VERNACULAR_NAME, response.getMatchingNameType());
101 105
    }
102 106

  
103 107
    @Test

Also available in: Unified diff