OpenCog Framework  Branch: master, revision 6f0b7fc776b08468cf1b74aa9db028f387b4f0c0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
NumberNode.h
Go to the documentation of this file.
1 /*
2  * opencog/atoms/NumberNode.h
3  *
4  * Copyright (C) 2015 Linas Vepstas
5  * All Rights Reserved
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License v3 as
9  * published by the Free Software Foundation and including the exceptions
10  * at http://opencog.org/wiki/Licenses
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program; if not, write to:
19  * Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22 
23 #ifndef _OPENCOG_NUMBER_NODE_H
24 #define _OPENCOG_NUMBER_NODE_H
25 
27 #include <opencog/atomspace/Node.h>
28 
29 namespace opencog
30 {
39 class NumberNode : public Node
40 {
41 protected:
42  double value;
43 
44 public:
45  NumberNode(const std::string& s,
48  // Convert to number and back to string to avoid miscompares.
49  : Node(NUMBER_NODE, std::to_string(std::stod(s)), tv, av),
50  value(std::stod(s))
51  {}
52 
53  NumberNode(double vvv,
56  : Node(NUMBER_NODE, std::to_string(vvv), tv, av),
57  value(vvv)
58  {}
59 
61  : Node(NUMBER_NODE, std::to_string(std::stod(n.getName())),
62  n.getTruthValue()->clone(), n.getAttentionValue()->clone()),
63  value(std::stod(n.getName()))
64  {
65  OC_ASSERT(NUMBER_NODE == n.getType(), "Bad NumberNode constructor!");
66  }
67 
68  static std::string validate(const std::string& str)
69  {
70  return std::to_string(std::stod(str));
71  }
72 
73  double get_value(void) { return value; }
74 };
75 
76 typedef std::shared_ptr<NumberNode> NumberNodePtr;
77 static inline NumberNodePtr NumberNodeCast(const Handle& h)
78  { AtomPtr a(h); return std::dynamic_pointer_cast<NumberNode>(a); }
80  { return std::dynamic_pointer_cast<NumberNode>(a); }
81 
82 // XXX temporary hack ...
83 #define createNumberNode std::make_shared<NumberNode>
84 
86 }
87 
88 #endif // _OPENCOG_NUMBER_NODE_H
AttentionValuePtr getAttentionValue()
Definition: Atom.cc:146
NumberNode(const std::string &s, TruthValuePtr tv=TruthValue::DEFAULT_TV(), AttentionValuePtr av=AttentionValue::DEFAULT_AV())
Definition: NumberNode.h:45
std::shared_ptr< Atom > AtomPtr
Definition: Handle.h:48
static std::string validate(const std::string &str)
Definition: NumberNode.h:68
std::shared_ptr< TruthValue > TruthValuePtr
Definition: TruthValue.h:85
std::shared_ptr< AttentionValue > AttentionValuePtr
Type getType() const
Definition: Atom.h:197
std::shared_ptr< NumberNode > NumberNodePtr
Definition: NumberNode.h:76
double get_value(void)
Definition: NumberNode.h:73
const std::string & getName() const
Definition: Node.h:87
static TruthValuePtr DEFAULT_TV()
Definition: TruthValue.cc:52
static NumberNodePtr NumberNodeCast(const Handle &h)
Definition: NumberNode.h:77
NumberNode(Node &n)
Definition: NumberNode.h:60
static AttentionValuePtr DEFAULT_AV()
to be used as default attention value
TruthValuePtr getTruthValue()
Definition: Atom.cc:104
NumberNode(double vvv, TruthValuePtr tv=TruthValue::DEFAULT_TV(), AttentionValuePtr av=AttentionValue::DEFAULT_AV())
Definition: NumberNode.h:53