Actor[] actors = currentState.getRefinement();
getRefinementReferredInputPorts(currentState);
// Transfer additional inputs needed by the refinement.
for (int i = 0; i < inputPortList.size(); i++) {
IOPort port = (IOPort) inputPortList.get(i);
if (_refinementReferredInputPorts.contains(port)
&& !_referredInputPorts.contains(port)) {
super.transferInputs(port);
controller.readInputs();
_referredInputPorts.add(port);
}
}
// Fire the refinement.
if (actors != null) {
for (int i = 0; i < actors.length; ++i) {
if (_stopRequested) {
break;
}
if (actors[i].prefire()) {
actors[i].fire();
actors[i].postfire();
}
}
}
controller.readOutputsFromRefinement();
// Get inputs needed by the nonpreemptive transitions.
getNonpreemptiveTransitionsReferredInputPorts(currentState);
// Transfer additional inputs needed by the refinement.
for (int i = 0; i < inputPortList.size(); i++) {
IOPort port = (IOPort) inputPortList.get(i);
if (_nonpreemptiveTransitionsInputs.contains(port)
&& !_referredInputPorts.contains(port)) {
super.transferInputs(port);
controller.readInputs();
_referredInputPorts.add(port);
}
}
// Choose a nonpreemptive transition.
enabledTransitions = controller.enabledTransitions(currentState
.nonpreemptiveTransitionList());
// Ensure that if there are multiple enabled transitions, all of them
// must be nondeterministic.
if (enabledTransitions.size() > 1) {
Iterator transitions = enabledTransitions.iterator();
while (transitions.hasNext()) {
Transition transition = (Transition) transitions.next();
if (!transition.isNondeterministic()) {
throw new MultipleEnabledTransitionsException(
controller.currentState(),
"Multiple enabled transitions found but "
+ transition.getName()
+ " is deterministic.");
}
}
}
// Randomly choose one transition from the list of the
// enabled trnasitions.
length = enabledTransitions.size();
if (length != 0) {
// Since the size of the list of enabled transitions usually (almost
// always) is less than the maximum value of integer. We can safely
// do the cast from long to int in the following statement.
int randomChoice = (int) Math.floor(Math.random() * length);
// There is tiny chance that randomChoice equals length.
// When this happens, we deduct 1 from the randomChoice.
if (randomChoice == length) {
randomChoice--;
}
enabledTransition = (Transition) enabledTransitions
.get(randomChoice);
}
_enabledTransition = enabledTransition;
}
if (enabledTransition != null) {
// Get additional inputs needed for output actions and set actions
// of the enabled transition.
getOutputActionsReferredInputPorts(enabledTransition);
getSetActionsReferredInputPorts(enabledTransition);
for (int i = 0; i < inputPortList.size(); i++) {
IOPort port = (IOPort) inputPortList.get(i);
if (_outputActionReferredInputPorts.contains(port)
&& !_referredInputPorts.contains(port)) {
super.transferInputs(port);
controller.readInputs();
_referredInputPorts.add(port);
}
}
controller.readInputs();
// execute output actions.
Iterator actions = enabledTransition.choiceActionList().iterator();
while (actions.hasNext()) {
Action action = (Action) actions.next();
action.execute();
}
// Get additional input ports needed by set actions of
// the enabeld transition.
for (int i = 0; i < inputPortList.size(); i++) {
IOPort port = (IOPort) inputPortList.get(i);
if (_setActionReferredInputPorts.contains(port)
&& !_referredInputPorts.contains(port)) {
super.transferInputs(port);
controller.readInputs();