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

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines