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

Comparing trunk/OOPSE-4/src/selection/SelectionEvaluator.cpp (file contents):
Revision 1961 by tim, Tue Feb 1 06:55:00 2005 UTC vs.
Revision 1966 by tim, Thu Feb 3 14:04:59 2005 UTC

# Line 43 | Line 43 | namespace oopse {
43   namespace oopse {
44  
45  
46 + bool SelectionEvaluator::loadScript(const std::string& filename, const std::string& script) {
47 +    this->filename = filename;
48 +    this->script = script;
49 +    if (! compiler.compile(filename, script)) {
50 +        error = true;
51 +        errorMessage = compiler.getErrorMessage();
52 +        return false;
53 +    }
54 +
55 +    pc = 0;
56 +    aatoken = compiler.getAatokenCompiled();
57 +    linenumbers = compiler.getLineNumbers();
58 +    lineIndices = compiler.getLineIndices();
59 +    return true;
60   }
61 +
62 + void SelectionEvaluator::clearState() {
63 +    for (int i = scriptLevelMax; --i >= 0; )
64 +        stack[i].clear();
65 +    scriptLevel = 0;
66 +    error = false;
67 +    errorMessage = "";
68 + }
69 +
70 + bool SelectionEvaluator::loadScriptString(const std::string& script) {
71 +    clearState();
72 +    return loadScript("", script);
73 + }
74 +
75 + bool SelectionEvaluator::loadScriptFile(const std::string& filename) {
76 +    clearState();
77 +    return loadScriptFileInternal(filename);
78 + }
79 +
80 + bool SelectionEvaluator::loadScriptFileInternal(const  string & filename) {
81 +
82 + }
83 +
84 + void SelectionEvaluator::instructionDispatchLoop(){
85 +
86 +    while ( pc < aatoken.size()) {
87 +        statement = aatoken[pc++];
88 +        statementLength = statement.size();
89 +        Token token = statement[0];
90 +        switch (token.tok) {
91 +            case Token::define:
92 +                define();
93 +            break;
94 +            case Token::select:
95 +                select();
96 +            break;
97 +            default:
98 +                unrecognizedCommand(token);
99 +            return;
100 +        }
101 +    }
102 + }
103 +
104 +  BitSet SelectionEvaluator::expression(std::vector<Token>& code, int pcStart) {
105 +    int numberOfAtoms = viewer.getAtomCount();
106 +    BitSet bs;
107 +    BitSet[] stack = new BitSet[10];
108 +    int sp = 0;
109 +
110 +    for (int pc = pcStart; ; ++pc) {
111 +      Token instruction = code[pc];
112 +
113 +      switch (instruction.tok) {
114 +      case Token::expressionBegin:
115 +        break;
116 +      case Token::expressionEnd:
117 +        break;
118 +      case Token::all:
119 +        bs = stack[sp++] = new BitSet(numberOfAtoms);
120 +        for (int i = numberOfAtoms; --i >= 0; )
121 +          bs.set(i);
122 +        break;
123 +      case Token::none:
124 +        stack[sp++] = new BitSet();
125 +        break;
126 +      case Token::opOr:
127 +        bs = stack[--sp];
128 +        stack[sp-1].or(bs);
129 +        break;
130 +      case Token::opAnd:
131 +        bs = stack[--sp];
132 +        stack[sp-1].and(bs);
133 +        break;
134 +      case Token::opNot:
135 +        bs = stack[sp - 1];
136 +        notSet(bs);
137 +        break;
138 +      case Token::within:
139 +        bs = stack[sp - 1];
140 +        stack[sp - 1] = new BitSet();
141 +        withinInstruction(instruction, bs, stack[sp - 1]);
142 +        break;
143 +      case Token::selected:
144 +        stack[sp++] = copyBitSet(viewer.getSelectionSet());
145 +        break;
146 +      case Token::name:
147 +
148 +        break;
149 +      case  Token::index:
150 +        
151 +        break;
152 +      case Token::molname:  
153 +
154 +        break;
155 +      case Token::molindex:
156 +        break;
157 +      case Token::identifier:
158 +        stack[sp++] = lookupIdentifierValue((std::string)instruction.value);
159 +        break;
160 +      case Token::opLT:
161 +      case Token::opLE:
162 +      case Token::opGE:
163 +      case Token::opGT:
164 +      case Token::opEQ:
165 +      case Token::opNE:
166 +        bs = stack[sp++] = new BitSet();
167 +        comparatorInstruction(instruction, bs);
168 +        break;
169 +      default:
170 +        unrecognizedExpression();
171 +      }
172 +    }
173 +    if (sp != 1)
174 +      evalError("atom expression compiler error - stack over/underflow");
175 +    return stack[0];
176 +  }
177 +
178 +
179 +
180 +  void SelectionEvaluator::comparatorInstruction(Token instruction, BitSet bs) {
181 +    int comparator = instruction.tok;
182 +    int property = instruction.intValue;
183 +    float propertyValue = 0; // just for temperature
184 +    int comparisonValue = ((Integer)instruction.value).intValue();
185 +    int numberOfAtoms = viewer.getAtomCount();
186 +    Frame frame = viewer.getFrame();
187 +    for (int i = 0; i < numberOfAtoms; ++i) {
188 +      Atom atom = frame.getAtomAt(i);
189 +      switch (property) {
190 +      case Token::mass:
191 +        //propertyValue = atom.getAtomNumber();
192 +        break;
193 +      case Token::charge:
194 +
195 +        break;
196 +      case Token::dipole:
197 +
198 +        break;
199 +      default:
200 +        unrecognizedAtomProperty(property);
201 +      }
202 +      bool match = false;
203 +      switch (comparator) {
204 +      case Token::opLT:
205 +        match = propertyValue < comparisonValue;
206 +        break;
207 +      case Token::opLE:
208 +        match = propertyValue <= comparisonValue;
209 +        break;
210 +      case Token::opGE:
211 +        match = propertyValue >= comparisonValue;
212 +        break;
213 +      case Token::opGT:
214 +        match = propertyValue > comparisonValue;
215 +        break;
216 +      case Token::opEQ:
217 +        match = propertyValue == comparisonValue;
218 +        break;
219 +      case Token::opNE:
220 +        match = propertyValue != comparisonValue;
221 +        break;
222 +      }
223 +      if (match)
224 +        bs.set(i);
225 +    }
226 +  }
227 +
228 + void SelectionEvaluator::withinInstruction(const Token& instruction, BitSet& bs, BitSet& bsResult)
229 +
230 +    boost::any withinSpec = instruction.value;
231 +    if (withinSpec.type() == typeid(float)){
232 +        withinDistance(boost::any_cast<float>(withinSpec), bs, bsResult);
233 +        return;
234 +    }
235 +    
236 +    evalError("Unrecognized within parameter:" + withinSpec);
237 + }
238 +
239 +  void SelectionEvaluator::withinDistance(float distance, const BitSet& bs, const BitSet& bsResult) {
240 +    Frame frame = viewer.getFrame();
241 +    for (int i = frame.getAtomCount(); --i >= 0; ) {
242 +      if (bs.get(i)) {
243 +        Atom atom = frame.getAtomAt(i);
244 +        AtomIterator iterWithin =
245 +          frame.getWithinIterator(atom, distance);
246 +        while (iterWithin.hasNext())
247 +          bsResult.set(iterWithin.next().getAtomIndex());
248 +      }
249 +    }
250 +  }
251 +
252 +  void SelectionEvaluator::define() {
253 +    assert(statement.size() >= 3);
254 +
255 +    std::string variable = boost::any_cast<std::string>(statement[1].value);
256 +    
257 +    variables.insert(std::make_pair(variable, expression(statement, 2)));
258 +  }
259 + }
260 +
261 + /** @todo */
262 + void SelectionEvaluator::predefine(const std::string& script) {
263 +
264 +    if (compiler.compile("#predefine", script)) {
265 +        std::vector<std::vector<Token> > aatoken = compiler.getAatokenCompiled();
266 +        if (aatoken.size() != 1) {
267 +            evalError("predefinition does not have exactly 1 command:"
268 +                + script);
269 +            return;
270 +        }
271 +        std::vector<Token> statement = aatoken[0];
272 +        if (statement.size() > 2) {
273 +            int tok = statement[1].tok;
274 +            if (tok == Token::identifier || (tok & Token::predefinedset) == Token::predefinedset) {
275 +                std::string variable = (std::string)statement[1].value;
276 +                variables.insert(std::make_pair(variable, statement));
277 +
278 +            } else {
279 +                evalError("invalid variable name:" + script);
280 +            }
281 +        }else {
282 +            evalError("bad predefinition length:" + script);
283 +        }
284 +
285 +        
286 +    } else {
287 +        evalError("predefined set compile error:" + script +
288 +          "\ncompile error:" + compiler.getErrorMessage());
289 +    }
290 +
291 + }
292 +
293 + void SelectionEvaluator::select(){
294 +    viewer.setSelectionSet(expression(statement, 1));
295 + }
296 +
297 + BitSet SelectionEvaluator::lookupValue(const std::string& variable){
298 +
299 +    std::map<std::string, boost::any>::iterator i = variables.find(variable);
300 +
301 +    if (i != variables.end()) {
302 +        if (i->second.type() == typeid(BitSet)) {
303 +            return boost::any_cast<BitSet>(i->second);
304 +        } else if (i->second.type() ==  typeid(std::vector<Token>)){
305 +            BitSet bs = expression(boost::any_cast(i->second), 2);
306 +            i->second =  bs; /**@todo fixme */
307 +            return bs;
308 +        }
309 +    }
310 +
311 + }
312 +
313 + }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines