Search in sources :

Example 6 with BSyncStatement

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

the class SimpleEventSelectionStrategyTest method testWithBlockingCase.

@Test
public void testWithBlockingCase() {
    BEvent expected = eventOne;
    Set<BSyncStatement> sets = new HashSet<>(Arrays.asList(BSyncStatement.make(null).request(eventOne), BSyncStatement.make(null).request(eventTwo), BSyncStatement.make(null).block(eventTwo)));
    assertEquals(singleton(expected), sut.selectableEvents(sets, Collections.emptyList()));
    assertEquals(Optional.of(new EventSelectionResult(expected)), sut.select(sets, Collections.emptyList(), singleton(expected)));
}
Also used : BSyncStatement(il.ac.bgu.cs.bp.bpjs.model.BSyncStatement) BEvent(il.ac.bgu.cs.bp.bpjs.model.BEvent) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 7 with BSyncStatement

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

the class SimpleEventSelectionStrategyTest method testUnanimousCase.

@Test
public void testUnanimousCase() {
    BEvent expected = eventOne;
    Set<BSyncStatement> sets = new HashSet<>(Arrays.asList(BSyncStatement.make(null).request(eventOne), BSyncStatement.make(null).request(eventOne).waitFor(eventTwo), BSyncStatement.make(null).request(eventOne)));
    assertEquals(singleton(expected), sut.selectableEvents(sets, Collections.emptyList()));
    assertEquals(Optional.of(new EventSelectionResult(expected)), sut.select(sets, Collections.emptyList(), singleton(eventOne)));
}
Also used : BSyncStatement(il.ac.bgu.cs.bp.bpjs.model.BSyncStatement) BEvent(il.ac.bgu.cs.bp.bpjs.model.BEvent) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 8 with BSyncStatement

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

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

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

the class BThreadJsProxy method bsync.

@Deprecated
public void bsync(NativeObject jsRWB, Object data) {
    if (!deprecationWarningPrinted) {
        deprecationWarningPrinted = true;
        System.err.println("Warning: bsync is deprecated and will be removed shortly. Please use bp.sync instead.");
    }
    Map<String, Object> jRWB = (Map) Context.jsToJava(jsRWB, Map.class);
    BSyncStatement stmt = BSyncStatement.make();
    Object req = jRWB.get("request");
    if (req != null) {
        if (req instanceof BEvent) {
            stmt = stmt.request((BEvent) req);
        } else if (req instanceof NativeArray) {
            NativeArray arr = (NativeArray) req;
            stmt = stmt.request(Arrays.asList(arr.getIndexIds()).stream().map(i -> (BEvent) arr.get(i)).collect(toList()));
        }
    }
    stmt = stmt.waitFor(convertToEventSet(jRWB.get("waitFor"))).block(convertToEventSet(jRWB.get("block"))).interrupt(convertToEventSet(jRWB.get("interrupt"))).data(data);
    captureBThreadState(stmt);
}
Also used : NativeArray(org.mozilla.javascript.NativeArray) BSyncStatement(il.ac.bgu.cs.bp.bpjs.model.BSyncStatement) NativeObject(org.mozilla.javascript.NativeObject) BEvent(il.ac.bgu.cs.bp.bpjs.model.BEvent) Map(java.util.Map)

Aggregations

BSyncStatement (il.ac.bgu.cs.bp.bpjs.model.BSyncStatement)11 BEvent (il.ac.bgu.cs.bp.bpjs.model.BEvent)9 ComposableEventSet (il.ac.bgu.cs.bp.bpjs.model.eventsets.ComposableEventSet)4 EventSet (il.ac.bgu.cs.bp.bpjs.model.eventsets.EventSet)4 EventSets (il.ac.bgu.cs.bp.bpjs.model.eventsets.EventSets)4 Collections.emptySet (java.util.Collections.emptySet)4 Collections.singleton (java.util.Collections.singleton)4 List (java.util.List)4 Set (java.util.Set)4 Collectors.toSet (java.util.stream.Collectors.toSet)4 HashSet (java.util.HashSet)3 Map (java.util.Map)3 Test (org.junit.Test)3 Context (org.mozilla.javascript.Context)3 Collectors (java.util.stream.Collectors)2 NativeArray (org.mozilla.javascript.NativeArray)2 NativeObject (org.mozilla.javascript.NativeObject)2 BProgramJsProxy (il.ac.bgu.cs.bp.bpjs.execution.jsproxy.BProgramJsProxy)1 BThreadJsProxy (il.ac.bgu.cs.bp.bpjs.execution.jsproxy.BThreadJsProxy)1 BThreadSyncSnapshot (il.ac.bgu.cs.bp.bpjs.model.BThreadSyncSnapshot)1