Skip to content
Snippets Groups Projects
Commit 533682af authored by Bernhard Johannes Berger's avatar Bernhard Johannes Berger
Browse files

[WIP] #3 Minor changes to fix bugs.

parent 4a888deb
No related branches found
No related tags found
No related merge requests found
Pipeline #243421 passed
Showing
with 185 additions and 8 deletions
......@@ -4,7 +4,7 @@ public final class ArffBlackboardEntry {
/**
* The arff file to read.
*/
public static final String ARFF_INPUT = "arff:arff-input";
public static final String ARFF_INPUT = "arff:input";
/**
......
......@@ -13,7 +13,7 @@ import java.io.FileOutputStream;
@Slf4j
@Dependent
@Named("json-writer")
@Named("arff-writer")
public class ArffPropertiesWriter implements PropertiesWriter {
private FileOutputStream outputStream;
......
package de.evoal.core.arff;
import de.evoal.core.api.properties.PropertiesSpecification;
import de.evoal.core.api.properties.io.PropertiesReader;
import de.evoal.core.api.utils.EvoalIOException;
import de.evoal.core.arff.utils.LanguageUtils;
import de.evoal.core.arff.utils.ResourceUtils;
import de.evoal.languages.model.ddl.DataDescriptionModel;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
public class ArffPropertiesReaderTest {
@Test
public void testSimpleArff() throws EvoalIOException, IOException {
final Path temporary = Files.createTempDirectory("evoal");
final File temporaryFolder = temporary.toFile();
ResourceUtils.unpack(getClass().getClassLoader(), temporaryFolder, "simple/input.arff", "simple/specification.ddl");
// final DataDescriptionModel model = LanguageUtils.load(new File(temporaryFolder, "simple/specification.ddl"), DataDescriptionModel.class);
// final PropertiesSpecification specification = toSpecification(model);
// final PropertiesReader testee = new ArffPropertiesReader()
// .init(new File(temporaryFolder, "input.arff"), specification);
// Assertions.assertTrue(testee.hasNext());
}
private PropertiesSpecification toSpecification(final DataDescriptionModel model) {
return PropertiesSpecification
.builder()
.add(model.getDescriptions().stream())
.build();
}
}
package de.evoal.core.arff.utils;
import de.evoal.languages.model.ddl.DataDescriptionModel;
import de.evoal.languages.model.ddl.DdlPackage;
import de.evoal.languages.model.ddl.dsl.DataDescriptionLanguageStandaloneSetup;
import lombok.NonNull;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import java.io.File;
import java.util.List;
public class LanguageUtils {
/**
* Loads an EMF model and returns the model instance.
*
* @param path Path of the model to load.
*
* @return A valid reference to the model
*
* @throws Exception If there is any problem while loading the model
*/
public static <T extends EObject> T load(final @NonNull File path, final Class<T> cl) {
loadPackages();
final URI modelURI = URI.createPlatformResourceURI(path.toString(), true);
return load(modelURI, cl);
}
private static void loadPackages() {
//DataDescriptionLanguageStandaloneSetup.doSetup();
}
private static <T extends EObject> T load(final URI modelURI, final Class<T> cl) {
final ResourceSet rs = new ResourceSetImpl();
final Resource modelResource = rs.getResource(modelURI, true);
final List<EObject> resourceContents = modelResource.getContents();
if(resourceContents.isEmpty()) {
throw new RuntimeException(String.format("Resource %s is empty.", modelURI));
}
if(resourceContents.size() > 1) {
throw new RuntimeException(String.format("Resource %s has to many members.", modelURI));
}
final EObject root = resourceContents.get(0);
try {
return cl.cast(root);
} catch(final ClassCastException e) {
throw new RuntimeException("Loaded object cannot be casted to " + cl.getName(), e);
}
}
}
package de.evoal.core.arff.utils;
import org.junit.jupiter.api.io.TempDir;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.InputStream;
import java.nio.file.*;
import java.io.IOException;
import java.net.URL;
import java.nio.file.attribute.FileAttribute;
import java.nio.file.attribute.PosixFileAttributes;
import java.nio.file.attribute.PosixFilePermission;
import java.nio.file.attribute.PosixFilePermissions;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Set;
public final class ResourceUtils {
private static Logger log = LoggerFactory.getLogger(ResourceUtils.class);
public static void unpack(final ClassLoader loader, final File destination, final String ... files) throws IOException {
for(final String base : files) {
log.info("Searching for file {}.", base);
final Enumeration<URL> resources = loader.getResources(base);
while(resources.hasMoreElements()) {
final URL resource = resources.nextElement();
final Path outputPath = Path.of(destination.toString(), base);
final Set<PosixFilePermission> permissions = new HashSet<>();
permissions.add(PosixFilePermission.OWNER_READ);
permissions.add(PosixFilePermission.OWNER_WRITE);
permissions.add(PosixFilePermission.OWNER_EXECUTE);
try {
Files.createDirectory(outputPath.getParent(), PosixFilePermissions.asFileAttribute(permissions));
} catch(final FileAlreadyExistsException e) {
// ignore
}
log.info("Copying to {}", outputPath);
try(final InputStream iStream = resource.openStream()) {
Files.copy(iStream, outputPath, StandardCopyOption.REPLACE_EXISTING);
}
}
}
}
}
% 1. Title: Iris Plants Database
%
% 2. Sources:
% (a) Creator: R.A. Fisher
% (b) Donor: Michael Marshall (MARSHALL%PLU@io.arc.nasa.gov)
% (c) Date: July, 1988
%
@RELATION iris
@ATTRIBUTE sepallength NUMERIC
@ATTRIBUTE sepalwidth NUMERIC
@ATTRIBUTE petallength NUMERIC
@ATTRIBUTE petalwidth NUMERIC
@ATTRIBUTE class {Iris-setosa,Iris-versicolor,Iris-virginica}
@DATA
5.1,3.5,1.4,0.2,Iris-setosa
4.9,3.0,1.4,0.2,Iris-setosa
4.7,3.2,1.3,0.2,Iris-setosa
4.6,3.1,1.5,0.2,Iris-setosa
5.0,3.6,1.4,0.2,Iris-setosa
5.4,3.9,1.7,0.4,Iris-setosa
4.6,3.4,1.4,0.3,Iris-setosa
5.0,3.4,1.5,0.2,Iris-setosa
4.4,2.9,1.4,0.2,Iris-setosa
4.9,3.1,1.5,0.1,Iris-setosa
\ No newline at end of file
......@@ -39,7 +39,7 @@ public final class Evoal {
try {
main = BeanProvider.getContextualReference(mainName, false, MainClass.class);
} catch(final Throwable e) {
logMainError();
logMainError(e);
System.exit(1);
}
......@@ -52,8 +52,9 @@ public final class Evoal {
cdiContainer.shutdown();
}
private static void logMainError() {
log.error("Name of main class was not set. Please specify the main class via command line (-BMAIN=<name>).");
private static void logMainError(final Throwable e) {
log.error("Filed to create main.", e);
log.error("Name of main class was not set correctly. Please specify the main class via command line (-B{}=<name>).", BlackboardEntry.MAIN);
Set<Bean<MainClass>> beans = BeanProvider.getBeanDefinitions(MainClass.class, true, true);
log.error(" possible names are:");
......
......@@ -33,16 +33,17 @@ public class BlackboardValueProducer {
@BlackboardValue(BlackboardEntry.EA_CONFIGURATION)
public String injectStringValue(final InjectionPoint ip, final Blackboard board) {
final BlackboardValue value = ip.getAnnotated().getAnnotation(BlackboardValue.class);
final Object result = board.get(value.value());
return castValue(board.get(value.value()));
return castValue(result);
}
@Produces
@BlackboardValue(BlackboardEntry.EA_CONFIGURATION)
public File injectFileValue(final InjectionPoint ip, final Blackboard board) {
final BlackboardValue value = ip.getAnnotated().getAnnotation(BlackboardValue.class);
return castValue(board.get(value.value()));
final Object result = board.get(value.value());
return new File(result.toString());
}
@Produces
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment