Search in sources :

Example 1 with BEvent

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

the class EventSelectionResultTest method testEquality.

@Test
public void testEquality() {
    EventSelectionResult esrA = new EventSelectionResult(new BEvent("a"));
    EventSelectionResult esrA1 = new EventSelectionResult(new BEvent("a"), new HashSet<>(Arrays.asList(1)));
    EventSelectionResult esrA2 = new EventSelectionResult(new BEvent("a"), new HashSet<>(Arrays.asList(2)));
    EventSelectionResult esrB = new EventSelectionResult(new BEvent("b"));
    assertEquals(esrA, esrA);
    assertNotEquals(esrA, esrB);
    assertNotEquals(esrA, esrA1);
    assertNotEquals(esrA2, esrA1);
    assertNotEquals(esrA, null);
    assertNotEquals(esrA, "hello");
    Set<EventSelectionResult> ess = new HashSet<>();
    ess.add(esrA);
    ess.add(esrA);
    ess.add(esrA1);
    ess.add(esrA1);
    ess.add(esrA2);
    ess.add(esrA2);
    assertEquals(3, ess.size());
}
Also used : BEvent(il.ac.bgu.cs.bp.bpjs.model.BEvent) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 2 with BEvent

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

the class EventSetsTest method testAllExcept.

/**
 * Test of allExcept method, of class EventSets.
 */
@Test
public void testAllExcept() {
    BEvent evtIn = new BEvent("In");
    BEvent evtOut = new BEvent("Out");
    EventSet es = EventSets.allExcept(evtOut);
    assertFalse(es.contains(evtOut));
    assertTrue(es.contains(evtIn));
}
Also used : BEvent(il.ac.bgu.cs.bp.bpjs.model.BEvent) Test(org.junit.Test)

Example 3 with BEvent

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

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

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

the class DynamicBThreadAdditionTest method test.

@Test
public void test() throws InterruptedException {
    BProgramRunner sut = new BProgramRunner(new SingleResourceBProgram("dynamicBthreadAddition.js"));
    sut.addListener(new PrintBProgramRunnerListener());
    InMemoryEventLoggingListener eventLogger = sut.addListener(new InMemoryEventLoggingListener());
    sut.run();
    Set<String> expected = new HashSet<>(Arrays.asList("e0", "e1", "e2", "e3"));
    assertEquals(expected, eventLogger.getEvents().stream().map(BEvent::getName).collect(toSet()));
}
Also used : PrintBProgramRunnerListener(il.ac.bgu.cs.bp.bpjs.execution.listeners.PrintBProgramRunnerListener) BProgramRunner(il.ac.bgu.cs.bp.bpjs.execution.BProgramRunner) 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) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

BEvent (il.ac.bgu.cs.bp.bpjs.model.BEvent)37 Test (org.junit.Test)25 SingleResourceBProgram (il.ac.bgu.cs.bp.bpjs.model.SingleResourceBProgram)16 BProgramRunner (il.ac.bgu.cs.bp.bpjs.execution.BProgramRunner)13 InMemoryEventLoggingListener (il.ac.bgu.cs.bp.bpjs.execution.listeners.InMemoryEventLoggingListener)13 PrintBProgramRunnerListener (il.ac.bgu.cs.bp.bpjs.execution.listeners.PrintBProgramRunnerListener)13 BSyncStatement (il.ac.bgu.cs.bp.bpjs.model.BSyncStatement)9 EventPattern (il.ac.bgu.cs.bp.bpjs.analysis.eventpattern.EventPattern)8 BProgram (il.ac.bgu.cs.bp.bpjs.model.BProgram)8 BThreadSyncSnapshot (il.ac.bgu.cs.bp.bpjs.model.BThreadSyncSnapshot)7 StringBProgram (il.ac.bgu.cs.bp.bpjs.model.StringBProgram)6 Context (org.mozilla.javascript.Context)6 BProgramSyncSnapshot (il.ac.bgu.cs.bp.bpjs.model.BProgramSyncSnapshot)5 EventSet (il.ac.bgu.cs.bp.bpjs.model.eventsets.EventSet)5 HashSet (java.util.HashSet)5 List (java.util.List)5 Set (java.util.Set)5 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