Skip to content

Commit

Permalink
Merge pull request opencog#1894 from linas/numb
Browse files Browse the repository at this point in the history
Handy utility for getting the number of a NumberNode
  • Loading branch information
linas authored Nov 4, 2018
2 parents 2d534de + e53a9f2 commit a5f5c7e
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 13 deletions.
1 change: 1 addition & 0 deletions opencog/guile/SchemeSmob.cc
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ void SchemeSmob::register_procs()

// property getters on atoms
register_proc("cog-name", 1, 0, 0, C(ss_name));
register_proc("cog-number", 1, 0, 0, C(ss_number));
register_proc("cog-type", 1, 0, 0, C(ss_type));
register_proc("cog-arity", 1, 0, 0, C(ss_arity));
register_proc("cog-incoming-set", 1, 0, 0, C(ss_incoming_set));
Expand Down
1 change: 1 addition & 0 deletions opencog/guile/SchemeSmob.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ class SchemeSmob

// Atom properties
static SCM ss_name(SCM);
static SCM ss_number(SCM);
static SCM ss_type(SCM);
static SCM ss_arity(SCM);
static SCM ss_as(SCM);
Expand Down
16 changes: 16 additions & 0 deletions opencog/guile/SchemeSmobAtom.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

#include <opencog/atoms/proto/NameServer.h>
#include <opencog/atoms/proto/ProtoAtom.h>
#include <opencog/atoms/core/NumberNode.h>
#include <opencog/truthvalue/AttentionValue.h>
#include <opencog/truthvalue/CountTruthValue.h>
#include <opencog/truthvalue/TruthValue.h>
Expand Down Expand Up @@ -75,6 +76,21 @@ SCM SchemeSmob::ss_name (SCM satom)
return str;
}

/**
* Return the number of the NumberNode
*/
SCM SchemeSmob::ss_number (SCM satom)
{
Handle h = verify_handle(satom, "cog-number");

NumberNodePtr nn(NumberNodeCast(h));
// Faster than saying if (not nameserver().isA(h->get_type(), NUMBER_NODE))
if (nullptr == nn)
scm_wrong_type_arg_msg("cog-number", 0, satom, "NumberNode");
SCM num = scm_from_double(nn->get_value());
return num;
}

SCM SchemeSmob::ss_type (SCM satom)
{
Handle h = verify_handle(satom, "cog-type");
Expand Down
15 changes: 15 additions & 0 deletions opencog/scm/opencog/base/core-docs.scm
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,21 @@
\"abc\"
")

(set-procedure-property! cog-number 'documentation
"
cog-number NUMBER-NODE
Return the floating point value of the NumberNode NUMBER-NODE.
If it is a NumberNode, then this is the same as saying
(string->number (cog-name NUMBER-NODE))
If it is not a NumberNode, then this will throw an exception.
Example:
; Define a node
guile> (define x (cog-new-node 'NumberNode 42))
guile> (cog-number x)
42.0
")

(set-procedure-property! cog-type 'documentation
"
cog-type ATOM
Expand Down
2 changes: 1 addition & 1 deletion opencog/scm/opencog/rule-engine/rule-engine-utils.scm
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@
"
Convert (NumberNode <number>) to number.
"
(string->number (cog-name A)))
(cog-number A))

;; Very handy and frequent rule precondition.
(define-public (gt-zero-confidence A)
Expand Down
2 changes: 1 addition & 1 deletion tests/query/define-schema.scm
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
; Demonstrate the used of DefinedSchemaNodes
;
(define (get-timestamp)
(NumberNode (number->string (current-time))))
(NumberNode (current-time)))

(DefineLink
(DefinedSchemaNode "set timestamp")
Expand Down
2 changes: 1 addition & 1 deletion tests/query/greater-compute.scm
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@

; Function that takes the square root of a numeric values
(define (eff x)
(NumberNode (sqrt (string->number (cog-name x))))
(NumberNode (sqrt (cog-number x)))
)

; Function that does some computation
Expand Down
2 changes: 1 addition & 1 deletion tests/query/greater_than.scm
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@
;; -----------------------------------------------------
;; This variant uses a hand-rolled scm compare function
(define (richer a b)
(if (> (string->number (cog-name a)) (string->number (cog-name b)))
(if (> (cog-number a) (cog-number b))
(stv 1 1) ;; true
(stv 0 1) ;; false
)
Expand Down
14 changes: 5 additions & 9 deletions tests/scm/SCMExecutionOutputUTest.cxxtest
Original file line number Diff line number Diff line change
Expand Up @@ -339,13 +339,9 @@ void SCMExecutionOutputUTest::test_nested(void)
// Define various generic utilities
eval->eval(
"(define (oc-plus x y)"
" (NumberNode (number->string (+ "
" (string->number (cog-name x)) "
" (string->number (cog-name y))))))"
" (NumberNode (+ (cog-number x) (cog-number y))))"
"(define (oc-times x y)"
" (NumberNode (number->string (* "
" (string->number (cog-name x)) "
" (string->number (cog-name y))))))"
" (NumberNode (* (cog-number x) (cog-number y))))"
);
CHKEV(eval);

Expand All @@ -357,9 +353,9 @@ void SCMExecutionOutputUTest::test_nested(void)
" (ExecutionOutputLink "
" (GroundedSchemaNode \"scm: oc-plus\")"
" (ListLink "
" (NumberNode \"1\")"
" (NumberNode \"2\")))"
" (NumberNode \"3\"))))"
" (NumberNode 1)"
" (NumberNode 2)))"
" (NumberNode 3))))"
);
CHKEV(eval);

Expand Down

0 comments on commit a5f5c7e

Please sign in to comment.