-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #446 from trueagi-io/das
A sketch of DAS integration
- Loading branch information
Showing
2 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
from hyperon import * | ||
from hyperon.ext import register_atoms | ||
|
||
import os | ||
import re | ||
import json | ||
|
||
from hyperon_das import DasAPI | ||
from hyperon_das.das import QueryOutputFormat as Format | ||
from hyperon_das.pattern_matcher import ( | ||
Link, | ||
Node, | ||
Variable, | ||
And, | ||
Or, | ||
Not, | ||
PatternMatchingAnswer, | ||
) | ||
|
||
class DASpace(AbstractSpace): | ||
|
||
def __init__(self, unwrap=True): | ||
super().__init__() | ||
self.das = DasAPI('hash_table') | ||
self.unwrap = unwrap | ||
|
||
def _atom2dict(self, atom): | ||
if isinstance(atom, ExpressionAtom): | ||
return { | ||
"type": "Expression", | ||
"targets": [self._atom2dict(ch) for ch in atom.get_children()] | ||
} | ||
else: | ||
return { | ||
"type": "Symbol", | ||
"name": repr(atom) | ||
} | ||
|
||
def _atom2query(self, atom): | ||
if isinstance(atom, ExpressionAtom): | ||
targets = atom.get_children() | ||
if isinstance(targets[0], SymbolAtom) and targets[0].get_name() == ',': | ||
return And([self._atom2query(ch) for ch in targets[1:]]) | ||
return Link("Expression", ordered=True, | ||
targets=[self._atom2query(ch) for ch in targets]) | ||
else: | ||
if isinstance(atom, VariableAtom): | ||
return Variable(repr(atom)) | ||
else: | ||
return Node("Symbol", repr(atom)) | ||
|
||
def _handle2atom(self, h): | ||
try: | ||
return S(self.das.get_node_name(h)) | ||
except Exception as e: | ||
return E(*[self._handle2atom(ch) for ch in self.das.get_link_targets(h)]) | ||
|
||
def query(self, query_atom): | ||
query = self._atom2query(query_atom) | ||
answer = PatternMatchingAnswer() | ||
new_bindings_set = BindingsSet.empty() | ||
if not query.matched(self.das.db, answer): | ||
return new_bindings_set | ||
for a in answer.assignments: | ||
bindings = Bindings() | ||
for var, val in a.mapping.items(): | ||
# remove '$', because it is automatically added | ||
bindings.add_var_binding(V(var[1:]), self._handle2atom(val)) | ||
new_bindings_set.push(bindings) | ||
return new_bindings_set | ||
|
||
def add(self, atom): | ||
dc = self._atom2dict(atom) | ||
if "name" in dc: | ||
self.das.add_node(dc) | ||
else: | ||
self.das.add_link(dc) | ||
|
||
#def remove(self, atom): | ||
# pass | ||
#def replace(self, from_atom, to_atom): | ||
# pass | ||
#def atom_count(self): | ||
# return self.das.count_atoms() | ||
#def atoms_iter(self): | ||
# return iter(self.atoms_list) | ||
|
||
@register_atoms(pass_metta=True) | ||
def das_atoms(metta): | ||
newDASpaceAtom = OperationAtom('new-das', lambda: [G(SpaceRef(DASpace()))], unwrap=False) | ||
return { | ||
r"new-das": newDASpaceAtom, | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
!(extend-py! dasgate) | ||
|
||
!(bind! &das (new-das)) | ||
|
||
!(add-atom &das (Test (Test 2))) | ||
!(add-atom &das (Best (Test 2))) | ||
!(add-atom &das Test) | ||
|
||
; The simplest match with grounding the variable in the node | ||
!(match &das ($v1 (Test 2)) (This $v1 works)) | ||
; The simplest match with grounding the variable in the link | ||
!(match &das (Test $v2) (This $v2 works)) | ||
|
||
!(add-atom &das (Rest (Test 3))) | ||
!(add-atom &das (Test (Test 3))) | ||
|
||
; Compositional (And) query test | ||
!(match &das (, (Best $x) ($v $x)) ($v $x)) | ||
; !(match &das (, ($v1 $x) (Test $x)) ($v1 Test $x)) | ||
|
||
; !(match &das ($v1 ($v2 2)) (This $v1 works)) |