Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
Created: 7/7/2005
Modified: 6/26/2005 6:44 PM
Printed: 5/6/2017 11:14 AM
Sim1 to Sim2 Additions/Changes
Functional Changes:
Generic Simulation Manager
Command-Line Arguments
Reading input from a file
Logging output to a file
GUI Control for Simulator
New Files:
DefaultSimControl.java
SIM.INPUT
TextFileTokenizer.java
DefaultSimControl.java
EventLogger.java
SIM.INPUT
TextFileTokenizer.java
iSimControl.java
eventLog.dat (generated)
Files that HAVE changed:
Clock.java
Conveyor.java
Domain.java
FailMachine.java
InBuffer.java
Machine.java
OutBuffer.java
Robot.java
SimulationManager.java
Simulator.java
Files that have NOT changed:
ActiveSimulationException.java
ActiveSimulationObject.java
Buffer.java
EventCalendar.java
EventRecord.java
Expon.java
LimitedNormal.java
Normal.java
Part.java
SortedLinkedList.java
StdNormal.java
Uniform01.java
UniformAB.java
iConstants.java
iSimObjectIds.java
iSort.java
D:\769859120.doc
1 of 11
Created: 7/7/2005
Modified: 6/26/2005 6:44 PM
Printed: 5/6/2017 11:14 AM
sim2: Introduction of Command-Line Arguments:
reading, parsing, and executing
SimulationManager.java:
SimulationManger contains all of the source code to implement command-line arguments.
There is a call in SimulationManager:main(…) and an implementation method,
parseArguments(…), below
Command-line arguments allow the user to enter a small amount of input into a program as
the executable is called. Command-line arguments are a Unix capability that Java supports
on all of its platforms.
java SimulationManager -speed 3.1 -end 3000 –input SIM.INPUT –output cmLog
Java command-line arguments are similar to those in C and C++ in that each argument is a
string, and each string is an array element. As opposed to C and C++, however, the name of
the executable, in our case, SimulationManager, is not the first argument in the array. In the
example above “-speed” is array element zero.
In the example, there are eight argument in the args [..]
main (String[ ] args)
args [ ] = {“-speed”, “3.1”, “-end”, “3000”, -input SIM.INPUT, -output cmLog}
To use command-line arguments, the application program must define a method to interpret
the arguments.
Sim1 has been modified such that SimulationManager.java includes a method, void
parseArguments(String args[ ]). This method is called in SimulationManager’s main ( )
method.
Note: Be careful where a method such as parseArguments( ) is called with respect to default
values; implementing new values from the command line may overwrite needed values or
visa versa. Morevover, if a new value overrides the default value of an instance variable in
an object such as _speedFactor in an instance of Clock, be sure the instance has been
created!!
On line 59, of SimulationManager:main(String agrs[]), there is a call to look for commandline arguments
manager.parseArguments(args); //sim2 line 59
D:\769859120.doc
2 of 11
Created: 7/7/2005
Modified: 6/26/2005 6:44 PM
Printed: 5/6/2017 11:14 AM
SimulattionManager: parseArguments(String[] args) begins on line 113 or so
/**
* Method to process command-line arguments
*/
public void parseArguments(String[] args) //sim2 line 113
{
int i = 0;
String arg;
while (i < args.length && args[i].startsWith("-")) {
/**
* Get current argument, then increment
* counter so we can get its parameter
* if necessary.
*/
arg = args[i];
i++;
if (arg.equalsIgnoreCase("-noLogFile")) {
log = false;
} else if (arg.equalsIgnoreCase("-speed")) {
if (i < args.length && !args[i].startsWith("-")) {
speedFactor = (new Double(args[i])).doubleValue();
i++;
} else {
System.err.println("Usage: -speed speedFactor");
System.exit(-1);
}
} else if (arg.equalsIgnoreCase("-end")) {
if (i < args.length && !args[i].startsWith("-")) {
endTime = (new Integer(args[i])).intValue();
i++;
} else {
System.err.println("Usage: -end endTime (in seconds)");
System.exit(-1);
}
} else if (arg.equalsIgnoreCase("-output")) {
if (i < args.length && !args[i].startsWith("-")) {
logFile = args[i];
i++;
} else {
System.err.println("Usage: -output outputFile");
System.exit(-1);
}
} else if (arg.equalsIgnoreCase("-input")) {
if (i < args.length && !args[i].startsWith("-")) {
factoryFile = args[i];
i++;
D:\769859120.doc
3 of 11
Created: 7/7/2005
Modified: 6/26/2005 6:44 PM
Printed: 5/6/2017 11:14 AM
} else {
System.err.println("Usage: -input inputFile");
System.exit(-1);
}
} else if (arg.equalsIgnoreCase("-debug")) {
debug = true;
} else {
System.err.println("Usage:");
System.err.println(" java SimulationManager ");
System.err.println("
[-noLogFile]");
System.err.println("
[-speed speedFactor] ");
System.err.println("
[-end endTime] ");
System.err.println("
[-input inputFile] ");
System.err.println("
[-output outputFile]");
System.err.println("
[-debug]");
System.exit(-1);
}
}
} //173
The parseArguments(…) method provide re-usable templates for reading command-line
arguments, doing so one-by-one, where every argument must start with “-“, and as necessary,
read in a value for the argument. For example, the command-line argument,
-noLogFile,
is a standalone argument
Whereas, -speed, must be interpreted by looking for aother argument, i.e., token, that
immediately follows. The second argument is a value for speedFactor. Thus, the full
‘argument’ to set as speedFactor value is
-speed speedFactor
Follow the while loop, which determines the number of arguments, or tokens, to read. It reads
one token at a time, and, using a giant “ if/else if” structure to determine the arguments’
meanings. The parseArguments(…) method uses a nice language feature that enables users to
make case mistakes without a problem.
arg.equalsIgnoreCase(“-noLogFile”)
Extending the arguments that SimulationManager, or parseArguments(…) takes is
straightforward: Follow the logic, decide if your added argument requires one or two tokens,
and emulate existing examples.
D:\769859120.doc
4 of 11
Created: 7/7/2005
Modified: 6/26/2005 6:44 PM
Printed: 5/6/2017 11:14 AM
2. Logging to Output to a File
New Files: EventLogger.java, eventLog.dat (generated)
Changed Files: SimulationManager.java and Simulator.java
While it is quick and easy to write to standard output (the screen), it is not very professional.
If your computing is important, you want to write your results to a file in order to easily save
them. Surprisingly, most people, somewhat computer literate, do not know programming
procedures to enable them to read input from a file and write output to another file.
Many sim2 files, which comprise factory components, have also been modified slightly
modified to use
_Simulator.log( ActiveSimulationObject anASO) //see Simulator about line 720
and
_Simulator log(String msg); //see Simulator about line705
instead of
System.out.println( _Simulator.getTimeNow()/1000 + “aMsg”);
Thus, there are small changes in numerous files. These include
Conveyor.java
FailMachine.java
InBuffer.java
Machine.java
OutBuffer.java
Robot.java
For example, in Conveyor.java, there are small changes in three methods to add logging
capabilities
(1) Conveyor: init(OutBuffer, theOutBuffer) //line 42
public void init(OutBuffer theOutBuffer)
{
_outputBuffer = theOutBuffer;
_Simulator.log(this);
}
(2) Conveyor: loadPart(Part part) //line51
D:\769859120.doc
5 of 11
Created: 7/7/2005
Modified: 6/26/2005 6:44 PM
Printed: 5/6/2017 11:14 AM
{
long tnow = _Simulator.getTimeNow();
_parts.addElement(part); //add to conveyor Vector
_Simulator.log(part + " loaded on " + getName());
_Simulator.schedEvent(tnow + _conveyTime, this, "deliver");
}
(3) Conveyor: deliver() //line 62
{
Part part = (Part) _parts.firstElement();
_outputBuffer.addPart(part);
_Simulator.log(part.toString() + " delivered to output buffer.");
_parts.removeElementAt(0);
}
SimulationManager.java: includes references to an EventFile.
Simulator.java: includes the two loging methods.
D:\769859120.doc
6 of 11
Created: 7/7/2005
Modified: 6/26/2005 6:44 PM
Printed: 5/6/2017 11:14 AM
Sim2: Reading Input from a File
New Files: TextFileTokenizer.java and SIM.INPUT
Changed Files: Domain.java and SimulationManager.java
Note: Follow along as I introduce how creating a domain from reading a file procedes.
Domain.java:
Added methods to read the factory from SIM.INPUT
Step1. public static Domain createDomainFromFile(String filename) //line 110
throws IOException
/**
* Factory method to create a Domain object from an
* ASCII file that describes the objects in the
* domain.
*/
{
// first try to open the file and create the Tokenizer
TextFileTokenizer tft = TextFileTokenizer.CreateTokenizer(filename);
// read general domain information
String factoryName = readFactoryName(tft);
Domain domain = new Domain();
domain.fileName = fileName;
domain.buildFromFile(tft);
// close file, we're done
tft.close();
return domain;
}
Step 2. private void buildFromFile(TextFileTokenizer tft) //line136
throws IOException
/**
* Once an initial domain has been constructed, use this method
* to populate domain from the contents of a input
* description file.
*/
{
// read InputBuffer information
inputBuffer = readInputBufferInfo(tft);
// read Robot information
D:\769859120.doc
7 of 11
Created: 7/7/2005
Modified: 6/26/2005 6:44 PM
Printed: 5/6/2017 11:14 AM
mobileRobot = readRobotInfo(tft);
// read machine information
readMachineAndConveyorInfo(tft);
// read output buffer information
outputBuffer = readOutputBufferInfo(tft);
}
Step 3. public static String readFactoryName(TextFileTokenizer tft) //line 157
throws IOException
/**
* Reads domain name information from file.
*/
{
String name;
int token;
// expecting line of form
// Domain 'factory name'
tft.readExpectedStringToken("Domain");
name = tft.readStringToken();
return name;
} //readFactoryName(...)
Step 4.
private static int ReadAsoIdInfo(TextFileTokenizer tft) //line178
throws IOException
/**
* Read Aso Id Information
*/
{
tft.readExpectedStringToken("iSimObjectId");
String idString = tft.readStringToken();
for (int i=0; i < iSimObjectIds.ID_NAMES.length; i++)
if (iSimObjectIds.ID_NAMES[i].equals(idString) )
return i;
throw new IOException("Unable to find proper ID="+ idString);
}//end of ReadAsoIdInfo(...)
D:\769859120.doc
8 of 11
Created: 7/7/2005
Modified: 6/26/2005 6:44 PM
Printed: 5/6/2017 11:14 AM
And so on!!!
As with the preceding sim2 features, sim2 methods for reading variable values from an input
file, provide templates, which you can extend to meet your own needs.
TextFileTokenizer.java is the core, and applications programmers should only have to use its
functionality.
Domain.java contains a wide range of examples of how to create, both an input file and a
corresponding series of methods, using TextFileTokenizer.
Before quitting, let’s take a peak at the sim2 INPUT-FILE.
Before the data below, SIM-INPUT begins with a long explanation of the templates it uses to
read a variety of data. In a quick summary, the template has the following form:
!Factory
name
!
! ConversionToMS numberToUseToConvertTimeNumbersToMilliseconds
!
! NumProcessors
number
!
! BufferClassProc nameOfBuffer
! iSimObjectId
intID
! PartTypes
characterArray
! Capacity
int
! PartGeneration seed mean (if InputBuffer)
! EmptyTime
seed mean (if OutputBuffer)
Thus, if reading with methods, such as
Domain: public static String readFactoryName(TextFileTokenizer tft) //line157
throws IOException
the method expects to find to find “a line of the form, Domain ‘factor name’ //line163
{
String name;
int token;
// expecting line of form
// Domain 'factory name'
tft.readExpectedStringToken("Domain");
name = tft.readStringToken();
return name;
} //readFactoryName(...)
D:\769859120.doc
9 of 11
Created: 7/7/2005
Modified: 6/26/2005 6:44 PM
Printed: 5/6/2017 11:14 AM
Thus, the snapshot of SIM-INPUT below should make sense: it provides the factory name,
the conversion into milliseconds, the number of processors (machines & buffers), the
characteristics of each type of ‘processor:’ name, ID, part-types it can handle, capacity, and
part generation parameter values.
Factory
"Factory Simulation"
ConversionToMS
1000
NumProcessors
4
InputBuffer
"Input Buffer"
iSimObjectId
ID_INBUFFER
PartTypes
"L,M"
Capacity
20
PartGeneration 3141592
4.0
Machine
"Turning Lathe" LATHE
iSimObjectId
ID_LATHE
PartTypes
"L"
FailProbability 0.0
ProcessTime
2718281
6.0 1.5
Machine
"CNC Milling Machine" MILL
iSimObjectId
ID_MILL
PartTypes
"M"
FailProbability 0.5
ProcessTime
9323847
15.0 2.5
RepairTime
8465927
4.0 1.0
OutputBuffer
"Output Buffer"
iSimObjectId
ID_OUTBUFFER
PartTypes
"L,M"
Capacity
10
EmptyTime
8944321
4.0
NumMovers
3
……..
…….
D:\769859120.doc
10 of 11
Created: 7/7/2005
Modified: 6/26/2005 6:44 PM
Printed: 5/6/2017 11:14 AM
And Last, but not Least, DefaultSimControl Panel Design and Extensions
But I am sure you can wait until tomorrow for that!!
D:\769859120.doc
11 of 11