You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@uima.apache.org by ea...@apache.org on 2019/06/11 21:47:40 UTC
svn commit: r1861077 - in /uima/uima-ducc/trunk/uima-ducc-examples/src/main:
java/org/apache/uima/ducc/sampleapps/
resources/org/apache/uima/ducc/sampleapps/
Author: eae
Date: Tue Jun 11 21:47:40 2019
New Revision: 1861077
URL: http://svn.apache.org/viewvc?rev=1861077&view=rev
Log:
UIMA-6062 add the CR and AE components
Added:
uima/uima-ducc/trunk/uima-ducc-examples/src/main/java/org/apache/uima/ducc/sampleapps/CommandLineRunnerAE.java
uima/uima-ducc/trunk/uima-ducc-examples/src/main/java/org/apache/uima/ducc/sampleapps/LinesFromFileCR.java
uima/uima-ducc/trunk/uima-ducc-examples/src/main/resources/org/apache/uima/ducc/sampleapps/CommandLineRunnerAE.xml
uima/uima-ducc/trunk/uima-ducc-examples/src/main/resources/org/apache/uima/ducc/sampleapps/LinesFromFileCR.xml
Added: uima/uima-ducc/trunk/uima-ducc-examples/src/main/java/org/apache/uima/ducc/sampleapps/CommandLineRunnerAE.java
URL: http://svn.apache.org/viewvc/uima/uima-ducc/trunk/uima-ducc-examples/src/main/java/org/apache/uima/ducc/sampleapps/CommandLineRunnerAE.java?rev=1861077&view=auto
==============================================================================
--- uima/uima-ducc/trunk/uima-ducc-examples/src/main/java/org/apache/uima/ducc/sampleapps/CommandLineRunnerAE.java (added)
+++ uima/uima-ducc/trunk/uima-ducc-examples/src/main/java/org/apache/uima/ducc/sampleapps/CommandLineRunnerAE.java Tue Jun 11 21:47:40 2019
@@ -0,0 +1,178 @@
+/* Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+ package org.apache.uima.ducc.sampleapps;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.lang.reflect.Field;
+import java.util.Iterator;
+import java.util.List;
+
+import org.apache.uima.UimaContext;
+import org.apache.uima.analysis_component.JCasAnnotator_ImplBase;
+import org.apache.uima.analysis_engine.AnalysisEngineProcessException;
+import org.apache.uima.jcas.JCas;
+import org.apache.uima.resource.ResourceInitializationException;
+
+import com.google.common.collect.Lists;
+
+public class CommandLineRunnerAE extends JCasAnnotator_ImplBase {
+
+ private List<Process> processes = Lists.newLinkedList();
+ private String[] envp;
+ private String lastStderrLine;
+
+ @Override
+ public void initialize(UimaContext aContext) throws ResourceInitializationException {
+ super.initialize(aContext);
+ System.out.println("AE init");
+
+ String cp = System.getenv("DUCC_BATCH_CLASSPATH");
+ String env = System.getenv("DUCC_BATCH_ENVIRONMENT");
+ String user= System.getenv("USER");
+ String home= System.getenv("HOME");
+ String lang= System.getenv("LANG");
+ String jobid=System.getenv("JobId");
+ envp = (env+" CLASSPATH="+cp+" USER="+user+" HOME="+home+" LANG="+lang+" DUCC_JOBID="+jobid).split("\\s+");
+ }
+
+ @Override
+ public void process(JCas jcas) throws AnalysisEngineProcessException {
+
+ String command = jcas.getDocumentText();
+ try {
+ runCommand(command, envp);
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ @Override
+ public void destroy() {
+ super.destroy();
+ System.out.println("CommandLineRunnerAE: Destroy method was called");
+ }
+
+ public void runCommand(String command, String[] envp) throws IOException {
+ System.out.println("Running: " + command);
+ Runtime rt = Runtime.getRuntime();
+ Process pr = rt.exec(command, envp);
+
+ StdoutReader sr = new StdoutReader();
+ sr.initialize(pr);
+ (new Thread(sr)).start();
+ processes.add(pr);
+ try {
+ Class<? extends Process> cl = pr.getClass();
+ Field field;
+ field = cl.getDeclaredField("pid");
+ field.setAccessible(true);
+ Object pidObject = field.get(pr);
+ System.out.println("AE: started PID=" + (Integer) pidObject);
+ } catch (NoSuchFieldException e1) {
+ e1.printStackTrace();
+ } catch (SecurityException e1) {
+ e1.printStackTrace();
+ } catch (IllegalArgumentException e) {
+ e.printStackTrace();
+ } catch (IllegalAccessException e) {
+ e.printStackTrace();
+ }
+
+ StderrReader ser = new StderrReader();
+ ser.initialize(pr);
+ (new Thread(ser)).start();
+
+ try {
+ pr.waitFor();
+ } catch (InterruptedException e) {
+ throw new RuntimeException(e);
+ }
+ int exitValue = pr.exitValue();
+ for (Iterator<Process> iter = processes.listIterator(); iter.hasNext(); ) {
+ Process p = iter.next();
+ if (p == pr) {
+ iter.remove();
+ }
+ }
+ if (exitValue != 0) {
+ throw new RuntimeException("Exit code=" + exitValue +" stderr=" + lastStderrLine);
+ }
+ }
+
+ class StdoutReader
+ implements Runnable
+ {
+ private Process mypr;
+ public void initialize(Process pr) {
+ mypr = pr;
+ }
+
+ @Override
+ public void run() {
+ String line;
+
+ BufferedReader br = new BufferedReader(new InputStreamReader(mypr.getInputStream()));
+ try {
+ while( (line = br.readLine()) != null)
+ {
+// if(line.equals("exit")) {
+// break;
+// }
+ System.out.println(line);
+ }
+ br.close();
+ } catch (IOException e) {
+ // exit the loop and the thread terminates ?
+ }
+
+ }
+ }
+
+ class StderrReader
+ implements Runnable
+ {
+ private Process mypr;
+ public void initialize(Process pr) {
+ mypr = pr;
+ }
+
+ @Override
+ public void run() {
+// String line;
+
+ BufferedReader br = new BufferedReader(new InputStreamReader(mypr.getErrorStream()));
+ try {
+ while( (lastStderrLine = br.readLine()) != null)
+ {
+// if(line.equals("exit")) {
+// break;
+// }
+ System.out.println(lastStderrLine);
+ }
+ br.close();
+ } catch (IOException e) {
+ // exit the loop and the thread terminates ?
+ }
+
+ }
+ }
+
+}
Added: uima/uima-ducc/trunk/uima-ducc-examples/src/main/java/org/apache/uima/ducc/sampleapps/LinesFromFileCR.java
URL: http://svn.apache.org/viewvc/uima/uima-ducc/trunk/uima-ducc-examples/src/main/java/org/apache/uima/ducc/sampleapps/LinesFromFileCR.java?rev=1861077&view=auto
==============================================================================
--- uima/uima-ducc/trunk/uima-ducc-examples/src/main/java/org/apache/uima/ducc/sampleapps/LinesFromFileCR.java (added)
+++ uima/uima-ducc/trunk/uima-ducc-examples/src/main/java/org/apache/uima/ducc/sampleapps/LinesFromFileCR.java Tue Jun 11 21:47:40 2019
@@ -0,0 +1,94 @@
+/* Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.uima.ducc.sampleapps;
+
+import java.io.BufferedReader;
+import java.io.FileReader;
+import java.io.IOException;
+import java.util.List;
+
+import org.apache.uima.cas.CAS;
+import org.apache.uima.collection.CollectionException;
+import org.apache.uima.collection.CollectionReader_ImplBase;
+import org.apache.uima.resource.ResourceInitializationException;
+import org.apache.uima.util.Progress;
+import org.apache.uima.util.ProgressImpl;
+
+import com.google.common.collect.Lists;
+
+
+public class LinesFromFileCR extends CollectionReader_ImplBase {
+
+ public static final String PARAM_FILENAME = "Filename";
+ private List<String> lines = Lists.newArrayList();
+ private int lineCount = 0;
+ private String filename;
+
+ @Override
+ public void initialize() throws ResourceInitializationException {
+ System.out.println("CR: init");
+ super.initialize();
+
+ Object configParameterValue = getUimaContext().getConfigParameterValue(PARAM_FILENAME);
+ if (configParameterValue == null) {
+ throw new RuntimeException("Filename: config param not set");
+ }
+ filename = (String) configParameterValue;
+ int linecnt = 0;
+ try {
+ FileReader fileReader = new FileReader(filename);
+ BufferedReader bufferedReader = new BufferedReader(fileReader);
+ String line = null;
+ while ((line = bufferedReader.readLine()) != null) {
+ if (! "#".equals(line.substring(0, 1))) {
+ lines.add(line);
+ linecnt++;
+ }
+ }
+ bufferedReader.close();
+ System.out.println("CR: found " + linecnt + " commands to run");
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ @Override
+ public synchronized void getNext(CAS cas) throws IOException, CollectionException {
+ cas.reset();
+ cas.setSofaDataString(lines.get(lineCount), "text");
+ System.out.println("CR: " + lineCount + " = " + cas.getDocumentText());
+ lineCount++;
+ return;
+ }
+
+ @Override
+ public boolean hasNext() throws IOException {
+ return (lines.size() > lineCount);
+ }
+
+ @Override
+ public Progress[] getProgress() {
+ return new Progress[] { new ProgressImpl(lineCount, lines.size(), Progress.ENTITIES) };
+ }
+
+ @Override
+ public void close() throws IOException {
+ }
+
+}
Added: uima/uima-ducc/trunk/uima-ducc-examples/src/main/resources/org/apache/uima/ducc/sampleapps/CommandLineRunnerAE.xml
URL: http://svn.apache.org/viewvc/uima/uima-ducc/trunk/uima-ducc-examples/src/main/resources/org/apache/uima/ducc/sampleapps/CommandLineRunnerAE.xml?rev=1861077&view=auto
==============================================================================
--- uima/uima-ducc/trunk/uima-ducc-examples/src/main/resources/org/apache/uima/ducc/sampleapps/CommandLineRunnerAE.xml (added)
+++ uima/uima-ducc/trunk/uima-ducc-examples/src/main/resources/org/apache/uima/ducc/sampleapps/CommandLineRunnerAE.xml Tue Jun 11 21:47:40 2019
@@ -0,0 +1,52 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+ <!--
+ ***************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ ***************************************************************
+ -->
+
+<analysisEngineDescription xmlns="http://uima.apache.org/resourceSpecifier">
+ <frameworkImplementation>org.apache.uima.java</frameworkImplementation>
+ <primitive>true</primitive>
+ <annotatorImplementationName>org.apache.uima.ducc.sampleapps.CommandLineRunnerAE</annotatorImplementationName>
+ <analysisEngineMetaData>
+ <name>CommandLineRunnerAE</name>
+ <description>Executes specified command line as a subprocess</description>
+ <version>1.0</version>
+ <vendor/>
+ <configurationParameters/>
+ <configurationParameterSettings/>
+ <typeSystemDescription/>
+ <typePriorities/>
+ <fsIndexCollection/>
+ <capabilities>
+ <capability>
+ <inputs/>
+ <outputs/>
+ <languagesSupported/>
+ </capability>
+ </capabilities>
+ <operationalProperties>
+ <modifiesCas>false</modifiesCas>
+ <multipleDeploymentAllowed>true</multipleDeploymentAllowed>
+ <outputsNewCASes>false</outputsNewCASes>
+ </operationalProperties>
+ </analysisEngineMetaData>
+ <resourceManagerConfiguration/>
+</analysisEngineDescription>
\ No newline at end of file
Added: uima/uima-ducc/trunk/uima-ducc-examples/src/main/resources/org/apache/uima/ducc/sampleapps/LinesFromFileCR.xml
URL: http://svn.apache.org/viewvc/uima/uima-ducc/trunk/uima-ducc-examples/src/main/resources/org/apache/uima/ducc/sampleapps/LinesFromFileCR.xml?rev=1861077&view=auto
==============================================================================
--- uima/uima-ducc/trunk/uima-ducc-examples/src/main/resources/org/apache/uima/ducc/sampleapps/LinesFromFileCR.xml (added)
+++ uima/uima-ducc/trunk/uima-ducc-examples/src/main/resources/org/apache/uima/ducc/sampleapps/LinesFromFileCR.xml Tue Jun 11 21:47:40 2019
@@ -0,0 +1,64 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+ <!--
+ ***************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ ***************************************************************
+ -->
+
+<collectionReaderDescription xmlns="http://uima.apache.org/resourceSpecifier">
+ <frameworkImplementation>org.apache.uima.java</frameworkImplementation>
+ <implementationName>org.apache.uima.ducc.sampleapps.LinesFromFileCR</implementationName>
+ <processingResourceMetaData>
+ <name>File System Line Collection Reader</name>
+ <description>Generates CASes with one line from a file in each CAS.</description>
+ <version>1.0</version>
+ <vendor>IBM</vendor>
+
+ <configurationParameters>
+
+ <configurationParameter>
+ <name>Filename</name>
+ <externalOverrideName>LinesFromFileCR~Filename</externalOverrideName>
+ <description>Path to file</description>
+ <type>String</type>
+ <multiValued>false</multiValued>
+ <mandatory>true</mandatory>
+ </configurationParameter>
+
+ </configurationParameters>
+
+ <configurationParameterSettings>
+ <!-- Omit the required input-path parameter setting so will fail if not overridden -->
+ </configurationParameterSettings>
+
+ <typeSystemDescription/>
+
+ <capabilities>
+ <capability>
+ <inputs/>
+ <outputs/>
+ </capability>
+ </capabilities>
+ <operationalProperties>
+ <modifiesCas>false</modifiesCas>
+ <multipleDeploymentAllowed>false</multipleDeploymentAllowed>
+ <outputsNewCASes>true</outputsNewCASes>
+ </operationalProperties>
+ </processingResourceMetaData>
+</collectionReaderDescription>