use of il.ac.bgu.cs.bp.bpjs.model.BEvent in project BPjs by bThink-BGU.
the class ContinuationProgramStateTest method testLoopSameWhenNoVarChanges.
@Test
public void testLoopSameWhenNoVarChanges() throws Exception {
// Generate snapshot 1
BProgram bprog = new StringBProgram(SRC_LOOP);
BProgramSyncSnapshot cur = bprog.setup();
cur = cur.start(exSvc);
BThreadSyncSnapshot snapshot = cur.getBThreadSnapshots().iterator().next();
NativeContinuation nc = (NativeContinuation) snapshot.getContinuation();
ContinuationProgramState sutPre = new ContinuationProgramState(nc);
cur = cur.triggerEvent(new BEvent("e"), exSvc, emptySet());
snapshot = cur.getBThreadSnapshots().iterator().next();
nc = (NativeContinuation) snapshot.getContinuation();
ContinuationProgramState sutLoop1 = new ContinuationProgramState(nc);
// Generate more snapshots
for (int i = 0; i < 10; i++) {
cur = cur.triggerEvent(new BEvent("e"), exSvc, emptySet());
snapshot = cur.getBThreadSnapshots().iterator().next();
nc = (NativeContinuation) snapshot.getContinuation();
ContinuationProgramState sutCurLoop = new ContinuationProgramState(nc);
assertEquals(sutLoop1, sutCurLoop);
assertNotEquals(sutPre, sutCurLoop);
}
}
use of il.ac.bgu.cs.bp.bpjs.model.BEvent in project BPjs by bThink-BGU.
the class NodeEqualsTest method basicsTest.
@Test
public void basicsTest() throws Exception {
// Create a program
final BProgram bprog = new StringBProgram(P1);
Node[] nodes = new Node[10];
BEvent eventX = new BEvent("X");
// Discard initial node, as it has no event, and so can't
// be used in the even/odd equalities later.
nodes[0] = Node.getInitialNode(bprog, exSvc).getNextNode(eventX, exSvc);
assertTrue(nodes[0].equals(nodes[0]));
assertFalse(nodes[0].equals(null));
assertFalse(nodes[0].equals("Not a Node"));
}
use of il.ac.bgu.cs.bp.bpjs.model.BEvent in project BPjs by bThink-BGU.
the class NodeEqualsTest method test2.
@Test
public void test2() throws Exception {
final BProgram bprog = new SingleResourceBProgram("BPJSDiningPhil.js");
bprog.putInGlobalScope("PHILOSOPHER_COUNT", 5);
String[] events = { "Pick1R", "Pick2R", "Pick3R", "Pick4R", "Pick5R" };
Node[] nodes = new Node[events.length + 1];
nodes[0] = Node.getInitialNode(bprog, exSvc);
for (int i = 0; i < events.length; i++) {
nodes[i + 1] = nodes[i].getNextNode(new BEvent(events[i]), exSvc);
}
for (int i = 0; i < nodes.length; i++) {
for (int j = 0; j < nodes.length; j++) {
if (i != j) {
assertFalse("node " + i + " should not equal node " + j, nodes[i].equals(nodes[j]));
}
}
}
}
use of il.ac.bgu.cs.bp.bpjs.model.BEvent in project BPjs by bThink-BGU.
the class NodeEqualsTest method test1.
@Test
public void test1() throws Exception {
// Create a program
final BProgram bprog = new StringBProgram(P1);
Node[] nodes = new Node[10];
BEvent eventX = new BEvent("X");
// Discard initial node, as it has no event, and so can't
// be used in the even/odd equalities later.
nodes[0] = Node.getInitialNode(bprog, exSvc).getNextNode(eventX, exSvc);
for (int i = 1; i < 10; i++) {
nodes[i] = nodes[i - 1].getNextNode(eventX, exSvc);
}
for (int i = 1; i < 10; i += 2) {
final BThreadSyncSnapshot sysState0 = nodes[0].getSystemState().getBThreadSnapshots().iterator().next();
final BThreadSyncSnapshot sysStatei_1 = nodes[i - 1].getSystemState().getBThreadSnapshots().iterator().next();
assertTrue(sysState0.equals(sysStatei_1));
assertEquals(nodes[0], nodes[i - 1]);
assertEquals(nodes[1], nodes[i]);
}
}
use of il.ac.bgu.cs.bp.bpjs.model.BEvent in project BPjs by bThink-BGU.
the class AbstractEventSelectionStrategy method select.
/**
* Randomly select an event from {@code selectableEvents}, or a non-blocked event from {@code externalEvents}, in case {@code selectableEvents} is empty.
*
* @param statements Statements at the current {@code bsync}.
* @param externalEvents
* @param selectableEvents
* @return An optional event selection result.
*/
@Override
public Optional<EventSelectionResult> select(Set<BSyncStatement> statements, List<BEvent> externalEvents, Set<BEvent> selectableEvents) {
if (selectableEvents.isEmpty()) {
return Optional.empty();
}
BEvent chosen = new ArrayList<>(selectableEvents).get(rnd.nextInt(selectableEvents.size()));
Set<BEvent> requested = statements.stream().filter((BSyncStatement stmt) -> stmt != null).flatMap((BSyncStatement stmt) -> stmt.getRequest().stream()).collect(Collectors.toSet());
if (requested.contains(chosen)) {
return Optional.of(new EventSelectionResult(chosen));
} else {
// that was an external event, need to find the first index
return Optional.of(new EventSelectionResult(chosen, singleton(externalEvents.indexOf(chosen))));
}
}
Aggregations