76 package adInterfaceMCC.util;
78 import ptolemy.actor.TypedAtomicActor;
79 import ptolemy.actor.TypedIOPort;
80 import ptolemy.actor.lib.SequenceActor;
81 import ptolemy.data.DoubleToken;
82 import ptolemy.data.IntToken;
83 import ptolemy.data.Token;
84 import ptolemy.data.ArrayToken;
85 import ptolemy.data.expr.Parameter;
86 import ptolemy.data.type.BaseType;
87 import ptolemy.kernel.CompositeEntity;
88 import ptolemy.kernel.util.IllegalActionException;
89 import ptolemy.kernel.util.NameDuplicationException;
90 import ptolemy.data.type.ArrayType;
91 import ptolemy.data.BooleanToken;
92 import ptolemy.data.StringToken;
93 import ptolemy.data.expr.FileParameter;
94 import ptolemy.kernel.util.Workspace;
95 import ptolemy.kernel.util.Attribute;
97 import java.io.FileNotFoundException;
98 import java.io.IOException;
99 import java.io.BufferedReader;
100 import java.io.InputStreamReader;
101 import java.util.ArrayList;
102 import java.util.Arrays;
103 import org.xml.sax.SAXException;
104 import javax.xml.validation.Schema;
105 import javax.xml.parsers.ParserConfigurationException;
118 public abstract class ADInterfaceMCC extends TypedAtomicActor implements SequenceActor {
130 throws NameDuplicationException, IllegalActionException {
131 super(container, name);
135 errorSignal =
new TypedIOPort(
this,
"errorSignal",
false,
true);
136 errorSignal.setTypeEquals(BaseType.INT);
139 errorMessage =
new TypedIOPort(
this,
"errorMessage",
false,
true);
140 errorMessage.setTypeEquals(BaseType.STRING);
144 consoleArr =
new TypedIOPort(
this,
"consoleOutput",
false,
true);
145 consoleArr.setTypeEquals(
new ArrayType(BaseType.STRING));
149 new Parameter(configurationFile,
"allowFiles", BooleanToken.TRUE);
150 new Parameter(configurationFile,
"allowDirectories", BooleanToken.FALSE);
153 continueWhenError =
new Parameter(
this,
"continueWhenError",
new BooleanToken(
true));
154 continueWhenError.setTypeEquals(BaseType.BOOLEAN);
165 public Object
clone(Workspace workspace)
throws CloneNotSupportedException {
169 newObject.errorMessage.setTypeEquals(BaseType.STRING);
170 newObject.errorSignal.setTypeEquals(BaseType.INT);
171 newObject.consoleArr.setTypeEquals(
new ArrayType(BaseType.STRING));
172 newObject.configurationFile = (FileParameter) newObject
173 .getAttribute(
"configurationFile");
174 newObject.continueWhenError = (Parameter) newObject
175 .getAttribute(
"continueWhenError");
185 throws IllegalActionException {
189 super.attributeChanged(attribute);
232 checkOperatingSystem();
239 checkADInterfaceMCCStackCompilation();
258 nPro = prop_arr.size();
264 new ArrayList<String>(
nPro);
280 final File confile = configurationFile.asFile();
282 if(!confile.exists())
283 throw new IllegalActionException(
"The configuration file '"
284 + confile.getPath() +
285 "' cannot be found.");
287 catch(NullPointerException e)
290 if (e.getMessage() == null)
291 em =
"Failed to read configuration file in ADInterfaceMCCReader or ADInterfaceMCCWriter." +
LS +
292 "Check parameters of these actors.";
295 throw new IllegalActionException(em);
316 if(!schemaFile.exists())
317 throw new IllegalActionException(
"The schema file '" +
schemaFileName
318 +
"' cannot be found.");
327 void checkOperatingSystem() throws IllegalActionException
329 if(
osName.startsWith(
"Mac") || osName.startsWith(
"Lin") )
330 throw new IllegalActionException(
"This program only works on Windows."
331 +
LS +
"The operating system '" +
osName +
"' is not supported.");
341 void checkADInterfaceMCCStackCompilation() throws IllegalActionException
343 String adrp = adDevMan.getReadPropertyBinaryPath();
344 String adwp = adDevMan.getWritePropertyBinaryPath();
346 File fadrp =
new File(adrp);
347 File fadwp =
new File(adwp);
348 if(!fadrp.exists() || !fadwp.exists())
349 throw new IllegalActionException
350 (
"The ADInterfaceMCC-stack library has not been compiled."
351 +
LS +
"Please compile it.");
364 throws IllegalActionException
366 ArrayList<ADInterfaceMCCObjectType> confdata =
new ArrayList<ADInterfaceMCCObjectType>();
370 catch(SAXException e)
372 throw new IllegalActionException(e.getMessage());
374 catch(ParserConfigurationException e)
376 throw new IllegalActionException(e.getMessage());
380 throw new IllegalActionException(e.getMessage());
389 Object[] propval_arr_obj =
new String[
nPro];
390 String[] propval_arr_str = (String[])
propval_arr.toArray(propval_arr_obj);
391 String[] propval_arr_temp_str =
new String[propval_arr_obj.length];
392 System.arraycopy(propval_arr_str,0,propval_arr_temp_str,0,
nPro);
393 propval_arr_temp =
new ArrayList<String>(Arrays.asList(propval_arr_temp_str));
395 Object[] consout_arr_obj =
new String[
nPro];
396 String[] consout_arr_str = (String[])
consout_arr.toArray(consout_arr_obj);
397 String[] consout_arr_temp_str =
new String[consout_arr_obj.length];
398 System.arraycopy(consout_arr_str,0,consout_arr_temp_str,0,
nPro);
399 new ArrayList<String>(Arrays.asList(consout_arr_temp_str));
424 throws IllegalActionException
429 String infoline =
"";
433 for(
int i=0;
i<process_arr.size();
i++){
435 p = process_arr.get(
i).redirectErrorStream(
true).start();
439 String em =
"Error while executing processes."
440 +
LS + e.getMessage();
444 throw new IllegalActionException(em);
446 catch(NullPointerException e)
448 String em =
"Some of the command arguments are null."
449 +
LS + e.getMessage();
453 throw new IllegalActionException(em);
455 catch(IndexOutOfBoundsException e)
457 String em =
"The process list is empty." +
462 throw new IllegalActionException(em);
464 catch(SecurityException e)
466 String em =
"Creation of the process is not allowed."
467 +
LS + e.getMessage();
471 throw new IllegalActionException(em);
473 BufferedReader br =
new BufferedReader(
new InputStreamReader
474 (p.getInputStream()));
476 while((line=br.readLine())!=null)
478 allline = allline + line;
480 !line.contains(
"Error") && !line.contains(
"Invalid")
481 && !line.contains(
"Note")){
483 infoline = infoline + line;
487 String em =
"The response from ADInterfaceMCC is '" + line +
"'." +
LS;
491 throw new IllegalActionException(em);
495 propval_arr.add(propval_arr_temp.get(
i));
502 String em =
"Error while reading response." +
507 throw new IllegalActionException(em);
512 catch(InterruptedException e)
514 String em =
"The process of running adInterfaceMCC stack" +
LS
515 +
"command has been interrupted" +
LS +
520 throw new IllegalActionException(em);
522 consout_arr.add(allline);
524 propval_arr.add(infoline);
526 int exit = p.exitValue();
529 String em =
"ADInterfaceMCC-stack process quit abnormally.";
533 throw new IllegalActionException(em);
536 exitval_arr.add(exit);
538 consout_arr.trimToSize();
539 propval_arr.trimToSize();
540 exitval_arr.trimToSize();
548 Token[] valArray =
new Token[data_str.size()];
549 for(
int i=0;
i<data_str.size();
i++)
550 valArray[
i] =
new StringToken( data_str.get(
i) );
555 Token[] valArray =
new Token[data_str.size()];
556 for(
int i=0;
i<data_str.size();
i++)
557 valArray[
i] =
new DoubleToken(Double.valueOf(data_str.get(
i)));
590 errorSignal.send(0,
new IntToken(
_errsig));
591 errorMessage.send(0,
new StringToken(
_errmsg));
592 consoleArr.send(0,
new ArrayToken(BaseType.STRING,
consout_tok));
608 for(
int i=0;
i<ad_arr.size();
i++)
610 String apptag = ad_arr.get(
i).getApplicationTag();
611 adDevMan.validateApplicationTag(apptag);
627 catch(FileNotFoundException e)
629 throw new IllegalActionException(e.getMessage());
633 throw new IllegalActionException(e.getMessage());
635 catch (SAXException e) {
636 throw new IllegalActionException(e.getMessage());
705 private ArrayList<ADInterfaceMCCObjectType>
ad_arr;
715 protected ArrayList<ADInterfaceMCCObjectType>
prop_arr;
749 public final static String
FS = System.getProperty(
"file.separator");
752 public final static String
LS = System.getProperty(
"line.separator");
755 public final static String
bcvtbhome = System.getenv(
"BCVTB_HOME");
758 public final static String
osName= System.getProperty(
"os.name");
ArrayList< ADInterfaceMCCObjectType > readXMLConfigurationFile()
Read the xml configuration file, store the elements to ADInterfaceMCCObjectType ArrayList.
static final String bcvtbhome
String that points to root directory of the BCVTB.
ArrayList< ProcessBuilder > proc_arr
Arraylist containing processes to be executed in console.
void prepareTokensToSend()
Get tokens to send for the next timestep.
ArrayList< Integer > devins_arr
ArrayList containing device instance number read from configuration file.
ADInterfaceMCCDeviceManager adDevMan
The ADInterfaceMCC device manager.
ArrayList< String > propval_arr_temp
ArrayList temporarily holding property values read from previous time step.
TypedIOPort consoleArr
The port that send response from console.
TypedIOPort errorSignal
The port that outputs the error signal.
static final String LS
Line Seperator.
ArrayList< ADInterfaceMCCObjectType > ad_arr
ArrayList containing elements read from configuration file.
void validateSyntax()
Validate xml configuration file syntax.
Parameter errorMessage_tokenInitProduction
The rate parameter for the errorMessage port that declares the initial production.
ArrayList< String > propval_arr
ArrayList containing strings of property values read from device.
abstract void setSchemaFileName()
Abstract class that sets the schema file name.
ArrayList< ADInterfaceMCCObjectType > prop_arr
Arraylist containing ADInterfaceMCCObjectType read from configuration file.
Token[] propval_tok
Token array of property values.
String _errmsg
detailed error message
boolean _continueWhenError
Parameter to control whether to continue if there was an error message.
int _errsig
signal to indicate whether there is an error, 0 means right, 1 means wrong
String configurationFilePath
String of configuration file path.
void storeDataInLastTimeStep()
Store response from last time step.
static final String FS
File seperator.
void checkSchemaFileExists()
Check if schema file exists.
void initialize()
Initialization section, initialize variables and check possible errors.
Parameter errorMessage_tokenProductionRate
The rate parameter for the errorMessage port.
boolean throwExceptions()
Returns true if an exception should be thrown, or false if only an error message should be sent to th...
TypedIOPort errorMessage
The errorMessage port.
int nPro
Number of ADInterfaceMCC properties specified in the configuration file.
Parameter continueWhenError
Parameter to control whether to continue if there was an error message.
Object clone(Workspace workspace)
Clone the actor into the specified workspace.
String schemaFileName
Schema file path.
Token[] transformArrayListDouble(ArrayList< String > data_str)
This is the driver class in ADInterfaceMCC.
void checkConfFileExists()
Check if configuration file exists.
void executeProcess(ArrayList< ProcessBuilder > process_arr)
Execute Processes, store results to consoleoutput_arr and exitvalue-arr.
ArrayList< Integer > exitval_arr
ArrayList containing strings process exit values for each process.
void validateAttributes()
Validate application tag.
Token[] transformArrayListString(ArrayList< String > data_str)
Transform String Arraylist to Token Arrays.
Token[] consout_tok
Token array of console outputs.
void setConfigurationFilePath()
Set configuration file path.
FileParameter configurationFile
Parameter to specify the path for configuraton file.
ADInterfaceMCC(CompositeEntity container, String name)
Construct an actor with the given container and name.
void removeToken()
Remove tokens in previous time step.
This is an abstract base class for actors that generates an output stream.
void sendToken()
Sends the error signal to the errorSignal port, the error message to the errorMessage port...
int _firecount
parameter indicate how many times executeProcess function has been run
static final String osName
Name of the operating system.
ArrayList< String > consout_arr
ArrayList containing strings of response from device for ad reading and writing process.
int nDev
Number of ADInterfaceMCC devices specified in the configuration file.
void attributeChanged(Attribute attribute)
Override the base class to determine which argument is being used.