OpenCog Framework  Branch: master, revision 6f0b7fc776b08468cf1b74aa9db028f387b4f0c0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
PatternTerm.h
Go to the documentation of this file.
1 /*
2  * PatternTerm.h
3  *
4  * Copyright (C) 2015 OpenCog Foundation
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  * Created by Jacek Świergocki <jswiergo@gmail.com> July 2015
23  */
24 
25 #ifndef _OPENCOG_PATTERN_TERM_H
26 #define _OPENCOG_PATTERN_TERM_H
27 
28 #include <vector>
29 
31 #include <opencog/atomspace/Link.h>
32 
33 namespace opencog {
34 
36 typedef std::shared_ptr<PatternTerm> PatternTermPtr;
37 typedef std::vector<PatternTermPtr> PatternTermSeq;
38 
40 {
41  protected:
45 
46  public:
47  static const PatternTermPtr UNDEFINED;
48 
50  {
53  }
54 
55  PatternTerm(const PatternTermPtr& parent, const Handle& h)
56  {
57  _parent = parent;
58  _handle = h;
59  }
60 
62  {
63  _outgoing.push_back(ptm);
64  }
65 
66  inline Handle getHandle()
67  {
68  return _handle;
69  };
70 
72  {
73  return _parent;
74  };
75 
76  inline const PatternTermSeq& getOutgoingSet() const
77  {
78  return _outgoing;
79  }
80 
81  inline Arity getArity() const {
82  return _outgoing.size();
83  }
84 
86  throw (RuntimeException)
87  {
88  // Checks for a valid position
89  if (pos < getArity()) {
90  return _outgoing[pos];
91  } else {
92  throw RuntimeException(TRACE_INFO,
93  "invalid outgoing set index %d", pos);
94  }
95  }
96 
97  inline std::string toString(std::string indent = ":") const
98  {
99  if (_handle == Handle::UNDEFINED) return "-";
100  std::string str = _parent->toString();
101  str += indent + std::to_string(_handle.value());
102  return str;
103  }
104 
105 };
106 
107 } // namespace opencog
108 
109 using namespace opencog;
110 
111 namespace std {
112 
113 // We need to overload standard comparison operator for PatternTerm pointers.
114 // Now we do not care much about complexity of this comparison. The cases of
115 // queries having repeated atoms that are deep should be very rare. So we just
116 // traverse up towards root node. Typically we compare only the first level
117 // handles on this path.
118 template<>
119 struct less<PatternTermPtr>
120 {
121  bool operator()(const PatternTermPtr& lhs, const PatternTermPtr& rhs) const
122  {
123  const Handle& lHandle = lhs->getHandle();
124  const Handle& rHandle = rhs->getHandle();
125  if (lHandle == rHandle)
126  {
127  if (lHandle == Handle::UNDEFINED) return false;
128  return lhs->getParent() < rhs->getParent();
129  }
130  return lHandle < rHandle;
131  }
132 
133 };
134 
135 }; // namespace std;
136 
137 #endif // _OPENCOG_PATTERN_TERM_H
PatternTermPtr getOutgoingTerm(Arity pos) const
Definition: PatternTerm.h:85
std::shared_ptr< PatternTerm > PatternTermPtr
Definition: PatternTerm.h:35
PatternTermSeq _outgoing
Definition: PatternTerm.h:44
Handle getHandle()
Definition: Atom.h:211
std::vector< PatternTermPtr > PatternTermSeq
Definition: PatternTerm.h:37
void addOutgoingTerm(const PatternTermPtr &ptm)
Definition: PatternTerm.h:61
static const Handle UNDEFINED
Definition: Handle.h:77
bool operator()(const PatternTermPtr &lhs, const PatternTermPtr &rhs) const
Definition: PatternTerm.h:121
Arity getArity() const
Definition: PatternTerm.h:81
static const PatternTermPtr UNDEFINED
Definition: PatternTerm.h:47
UUID value(void) const
Definition: Handle.h:85
std::string toString(std::string indent=":") const
Definition: PatternTerm.h:97
unsigned short Arity
arity of Links, represented as short integer (16 bits)
Definition: Link.h:40
const PatternTermSeq & getOutgoingSet() const
Definition: PatternTerm.h:76
PatternTermPtr getParent()
Definition: PatternTerm.h:71
PatternTermPtr _parent
Definition: PatternTerm.h:43
PatternTerm(const PatternTermPtr &parent, const Handle &h)
Definition: PatternTerm.h:55