Skip to content

Commit

Permalink
Merge pull request #101 from davidprueser/orm
Browse files Browse the repository at this point in the history
Rework of ORM
  • Loading branch information
Tigul authored Nov 17, 2023
2 parents f1129a3 + 0088e59 commit 1a36f36
Show file tree
Hide file tree
Showing 15 changed files with 748 additions and 598 deletions.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
gitpython>=3.1.37
pybullet~=3.2.5
rospkg~=1.4.0
roslibpy~=1.2.1
Expand Down
96 changes: 42 additions & 54 deletions src/pycram/designator.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
from copy import copy
from inspect import isgenerator, isgeneratorfunction

from sqlalchemy.orm.session import Session
import rospy
import sqlalchemy

from .bullet_world import (Object as BulletWorldObject, BulletWorld)
from .helper import GeneratorList, bcolors
Expand All @@ -22,9 +22,10 @@
import logging

from .orm.action_designator import (Action as ORMAction)
from .orm.object_designator import (ObjectDesignator as ORMObjectDesignator)
from .orm.object_designator import (Object as ORMObjectDesignator)
from .orm.motion_designator import (Motion as ORMMotionDesignator)

from .orm.base import Quaternion, Position, Base, RobotState, MetaData
from .orm.base import Quaternion, Position, Base, RobotState, ProcessMetaData
from .task import with_tree


Expand Down Expand Up @@ -377,6 +378,7 @@ class Motion:
every motion designator.
"""

@with_tree
def perform(self):
"""
Passes this designator to the process module for execution.
Expand All @@ -386,29 +388,32 @@ def perform(self):
raise NotImplementedError()
# return ProcessModule.perform(self)

def ground(self) -> Motion:
"""Fill all missing parameters and pass the designator to the process module. """
raise NotImplementedError(f"{type(self)}.ground() is not implemented.")
def to_sql(self) -> ORMMotionDesignator:
"""
Create an ORM object that corresponds to this description.
def to_sql(self) -> Base:
"""
Create an ORM object that corresponds to this description.
:return: The created ORM object.
"""
return ORMMotionDesignator()

:return: The created ORM object.
"""
raise NotImplementedError(f"{type(self)} has no implementation of to_sql. Feel free to implement it.")
def insert(self, session: Session, *args, **kwargs) -> ORMMotionDesignator:
"""
Add and commit this and all related objects to the session.
Auto-Incrementing primary keys and foreign keys have to be filled by this method.
def insert(self, session: sqlalchemy.orm.session.Session, *args, **kwargs) -> Base:
"""
Add and commit this and all related objects to the session.
Auto-Incrementing primary keys and foreign keys have to be filled by this method.
:param session: Session with a database that is used to add and commit the objects
:return: The completely instanced ORM motion.
"""
metadata = ProcessMetaData().insert(session)

:param session: Session with a database that is used to add and commit the objects
:param args: Possible extra arguments
:param kwargs: Possible extra keyword arguments
:return: The completely instanced ORM object
"""
raise NotImplementedError(f"{type(self)} has no implementation of insert. Feel free to implement it.")
motion = self.to_sql()
motion.process_metadata_id = metadata.id

return motion

def ground(self) -> Motion:
"""Fill all missing parameters and pass the designator to the process module. """
raise NotImplementedError(f"{type(self)}.ground() is not implemented.")

def __init__(self, resolver=None):
"""
Expand Down Expand Up @@ -499,7 +504,7 @@ def to_sql(self) -> ORMAction:
"""
raise NotImplementedError(f"{type(self)} has no implementation of to_sql. Feel free to implement it.")

def insert(self, session: sqlalchemy.orm.session.Session, *args, **kwargs) -> ORMAction:
def insert(self, session: Session, *args, **kwargs) -> ORMAction:
"""
Add and commit this and all related objects to the session.
Auto-Incrementing primary keys and foreign keys have to be filled by this method.
Expand All @@ -510,34 +515,21 @@ def insert(self, session: sqlalchemy.orm.session.Session, *args, **kwargs) -> OR
:return: The completely instanced ORM object
"""

# get or create metadata
metadata = MetaData().insert(session)
pose = self.robot_position.insert(session)

# create position
position = Position(*self.robot_position.position_as_list())
position.metadata_id = metadata.id

# create orientation
orientation = Quaternion(*self.robot_position.orientation_as_list())
orientation.metadata_id = metadata.id

session.add_all([position, orientation])
session.commit()
# get or create metadata
metadata = ProcessMetaData().insert(session)

# create robot-state object
robot_state = RobotState()
robot_state.position = position.id
robot_state.orientation = orientation.id
robot_state.torso_height = self.robot_torso_height
robot_state.type = self.robot_type
robot_state.metadata_id = metadata.id
robot_state = RobotState(self.robot_torso_height, self.robot_type, pose.id)
robot_state.process_metadata_id = metadata.id
session.add(robot_state)
session.commit()

# create action
action = self.to_sql()
action.metadata_id = metadata.id
action.robot_state = robot_state.id
action.process_metadata_id = metadata.id
action.robot_state_id = robot_state.id

return action

Expand Down Expand Up @@ -632,27 +624,23 @@ def to_sql(self) -> ORMObjectDesignator:
"""
return ORMObjectDesignator(self.type, self.name)

def insert(self, session: sqlalchemy.orm.session.Session) -> ORMObjectDesignator:
def insert(self, session: Session) -> ORMObjectDesignator:
"""
Add and commit this and all related objects to the session.
Auto-Incrementing primary keys and foreign keys have to be filled by this method.
:param session: Session with a database that is used to add and commit the objects
:return: The completely instanced ORM object
"""
metadata = MetaData().insert(session)
# insert position and orientation of object of the designator
orm_position = Position(*self.pose.position_as_list(), metadata.id)
orm_orientation = Quaternion(*self.pose.orientation_as_list(), metadata.id)
session.add(orm_position)
session.add(orm_orientation)
session.commit()
metadata = ProcessMetaData().insert(session)

# create object orm designator
obj = self.to_sql()
obj.metadata_id = metadata.id
obj.position = orm_position.id
obj.orientation = orm_orientation.id
obj.process_metadata_id = metadata.id

pose = self.pose.insert(session)
obj.pose_id = pose.id

session.add(obj)
session.commit()
return obj
Expand Down
Loading

0 comments on commit 1a36f36

Please sign in to comment.