use of il.ac.bgu.cs.bp.bpjs.model.BEvent in project BPjs by bThink-BGU.
the class EventsArraysTest method testEventsWithData.
@Test
public void testEventsWithData() throws Exception {
BProgramRunner bpr = new BProgramRunner(new SingleResourceBProgram("EventArrays.js"));
bpr.addListener(new PrintBProgramRunnerListener());
InMemoryEventLoggingListener events = bpr.addListener(new InMemoryEventLoggingListener());
bpr.run();
assertEquals(Arrays.asList("e11", "e21"), events.getEvents().stream().map(BEvent::getName).collect(toList()));
}
use of il.ac.bgu.cs.bp.bpjs.model.BEvent in project BPjs by bThink-BGU.
the class InterruptTest method superStepTest.
@Test
public void superStepTest() throws InterruptedException {
BProgramRunner sut = new BProgramRunner(new SingleResourceBProgram("Interrupt.js"));
sut.addListener(new PrintBProgramRunnerListener());
InMemoryEventLoggingListener eventLogger = sut.addListener(new InMemoryEventLoggingListener());
sut.run();
eventLogger.getEvents().forEach(e -> System.out.println(e));
final BEvent breakingEvent = new BEvent("breaking");
assertEquals(Arrays.asList(breakingEvent, breakingEvent), eventLogger.getEvents());
}
use of il.ac.bgu.cs.bp.bpjs.model.BEvent in project BPjs by bThink-BGU.
the class PrioritizedBSyncEventSelectionStrategy method selectableEvents.
@Override
public Set<BEvent> selectableEvents(Set<BSyncStatement> statements, List<BEvent> externalEvents) {
EventSet blocked = ComposableEventSet.anyOf(statements.stream().filter(stmt -> stmt != null).map(BSyncStatement::getBlock).filter(r -> r != EventSets.none).collect(Collectors.toSet()));
OptionalInt maxValueOpt = statements.stream().filter(s -> !getRequestedAndNotBlocked(s, blocked).isEmpty()).mapToInt(this::getValue).max();
if (maxValueOpt.isPresent()) {
int maxValue = maxValueOpt.getAsInt();
return statements.stream().filter(s -> getValue(s) == maxValue).flatMap(s -> getRequestedAndNotBlocked(s, blocked).stream()).collect(toSet());
} else {
// Can't select any internal event, defer to the external, non-blocked ones.
return // No internal events requested, defer to externals.
externalEvents.stream().filter(e -> !blocked.contains(e)).findFirst().map(e -> singleton(e)).orElse(emptySet());
}
}
use of il.ac.bgu.cs.bp.bpjs.model.BEvent in project BPjs by bThink-BGU.
the class SimpleEventSelectionStrategy method selectableEvents.
@Override
public Set<BEvent> selectableEvents(Set<BSyncStatement> statements, List<BEvent> externalEvents) {
if (statements.isEmpty()) {
// Corner case, not sure this is even possible.
return externalEvents.isEmpty() ? emptySet() : singleton(externalEvents.get(0));
}
EventSet blocked = ComposableEventSet.anyOf(statements.stream().filter(stmt -> stmt != null).map(BSyncStatement::getBlock).filter(r -> r != EventSets.none).collect(toSet()));
Set<BEvent> requested = statements.stream().filter(stmt -> stmt != null).flatMap(stmt -> stmt.getRequest().stream()).collect(toSet());
// Let's see what internal events are requested and not blocked (if any).
try {
Context.enter();
Set<BEvent> requestedAndNotBlocked = requested.stream().filter(req -> !blocked.contains(req)).collect(toSet());
return requestedAndNotBlocked.isEmpty() ? // No internal events requested, defer to externals.
externalEvents.stream().filter(e -> !blocked.contains(e)).findFirst().map(e -> singleton(e)).orElse(emptySet()) : requestedAndNotBlocked;
} finally {
Context.exit();
}
}
use of il.ac.bgu.cs.bp.bpjs.model.BEvent in project BPjs by bThink-BGU.
the class ContinuationGames method main.
public static void main(String[] args) throws Exception {
// Create a program
BProgram bprog = new StringBProgram(SRC);
// Run the top-level code (b-threads are registered but not yet run)
BProgramSyncSnapshot cur = bprog.setup();
// Run to first bsync
cur = cur.start(ExecutorServiceMaker.makeWithName("TEST"));
// Get a snapshot
final BThreadSyncSnapshot snapshot = cur.getBThreadSnapshots().iterator().next();
System.out.println(snapshot);
// Serialize snapshot
byte[] serializedContinuationAndScope = null;
Object bp = null;
try {
// need Javascript environment for this, even though we're not executing code per se.
Context ctxt = Context.enter();
// first, get bp out of the scope
Object cnt = snapshot.getContinuation();
final Scriptable scope = snapshot.getScope();
for (Scriptable sc = scope; sc != null; sc = sc.getParentScope()) {
System.out.println("SCOPE START");
if (sc.has("bp", sc)) {
bp = sc.get("bp", sc);
sc.delete("bp");
System.out.println("bp deleted.");
}
if (sc.has("j", sc)) {
System.out.println("Found j:" + sc.get("j", sc));
}
System.out.println("SCOPE END\n");
}
// second, serialize
final Scriptable topLevelScope = ctxt.initSafeStandardObjects();
try (ByteArrayOutputStream bytes = new ByteArrayOutputStream();
ScriptableOutputStream outs = new ScriptableOutputStream(bytes, topLevelScope)) {
outs.writeObject(cnt);
outs.flush();
serializedContinuationAndScope = bytes.toByteArray();
}
System.out.println("Seriazlied to " + serializedContinuationAndScope.length + " bytes.");
} finally {
Context.exit();
}
// Run the BThread a few times:
try {
Context ctxt = ContextFactory.getGlobal().enterContext();
// must use interpreter mode
ctxt.setOptimizationLevel(-1);
final Scriptable topLevelScope = ctxt.initSafeStandardObjects();
for (int i = 0; i < 10; i++) {
try (ScriptableInputStream sis = new ScriptableInputStream(new ByteArrayInputStream(serializedContinuationAndScope), topLevelScope)) {
// read cnt and scope
Scriptable cnt2 = (Scriptable) sis.readObject();
// re-add bp to the scope
cnt2.getParentScope().put("bp", cnt2.getParentScope(), new BProgramJsProxy(bprog));
// go - we can push whichever event we want.
ctxt.resumeContinuation(cnt2, cnt2, new BEvent("e-" + i));
// this extra run will use the same control flow, but the variable
// values will be from the previous run. So don't do this :-)
ctxt.resumeContinuation(cnt2, cnt2, new BEvent("arbitrary/" + i));
}
}
// create three continuation objects that should be the same.
Scriptable[] cnts = new Scriptable[3];
for (int i = 0; i < 3; i++) {
try (ScriptableInputStream sis = new ScriptableInputStream(new ByteArrayInputStream(serializedContinuationAndScope), topLevelScope)) {
// read cnt and scope
cnts[i] = (Scriptable) sis.readObject();
}
}
System.out.println(cnts[0].equals(cnts[0]));
System.out.println(cnts[0].equals(cnts[1]));
System.out.println("continuationsEq = " + continuationEq((NativeContinuation) cnts[0], (NativeContinuation) cnts[1]));
} finally {
Context.exit();
}
}
Aggregations