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