diff --git a/hyperon_das/query_engines.py b/hyperon_das/query_engines.py index ed06e784..960617b0 100644 --- a/hyperon_das/query_engines.py +++ b/hyperon_das/query_engines.py @@ -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 @@ -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': @@ -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() diff --git a/hyperon_das/utils.py b/hyperon_das/utils.py index fb222041..7da8d929 100644 --- a/hyperon_das/utils.py +++ b/hyperon_das/utils.py @@ -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) diff --git a/tests/integration/test_remote_das.py b/tests/integration/test_remote_das.py index d72ac65a..f70f173a 100644 --- a/tests/integration/test_remote_das.py +++ b/tests/integration/test_remote_das.py @@ -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", @@ -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(