OpenCog Framework  Branch: master, revision 6f0b7fc776b08468cf1b74aa9db028f387b4f0c0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
FreeLink.cc
Go to the documentation of this file.
1 /*
2  * opencog/atoms/core/FreeLink.cc
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 #include <opencog/atomspace/atom_types.h>
25 #include "FreeLink.h"
26 
27 using namespace opencog;
28 
32  : Link(FREE_LINK, oset, tv, av)
33 {
34  init();
35 }
36 
40  : Link(FREE_LINK, a, tv, av)
41 {
42  init();
43 }
44 
48  : Link(t, oset, tv, av)
49 {
50  if (not classserver().isA(t, FREE_LINK))
51  throw InvalidParamException(TRACE_INFO, "Expecting a FreeLink");
52 
53  // Derived classes have thier own init routines.
54  if (FREE_LINK != t) return;
55  init();
56 }
57 
61  : Link(t, a, tv, av)
62 {
63  if (not classserver().isA(t, FREE_LINK))
64  throw InvalidParamException(TRACE_INFO, "Expecting a FreeLink");
65 
66  // Derived classes have thier own init routines.
67  if (FREE_LINK != t) return;
68  init();
69 }
70 
71 FreeLink::FreeLink(Type t, const Handle& a, const Handle& b,
74  : Link(t, a, b, tv, av)
75 {
76  if (not classserver().isA(t, FREE_LINK))
77  throw InvalidParamException(TRACE_INFO, "Expecting a FreeLink");
78 
79  // Derived classes have thier own init routines.
80  if (FREE_LINK != t) return;
81  init();
82 }
83 
85  : Link(l)
86 {
87  Type tscope = l.getType();
88  if (not classserver().isA(tscope, FREE_LINK))
89  throw InvalidParamException(TRACE_INFO, "Expecting a FreeLink");
90 
91  // Derived classes have thier own init routines.
92  if (FREE_LINK != tscope) return;
93  init();
94 }
95 
96 /* ================================================================= */
103 void FreeLink::find_vars(std::set<Handle>& varset, const HandleSeq& oset)
104 {
105  for (const Handle& h : oset)
106  {
107  Type t = h->getType();
108  if (QUOTE_LINK == t) continue;
109  if (VARIABLE_NODE == t and
110  0 == varset.count(h))
111  {
112  _varseq.push_back(h);
113  varset.insert(h);
114  }
115  LinkPtr lll(LinkCast(h));
116  if (NULL == lll) continue;
117 
118  find_vars(varset, lll->getOutgoingSet());
119  }
120 }
121 
122 /* ================================================================= */
130 {
131  if (0 < _index.size()) return;
132  size_t sz = _varseq.size();
133  for (size_t i=0; i<sz; i++)
134  _index.insert(std::pair<Handle, unsigned int>(_varseq[i], i));
135 }
136 
137 /* ================================================================= */
138 
139 void FreeLink::init(void)
140 {
141  std::set<Handle> varset;
142  find_vars(varset, _outgoing);
143  build_index();
144 }
145 
147 {
148  throw RuntimeException(TRACE_INFO, "Not reducible!");
149 }
std::vector< Handle > HandleSeq
a list of handles
Definition: Handle.h:246
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
ClassServer & classserver(ClassServerFactory *=ClassServer::createInstance)
Definition: ClassServer.cc:159
static LinkPtr LinkCast(const Handle &h)
Definition: Link.h:263
unsigned short Type
type of Atoms, represented as short integer (16 bits)
Definition: types.h:40