| … | … | @@ -320,10 +320,48 @@ class OpencogAgent: |
| 320 | 320 | self.percepta_record.append(set()) |
| 321 | 321 | self.percepta_record[i].add(timed_atom) |
| 322 | 322 | |
| 323 | def timed_percepta_to_scheme_str(self, timed_percepta: set[Atom], with_member: bool = False) -> str: |
| 324 | """Convert percepta at a given cycle into a string in Scheme format. |
| 325 | |
| 326 | Percepta are preceded by a comment in human readable form. |
| 327 | |
| 328 | If with_member is set to True then each perceptum is wrapped with |
| 329 | a member link from it to the percepta record concept. |
| 330 | |
| 331 | """ |
| 332 | |
| 333 | # Wrap member to percepta record concept around each perceptum |
| 334 | if with_member: |
| 335 | timed_percepta = {self.add_to_percepta_atomspace(timed_perceptum) |
| 336 | for timed_perceptum in timed_percepta} |
| 337 | cmt = "\n".join(";; " + to_human_readable_str(tpm) for tpm in timed_percepta) |
| 338 | scm = "\n".join(tpm.long_string() for tpm in timed_percepta) |
| 339 | return "\n".join([cmt, scm]) |
| 340 | |
| 341 | def percepta_record_to_scheme_str(self, with_member: bool = False) -> str: |
| 342 | """Convert a percepta record into a string in Scheme format. |
| 343 | |
| 344 | Each perception is preceded by a comment in human readable form. |
| 345 | |
| 346 | """ |
| 347 | |
| 348 | return "\n\n".join([self.timed_percepta_to_scheme_str(tp, with_member) |
| 349 | for tp in self.percepta_record]) |
| 350 | |
| 351 | def add_to_percepta_atomspace(self, timed_atom: Atom) -> Atom: |
| 352 | """Add member link around timestamped atom to Percepta Atomspace. |
| 353 | |
| 354 | Return the added member link. |
| 355 | |
| 356 | """ |
| 357 | |
| 358 | mbr = MemberLink(timed_atom, self.percepta_record_cpt, tv=TRUE_TV) |
| 359 | return self.percepta_atomspace.add_atom(mbr) |
| 360 | |
| 323 | 361 | def record(self, atom: Atom, i: int, tv=None) -> Atom: |
| 324 | 362 | """Timestamp and record an atom to the Percepta Record. |
| 325 | 363 | |
| 326 | | That is add the following in the atomspace |
| 364 | That is add the following in the percepta atomspace |
| 327 | 365 | |
| 328 | 366 | MemberLink (stv 1 1) |
| 329 | 367 | AtTimeLink <tv> |
| … | … | @@ -337,9 +375,8 @@ class OpencogAgent: |
| 337 | 375 | """ |
| 338 | 376 | |
| 339 | 377 | timed_atom = timestamp(atom, i, tv) |
| 340 | | mbr = MemberLink(timed_atom, self.percepta_record_cpt, tv=TRUE_TV) |
| 341 | 378 | self.insert_to_percepta_record(timed_atom, i) |
| 342 | | return self.percepta_atomspace.add_atom(mbr) |
| 379 | return self.add_to_percepta_atomspace(timed_atom) |
| 343 | 380 | |
| 344 | 381 | def make_goal(self) -> Atom: |
| 345 | 382 | """Define the goal of the current iteration. |
| … | … | @@ -482,7 +519,7 @@ class OpencogAgent: |
| 482 | 519 | # debugging the pattern miner |
| 483 | 520 | agent_log.fine( |
| 484 | 521 | "Percepta record:\n{}".format( |
| 485 | | percepta_record_to_scheme_str(self.percepta_record) |
| 522 | self.percepta_record_to_scheme_str() |
| 486 | 523 | ) |
| 487 | 524 | ) |
| 488 | 525 | |
| … | … | @@ -1609,10 +1646,9 @@ class OpencogAgent: |
| 1609 | 1646 | |
| 1610 | 1647 | """ |
| 1611 | 1648 | |
| 1612 | | # TODO: better print the percepta_record using |
| 1613 | | # percepta_record_to_scheme_str (though the member links |
| 1614 | | # should also be included). |
| 1615 | | return save_atomspace(self.percepta_atomspace, filepath, overwrite) |
| 1649 | with open(filepath, "w" if overwrite else "a") as file: |
| 1650 | file.write(self.percepta_record_to_scheme_str(with_member=True) + "\n") |
| 1651 | return True |
| 1616 | 1652 | |
| 1617 | 1653 | def load_percepta_atomspace( |
| 1618 | 1654 | self, filepath: str, overwrite: bool = True, fast: bool = True |
| … | … | @@ -1636,10 +1672,17 @@ class OpencogAgent: |
| 1636 | 1672 | so that new percepts do not have the same timestamps as the |
| 1637 | 1673 | just loaded ones. |
| 1638 | 1674 | |
| 1675 | It is assumed that the percepta record concept is |
| 1676 | |
| 1677 | Concept "Percepta Record" |
| 1678 | |
| 1639 | 1679 | """ |
| 1640 | 1680 | |
| 1641 | 1681 | success = load_atomspace(self.percepta_atomspace, filepath, overwrite) |
| 1642 | 1682 | if success: |
| 1683 | pas_roots = atomspace_roots(self.percepta_atomspace) |
| 1684 | if overwrite: |
| 1685 | self.percepta_record.clear() |
| 1643 | 1686 | print("TODO") |
| 1644 | 1687 | # NEXT: take care of filling the percepta record |
| 1645 | 1688 | # NEXT: update the cycle count as well |