Skip to content

Commit

Permalink
✨ get_attributes list of keys as input
Browse files Browse the repository at this point in the history
  • Loading branch information
Rezenders committed Jan 8, 2024
1 parent e3e7894 commit 8315431
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 21 deletions.
32 changes: 16 additions & 16 deletions ros_typedb/ros_typedb/typedb_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down Expand Up @@ -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):
Expand Down
13 changes: 8 additions & 5 deletions ros_typedb/test/test_typedb_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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

Expand All @@ -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

Expand All @@ -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

Expand Down

0 comments on commit 8315431

Please sign in to comment.