Skip to content

Commit

Permalink
ttools: add some programmatic invocation example classes
Browse files Browse the repository at this point in the history
  • Loading branch information
mbtaylor authored and mmpcn committed Nov 27, 2014
1 parent 381bc54 commit 312822c
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
41 changes: 41 additions & 0 deletions ttools/src/main/uk/ac/starlink/ttools/example/Calculator.java
@@ -0,0 +1,41 @@
package uk.ac.starlink.ttools.example;

import java.io.IOException;
import uk.ac.starlink.task.Task;
import uk.ac.starlink.task.TaskException;
import uk.ac.starlink.ttools.task.MapEnvironment;
import uk.ac.starlink.ttools.task.Calc;

/**
* Minimal example of invoking a stilts task programmatically
* using the parameter system.
*
* @author Mark Taylor
* @since 3 Oct 2014
*/
public class Calculator {
public static void main( String[] args ) throws TaskException, IOException {

/* Get expression from command line. */
if ( args.length != 1 ) {
System.err.println( "\n Usage: " + Calculator.class.getName()
+ " <expression>\n" );
System.exit( 1 );
}
String expr = args[ 0 ];

/* Set up and populate an execution environment with parameter
* values. */
MapEnvironment env = new MapEnvironment();
env.setValue( "expression", expr );

/* Create and execute a task of the right kind with these parameters.
* The Calc class is the one used by the stilts calc command. */
Task calcTask = new Calc();
calcTask.createExecutable( env ).execute();

/* Retrieve the result from the environment, and output it. */
String result = env.getOutputText();
System.out.println( expr + "\t=\t" + result );
}
}
49 changes: 49 additions & 0 deletions ttools/src/main/uk/ac/starlink/ttools/example/Head10.java
@@ -0,0 +1,49 @@
package uk.ac.starlink.ttools.example;

import java.io.IOException;
import uk.ac.starlink.table.StarTable;
import uk.ac.starlink.table.StarTableOutput;
import uk.ac.starlink.task.Task;
import uk.ac.starlink.task.TaskException;
import uk.ac.starlink.ttools.task.MapEnvironment;
import uk.ac.starlink.ttools.task.TablePipe;

/**
* Minimal example of invoking a stilts table processing task
* programmatically using the parameter system.
*
* @author Mark Taylor
* @since 3 Oct 2014
*/
public class Head10 {
public static void main( String[] args ) throws TaskException, IOException {

/* Get input table file name from the command line. */
if ( args.length != 1 ) {
System.err.println( "\n Usage: " + Head10.class.getName()
+ " <table>\n" );
}
String fileName = args[ 0 ];

/* Set up and populate an execution environment
* with parameter values. An alternative to setting the value
* of the "in" parameter with a string would be to use a StarTable
* object. */
MapEnvironment env = new MapEnvironment();
env.setValue( "in", fileName );
env.setValue( "cmd", "head 10" );

/* Create and execute a task of the right kind with these parameters.
* The TablePipe class is the one used by the stilts tpipe command. */
Task task = new TablePipe();
task.createExecutable( env ).execute();

/* Retrieve the output table from the execution environment.
* The output parameter is "omode", which is the parameter of
* type ProcessingMode in the tpipe task. */
StarTable outTable = env.getOutputTable( "omode" );

/* Write the result to standard output. */
new StarTableOutput().writeStarTable( outTable, "-", "ascii" );
}
}

0 comments on commit 312822c

Please sign in to comment.