Search in sources :

Example 36 with InteractionListener

use of org.eol.globi.process.InteractionListener in project eol-globi-data by jhpoelen.

the class DatasetImporterForRSSTest method enrichingInteractionListenerSourceOccurrence.

@Test()
public void enrichingInteractionListenerSourceOccurrence() throws StudyImporterException {
    DatasetImporterWithListener studyImporter = new DatasetImporterWithListener(new ParserFactory() {

        @Override
        public LabeledCSVParser createParser(URI studyResource, String characterEncoding) throws IOException {
            return null;
        }
    }, new NodeFactoryNull()) {

        @Override
        public void importStudy() throws StudyImporterException {
        // 
        }
    };
    final List<Map<String, String>> receivedLinks = new ArrayList<>();
    studyImporter.setInteractionListener(new InteractionListener() {

        @Override
        public void on(Map<String, String> interaction) throws StudyImporterException {
            receivedLinks.add(interaction);
        }
    });
    TreeMap<Pair<String, String>, Map<String, String>> interactionsWithUnresolvedOccurrenceIds = new TreeMap<Pair<String, String>, Map<String, String>>() {

        {
            put(Pair.of(DatasetImporterForTSV.TARGET_OCCURRENCE_ID, "1234"), new TreeMap<String, String>() {

                {
                    put(DatasetImporterForTSV.TARGET_OCCURRENCE_ID, "1234");
                    put(DatasetImporterForTSV.TARGET_LIFE_STAGE_NAME, "lifeStageName");
                    put(DatasetImporterForTSV.TARGET_LIFE_STAGE_ID, "lifeStageId");
                    put(DatasetImporterForTSV.TARGET_BODY_PART_NAME, "bodyPartName");
                    put(DatasetImporterForTSV.TARGET_BODY_PART_ID, "bodyPartId");
                    put(TaxonUtil.TARGET_TAXON_NAME, "taxonName");
                    put(TaxonUtil.TARGET_TAXON_ID, "taxonId");
                }
            });
        }
    };
    InteractionListenerResolving listener = new InteractionListenerResolving(interactionsWithUnresolvedOccurrenceIds, studyImporter.getInteractionListener());
    listener.on(new TreeMap<String, String>() {

        {
            put(DatasetImporterForTSV.SOURCE_OCCURRENCE_ID, "4567");
            put(DatasetImporterForTSV.TARGET_OCCURRENCE_ID, "1234");
        }
    });
    assertThat(receivedLinks.size(), is(1));
    Map<String, String> received = receivedLinks.get(0);
    assertThat(received.get(DatasetImporterForTSV.SOURCE_OCCURRENCE_ID), is("4567"));
    assertThat(received.get(TaxonUtil.TARGET_TAXON_NAME), is("taxonName"));
    assertThat(received.get(TaxonUtil.TARGET_TAXON_ID), is("taxonId"));
    assertThat(received.get(DatasetImporterForTSV.TARGET_OCCURRENCE_ID), is("1234"));
    assertThat(received.get(DatasetImporterForTSV.TARGET_BODY_PART_NAME), is("bodyPartName"));
    assertThat(received.get(DatasetImporterForTSV.TARGET_BODY_PART_ID), is("bodyPartId"));
    assertThat(received.get(DatasetImporterForTSV.TARGET_LIFE_STAGE_NAME), is("lifeStageName"));
    assertThat(received.get(DatasetImporterForTSV.TARGET_LIFE_STAGE_ID), is("lifeStageId"));
}
Also used : ArrayList(java.util.ArrayList) Matchers.containsString(org.hamcrest.Matchers.containsString) IOException(java.io.IOException) LabeledCSVParser(com.Ostermiller.util.LabeledCSVParser) TreeMap(java.util.TreeMap) URI(java.net.URI) InteractionListener(org.eol.globi.process.InteractionListener) InteractionListenerResolving(org.eol.globi.util.InteractionListenerResolving) Map(java.util.Map) TreeMap(java.util.TreeMap) Pair(org.apache.commons.lang3.tuple.Pair) Test(org.junit.Test)

Example 37 with InteractionListener

use of org.eol.globi.process.InteractionListener in project eol-globi-data by jhpoelen.

the class DatasetImporterForBatBaseTest method parseInteraction.

