OverviewFilesCommitsIssuesDocsReleasesPeers

opencog-rocca commits / 381773c

Connect to the atomspace visualizer
381773c77875452d4754b6c89cbdb9d38bf91d30
by Hedra <tanksh24@gmail.com> · 2022-03-10T16:00:11.000Z

Changed files (2) +81 -0

rocca/agents/core.py +5 -0
……@@ -792,6 +792,7 @@ class OpencogAgent:
792792
793793 add_to_atomspace(new_cogscms, self.cogscms_atomspace)
794794 self.cognitive_schematics.update(new_cogscms)
795 post_to_restapi_scheme_endpoint(new_cogscms, PORT=5001)
795796
796797 def learn(self):
797798 """Discover patterns in the world and in itself."""
……@@ -1557,6 +1558,7 @@ class OpencogAgent:
15571558 self.cycle_count, atoms_to_scheme_str(obs_record)
15581559 )
15591560 )
1561 post_to_restapi_scheme_endpoint(obs_record)
15601562
15611563 # Make the goal for that iteration
15621564 goal = self.make_goal()
……@@ -1575,6 +1577,7 @@ class OpencogAgent:
15751577 self.cycle_count, len(cogscms), atoms_to_scheme_str(cogscms)
15761578 )
15771579 )
1580 post_to_restapi_scheme_endpoint(cogscms)
15781581
15791582 # Deduce the action distribution
15801583 mxmdl = self.deduce(cogscms)
……@@ -1607,6 +1610,7 @@ class OpencogAgent:
16071610 self.cycle_count, atom_to_scheme_str(action)
16081611 )
16091612 )
1613 post_to_restapi_scheme_endpoint(action_record)
16101614
16111615 # Increment the counter for that action and log it
16121616 self.action_counter[action] += 1
……@@ -1647,6 +1651,7 @@ class OpencogAgent:
16471651 self.cycle_count, atom_to_scheme_str(reward_record)
16481652 )
16491653 )
1654 post_to_restapi_scheme_endpoint(action_record)
16501655
16511656 return done
16521657
rocca/agents/utils.py +76 -0
……@@ -39,6 +39,10 @@ from opencog.type_constructors import (
3939)
4040from opencog.utilities import get_free_variables, load_file
4141
42from requests import post
43import json
44import re
45
4246#############
4347# Constants #
4448#############
……@@ -58,6 +62,78 @@ agent_log.set_component("Agent")
5862#############
5963
6064
65def pre_process_atoms(exp):
66 # Processing empty variableset
67 exp = re.sub(r"\(VariableSet\)", "", exp)
68 exp = re.sub(r"\(VariableSet\s*\)", "", exp)
69 # exp = re.sub(r'back', '', exp, flags=re.IGNORECASE)
70
71 # Processing szlinks
72 exp = re.sub(r";.*\n", "\n", exp, flags=re.MULTILINE)
73 exp = re.sub(r"\(ZLink\s+", "(ZLink", exp)
74 exp = re.sub(r"\)\s+\)", "))", exp)
75 exp = re.sub(r"\)\s+\)", "))", exp)
76
77 starting_indices_slinks = [m.start() for m in re.finditer("SLink", exp)]
78 starting_indices_zlinks = [m.start() for m in re.finditer("ZLink", exp)]
79
80 for i in range(len(starting_indices_zlinks)):
81 # Get the index of its preceding zlink
82 index_preceding_zlink = -1 if i == 0 else starting_indices_zlinks[i - 1]
83 # Count the number of its parent links and the index of the start of its parent slink
84 parent_count = 0
85 index_parent_slink = 0
86 for p in starting_indices_slinks:
87 if p < starting_indices_zlinks[i] and p > index_preceding_zlink:
88 parent_count += 1
89 if parent_count == 1:
90 index_parent_slink = p
91 if p > starting_indices_zlinks[i]:
92 break
93 # Form the new string
94 if parent_count == 0:
95 # ='s are added and removed later to keep the length of the string exp the same while other szlinks are processed.
96 # This will keep previously calculated sz indices valid
97 exp = (
98 exp[0 : starting_indices_zlinks[i] - 1]
99 + "=" * 7
100 + exp[starting_indices_zlinks[i] + 6 :]
101 )
102 else:
103 len_szlinks = (
104 (starting_indices_zlinks[i] + 4 + 1 + parent_count + 1 - 1)
105 - (index_parent_slink - 1)
106 + 1
107 )
108 len_time_node = len('(TimeNode "' + str(parent_count) + '")')
109 # "`" is added and removed later to keep the length of the string exp the same while other szlinks are processed.
110 # This will keep previously calculated sz indices valid
111 exp = (
112 exp[0 : index_parent_slink - 1]
113 + '(TimeNode "'
114 + str(parent_count)
115 + '")'
116 + "`" * (len_szlinks - len_time_node)
117 + exp[starting_indices_zlinks[i] + 4 + 1 + parent_count + 1 :]
118 )
119
120 exp = exp.replace("=" * 7, '(TimeNode "0")')
121 exp = exp.replace("`", "")
122 return exp
123
124
125def post_to_restapi_scheme_endpoint(cogscm, PORT=5000, IP_ADDRESS="127.0.0.1"):
126 """Post data to the visualizer"""
127 uri = "http://" + IP_ADDRESS + ":" + str(PORT) + "/api/v1.1/"
128 headers = {"content-type": "application/json"}
129
130 # Execute scheme command
131 # command = {'command': '(ConceptNode "cat")'}
132 if cogscm:
133 command = {"command": pre_process_atoms(str(cogscm))}
134 post_response = post(uri + "scheme", data=json.dumps(command), headers=headers)
135
136
61137def add_to_atomspace(atoms: set[Atom] | list[Atom], atomspace: AtomSpace) -> None:
62138 """Add all atoms to the atomspace."""
63139