public class ExternalModule extends Object implements MaryModule
Any external module extending this class will need to implement a constructor calling this class's constructor. If data
input/output requires additional processing, the subclass may override externalIO(), open() and/or
close().
Example for a subclass:
Default case (external module reads from stdin and writes to stdout without requiring any particular triggers):
public class Intonation extends ExternalModule {
public Intonation() {
super("Intonation", System.getProperty("mary.base") + File.separator + "src" + File.separator + "modules"
+ File.separator + "intonation" + File.separator + "intonation_tts", MaryDataType.INTONISED);
}
}
Non-standard case (external module needs special trigger, data needs to be post-processed):
public class Tokeniser extends ExternalModule {
public Tokeniser()
{
super(...);
}
protected MaryData externalIO(MaryData d) throws TransformerConfigurationException, TransformerException,
FileNotFoundException, IOException, ParserConfigurationException, SAXException, Exception {
MaryData result;
// Write to and read from external module similarly to super class,
// but write e.g. an empty line after writing the data,
// to mark end of input.
// Modify the result tree
myModifications(result);
return result;
}
}
| Modifier and Type | Class and Description |
|---|---|
class |
ExternalModule.ProcessingThread |
class |
ExternalModule.RestarterThread |
| Modifier and Type | Field and Description |
|---|---|
protected StreamLogger |
errorLogger |
protected InputStream |
from |
protected String |
ignorePattern
A regular expression describing what to be ignored in the external module's standard error output.
|
protected org.apache.log4j.Logger |
logger
The logger instance to be used by this module.
|
protected Process |
process |
protected ExternalModule.ProcessingThread |
processingThread |
protected ExternalModule.RestarterThread |
restarterThread |
protected boolean |
retrying
Remember if a retry attempt is undertaken in
process(). |
protected int |
state |
protected long |
timeLimit
The duration given to the module before timeout occurs (in milliseconds).
|
protected OutputStream |
to |
MODULE_OFFLINE, MODULE_RUNNING| Modifier | Constructor and Description |
|---|---|
protected |
ExternalModule(String name,
String cmd,
MaryDataType inputType,
MaryDataType outputType,
Locale locale) |
| Modifier and Type | Method and Description |
|---|---|
protected void |
addRequest(Object r) |
protected void |
close()
Closes the external process's input and output streams, and destroys the process.
|
protected String |
cmd()
The command line to execute as an external process.
|
protected void |
doNotifyAll() |
protected void |
doWait() |
protected void |
doWait(long millis) |
protected boolean |
exitRequested() |
protected MaryData |
externalIO(MaryData d)
The actual external input and output.
|
protected InputStream |
from()
The stream on which data is read from the external process.
|
MaryDataType |
getInputType() |
Locale |
getLocale()
The locale of this module, i.e.
|
protected ExternalModuleRequest |
getNextRequest() |
MaryDataType |
getOutputType() |
protected Process |
getProcess()
Get the process object representing the external module program.
|
int |
getState()
Inform about the state of this module.
|
protected boolean |
haveWaitingRequests() |
MaryDataType |
inputType()
Deprecated.
|
String |
name()
This module's name, as free text, for example "Tokeniser"
|
protected boolean |
needToRestart() |
protected void |
open()
Execute the command
cmd as an external process. |
MaryDataType |
outputType()
Deprecated.
|
void |
powerOnSelfTest()
Perform a power-on self test by processing some example input data.
|
MaryData |
process(MaryData d)
Feed the input data into the external module, and return the result.
|
protected void |
removeRequest(ExternalModuleRequest r) |
protected void |
setCmd(String cmd)
Sets the command line to execute.
|
protected void |
setExitRequested(boolean b)
Tell all helper threads to exit.
|
protected void |
setNeedToRestart(boolean needToRestart) |
void |
shutdown()
Allow the module to shut down cleanly.
|
void |
startup()
Allow the module to start up, performing whatever is necessary to become operational.
|
protected OutputStream |
to()
The stream on which data is written to the external process.
|
protected int state
protected Process process
protected OutputStream to
protected InputStream from
protected StreamLogger errorLogger
protected ExternalModule.ProcessingThread processingThread
protected ExternalModule.RestarterThread restarterThread
protected org.apache.log4j.Logger logger
protected long timeLimit
protected boolean retrying
process().processprotected String ignorePattern
null.protected ExternalModule(String name, String cmd, MaryDataType inputType, MaryDataType outputType, Locale locale) throws NoSuchPropertyException
NoSuchPropertyExceptionprotected Process getProcess()
protected void open()
throws IOException
cmd as an external process. The process's input and output streams are accessible from
then on via the from() and to() methods; the process's error stream is logged by a separate
StreamLogger thread.IOException - IOExceptionto(),
from(),
StreamLoggerprotected void close()
protected OutputStream to()
protected InputStream from()
protected String cmd()
protected void setCmd(String cmd)
cmd - cmdpublic String name()
MaryModulename in interface MaryModule@Deprecated public MaryDataType inputType()
MaryModuleinputType in interface MaryModulepublic MaryDataType getInputType()
getInputType in interface MaryModule@Deprecated public MaryDataType outputType()
MaryModuleoutputType in interface MaryModulepublic MaryDataType getOutputType()
getOutputType in interface MaryModulepublic Locale getLocale()
MaryModulegetLocale in interface MaryModulepublic int getState()
MaryModulegetState in interface MaryModulepublic void startup()
throws Exception
MaryModulestartup in interface MaryModuleException - Exceptionpublic void powerOnSelfTest()
throws Error
powerOnSelfTest in interface MaryModuleError - if the module does not work properly.public void shutdown()
MaryModuleshutdown in interface MaryModuleprotected MaryData externalIO(MaryData d) throws TransformerConfigurationException, TransformerException, FileNotFoundException, IOException, ParserConfigurationException, SAXException, Exception
d - dTransformerConfigurationException - TransformerConfigurationExceptionTransformerException - TransformerExceptionFileNotFoundException - FileNotFoundExceptionIOException - IOExceptionParserConfigurationException - ParserConfigurationExceptionSAXException - SAXExceptionException - Exceptionpublic final MaryData process(MaryData d) throws TransformerConfigurationException, TransformerException, FileNotFoundException, IOException, ParserConfigurationException, SAXException, Exception
final. The actual input and output is
performed by externalIO() and may be overridden by subclasses to account for module-specifics.
If timeout occurs, the external module is restarted, and a second attempt is made. If it fails again, an IOException is thrown. Even in case of the second failure, the external process is restarted, because failure may have been provoked by this particular input.
For the time being, external modules are thread-safe simply by this method being synchronized.
process in interface MaryModuled - doutputType() encapsulating the processing result.Exception - ExceptionTransformerConfigurationExceptionTransformerExceptionFileNotFoundExceptionIOExceptionParserConfigurationExceptionSAXExceptionprotected void addRequest(Object r)
protected ExternalModuleRequest getNextRequest()
protected void removeRequest(ExternalModuleRequest r)
protected boolean haveWaitingRequests()
protected void setNeedToRestart(boolean needToRestart)
protected boolean needToRestart()
protected void doNotifyAll()
protected void doWait()
protected void doWait(long millis)
protected void setExitRequested(boolean b)
b - bprotected boolean exitRequested()
Copyright © 2000–2016 DFKI GmbH. All rights reserved.