Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#235] Create custom serialize and deserialize to QueryAnswer #236

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 14 additions & 17 deletions hyperon_das/query_engines.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,20 +317,13 @@ def query(
query: Union[List[Dict[str, Any]], Dict[str, Any]],
parameters: Optional[Dict[str, Any]] = {},
) -> Union[QueryAnswerIterator, List[Tuple[Assignment, Dict[str, str]]]]:
no_iterator = parameters.get("no_iterator", False)
if no_iterator:
logger().debug(
{
'message': '[DistributedAtomSpace][query] - Start',
'data': {'query': query, 'parameters': parameters},
}
)
logger().debug({f"query: {query} - parameters: {str(parameters)}"})

query_results = self._recursive_query(query, parameters)
if no_iterator:
answer = []
for result in query_results:
answer.append(tuple([result.assignment, result.subgraph]))
logger().debug(f"query: {query} result: {str(answer)}")

if parameters.get("no_iterator", False):
answer = [result.serialize() for result in query_results]
logger().debug(f"query: {query} - result: {str(answer)}")
return answer
else:
return query_results
Expand Down Expand Up @@ -484,13 +477,16 @@ def query(
parameters: Optional[Dict[str, Any]] = {},
) -> List[Dict[str, Any]]:
query_scope = parameters.get('query_scope', 'remote_only')

if query_scope == 'remote_only' or query_scope == 'synchronous_update':
if query_scope == 'synchronous_update':
self.commit()
previous_value = parameters.get('no_iterator', False)

parameters['no_iterator'] = True
answer = self.remote_das.query(query, parameters)
parameters['no_iterator'] = previous_value

response = self.remote_das.query(query, parameters)

answer = [QueryAnswer.deserialize(resp) for resp in response]
elif query_scope == 'local_only':
answer = self.local_query_engine.query(query, parameters)
elif query_scope == 'local_and_remote':
Expand All @@ -505,7 +501,8 @@ def query(
message=f'Invalid value for "query_scope": "{query_scope}"'
)
)
return answer

return ListIterator(answer)

def count_atoms(self) -> Tuple[int, int]:
local_answer = self.local_query_engine.count_atoms()
Expand Down
7 changes: 7 additions & 0 deletions hyperon_das/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,13 @@ class QueryAnswer:
subgraph: Optional[Dict] = None
assignment: Optional[Assignment] = None

def serialize(self) -> tuple:
return tuple([self.assignment, self.subgraph])

@classmethod
def deserialize(cls, data: tuple) -> 'QueryAnswer':
return cls(subgraph=data[1], assignment=data[0])


def get_package_version(package_name: str) -> str:
package_module = import_module(package_name)
Expand Down
12 changes: 7 additions & 5 deletions tests/integration/test_remote_das.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def test_query(self, remote_das: DistributedAtomSpace):
metta_animal_base_handles.inheritance_rhino_mammal,
]

answer = remote_das.query(
response = remote_das.query(
{
"atom_type": "link",
"type": "Expression",
Expand All @@ -163,13 +163,15 @@ def test_query(self, remote_das: DistributedAtomSpace):
{"atom_type": "variable", "name": "v1"},
{"atom_type": "node", "type": "Symbol", "name": '"mammal"'},
],
},
{'no_iterator': True},
}
)

assert len(answer) == 4
answers = [i for i in response]

assert len(answers) == 4

for _, link in answer:
for answer in answers:
_, link = answer.serialize()
assert link['handle'] in all_inheritance_mammal
if link['handle'] == metta_animal_base_handles.inheritance_chimp_mammal:
assert _check_docs(
Expand Down
Loading