Skip to content

Commit

Permalink
[Feature #15] Support streams instead of files
Browse files Browse the repository at this point in the history
  • Loading branch information
blcham committed Sep 14, 2021
1 parent 1f2a6c5 commit ce83605
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,20 @@
import cz.cvut.spipes.constants.SML;
import cz.cvut.spipes.engine.ExecutionContext;
import cz.cvut.spipes.engine.ExecutionContextFactory;
import cz.cvut.spipes.exception.ResourceNotFoundException;
import cz.cvut.spipes.registry.StreamResource;
import cz.cvut.spipes.registry.StreamResourceRegistry;
import org.apache.jena.datatypes.xsd.XSDDatatype;
import org.apache.jena.rdf.model.*;
import org.eclipse.rdf4j.model.vocabulary.RDF;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.supercsv.io.CsvListReader;
import org.supercsv.io.ICsvListReader;
import org.supercsv.prefs.CsvPreference;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.*;
import java.util.List;

/**
Expand All @@ -31,6 +33,7 @@ public class TabularModule extends AbstractModule {

private final Property P_DELIMETER = getSpecificParameter("delimiter");
private final Property P_DATE_PREFIX = getSpecificParameter("data-prefix");
private final Property P_SOURCE_RESOURCE_URI = getSpecificParameter("source-resource-uri");

/**
* Output data mode.
Expand All @@ -46,8 +49,8 @@ public enum Mode {
//sml:replace
private boolean isReplace;

//sml:sourceFilePath
private String sourceFilePath;
//:source-resource-uri
private StreamResource sourceResource;

//:delimiter
private int delimiter;
Expand Down Expand Up @@ -76,14 +79,6 @@ public void setReplace(boolean replace) {
isReplace = replace;
}

public String getSourceFilePath() {
return sourceFilePath;
}

public void setSourceFilePath(String sourceFilePath) {
this.sourceFilePath = sourceFilePath;
}

public int getDelimiter() {
return delimiter;
}
Expand All @@ -100,6 +95,14 @@ public void setDataPrefix(String dataPrefix) {
this.dataPrefix = dataPrefix;
}

public StreamResource getSourceResource() {
return sourceResource;
}

public void setSourceResource(StreamResource sourceResource) {
this.sourceResource = sourceResource;
}

@Override
ExecutionContext executeSelf() {
outputModel = ModelFactory.createDefaultModel();
Expand All @@ -109,7 +112,7 @@ ExecutionContext executeSelf() {

onTableGroup(null);

onTable("file://" + sourceFilePath, null);
onTable(sourceResource.getUri(), null);

CsvPreference csvPreference = new CsvPreference.Builder('"',
delimiter,
Expand All @@ -118,7 +121,7 @@ ExecutionContext executeSelf() {
// for each row
ICsvListReader listReader = null;
try {
listReader = new CsvListReader(getFileReader(), csvPreference);
listReader = new CsvListReader(getReader(), csvPreference);

String[] header = listReader.getHeader(true); // skip the header (can't be used with CsvListReader)

Expand Down Expand Up @@ -185,13 +188,13 @@ ExecutionContext executeSelf() {
}

} catch (IOException e) {
LOG.error("Error while reading file {}", sourceFilePath, e);
LOG.error("Error while reading file from resource uri {}", sourceResource, e);
} finally {
if( listReader != null ) {
try {
listReader.close();
} catch (IOException e) {
LOG.error("Error while closing file {}", sourceFilePath, e);
LOG.error("Error while closing file from resource uri {}", sourceResource, e);
}
}
}
Expand All @@ -207,9 +210,9 @@ public String getTypeURI() {
@Override
public void loadConfiguration() {
isReplace = this.getPropertyValue(SML.replace, false);
sourceFilePath = getEffectiveValue(SML.sourceFilePath).asLiteral().toString();
delimiter = getPropertyValue(P_DELIMETER, '\t');
dataPrefix = getEffectiveValue(P_DATE_PREFIX).asLiteral().toString();
sourceResource = getResourceByUri(getEffectiveValue(P_SOURCE_RESOURCE_URI).asLiteral().toString());
}

private static Property getSpecificParameter(String localPropertyName) {
Expand Down Expand Up @@ -275,8 +278,19 @@ private void onTable(String tableResource, String tableUri) {
}
}

private FileReader getFileReader() throws FileNotFoundException {
return new FileReader(sourceFilePath);
private Reader getReader() {
return new StringReader(new String(sourceResource.getContent()));
}

@NotNull
private StreamResource getResourceByUri(@NotNull String resourceUri) {

StreamResource res = StreamResourceRegistry.getInstance().getResourceByUrl(resourceUri);

if (res == null) {
throw new ResourceNotFoundException("Stream resource " + resourceUri + " not found. ");
}
return res;
}

private Statement getCellStatement(Resource rowResource, String columnName, String value) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package cz.cvut.spipes.util;

import cz.cvut.spipes.registry.StreamResource;
import cz.cvut.spipes.registry.StringStreamResource;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

public class StreamResourceUtils {
public static StreamResource getStreamResource(String uri, Path filePath) throws IOException {
return new StringStreamResource(uri, Files.readAllBytes(filePath), null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,31 @@

import cz.cvut.spipes.engine.ExecutionContext;
import cz.cvut.spipes.engine.ExecutionContextFactory;
import cz.cvut.spipes.util.StreamResourceUtils;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Path;

import static org.junit.jupiter.api.Assertions.assertTrue;

public class TabularToRDFModelModuleTest extends AbstractModuleTestHelper {
public class TabularModuleTest extends AbstractModuleTestHelper {

@Override
public String getModuleName() {
return "tabular";
}

@Test
public void executeWithSimpleTransformation() throws URISyntaxException {
public void executeWithSimpleTransformation() throws URISyntaxException, IOException {

TabularModule module = new TabularModule();

Path filePath = this.getFilePath("countries.tsv");

module.setSourceFilePath(filePath.toString());
module.setSourceResource(
StreamResourceUtils.getStreamResource(
"http://test-file",
getFilePath("countries.tsv"))
);
module.setReplace(true);
module.setDelimiter('\t');
module.setDataPrefix("http://onto.fel.cvut.cz/data/");
Expand Down

0 comments on commit ce83605

Please sign in to comment.