Skip to content

Commit

Permalink
ttools: implement DynamicTask for plot2 tasks
Browse files Browse the repository at this point in the history
Now:

    plot2task layerN=xx help=yyN

reports something useful when yy is a parameter associated with
layer type xx.  It works for help=yy as well.
  • Loading branch information
mbtaylor committed Oct 3, 2014
1 parent 1096f14 commit 2ce410b
Show file tree
Hide file tree
Showing 2 changed files with 205 additions and 23 deletions.
177 changes: 163 additions & 14 deletions ttools/src/main/uk/ac/starlink/ttools/plot2/task/AbstractPlot2Task.java
Expand Up @@ -79,6 +79,7 @@
import uk.ac.starlink.ttools.plottask.SwingPainter;
import uk.ac.starlink.ttools.task.AddEnvironment;
import uk.ac.starlink.ttools.task.ConsumerTask;
import uk.ac.starlink.ttools.task.DynamicTask;
import uk.ac.starlink.ttools.task.FilterParameter;
import uk.ac.starlink.ttools.task.InputTableParameter;
import uk.ac.starlink.ttools.task.StringMultiParameter;
Expand All @@ -92,7 +93,7 @@
* @author Mark Taylor
* @since 22 Aug 2014
*/
public abstract class AbstractPlot2Task implements Task {
public abstract class AbstractPlot2Task implements Task, DynamicTask {

private final boolean allowAnimate_;
private final IntegerParameter xpixParam_;
Expand Down Expand Up @@ -685,6 +686,135 @@ public JComponent createPlotComponent( Environment env, boolean caching )
true, caching );
}

public Parameter getParameterByName( Environment env, String paramName )
throws TaskException {

/* Find each layer that has been set in the environment
* (by a layerN setting). Find its layer type and suffix.
* Then it's a case of going through all the parameters that
* come with that layer type to see if any of them match the
* requested on by name. */
PlotContext context = getPlotContext( env );
for ( Map.Entry<String,LayerType> entry :
getLayers( env, context ).entrySet() ) {
String suffix = entry.getKey();
final LayerType layer = entry.getValue();

/* Layer type associated parameters. */
int nassoc = layer.getAssociatedParameters( suffix ).length;
for ( int ia = 0; ia < nassoc; ia++ ) {
final int iassoc = ia;
Parameter param = new ParameterFinder<Parameter>() {
protected Parameter createParameter( String sfix ) {
return layer.getAssociatedParameters( sfix )[ iassoc ];
}
}.findParameterByName( paramName, suffix );
if ( param != null ) {
return param;
}
}

/* Layer positional parameters. */
int npos = layer.getPositionCount();
DataGeom geom = context.getGeom( env, suffix );
Coord[] posCoords = geom.getPosCoords();
for ( int ipos = 0; ipos < npos; ipos++ ) {
final String posSuffix = npos > 1
? PlotUtil.getIndexSuffix( ipos )
: "";
for ( Coord coord : posCoords ) {
for ( final Input input : coord.getInputs() ) {
Parameter param = new ParameterFinder<Parameter>() {
protected Parameter createParameter( String sfix ) {
return createDataParameter( input,
posSuffix + sfix,
true );
}
}.findParameterByName( paramName, suffix );
if ( param != null ) {
return param;
}
}
}
}

/* Layer non-positional parameters. */
Coord[] extraCoords = layer.getExtraCoords();
for ( Coord coord : extraCoords ) {
for ( final Input input : coord.getInputs() ) {
Parameter param = new ParameterFinder<Parameter>() {
protected Parameter createParameter( String sfix ) {
return createDataParameter( input, sfix, true );
}
}.findParameterByName( paramName, suffix );
if ( param != null ) {
return param;
}
}
}

/* Layer style parameters. */
ConfigKey[] styleKeys = layer.getStyleKeys();
for ( final ConfigKey key : styleKeys ) {
Parameter param = new ParameterFinder<Parameter>() {
protected Parameter createParameter( String sfix ) {
return ConfigParameter
.createSuffixedParameter( key, sfix, true );
}
}.findParameterByName( paramName, suffix );
if ( param != null ) {
return param;
}
}

/* Now try shading parameters if appropriate. */
if ( layer instanceof ShapeFamilyLayerType ) {
final ShapeFamilyLayerType shadeLayer =
(ShapeFamilyLayerType) layer;
ShapeMode shapeMode =
new ParameterFinder<Parameter<ShapeMode>>() {
protected Parameter<ShapeMode>
createParameter( String sfix ) {
return shadeLayer.createShapeModeParameter( sfix );
}
}.getParameter( env, suffix )
.objectValue( env );

/* Shading coordinate parameters. */
Coord[] shadeCoords = shapeMode.getExtraCoords();
for ( Coord coord : shadeCoords ) {
for ( final Input input : coord.getInputs() ) {
Parameter param = new ParameterFinder<Parameter>() {
protected Parameter createParameter( String sfix ) {
return createDataParameter( input, sfix, true );
}
}.findParameterByName( paramName, suffix );
if ( param != null ) {
return param;
}
}
}

/* Shading config parameters. */
ConfigKey[] shadeKeys = shapeMode.getConfigKeys();
for ( final ConfigKey key : shadeKeys ) {
Parameter param = new ParameterFinder<Parameter>() {
protected Parameter createParameter( String sfix ) {
return ConfigParameter
.createSuffixedParameter( key, sfix, true );
}
}.findParameterByName( paramName, suffix );
if ( param != null ) {
return param;
}
}
}
}

/* No luck. */
return null;
}