public List<Map<String, String>> parseInteraction(Map<String, Taxon> taxa, DatasetImporterForBatBase.Version version) throws IOException, StudyImporterException {
    Map<String, String> sources = new TreeMap<>();
    sources.put("955", "some reference");
    Map<String, Map<String, String>> locations = new TreeMap<String, Map<String, String>>() {

        {
            put("30", new TreeMap<String, String>() {

                {
                    put(DatasetImporterForTSV.LOCALITY_ID, "foo:locality:id:30");
                }
            });
        }
    };
    String interactionJson = "{\"interaction\": {" + "    \"1\": \"{\\\"id\\\":1," + "\\\"source\\\":955," + "\\\"interactionType\\\":{\\\"id\\\":11,\\\"displayName\\\":\\\"Predation\\\"}," + "\\\"location\\\":30," + "\\\"subject\\\":974," + "\\\"object\\\":885," + "\\\"tags\\\":[{\\\"id\\\":6,\\\"displayName\\\":\\\"Secondary\\\"}]," + "\\\"updatedBy\\\":\\\"Sarah\\\"," + "\\\"serverUpdatedAt\\\":\\\"2020-04-28T15:44:41-05:00\\\"" + "}\"" + "}}";
    List<Map<String, String>> links = new ArrayList<>();
    InteractionListener testListener = links::add;
    DatasetImporterForBatPlant.parseInteractions(taxa, sources, interactionJson, testListener, locations, getFooPrefixer(), version);
    return links;
}
Also used : InteractionListener(org.eol.globi.process.InteractionListener) ArrayList(java.util.ArrayList) TreeMap(java.util.TreeMap) TreeMap(java.util.TreeMap) Map(java.util.Map)

Example 38 with InteractionListener

use of org.eol.globi.process.InteractionListener in project eol-globi-data by jhpoelen.

the class DatasetImporterForBatBaseIT method importAll.

@Test
public void importAll() throws StudyImporterException {
    AtomicInteger counter = new AtomicInteger(0);
    DatasetImporterForBatBase importer = new DatasetImporterForBatBase(null, null);
    DatasetImpl dataset = new DatasetImpl("test/batplant", URI.create("classpath:/org/eol/globi/data/batplant/"), is -> is);
    importer.setDataset(dataset);
    importer.setInteractionListener(new InteractionValidator(new InteractionListener() {

        @Override
        public void on(Map<String, String> interaction) throws StudyImporterException {
            counter.incrementAndGet();
        }
    }, new NullImportLogger() {

        @Override
        public void warn(LogContext ctx, String message) {
            fail("unexpected warning: [" + message + "]");
        }
    }));
    importer.importStudy();
    assertThat(counter.get() > 0, Is.is(true));
}
Also used : NullImportLogger(org.eol.globi.tool.NullImportLogger) InteractionListener(org.eol.globi.process.InteractionListener) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) InteractionValidator(org.eol.globi.process.InteractionValidator) LogContext(org.eol.globi.domain.LogContext) DatasetImpl(org.globalbioticinteractions.dataset.DatasetImpl) Map(java.util.Map) Test(org.junit.Test)

Example 39 with InteractionListener

use of org.eol.globi.process.InteractionListener in project eol-globi-data by jhpoelen.

the class DatasetImporterForSzoboszlaiTest method importLines.

@Test
public void importLines() throws IOException, StudyImporterException {
    DatasetImporterForSzoboszlai studyImporterForSzoboszlai = new DatasetImporterForSzoboszlai(new ParserFactoryLocal(), nodeFactory);
    studyImporterForSzoboszlai.setDataset(getTestDataset());
    final List<Map<String, String>> maps = new ArrayList<Map<String, String>>();
    TreeMap<Integer, LatLng> localeMap = new TreeMap<Integer, LatLng>();
    localeMap.put(2361, new LatLng(34.00824202376044, -120.72716166720323));
    studyImporterForSzoboszlai.importLinks(IOUtils.toInputStream(firstFewLines(), StandardCharsets.UTF_8), new InteractionListener() {

        @Override
        public void on(Map<String, String> interaction) {
            maps.add(interaction);
        }
    }, localeMap);
    assertThat(maps.size(), is(4));
    Map<String, String> firstLink = maps.get(0);
    assertThat(firstLink.get(TaxonUtil.SOURCE_TAXON_ID), is(nullValue()));
    assertThat(firstLink.get(TaxonUtil.SOURCE_TAXON_NAME), is("Thunnus thynnus"));
    assertThat(firstLink.get(TaxonUtil.TARGET_TAXON_ID), is("ITIS:161828"));
    assertThat(firstLink.get(TaxonUtil.TARGET_TAXON_NAME), is("Engraulis mordax"));
    assertThat(firstLink.get(DATASET_CITATION), containsString("Szoboszlai AI, Thayer JA, Wood SA, Sydeman WJ, Koehn LE (2015) Data from: Forage species in predator diets: synthesis of data from the California Current. Dryad Digital Repository. https://doi.org/10.5061/dryad.nv5d2"));
    assertThat(firstLink.get(DATASET_CITATION), containsString("Accessed at"));
    assertThat(firstLink.get(REFERENCE_CITATION), is("Blunt, CE. 1958. California bluefin tuna-wary wanderer of the Pacific. Outdoor California. v.19. pp.14"));
    assertThat(firstLink.get(REFERENCE_DOI), is(nullValue()));
    assertThat(firstLink.get(REFERENCE_URL), is(nullValue()));
    assertThat(firstLink.get(LOCALITY_NAME), is("USA, CA"));
    assertThat(firstLink.get(DECIMAL_LONGITUDE), is("-120.72716166720323"));
    assertThat(firstLink.get(DECIMAL_LATITUDE), is("34.00824202376044"));
    assertThat(firstLink.get(INTERACTION_TYPE_ID), is("RO:0002439"));
    assertThat(firstLink.get(INTERACTION_TYPE_NAME), is("preysOn"));
}
Also used : ArrayList(java.util.ArrayList) Matchers.containsString(org.hamcrest.Matchers.containsString) TreeMap(java.util.TreeMap) InteractionListener(org.eol.globi.process.InteractionListener) LatLng(org.eol.globi.geo.LatLng) TreeMap(java.util.TreeMap) Map(java.util.Map) Test(org.junit.Test)

