Search in sources :

Example 1 with EventType

use of org.apache.flink.streaming.examples.statemachine.event.EventType in project flink by apache.

the class EventsGenerator method nextInvalid.

/**
 * Creates an event for an illegal state transition of one of the internal state machines. If
 * the generator has not yet started any state machines (for example, because no call to {@link
 * #next(int, int)} was made, yet), this will return null.
 *
 * @return An event for a illegal state transition, or null, if not possible.
 */
@Nullable
public Event nextInvalid() {
    final Iterator<Entry<Integer, State>> iter = states.entrySet().iterator();
    if (iter.hasNext()) {
        final Entry<Integer, State> entry = iter.next();
        State currentState = entry.getValue();
        int address = entry.getKey();
        iter.remove();
        EventType event = currentState.randomInvalidTransition(rnd);
        return new Event(event, address);
    } else {
        return null;
    }
}
Also used : Entry(java.util.Map.Entry) 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) Nullable(javax.annotation.Nullable)

Example 2 with EventType

use of org.apache.flink.streaming.examples.statemachine.event.EventType 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)2 EventTypeAndState (org.apache.flink.streaming.examples.statemachine.dfa.EventTypeAndState)2 State (org.apache.flink.streaming.examples.statemachine.dfa.State)2 Event (org.apache.flink.streaming.examples.statemachine.event.Event)2 EventType (org.apache.flink.streaming.examples.statemachine.event.EventType)2 Nullable (javax.annotation.Nullable)1