All Downloads are FREE. Search and download functionalities are using the official Maven repository.

il.ac.bgu.cs.bp.bpjs.eventselection.SimpleEventSelectionStrategy Maven / Gradle / Ivy

package il.ac.bgu.cs.bp.bpjs.eventselection;

import il.ac.bgu.cs.bp.bpjs.bprogram.runtimeengine.BSyncStatement;
import il.ac.bgu.cs.bp.bpjs.events.BEvent;
import il.ac.bgu.cs.bp.bpjs.eventsets.ComposableEventSet;
import il.ac.bgu.cs.bp.bpjs.eventsets.EventSet;
import il.ac.bgu.cs.bp.bpjs.eventsets.Events;
import java.util.ArrayList;
import static java.util.Collections.emptySet;
import static java.util.Collections.singleton;
import java.util.List;
import java.util.Optional;
import java.util.Random;
import java.util.Set;
import java.util.stream.Collectors;
import static java.util.stream.Collectors.toSet;
import org.mozilla.javascript.Context;

/**
 * An event selection strategy that:
 * 
    *
  1. Randomly selects an internal event that's requested and not blocked.
  2. *
  3. If no such event is available, selects the first external event that's not blocked
  4. *
  5. If no such event is available, returns an empty result.
  6. *
* * Under this strategy, if the selected event is internal, and has {@code equal} events queued externally, * these events are not removed. * * @author michael */ public class SimpleEventSelectionStrategy implements EventSelectionStrategy { private final Random rnd; private final long seed; public SimpleEventSelectionStrategy( long seed ) { rnd = new Random(seed); this.seed = seed; } public SimpleEventSelectionStrategy() { rnd = new Random(); seed = rnd.nextLong(); rnd.setSeed(seed); } @Override public Set selectableEvents(Set statements, List 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 != Events.emptySet ) .collect( Collectors.toSet() ) ); Set requested = statements.stream() .filter( stmt -> stmt!=null ) .flatMap( stmt -> stmt.getRequest().stream() ) .collect( Collectors.toSet() ); // Let's see what internal events are requested and not blocked (if any). try { Context.enter(); Set requestedAndNotBlocked = requested.stream() .filter( req -> !blocked.contains(req) ) .collect( toSet() ); return requestedAndNotBlocked.isEmpty() ? externalEvents.stream().filter( e->!blocked.contains(e) ) // No internal events requested, defer to externals. .findFirst().map( e->singleton(e) ).orElse(emptySet()) : requestedAndNotBlocked; } finally { Context.exit(); } } @Override public Optional select(Set statements, List externalEvents, Set selectableEvents) { if ( selectableEvents.isEmpty() ) { return Optional.empty(); } BEvent chosen = new ArrayList<>(selectableEvents).get(rnd.nextInt(selectableEvents.size())); Set requested = statements.stream() .filter( stmt -> stmt!=null ) .flatMap( stmt -> stmt.getRequest().stream() ) .collect( Collectors.toSet() ); if (requested.contains(chosen)) { return Optional.of(new EventSelectionResult(chosen)); } else { // that was an internal event, need to find the first index return Optional.of(new EventSelectionResult(chosen, singleton(externalEvents.indexOf(chosen)))); } } public long getSeed() { return seed; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy