{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": "# Usage of the EventClass - demonstration on concrete class **LogEvent**\n" }, { "metadata": {}, "cell_type": "markdown", "source": "EventClass is merely an abstract class and serves as an interface for all EventClasses." }, { "metadata": {}, "cell_type": "markdown", "source": [ "The LogEvent class extends an abstract base class and includes the following fields:\n", "- extra_data\n", "- metadata\n", "- original\n", "\n", "LogEvent validates extra_data and checks whether all entries are in the `DELIVERED` state before setting itself to `DELIVERED`." ] }, { "metadata": { "ExecuteTime": { "end_time": "2025-07-04T08:26:18.587612Z", "start_time": "2025-07-04T08:26:18.568644Z" } }, "cell_type": "code", "source": [ "import pickle\n", "\n", "from tinycss2 import serialize\n", "\n", "from logprep.ng.event.log_event import LogEvent\n", "from logprep.ng.event.event_state import EventStateType\n", "from logprep.ng.abc.event import Event\n", "\n", "\n", "class DummyEvent(Event):\n", " __slots__ = Event.__slots__" ], "outputs": [], "execution_count": 2 }, { "metadata": {}, "cell_type": "markdown", "source": "**Set LogEvent to delivered with SUCCESS**" }, { "metadata": { "ExecuteTime": { "end_time": "2025-07-04T08:26:20.589331Z", "start_time": "2025-07-04T08:26:20.585603Z" } }, "cell_type": "code", "source": [ "child1 = DummyEvent({\"c1\": 1})\n", "child2 = DummyEvent({\"c2\": 2})\n", "child1.state.current_state = EventStateType.DELIVERED\n", "child2.state.current_state = EventStateType.DELIVERED\n", "\n", "log_event = LogEvent(\n", " data={\"parent\": \"yes\"},\n", " original=b\"...\",\n", " extra_data=[child1, child2],\n", ")\n", "log_event.state.current_state = EventStateType.STORED_IN_OUTPUT\n", "log_event.state.next_state(success=True)\n", "\n", "print(log_event.state.current_state)" ], "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "delivered\n" ] } ], "execution_count": 3 }, { "metadata": {}, "cell_type": "markdown", "source": "**Set LogEvent to delivered FAILING**\n" }, { "metadata": { "ExecuteTime": { "end_time": "2025-07-04T08:26:22.613362Z", "start_time": "2025-07-04T08:26:22.528342Z" } }, "cell_type": "code", "source": [ "child1 = DummyEvent({\"c1\": 1})\n", "child2 = DummyEvent({\"c2\": 2})\n", "child1.state.current_state = EventStateType.DELIVERED\n", "child2.state.current_state = EventStateType.PROCESSING\n", "\n", "log_event = LogEvent(\n", " data={\"parent\": \"yes\"},\n", " original=b\"...\",\n", " extra_data=[child1, child2],\n", ")\n", "log_event.state.current_state = EventStateType.STORED_IN_OUTPUT\n", "log_event.state.next_state(success=True)\n", "\n", "print(log_event.state.current_state)" ], "outputs": [ { "ename": "ValueError", "evalue": "Cannot assign DELIVERED state: not all extra_data events are DELIVERED.", "output_type": "error", "traceback": [ "\u001B[31m---------------------------------------------------------------------------\u001B[39m", "\u001B[31mValueError\u001B[39m Traceback (most recent call last)", "\u001B[36mCell\u001B[39m\u001B[36m \u001B[39m\u001B[32mIn[4]\u001B[39m\u001B[32m, line 12\u001B[39m\n\u001B[32m 6\u001B[39m log_event = LogEvent(\n\u001B[32m 7\u001B[39m data={\u001B[33m\"\u001B[39m\u001B[33mparent\u001B[39m\u001B[33m\"\u001B[39m: \u001B[33m\"\u001B[39m\u001B[33myes\u001B[39m\u001B[33m\"\u001B[39m},\n\u001B[32m 8\u001B[39m original=\u001B[33mb\u001B[39m\u001B[33m\"\u001B[39m\u001B[33m...\u001B[39m\u001B[33m\"\u001B[39m,\n\u001B[32m 9\u001B[39m extra_data=[child1, child2],\n\u001B[32m 10\u001B[39m )\n\u001B[32m 11\u001B[39m log_event.state.current_state = EventStateType.STORED_IN_OUTPUT\n\u001B[32m---> \u001B[39m\u001B[32m12\u001B[39m \u001B[43mlog_event\u001B[49m\u001B[43m.\u001B[49m\u001B[43mstate\u001B[49m\u001B[43m.\u001B[49m\u001B[43mnext_state\u001B[49m\u001B[43m(\u001B[49m\u001B[43msuccess\u001B[49m\u001B[43m=\u001B[49m\u001B[38;5;28;43;01mTrue\u001B[39;49;00m\u001B[43m)\u001B[49m\n\u001B[32m 14\u001B[39m \u001B[38;5;28mprint\u001B[39m(log_event.state.current_state)\n", "\u001B[36mFile \u001B[39m\u001B[32m~/dev/bwi/Logprep/logprep/ng/event/log_event.py:69\u001B[39m, in \u001B[36mLogEvent._next_state_validation_helper\u001B[39m\u001B[34m(self, success)\u001B[39m\n\u001B[32m 65\u001B[39m \u001B[38;5;28;01mdef\u001B[39;00m\u001B[38;5;250m \u001B[39m\u001B[34m_next_state_validation_helper\u001B[39m(\n\u001B[32m 66\u001B[39m \u001B[38;5;28mself\u001B[39m, *, success: \u001B[38;5;28mbool\u001B[39m | \u001B[38;5;28;01mNone\u001B[39;00m = \u001B[38;5;28;01mNone\u001B[39;00m\n\u001B[32m 67\u001B[39m ) -> EventStateType | \u001B[38;5;28;01mNone\u001B[39;00m:\n\u001B[32m 68\u001B[39m new_state: EventStateType = \u001B[38;5;28mself\u001B[39m._origin_state_next_state_fn(success=success)\n\u001B[32m---> \u001B[39m\u001B[32m69\u001B[39m \u001B[38;5;28;43mself\u001B[39;49m\u001B[43m.\u001B[49m\u001B[43m_validate_state\u001B[49m\u001B[43m(\u001B[49m\u001B[43mnew_state\u001B[49m\u001B[43m)\u001B[49m\n\u001B[32m 70\u001B[39m \u001B[38;5;28;01mreturn\u001B[39;00m new_state\n", "\u001B[36mFile \u001B[39m\u001B[32m~/dev/bwi/Logprep/logprep/ng/event/log_event.py:75\u001B[39m, in \u001B[36mLogEvent._validate_state\u001B[39m\u001B[34m(self, state_type)\u001B[39m\n\u001B[32m 73\u001B[39m \u001B[38;5;28;01mif\u001B[39;00m state_type == EventStateType.DELIVERED:\n\u001B[32m 74\u001B[39m \u001B[38;5;28;01mif\u001B[39;00m \u001B[38;5;129;01mnot\u001B[39;00m \u001B[38;5;28mall\u001B[39m(e.state.current_state == EventStateType.DELIVERED \u001B[38;5;28;01mfor\u001B[39;00m e \u001B[38;5;129;01min\u001B[39;00m \u001B[38;5;28mself\u001B[39m.extra_data):\n\u001B[32m---> \u001B[39m\u001B[32m75\u001B[39m \u001B[38;5;28;01mraise\u001B[39;00m \u001B[38;5;167;01mValueError\u001B[39;00m(\n\u001B[32m 76\u001B[39m \u001B[33m\"\u001B[39m\u001B[33mCannot assign DELIVERED state: not all extra_data events are DELIVERED.\u001B[39m\u001B[33m\"\u001B[39m\n\u001B[32m 77\u001B[39m )\n", "\u001B[31mValueError\u001B[39m: Cannot assign DELIVERED state: not all extra_data events are DELIVERED." ] } ], "execution_count": 4 }, { "metadata": {}, "cell_type": "markdown", "source": "**Pickable / Hashable**" }, { "metadata": {}, "cell_type": "markdown", "source": [ "The class and all its subclasses must be **picklable** and **hashable**.\n", "\n", "- **Pickable**: means that an object can be **serialized** and **deserialized** using Python’s pickle module, allowing it to be saved to disk or transferred between processes.\n", "\n", "- **Hashable**: means that an object can be used as a key in dictionaries, stored in sets or other hash-based containers, and used in equality comparisons with other objects.\n", "To be hashable, the object must have a stable __hash__() implementation and a consistent __eq__() behavior." ] }, { "metadata": {}, "cell_type": "markdown", "source": "**EQUALITY**" }, { "metadata": { "ExecuteTime": { "end_time": "2025-07-04T08:27:37.254605Z", "start_time": "2025-07-04T08:27:37.251798Z" } }, "cell_type": "code", "source": [ "log_event = LogEvent(\n", " data={\"log\": \"yes\"},\n", " original=b\"...\",\n", ")\n", "\n", "other_equal_log_event = LogEvent(data={\"log\": \"yes\"}, original=b\"...\")\n", "\n", "print(log_event==other_equal_log_event)" ], "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "execution_count": 5 }, { "metadata": { "ExecuteTime": { "end_time": "2025-07-04T08:28:02.280946Z", "start_time": "2025-07-04T08:28:02.278234Z" } }, "cell_type": "code", "source": [ "other_not_equal_log_event = LogEvent(data={\"log\": \"no\"}, original=b\"...\")\n", "print(log_event==other_not_equal_log_event)" ], "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False\n" ] } ], "execution_count": 6 }, { "metadata": {}, "cell_type": "markdown", "source": "**PICKLABILITY**" }, { "metadata": { "ExecuteTime": { "end_time": "2025-07-04T08:28:21.369226Z", "start_time": "2025-07-04T08:28:21.366961Z" } }, "cell_type": "code", "source": [ "import pickle\n", "serialized = pickle.dumps(log_event)\n", "\n", "print(serialized)" ], "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "b'\\x80\\x04\\x95\\xb1\\x01\\x00\\x00\\x00\\x00\\x00\\x00\\x8c\\x1alogprep.ng.event.log_event\\x94\\x8c\\x08LogEvent\\x94\\x93\\x94)\\x81\\x94N}\\x94(\\x8c\\x08original\\x94C\\x03...\\x94\\x8c\\nextra_data\\x94]\\x94\\x8c\\x08metadata\\x94N\\x8c\\x06_state\\x94\\x8c\\x1clogprep.ng.event.event_state\\x94\\x8c\\nEventState\\x94\\x93\\x94)\\x81\\x94}\\x94(\\x8c\\rcurrent_state\\x94h\\x0b\\x8c\\x0eEventStateType\\x94\\x93\\x94\\x8c\\treceiving\\x94\\x85\\x94R\\x94\\x8c\\nnext_state\\x94\\x8c\\x08builtins\\x94\\x8c\\x07getattr\\x94\\x93\\x94h\\x03\\x8c\\x1d_next_state_validation_helper\\x94\\x86\\x94R\\x94ub\\x8c\\x1b_origin_state_next_state_fn\\x94\\x8c\\tfunctools\\x94\\x8c\\x07partial\\x94\\x93\\x94h\\x0b\\x8c\\x15EventState.next_state\\x94\\x93\\x94\\x85\\x94R\\x94(h\"h\\x0e\\x85\\x94}\\x94Nt\\x94b\\x8c\\x04data\\x94}\\x94\\x8c\\x03log\\x94\\x8c\\x03yes\\x94s\\x8c\\x06errors\\x94]\\x94\\x8c\\x08warnings\\x94]\\x94u\\x86\\x94b.'\n" ] } ], "execution_count": 7 }, { "metadata": { "ExecuteTime": { "end_time": "2025-07-04T08:28:48.841672Z", "start_time": "2025-07-04T08:28:48.838316Z" } }, "cell_type": "code", "source": [ "copy_log_event = pickle.loads(serialized)\n", "print(\"Comparison between original and deserialized object.\\n Result (is equal):\", log_event == copy_log_event, end=\"\\n\\n\")\n", "\n", "print(copy_log_event)" ], "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Comparison between original and deserialized object.\n", " Result (is equal): True\n", "\n", "LogEvent(data={'log': 'yes'}, state=receiving)\n" ] } ], "execution_count": 8 } ], "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 }