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