Search in sources :

Example 1 with StreamReader

use of org.dkpro.lab.storage.StreamReader in project dkpro-lab by dkpro.

the class SimpleExecutionEngineTest method testInit.

@Test
public void testInit() throws Exception {
    File repo = new File("target/repository");
    FileUtils.deleteDirectory(repo);
    ((FileSystemStorageService) storageService).setStorageRoot(repo);
    assertNotNull(executionService);
    assertNotNull(contextFactory);
    TypeSystemDescription tsd = createTypeSystemDescription(new String[0]);
    AnalysisEngineDescription desc = createEngineDescription(DummyAE.class, tsd);
    DefaultUimaTask cfg = new DefaultUimaTask();
    cfg.setReaderDescription(createReaderDescription(TestReader.class, tsd));
    cfg.setAnalysisEngineDescription(desc);
    TaskExecutionEngine runner = executionService.createEngine(cfg);
    String uuid = runner.run(cfg);
    System.out.println("=== Experiments in repository ===");
    List<TaskContextMetadata> experiments = storageService.getContexts();
    for (TaskContextMetadata e : experiments) {
        System.out.println(e);
    }
    final StringBuilder sb = new StringBuilder();
    storageService.retrieveBinary(uuid, "test", new StreamReader() {

        @Override
        public void read(InputStream aInputStream) throws IOException {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            Util.shoveAndClose(aInputStream, bos);
            sb.append(new String(bos.toByteArray(), "UTF-8"));
        }
    });
    assertEquals("works", sb.toString());
}
Also used : TypeSystemDescription(org.apache.uima.resource.metadata.TypeSystemDescription) TypeSystemDescriptionFactory.createTypeSystemDescription(org.apache.uima.fit.factory.TypeSystemDescriptionFactory.createTypeSystemDescription) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) TaskExecutionEngine(org.dkpro.lab.engine.TaskExecutionEngine) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DefaultUimaTask(org.dkpro.lab.uima.task.impl.DefaultUimaTask) TaskContextMetadata(org.dkpro.lab.task.TaskContextMetadata) StreamReader(org.dkpro.lab.storage.StreamReader) AnalysisEngineDescription(org.apache.uima.analysis_engine.AnalysisEngineDescription) File(java.io.File) FileSystemStorageService(org.dkpro.lab.storage.filesystem.FileSystemStorageService) Test(org.junit.Test)

Example 2 with StreamReader

use of org.dkpro.lab.storage.StreamReader in project dkpro-lab by dkpro.

the class UimaAsExecutionEngineTest method testInit.

@Test
public void testInit() throws Exception {
    File repo = new File("target/repository");
    FileUtils.deleteQuietly(repo);
    ((FileSystemStorageService) storageService).setStorageRoot(repo);
    assertNotNull(executionService);
    assertNotNull(contextFactory);
    AnalysisEngineDescription desc = createEngineDescription(DummyAE.class);
    DefaultUimaTask cfg = new DefaultUimaTask();
    cfg.setReaderDescription(createReaderDescription(TestReader.class));
    cfg.setAnalysisEngineDescription(desc);
    TaskExecutionEngine runner = executionService.createEngine(cfg);
    String uuid = runner.run(cfg);
    System.out.println("=== Experiments in repository ===");
    List<TaskContextMetadata> experiments = storageService.getContexts();
    for (TaskContextMetadata e : experiments) {
        System.out.println(e);
    }
    final StringBuilder sb = new StringBuilder();
    storageService.retrieveBinary(uuid, "test", new StreamReader() {

        @Override
        public void read(InputStream aInputStream) throws IOException {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            Util.shoveAndClose(aInputStream, bos);
            sb.append(new String(bos.toByteArray(), "UTF-8"));
        }
    });
    assertEquals("works", sb.toString());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) TaskExecutionEngine(org.dkpro.lab.engine.TaskExecutionEngine) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DefaultUimaTask(org.dkpro.lab.uima.task.impl.DefaultUimaTask) TaskContextMetadata(org.dkpro.lab.task.TaskContextMetadata) StreamReader(org.dkpro.lab.storage.StreamReader) AnalysisEngineDescription(org.apache.uima.analysis_engine.AnalysisEngineDescription) File(java.io.File) FileSystemStorageService(org.dkpro.lab.storage.filesystem.FileSystemStorageService) Test(org.junit.Test)

Example 3 with StreamReader

use of org.dkpro.lab.storage.StreamReader in project dkpro-lab by dkpro.

