| 45 |
|
#include "primitives/DirectionalAtom.hpp" |
| 46 |
|
#include "primitives/RigidBody.hpp" |
| 47 |
|
#include "primitives/Molecule.hpp" |
| 48 |
+ |
#include "io/basic_ifstrstream.hpp" |
| 49 |
|
|
| 50 |
|
namespace oopse { |
| 51 |
|
|
| 52 |
|
|
| 53 |
|
SelectionEvaluator::SelectionEvaluator(SimInfo* si) |
| 54 |
< |
: info(si), nameFinder(info), distanceFinder(info), indexFinder(info), isLoaded_(false){ |
| 55 |
< |
|
| 56 |
< |
nStuntDouble = info->getNGlobalAtoms() + info->getNRigidBodies(); |
| 54 |
> |
: info(si), nameFinder(info), distanceFinder(info), indexFinder(info), |
| 55 |
> |
isLoaded_(false){ |
| 56 |
> |
nStuntDouble = info->getNGlobalAtoms() + info->getNGlobalRigidBodies(); |
| 57 |
|
} |
| 58 |
|
|
| 59 |
< |
bool SelectionEvaluator::loadScript(const std::string& filename, const std::string& script) { |
| 59 |
> |
bool SelectionEvaluator::loadScript(const std::string& filename, |
| 60 |
> |
const std::string& script) { |
| 61 |
|
clearDefinitionsAndLoadPredefined(); |
| 62 |
|
this->filename = filename; |
| 63 |
|
this->script = script; |
| 64 |
|
if (! compiler.compile(filename, script)) { |
| 65 |
|
error = true; |
| 66 |
|
errorMessage = compiler.getErrorMessage(); |
| 67 |
< |
std::cerr << "SelectionCompiler Error: " << errorMessage << std::endl; |
| 67 |
> |
|
| 68 |
> |
sprintf( painCave.errMsg, |
| 69 |
> |
"SelectionCompiler Error: %s\n", errorMessage.c_str()); |
| 70 |
> |
painCave.severity = OOPSE_ERROR; |
| 71 |
> |
painCave.isFatal = 1; |
| 72 |
> |
simError(); |
| 73 |
|
return false; |
| 74 |
|
} |
| 75 |
|
|
| 107 |
|
return loadScriptFileInternal(filename); |
| 108 |
|
} |
| 109 |
|
|
| 110 |
< |
bool SelectionEvaluator::loadScriptFileInternal(const std::string & filename) { |
| 111 |
< |
std::ifstream ifs(filename.c_str()); |
| 110 |
> |
bool SelectionEvaluator::loadScriptFileInternal(const std::string & filename) { |
| 111 |
> |
ifstrstream ifs(filename.c_str()); |
| 112 |
|
if (!ifs.is_open()) { |
| 113 |
|
return false; |
| 114 |
|
} |
| 115 |
< |
|
| 115 |
> |
|
| 116 |
|
const int bufferSize = 65535; |
| 117 |
|
char buffer[bufferSize]; |
| 118 |
|
std::string script; |
| 121 |
|
} |
| 122 |
|
return loadScript(filename, script); |
| 123 |
|
} |
| 124 |
< |
|
| 124 |
> |
|
| 125 |
|
void SelectionEvaluator::instructionDispatchLoop(OOPSEBitSet& bs){ |
| 126 |
|
|
| 127 |
|
while ( pc < aatoken.size()) { |
| 143 |
|
|
| 144 |
|
} |
| 145 |
|
|
| 146 |
< |
OOPSEBitSet SelectionEvaluator::expression(const std::vector<Token>& code, int pcStart) { |
| 146 |
> |
OOPSEBitSet SelectionEvaluator::expression(const std::vector<Token>& code, |
| 147 |
> |
int pcStart) { |
| 148 |
|
OOPSEBitSet bs; |
| 149 |
< |
std::stack<OOPSEBitSet> stack; |
| 150 |
< |
|
| 149 |
> |
std::stack<OOPSEBitSet> stack; |
| 150 |
> |
|
| 151 |
|
for (int pc = pcStart; pc < code.size(); ++pc) { |
| 152 |
|
Token instruction = code[pc]; |
| 153 |
|
|
| 179 |
|
stack.top().flip(); |
| 180 |
|
break; |
| 181 |
|
case Token::within: |
| 174 |
– |
|
| 182 |
|
withinInstruction(instruction, stack.top()); |
| 183 |
|
break; |
| 184 |
|
//case Token::selected: |
| 227 |
|
Atom* atom; |
| 228 |
|
Molecule::RigidBodyIterator rbIter; |
| 229 |
|
RigidBody* rb; |
| 223 |
– |
|
| 224 |
– |
for (mol = info->beginMolecule(mi); mol != NULL; mol = info->nextMolecule(mi)) { |
| 230 |
|
|
| 231 |
+ |
for (mol = info->beginMolecule(mi); mol != NULL; |
| 232 |
+ |
mol = info->nextMolecule(mi)) { |
| 233 |
+ |
|
| 234 |
|
for(atom = mol->beginAtom(ai); atom != NULL; atom = mol->nextAtom(ai)) { |
| 235 |
|
compareProperty(atom, bs, property, comparator, comparisonValue); |
| 236 |
|
} |
| 237 |
< |
|
| 238 |
< |
for (rb = mol->beginRigidBody(rbIter); rb != NULL; rb = mol->nextRigidBody(rbIter)) { |
| 239 |
< |
compareProperty(rb, bs, property, comparator, comparisonValue); |
| 240 |
< |
} |
| 237 |
> |
|
| 238 |
> |
for (rb = mol->beginRigidBody(rbIter); rb != NULL; |
| 239 |
> |
rb = mol->nextRigidBody(rbIter)) { |
| 240 |
> |
compareProperty(rb, bs, property, comparator, comparisonValue); |
| 241 |
> |
} |
| 242 |
|
} |
| 243 |
|
|
| 244 |
|
return bs; |
| 245 |
|
} |
| 246 |
|
|
| 247 |
< |
void SelectionEvaluator::compareProperty(StuntDouble* sd, OOPSEBitSet& bs, int property, int comparator, float comparisonValue) { |
| 247 |
> |
void SelectionEvaluator::compareProperty(StuntDouble* sd, OOPSEBitSet& bs, |
| 248 |
> |
int property, int comparator, |
| 249 |
> |
float comparisonValue) { |
| 250 |
|
RealType propertyValue = 0.0; |
| 251 |
|
switch (property) { |
| 252 |
|
case Token::mass: |
| 301 |
|
} |
| 302 |
|
if (match) |
| 303 |
|
bs.setBitOn(sd->getGlobalIndex()); |
| 304 |
< |
|
| 304 |
> |
|
| 305 |
|
} |
| 306 |
|
|
| 307 |
< |
void SelectionEvaluator::withinInstruction(const Token& instruction, OOPSEBitSet& bs){ |
| 307 |
> |
void SelectionEvaluator::withinInstruction(const Token& instruction, |
| 308 |
> |
OOPSEBitSet& bs){ |
| 309 |
|
|
| 310 |
|
boost::any withinSpec = instruction.value; |
| 311 |
|
float distance; |
| 320 |
|
|
| 321 |
|
bs = distanceFinder.find(bs, distance); |
| 322 |
|
} |
| 323 |
< |
|
| 323 |
> |
|
| 324 |
|
void SelectionEvaluator::define() { |
| 325 |
|
assert(statement.size() >= 3); |
| 326 |
< |
|
| 326 |
> |
|
| 327 |
|
std::string variable = boost::any_cast<std::string>(statement[1].value); |
| 328 |
< |
|
| 329 |
< |
variables.insert(VariablesType::value_type(variable, expression(statement, 2))); |
| 328 |
> |
|
| 329 |
> |
variables.insert(VariablesType::value_type(variable, |
| 330 |
> |
expression(statement, 2))); |
| 331 |
|
} |
| 332 |
+ |
|
| 333 |
|
|
| 320 |
– |
|
| 334 |
|
/** @todo */ |
| 335 |
|
void SelectionEvaluator::predefine(const std::string& script) { |
| 336 |
< |
|
| 336 |
> |
|
| 337 |
|
if (compiler.compile("#predefine", script)) { |
| 338 |
|
std::vector<std::vector<Token> > aatoken = compiler.getAatokenCompiled(); |
| 339 |
|
if (aatoken.size() != 1) { |
| 344 |
|
std::vector<Token> statement = aatoken[0]; |
| 345 |
|
if (statement.size() > 2) { |
| 346 |
|
int tok = statement[1].tok; |
| 347 |
< |
if (tok == Token::identifier || (tok & Token::predefinedset) == Token::predefinedset) { |
| 347 |
> |
if (tok == Token::identifier || |
| 348 |
> |
(tok & Token::predefinedset) == Token::predefinedset) { |
| 349 |
|
std::string variable = boost::any_cast<std::string>(statement[1].value); |
| 350 |
|
variables.insert(VariablesType::value_type(variable, statement)); |
| 351 |
< |
|
| 351 |
> |
|
| 352 |
|
} else { |
| 353 |
|
evalError("invalid variable name:" + script); |
| 354 |
|
} |
| 355 |
|
}else { |
| 356 |
|
evalError("bad predefinition length:" + script); |
| 357 |
< |
} |
| 344 |
< |
|
| 357 |
> |
} |
| 358 |
|
|
| 359 |
|
} else { |
| 360 |
|
evalError("predefined set compile error:" + script + |
| 361 |
|
"\ncompile error:" + compiler.getErrorMessage()); |
| 362 |
|
} |
| 350 |
– |
|
| 363 |
|
} |
| 364 |
|
|
| 365 |
|
void SelectionEvaluator::select(OOPSEBitSet& bs){ |
| 366 |
|
bs = expression(statement, 1); |
| 367 |
|
} |
| 368 |
< |
|
| 368 |
> |
|
| 369 |
|
OOPSEBitSet SelectionEvaluator::lookupValue(const std::string& variable){ |
| 370 |
< |
|
| 370 |
> |
|
| 371 |
|
OOPSEBitSet bs(nStuntDouble); |
| 372 |
|
std::map<std::string, boost::any>::iterator i = variables.find(variable); |
| 373 |
|
|
| 382 |
|
} else { |
| 383 |
|
unrecognizedIdentifier(variable); |
| 384 |
|
} |
| 385 |
< |
|
| 385 |
> |
|
| 386 |
|
return bs; |
| 387 |
|
} |
| 388 |
< |
|
| 389 |
< |
OOPSEBitSet SelectionEvaluator::nameInstruction(const std::string& name){ |
| 390 |
< |
|
| 379 |
< |
return nameFinder.match(name); |
| 380 |
< |
|
| 388 |
> |
|
| 389 |
> |
OOPSEBitSet SelectionEvaluator::nameInstruction(const std::string& name){ |
| 390 |
> |
return nameFinder.match(name); |
| 391 |
|
} |
| 392 |
|
|
| 393 |
|
bool SelectionEvaluator::containDynamicToken(const std::vector<Token>& tokens){ |
| 397 |
|
return true; |
| 398 |
|
} |
| 399 |
|
} |
| 400 |
< |
|
| 400 |
> |
|
| 401 |
|
return false; |
| 402 |
|
} |
| 403 |
|
|