-
Notifications
You must be signed in to change notification settings - Fork 5
/
mettalog_das_client.py
executable file
·88 lines (71 loc) · 2.7 KB
/
mettalog_das_client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
print(";; Doing HD...",__name__)
from hyperon import *
from mettalog import *
from hyperon_das import DistributedAtomSpace
def _name(link, index, typed = False):
named_type = f"{link['targets'][index]['type']}:" if typed else ''
return f"{named_type}{link['targets'][index]['name']}"
def _print_query_answer(query_answer, typed = False):
if query_answer:
for link in query_answer:
if len(link['targets']) == 2:
print(f"{link['type']}: {_name(link, 0)} -> {_name(link, 1)}")
elif len(link['targets']) == 3:
print(f"{link['type']}: {_name(link, 0)}({_name(link, 1, typed)}) -> {_name(link, 2, typed)}")
else:
assert False
host = '104.238.183.115'
port = '8081'
das = DistributedAtomSpace(query_engine='remote', host=host, port=port)
print(f"Connected to DAS at {host}:{port}")
def _filter(query_answer, index, value):
filtered = []
for link in query_answer:
if link['targets'][index]['type'] == value:
filtered.append(link)
return filtered
def _fbgns(das, symbol, handles=False):
answer = das.query({
"atom_type": "link",
"type": "Execution",
"targets": [
{"atom_type": "node", "type": "Schema", "name": "Schema:fb_synonym_primary_FBid"},
{"atom_type": "node", "type": "Verbatim", "name": symbol},
{"atom_type": "variable", "name": "v1"},
]
})
if handles:
return [link['targets'][2]['handle'] for link in answer]
else:
return [link['targets'][2]['name'] for link in answer]
def query(das, symbol, node_type = None):
fbgns = _fbgns(das, symbol)
print(f"FBgn: {fbgns}")
answer = []
for fbgn in fbgns:
query_answer = das.query({
"atom_type": "link",
"type": "Execution",
"targets": [
{"atom_type": "variable", "name": "v0"},
{"atom_type": "variable", "name": "v1"},
{"atom_type": "node", "type": "Verbatim", "name": fbgn},
]
})
if node_type:
query_answer = _filter(query_answer, 1, node_type)
answer.extend(query_answer)
query_answer = das.query({
"atom_type": "link",
"type": "Execution",
"targets": [
{"atom_type": "variable", "name": "v0"},
{"atom_type": "node", "type": "Verbatim", "name": fbgn},
{"atom_type": "variable", "name": "v1"},
]
})
if node_type:
query_answer = _filter(query_answer, 0, node_type)
answer.extend(query_answer)
return answer
_print_query_answer(query(das, "Myc", "BiologicalProcess"))