org.apache.flink.streaming.examples.statemachine.event.Event Java Examples

The following examples show how to use org.apache.flink.streaming.examples.statemachine.event.Event. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: StateMachineExample.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public void flatMap(Event evt, Collector<Alert> out) throws Exception {
	// get the current state for the key (source address)
	// if no state exists, yet, the state must be the state machine's initial state
	State state = currentState.value();
	if (state == null) {
		state = State.Initial;
	}

	// ask the state machine what state we should go to based on the given event
	State nextState = state.transition(evt.type());

	if (nextState == State.InvalidTransition) {
		// the current event resulted in an invalid transition
		// raise an alert!
		out.collect(new Alert(evt.sourceAddress(), state, evt.type()));
	}
	else if (nextState.isTerminal()) {
		// we reached a terminal state, clean up the current state
		currentState.clear();
	}
	else {
		// remember the new state
		currentState.update(nextState);
	}
}
 
Example #2
Source File: EventsGenerator.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
	}
}
 
Example #3
Source File: StateMachineExample.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Override
public void flatMap(Event evt, Collector<Alert> out) throws Exception {
	// get the current state for the key (source address)
	// if no state exists, yet, the state must be the state machine's initial state
	State state = currentState.value();
	if (state == null) {
		state = State.Initial;
	}

	// ask the state machine what state we should go to based on the given event
	State nextState = state.transition(evt.type());

	if (nextState == State.InvalidTransition) {
		// the current event resulted in an invalid transition
		// raise an alert!
		out.collect(new Alert(evt.sourceAddress(), state, evt.type()));
	}
	else if (nextState.isTerminal()) {
		// we reached a terminal state, clean up the current state
		currentState.clear();
	}
	else {
		// remember the new state
		currentState.update(nextState);
	}
}
 
Example #4
Source File: EventsGeneratorSource.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public void run(SourceContext<Event> sourceContext) throws Exception {
	final EventsGenerator generator = new EventsGenerator(errorProbability);

	final int range = Integer.MAX_VALUE / getRuntimeContext().getNumberOfParallelSubtasks();
	final int min = range * getRuntimeContext().getIndexOfThisSubtask();
	final int max = min + range;

	while (running) {
		sourceContext.collect(generator.next(min, max));

		if (delayPerRecordMillis > 0) {
			Thread.sleep(delayPerRecordMillis);
		}
	}
}
 
Example #5
Source File: EventsGeneratorSource.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Override
public void run(SourceContext<Event> sourceContext) throws Exception {
	final EventsGenerator generator = new EventsGenerator(errorProbability);

	final int range = Integer.MAX_VALUE / getRuntimeContext().getNumberOfParallelSubtasks();
	final int min = range * getRuntimeContext().getIndexOfThisSubtask();
	final int max = min + range;

	while (running) {
		sourceContext.collect(generator.next(min, max));

		if (delayPerRecordMillis > 0) {
			Thread.sleep(delayPerRecordMillis);
		}
	}
}
 
Example #6
Source File: StateMachineExample.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public void flatMap(Event evt, Collector<Alert> out) throws Exception {
	// get the current state for the key (source address)
	// if no state exists, yet, the state must be the state machine's initial state
	State state = currentState.value();
	if (state == null) {
		state = State.Initial;
	}

	// ask the state machine what state we should go to based on the given event
	State nextState = state.transition(evt.type());

	if (nextState == State.InvalidTransition) {
		// the current event resulted in an invalid transition
		// raise an alert!
		out.collect(new Alert(evt.sourceAddress(), state, evt.type()));
	}
	else if (nextState.isTerminal()) {
		// we reached a terminal state, clean up the current state
		currentState.clear();
	}
	else {
		// remember the new state
		currentState.update(nextState);
	}
}
 
Example #7
Source File: EventsGenerator.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
	}
}
 
