OpenCog Framework  Branch: master, revision 6f0b7fc776b08468cf1b74aa9db028f387b4f0c0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
DefineLink.cc
Go to the documentation of this file.
1 /*
2  * DefineLink.cc
3  *
4  * Copyright (C) 2015 Linas Vepstas
5  *
6  * Author: Linas Vepstas <linasvepstas@gmail.com> January 2009
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
11  * exceptions
12  * at http://opencog.org/wiki/Licenses
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU Affero General Public
20  * License
21  * along with this program; if not, write to:
22  * Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24  */
25 
27 
28 #include "DefineLink.h"
29 
30 using namespace opencog;
31 
32 void DefineLink::init(const HandleSeq& oset)
33 {
34  // Must have name and body
35  if (2 != oset.size())
36  throw InvalidParamException(TRACE_INFO,
37  "Expecting name and definition, got size %d", oset.size());
38 
39  _alias = oset[0];
40  _definition = oset[1];
41 
42  // The name must not be used in another definition
43  IncomingSet defs = _alias->getIncomingSetByType(DEFINE_LINK);
44  for (LinkPtr def : defs)
45  if (def->isSource(_alias))
46  throw InvalidParamException(TRACE_INFO,
47  "Cannot define %s\n"
48  "with alias %s\n"
49  "as it is already defined in %s",
50  _definition->toString().c_str(),
51  _alias->toString().c_str(),
52  def->toString().c_str());
53 }
54 
57  : Link(DEFINE_LINK, oset, tv, av)
58 {
59  init(oset);
60 }
61 
62 DefineLink::DefineLink(const Handle& name, const Handle& defn,
64  : Link(DEFINE_LINK, HandleSeq({name, defn}), tv, av)
65 {
66  init(getOutgoingSet());
67 }
68 
70  : Link(l)
71 {
72  // Type must be as expected
73  Type tscope = l.getType();
74  if (not classserver().isA(tscope, DEFINE_LINK))
75  {
76  const std::string& tname = classserver().getTypeName(tscope);
77  throw InvalidParamException(TRACE_INFO,
78  "Expecting a DefineLink, got %s", tname.c_str());
79  }
80 
81  init(l.getOutgoingSet());
82 }
83 
85  // Get all DefineLinks associated with that alias, beware that it
86  // will also return DefineLink with that alias as definition body.
87  IncomingSet defs = alias->getIncomingSetByType(DEFINE_LINK);
88 
89  // Return the first (supposedly unique) definition
90  for (LinkPtr defl : defs) {
91  DefineLinkPtr def(DefineLinkCast(defl->getHandle()));
92  if (def->get_alias() == alias)
93  return def->get_definition();
94  }
95 
96  // There is no definition for that alias
97  throw InvalidParamException(TRACE_INFO,
98  "Cannot find defined hypergraph for atom %s",
99  alias->toString().c_str());
100  return Handle::UNDEFINED;
101 }
102 
103 /* ===================== END OF FILE ===================== */
static DefineLinkPtr DefineLinkCast(const Handle &h)
Definition: DefineLink.h:101
std::vector< Handle > HandleSeq
a list of handles
Definition: Handle.h:246
std::shared_ptr< DefineLink > DefineLinkPtr
Definition: DefineLink.h:100
std::shared_ptr< TruthValue > TruthValuePtr
Definition: TruthValue.h:85
std::shared_ptr< AttentionValue > AttentionValuePtr
virtual std::string toString(std::string indent="")=0
Type getType() const
Definition: Atom.h:197
std::shared_ptr< Link > LinkPtr
Definition: Atom.h:53
ClassServer & classserver(ClassServerFactory *=ClassServer::createInstance)
Definition: ClassServer.cc:159
static const Handle UNDEFINED
Definition: Handle.h:77
const std::string & getTypeName(Type type)
Definition: ClassServer.cc:148
std::vector< LinkPtr > IncomingSet
Definition: Atom.h:55
unsigned short Type
type of Atoms, represented as short integer (16 bits)
Definition: types.h:40
OutputIterator getIncomingSetByType(OutputIterator result, Type type, bool subclass=false)
Definition: Atom.h:353