Commit 6fa8694f authored by R.W.Majeed's avatar R.W.Majeed
Browse files

maven plugin for RDF ontology to SQL translation

parent 3cdc7271
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
.settings/
.classpath
.project
target/
+73 −0
Original line number Diff line number Diff line
<project>
	<modelVersion>4.0.0</modelVersion>

	<groupId>de.sekmi.histream.maven</groupId>
	<artifactId>histream-maven-plugin</artifactId>
	<version>0.9-SNAPSHOT</version>
	<packaging>maven-plugin</packaging>

	<name>HIStream maven plugin</name>

	<build>
		<plugins>
		<!-- use new plugin plugin. default 3.2 will not work with java 8 -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-plugin-plugin</artifactId>
				<version>3.4</version>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.5.1</version>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
				</configuration>
			</plugin>

		</plugins>
	</build>
	<dependencies>
		<!-- functional dependencies -->
		<dependency>
			<groupId>de.sekmi.histream</groupId>
			<artifactId>histream-i2b2</artifactId>
			<version>0.9-SNAPSHOT</version>
		</dependency>
		<dependency>
			<groupId>de.sekmi.histream</groupId>
			<artifactId>histream-skos</artifactId>
			<version>0.9-SNAPSHOT</version>
		</dependency>

		<!-- maven plugin dependencies -->
		<dependency>
			<groupId>org.apache.maven</groupId>
			<artifactId>maven-plugin-api</artifactId>
			<version>3.3.9</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.apache.maven.shared/file-management -->
		<dependency>
		    <groupId>org.apache.maven.shared</groupId>
		    <artifactId>file-management</artifactId>
		    <version>3.0.0</version>
		</dependency>

		<!-- dependencies to annotations -->
		<dependency>
			<groupId>org.apache.maven.plugin-tools</groupId>
			<artifactId>maven-plugin-annotations</artifactId>
			<version>3.4</version>
			<scope>provided</scope>
		</dependency>

		<!-- testing -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
			<scope>test</scope>
		</dependency>
	</dependencies>
</project>
+121 −0
Original line number Diff line number Diff line
package de.sekmi.histream.maven;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.apache.maven.shared.model.fileset.FileSet;
import org.apache.maven.shared.model.fileset.util.FileSetManager;
import org.openrdf.repository.RepositoryException;

import de.sekmi.histream.i2b2.ont.SQLGenerator;
import de.sekmi.histream.ontology.OntologyException;
import de.sekmi.histream.ontology.skos.Store;

@Mojo(name="ontology")
public class OntologyMojo extends AbstractMojo {	
	@Parameter(required=true)
	FileSet source;

	/**
	 * Destination for the generated resources. 
	 * Defaults to {@code ${project.build.directory}/generated-resources/sql/}
	 */
	@Parameter(required=false, defaultValue="${project.build.directory}/generated-resources/sql")
	File destination;
	
	/**
	 * Overwrite existing files in the destination
	 */
	@Parameter
	boolean overwrite;

	@Parameter
	Properties properties;

	@Parameter(defaultValue="${project}", readonly=true, required=true)
	MavenProject project;

	private void logWarning(String message){
		getLog().warn(message);
	}
	private List<File> getSourceFiles(){
		// read source files
		Objects.requireNonNull(source);
		FileSetManager fsm = new FileSetManager();
		String[] files = fsm.getIncludedFiles(source);
		String base = source.getDirectory();
		List<File> list = new ArrayList<>(files.length);
		for( int i=0; i<files.length; i++ ){
			list.add(new File(base, files[i]));
		}
		return list;
	}
	private Map<String,String> loadConfiguration(){
		Map<String,String> map = new HashMap<>();
		if( properties != null ){
			for( String name : properties.stringPropertyNames() ){
				map.put(name, properties.getProperty(name));
			}
		}
		if( !map.containsKey("meta.sourcesystem_cd") ){
			// use artifact id
			this.getLog().warn("Using project artifact id for missing property meta.sourcesystem_cd");
			map.put("meta.sourcesystem_cd", project.getArtifactId());
		}
		// language
		if( !map.containsKey("ont.language") ){
			this.getLog().warn("Using system default language for missing property ont.language: "+Locale.getDefault().getLanguage());
			map.put("ont.language", Locale.getDefault().getLanguage());
		}
		// TODO use defaults for missing properties meta.table, etc
		return map;
	}
	@Override
	public void execute() throws MojoExecutionException, MojoFailureException {
		// copy properties to string map
		Map<String,String> map = loadConfiguration();
		// create destination directories if not existing
		try {
			Files.createDirectories(destination.toPath());
		} catch (IOException e) {
			throw new MojoExecutionException("Unable to create destination directory: "+destination, e);
		}
		Path metaSQL = new File(destination, "meta.sql").toPath();
		Path dataSQL = new File(destination, "data.sql").toPath();

		// load ontology
		Store store;
		try {
			store = new Store(getSourceFiles(), null);
		} catch (RepositoryException | IOException e) {
			throw new MojoExecutionException("Unable to load ontology", e);
		}
		
		try {
			SQLGenerator gen = new SQLGenerator(metaSQL, dataSQL, map, overwrite);
			gen.setWarningHandler(this::logWarning);
			gen.writeOntologySQL(store);
			gen.close();
			store.close();
		} catch (IOException | SQLException | OntologyException e) {
			throw new MojoExecutionException("Ontology to SQL transformation failed", e);
		}
	}

}
+33 −0
Original line number Diff line number Diff line
package de.sekmi.histream.maven;

import java.io.File;
import java.io.InputStream;
import java.net.URL;
import java.util.Collections;
import java.util.Properties;

import org.apache.maven.shared.model.fileset.FileSet;
import org.junit.Assert;
import org.junit.Test;

public class TestOntologyMojo {

	@Test
	public void expectSuccessfulExecution() throws Exception{
		URL ttl = getClass().getResource("/skos-aktin-cda.ttl");
		File source = new File(ttl.toURI());
		Assert.assertTrue(source.isFile());
		OntologyMojo mojo = new OntologyMojo();
		mojo.source = new FileSet();
		mojo.source.setDirectory(source.getParent());
		mojo.source.addInclude(source.getName());
		mojo.destination = new File("target/");
		mojo.properties = new Properties();
		mojo.overwrite = true;
		try( InputStream in = getClass().getResourceAsStream("/ontology.properties") ){
			mojo.properties.load(in);			
		}
				
		mojo.execute();
	}
}
+13 −0
Original line number Diff line number Diff line
# metadata table
meta.table=i2b2metadata.i2b2
# metadata table access
meta.access=i2b2metadata.table_access
meta.basepath=\\i2b2\\
# Before each import, data for this sourcesystem_cd will be deleted
meta.sourcesystem_cd=test

data.concept.table=i2b2demodata.concept_dimension


# language to use for accessing ontology
ont.language=DE
Loading