it.tidalwave.bluebill.taxonomy.birds.GeneratorSupport Maven / Gradle / Ivy
The newest version!
/***********************************************************************************************************************
*
* blueBill Resources - open source birding
* Copyright (C) 2009-2011 by Tidalwave s.a.s. (http://www.tidalwave.it)
*
***********************************************************************************************************************
*
* 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.
*
***********************************************************************************************************************
*
* WWW: http://bluebill.tidalwave.it
* SCM: https://java.net/hg/bluebill~resources-src
*
**********************************************************************************************************************/
package it.tidalwave.bluebill.taxonomy.birds;
import it.tidalwave.bluebill.taxonomy.Taxonomy;
import it.tidalwave.bluebill.taxonomy.TaxonomyManager;
import it.tidalwave.bluebill.taxonomy.io.Exporter;
import it.tidalwave.netbeans.util.Locator;
import java.util.SortedSet;
import org.openrdf.elmo.ElmoManager;
import org.openrdf.elmo.ElmoModule;
import org.openrdf.elmo.sesame.SesameManagerFactory;
import org.openrdf.repository.Repository;
import org.openrdf.repository.sail.SailRepository;
import org.openrdf.sail.memory.MemoryStore;
import it.tidalwave.openrdf.elmo.ElmoManagerThreadLocal;
import javax.annotation.Nonnull;
import java.io.FileOutputStream;
import java.io.File;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import lombok.Cleanup;
import lombok.extern.slf4j.Slf4j;
/***********************************************************************************************************************
*
* @author Fabrizio Giudici
* @version $Id$
*
**********************************************************************************************************************/
@Slf4j
public abstract class GeneratorSupport
{
protected final File resourcesFolder;
protected final File targetFolder;
protected SesameManagerFactory smf;
protected Repository repository;
@Nonnull
protected final String taxonomyName;
@Nonnull
protected final String taxonomyFileName;
private final TaxonomyManager taxonomyManager = Locator.find(TaxonomyManager.class);
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
public GeneratorSupport (final @Nonnull String baseDir,
final @Nonnull String taxonomyName,
final @Nonnull String taxonomyFileName)
{
this.taxonomyName = taxonomyName;
this.taxonomyFileName = taxonomyFileName;
resourcesFolder = new File(baseDir + "/target/");
targetFolder = new File(baseDir + "/target/test-artifacts/");
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
public void execute()
throws Exception
{
final File rdfFile = new File(targetFolder, taxonomyFileName + ".rdf");
final File n3File = new File(targetFolder, taxonomyFileName + ".n3");
//
// This allows to run this from 'mvn deploy' without regenerating the stuff in case they have already been done.
//
if (rdfFile.exists() || n3File.exists())
{
log.warn("{} or {} exist: won't run. Please delete both of them and re-run me.", rdfFile, n3File);
}
else
{
initialize();
final BirdTaxonomyImporter importer = run();
final File file = new File(targetFolder, taxonomyFileName + "-importer.ser");
final ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
oos.writeObject(importer);
oos.close();
final Taxonomy taxonomy = taxonomyManager.findTaxonomyByName(taxonomyName, repository);
taxonomy.as(Exporter.class).export(rdfFile);
taxonomy.as(Exporter.class).export(n3File);
final File brokenSpeciesReportFile = new File("target/test-artifacts/broken-species.txt");
brokenSpeciesReportFile.getParentFile().mkdirs();
@Cleanup final PrintWriter pw = new PrintWriter(brokenSpeciesReportFile);
final SortedSet brokenSpecies = importer.getBrokenSpecies();
for (final String species : brokenSpecies)
{
pw.println(species);
}
pw.close();
if (!brokenSpecies.isEmpty())
{
log.warn("WARNING: there are {} broken species", brokenSpecies.size());
}
}
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
public abstract BirdTaxonomyImporter run()
throws Exception;
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
protected void initialize()
throws Exception
{
repository = new SailRepository(new MemoryStore());
repository.initialize();
final ElmoModule elmoModule = new ElmoModule();
smf = new SesameManagerFactory(elmoModule, repository);
final ElmoManager em = smf.createElmoManager();
ElmoManagerThreadLocal.set(em);
targetFolder.mkdirs();
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
protected void close()
{
ElmoManagerThreadLocal.get().close();
ElmoManagerThreadLocal.set(null);
smf.close();
}
}