Search in sources :

Example 1 with EventSet

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();
    }
}
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 2 with EventSet

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();
    }
}
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 3 with EventSet

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());
    }
}
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 4 with EventSet

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();
    }
}
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 5 with EventSet

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()));
}
Also used : PrintBProgramRunnerListener(il.ac.bgu.cs.bp.bpjs.execution.listeners.PrintBProgramRunnerListener) EventPattern(il.ac.bgu.cs.bp.bpjs.analysis.eventpattern.EventPattern) BProgramRunner(il.ac.bgu.cs.bp.bpjs.execution.BProgramRunner) EventSet(il.ac.bgu.cs.bp.bpjs.model.eventsets.EventSet) SingleResourceBProgram(il.ac.bgu.cs.bp.bpjs.model.SingleResourceBProgram) InMemoryEventLoggingListener(il.ac.bgu.cs.bp.bpjs.execution.listeners.InMemoryEventLoggingListener) BEvent(il.ac.bgu.cs.bp.bpjs.model.BEvent) Test(org.junit.Test)

Aggregations

BEvent (il.ac.bgu.cs.bp.bpjs.model.BEvent)5 EventSet (il.ac.bgu.cs.bp.bpjs.model.eventsets.EventSet)5 BSyncStatement (il.ac.bgu.cs.bp.bpjs.model.BSyncStatement)4 ComposableEventSet (il.ac.bgu.cs.bp.bpjs.model.eventsets.ComposableEventSet)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 Context (org.mozilla.javascript.Context)3 Collectors (java.util.stream.Collectors)2 EventPattern (il.ac.bgu.cs.bp.bpjs.analysis.eventpattern.EventPattern)1 BProgramRunner (il.ac.bgu.cs.bp.bpjs.execution.BProgramRunner)1 InMemoryEventLoggingListener (il.ac.bgu.cs.bp.bpjs.execution.listeners.InMemoryEventLoggingListener)1 PrintBProgramRunnerListener (il.ac.bgu.cs.bp.bpjs.execution.listeners.PrintBProgramRunnerListener)1 SingleResourceBProgram (il.ac.bgu.cs.bp.bpjs.model.SingleResourceBProgram)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1