{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## model.BasicActivity\n", "\n", "This notebook shows a very simple OpenCLSim example. We use OpenCLSim's __model.BasicActivity__ to yield a timeout only. Some basic functionality of the logging is shown. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 0. Import libraries" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import datetime, time\n", "import simpy\n", "\n", "import pandas as pd\n", "\n", "import openclsim.core as core\n", "import openclsim.model as model\n", "import openclsim.plot as plot" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 1. Initialise simpy environment" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "# setup environment\n", "simulation_start = 0\n", "my_env = simpy.Environment(initial_time=simulation_start)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 2. Define object classes\n", "In this simple example we won't define specific classes. We will only use a built-in activity from OpenCLSim." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 3. Create objects\n", "##### 3.1. Create site object(s)\n", "No site objects are created." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### 3.2. Create vessel object(s)\n", "No vessel objects are created." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### 3.3 Create activity/activities" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "# initialise registry\n", "registry = {}" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "# create a basic activity (it just creates an event that shifts time for 'duration', in this case 42 time units)\n", "activity = model.BasicActivity(\n", " env=my_env,\n", " name=\"Basic activity\",\n", " registry=registry,\n", " duration=42,\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 4. Register processes and run simpy" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "# initate the simpy processes defined in the 'basic activity' and run simpy\n", "model.register_processes([activity])\n", "my_env.run()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 5. Inspect results\n", "##### 5.1 Inspect logs\n", "The method __plot.get_log_dataframe__ returns the log of an activity in the form of a dataframe. By adding other activities in a list as the second argument, the Activity can be made more human readable. " ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
ActivityTimestampActivityState
0bc355df0-b2db-4dbd-9e36-37fc31cc84941970-01-01 00:00:00START
1bc355df0-b2db-4dbd-9e36-37fc31cc84941970-01-01 00:00:42STOP
\n", "
" ], "text/plain": [ " Activity Timestamp ActivityState\n", "0 bc355df0-b2db-4dbd-9e36-37fc31cc8494 1970-01-01 00:00:00 START\n", "1 bc355df0-b2db-4dbd-9e36-37fc31cc8494 1970-01-01 00:00:42 STOP" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "plot.get_log_dataframe(activity)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
ActivityTimestampActivityState
0Basic activity1970-01-01 00:00:00START
1Basic activity1970-01-01 00:00:42STOP
\n", "
" ], "text/plain": [ " Activity Timestamp ActivityState\n", "0 Basic activity 1970-01-01 00:00:00 START\n", "1 Basic activity 1970-01-01 00:00:42 STOP" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "plot.get_log_dataframe(activity, [activity])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### 5.2. More advanced logging options\n", "The scenario can be extended by adding additional logging instances, that is, the information of the basic activity will also be logged in the activity log of additional activities. This is useful for more complex nested activities. In this example a reporting activity is added where the basic activity log is also added." ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "# initiate SimPy environment\n", "simulation_start = 0\n", "my_env = simpy.Environment(initial_time=simulation_start)\n", "\n", "# create activities\n", "registry = {}\n", "reporting_activity = model.BasicActivity(\n", " env=my_env,\n", " name=\"Reporting activity\",\n", " registry=registry,\n", " duration=0,\n", ")\n", "basic_activity = model.BasicActivity(\n", " env=my_env,\n", " name=\"Basic activity\",\n", " registry=registry,\n", " duration=42,\n", " additional_logs=[reporting_activity],\n", ")\n", "\n", "# initate the simpy processes defined in the 'while activity' and run simpy\n", "model.register_processes([basic_activity])\n", "my_env.run()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We now show the dataframe of the reporting_activity log, and use the basic_activity to make it more human readable." ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
ActivityTimestampActivityStatetyperef
0Basic activity1970-01-01 00:00:00STARTadditional logde553233-2ac6-4085-81aa-cadc21a61431
1Basic activity1970-01-01 00:00:42STOPadditional logde553233-2ac6-4085-81aa-cadc21a61431
\n", "
" ], "text/plain": [ " Activity Timestamp ActivityState type \\\n", "0 Basic activity 1970-01-01 00:00:00 START additional log \n", "1 Basic activity 1970-01-01 00:00:42 STOP additional log \n", "\n", " ref \n", "0 de553233-2ac6-4085-81aa-cadc21a61431 \n", "1 de553233-2ac6-4085-81aa-cadc21a61431 " ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "plot.get_log_dataframe(reporting_activity, [basic_activity])" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.9.12" } }, "nbformat": 4, "nbformat_minor": 2 }