Example 40 with InteractionListener

use of org.eol.globi.process.InteractionListener in project eol-globi-data by jhpoelen.

the class DatasetImporterForFishbase3 method importStudy.

@Override
public void importStudy() throws StudyImporterException {
    try {
        String defaultNamespace = getDataset().getOrDefault("namespace", "FB");
        HashMap<String, Map<String, String>> countries = new HashMap<>();
        importCountries(countries, getDataset().retrieve(URI.create("countref.tsv")));
        HashMap<String, Map<String, String>> references = new HashMap<>();
        importReferences(references, getDataset().retrieve(URI.create("refrens.tsv")), defaultNamespace);
        HashMap<String, Map<String, String>> speciesMap = new HashMap<>();
        importSpecies(speciesMap, getDataset().retrieve(URI.create("species_sealifebase.tsv")), "SLB");
        importSpecies(speciesMap, getDataset().retrieve(URI.create("species_fishbase.tsv")), "FB");
        InteractionListener listener = new InteractionListener() {

            private final InteractionListener listener = getInteractionListener();

            @Override
            public void on(Map<String, String> interaction) throws StudyImporterException {
                listener.on(new TreeMap<String, String>(interaction) {

                    {
                        put(DatasetImporterForTSV.DATASET_CITATION, getSourceCitationLastAccessed());
                    }
                });
            }
        };
        importDiet(listener, getDataset().retrieve(URI.create("diet.tsv")), speciesMap, references, countries, defaultNamespace);
        importDietFoodII(listener, getDataset().retrieve(URI.create("diet.tsv")), speciesMap, references, countries, defaultNamespace);
        importPredators(listener, getDataset().retrieve(URI.create("predats.tsv")), speciesMap, references, countries, defaultNamespace);
        importFoodItemsByFoodName(listener, getDataset().retrieve(URI.create("fooditems.tsv")), speciesMap, references, countries, defaultNamespace);
        importFoodItemsByFoodII(listener, getDataset().retrieve(URI.create("fooditems.tsv")), speciesMap, references, countries, defaultNamespace);
    } catch (IOException e) {
        throw new StudyImporterException("failed to import", e);
    }
}
Also used : InteractionListener(org.eol.globi.process.InteractionListener) HashMap(java.util.HashMap) IOException(java.io.IOException) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) Map(java.util.Map)

Aggregations

InteractionListener (org.eol.globi.process.InteractionListener)58 Test (org.junit.Test)51 Map (java.util.Map)46 ArrayList (java.util.ArrayList)36 TreeMap (java.util.TreeMap)26 URI (java.net.URI)24 IOException (java.io.IOException)22 JsonNode (com.fasterxml.jackson.databind.JsonNode)20 DatasetImpl (org.globalbioticinteractions.dataset.DatasetImpl)20 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)19 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)16 URL (java.net.URL)15 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)14 InputStream (java.io.InputStream)13 List (java.util.List)13 TaxonUtil (org.eol.globi.service.TaxonUtil)13 Archive (org.gbif.dwc.Archive)13 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)13 HashMap (java.util.HashMap)11 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)10