| … | … | @@ -39,6 +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 |
| 43 | import json |
| 44 | import re |
| 45 | |
| 42 | 46 | ############# |
| 43 | 47 | # Constants # |
| 44 | 48 | ############# |
| … | … | @@ -58,6 +62,78 @@ agent_log.set_component("Agent") |
| 58 | 62 | ############# |
| 59 | 63 | |
| 60 | 64 | |
| 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 | |
| 61 | 137 | def add_to_atomspace(atoms: set[Atom] | list[Atom], atomspace: AtomSpace) -> None: |
| 62 | 138 | """Add all atoms to the atomspace.""" |
| 63 | 139 | |