From e036e9b12567a68693a8eabb91417055d0963510 Mon Sep 17 00:00:00 2001 From: Oleg Date: Wed, 31 Jan 2024 13:40:01 +0300 Subject: [PATCH] fix for variable bindings --- python/sandbox/das_gate/dasgate.py | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/python/sandbox/das_gate/dasgate.py b/python/sandbox/das_gate/dasgate.py index 8206b9562..62818db85 100644 --- a/python/sandbox/das_gate/dasgate.py +++ b/python/sandbox/das_gate/dasgate.py @@ -3,7 +3,6 @@ from hyperon_das import DistributedAtomSpace -# from hyperon_das.utils import QueryOutputFormat from hyperon_das.constants import QueryOutputFormat import time @@ -55,6 +54,17 @@ def _atom2dict_new(self, atom): elif isinstance(atom, GroundedAtom): return {"atom_type": "node", "type": "Symbol", "name": repr(atom)} + def _get_all_vars(self, atom): + stack = [atom] + while stack: + node = stack.pop() + if isinstance(node, VariableAtom): + yield {"atom_type": "variable", "name": repr(node)} + if isinstance(node, ExpressionAtom): + targets = node.get_children() + for ch in targets: + stack.append(ch) + def _atom2query(self, atom): if isinstance(atom, ExpressionAtom): targets = atom.get_children() @@ -92,8 +102,15 @@ def _handle2atom2(self, h): except Exception as e: return E(*[self._handle2atom2(ch) for ch in self._get_link_targets(h)]) + def _handle2atom3(self, h): + if h['type']=='Symbol': + return S(h['name']) + elif h['type']=='Expression': + return E(*[self._handle2atom3(ch) for ch in h['targets']]) + def query(self, query_atom): query = self._atom2dict_new(query_atom) + vars = [x for x in self._get_all_vars(query_atom)] query_params = { "toplevel_only": False, @@ -111,12 +128,8 @@ def query(self, query_atom): b = a[0] else: b = a - val = b['handle'] - if b["type"] == "Expression": - var = 'ex' - else: - var = b['name'] - bindings.add_var_binding(V(var), self._handle2atom(val)) + var = vars[0]['name'][1:] + bindings.add_var_binding(V(var), self._handle2atom3(b)) new_bindings_set.push(bindings) return new_bindings_set