Skip to main content

Querying the NEEMs (Narrative Enabled Episodic Memories) datasbase

Project description

NEEMQuery

A python library for easily querying NEEMS which have been migrated to an SQL database using the neem_to_sql converter. NEEMQuery uses SQLAlchemy and provides similar interface to the users. This makes if feel very familiar to SQLAlchemhy or SQL users in general.

Installation

Make sure you have MariaDB installed on your device.

pip install neemquery

Example Usage

All below examples assume the neems are located in a 'test' database at 'localhost' which can be accessed by 'newuser' using password 'password'.

Getting a robot plan for a certain neem:

Highest Abstraction Level:

This is done by using the NeemInterface class which provides a higher level of abstraction and hides the complexity of the underlying SQL queries.

from neem_query.neem_interface import NeemInterface

ni = NeemInterface("mysql+pymysql://newuser:password@localhost/test")

neem_id = 2
df = ni.query_task_sequence_of_neem(neem_id).get_result().df

print(df)

Medium Abstraction Level:

This is done by using the NeemQuery class directly and using predefined joins and filters to build the query.

from neem_query import NeemQuery

nq = NeemQuery("mysql+pymysql://newuser:password@localhost/test")

neem_ids = [2]

df = (nq.select_task_type().
      select_time_columns().
      select_neem_id().
      select_from_tasks().
      join_task_types().
      join_neems_metadata().filter_by_sql_neem_id(neem_ids).
      join_task_time_interval().
      order_by_interval_begin()).get_result().df

print(df)

Lowest Abstraction Level:

This is done by using the NeemQuery class directly and explicitly writing the SQL query with the correct join conditions and column names and table names.

from neem_query import NeemQuery, TaskType
from neem_query.neems_database import SomaHasIntervalBegin, SomaHasIntervalEnd, DulExecutesTask,\
    DulHasTimeInterval, Neem
from sqlalchemy import and_

nq = NeemQuery("mysql+pymysql://newuser:password@localhost/test")

neem_ids = [2]

df = (nq.select(TaskType.o).
      select(SomaHasIntervalBegin.o).select(SomaHasIntervalEnd.o).
      select(DulExecutesTask.neem_id).
      select_from(DulExecutesTask).
      join_neem_id_tables(TaskType, DulExecutesTask,
                          and_(TaskType.s == DulExecutesTask.dul_Task_o,
                               TaskType.o != "owl:NamedIndividual")).
      join(Neem,
           Neem._id == DulExecutesTask.neem_id).filter(Neem.ID.in_(neem_ids)).
      join_neem_id_tables(DulHasTimeInterval, DulExecutesTask,
                          DulHasTimeInterval.dul_Event_s == DulExecutesTask.dul_Action_s).
      join_neem_id_tables(SomaHasIntervalBegin, DulHasTimeInterval,
                          SomaHasIntervalBegin.dul_TimeInterval_s == DulHasTimeInterval.dul_TimeInterval_o).
      join_neem_id_tables(SomaHasIntervalEnd, DulHasTimeInterval,
                          SomaHasIntervalEnd.dul_TimeInterval_s == DulHasTimeInterval.dul_TimeInterval_o).
      order_by(SomaHasIntervalBegin.o)).get_result().df

print(df)

The result for all of the above examples is exactly the same, and it looks like this:

           task_type          begin            end                   neem_id
0  soma:PhysicalTask 1608292974.092 1608293007.087  5fdca2bcdab33892cea161a0
1  soma:PhysicalTask 1608292995.581 1608293007.041  5fdca2bcdab33892cea161a0
2      soma:Fetching 1608292995.851 1608292996.978  5fdca2bcdab33892cea161a0
3      soma:Reaching 1608292996.124 1608292996.292  5fdca2bcdab33892cea161a0
4     soma:PickingUp 1608292996.546 1608292996.808  5fdca2bcdab33892cea161a0
5  soma:Transporting 1608292999.264 1608292999.863  5fdca2bcdab33892cea161a0
6      soma:MovingTo 1608292999.555 1608292999.657  5fdca2bcdab33892cea161a0
7       soma:Placing 1608293005.093 1608293006.889  5fdca2bcdab33892cea161a0
8      soma:Lowering 1608293005.355 1608293005.462  5fdca2bcdab33892cea161a0
9     soma:Releasing 1608293006.717 1608293006.831  5fdca2bcdab33892cea161a0

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

neemquery-1.1.2.tar.gz (65.6 kB view details)

Uploaded Source

Built Distribution

NEEMQuery-1.1.2-py3-none-any.whl (50.7 kB view details)

Uploaded Python 3

File details

Details for the file neemquery-1.1.2.tar.gz.

File metadata

  • Download URL: neemquery-1.1.2.tar.gz
  • Upload date:
  • Size: 65.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.0 CPython/3.12.4

File hashes

Hashes for neemquery-1.1.2.tar.gz
Algorithm Hash digest
SHA256 2725f7b0b72008e5dcf5d91cad3dbc02d5c8d7b5361df8e923f4dd65a1965139
MD5 e95f497e57a5ba6cc8d379903645ca09
BLAKE2b-256 54c6997023f5c95643fce85258aa74950ec6bb384e2ab03a730f2166afe9c9bf

See more details on using hashes here.

File details

Details for the file NEEMQuery-1.1.2-py3-none-any.whl.

File metadata

  • Download URL: NEEMQuery-1.1.2-py3-none-any.whl
  • Upload date:
  • Size: 50.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.0 CPython/3.12.4

File hashes

Hashes for NEEMQuery-1.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 30a19dbc043f577df4c93a1758692c8b26f8fd8126e759e6e41c22a2ddfe3a04
MD5 b301c252085d5be417e21e8cd2b17781
BLAKE2b-256 9bcf16d6a0049ba6ba68b9127e140e04ebd175c0f8bdfa966f9e38471c231c86

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page