/**
* Gets a painter value from an environment.
*
Expand Down Expand Up @@ -877,34 +1007,53 @@ private PlotLayer[] createLayers( Environment env, PlotContext context )
}

/**
* Returns a map of suffix strings to Plotter objects.
* Returns a map of suffix strings to LayerType objects.
* Each suffix string is appended to parameters associated with the
* relevant plotter as a namespacing device on the command line.
* relevant LayerType as a namespacing device on the command line.
*
* @param env execution environment
* @param context plot context
* @return mapping from suffixes to plotters for the environment
* @return mapping from suffixes to layer types for the environment
*/
private Map<String,Plotter> getPlotters( Environment env,
private Map<String,LayerType> getLayers( Environment env,
PlotContext context )
throws TaskException {
String prefix = PLOTTER_PREFIX;
Map<String,Plotter> map = new LinkedHashMap<String,Plotter>();
String[] pnames = env.getNames();
for ( int in = 0; in < pnames.length; in++ ) {
String name = pnames[ in ];
if ( name != null &&
name.toLowerCase().startsWith( prefix.toLowerCase() ) ) {
String suffix = name.substring( prefix.length() );
Map<String,LayerType> map = new LinkedHashMap<String,LayerType>();
for ( String pname : env.getNames() ) {
if ( pname != null &&
pname.toLowerCase().startsWith( prefix.toLowerCase() ) ) {
String suffix = pname.substring( prefix.length() );
LayerType ltype = createLayerTypeParameter( suffix, context )
.objectValue( env );
Plotter plotter = ltype.getPlotter( env, suffix );
map.put( suffix, plotter );
map.put( suffix, ltype );
}
}
return map;
}

/**
* Returns a map of suffix strings to Plotter objects.
* Each suffix string is appended to parameters associated with the
* relevant plotter as a namespacing device on the command line.
*
* @param env execution environment
* @param context plot context
* @return mapping from suffixes to plotters for the environment
*/
private Map<String,Plotter> getPlotters( Environment env,
PlotContext context )
throws TaskException {
Map<String,Plotter> plotterMap = new LinkedHashMap<String,Plotter>();
for ( Map.Entry<String,LayerType> entry :
getLayers( env, context ).entrySet() ) {
String suffix = entry.getKey();
LayerType ltype = entry.getValue();
plotterMap.put( suffix, ltype.getPlotter( env, suffix ) );
}
return plotterMap;
}

/**
* Creates a PlotLayer from the environment given a plotter, suffix
* and geom.
Expand Down
Expand Up @@ -32,23 +32,38 @@ public abstract class ParameterFinder<P extends Parameter> {
protected abstract P createParameter( String suffix );

/**
* Returns a parameter to use for obtaining a value associated
* Calls {@link #findParameter}, but if the result is null,
* a parameter with the full suffix is returned.
* In that case, the environment doesn't already have a
* value for the parameter, but it may take steps to obtain one
* (like asking the user).
*
* @param env execution environment, possibly populated with values
* @param fullSuffix suffix associated with the layer for which
* the value is required
* @return parameter for obtaining a value associated with the
* layer suffix, not null
*/
public P getParameter( Environment env, String fullSuffix ) {
P param = findParameter( env, fullSuffix );
return param != null ? param : createParameter( fullSuffix );
}

/**
* Returns an existing parameter to use for obtaining a value associated
* with the given layer suffix from the given environment.
* If the environment contains a value for the parameter
* with the given suffix, or of any shortened form of that suffix
* (including the empty string), that parameter is returned.
* Otherwise, a parameter with the full suffix is returned.
* In the latter case, the environment doesn't already have a
* value for the parameter, but it may take steps to obtain one
* (like asking the user).
* Otherwise, null is returned.
*
* @param env execution environment, possibly populated with values
* @param fullSuffix suffix associated with the layer for which
* the value is required
* @return parameter for obtaining a value associated with the
* layer suffix
* layer suffix, or null
*/
P getParameter( Environment env, String fullSuffix ) {
public P findParameter( Environment env, String fullSuffix ) {
Collection<String> names =
new HashSet<String>( Arrays.asList( env.getNames() ) );
for ( int i = fullSuffix.length(); i >= 0; i-- ) {
Expand All @@ -57,7 +72,25 @@ P getParameter( Environment env, String fullSuffix ) {
return param;
}
}
return createParameter( fullSuffix );
return null;
}
}

/**
* Attempts to locate a parameter known by this finder with the given name.
* If it can't be found, return null.
*
* @param target required parameter name (not case-sensitive)
* @param fullSuffix suffix associated with the layer for which
* the value is required
* @return parameter with name <code>target</code>, or null
*/
public P findParameterByName( String target, String fullSuffix ) {
for ( int i = fullSuffix.length(); i >= 0; i-- ) {
P param = createParameter( fullSuffix.substring( 0, i ) );
if ( target.equalsIgnoreCase( param.getName() ) ) {
return param;
}
}
return null;
}
}

0 comments on commit 2ce410b

Please sign in to comment.