Search in sources :

Example 1 with EventTypeAndState

use of org.apache.flink.streaming.examples.statemachine.dfa.EventTypeAndState in project flink by apache.

the class EventsGenerator method next.

// ------------------------------------------------------------------------
/**
 * Creates a new random event. This method randomly pick either one of its currently running
 * state machines, or start a new state machine for a random IP address.
 *
 * <p>With {@link #errorProb} probability, the generated event will be from an illegal state
 * transition of one of the currently running state machines.
 *
 * @param minIp The lower bound for the range from which a new IP address may be picked.
 * @param maxIp The upper bound for the range from which a new IP address may be picked.
 * @return A next random event.
 */
public Event next(int minIp, int maxIp) {
    final double p = rnd.nextDouble();
    if (p * 1000 >= states.size()) {
        // create a new state machine
        final int nextIP = rnd.nextInt(maxIp - minIp) + minIp;
        if (!states.containsKey(nextIP)) {
            EventTypeAndState eventAndState = State.Initial.randomTransition(rnd);
            states.put(nextIP, eventAndState.state);
            return new Event(eventAndState.eventType, nextIP);
        } else {
            // collision on IP address, try again
            return next(minIp, maxIp);
        }
    } else {
        // pick an existing state machine
        // skip over some elements in the linked map, then take the next
        // update it, and insert it at the end
        int numToSkip = Math.min(20, rnd.nextInt(states.size()));
        Iterator<Entry<Integer, State>> iter = states.entrySet().iterator();
        for (int i = numToSkip; i > 0; --i) {
            iter.next();
        }
        Entry<Integer, State> entry = iter.next();
        State currentState = entry.getValue();
        int address = entry.getKey();
        iter.remove();
        if (p < errorProb) {
            EventType event = currentState.randomInvalidTransition(rnd);
            return new Event(event, address);
        } else {
            EventTypeAndState eventAndState = currentState.randomTransition(rnd);
            if (!eventAndState.state.isTerminal()) {
                // reinsert
                states.put(address, eventAndState.state);
            }
            return new Event(eventAndState.eventType, address);
        }
    }
}
Also used : Entry(java.util.Map.Entry) EventTypeAndState(org.apache.flink.streaming.examples.statemachine.dfa.EventTypeAndState) EventType(org.apache.flink.streaming.examples.statemachine.event.EventType) State(org.apache.flink.streaming.examples.statemachine.dfa.State) EventTypeAndState(org.apache.flink.streaming.examples.statemachine.dfa.EventTypeAndState) Event(org.apache.flink.streaming.examples.statemachine.event.Event)

Aggregations

Entry (java.util.Map.Entry)1 EventTypeAndState (org.apache.flink.streaming.examples.statemachine.dfa.EventTypeAndState)1 State (org.apache.flink.streaming.examples.statemachine.dfa.State)1 Event (org.apache.flink.streaming.examples.statemachine.event.Event)1 EventType (org.apache.flink.streaming.examples.statemachine.event.EventType)1