LOOS  v2.3.2
KernelActions.cpp
1 /*
2  This file is part of LOOS.
3 
4  LOOS (Lightweight Object-Oriented Structure library)
5  Copyright (c) 2008, Tod D. Romo, Alan Grossfield
6  Department of Biochemistry and Biophysics
7  School of Medicine & Dentistry, University of Rochester
8 
9  This package (LOOS) is free software: you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation under version 3 of the License.
12 
13  This package 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 General Public License
19  along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21 
22 #include <KernelActions.hpp>
23 #include <Atom.hpp>
24 #include <sstream>
25 #include <Selectors.hpp>
26 
27 
28 namespace loos {
29 
30  namespace internal {
31 
32 
33  int Action::binComp(void) {
34  Value v1 = stack->pop();
35  Value v2 = stack->pop();
36  return(compare(v2, v1));
37  }
38 
39  bool Action::negativeOperand(void) {
40  Value v1 = stack->peek(-1);
41  Value v2 = stack->peek(-2);
42 
43  if ( (v1.type == Value::INT && v1.itg < 0) ||
44  (v2.type == Value::INT && v2.itg < 0) )
45  return(true);
46 
47  return(false);
48  }
49 
50 
51  void Action::binaryFalseResult(void) {
52  Value v(0);
53 
54  stack->drop();
55  stack->drop();
56  stack->push(v);
57  }
58 
59 
60  void Action::requireAtom(void) {
61  if (atom == 0)
62  throw(LOOSError("No atom set"));
63  }
64 
65 
66  void Action::setStack(ValueStack* ptr) { stack=ptr; }
67  void Action::setAtom(pAtom& pa) { atom = pa; }
68 
69  std::string Action::name(void) const { return(my_name); }
70 
71  //-------------------------------------------------------------
72 
73 
74  void pushString::execute(void) { stack->push(val); }
75  std::string pushString::name(void) const {
76  std::stringstream s;
77  s << my_name << "(" << val << ")";
78  return(s.str());
79  }
80 
81  void pushInt::execute(void) { stack->push(val); }
82  std::string pushInt::name(void) const {
83  std::stringstream s;
84  s << my_name << "(" << val << ")";
85  return(s.str());
86  }
87 
88  void pushFloat::execute(void) { stack->push(val); }
89  std::string pushFloat::name(void) const {
90  std::stringstream s;
91  s << my_name << "(" << val << ")";
92  return(s.str());
93  }
94 
95 
96  void drop::execute(void) { stack->drop(); }
97 
98  void dup::execute(void) { stack->dup(); }
99 
100  void equals::execute(void) {
101  Value v(binComp() == 0);
102  stack->push(v);
103  }
104 
105  void lessThan::execute(void) {
106  if (negativeOperand())
107  binaryFalseResult();
108  else {
109  Value v(binComp() < 0);
110  stack->push(v);
111  }
112  }
113 
114  void lessThanEquals::execute(void) {
115  if (negativeOperand())
116  binaryFalseResult();
117  else {
118  Value v(binComp() <= 0);
119  stack->push(v);
120  }
121  }
122 
123 
124  void greaterThan::execute(void) {
125  Value v(binComp() > 0);
126  stack->push(v);
127  }
128 
129  void greaterThanEquals::execute(void) {
130  Value v(binComp() >= 0);
131  stack->push(v);
132  }
133 
134  void matchRegex::execute(void) {
135  Value v = stack->pop();
136  Value r(0);
137  if (boost::regex_search(v.getString(), regexp))
138  r.setInt(1);
139 
140  stack->push(r);
141  }
142 
143  std::string matchRegex::name(void) const {
144  return(my_name + "(" + pattern + ")");
145  }
146 
147 
148 
149  void matchStringAsRegex::execute(void) {
150  Value v = stack->pop();
151  boost::regex re(v.getString(), boost::regex::perl|boost::regex::icase);
152  Value u = stack->pop();
153  Value r(0);
154 
155  if (boost::regex_search(u.getString(), re))
156  r.setInt(1);
157 
158  stack->push(r);
159  }
160 
161  void extractNumber::execute(void) {
162  Value v = stack->pop();
163  Value r(-1);
164  boost::smatch what;
165 
166  if (boost::regex_search(v.getString(), what, regexp)) {
167  unsigned i;
168  int val;
169  for (i=0; i<what.size(); i++) {
170  if ((std::stringstream(what[i]) >> val)) {
171  r.setInt(val);
172  break;
173  }
174  }
175  }
176 
177  stack->push(r);
178  }
179 
180  std::string extractNumber::name(void) const {
181  return(my_name + "(" + pattern + ")");
182  }
183 
184 
185  void pushAtomName::execute(void) {
186  requireAtom();
187  Value v(atom->name());
188  stack->push(v);
189  }
190 
191  void pushAtomId::execute(void) {
192  requireAtom();
193  Value v(atom->id());
194  stack->push(v);
195  }
196 
197  // Beware of overflows here!!!
198  void pushAtomIndex::execute(void) {
199  requireAtom();
200  Value v(static_cast<long>(atom->index()));
201  stack->push(v);
202  }
203 
204  void pushAtomResname::execute(void) {
205  requireAtom();
206  Value v(atom->resname());
207  stack->push(v);
208  }
209 
210  void pushAtomResid::execute(void) {
211  requireAtom();
212  Value v(atom->resid());
213  stack->push(v);
214  }
215 
216 
217  void pushAtomSegid::execute(void) {
218  requireAtom();
219  Value v(atom->segid());
220  stack->push(v);
221  }
222 
223  void pushAtomChainId::execute(void) {
224  requireAtom();
225  Value v(atom->chainId());
226  stack->push(v);
227  }
228 
229 
230  void logicalAnd::execute(void) {
231  Value v2 = stack->pop();
232  Value v1 = stack->pop();
233 
234  if (!(v1.type == Value::INT && v2.type == Value::INT))
235  throw(LOOSError("Invalid operands to logicalAnd"));
236 
237  Value u(v1.itg && v2.itg);
238  stack->push(u);
239  }
240 
241 
242  void logicalOr::execute(void) {
243  Value v1 = stack->pop();
244  Value v2 = stack->pop();
245 
246  if (!(v1.type == Value::INT && v2.type == Value::INT))
247  throw(LOOSError("Invalid operands to logicalOr"));
248 
249  Value u(v1.itg || v2.itg);
250  stack->push(u);
251  }
252 
253 
254  void logicalNot::execute(void) {
255  Value v1 = stack->pop();
256 
257  if (v1.type != Value::INT)
258  throw(LOOSError("Invalid operand to logicalNot"));
259 
260  Value u(!v1.itg);
261  stack->push(u);
262  }
263 
264  void logicalTrue::execute(void) {
265  Value v((int)1);
266  stack->push(v);
267  }
268 
269 
270  void Hydrogen::execute(void) {
271  requireAtom();
272 
273  bool masscheck = true;
274  if (atom->checkProperty(Atom::massbit))
275  masscheck = (atom->mass() < 1.1);
276 
277  std::string n = atom->name();
278  Value v;
279  v.setInt( (n[0] == 'H' && masscheck) );
280  stack->push(v);
281  }
282 
283 
284 
285  // Provide storage for class-level selector
286  BackboneSelector Backbone::bbsel;
287 
288 
289  void Backbone::execute(void) {
290  requireAtom();
291  Value v(bbsel(atom));
292 
293  stack->push(v);
294  }
295 
296 
297  }
298 
299 }
int binComp(void)
Compare the top two items on the stack...
ValueStack * stack
Pointer to the data stack.
Generic LOOS exception.
Definition: exceptions.hpp:40
void requireAtom(void)
Check to make sure an atom has been set...
int compare(const Value &x, const Value &y)
Compare two Value objects, depending on their types.
Definition: KernelValue.cpp:31
std::string my_name
Record of command-name (for printing)
Namespace for most things not already encapsulated within a class.
Value class for the LOOS Kernel (virtual machine)
Definition: KernelValue.hpp:51
pAtom atom
Pointer to the atom we'll be working on...