49 |
|
#include "brains/SimInfo.hpp" |
50 |
|
#include "selection/Token.hpp" |
51 |
|
#include "selection/SelectionCompiler.hpp" |
52 |
+ |
#include "selection/NameFinder.hpp" |
53 |
+ |
#include "selection/DistanceFinder.hpp" |
54 |
|
#include "utils/BitSet.hpp" |
55 |
< |
|
55 |
> |
#include "primitives/StuntDouble.hpp" |
56 |
> |
#include "utils/StringUtils.hpp" |
57 |
|
namespace oopse { |
58 |
|
|
59 |
+ |
|
60 |
+ |
//class Context { |
61 |
+ |
// public: |
62 |
+ |
// |
63 |
+ |
// void clear() { |
64 |
+ |
// linenumbers.clear(); |
65 |
+ |
// lineIndices.clear(); |
66 |
+ |
// aatoken.clear(); |
67 |
+ |
// } |
68 |
+ |
// |
69 |
+ |
// std::string filename; |
70 |
+ |
// std::string script; |
71 |
+ |
// std::vector<int> linenumbers; |
72 |
+ |
// std::vector<int> lineIndices; |
73 |
+ |
// std::vector<std::vector<Token> > aatoken; |
74 |
+ |
// int pc; |
75 |
+ |
//}; |
76 |
+ |
|
77 |
+ |
|
78 |
|
/** |
79 |
|
* @class SelectionEvaluator SelectionEvaluator.hpp "selection/SelectionEvaluator" |
80 |
|
* @brief Evalute the tokens compiled by SelectionCompiler and return a BitSet |
82 |
|
class SelectionEvaluator{ |
83 |
|
public: |
84 |
|
|
85 |
< |
SelectionEvaluator(SimInfo* info, const std::string& script); |
85 |
> |
SelectionEvaluator(SimInfo* info); |
86 |
|
|
87 |
+ |
|
88 |
+ |
bool loadScriptString(const std::string& script); |
89 |
+ |
bool loadScriptFile(const std::string& filename); |
90 |
+ |
|
91 |
|
BitSet evaluate(); |
92 |
|
|
93 |
< |
BitSet evaluate(int frameNo); |
93 |
> |
//BitSet evaluate(Snapshot* snapshot); |
94 |
|
|
95 |
|
/** |
96 |
< |
* Tests if the result from evaluation of script is static. |
71 |
< |
* @return true if the result keeps the same even the frame change, otherwise return false |
96 |
> |
* Tests if the result from evaluation of script is dynamic. |
97 |
|
*/ |
98 |
< |
bool isStatic(); |
99 |
< |
|
98 |
> |
bool isDynamic() { |
99 |
> |
return isDynamic_; |
100 |
> |
} |
101 |
> |
|
102 |
> |
bool hadRuntimeError() const{ |
103 |
> |
return error; |
104 |
> |
} |
105 |
> |
|
106 |
> |
std::string getErrorMessage() const { |
107 |
> |
return errorMessage; |
108 |
> |
} |
109 |
> |
|
110 |
> |
|
111 |
> |
int getLinenumber() { |
112 |
> |
return linenumbers[pc]; |
113 |
> |
} |
114 |
> |
|
115 |
> |
std::string getLine() { |
116 |
> |
int ichBegin = lineIndices[pc]; |
117 |
> |
int ichEnd; |
118 |
> |
if ((ichEnd = script.find('\r', ichBegin)) == std::string::npos && |
119 |
> |
(ichEnd = script.find('\n', ichBegin)) == std::string::npos) { |
120 |
> |
ichEnd = script.size(); |
121 |
> |
} |
122 |
> |
return script.substr(ichBegin, ichEnd); |
123 |
> |
} |
124 |
> |
|
125 |
|
private: |
126 |
|
|
127 |
< |
/** */ |
127 |
> |
void clearState(); |
128 |
> |
|
129 |
> |
bool loadScript(const std::string& filename, const std::string& script); |
130 |
> |
|
131 |
> |
bool loadScriptFileInternal(const std::string& filename); |
132 |
> |
|
133 |
> |
|
134 |
> |
void clearDefinitionsAndLoadPredefined(); |
135 |
> |
|
136 |
|
void define(); |
137 |
+ |
void select(BitSet& bs); |
138 |
+ |
void predefine(const std::string& script); |
139 |
|
|
140 |
< |
/** */ |
81 |
< |
void predefine(); |
140 |
> |
void instructionDispatchLoop(BitSet& bs); |
141 |
|
|
142 |
< |
withinInstruction(Token instruction, BitSet , BitSet); |
142 |
> |
void withinInstruction(const Token& instruction, BitSet& bs); |
143 |
> |
|
144 |
> |
BitSet comparatorInstruction(const Token& instruction); |
145 |
> |
void compareProperty(StuntDouble* sd, BitSet& bs, int property, int comparator, float comparisonValue); |
146 |
> |
BitSet nameInstruction(const std::string& name); |
147 |
> |
BitSet indexInstruction(const boost::any& value); |
148 |
> |
BitSet expression(const std::vector<Token>& tokens, int pc); |
149 |
|
|
150 |
< |
BitSet expression(std::vector<Token>& tokens, int pc); |
150 |
> |
BitSet lookupValue(const std::string& variable); |
151 |
|
|
152 |
< |
SimInfo* info_; |
152 |
> |
void evalError(const std::string& message) { |
153 |
> |
std::cerr << "SelectionEvaulator Error: " << message << std::endl; |
154 |
> |
} |
155 |
|
|
156 |
< |
std::map<std::string, std::vector<Token> > variables_; |
157 |
< |
std::string script_; |
156 |
> |
void unrecognizedCommand(const Token& token) { |
157 |
> |
evalError("unrecognized command:" + boost::any_cast<std::string>(token.value)); |
158 |
> |
} |
159 |
> |
|
160 |
> |
void unrecognizedExpression() { |
161 |
> |
evalError("unrecognized expression"); |
162 |
> |
} |
163 |
> |
|
164 |
> |
void unrecognizedAtomProperty(int property){ |
165 |
> |
evalError("unrecognized atom property"); |
166 |
> |
} |
167 |
> |
|
168 |
> |
void unrecognizedIdentifier(const std::string& identifier) { |
169 |
> |
evalError("unrecognized identifier:" + identifier); |
170 |
> |
} |
171 |
> |
|
172 |
> |
void invalidIndexRange(std::pair<int, int> range) { |
173 |
> |
evalError("invalid index range: [" + toString(range.first) + ", " + toString(range.second) + ")"); |
174 |
> |
} |
175 |
> |
|
176 |
> |
void invalidIndex(int index) { |
177 |
> |
evalError("invalid index : " + toString(index) ); |
178 |
> |
} |
179 |
> |
|
180 |
> |
|
181 |
> |
bool containDynamicToken(const std::vector<Token>& tokens); |
182 |
> |
|
183 |
> |
SelectionCompiler compiler; |
184 |
> |
|
185 |
> |
//const static int scriptLevelMax = 10; |
186 |
> |
//int scriptLevel; |
187 |
> |
|
188 |
> |
//Context stack[scriptLevelMax]; |
189 |
> |
|
190 |
> |
std::string filename; |
191 |
> |
std::string script; |
192 |
> |
std::vector<int> linenumbers; |
193 |
> |
std::vector<int> lineIndices; |
194 |
> |
std::vector<std::vector<Token> > aatoken; |
195 |
> |
int pc; // program counter |
196 |
> |
|
197 |
> |
bool error; |
198 |
> |
std::string errorMessage; |
199 |
> |
|
200 |
> |
std::vector<Token> statement; |
201 |
> |
int statementLength; |
202 |
> |
|
203 |
> |
SimInfo* info; |
204 |
> |
NameFinder nameFinder; |
205 |
> |
DistanceFinder distanceFinder; |
206 |
> |
int nStuntDouble; //natoms + nrigidbodies |
207 |
> |
std::map<std::string, boost::any > variables; |
208 |
> |
|
209 |
> |
bool isDynamic_; |
210 |
> |
bool isLoaded_; |
211 |
> |
|
212 |
|
}; |
213 |
|
|
214 |
|
} |