the class FlexTable method getCsvReader.

public StreamReader getCsvReader() {
    return new StreamReader() {

        @Override
        public void read(InputStream aStream) throws IOException {
            try {
                CSVReader reader = new CSVReader(new InputStreamReader(aStream, "UTF-8"));
                String[] headers = reader.readNext();
                Method converter = FlexTable.this.dataClass.getMethod("valueOf", String.class);
                String[] data;
                while ((data = reader.readNext()) != null) {
                    Map<String, V> row = new LinkedHashMap<String, V>();
                    for (int i = 1; i < headers.length; i++) {
                        @SuppressWarnings("unchecked") V value = (V) converter.invoke(null, data[i]);
                        row.put(headers[i], value);
                    }
                    addRow(data[0], row);
                }
                reader.close();
            } catch (IOException e) {
                throw e;
            } catch (NoSuchMethodException e) {
                throw new IOException("Data class " + FlexTable.this.dataClass.getName() + " does not have a " + "public static Object valueOf(String) method - unable unmarshall the " + "data.");
            } catch (Exception e) {
                throw new IOException(e);
            }
        }
    };
}
Also used : InputStreamReader(java.io.InputStreamReader) CSVReader(au.com.bytecode.opencsv.CSVReader) InputStream(java.io.InputStream) Method(java.lang.reflect.Method) IOException(java.io.IOException) IOException(java.io.IOException) LinkedHashMap(java.util.LinkedHashMap) StreamReader(org.dkpro.lab.storage.StreamReader) InputStreamReader(java.io.InputStreamReader)

Example 4 with StreamReader

use of org.dkpro.lab.storage.StreamReader in project dkpro-tc by dkpro.

the class TcFlexTable method getCsvReader.

public StreamReader getCsvReader() {
    return new StreamReader() {

        @Override
        public void read(InputStream aStream) throws IOException {
            try {
                CSVReader reader = new CSVReader(new InputStreamReader(aStream, "UTF-8"));
                String[] headers = reader.readNext();
                Method converter = TcFlexTable.this.dataClass.getMethod("valueOf", String.class);
                String[] data;
                while ((data = reader.readNext()) != null) {
                    Map<String, V> row = new LinkedHashMap<String, V>();
                    for (int i = 1; i < headers.length; i++) {
                        @SuppressWarnings("unchecked") V value = (V) converter.invoke(null, data[i]);
                        row.put(headers[i], value);
                    }
                    addRow(data[0], row);
                }
                reader.close();
            } catch (IOException e) {
                throw e;
            } catch (NoSuchMethodException e) {
                throw new IOException("Data class " + TcFlexTable.this.dataClass.getName() + " does not have a " + "public static Object valueOf(String) method - unable unmarshall the " + "data.");
            } catch (Exception e) {
                throw new IOException(e);
            }
        }
    };
}
Also used : InputStreamReader(java.io.InputStreamReader) CSVReader(au.com.bytecode.opencsv.CSVReader) InputStream(java.io.InputStream) Method(java.lang.reflect.Method) IOException(java.io.IOException) IOException(java.io.IOException) LinkedHashMap(java.util.LinkedHashMap) StreamReader(org.dkpro.lab.storage.StreamReader) InputStreamReader(java.io.InputStreamReader)

Aggregations

IOException (java.io.IOException)4 InputStream (java.io.InputStream)4 StreamReader (org.dkpro.lab.storage.StreamReader)4 CSVReader (au.com.bytecode.opencsv.CSVReader)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 File (java.io.File)2 InputStreamReader (java.io.InputStreamReader)2 Method (java.lang.reflect.Method)2 LinkedHashMap (java.util.LinkedHashMap)2 AnalysisEngineDescription (org.apache.uima.analysis_engine.AnalysisEngineDescription)2 TaskExecutionEngine (org.dkpro.lab.engine.TaskExecutionEngine)2 FileSystemStorageService (org.dkpro.lab.storage.filesystem.FileSystemStorageService)2 TaskContextMetadata (org.dkpro.lab.task.TaskContextMetadata)2 DefaultUimaTask (org.dkpro.lab.uima.task.impl.DefaultUimaTask)2 Test (org.junit.Test)2 TypeSystemDescriptionFactory.createTypeSystemDescription (org.apache.uima.fit.factory.TypeSystemDescriptionFactory.createTypeSystemDescription)1 TypeSystemDescription (org.apache.uima.resource.metadata.TypeSystemDescription)1