use of il.ac.bgu.cs.bp.bpjs.model.eventselection.EventSelectionResult in project BPjs by bThink-BGU.
the class BProgramRunner method run.
@Override
public void run() {
try {
// setup bprogram and runtime parts.
execSvc = ExecutorServiceMaker.makeWithName("BProgramRunner-" + instanceNum);
failedAssertion = null;
listeners.forEach(l -> l.starting(bprog));
BProgramSyncSnapshot cur = bprog.setup();
cur.getBThreadSnapshots().forEach(sn -> listeners.forEach(l -> l.bthreadAdded(bprog, sn)));
// start it
listeners.forEach(l -> l.started(bprog));
cur = cur.start(execSvc);
boolean go = true;
if (!cur.isStateValid()) {
failedAssertion = cur.getFailedAssertion();
listeners.forEach(l -> l.assertionFailed(bprog, failedAssertion));
go = false;
}
// while snapshot not empty, select an event and get the next snapshot.
while ((!cur.noBThreadsLeft()) && go) {
// see which events are selectable
Set<BEvent> possibleEvents = bprog.getEventSelectionStrategy().selectableEvents(cur.getStatements(), cur.getExternalEvents());
if (possibleEvents.isEmpty()) {
// No events available or selection. Terminate or wait for external one (in daemon mode).
if (bprog.isDaemonMode()) {
listeners.forEach(l -> l.superstepDone(bprog));
// and now we wait.
BEvent next = bprog.takeExternalEvent();
if (next == null) {
// program is not a daemon anymore.
go = false;
} else {
cur.getExternalEvents().add(next);
}
} else {
// Ending the program - no selectable event.
listeners.forEach(l -> l.superstepDone(bprog));
go = false;
}
} else {
// we can select some events - select one and advance.
Optional<EventSelectionResult> res = bprog.getEventSelectionStrategy().select(cur.getStatements(), cur.getExternalEvents(), possibleEvents);
if (res.isPresent()) {
EventSelectionResult esr = res.get();
if (!esr.getIndicesToRemove().isEmpty()) {
// the event selection affcted the external event queue.
List<BEvent> updatedExternals = new ArrayList<>(cur.getExternalEvents());
esr.getIndicesToRemove().stream().sorted(reverseOrder()).forEach(idxObj -> updatedExternals.remove(idxObj.intValue()));
cur = cur.copyWith(updatedExternals);
}
listeners.forEach(l -> l.eventSelected(bprog, esr.getEvent()));
cur = cur.triggerEvent(esr.getEvent(), execSvc, listeners);
if (!cur.isStateValid()) {
failedAssertion = cur.getFailedAssertion();
listeners.forEach(l -> l.assertionFailed(bprog, failedAssertion));
go = false;
}
} else {
// edge case: we can select events, but we didn't. Might be a bug in the EventSelectionStrategy.
go = false;
}
}
}
listeners.forEach(l -> l.ended(bprog));
} catch (InterruptedException itr) {
System.err.println("BProgramRunner interrupted: " + itr.getMessage());
} finally {
execSvc.shutdown();
}
}
Aggregations