Java Reference
In-Depth Information
public void
subscribe(DiscreteProcess s, String event,
DiscreteProcess p) {
// Process "s" subscribes only to events activated
// after this subscription
subscriptions.add(
new
Subscription(s, event, p));
}
public void
activate(DiscreteProcess p, String event,
long
lifetime, Object obj){
// Publisher "p" activates "event" that will be raised
// after "lifeTime" seconds
eventQueue.add(
new
DiscreteEvent(event,
currentTime
!
lifetime, p.name, obj));
Collections.sort(eventQueue);
}
public void
next() {
// the GUI invokes this method to process the next event
while
(eventQueue.size() > 0) {
DiscreteEvent mostRecentEvent
#
(DiscreteEvent) eventQueue.remove(0);
currentTime
#
mostRecentEvent.raiseTime;
for
(
int
i
#
0; i < subscriptions.size(); i
!!
)
((Subscription) subscriptions.get(i)).
notifyEvent(mostRecentEvent);
}
}
}
Class
PieceSource
implements the finite state machine of the inventory
system that loads raw pieces into the machines' input buffers. The current
version does not deal with the situation when the input buffers get full. It
generates new
LOADED
events at random time instants. The constructor
initializes the process name, the piece type and the loading rate, that is the
maximum number of pieces loaded per minute. The minimum rate is set to
half of the maximum rate. The actual rate is computed as a random number
between the minimum and maximum rate. An event's lifetime is computed
as the inverse of the actual loading rate.
package
workcell;
public class
PieceSource
extends
DiscreteProcess {
int
numPieces
#
0; // the total number of loaded pieces
String pieceType; // type of piece (nut or screw)
double
maxRate; // max number of pieces per minute
// (in millisecs)
public
PieceSource(String name, String type,
DiscreteClock clock,
double
rate) {
super
(name, clock);
this
.pieceType
#
type;