| … | … | @@ -39,9 +39,10 @@ from opencog.type_constructors import ( |
| 39 | 39 | ) |
| 40 | 40 | from opencog.utilities import get_free_variables, load_file |
| 41 | 41 | |
| 42 | | from requests import post |
| 42 | from requests import post, get, delete |
| 43 | 43 | import json |
| 44 | 44 | import re |
| 45 | import time |
| 45 | 46 | |
| 46 | 47 | ############# |
| 47 | 48 | # Constants # |
| … | … | @@ -62,78 +63,6 @@ agent_log.set_component("Agent") |
| 62 | 63 | ############# |
| 63 | 64 | |
| 64 | 65 | |
| 65 | | def 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 | | |
| 125 | | def 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 | | |
| 137 | 66 | def add_to_atomspace(atoms: set[Atom] | list[Atom], atomspace: AtomSpace) -> None: |
| 138 | 67 | """Add all atoms to the atomspace.""" |
| 139 | 68 | |
| … | … | @@ -1365,6 +1294,134 @@ def load_atomspace( |
| 1365 | 1294 | return False |
| 1366 | 1295 | |
| 1367 | 1296 | |
| 1297 | def pre_process_atoms(exp): |
| 1298 | # Processing empty variableset |
| 1299 | exp = re.sub(r"\(VariableSet\)", "", exp) |
| 1300 | exp = re.sub(r"\(VariableSet\s*\)", "", exp) |
| 1301 | # exp = re.sub(r'back', '', exp, flags=re.IGNORECASE) |
| 1302 | |
| 1303 | # Processing szlinks |
| 1304 | exp = re.sub(r";.*\n", "\n", exp, flags=re.MULTILINE) |
| 1305 | exp = re.sub(r"\(ZLink\s+", "(ZLink", exp) |
| 1306 | exp = re.sub(r"\)\s+\)", "))", exp) |
| 1307 | exp = re.sub(r"\)\s+\)", "))", exp) |
| 1308 | |
| 1309 | starting_indices_slinks = [m.start() for m in re.finditer("SLink", exp)] |
| 1310 | starting_indices_zlinks = [m.start() for m in re.finditer("ZLink", exp)] |
| 1311 | |
| 1312 | for i in range(len(starting_indices_zlinks)): |
| 1313 | # Get the index of its preceding zlink |
| 1314 | index_preceding_zlink = -1 if i == 0 else starting_indices_zlinks[i - 1] |
| 1315 | # Count the number of its parent links and the index of the start of its parent slink |
| 1316 | parent_count = 0 |
| 1317 | index_parent_slink = 0 |
| 1318 | for p in starting_indices_slinks: |
| 1319 | if p < starting_indices_zlinks[i] and p > index_preceding_zlink: |
| 1320 | parent_count += 1 |
| 1321 | if parent_count == 1: |
| 1322 | index_parent_slink = p |
| 1323 | if p > starting_indices_zlinks[i]: |
| 1324 | break |
| 1325 | # Form the new string |
| 1326 | if parent_count == 0: |
| 1327 | # ='s are added and removed later to keep the length of the string exp |
| 1328 | # the same while other szlinks are processed. |
| 1329 | # This will keep previously calculated sz indices valid |
| 1330 | exp = ( |
| 1331 | exp[0 : starting_indices_zlinks[i] - 1] |
| 1332 | + "=" * 7 |
| 1333 | + exp[starting_indices_zlinks[i] + 6 :] |
| 1334 | ) |
| 1335 | else: |
| 1336 | len_szlinks = ( |
| 1337 | (starting_indices_zlinks[i] + 4 + 1 + parent_count + 1 - 1) |
| 1338 | - (index_parent_slink - 1) |
| 1339 | + 1 |
| 1340 | ) |
| 1341 | len_time_node = len('(TimeNode "' + str(parent_count) + '")') |
| 1342 | # "`" is added and removed later to keep the length of the string exp |
| 1343 | # the same while other szlinks are processed. |
| 1344 | # This will keep previously calculated sz indices valid |
| 1345 | exp = ( |
| 1346 | exp[0 : index_parent_slink - 1] |
| 1347 | + '(TimeNode "' |
| 1348 | + str(parent_count) |
| 1349 | + '")' |
| 1350 | + "`" * (len_szlinks - len_time_node) |
| 1351 | + exp[starting_indices_zlinks[i] + 4 + 1 + parent_count + 1 :] |
| 1352 | ) |
| 1353 | |
| 1354 | exp = exp.replace("=" * 7, '(TimeNode "0")') |
| 1355 | exp = exp.replace("`", "") |
| 1356 | return exp |
| 1357 | |
| 1358 | |
| 1359 | def post_to_restapi_scheme_endpoint( |
| 1360 | cogscm_info, PORT=5000, IP_ADDRESS="127.0.0.1", cogscm_as=False |
| 1361 | ): |
| 1362 | """ |
| 1363 | Visualize cognitive schema and selected action in each cycle. |
| 1364 | |
| 1365 | cogscm_as: an atomspace containing cognitive schema. |
| 1366 | cogscm_info: a dictionary containing |
| 1367 | - The discovered cognitive schema cogscm |
| 1368 | - human readable form msg |
| 1369 | - cycle count |
| 1370 | |
| 1371 | """ |
| 1372 | # Post data to the visualizer |
| 1373 | uri = "http://" + IP_ADDRESS + ":" + str(PORT) + "/api/v1.1/" |
| 1374 | headers = {"content-type": "application/json"} |
| 1375 | |
| 1376 | if cogscm_as: |
| 1377 | execute_command("(clear)", uri, headers) |
| 1378 | all_cogscms = cogscm_as.get_atoms_by_type( |
| 1379 | types.BackPredictiveImplicationScopeLink |
| 1380 | ) |
| 1381 | print("Discovered {} cogscms".format(len(all_cogscms))) |
| 1382 | |
| 1383 | for bpi in all_cogscms: |
| 1384 | bpi = """ |
| 1385 | {} (ConceptNode "cogscm:{}")""".format( |
| 1386 | bpi, to_human_readable_str(bpi) |
| 1387 | ) |
| 1388 | bpi = pre_process_atoms(str(bpi)) |
| 1389 | execute_command(bpi, uri, headers) |
| 1390 | time.sleep(5) |
| 1391 | else: |
| 1392 | cogscm = cogscm_info["cogscm"] |
| 1393 | cycle = cogscm_info["cycle"] |
| 1394 | msg = cogscm_info["msg"] |
| 1395 | |
| 1396 | if cycle > 49: |
| 1397 | # clear the visualizer atomspace |
| 1398 | execute_command("(clear)", uri, headers) |
| 1399 | else: |
| 1400 | # update the current selected action |
| 1401 | get_response = get(uri + "atoms", params={"type": "ConceptNode"}) |
| 1402 | for atom in get_response.json()["result"]["atoms"]: |
| 1403 | if "cogscm:" in atom["name"]: |
| 1404 | delete_response = delete(uri + "atoms/" + str(atom["handle"])) |
| 1405 | |
| 1406 | cogscm = """ |
| 1407 | {} (ConceptNode "{}") |
| 1408 | """.format( |
| 1409 | cogscm, msg |
| 1410 | ) |
| 1411 | cogscm = pre_process_atoms(str(cogscm)) |
| 1412 | execute_command(cogscm, uri, headers) |
| 1413 | time.sleep(5) |
| 1414 | |
| 1415 | |
| 1416 | def execute_command(scheme_command, uri, headers): |
| 1417 | """ |
| 1418 | Execute scheme command |
| 1419 | command = {'command': '(ConceptNode "cat")'} |
| 1420 | """ |
| 1421 | command = {"command": scheme_command} |
| 1422 | post_response = post(uri + "scheme", data=json.dumps(command), headers=headers) |
| 1423 | |
| 1424 | |
| 1368 | 1425 | class MinerLogger: |
| 1369 | 1426 | |
| 1370 | 1427 | """Quick and dirty miner logger Python bindings""" |