Search in sources :

Example 6 with BThreadSyncSnapshot

use of il.ac.bgu.cs.bp.bpjs.model.BThreadSyncSnapshot in project BPjs by bThink-BGU.

the class ContinuationProgramStateTest method testStackHeight.

@Test
public void testStackHeight() throws Exception {
    // Generate a continuation
    BProgram bprog = new StringBProgram(SRC_SHORT);
    BProgramSyncSnapshot cur = bprog.setup();
    cur = cur.start(exSvc);
    BThreadSyncSnapshot snapshot = cur.getBThreadSnapshots().iterator().next();
    // Read frame data of P1
    NativeContinuation nc = (NativeContinuation) snapshot.getContinuation();
    ContinuationProgramState sut1 = new ContinuationProgramState(nc);
    assertEquals(0, sut1.getFrameIndex());
    bprog = new StringBProgram(SRC_MORE_FUNC);
    cur = bprog.setup();
    cur = cur.start(exSvc);
    snapshot = cur.getBThreadSnapshots().iterator().next();
    // Read frame data of P1
    nc = (NativeContinuation) snapshot.getContinuation();
    ContinuationProgramState sut2 = new ContinuationProgramState(nc);
    assertEquals(1, sut2.getFrameIndex());
}
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) BThreadSyncSnapshot(il.ac.bgu.cs.bp.bpjs.model.BThreadSyncSnapshot) StringBProgram(il.ac.bgu.cs.bp.bpjs.model.StringBProgram) Test(org.junit.Test)

Example 7 with BThreadSyncSnapshot

use of il.ac.bgu.cs.bp.bpjs.model.BThreadSyncSnapshot in project BPjs by bThink-BGU.

the class ContinuationProgramStateTest method testCorrectExtraction.

@Test
public void testCorrectExtraction() throws Exception {
    // Generate a continuation
    BProgram bprog = new StringBProgram(SRC);
    BProgramSyncSnapshot cur = bprog.setup();
    cur = cur.start(exSvc);
    final BThreadSyncSnapshot snapshot = cur.getBThreadSnapshots().iterator().next();
    // Read frame data
    NativeContinuation nc = (NativeContinuation) snapshot.getContinuation();
    ContinuationProgramState sut = new ContinuationProgramState(nc);
    assertEquals(0, sut.getFrameIndex());
    assertEquals("gVar content", sut.getVisibleVariables().get("gVar"));
    assertEquals("fVar content", sut.getVisibleVariables().get("fVar"));
    assertEquals("updated content", sut.getVisibleVariables().get("shadowed"));
    assertEquals(2.42, sut.getVisibleVariables().get("fDblVar2"));
}
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) BThreadSyncSnapshot(il.ac.bgu.cs.bp.bpjs.model.BThreadSyncSnapshot) StringBProgram(il.ac.bgu.cs.bp.bpjs.model.StringBProgram) Test(org.junit.Test)

Example 8 with BThreadSyncSnapshot

use of il.ac.bgu.cs.bp.bpjs.model.BThreadSyncSnapshot 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 9 with BThreadSyncSnapshot

use of il.ac.bgu.cs.bp.bpjs.model.BThreadSyncSnapshot in project BPjs by bThink-BGU.

the class ResumeBThreadTest method testToString.

/**
 * Test of toString method, of class ResumeBThread.
 */
@Test
public void testToString() {
    BEvent evt = new BEvent("evtName");
    ResumeBThread sut = new ResumeBThread(new BThreadSyncSnapshot("snap-name", null), evt, null);
    String toString = sut.toString();
    assertTrue(toString.startsWith("[ResumeBThread:"));
    assertTrue(toString.contains("snap-name"));
    assertTrue(toString.split("event:")[1].contains(evt.toString()));
}
Also used : BEvent(il.ac.bgu.cs.bp.bpjs.model.BEvent) BThreadSyncSnapshot(il.ac.bgu.cs.bp.bpjs.model.BThreadSyncSnapshot) Test(org.junit.Test)

Example 10 with BThreadSyncSnapshot

use of il.ac.bgu.cs.bp.bpjs.model.BThreadSyncSnapshot 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();
    }
}
Also used : Context(org.mozilla.javascript.Context) ScriptableInputStream(org.mozilla.javascript.serialize.ScriptableInputStream) ScriptableOutputStream(org.mozilla.javascript.serialize.ScriptableOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Scriptable(org.mozilla.javascript.Scriptable) BProgramJsProxy(il.ac.bgu.cs.bp.bpjs.execution.jsproxy.BProgramJsProxy) StringBProgram(il.ac.bgu.cs.bp.bpjs.model.StringBProgram) ByteArrayInputStream(java.io.ByteArrayInputStream) BProgram(il.ac.bgu.cs.bp.bpjs.model.BProgram) StringBProgram(il.ac.bgu.cs.bp.bpjs.model.StringBProgram) 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)

Aggregations

BThreadSyncSnapshot (il.ac.bgu.cs.bp.bpjs.model.BThreadSyncSnapshot)13 BProgram (il.ac.bgu.cs.bp.bpjs.model.BProgram)9 BProgramSyncSnapshot (il.ac.bgu.cs.bp.bpjs.model.BProgramSyncSnapshot)9 StringBProgram (il.ac.bgu.cs.bp.bpjs.model.StringBProgram)9 Test (org.junit.Test)9 BEvent (il.ac.bgu.cs.bp.bpjs.model.BEvent)7 NativeContinuation (org.mozilla.javascript.NativeContinuation)7 ByteArrayInputStream (java.io.ByteArrayInputStream)3 Context (org.mozilla.javascript.Context)3 ScriptableObject (org.mozilla.javascript.ScriptableObject)3 BProgramJsProxy (il.ac.bgu.cs.bp.bpjs.execution.jsproxy.BProgramJsProxy)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 Scriptable (org.mozilla.javascript.Scriptable)2 ScriptableInputStream (org.mozilla.javascript.serialize.ScriptableInputStream)2 BThreadJsProxy (il.ac.bgu.cs.bp.bpjs.execution.jsproxy.BThreadJsProxy)1 BSyncStatement (il.ac.bgu.cs.bp.bpjs.model.BSyncStatement)1 SingleResourceBProgram (il.ac.bgu.cs.bp.bpjs.model.SingleResourceBProgram)1 ObjectOutputStream (java.io.ObjectOutputStream)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1