{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": "# Demonstration of the Event States\n" }, { "metadata": {}, "cell_type": "markdown", "source": [ "The Event State Class manages the lifecycle of a log event using a finite state machine.\n", "\n", "This class encapsulates valid transitions between event states such as `receiving`, `processing`, `delivery`, and `failure` handling.\n", "\n", "It supports automatic and conditional transitions based on success flags." ] }, { "metadata": { "ExecuteTime": { "end_time": "2025-07-04T08:21:11.990904Z", "start_time": "2025-07-04T08:21:11.987705Z" } }, "cell_type": "code", "source": [ "from logprep.ng.event.event_state import EventState\n", "\n", "state = EventState() # Initial start at: RECEIVING\n", "print(state.current_state)" ], "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "receiving\n" ] } ], "execution_count": 8 }, { "metadata": { "ExecuteTime": { "end_time": "2025-07-04T08:21:12.047962Z", "start_time": "2025-07-04T08:21:12.045425Z" } }, "cell_type": "code", "source": [ "state.next_state() # switch to: RECEIVED\n", "print(state.current_state)" ], "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "received\n" ] } ], "execution_count": 9 }, { "metadata": { "ExecuteTime": { "end_time": "2025-07-04T08:21:12.098616Z", "start_time": "2025-07-04T08:21:12.095801Z" } }, "cell_type": "code", "source": [ "state.next_state() # switch to: PROCESSING\n", "print(state.current_state)" ], "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "processing\n" ] } ], "execution_count": 10 }, { "metadata": { "ExecuteTime": { "end_time": "2025-07-04T08:21:12.151344Z", "start_time": "2025-07-04T08:21:12.147743Z" } }, "cell_type": "code", "source": [ "if True:\n", " state.next_state(success=True) # with SUCCESS: switch to: PROCESSED\n", " print(state.current_state)\n", "else:\n", " state.next_state(success=False) # with FAILURE: switch to: FAILED\n", " print(state.current_state)\n", "\n", " state.next_state() # BUT: FAILED state transition mismatch! switch to: STORED_IN_ERROR\n", " print(state.current_state)" ], "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "processed\n" ] } ], "execution_count": 11 }, { "metadata": { "ExecuteTime": { "end_time": "2025-07-04T08:21:12.199321Z", "start_time": "2025-07-04T08:21:12.196571Z" } }, "cell_type": "code", "source": [ "state.next_state() # switch to: STORED_IN_OUTPUT\n", "print(state.current_state)" ], "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "stored_in_output\n" ] } ], "execution_count": 12 }, { "metadata": { "ExecuteTime": { "end_time": "2025-07-04T08:21:12.287007Z", "start_time": "2025-07-04T08:21:12.283788Z" } }, "cell_type": "code", "source": [ "state.next_state(success=False) # witch FAILURE: switch to: FAILED\n", "print(state.current_state)" ], "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "failed\n" ] } ], "execution_count": 13 }, { "metadata": { "ExecuteTime": { "end_time": "2025-07-04T08:21:12.340538Z", "start_time": "2025-07-04T08:21:12.337297Z" } }, "cell_type": "code", "source": [ "state.next_state() # [In FAIL State] switch to: STORED_IN_ERROR\n", "print(state.current_state)" ], "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "stored_in_error\n" ] } ], "execution_count": 14 }, { "metadata": { "ExecuteTime": { "end_time": "2025-07-04T08:21:12.390844Z", "start_time": "2025-07-04T08:21:12.387566Z" } }, "cell_type": "code", "source": [ "state.next_state(success=True) # with SUCCESS: switch to DELIVERED\n", "print(state.current_state)" ], "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "delivered\n" ] } ], "execution_count": 15 }, { "metadata": { "ExecuteTime": { "end_time": "2025-07-04T08:21:12.441869Z", "start_time": "2025-07-04T08:21:12.438106Z" } }, "cell_type": "code", "source": [ "state.next_state() # switch to ACKED\n", "print(state.current_state)" ], "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "acked\n" ] } ], "execution_count": 16 } ], "metadata": { "kernelspec": { "display_name": "Python 3.11.0 ('.venv': venv)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.0" }, "orig_nbformat": 4, "vscode": { "interpreter": { "hash": "586280540a85d3e21edc698fe7b86af2848b9b02644e6c22463da25c40a3f1be" } } }, "nbformat": 4, "nbformat_minor": 2 }