Example #8
Source File: EventsGeneratorSource.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public void run(SourceContext<Event> sourceContext) throws Exception {
	final EventsGenerator generator = new EventsGenerator(errorProbability);

	final int range = Integer.MAX_VALUE / getRuntimeContext().getNumberOfParallelSubtasks();
	final int min = range * getRuntimeContext().getIndexOfThisSubtask();
	final int max = min + range;

	while (running) {
		sourceContext.collect(generator.next(min, max));

		if (delayPerRecordMillis > 0) {
			Thread.sleep(delayPerRecordMillis);
		}
	}
}
 
Example #9
Source File: EventsGenerator.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
	}
}
 
Example #10
Source File: EventDeSerializer.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public byte[] serialize(Event evt) {
	ByteBuffer byteBuffer = ByteBuffer.allocate(8).order(ByteOrder.LITTLE_ENDIAN);
	byteBuffer.putInt(0, evt.sourceAddress());
	byteBuffer.putInt(4, evt.type().ordinal());
	return byteBuffer.array();
}
 
Example #11
Source File: EventDeSerializer.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public Event deserialize(byte[] message) throws IOException {
	ByteBuffer buffer = ByteBuffer.wrap(message).order(ByteOrder.LITTLE_ENDIAN);
	int address = buffer.getInt(0);
	int typeOrdinal = buffer.getInt(4);
	return new Event(EventType.values()[typeOrdinal], address);
}
 
Example #12
Source File: EventDeSerializer.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public byte[] serialize(Event evt) {
	ByteBuffer byteBuffer = ByteBuffer.allocate(8).order(ByteOrder.LITTLE_ENDIAN);
	byteBuffer.putInt(0, evt.sourceAddress());
	byteBuffer.putInt(4, evt.type().ordinal());
	return byteBuffer.array();
}
 
Example #13
Source File: StandaloneThreadedGenerator.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void run() {
	final EventsGenerator generator = new EventsGenerator();

	while (running) {
		if (injectInvalidNext) {
			injectInvalidNext = false;
			Event next = generator.nextInvalid();
			if (next != null) {
				out.collect(next);
			}
		}
		else {
			out.collect(generator.next(minAddress, maxAddress));
		}

		count += 1;

		// sleep the delay to throttle
		if (delay > 0) {
			try {
				Thread.sleep(delay);
			} catch (InterruptedException e) {
				Thread.currentThread().interrupt();
			}
		}
	}
}
 
Example #14
Source File: EventDeSerializer.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public Event deserialize(byte[] message) throws IOException {
	ByteBuffer buffer = ByteBuffer.wrap(message).order(ByteOrder.LITTLE_ENDIAN);
	int address = buffer.getInt(0);
	int typeOrdinal = buffer.getInt(4);
	return new Event(EventType.values()[typeOrdinal], address);
}
 
Example #15
Source File: EventDeSerializer.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
public Event deserialize(byte[] message) throws IOException {
	ByteBuffer buffer = ByteBuffer.wrap(message).order(ByteOrder.LITTLE_ENDIAN);
	int address = buffer.getInt(0);
	int typeOrdinal = buffer.getInt(4);
	return new Event(EventType.values()[typeOrdinal], address);
}
 
Example #16
Source File: EventDeSerializer.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
public byte[] serialize(Event evt) {
	ByteBuffer byteBuffer = ByteBuffer.allocate(8).order(ByteOrder.LITTLE_ENDIAN);
	byteBuffer.putInt(0, evt.sourceAddress());
	byteBuffer.putInt(4, evt.type().ordinal());
	return byteBuffer.array();
}
 
Example #17
Source File: StandaloneThreadedGenerator.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
public void run() {
	final EventsGenerator generator = new EventsGenerator();

	while (running) {
		if (injectInvalidNext) {
			injectInvalidNext = false;
			Event next = generator.nextInvalid();
			if (next != null) {
				out.collect(next);
			}
		}
		else {
			out.collect(generator.next(minAddress, maxAddress));
		}

		count += 1;

		// sleep the delay to throttle
		if (delay > 0) {
			try {
				Thread.sleep(delay);
			} catch (InterruptedException e) {
				Thread.currentThread().interrupt();
			}
		}
	}
}
 
