Search in sources :

Example 16 with BEvent

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);
    }
}
Also used : BProgram(il.ac.bgu.cs.bp.bpjs.model.BProgram) StringBProgram(il.ac.bgu.cs.bp.bpjs.model.StringBProgram) NativeContinuation(org.mozilla.javascript.NativeContinuation) BProgramSyncSnapshot(il.ac.bgu.cs.bp.bpjs.model.BProgramSyncSnapshot) BEvent(il.ac.bgu.cs.bp.bpjs.model.BEvent) BThreadSyncSnapshot(il.ac.bgu.cs.bp.bpjs.model.BThreadSyncSnapshot) StringBProgram(il.ac.bgu.cs.bp.bpjs.model.StringBProgram) Test(org.junit.Test)

Example 17 with BEvent

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"));
}
Also used : SingleResourceBProgram(il.ac.bgu.cs.bp.bpjs.model.SingleResourceBProgram) BProgram(il.ac.bgu.cs.bp.bpjs.model.BProgram) StringBProgram(il.ac.bgu.cs.bp.bpjs.model.StringBProgram) BEvent(il.ac.bgu.cs.bp.bpjs.model.BEvent) StringBProgram(il.ac.bgu.cs.bp.bpjs.model.StringBProgram) Test(org.junit.Test)

Example 18 with BEvent

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]));
            }
        }
    }
}
Also used : SingleResourceBProgram(il.ac.bgu.cs.bp.bpjs.model.SingleResourceBProgram) BProgram(il.ac.bgu.cs.bp.bpjs.model.BProgram) StringBProgram(il.ac.bgu.cs.bp.bpjs.model.StringBProgram) SingleResourceBProgram(il.ac.bgu.cs.bp.bpjs.model.SingleResourceBProgram) BEvent(il.ac.bgu.cs.bp.bpjs.model.BEvent) Test(org.junit.Test)

Example 19 with BEvent

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]);
    }
}
Also used : SingleResourceBProgram(il.ac.bgu.cs.bp.bpjs.model.SingleResourceBProgram) BProgram(il.ac.bgu.cs.bp.bpjs.model.BProgram) StringBProgram(il.ac.bgu.cs.bp.bpjs.model.StringBProgram) BEvent(il.ac.bgu.cs.bp.bpjs.model.BEvent) BThreadSyncSnapshot(il.ac.bgu.cs.bp.bpjs.model.BThreadSyncSnapshot) StringBProgram(il.ac.bgu.cs.bp.bpjs.model.StringBProgram) Test(org.junit.Test)

Example 20 with BEvent

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))));
    }
}
Also used : BSyncStatement(il.ac.bgu.cs.bp.bpjs.model.BSyncStatement) BEvent(il.ac.bgu.cs.bp.bpjs.model.BEvent)

Aggregations

BEvent (il.ac.bgu.cs.bp.bpjs.model.BEvent)37 Test (org.junit.Test)25 SingleResourceBProgram (il.ac.bgu.cs.bp.bpjs.model.SingleResourceBProgram)16 BProgramRunner (il.ac.bgu.cs.bp.bpjs.execution.BProgramRunner)13 InMemoryEventLoggingListener (il.ac.bgu.cs.bp.bpjs.execution.listeners.InMemoryEventLoggingListener)13 PrintBProgramRunnerListener (il.ac.bgu.cs.bp.bpjs.execution.listeners.PrintBProgramRunnerListener)13 BSyncStatement (il.ac.bgu.cs.bp.bpjs.model.BSyncStatement)9 EventPattern (il.ac.bgu.cs.bp.bpjs.analysis.eventpattern.EventPattern)8 BProgram (il.ac.bgu.cs.bp.bpjs.model.BProgram)8 BThreadSyncSnapshot (il.ac.bgu.cs.bp.bpjs.model.BThreadSyncSnapshot)7 StringBProgram (il.ac.bgu.cs.bp.bpjs.model.StringBProgram)6 Context (org.mozilla.javascript.Context)6 BProgramSyncSnapshot (il.ac.bgu.cs.bp.bpjs.model.BProgramSyncSnapshot)5 EventSet (il.ac.bgu.cs.bp.bpjs.model.eventsets.EventSet)5 HashSet (java.util.HashSet)5 List (java.util.List)5 Set (java.util.Set)5 ComposableEventSet (il.ac.bgu.cs.bp.bpjs.model.eventsets.ComposableEventSet)4 EventSets (il.ac.bgu.cs.bp.bpjs.model.eventsets.EventSets)4 Collections.emptySet (java.util.Collections.emptySet)4