Lumiera  0.pre.03
»edit your freedom«
common/advice/binding.cpp
Go to the documentation of this file.
1 /*
2  Binding - pattern defining a specific attachment to the Advice system
3 
4  Copyright (C) Lumiera.org
5  2010, Hermann Vosseler <Ichthyostega@web.de>
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of
10  the License, or (at your option) any later version.
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 General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 
21 * *****************************************************/
22 
23 
34 #include "lib/symbol.hpp"
35 #include "lib/util.hpp"
36 
37 #include <boost/functional/hash.hpp>
38 #include <boost/lexical_cast.hpp>
39 #include <regex>
40 
41 
42 using lib::Literal;
43 using util::isnil;
44 
45 using std::regex;
46 using std::smatch;
47 using std::sregex_iterator;
48 using std::regex_constants::match_continuous;
49 using boost::hash_combine;
50 using boost::lexical_cast;
51 
52 
53 namespace lumiera{
54 namespace advice {
55 
56  LUMIERA_ERROR_DEFINE (BINDING_PATTERN_SYNTAX, "Unable to parse the given binding pattern definition");
57 
58 
59 
60 
61 
63  namespace{ // Implementation details
64 
65  const string matchSym = "(\\w+(?:[\\.\\-]\\w+)*)";
66  const string matchArg = "\\(\\s*"+matchSym+"?\\s*\\)";
67  regex findPredicate ("\\s*"+matchSym+"("+matchArg+")?\\s*,?");
68 
73  inline uint
74  detectArity (smatch const& match)
75  {
76  if (!match[2].matched) return 0; // no parenthesis at all
77  if (!match[3].matched) return 0; // empty parenthesis
78 
79  // later we could analyse the argument in detail here...
80  return 1; // but now we just accept a single constant symbol
81  }
82  }
83 
84 
85  void
87  {
88  string def(lit);
89  string::const_iterator end_of_last_match = def.begin();
90 
91  sregex_iterator end;
92  sregex_iterator pos (def.begin(),def.end(), findPredicate,
93  match_continuous); // continuous: don't allow garbage *not* matched by the RegExp
94  while (pos != end)
95  {
96  smatch match = *pos;
97  atoms_.insert (Atom (match[1], detectArity(match), match[3]));
98  end_of_last_match = match[0].second;
99  ++pos;
100  }
101 
102  if ( end_of_last_match !=def.end()
103  && *end_of_last_match !='.'
104  ) // if the match did *not stop at the end of the pattern definition list
105  throw lumiera::error::Invalid ("Trailing garbage in binding pattern definition"
106  , LUMIERA_ERROR_BINDING_PATTERN_SYNTAX);
107  }
108 
109 
111  : atoms_()
112  { }
113 
115  {
116  if (!isnil(spec))
117  parse_and_append (spec);
118  }
119 
120 
121  void
123  {
124  REQUIRE (spec);
125  parse_and_append (spec);
126  }
127 
128 
129 
130  Binding::operator string() const
131  {
132  string repr("Binding[");
133  typedef NormalisedAtoms::const_iterator AIter;
134  AIter end = atoms_.end();
135  AIter pos = atoms_.begin();
136  for ( ; pos!=end ; ++pos)
137  repr += string(*pos)+", ";
138 
139  if (0 < atoms_.size())
140  repr.resize(repr.size()-2);
141 
142  repr += "]";
143  return repr;
144  }
145 
146 
147  Binding::Atom::operator string() const
148  {
149  return sym_+"/"+lexical_cast<string> (ari_)
150  +"("+arg_+")";
151  }
152 
153 
154  HashVal
155  Binding::calculateHash() const
156  {
157  HashVal hash=0;
158 
159  typedef NormalisedAtoms::const_iterator AIter;
160  AIter pos = atoms_.begin();
161  AIter end = atoms_.end();
162  for ( ; pos!=end ; ++pos)
163  {
164  hash_combine (hash, pos->sym());
165  hash_combine (hash, pos->arity());
166  hash_combine (hash, pos->arg());
167  }
168 
169  return hash;
170  }
171 
172 
181  bool
182  operator== (Binding const& b1, Binding const& b2)
183  {
184  if (b1.atoms_.size() != b2.atoms_.size())
185  return false;
186 
187  ASSERT (b1.atoms_.size() == b2.atoms_.size());
188 
189  typedef Binding::NormalisedAtoms::const_iterator Iter;
190  Iter end = b1.atoms_.end();
191  Iter p1 = b1.atoms_.begin();
192  Iter p2 = b2.atoms_.begin();
193 
194  for ( ; p1!=end; ++p1, ++p2 )
195  if (! p1->identical(*p2))
196  return false;
197 
198  return true;
199  }
200 
201 
202 
203 }} // namespace lumiera::advice
regex findPredicate("\*"+matchSym+"("+matchArg+")?\*,?")
sym(arg), groups: [symbol, parenthesis, argument symbol]
Conjunction of predicates to be matched against a collaboration partner for establishing an Advice co...
friend bool operator==(Binding const &, Binding const &)
bindings are considered equivalent if, after normalisation, their respective definitions are identica...
void parse_and_append(Literal def)
internal: parse into atoms, and insert them
inline string literal This is a marker type to indicate that
Definition: symbol.hpp:85
single predicate as part of an advice binding pattern
Derived specific exceptions within Lumiera&#39;s exception hierarchy.
Definition: error.hpp:199
A pattern to define and identify a specific attachment to the Advice system.
Marker types to indicate a literal string and a Symbol.
Tiny helper functions and shortcuts to be used everywhere Consider this header to be effectively incl...
void addPredicate(Literal spec)
extend the definition of this binding by adding a predicate according to the given textual definition...
size_t HashVal
a STL compatible hash value
Definition: hash-value.h:56
Lumiera public interface.
Definition: advice.cpp:113
uint detectArity(smatch const &match)
detect the arity of an predicate, as matched by findPredicate.
Binding()
create the empty binding, equivalent to true
#define LUMIERA_ERROR_DEFINE(err, msg)
Definition and initialisation of an error constant.
Definition: error.h:80