Example #18
Source File: StandaloneThreadedGenerator.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void run() {
	final EventsGenerator generator = new EventsGenerator();

	while (running) {
		if (injectInvalidNext) {
			injectInvalidNext = false;
			Event next = generator.nextInvalid();
			if (next != null) {
				out.collect(next);
			}
		}
		else {
			out.collect(generator.next(minAddress, maxAddress));
		}

		count += 1;

		// sleep the delay to throttle
		if (delay > 0) {
			try {
				Thread.sleep(delay);
			} catch (InterruptedException e) {
				Thread.currentThread().interrupt();
			}
		}
	}
}
 
Example #19
Source File: KafkaStandaloneGenerator.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public void collect(Event evt) {
	byte[] serialized = serializer.serialize(evt);
	producer.send(new ProducerRecord<>(topic, partition, null, serialized));
}
 
Example #20
Source File: KafkaStandaloneGenerator.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public void collect(Event evt) {
	byte[] serialized = serializer.serialize(evt);
	producer.send(new ProducerRecord<>(topic, partition, null, serialized));
}
 
Example #21
Source File: EventDeSerializer.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public boolean isEndOfStream(Event nextElement) {
	return false;
}
 
Example #22
Source File: EventDeSerializer.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public TypeInformation<Event> getProducedType() {
	return TypeInformation.of(Event.class);
}
 
Example #23
Source File: EventDeSerializer.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public TypeInformation<Event> getProducedType() {
	return TypeInformation.of(Event.class);
}
 
Example #24
Source File: EventDeSerializer.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public boolean isEndOfStream(Event nextElement) {
	return false;
}
 
Example #25
Source File: EventDeSerializer.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
@Override
public TypeInformation<Event> getProducedType() {
	return TypeInformation.of(Event.class);
}
 
Example #26
Source File: EventDeSerializer.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
@Override
public boolean isEndOfStream(Event nextElement) {
	return false;
}
 
Example #27
Source File: KafkaStandaloneGenerator.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
@Override
public void collect(Event evt) {
	byte[] serialized = serializer.serialize(evt);
	producer.send(new ProducerRecord<>(topic, partition, null, serialized));
}
 
Example #28
Source File: StandaloneThreadedGenerator.java    From flink with Apache License 2.0 3 votes vote down vote up
/**
 * Creates a new generator thread.
 *
 * @param out The collector to push the generated records to.
 * @param minAddress The lower bound for the range from which a new IP address may be picked.
 * @param maxAddress The upper bound for the range from which a new IP address may be picked.
 */
GeneratorThread(Collector<Event> out, int minAddress, int maxAddress) {
	this.out = out;
	this.minAddress = minAddress;
	this.maxAddress = maxAddress;
	this.running = true;
}
 
Example #29
Source File: StandaloneThreadedGenerator.java    From flink with Apache License 2.0 3 votes vote down vote up
/**
 * Creates a new generator thread.
 *
 * @param out The collector to push the generated records to.
 * @param minAddress The lower bound for the range from which a new IP address may be picked.
 * @param maxAddress The upper bound for the range from which a new IP address may be picked.
 */
GeneratorThread(Collector<Event> out, int minAddress, int maxAddress) {
	this.out = out;
	this.minAddress = minAddress;
	this.maxAddress = maxAddress;
	this.running = true;
}
 
Example #30
Source File: StandaloneThreadedGenerator.java    From Flink-CEPplus with Apache License 2.0 3 votes vote down vote up
/**
 * Creates a new generator thread.
 *
 * @param out The collector to push the generated records to.
 * @param minAddress The lower bound for the range from which a new IP address may be picked.
 * @param maxAddress The upper bound for the range from which a new IP address may be picked.
 */
GeneratorThread(Collector<Event> out, int minAddress, int maxAddress) {
	this.out = out;
	this.minAddress = minAddress;
	this.maxAddress = maxAddress;
	this.running = true;
}