Skip to content

Commit

Permalink
Fixed Codacy issues.
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelRoeder committed Aug 25, 2023
1 parent 84de9c8 commit caddbab
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 47 deletions.
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
FROM hub.cs.upb.de/enexa/images/enexa-utils:1 as enexa-utils
FROM maven:3.9-eclipse-temurin-17-alpine

WORKDIR /app

# Add enexa-utils
COPY --from=hub.cs.upb.de/enexa/images/enexa-utils:1 / /.
COPY --from=enexa-utils / /.

# Add Maven dependencies (not shaded into the artifact; Docker-cached)
COPY target/lib /app/lib
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,46 +46,56 @@ public class EnexaTransformator {

private static final Logger LOGGER = LoggerFactory.getLogger(EnexaTransformator.class);

public static void main(String[] args) {
// 0. init
String experimentIri = getEnvVariable("ENEXA_EXPERIMENT_IRI");
String endpoint = getEnvVariable("ENEXA_META_DATA_ENDPOINT");
String metaGraph = getEnvVariable("ENEXA_META_DATA_GRAPH");
String moduleInstance = getEnvVariable("ENEXA_MODULE_INSTANCE_IRI");
String sharedDir = getEnvVariable("ENEXA_SHARED_DIRECTORY");
String outputDir = getEnvVariable("ENEXA_MODULE_INSTANCE_DIRECTORY");
String enexaServiceUrl = getEnvVariable("ENEXA_SERVICE_URL");
protected Resource experiment;
protected String endpoint;
protected String metaGraph;
protected Resource moduleInstance;
protected String sharedDir;
protected String outputDir;
protected String enexaServiceUrl;
protected Model parameterModel;
protected List<Resource> sourceFiles;
protected Resource targetMediaResource;
protected File outputFile;
protected String enexaFileLocation;

public EnexaTransformator() throws IllegalStateException {
experiment = ResourceFactory.createResource(getEnvVariable("ENEXA_EXPERIMENT_IRI"));
endpoint = getEnvVariable("ENEXA_META_DATA_ENDPOINT");
metaGraph = getEnvVariable("ENEXA_META_DATA_GRAPH");
moduleInstance = ResourceFactory.createResource(getEnvVariable("ENEXA_MODULE_INSTANCE_IRI"));
sharedDir = getEnvVariable("ENEXA_SHARED_DIRECTORY");
if (!sharedDir.endsWith(File.separator)) {
sharedDir += File.separator;
}
outputDir = getEnvVariable("ENEXA_MODULE_INSTANCE_DIRECTORY");
enexaServiceUrl = getEnvVariable("ENEXA_SERVICE_URL");
if (!enexaServiceUrl.endsWith("/")) {
enexaServiceUrl += "/";
}
}

Resource moduleInsResource = ResourceFactory.createResource(moduleInstance);
Resource experimentResource = ResourceFactory.createResource(experimentIri);

// 1. get parameters from SPARQL endpoint
Model parameterModel = queryParameterModel(endpoint, metaGraph, moduleInstance);
protected void requestParameters() throws IllegalArgumentException {
parameterModel = queryParameterModel(endpoint, metaGraph, moduleInstance.getURI());
LOGGER.debug("Parameter Model: {}", parameterModel);
List<Resource> sourceFiles = RdfHelper.getObjectResources(parameterModel, moduleInsResource,
TransformVocab.input);
sourceFiles = RdfHelper.getObjectResources(parameterModel, moduleInstance, TransformVocab.input);
if (sourceFiles.size() == 0) {
LOGGER.error("No input files defined. Aborting.");
return;
throw new IllegalArgumentException(
"No input files defined (parameter IRI:" + TransformVocab.input.getURI() + ").");
}
Resource targetMediaResource = RdfHelper.getObjectResource(parameterModel, moduleInsResource,
targetMediaResource = RdfHelper.getObjectResource(parameterModel, moduleInstance,
TransformVocab.outputMediaType);
if (targetMediaResource == null) {
LOGGER.error("The output media type has not been defined. Aborting.");
return;
throw new IllegalArgumentException("The output media type has not been defined (parameter IRI:"
+ TransformVocab.outputMediaType.getURI() + ").");
}
if (!targetMediaResource.isURIResource()) {
LOGGER.error("The output media type is not an IRI. Aborting.");
return;
}
// 3. create transformer
if (!sharedDir.endsWith(File.separator)) {
sharedDir += File.separator;
throw new IllegalArgumentException(
"The output media type is not an IRI (" + targetMediaResource.toString() + ").");
}
File outputFile = null;
}

protected void executeTransformation() throws Exception {
try (Transformator transformator = new TransformatorBuilder().setOutputFormat(targetMediaResource.getURI())
// .setCompression(compression)
// .setOutputFileName(outputFile.getName())
Expand All @@ -95,21 +105,17 @@ public static void main(String[] args) {
}
// update output file after writing
outputFile = transformator.getOutputFile();
} catch (Exception e) {
LOGGER.error("Got an exception while transforming files. Aborting.", e);
return;
}
enexaFileLocation = EnexaPathUtils.translateLocal2EnexaPath(outputFile, sharedDir);
}

String enexaFileLocation = EnexaPathUtils.translateLocal2EnexaPath(outputFile, sharedDir);

// 3. write file metadata
protected void sendFileMetadata() throws IOException {
Model metadata = ModelFactory.createDefaultModel();
Resource fileResource = metadata.createResource();
metadata.add(fileResource, RDF.type, metadata.createResource("http://www.w3.org/ns/prov#Entity"));
metadata.add(fileResource, ENEXA.experiment, experimentResource);
metadata.add(fileResource, ENEXA.experiment, experiment);
metadata.add(fileResource, ENEXA.location, enexaFileLocation);
metadata.add(fileResource, metadata.createProperty("http://www.w3.org/ns/prov#wasGeneratedBy"),
moduleInsResource);
metadata.add(fileResource, metadata.createProperty("http://www.w3.org/ns/prov#wasGeneratedBy"), moduleInstance);
metadata.add(fileResource, DCAT.mediaType, targetMediaResource);
try {
metadata.addLiteral(fileResource, DCAT.byteSize, outputFile.length());
Expand All @@ -118,13 +124,45 @@ public static void main(String[] args) {
}
// Add the direct connection that the generated file is the output of this
// module instance
metadata.add(moduleInsResource, TransformVocab.output, fileResource);
metadata.add(moduleInstance, TransformVocab.output, fileResource);

if (sendRequest(enexaServiceUrl + "add-resource", metadata) != null) {
LOGGER.info("This module seems to have been successful.");
}
}

public static void main(String[] args) {
// 1. init
EnexaTransformator module = null;
try {
module = new EnexaTransformator();
} catch (Exception e) {
LOGGER.error("Error during initialization. Aborting.", e);
System.exit(-1);
}
// 2. get parameters from SPARQL endpoint
try {
module.requestParameters();
} catch (Exception e) {
LOGGER.error("Received invalid parameters. Aborting.", e);
System.exit(-1);
}
// 3. create transformer
try {
module.executeTransformation();
} catch (Exception e) {
LOGGER.error("Got an exception while transforming files. Aborting.", e);
System.exit(-1);
}
// 4. write file metadata
try {
module.sendFileMetadata();
} catch (Exception e) {
LOGGER.error("Exception while sending meta data of generated file. Aborting.", e);
System.exit(-1);
}
}

/**
* Returns the {@link Lang} instance for the output file, or {@code null} if the
* given media type is not known or the language is not supported for being
Expand Down Expand Up @@ -153,7 +191,7 @@ private static Lang getOutputLang(Resource targetMediaResource) {
* @return the value of the given variable
* @throws IllegalStateException if the key is not known
*/
private static String getEnvVariable(String key) {
private static String getEnvVariable(String key) throws IllegalStateException {
String value = System.getenv(key);
if (value == null) {
String msg = "Couldn't get value of variable " + key + ". The environment is not correctly set up.";
Expand Down Expand Up @@ -223,18 +261,16 @@ protected static Model queryParameterModel(String metaDataEndpoint, String metaD
* @param url the URL to which the data should be sent
* @param data the RDF data that should be sent within the request body
* @return the RDF model that has been received as response
* @throws IOException
*/
protected static Model sendRequest(String url, Model data) {
protected static Model sendRequest(String url, Model data) throws IOException {
HttpPost request = new HttpPost(url);
request.addHeader(HttpHeaders.ACCEPT, WebContent.contentTypeJSONLD);
if (data != null) {
try (StringWriter writer = new StringWriter()) {
request.addHeader(HttpHeaders.CONTENT_TYPE, WebContent.contentTypeJSONLD);
RDFDataMgr.write(writer, data, Lang.JSONLD);
request.setEntity(new StringEntity(writer.toString()));
} catch (IOException e) {
LOGGER.error("Catched unexpected exception while adding data to the request. Returning null.", e);
return null;
}
}
try (CloseableHttpClient client = HttpClients.createDefault();) {
Expand All @@ -255,10 +291,7 @@ protected static Model sendRequest(String url, Model data) {
return new Result(response.getCode(), model);
});
return result.getContent();
} catch (Exception e) {
LOGGER.error("Caught an exception while running request. Returning null.", e);
}
return null;
}

/**
Expand Down

0 comments on commit caddbab

Please sign in to comment.