Search in sources :

Example 6 with BProgramSyncSnapshot

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

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

use of il.ac.bgu.cs.bp.bpjs.model.BProgramSyncSnapshot 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)

Example 9 with BProgramSyncSnapshot

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

the class BProgramSyncSnapshotClonerTest method testSerialization.

@Test
public void testSerialization() throws Exception {
    System.out.println("\nSTART Serialization test");
    BProgram bprog = new SingleResourceBProgram("BProgramSyncSnapshotClonerTest.js");
    BProgramSyncSnapshot cur = bprog.setup();
    ExecutorService exSvc = ExecutorServiceMaker.makeWithName("test");
    cur = cur.start(exSvc);
    cur.triggerEvent(cur.getStatements().stream().flatMap(s -> s.getRequest().stream()).findFirst().get(), exSvc, emptySet());
    BProgramSyncSnapshotIO io = new BProgramSyncSnapshotIO(bprog);
    byte[] out = io.serialize(cur);
    System.out.println("de-serializing\n");
    io.deserialize(out);
    System.out.println("END Serialization test\n");
}
Also used : BProgramRunner(il.ac.bgu.cs.bp.bpjs.execution.BProgramRunner) VerificationResult(il.ac.bgu.cs.bp.bpjs.analysis.VerificationResult) SingleResourceBProgram(il.ac.bgu.cs.bp.bpjs.model.SingleResourceBProgram) Collections.emptySet(java.util.Collections.emptySet) BProgram(il.ac.bgu.cs.bp.bpjs.model.BProgram) DfsBProgramVerifier(il.ac.bgu.cs.bp.bpjs.analysis.DfsBProgramVerifier) BProgramSyncSnapshot(il.ac.bgu.cs.bp.bpjs.model.BProgramSyncSnapshot) PrintBProgramRunnerListener(il.ac.bgu.cs.bp.bpjs.execution.listeners.PrintBProgramRunnerListener) Test(org.junit.Test) InMemoryEventLoggingListener(il.ac.bgu.cs.bp.bpjs.execution.listeners.InMemoryEventLoggingListener) ExecutorService(java.util.concurrent.ExecutorService) ExecutorServiceMaker(il.ac.bgu.cs.bp.bpjs.internal.ExecutorServiceMaker) SingleResourceBProgram(il.ac.bgu.cs.bp.bpjs.model.SingleResourceBProgram) BProgram(il.ac.bgu.cs.bp.bpjs.model.BProgram) SingleResourceBProgram(il.ac.bgu.cs.bp.bpjs.model.SingleResourceBProgram) ExecutorService(java.util.concurrent.ExecutorService) BProgramSyncSnapshot(il.ac.bgu.cs.bp.bpjs.model.BProgramSyncSnapshot) Test(org.junit.Test)

Example 10 with BProgramSyncSnapshot

use of il.ac.bgu.cs.bp.bpjs.model.BProgramSyncSnapshot 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();
    }
}
Also used : BProgram(il.ac.bgu.cs.bp.bpjs.model.BProgram) BProgramSyncSnapshot(il.ac.bgu.cs.bp.bpjs.model.BProgramSyncSnapshot) EventSelectionStrategy(il.ac.bgu.cs.bp.bpjs.model.eventselection.EventSelectionStrategy) Set(java.util.Set) ArrayList(java.util.ArrayList) List(java.util.List) FailedAssertion(il.ac.bgu.cs.bp.bpjs.model.FailedAssertion) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Collections.reverseOrder(java.util.Collections.reverseOrder) Optional(java.util.Optional) BEvent(il.ac.bgu.cs.bp.bpjs.model.BEvent) BProgramRunnerListener(il.ac.bgu.cs.bp.bpjs.execution.listeners.BProgramRunnerListener) EventSelectionResult(il.ac.bgu.cs.bp.bpjs.model.eventselection.EventSelectionResult) ExecutorService(java.util.concurrent.ExecutorService) ExecutorServiceMaker(il.ac.bgu.cs.bp.bpjs.internal.ExecutorServiceMaker) EventSelectionResult(il.ac.bgu.cs.bp.bpjs.model.eventselection.EventSelectionResult) ArrayList(java.util.ArrayList) BProgramSyncSnapshot(il.ac.bgu.cs.bp.bpjs.model.BProgramSyncSnapshot) BEvent(il.ac.bgu.cs.bp.bpjs.model.BEvent)

Aggregations

BProgramSyncSnapshot (il.ac.bgu.cs.bp.bpjs.model.BProgramSyncSnapshot)11 BProgram (il.ac.bgu.cs.bp.bpjs.model.BProgram)10 BThreadSyncSnapshot (il.ac.bgu.cs.bp.bpjs.model.BThreadSyncSnapshot)9 StringBProgram (il.ac.bgu.cs.bp.bpjs.model.StringBProgram)8 Test (org.junit.Test)8 NativeContinuation (org.mozilla.javascript.NativeContinuation)7 BEvent (il.ac.bgu.cs.bp.bpjs.model.BEvent)5 ExecutorServiceMaker (il.ac.bgu.cs.bp.bpjs.internal.ExecutorServiceMaker)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ArrayList (java.util.ArrayList)2 ExecutorService (java.util.concurrent.ExecutorService)2 Context (org.mozilla.javascript.Context)2 ScriptableInputStream (org.mozilla.javascript.serialize.ScriptableInputStream)2 DfsBProgramVerifier (il.ac.bgu.cs.bp.bpjs.analysis.DfsBProgramVerifier)1 VerificationResult (il.ac.bgu.cs.bp.bpjs.analysis.VerificationResult)1 BProgramRunner (il.ac.bgu.cs.bp.bpjs.execution.BProgramRunner)1 BProgramJsProxy (il.ac.bgu.cs.bp.bpjs.execution.jsproxy.BProgramJsProxy)1 BProgramRunnerListener (il.ac.bgu.cs.bp.bpjs.execution.listeners.BProgramRunnerListener)1 InMemoryEventLoggingListener (il.ac.bgu.cs.bp.bpjs.execution.listeners.InMemoryEventLoggingListener)1 PrintBProgramRunnerListener (il.ac.bgu.cs.bp.bpjs.execution.listeners.PrintBProgramRunnerListener)1