All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
it.tidalwave.bluebill.taxonomy.birds.BirdTaxonomyImporter Maven / Gradle / Ivy
/***********************************************************************************************************************
*
* blueBill Mobile - open source birdwatching
* ==========================================
*
* Copyright (C) 2009, 2010 by Tidalwave s.a.s. (http://www.tidalwave.it)
* http://bluebill.tidalwave.it/mobile/
*
***********************************************************************************************************************
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
***********************************************************************************************************************
*
* $Id: BirdTaxonomyImporter.java,v d3b28eab0cdd 2010/08/01 20:17:04 fabrizio $
*
**********************************************************************************************************************/
package it.tidalwave.bluebill.taxonomy.birds;
import it.tidalwave.bluebill.taxonomy.birds.itis.ItisCodeMap;
import it.tidalwave.util.logging.Logger;
import java.util.ArrayList;
import java.util.List;
import java.util.SortedMap;
import java.util.TreeMap;
import javax.xml.namespace.QName;
import org.openrdf.elmo.LiteralManager;
import org.openrdf.model.Literal;
import org.openrdf.model.URI;
import org.openrdf.model.impl.URIImpl;
import java.util.SortedSet;
import java.util.TreeSet;
import it.tidalwave.bluebill.taxonomy.AnonymousTaxon;
import it.tidalwave.bluebill.taxonomy.Taxonomy;
import java.io.IOException;
import javax.annotation.Nonnull;
import java.util.HashMap;
import java.util.Map;
import java.io.File;
import org.openrdf.elmo.Entity;
import org.openrdf.elmo.ResourceManager;
import org.openrdf.elmo.sesame.SesameManager;
import org.openrdf.model.Resource;
import org.openrdf.model.vocabulary.RDF;
import org.openrdf.repository.RepositoryException;
import it.tidalwave.util.Initializer;
import it.tidalwave.util.NotFoundException;
import it.tidalwave.openrdf.elmo.ElmoManagerThreadLocal;
import it.tidalwave.bluebill.taxonomy.TaxonomyConcept;
import it.tidalwave.bluebill.taxonomy.TaxonomyManager;
import java.io.FileNotFoundException;
import java.io.Serializable;
import java.util.Collections;
import org.openrdf.repository.Repository;
import static it.tidalwave.bluebill.taxonomy.elmo.ElmoTaxonomyVocabulary.*;
/***********************************************************************************************************************
*
* @author Fabrizio Giudici
* @version $Id: BirdTaxonomyImporter.java,v d3b28eab0cdd 2010/08/01 20:17:04 fabrizio $
*
**********************************************************************************************************************/
public abstract class BirdTaxonomyImporter implements Serializable
{
private static final String CLASS = BirdTaxonomyImporter.class.getName();
private static final Logger logger = Logger.getLogger(CLASS);
protected transient TaxonomyManager taxonomyManager = TaxonomyManager.Locator.findTaxonomyManager();
protected transient Taxonomy metaTaxonomy;
protected transient TaxonomyConcept classType; // FIXME: or subtype?
protected transient TaxonomyConcept orderType;
protected transient TaxonomyConcept familyType;
protected transient TaxonomyConcept subFamilyType;
protected transient TaxonomyConcept genusType;
protected transient TaxonomyConcept speciesType;
protected transient TaxonomyConcept subSpeciesType;
protected transient Taxonomy birdTaxonomy;
protected transient BirdLocaleInfo birdLocaleInfo;
protected transient final Map> childCache = new HashMap>();
protected transient TaxonUniqueIdManager taxonUniqueIdManager;
protected transient String sourceResourceName;
protected transient final ItisCodeMap itisCodeMap = new ItisCodeMap();
protected final SortedSet brokenSpecies = new TreeSet();
protected final SortedSet speciesWithoutItisCode = new TreeSet();
protected final SortedMap> speciesWithoutTranslations = new TreeMap>();
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
protected transient TaxonTranslator catalogueOfLifeTranslator = new TaxonTranslator()
{
@Override
public void loadConfiguration()
{
}
@Override
public String fixTypos (final @Nonnull String string)
{
return string;
}
};
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
public void initialize()
throws IOException
{
itisCodeMap.initialize();
// itisTranslator.loadConfiguration();
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
public void setSourceResourceName (final @Nonnull String sourceResourceName)
throws FileNotFoundException
{
// if (!sourceFile.exists())
// {
// throw new FileNotFoundException(sourceFile.toString());
// }
this.sourceResourceName = sourceResourceName;
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
public void setCatalogueOfLifeTranslator (final @Nonnull TaxonTranslator catalogueOfLifeTranslator)
{
this.catalogueOfLifeTranslator = catalogueOfLifeTranslator;
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
public void setBirdLocaleInfo (final @Nonnull BirdLocaleInfo birdLocaleInfo)
{
this.birdLocaleInfo = birdLocaleInfo;
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
public void setIdentifierMapFile (final @Nonnull File identifierMapFile)
throws IOException
{
taxonUniqueIdManager = new TaxonUniqueIdManager(identifierMapFile);
catalogueOfLifeTranslator.loadConfiguration();
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@Nonnull
public SortedSet getBrokenSpecies()
{
return brokenSpecies;
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@Nonnull
public SortedSet getSpeciesWithoutItisCode()
{
return speciesWithoutItisCode;
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@Nonnull
public SortedMap> getSpeciesWithoutTranslations()
{
return Collections.unmodifiableSortedMap(speciesWithoutTranslations);
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
public void init (final @Nonnull Repository repository)
{
taxonomyManager = TaxonomyManager.Locator.findTaxonomyManager();
metaTaxonomy = taxonomyManager.createTaxonomy("Taxonomy", ID_TAXONOMY_STRUCTURE_PREFIX, repository);
classType = metaTaxonomy.createTopConcept("class", ID_TAXONOMY_STRUCTURE_PREFIX + "class"); // FIXME: or subtype?
orderType = classType.createSubConcept("order", ID_TAXONOMY_STRUCTURE_PREFIX + "order");
familyType = orderType.createSubConcept("family", ID_TAXONOMY_STRUCTURE_PREFIX + "family");
subFamilyType = familyType.createSubConcept("subfamily", ID_TAXONOMY_STRUCTURE_PREFIX + "subfamily");
genusType = subFamilyType.createSubConcept("genus", ID_TAXONOMY_STRUCTURE_PREFIX + "genus");
speciesType = genusType.createSubConcept("species", ID_TAXONOMY_STRUCTURE_PREFIX + "species");
subSpeciesType = speciesType.createSubConcept("subspecies", ID_TAXONOMY_STRUCTURE_PREFIX + "subspecies");
birdTaxonomy = taxonomyManager.createTaxonomy("Birds", ID_TAXONOMY_PREFIX + "BirdsFlat", repository);
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
protected void addMissingTranslation (final @Nonnull String species, final @Nonnull String language)
{
List languages = speciesWithoutTranslations.get(species);
if (languages == null)
{
languages = new ArrayList();
speciesWithoutTranslations.put(species, languages);
}
languages.add(language);
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@Nonnull
protected TaxonomyConcept findOrCreateSubConcept (final @Nonnull TaxonomyConcept parentConcept,
final @Nonnull String name,
final @Nonnull String root,
final @Nonnull String path,
final @Nonnull TaxonomyConcept type,
final @Nonnull Initializer extraInitializer)
throws RepositoryException
{
return findOrCreateSubConcept(parentConcept, name, root + path, new Initializer()
{
@Override
public TaxonomyConcept initialize (@Nonnull TaxonomyConcept taxon)
{
try
{
final String colTranslatedPath = catalogueOfLifeTranslator.translated(path);
logger.info("Processing: " + path);
if (!colTranslatedPath.equals(path))
{
logger.fine("Translated to: " + colTranslatedPath);
}
// final String itisTranslatedPath = itisTranslator.translated(translatedPath);
//
// if (!itisTranslatedPath.equals(translatedPath))
// {
// logger.fine("Translated to: " + itisTranslatedPath);
// }
final String id = taxonUniqueIdManager.findId(colTranslatedPath);
final AnonymousTaxon equivalentTaxon = birdTaxonomy.findOrCreateAnonymousTaxon(id);
setType(taxon, type); // use order.as(TypeSet).add(orderType)
taxon.setAnonymousSynonym(equivalentTaxon);
// try
// {
// final String itisCode = itisCodeMap.findCodeByPath(itisTranslatedPath);
// addStatement(taxon.as(Entity.class), "http://rdf.geospecies.org/ont/txn.owl#hasITIS", itisCode);
// }
// catch (NotFoundException e2)
// {
// speciesWithoutItisCode.add(path + " -> " + colTranslatedPath + " -> " + itisTranslatedPath);
// }
}
catch (RepositoryException e)
{
throw new RuntimeException(e);
}
catch (RuntimeException e)
{
brokenSpecies.add(path + ": " + e);
}
taxon = extraInitializer.initialize(taxon);
return taxon;
}
});
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
// FIXME: move to TaxonomyConcept
// FIXME: but you can't move until you get rid of childCache, which is fundamental for performance
@Nonnull
protected TaxonomyConcept findOrCreateSubConcept (final @Nonnull TaxonomyConcept parentConcept,
final @Nonnull String name,
final @Nonnull String id,
final @Nonnull Initializer initializer)
throws RepositoryException
{
TaxonomyConcept concept = null;
Map map = childCache.get(parentConcept);
try
{
if (map == null)
{
map = new HashMap();
childCache.put(parentConcept, map);
throw new NotFoundException();
}
concept = map.get(name);
if (concept == null)
{
throw new NotFoundException();
}
// concept = parentConcept.findSubConcepts().name(name).result();
}
catch (NotFoundException e)
{
concept = parentConcept.createSubConcept(name, id);
map.put(name, concept);
initializer.initialize(concept);
}
return concept;
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
protected void setType (final @Nonnull TaxonomyConcept concept,
final @Nonnull TaxonomyConcept type)
throws RepositoryException
{
final SesameManager em = (SesameManager)ElmoManagerThreadLocal.get();
final ResourceManager rm = em.getResourceManager();
em.getConnection().add(rm.createResource(concept.getLookup().lookup(Entity.class).getQName()),
RDF.TYPE,
rm.createResource(type.getLookup().lookup(Entity.class).getQName())); // FIXME
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
protected static void addStatement (final @Nonnull Entity entity,
final @Nonnull String predicate2,
final @Nonnull String literal2)
{
final SesameManager em = (SesameManager)ElmoManagerThreadLocal.get();
final LiteralManager literalManager = em.getLiteralManager();
final Literal literal = literalManager.getLiteral(literal2);
final URI predicate = new URIImpl(predicate2); // FIXME
try
{
// final QName qName = entity.getLookup().lookup(Entity.class).getQName();
final QName qName = entity.getQName();
final Resource entityResource = em.getResourceManager().createResource(qName);
em.getConnection().add(entityResource, predicate, literal);
}
catch (RepositoryException e)
{
throw new RuntimeException(e);
}
}
}