genopt.simulation
Class SimulationStarter

java.lang.Object
  extended by genopt.simulation.SimulationStarter
All Implemented Interfaces:
java.lang.Cloneable

public class SimulationStarter
extends java.lang.Object
implements java.lang.Cloneable

Object for calling a simulation program.

This project was carried out at:

and supported by

GenOpt Copyright (c) 1998-2011, The Regents of the University of California, through Lawrence Berkeley National Laboratory (subject to receipt of any required approvals from the U.S. Dept. of Energy). All rights reserved.

Version:
GenOpt(R) 3.1.0 (December 8, 2011)

Author:
Michael Wetter

Field Summary
protected  java.lang.String CommandLine
           
protected  java.util.concurrent.locks.ReentrantLock lock
           
private static java.lang.String LS
          System dependent line separator
protected  int maxThrPoo
          The maximum number of threads in the pool
protected  OptimizationIni OptIni
           
protected  java.lang.Process[] pro
           
protected  java.util.concurrent.atomic.AtomicBoolean[] proFre
          Array with boolean flag, flag is true if this process number can be used
protected  boolean PrombtFileExtension
           
protected  java.lang.String worDir
           
 
Constructor Summary
SimulationStarter(java.lang.String command, boolean promptInputFileExtension, java.lang.String workingDirectory, OptimizationIni optIni)
          constructor
 
Method Summary
private  int _getThreadNumber()
          Returns the thread number to be used by this process
(package private)  void _processProcessOutput(int iPro, ThreadedInputStream errorStream, java.io.File proWorDir, java.lang.String comLin)
          Processes the output of the simulation
private  java.lang.String _updateAndGetCommandLine(java.lang.String worDirPre)
          Updates the command line by replacing the output paths with the output path of the working directory, which can change from one simulation to another.
private  void _updateCommandLine()
          Updates the command line.
 void destroyProcess()
          Destroys all processes that exists
 void destroyProcess(int iPro)
          destroys the process if it exists
 java.lang.String getCommandDiagnostics(java.lang.String worDirPre)
          Gets a string representation of the command line and the working directory.
static java.lang.String replaceString(java.lang.String Original, java.lang.String Find, java.lang.String Set)
          Replace all occurences of the String 'Find' with the String 'Set' in the String 'Original' even if 'Find' appears several times.
 void run(java.lang.String worDirPre)
          Runs the simulation program
 void setMaximumNumberOfThreads(int maximumThreads)
          Sets the maximum number of threads in the pool.
 void setWorkingDirectory(java.lang.String workingDirectory)
          Sets the working directory.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LS

private static final java.lang.String LS
System dependent line separator


CommandLine

protected java.lang.String CommandLine

PrombtFileExtension

protected boolean PrombtFileExtension

worDir

protected java.lang.String worDir

OptIni

protected OptimizationIni OptIni

pro

protected java.lang.Process[] pro

proFre

protected java.util.concurrent.atomic.AtomicBoolean[] proFre
Array with boolean flag, flag is true if this process number can be used


maxThrPoo

protected int maxThrPoo
The maximum number of threads in the pool


lock

protected java.util.concurrent.locks.ReentrantLock lock
Constructor Detail

SimulationStarter

public SimulationStarter(java.lang.String command,
                         boolean promptInputFileExtension,
                         java.lang.String workingDirectory,
                         OptimizationIni optIni)
                  throws java.io.IOException
constructor

Parameters:
command - the command line (with placeholder for the variables)
promptInputFileExtension - flag for specifying whether the extension of the inputfile must be written in the command or not. (Set it true if the extension has to be written, false otherwise.)
workingDirectory - the working directory for the process.
optIni - Instance of the OptimizationIni class
Throws:
java.io.IOException - If an I/O error occurs, which is possible because the construction of the canonical pathname may require filesystem queries
Method Detail

setMaximumNumberOfThreads

public void setMaximumNumberOfThreads(int maximumThreads)
                               throws OptimizerException
Sets the maximum number of threads in the pool. This method can only be called once.

Parameters:
maximumThreads - The maximum number of threads in the pool
Throws:
OptimizerException - If the maximum number of processes has already been allocated

_updateCommandLine

private void _updateCommandLine()
Updates the command line. This command must be used to update the command line that was set by the constructor. Prior to the simulation, the method _updateCommandLine(String) needs to be called to update the output and log directory with the full name of the temporary working directory.


_updateAndGetCommandLine

private java.lang.String _updateAndGetCommandLine(java.lang.String worDirPre)
                                           throws java.io.IOException
Updates the command line by replacing the output paths with the output path of the working directory, which can change from one simulation to another. Prior to this function, the function _updateCommandLine() need to be called.

Parameters:
worDirPre - Prefix for working directory. This will be added to all output and log paths.
Throws:
java.io.IOException - If an I/O error occurs, which is possible because the construction of the canonical pathname may require filesystem queries

replaceString

public static java.lang.String replaceString(java.lang.String Original,
                                             java.lang.String Find,
                                             java.lang.String Set)
Replace all occurences of the String 'Find' with the String 'Set' in the String 'Original' even if 'Find' appears several times.
Note: The String 'Find' must not necessarily be found in String Original and could also be an empty string.

Parameters:
Original - String that has be scanned for 'Find'
Find - String that has to be found and replaced
Set - String that has to be set at the position of 'Find'
Returns:
String 'Original' where each occurence of 'Find' is replaced with 'Set'

_getThreadNumber

private int _getThreadNumber()
                      throws OptimizerException
Returns the thread number to be used by this process

Returns:
The thread number to be used by this process
Throws:
OptimizerException - If no thread is available. This condition should never occur.

getCommandDiagnostics

public java.lang.String getCommandDiagnostics(java.lang.String worDirPre)
                                       throws java.io.IOException
Gets a string representation of the command line and the working directory. Use this method for diagnostics report in case the simulation had errors.

Parameters:
worDirPre - working directory prefix, to be added to current working directory to enable parallel simulations
Throws:
java.io.IOException

run

public void run(java.lang.String worDirPre)
         throws java.io.IOException,
                OptimizerException,
                java.lang.Exception
Runs the simulation program

Parameters:
worDirPre - working directory prefix, to be added to current working directory to enable parallel simulations
Throws:
java.io.IOException
OptimizerException
java.lang.Exception

_processProcessOutput

void _processProcessOutput(int iPro,
                           ThreadedInputStream errorStream,
                           java.io.File proWorDir,
                           java.lang.String comLin)
                     throws java.lang.NullPointerException,
                            OptimizerException,
                            java.io.IOException
Processes the output of the simulation

Parameters:
iPro - the process number
proWorDir - the process working directory (used for error reporting)
comLin - the command line (used for error reporting)
Throws:
java.lang.NullPointerException - If the user requested GenOpt to stop
OptimizerException
java.io.IOException - if the output or error stream cannot be read

destroyProcess

public void destroyProcess()
Destroys all processes that exists


destroyProcess

public void destroyProcess(int iPro)
destroys the process if it exists

Parameters:
iPro - number of the process

setWorkingDirectory

public void setWorkingDirectory(java.lang.String workingDirectory)
Sets the working directory.

Parameters:
workingDirectory - the working directory.