Search in sources :

Example 26 with BEvent

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()));
}
Also used : PrintBProgramRunnerListener(il.ac.bgu.cs.bp.bpjs.execution.listeners.PrintBProgramRunnerListener) BProgramRunner(il.ac.bgu.cs.bp.bpjs.execution.BProgramRunner) SingleResourceBProgram(il.ac.bgu.cs.bp.bpjs.model.SingleResourceBProgram) InMemoryEventLoggingListener(il.ac.bgu.cs.bp.bpjs.execution.listeners.InMemoryEventLoggingListener) BEvent(il.ac.bgu.cs.bp.bpjs.model.BEvent) Test(org.junit.Test)

Example 27 with BEvent

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());
}
Also used : PrintBProgramRunnerListener(il.ac.bgu.cs.bp.bpjs.execution.listeners.PrintBProgramRunnerListener) BProgramRunner(il.ac.bgu.cs.bp.bpjs.execution.BProgramRunner) SingleResourceBProgram(il.ac.bgu.cs.bp.bpjs.model.SingleResourceBProgram) InMemoryEventLoggingListener(il.ac.bgu.cs.bp.bpjs.execution.listeners.InMemoryEventLoggingListener) BEvent(il.ac.bgu.cs.bp.bpjs.model.BEvent) Test(org.junit.Test)

Example 28 with BEvent

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());
    }
}
Also used : List(java.util.List) Collections.emptySet(java.util.Collections.emptySet) Collections.singleton(java.util.Collections.singleton) BSyncStatement(il.ac.bgu.cs.bp.bpjs.model.BSyncStatement) ComposableEventSet(il.ac.bgu.cs.bp.bpjs.model.eventsets.ComposableEventSet) Set(java.util.Set) EventSet(il.ac.bgu.cs.bp.bpjs.model.eventsets.EventSet) BEvent(il.ac.bgu.cs.bp.bpjs.model.BEvent) EventSets(il.ac.bgu.cs.bp.bpjs.model.eventsets.EventSets) OptionalInt(java.util.OptionalInt) Collectors(java.util.stream.Collectors) Collectors.toSet(java.util.stream.Collectors.toSet) ComposableEventSet(il.ac.bgu.cs.bp.bpjs.model.eventsets.ComposableEventSet) EventSet(il.ac.bgu.cs.bp.bpjs.model.eventsets.EventSet) OptionalInt(java.util.OptionalInt)

Example 29 with BEvent

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();
    }
}
Also used : List(java.util.List) Collections.emptySet(java.util.Collections.emptySet) Collections.singleton(java.util.Collections.singleton) Context(org.mozilla.javascript.Context) BSyncStatement(il.ac.bgu.cs.bp.bpjs.model.BSyncStatement) ComposableEventSet(il.ac.bgu.cs.bp.bpjs.model.eventsets.ComposableEventSet) Set(java.util.Set) EventSet(il.ac.bgu.cs.bp.bpjs.model.eventsets.EventSet) BEvent(il.ac.bgu.cs.bp.bpjs.model.BEvent) EventSets(il.ac.bgu.cs.bp.bpjs.model.eventsets.EventSets) Collectors.toSet(java.util.stream.Collectors.toSet) ComposableEventSet(il.ac.bgu.cs.bp.bpjs.model.eventsets.ComposableEventSet) EventSet(il.ac.bgu.cs.bp.bpjs.model.eventsets.EventSet) BEvent(il.ac.bgu.cs.bp.bpjs.model.BEvent)

Example 30 with BEvent

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();
    }
}
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

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