opencog / asmoses

MOSES Machine Learning: Meta-Optimizing Semantic Evolutionary Search for the AtomSpace (https://github.com/opencog/atomspace)
https://wiki.opencog.org/w/Meta-Optimizing_Semantic_Evolutionary_Search
Other
38 stars 29 forks source link

Fix help and log msg #93

Closed ngeiswei closed 2 years ago

linas commented 2 years ago

Hi @ngeiswei FYI, I've recently started doing logging in the atomspace, itself. This is harder than reading the log file, but has the bonus of being saved whenever you save the atomspace. Here's a short example:

(define-public (make-data-logger ATOM KEY)
"
  make-data-logger ATOM KEY -- return function that appends data.

  This returns a function that will append data to a Value located
  at KEY on ATOM. The ATOM will then be saved to the currently open
  database connection.

  Example:
     (define anchor (Concept \"anchor\"))
     (define key (Predicate \"key\"))
     (define log-foo (make-data-logger anchor key))
     (log-foo 1)
     (log-foo 2)
     (log-foo 3)
     (cog-value anchor key)
     ; This will return (FloatValue 1 2 3)

  Currently, only numbers, strings, Atoms and Values are supported.
  All logged items must be of the same type as the first logged item.
  The current implementation is not thread-safe.
"
   (lambda (VAL)
      (define v (cog-value ATOM KEY))
      (define typ (if v (cog-type v)
         (cond
            ((real? VAL) 'FloatValue)
            ((string? VAL) 'StringValue)
            ((cog-value? VAL) 'LinkValue)
            (else
               (throw 'unsupported 'make-data-logger
                  "Don't know how to handle this type")))))

      (define old (if v (cog-value->list v) '()))
      (define new (append old (list VAL)))

      ; FIXME: use a thread-safe test-n-set instead.
      (cog-set-value! ATOM KEY (cog-new-value typ new)))
)