From 83154311bfd4a26b94b3e4f3d4632aa68d9c929c Mon Sep 17 00:00:00 2001 From: Gustavo Date: Mon, 8 Jan 2024 17:00:46 -0300 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20get=5Fattributes=20list=20of=20keys?= =?UTF-8?q?=20as=20input?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ros_typedb/ros_typedb/typedb_interface.py | 32 +++++++++++------------ ros_typedb/test/test_typedb_interface.py | 13 +++++---- 2 files changed, 24 insertions(+), 21 deletions(-) diff --git a/ros_typedb/ros_typedb/typedb_interface.py b/ros_typedb/ros_typedb/typedb_interface.py index e229311..c07b094 100644 --- a/ros_typedb/ros_typedb/typedb_interface.py +++ b/ros_typedb/ros_typedb/typedb_interface.py @@ -452,48 +452,47 @@ def insert_relationship( query = match_query + insert_query return self.insert_database(query) - def get_attribute_from_thing_raw(self, thing, key, key_value, attr): + def get_attribute_from_thing_raw(self, thing, key_attr_list, attr): """ Get raw attribute values from a instance of a thing. :param thing: thing name :type thing: str - :param key: attribute name to identify the individual - :type key: str - :param key_value: attribute value to identify the individual - :type key_value: str or int or float or datetime or bool + :param key_attr_list: list with attribute tuple (name, value) + :type key_attr_list: list[tuple[str, str or int or float or datetime]] :param attr: attribute name to be return :type attr: str :return: List of dictionary with the query result. :rtype: list[dict[str, dict[str, str or int or float or datetime or bool]]] """ - key_value = self.convert_py_type_to_query_type(key_value) query = f""" - match $thing isa {thing}, - has {key} {key_value}, - has {attr} $attribute; + match $thing isa {thing} + """ + for (key, value) in key_attr_list: + value = self.convert_py_type_to_query_type(value) + query += f""", has {key} {value} """ + query += f""" + , has {attr} $attribute; get $attribute; """ return self.match_database(query) - def get_attribute_from_thing(self, thing, key, key_value, attr): + def get_attribute_from_thing(self, thing, key_attr_list, attr): """ Get attribute value from a instance of a thing. :param thing: thing name :type thing: str - :param key: attribute name to identify the instance - :type key: str - :param key_value: attribute value to identify the instance - :type key_value: str or int or float or datetime or bool + :param key_attr_list: list with attribute tuple (name, value) + :type key_attr_list: list[tuple[str, str or int or float or datetime]] :param attr: attribute name to be return :type attr: str :return: List with the attribute values of type attr. :rtype: list[str or int or float or datetime or bool] """ result = self.get_attribute_from_thing_raw( - thing, key, key_value, attr) + thing, key_attr_list, attr) return [self.covert_query_type_to_py_type(r.get('attribute')) for r in result] @@ -583,7 +582,8 @@ def update_attribute_in_thing( thing, key, key_value, attr) self.insert_attribute_in_thing( thing, key, key_value, attr, attr_value) - _result = self.get_attribute_from_thing(thing, key, key_value, attr) + _result = self.get_attribute_from_thing( + thing, [(key, key_value)], attr) return len(_result) > 0 def update_attributes_in_thing(self, match_dict): diff --git a/ros_typedb/test/test_typedb_interface.py b/ros_typedb/test/test_typedb_interface.py index a71e42e..00bf114 100644 --- a/ros_typedb/test/test_typedb_interface.py +++ b/ros_typedb/test/test_typedb_interface.py @@ -64,7 +64,7 @@ def test_insert_attribute_in_thing(typedb_interface, attr, attr_value): typedb_interface.insert_attribute_in_thing( 'person', 'email', 'test@email.test', attr, attr_value) result = typedb_interface.get_attribute_from_thing( - 'person', 'email', 'test@email.test', attr) + 'person', [('email', 'test@email.test')], attr) assert result @@ -77,12 +77,15 @@ def test_insert_attribute_in_thing(typedb_interface, attr, attr_value): datetime.now().isoformat(timespec='milliseconds'))), ]) def test_get_attribute_from_thing(typedb_interface, attr, attr_value): - typedb_interface.insert_entity('person', [('email', 'test@email.test')]) + typedb_interface.insert_entity( + 'person', + [('email', 'test@email.test'), ('gender', 'male')] + ) typedb_interface.insert_attribute_in_thing( 'person', 'email', 'test@email.test', attr, attr_value) result = typedb_interface.get_attribute_from_thing( - 'person', 'email', 'test@email.test', attr) + 'person', [('email', 'test@email.test'), ('gender', 'male')], attr) assert result[0] == attr_value @@ -104,7 +107,7 @@ def test_delete_attribute_from_thing(typedb_interface, attr, attr_value): 'person', 'email', 'test@email.test', attr) result = typedb_interface.get_attribute_from_thing( - 'person', 'email', 'test@email.test', attr) + 'person', [('email', 'test@email.test')], attr) assert len(result) == 0 @@ -126,7 +129,7 @@ def test_update_attribute_in_thing(typedb_interface, attr, attr_value, new_v): result_update = typedb_interface.update_attribute_in_thing( 'person', 'email', 'test@email.test', attr, new_v) result = typedb_interface.get_attribute_from_thing( - 'person', 'email', 'test@email.test', attr) + 'person', [('email', 'test@email.test')], attr) assert result_update is True and result[0] == new_v