Skip to content

Commit

Permalink
✏️ ros_typedb match->fetch (v2.27.0)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rezenders committed Apr 24, 2024
1 parent 590c09c commit 8463b08
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 23 deletions.
32 changes: 16 additions & 16 deletions ros_typedb/ros_typedb/ros_typedb_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,13 @@ def set_query_result_value(
return _param_value


def match_query_result_to_ros_msg(
def fetch_query_result_to_ros_msg(
query_result: list[dict[str, MatchResultDict]] | None
) -> ros_typedb_msgs.srv.Query.Response:
"""
Convert typedb match query result to :class:`ros_typedb_msgs.srv.Query`.
Convert typedb fetch query result to :class:`ros_typedb_msgs.srv.Query`.
:param query_result: typedb match query result.
:param query_result: typedb fetch query result.
:return: converted query response.
"""
response = Query.Response()
Expand All @@ -95,13 +95,13 @@ def match_query_result_to_ros_msg(
return response


def match_aggregate_query_result_to_ros_msg(
def get_aggregate_query_result_to_ros_msg(
query_result: int | float | None
) -> ros_typedb_msgs.srv.Query.Response:
"""
Convert match aggregate query result to :class:`ros_typedb_msgs.srv.Query`.
Convert get aggregate query result to :class:`ros_typedb_msgs.srv.Query`.
:param query_result: typedb match aggreate query result.
:param query_result: typedb get aggreate query result.
:return: converted query response.
"""
response = Query.Response()
Expand All @@ -114,21 +114,21 @@ def match_aggregate_query_result_to_ros_msg(


def query_result_to_ros_msg(
query_type: Literal['match', 'match_aggregate'],
query_type: Literal['fetch', 'get_aggregate'],
query_result: list[dict[str, MatchResultDict]] | int | float | None
) -> ros_typedb_msgs.srv.Query.Response:
"""
Convert typedb query result to :class:`ros_typedb_msgs.srv.Query`.
:param query_type: query_type, e.g., 'match' or 'match_aggregate'
:param query_type: query_type, e.g., 'fetch' or 'get_aggregate'
:param query_result: typedb query result.
:return: converted query response.
"""
response = Query.Response()
if query_type == 'match':
response = match_query_result_to_ros_msg(query_result)
elif query_type == 'match_aggregate':
response = match_aggregate_query_result_to_ros_msg(query_result)
if query_type == 'fetch':
response = fetch_query_result_to_ros_msg(query_result)
elif query_type == 'get_aggregate':
response = get_aggregate_query_result_to_ros_msg(query_result)
return response


Expand Down Expand Up @@ -264,10 +264,10 @@ def query_service_cb(
query_func = self.typedb_interface.insert_database
elif req.query_type == 'delete':
query_func = self.typedb_interface.delete_from_database
elif req.query_type == 'match':
query_func = self.typedb_interface.match_database
elif req.query_type == 'match_aggregate':
query_func = self.typedb_interface.match_aggregate_database
elif req.query_type == 'fetch':
query_func = self.typedb_interface.fetch_database
elif req.query_type == 'get_aggregate':
query_func = self.typedb_interface.get_aggregate_database
else:
self.get_logger().warning(
'Query type {} not recognized'.format(req.query_type))
Expand Down
16 changes: 9 additions & 7 deletions ros_typedb/test/test_ros_typedb_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,15 +154,17 @@ def test_ros_typedb_delete_query(insert_query):
query_res = node.call_service(node.query_srv, query_req)

match_query_req = Query.Request()
match_query_req.query_type = 'match'
match_query_req.query_type = 'get_aggregate'
match_query_req.query = """
match $entity isa person, has email "test@test.com";
get $entity;
count;
"""
match_query_res = node.call_service(node.query_srv, match_query_req)

assert query_res.success and match_query_res.success and \
len(match_query_res.attributes) == 0
match_query_res.attributes[0].value.type == 2 and \
match_query_res.attributes[0].value.integer_value == 0
finally:
rclpy.shutdown()

Expand Down Expand Up @@ -211,7 +213,7 @@ def test_ros_typedb_wrong_query(insert_query):


@pytest.mark.launch(fixture=generate_test_description)
def test_ros_typedb_match_query_attribute(insert_query):
def test_ros_typedb_fetch_query_attribute(insert_query):
rclpy.init()
try:
node = MakeTestNode()
Expand All @@ -221,7 +223,7 @@ def test_ros_typedb_match_query_attribute(insert_query):
node.call_service(node.query_srv, insert_query)

query_req = Query.Request()
query_req.query_type = 'match'
query_req.query_type = 'fetch'
query_req.query = """
match $entity isa person,
has email "test@test.com",
Expand All @@ -230,7 +232,7 @@ def test_ros_typedb_match_query_attribute(insert_query):
has height $height,
has alive $alive,
has birth-date $date;
get $nick, $age, $height, $alive, $date;
fetch $nick, $age, $height, $alive, $date;
"""
query_res = node.call_service(node.query_srv, query_req)

Expand Down Expand Up @@ -260,7 +262,7 @@ def test_ros_typedb_match_query_attribute(insert_query):


@pytest.mark.launch(fixture=generate_test_description)
def test_ros_typedb_match_aggregate_query(insert_query):
def test_ros_typedb_get_aggregate_query(insert_query):
rclpy.init()
try:
node = MakeTestNode()
Expand All @@ -270,7 +272,7 @@ def test_ros_typedb_match_aggregate_query(insert_query):
node.call_service(node.query_srv, insert_query)

query_req = Query.Request()
query_req.query_type = 'match_aggregate'
query_req.query_type = 'get_aggregate'
query_req.query = """
match
$person isa person,
Expand Down

0 comments on commit 8463b08

Please sign in to comment.