use of il.ac.bgu.cs.bp.bpjs.model.eventsets.EventSet 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.eventsets.EventSet 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.eventsets.EventSet 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.eventsets.EventSet 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.eventsets.EventSet in project BPjs by bThink-BGU.
the class AddingBthreadsTest method superStepTest.
@Test
public void superStepTest() throws InterruptedException {
final BEvent parentDone = new BEvent("parentDone");
final BEvent kidADone = new BEvent("kidADone");
final BEvent kidBDone = new BEvent("kidBDone");
BProgramRunner sut = new BProgramRunner(new SingleResourceBProgram("AddingBthreads.js"));
sut.addListener(new PrintBProgramRunnerListener());
InMemoryEventLoggingListener eventLogger = sut.addListener(new InMemoryEventLoggingListener());
sut.run();
EventSet kiddies = il.ac.bgu.cs.bp.bpjs.model.eventsets.ComposableEventSet.anyOf(kidADone, kidBDone);
EventPattern expected = new EventPattern().append(kiddies).append(kiddies).append(parentDone).append(kiddies).append(kiddies).append(parentDone);
System.out.println("Actual events:");
eventLogger.getEvents().forEach(System.out::println);
System.out.println("/Actual events");
assertTrue(expected.matches(eventLogger.getEvents()));
}
Aggregations