ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE-2.0/src/selection/SelectionEvaluator.cpp
(Generate patch)

Comparing trunk/OOPSE-2.0/src/selection/SelectionEvaluator.cpp (file contents):
Revision 1962 by tim, Tue Feb 1 22:49:23 2005 UTC vs.
Revision 2469 by tim, Fri Dec 2 15:38:03 2005 UTC

# Line 39 | Line 39
39   * such damages.
40   */
41  
42 + #include <stack>
43   #include "selection/SelectionEvaluator.hpp"
44 + #include "primitives/Atom.hpp"
45 + #include "primitives/DirectionalAtom.hpp"
46 + #include "primitives/RigidBody.hpp"
47 + #include "primitives/Molecule.hpp"
48 +
49   namespace oopse {
50  
51  
52 < bool SelectionEvaluator::loadScript(const std::string& filename, const std::string& script) {
52 >  SelectionEvaluator::SelectionEvaluator(SimInfo* si)
53 >    : info(si), nameFinder(info), distanceFinder(info), indexFinder(info), isLoaded_(false){
54 >    
55 >      nStuntDouble = info->getNGlobalAtoms() + info->getNRigidBodies();
56 >    }            
57 >
58 >  bool SelectionEvaluator::loadScript(const std::string& filename, const std::string& script) {
59 >    clearDefinitionsAndLoadPredefined();
60      this->filename = filename;
61      this->script = script;
62      if (! compiler.compile(filename, script)) {
63 <        error = true;
64 <        errorMessage = compiler.getErrorMessage();
65 <        return false;
63 >      error = true;
64 >      errorMessage = compiler.getErrorMessage();
65 >      std::cerr << "SelectionCompiler Error: " << errorMessage << std::endl;
66 >      return false;
67      }
68  
69      pc = 0;
70      aatoken = compiler.getAatokenCompiled();
71      linenumbers = compiler.getLineNumbers();
72      lineIndices = compiler.getLineIndices();
73 +
74 +    std::vector<std::vector<Token> >::const_iterator i;  
75 +
76 +    isDynamic_ = false;
77 +    for (i = aatoken.begin(); i != aatoken.end(); ++i) {
78 +      if (containDynamicToken(*i)) {
79 +        isDynamic_ = true;
80 +        break;
81 +      }
82 +    }
83 +
84 +    isLoaded_ = true;
85      return true;
86 < }
86 >  }
87  
88 < void SelectionEvaluator::clearState() {
63 <    for (int i = scriptLevelMax; --i >= 0; )
64 <        stack[i] = null;
65 <    scriptLevel = 0;
88 >  void SelectionEvaluator::clearState() {
89      error = false;
90 <    errorMessage = null;
91 < }
90 >    errorMessage = "";
91 >  }
92  
93 < bool SelectionEvaluator::loadScriptString(const std::string& script) {
93 >  bool SelectionEvaluator::loadScriptString(const std::string& script) {
94      clearState();
95 <    return loadScript(null, script);
96 < }
95 >    return loadScript("", script);
96 >  }
97  
98 < bool SelectionEvaluator::loadScriptFile(const std::string& filename) {
98 >  bool SelectionEvaluator::loadScriptFile(const std::string& filename) {
99      clearState();
100      return loadScriptFileInternal(filename);
101 < }
101 >  }
102  
103 +  bool SelectionEvaluator::loadScriptFileInternal(const  std::string & filename) {
104 +    std::ifstream ifs(filename.c_str());
105 +    if (!ifs.is_open()) {
106 +      return false;
107 +    }
108  
109 < void SelectionEvaluator::instructionDispatchLoop(){
110 <
111 <    while ( pc < aatoken.length) {
112 <        statement = aatoken[pc++];
113 <        statementLength = statement.length;
86 <        Token token = statement[0];
87 <        switch (token.tok) {
88 <            case Token.define:
89 <                define();
90 <            break;
91 <            case Token.select:
92 <                select();
93 <            break;
94 <            default:
95 <                unrecognizedCommand(token);
96 <            return;
97 <        }
109 >    const int bufferSize = 65535;
110 >    char buffer[bufferSize];
111 >    std::string script;
112 >    while(ifs.getline(buffer, bufferSize)) {
113 >      script += buffer;
114      }
115 < }
115 >    return loadScript(filename, script);
116 >  }
117  
118 <  void SelectionEvaluator::predefine(String script) {
119 <    if (compiler.compile("#predefine", script)) {
120 <      Token [][] aatoken = compiler.getAatokenCompiled();
121 <      if (aatoken.length != 1) {
122 <        viewer.scriptStatus("predefinition does not have exactly 1 command:"
123 <                            + script);
124 <        return;
118 >  void SelectionEvaluator::instructionDispatchLoop(OOPSEBitSet& bs){
119 >    
120 >    while ( pc < aatoken.size()) {
121 >      statement = aatoken[pc++];
122 >      statementLength = statement.size();
123 >      Token token = statement[0];
124 >      switch (token.tok) {
125 >      case Token::define:
126 >        define();
127 >        break;
128 >      case Token::select:
129 >        select(bs);
130 >        break;
131 >      default:
132 >        unrecognizedCommand(token);
133 >        return;
134        }
109      Token[] statement = aatoken[0];
110      if (statement.length > 2) {
111        int tok = statement[1].tok;
112        if (tok == Token.identifier ||
113            (tok & Token.predefinedset) == Token.predefinedset) {
114          String variable = (String)statement[1].value;
115          variables.put(variable, statement);
116        } else {
117          viewer.scriptStatus("invalid variable name:" + script);
118        }
119      } else {
120        viewer.scriptStatus("bad predefinition length:" + script);
121      }
122    } else {
123      viewer.scriptStatus("predefined set compile error:" + script +
124                          "\ncompile error:" + compiler.getErrorMessage());
135      }
136 +
137    }
138  
139 <
140 <
141 <  BitSet SelectionEvaluator::expression(Token[] code, int pcStart) throws ScriptException {
142 <    int numberOfAtoms = viewer.getAtomCount();
143 <    BitSet bs;
133 <    BitSet[] stack = new BitSet[10];
134 <    int sp = 0;
135 <
136 <    for (int pc = pcStart; ; ++pc) {
139 >  OOPSEBitSet SelectionEvaluator::expression(const std::vector<Token>& code, int pcStart) {
140 >    OOPSEBitSet bs;
141 >    std::stack<OOPSEBitSet> stack;
142 >    
143 >    for (int pc = pcStart; pc < code.size(); ++pc) {
144        Token instruction = code[pc];
145 <      if (logMessages)
139 <        viewer.scriptStatus("instruction=" + instruction);
145 >
146        switch (instruction.tok) {
147 <      case Token.expressionBegin:
147 >      case Token::expressionBegin:
148          break;
149 <      case Token.expressionEnd:
144 <        break expression_loop;
145 <      case Token.all:
146 <        bs = stack[sp++] = new BitSet(numberOfAtoms);
147 <        for (int i = numberOfAtoms; --i >= 0; )
148 <          bs.set(i);
149 >      case Token::expressionEnd:
150          break;
151 <      case Token.none:
152 <        stack[sp++] = new BitSet();
151 >      case Token::all:
152 >        bs = OOPSEBitSet(nStuntDouble);
153 >        bs.setAll();
154 >        stack.push(bs);            
155          break;
156 <      case Token.opOr:
157 <        bs = stack[--sp];
158 <        stack[sp-1].or(bs);
156 >      case Token::none:
157 >        bs = OOPSEBitSet(nStuntDouble);
158 >        stack.push(bs);            
159          break;
160 <      case Token.opAnd:
161 <        bs = stack[--sp];
162 <        stack[sp-1].and(bs);
160 >      case Token::opOr:
161 >        bs = stack.top();
162 >        stack.pop();
163 >        stack.top() |= bs;
164          break;
165 <      case Token.opNot:
166 <        bs = stack[sp - 1];
167 <        notSet(bs);
165 >      case Token::opAnd:
166 >        bs = stack.top();
167 >        stack.pop();
168 >        stack.top() &= bs;
169          break;
170 <      case Token.within:
171 <        bs = stack[sp - 1];
167 <        stack[sp - 1] = new BitSet();
168 <        withinInstruction(instruction, bs, stack[sp - 1]);
170 >      case Token::opNot:
171 >        stack.top().flip();
172          break;
173 <      case Token.selected:
174 <        stack[sp++] = copyBitSet(viewer.getSelectionSet());
173 >      case Token::within:
174 >
175 >        withinInstruction(instruction, stack.top());
176          break;
177 <      case Token.y:
178 <      case Token.amino:
179 <      case Token.backbone:
180 <      case Token.solvent:
181 <      case Token.identifier:
178 <      case Token.sidechain:
179 <      case Token.surface:
180 <        stack[sp++] = lookupIdentifierValue((String)instruction.value);
177 >        //case Token::selected:
178 >        //  stack.push(getSelectionSet());
179 >        //  break;
180 >      case Token::name:
181 >        stack.push(nameInstruction(boost::any_cast<std::string>(instruction.value)));
182          break;
183 <      case Token.opLT:
184 <      case Token.opLE:
184 <      case Token.opGE:
185 <      case Token.opGT:
186 <      case Token.opEQ:
187 <      case Token.opNE:
188 <        bs = stack[sp++] = new BitSet();
189 <        comparatorInstruction(instruction, bs);
183 >      case Token::index:
184 >        stack.push(indexInstruction(instruction.value));
185          break;
186 +      case Token::identifier:
187 +        stack.push(lookupValue(boost::any_cast<std::string>(instruction.value)));
188 +        break;
189 +      case Token::opLT:
190 +      case Token::opLE:
191 +      case Token::opGE:
192 +      case Token::opGT:
193 +      case Token::opEQ:
194 +      case Token::opNE:
195 +        stack.push(comparatorInstruction(instruction));
196 +        break;
197        default:
198          unrecognizedExpression();
199        }
200      }
201 <    if (sp != 1)
201 >    if (stack.size() != 1)
202        evalError("atom expression compiler error - stack over/underflow");
203 <    return stack[0];
203 >          
204 >    return stack.top();
205    }
206  
207  
208  
209 <  void SelectionEvaluator::comparatorInstruction(Token instruction, BitSet bs) {
209 >  OOPSEBitSet SelectionEvaluator::comparatorInstruction(const Token& instruction) {
210      int comparator = instruction.tok;
211      int property = instruction.intValue;
212 <    float propertyValue = 0; // just for temperature
213 <    int comparisonValue = ((Integer)instruction.value).intValue();
214 <    int numberOfAtoms = viewer.getAtomCount();
215 <    Frame frame = viewer.getFrame();
216 <    for (int i = 0; i < numberOfAtoms; ++i) {
217 <      Atom atom = frame.getAtomAt(i);
218 <      switch (property) {
219 <      case Token.atomno:
220 <        propertyValue = atom.getAtomNumber();
221 <        break;
222 <      case Token.elemno:
223 <        propertyValue = atom.getElementNumber();
224 <        break;
225 <      case Token.temperature:
226 <        propertyValue = atom.getBfactor100();
227 <        if (propertyValue < 0)
221 <          continue;
222 <        propertyValue /= 100;
223 <        break;
224 <      case Token._atomID:
225 <        propertyValue = atom.getSpecialAtomID();
226 <        if (propertyValue < 0)
227 <          continue;
228 <        break;
229 <      case Token._structure:
230 <        propertyValue = getProteinStructureType(atom);
231 <        if (propertyValue == -1)
232 <          continue;
233 <        break;
234 <      case Token.radius:
235 <        propertyValue = atom.getRasMolRadius();
236 <        break;
237 <      case Token._bondedcount:
238 <        propertyValue = atom.getCovalentBondCount();
239 <        break;
240 <      case Token.model:
241 <        propertyValue = atom.getModelTagNumber();
242 <        break;
243 <      default:
244 <        unrecognizedAtomProperty(property);
212 >    float comparisonValue = boost::any_cast<float>(instruction.value);
213 >    float propertyValue;
214 >    OOPSEBitSet bs(nStuntDouble);
215 >    bs.clearAll();
216 >    
217 >    SimInfo::MoleculeIterator mi;
218 >    Molecule* mol;
219 >    Molecule::AtomIterator ai;
220 >    Atom* atom;
221 >    Molecule::RigidBodyIterator rbIter;
222 >    RigidBody* rb;
223 >    
224 >    for (mol = info->beginMolecule(mi); mol != NULL; mol = info->nextMolecule(mi)) {
225 >
226 >      for(atom = mol->beginAtom(ai); atom != NULL; atom = mol->nextAtom(ai)) {
227 >        compareProperty(atom, bs, property, comparator, comparisonValue);
228        }
229 <      boolean match = false;
230 <      switch (comparator) {
231 <      case Token.opLT:
232 <        match = propertyValue < comparisonValue;
233 <        break;
234 <      case Token.opLE:
235 <        match = propertyValue <= comparisonValue;
236 <        break;
237 <      case Token.opGE:
238 <        match = propertyValue >= comparisonValue;
239 <        break;
240 <      case Token.opGT:
241 <        match = propertyValue > comparisonValue;
242 <        break;
243 <      case Token.opEQ:
244 <        match = propertyValue == comparisonValue;
245 <        break;
246 <      case Token.opNE:
247 <        match = propertyValue != comparisonValue;
248 <        break;
229 >        
230 >      for (rb = mol->beginRigidBody(rbIter); rb != NULL; rb = mol->nextRigidBody(rbIter)) {
231 >        compareProperty(rb, bs, property, comparator, comparisonValue);
232 >      }        
233 >    }
234 >
235 >    return bs;
236 >  }
237 >
238 >  void SelectionEvaluator::compareProperty(StuntDouble* sd, OOPSEBitSet& bs, int property, int comparator, float comparisonValue) {
239 >    double propertyValue = 0.0;
240 >    switch (property) {
241 >    case Token::mass:
242 >      propertyValue = sd->getMass();
243 >      break;
244 >    case Token::charge:
245 >      if (sd->isAtom()){
246 >        Atom* atom = static_cast<Atom*>(sd);
247 >        propertyValue = getCharge(atom);
248 >      } else if (sd->isRigidBody()) {
249 >        RigidBody* rb = static_cast<RigidBody*>(sd);
250 >        RigidBody::AtomIterator ai;
251 >        Atom* atom;
252 >        for (atom = rb->beginAtom(ai); atom != NULL; atom = rb->nextAtom(ai)) {
253 >          propertyValue+=  getCharge(atom);
254 >        }
255        }
256 <      if (match)
257 <        bs.set(i);
256 >      break;
257 >    default:
258 >      unrecognizedAtomProperty(property);
259      }
260 +        
261 +    bool match = false;
262 +    switch (comparator) {
263 +    case Token::opLT:
264 +      match = propertyValue < comparisonValue;
265 +      break;
266 +    case Token::opLE:
267 +      match = propertyValue <= comparisonValue;
268 +      break;
269 +    case Token::opGE:
270 +      match = propertyValue >= comparisonValue;
271 +      break;
272 +    case Token::opGT:
273 +      match = propertyValue > comparisonValue;
274 +      break;
275 +    case Token::opEQ:
276 +      match = propertyValue == comparisonValue;
277 +      break;
278 +    case Token::opNE:
279 +      match = propertyValue != comparisonValue;
280 +      break;
281 +    }
282 +    if (match)
283 +      bs.setBitOn(sd->getGlobalIndex());
284 +
285    }
286  
287 < void SelectionEvaluator::withinInstruction(Token instruction, BitSet bs, BitSet bsResult)
287 >  void SelectionEvaluator::withinInstruction(const Token& instruction, OOPSEBitSet& bs){
288 >    
289 >    boost::any withinSpec = instruction.value;
290 >    float distance;
291 >    if (withinSpec.type() == typeid(float)){
292 >      distance = boost::any_cast<float>(withinSpec);
293 >    } else if (withinSpec.type() == typeid(int)) {
294 >      distance = boost::any_cast<int>(withinSpec);    
295 >    } else {
296 >      evalError("casting error in withinInstruction");
297 >      bs.clearAll();
298 >    }
299 >    
300 >    bs = distanceFinder.find(bs, distance);            
301 >  }
302  
303 <    Object withinSpec = instruction.value;
304 <    if (withinSpec instanceof Float) {
305 <        withinDistance(((Float)withinSpec).floatValue(), bs, bsResult);
306 <        return;
303 >  void SelectionEvaluator::define() {
304 >    assert(statement.size() >= 3);
305 >
306 >    std::string variable = boost::any_cast<std::string>(statement[1].value);
307 >
308 >    variables.insert(VariablesType::value_type(variable, expression(statement, 2)));
309 >  }
310 >
311 >
312 >  /** @todo */
313 >  void SelectionEvaluator::predefine(const std::string& script) {
314 >
315 >    if (compiler.compile("#predefine", script)) {
316 >      std::vector<std::vector<Token> > aatoken = compiler.getAatokenCompiled();
317 >      if (aatoken.size() != 1) {
318 >        evalError("predefinition does not have exactly 1 command:"
319 >                  + script);
320 >        return;
321 >      }
322 >      std::vector<Token> statement = aatoken[0];
323 >      if (statement.size() > 2) {
324 >        int tok = statement[1].tok;
325 >        if (tok == Token::identifier || (tok & Token::predefinedset) == Token::predefinedset) {
326 >          std::string variable = boost::any_cast<std::string>(statement[1].value);
327 >          variables.insert(VariablesType::value_type(variable, statement));
328 >
329 >        } else {
330 >          evalError("invalid variable name:" + script);
331 >        }
332 >      }else {
333 >        evalError("bad predefinition length:" + script);
334 >      }
335 >
336 >        
337 >    } else {
338 >      evalError("predefined set compile error:" + script +
339 >                "\ncompile error:" + compiler.getErrorMessage());
340      }
279    evalError("Unrecognized within parameter:" + withinSpec);
280 }
341  
342 <  void SelectionEvaluator::withinDistance(float distance, BitSet bs, BitSet bsResult) {
343 <    Frame frame = viewer.getFrame();
344 <    for (int i = frame.getAtomCount(); --i >= 0; ) {
345 <      if (bs.get(i)) {
346 <        Atom atom = frame.getAtomAt(i);
347 <        AtomIterator iterWithin =
348 <          frame.getWithinIterator(atom, distance);
349 <        while (iterWithin.hasNext())
350 <          bsResult.set(iterWithin.next().getAtomIndex());
342 >  }
343 >
344 >  void SelectionEvaluator::select(OOPSEBitSet& bs){
345 >    bs = expression(statement, 1);
346 >  }
347 >
348 >  OOPSEBitSet SelectionEvaluator::lookupValue(const std::string& variable){
349 >
350 >    OOPSEBitSet bs(nStuntDouble);
351 >    std::map<std::string, boost::any>::iterator i = variables.find(variable);
352 >    
353 >    if (i != variables.end()) {
354 >      if (i->second.type() == typeid(OOPSEBitSet)) {
355 >        return boost::any_cast<OOPSEBitSet>(i->second);
356 >      } else if (i->second.type() ==  typeid(std::vector<Token>)){
357 >        bs = expression(boost::any_cast<std::vector<Token> >(i->second), 2);
358 >        i->second =  bs; /**@todo fixme */
359 >        return bs;
360        }
361 +    } else {
362 +      unrecognizedIdentifier(variable);
363      }
364 +
365 +    return bs;
366    }
367  
368 <  void SelectionEvaluator::define() throws ScriptException {
369 <    String variable = (String)statement[1].value;
370 <    variables.put(variable, (expression(statement, 2)));
368 >  OOPSEBitSet SelectionEvaluator::nameInstruction(const std::string& name){
369 >    
370 >    return nameFinder.match(name);
371 >
372 >  }    
373 >
374 >  bool SelectionEvaluator::containDynamicToken(const std::vector<Token>& tokens){
375 >    std::vector<Token>::const_iterator i;
376 >    for (i = tokens.begin(); i != tokens.end(); ++i) {
377 >      if (i->tok & Token::dynamic) {
378 >        return true;
379 >      }
380 >    }
381 >
382 >    return false;
383 >  }    
384 >
385 >  void SelectionEvaluator::clearDefinitionsAndLoadPredefined() {
386 >    variables.clear();
387 >    //load predefine
388 >    //predefine();
389    }
299 }
390  
391 <  void SelectionEvaluator::predefine(Token[] statement) {
392 <    String variable = (String)statement[1].value;
393 <    variables.put(variable, statement);
391 >  OOPSEBitSet SelectionEvaluator::evaluate() {
392 >    OOPSEBitSet bs(nStuntDouble);
393 >    if (isLoaded_) {
394 >      pc = 0;
395 >      instructionDispatchLoop(bs);
396 >    }
397 >
398 >    return bs;
399    }
400  
401 <  void SelectionEvaluator::select(){
402 <    // NOTE this is called by restrict()
403 <    if (statementLength == 1) {
404 <      viewer.selectAll();
405 <      if (!viewer.getRasmolHydrogenSetting())
406 <        viewer.excludeSelectionSet(getHydrogenSet());
407 <      if (!viewer.getRasmolHeteroSetting())
408 <        viewer.excludeSelectionSet(getHeteroSet());
409 <    } else {
410 <      viewer.setSelectionSet(expression(statement, 1));
401 >  OOPSEBitSet SelectionEvaluator::indexInstruction(const boost::any& value) {
402 >    OOPSEBitSet bs(nStuntDouble);
403 >
404 >    if (value.type() == typeid(int)) {
405 >      int index = boost::any_cast<int>(value);
406 >      if (index < 0 || index >= bs.size()) {
407 >        invalidIndex(index);
408 >      } else {
409 >        bs = indexFinder.find(index);
410 >      }
411 >    } else if (value.type() == typeid(std::pair<int, int>)) {
412 >      std::pair<int, int> indexRange= boost::any_cast<std::pair<int, int> >(value);
413 >      assert(indexRange.first <= indexRange.second);
414 >      if (indexRange.first < 0 || indexRange.second >= bs.size()) {
415 >        invalidIndexRange(indexRange);
416 >      }else {
417 >        bs = indexFinder.find(indexRange.first, indexRange.second);
418 >      }
419      }
420 <    viewer.scriptStatus("" + viewer.getSelectionCount() + " atoms selected");
420 >
421 >    return bs;
422    }
423 <  
424 <  }
423 >
424 >
425 >  double SelectionEvaluator::getCharge(Atom* atom) {
426 >    double charge =0.0;
427 >    AtomType* atomType = atom->getAtomType();
428 >    if (atomType->isCharge()) {
429 >      GenericData* data = atomType->getPropertyByName("Charge");
430 >      if (data != NULL) {
431 >        DoubleGenericData* doubleData= dynamic_cast<DoubleGenericData*>(data);
432 >
433 >        if (doubleData != NULL) {
434 >          charge = doubleData->getData();
435 >
436 >        } else {
437 >          sprintf( painCave.errMsg,
438 >                   "Can not cast GenericData to DoubleGenericData\n");
439 >          painCave.severity = OOPSE_ERROR;
440 >          painCave.isFatal = 1;
441 >          simError();          
442 >        }
443 >      }
444 >    }
445 >
446 >    return charge;
447 >  }
448 >
449 > }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines