Search in sources :

Example 1 with BSyncStatement

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

the class SimpleEventSelectionStrategyTest method testDeadlock.

@Test
public void testDeadlock() {
    ExplicitEventSet setOfEvt2 = new ExplicitEventSet();
    setOfEvt2.add(eventTwo);
    Set<BSyncStatement> sets = new HashSet<>(Arrays.asList(BSyncStatement.make(null).request(eventOne), BSyncStatement.make(null).request(setOfEvt2), BSyncStatement.make(null).block(eventTwo), BSyncStatement.make(null).block(eventOne)));
    assertEquals(emptySet(), sut.selectableEvents(sets, Collections.emptyList()));
    assertEquals(Optional.empty(), sut.select(sets, Collections.emptyList(), emptySet()));
}
Also used : BSyncStatement(il.ac.bgu.cs.bp.bpjs.model.BSyncStatement) ExplicitEventSet(il.ac.bgu.cs.bp.bpjs.model.eventsets.ExplicitEventSet) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 2 with BSyncStatement

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

the class OrderedEventSelectionStrategy 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).map(stmt -> (BEvent) (stmt.getRequest().stream().filter(e -> !blocked.contains(e)).findFirst().orElse(null))).filter(e -> e != null).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 3 with BSyncStatement

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

the class PrioritizedBThreadsEventSelectionStrategy 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));
    }
    final EventSet blocked = ComposableEventSet.anyOf(statements.stream().filter(stmt -> stmt != null).map(BSyncStatement::getBlock).filter(r -> r != EventSets.none).collect(toSet()));
    Set<Pair<BEvent, Integer>> requested = statements.stream().filter(stmt -> stmt != null).flatMap(stmt -> eventsToPrioritizedPairs(stmt)).collect(Collectors.toSet());
    // Let's see what internal events are requested and not blocked (if any).
    try {
        Context.enter();
        Set<Pair<BEvent, Integer>> requestedAndNotBlockedWithPriorities = requested.stream().filter(req -> !blocked.contains(req.getLeft())).collect(toSet());
        Integer highestPriority = requestedAndNotBlockedWithPriorities.stream().map(p -> p.getRight()).max(Integer::max).get();
        Set<BEvent> requestedAndNotBlocked = requestedAndNotBlockedWithPriorities.stream().filter(p -> p.getRight().intValue() == highestPriority.intValue()).map(p -> p.getLeft()).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 : Collections.emptySet(java.util.Collections.emptySet) Context(org.mozilla.javascript.Context) BSyncStatement(il.ac.bgu.cs.bp.bpjs.model.BSyncStatement) Collection(java.util.Collection) Set(java.util.Set) HashMap(java.util.HashMap) Collectors(java.util.stream.Collectors) List(java.util.List) Stream(java.util.stream.Stream) Collections.singleton(java.util.Collections.singleton) Pair(org.apache.commons.lang3.tuple.Pair) Map(java.util.Map) 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) 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) Pair(org.apache.commons.lang3.tuple.Pair)

Example 4 with BSyncStatement

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

the class BProgramJsProxy method sync.

public void sync(NativeObject jsRWB, Object data) {
    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)

Example 5 with BSyncStatement

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

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