Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ttools: add some programmatic invocation example classes
- Loading branch information
Showing
2 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
ttools/src/main/uk/ac/starlink/ttools/example/Calculator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" ); | ||
} | ||
} |