OpenCog Framework  Branch: master, revision 6f0b7fc776b08468cf1b74aa9db028f387b4f0c0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Link.h
Go to the documentation of this file.
1 /*
2  * opencog/atomspace/Link.h
3  *
4  * Copyright (C) 2002-2007 Novamente LLC
5  * Copyright (C) 2008-2010 OpenCog Foundation
6  * All Rights Reserved
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Affero General Public License v3 as
10  * published by the Free Software Foundation and including the exceptions
11  * at http://opencog.org/wiki/Licenses
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU Affero General Public License
19  * along with this program; if not, write to:
20  * Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  */
23 
24 #ifndef _OPENCOG_LINK_H
25 #define _OPENCOG_LINK_H
26 
27 #include <string>
28 
29 #include <opencog/util/oc_assert.h>
30 #include <opencog/atomspace/Atom.h>
31 
32 namespace opencog
33 {
34 class AtomTable;
39 typedef unsigned short Arity;
41 
50 class Link : public Atom
51 {
52  friend class AtomTable;
53 
54 private:
55  void init(const HandleSeq&) throw (InvalidParamException);
56  void resort(void);
57 
58  Link(const Link &l) : Atom(0)
59  { OC_ASSERT(false, "Link: bad use of copy ctor"); }
60 
61 protected:
62 
66 
67 public:
77  Link(Type t, const HandleSeq& oset,
80  : Atom(t, tv, av)
81  {
82  init(oset);
83  }
84 
85  Link(Type t, const Handle& h,
88  : Atom(t, tv, av)
89  {
90  HandleSeq oset;
91  oset.push_back(h);
92  init(oset);
93  }
94 
95  Link(Type t, const Handle& ha, const Handle &hb,
98  : Atom(t, tv, av)
99  {
100  HandleSeq oset;
101  oset.push_back(ha);
102  oset.push_back(hb);
103  init(oset);
104  }
105 
106  Link(Type t, const Handle& ha, const Handle &hb, const Handle &hc,
109  : Atom(t, tv, av)
110  {
111  HandleSeq oset;
112  oset.push_back(ha);
113  oset.push_back(hb);
114  oset.push_back(hc);
115  init(oset);
116  }
117  Link(Type t, const Handle& ha, const Handle &hb,
118  const Handle &hc, const Handle &hd,
121  : Atom(t, tv, av)
122  {
123  HandleSeq oset;
124  oset.push_back(ha);
125  oset.push_back(hb);
126  oset.push_back(hc);
127  oset.push_back(hd);
128  init(oset);
129  }
130 
135  Link(Link &l)
136  : Atom(l.getType(),
137  l.getTruthValue()->clone(),
138  l.getAttentionValue()->clone())
139  {
140  init(l.getOutgoingSet());
141  }
142 
146  ~Link();
147 
148  inline Arity getArity() const {
149  return _outgoing.size();
150  }
151 
158  inline const HandleSeq& getOutgoingSet() const
159  {
160  return _outgoing;
161  }
168  inline Handle getOutgoingAtom(Arity pos) const throw (RuntimeException)
169  {
170  // Checks for a valid position
171  if (pos < getArity()) {
172  return _outgoing[pos];
173  } else {
174  throw RuntimeException(TRACE_INFO, "invalid outgoing set index %d", pos);
175  }
176  }
177 
182  template<class T>
183  inline bool foreach_outgoing(bool (T::*cb)(const Handle&), T *data)
184  {
185  for (const Handle& out_h : getOutgoingSet()) {
186  if ((data->*cb)(out_h)) return true;
187  }
188  return false;
189  }
190 
196  std::string toString(std::string indent = "");
197 
206  std::string toShortString(std::string indent = "");
207 
215  bool isSource(Handle) const throw (InvalidParamException);
216 
226  bool isSource(size_t) const throw (IndexErrorException, InvalidParamException);
227 
235  bool isTarget(Handle) const throw (InvalidParamException);
236 
246  bool isTarget(size_t) const throw (IndexErrorException, InvalidParamException);
247 
253  virtual bool operator==(const Atom&) const;
254 
260  virtual bool operator!=(const Atom&) const;
261 };
262 
263 static inline LinkPtr LinkCast(const Handle& h)
264  { AtomPtr a(h); return std::dynamic_pointer_cast<Link>(a); }
265 static inline LinkPtr LinkCast(const AtomPtr& a)
266  { return std::dynamic_pointer_cast<Link>(a); }
267 
268 // XXX temporary hack ...
269 #define createLink std::make_shared<Link>
270 
272 } // namespace opencog
273 
274 #endif // _OPENCOG_LINK_H
AttentionValuePtr getAttentionValue()
Definition: Atom.cc:146
std::vector< Handle > HandleSeq
a list of handles
Definition: Handle.h:246
std::shared_ptr< Atom > AtomPtr
Definition: Handle.h:48
std::shared_ptr< TruthValue > TruthValuePtr
Definition: TruthValue.h:85
std::shared_ptr< AttentionValue > AttentionValuePtr
Type getType() const
Definition: Atom.h:197
std::shared_ptr< Link > LinkPtr
Definition: Atom.h:53
static TruthValuePtr DEFAULT_TV()
Definition: TruthValue.cc:52
static LinkPtr LinkCast(const Handle &h)
Definition: Link.h:263
static AttentionValuePtr DEFAULT_AV()
to be used as default attention value
TruthValuePtr getTruthValue()
Definition: Atom.cc:104
unsigned short Type
type of Atoms, represented as short integer (16 bits)
Definition: types.h:40
unsigned short Arity
arity of Links, represented as short integer (16 bits)
Definition: Link.h:40