/
script.cpp
executable file
·373 lines (282 loc) · 13.6 KB
/
script.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#include "script.h"
#include "simulation.h"
#include "simulationresult.h"
#include "compute.h"
#include "strings.h"
// Multithreading
#include <thread>
#include <mutex>
#include <chrono>
// File support
#include <QFile>
#include <QTextStream>
// Script support
#include <QScriptEngine>
#include <QScriptValue>
#include "scriptobjects.h"
// Database support
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlError>
#include <QDebug>
// Linear algebra
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#include <Eigen/Core>
#include <Eigen/Dense>
#include <Eigen/Eigenvalues>
#include <Eigen/LU>
#pragma GCC diagnostic pop
using namespace std;
using namespace Eigen;
using namespace ScriptObject;
/*************************************************************************************************************
* Class CompiledFunction
************************************************************************************************************/
/*************************************************************************************************************
* Constructors
************************************************************************************************************/
CompiledFunction::CompiledFunction(const QString & name, unsigned int index, const QString & code)
: p_name(name), p_code(code), p_index(index), p_dependencyBuilt(false), p_valid(true), p_flag(0)
{
QRegExp regexp(QString("\\b") + p_name + QString("\\b"));
if(regexp.indexIn(p_code) != -1) {
p_valid = false;
qDebug() << "CompiledFunction: Function depends on itself!";
}
}
/*************************************************************************************************************
* Public Members
************************************************************************************************************/
bool CompiledFunction::buildDependency(const vector<Parameter> & parameters, const vector<CompiledFunction *> & functions)
{
if(p_dependencyBuilt || !p_valid) {
return p_valid;
}
p_dependencyBuilt = true;
for(const Parameter & parameter : parameters) {
QRegExp regexp(QString("\\b") + parameter.name + QString("\\b"));
if(regexp.indexIn(p_code) != -1) {
p_dependencies.insert(parameter.name);
}
}
for(CompiledFunction * function : functions) {
QRegExp regexp(QString("\\b") + function->name() + QString("\\b"));
if(regexp.indexIn(p_code) != -1) {
function->buildDependency(parameters, functions);
p_dependencies.insert(function->name());
for(QString name : function->p_dependencies) {
p_dependencies.insert(name);
}
}
}
if(p_dependencies.find(p_name) != p_dependencies.end()) {
p_valid = false;
qDebug() << "CompiledFunction::buildDependency : Function depends on itself!";
}
return p_valid;
}
bool CompiledFunction::compile(const mathpresso::Context & context)
{
if(!p_valid) {
return false;
}
mathpresso::Error error = p_compiledCode.compile(context, p_code.toUtf8().data(), mathpresso::kNoOptions);
if(error != mathpresso::kErrorOk) {
qDebug() << "CompiledFunction::compile : Error" << error;
return false;
}
return true;
}
bool CompiledFunction::dependsOn(const QString & name) const
{
if(!p_dependencyBuilt) {
qDebug() << "CompiledFunction::dependsOn : called before buildDependency()";
return true;
}
return p_dependencies.find(name) != p_dependencies.end();
}
/*************************************************************************************************************
* Class ScriptLoader
************************************************************************************************************/
/*************************************************************************************************************
* Static Members
************************************************************************************************************/
QScriptValue ScriptLoader::scriptFunctionLoadScript(QScriptContext * context, QScriptEngine *)
{
// Check the argument count
if(context->argumentCount() != 1) {
qDebug() << "ScriptLoader::scriptFunction:loadScript: expecting exactly one argument.";
return QScriptValue::UndefinedValue;
}
// Check the argument type
if(!context->argument(0).isString()) {
qDebug() << "ScriptLoader::scriptFunction:loadScript: argument is not a string";
return QScriptValue::UndefinedValue;
}
// Locate the corresponding file
QString filename = context->argument(0).toString();
QFile file(filename);
if(!file.open(QIODevice::ReadOnly)) {
qDebug() << "ScriptLoader::scriptFunction:loadScript: failed to open the file" << filename.toUtf8().data();
return QScriptValue::UndefinedValue;
}
// Load the script
if(!ScriptLoader::load(QTextStream(&file).readAll())) {
qDebug() << "ScriptLoader::scriptFunction:loadScript: failed to load the script";
}
return QScriptValue::UndefinedValue;
}
QScriptValue ScriptLoader::scriptFunctionSimulationConstructor(QScriptContext * context, QScriptEngine * engine)
{
using namespace Strings::Script;
QScriptValue thisObject;
if(context->isCalledAsConstructor()) {
thisObject = context->thisObject();
} else {
thisObject = engine->newObject();
thisObject.setPrototype(context->callee().prototype());
}
// Bind C++ function to Simulation.run
thisObject.setProperty(Simulation::Run, engine->newFunction(ScriptLoader::scriptFunctionSimulationRun));
// TODO: move this to setup.js
//Default values
thisObject.setProperty(Simulation::Frequency, QScriptValue(engine, "0"));
thisObject.setProperty(Simulation::Exchange, QScriptValue(engine, "0"));
thisObject.setProperty(Simulation::Damping, QScriptValue(engine, "0"));
QScriptValue atomLoss = engine->newObject();
atomLoss.setProperty(AtomLoss::FromF1, QScriptValue(engine, "0"));
atomLoss.setProperty(AtomLoss::FromF2, QScriptValue(engine, "0"));
thisObject.setProperty(Simulation::AtomLoss, atomLoss);
QScriptValue intialSpin = engine->newArray();
intialSpin.setProperty(0, QScriptValue(engine, "1"));
intialSpin.setProperty(1, QScriptValue(engine, "0"));
intialSpin.setProperty(2, QScriptValue(engine, "0"));
thisObject.setProperty(Simulation::InitialSpin, intialSpin);
thisObject.setProperty(Simulation::Operations, engine->newArray());
thisObject.setProperty(Simulation::Name, engine->undefinedValue());
return thisObject;
}
QScriptValue ScriptLoader::scriptFunctionSimulationRun(QScriptContext * context, QScriptEngine * engine)
{
namespace StrScr = Strings::Script;
// Check the argument count
if(context->argumentCount() > 0) {
qDebug() << "ScriptLoader:Simulation.run called with an incorrect parameter count:" << context->argumentCount() << ", expected no argument.";
return QScriptValue::UndefinedValue;
}
// TODO: Check the arguments type
// TODO: Check the simulation validity
QScriptValue sim = context->thisObject();
Simulation simulation;
simulation.setName(sim.property(StrScr::Simulation::Name).toString());
simulation.setCode(Index::Frequency, sim.property(Strings::Script::Simulation::Frequency).toString());
simulation.setCode(Index::Exchange, sim.property(Strings::Script::Simulation::Exchange).toString());
simulation.setCode(Index::Damping, sim.property(Strings::Script::Simulation::Damping).toString());
simulation.setCode(Index::AtomLossFromF1, sim.property(Strings::Script::Simulation::AtomLoss).property(Strings::Script::AtomLoss::FromF1).toString());
simulation.setCode(Index::AtomLossFromF2, sim.property(Strings::Script::Simulation::AtomLoss).property(Strings::Script::AtomLoss::FromF2).toString());
simulation.setCode(Index::InitialSpinX, sim.property(Strings::Script::Simulation::InitialSpin).property(0).toString());
simulation.setCode(Index::InitialSpinY, sim.property(Strings::Script::Simulation::InitialSpin).property(1).toString());
simulation.setCode(Index::InitialSpinZ, sim.property(Strings::Script::Simulation::InitialSpin).property(2).toString());
SimulationContext simulationContext;
simulationContext.setSettings(ScriptObject::Settings::fromScriptValue(sim.property(StrScr::Simulation::Settings)));
simulationContext.setSimulation(simulation);
for(quint32 i = 0; i < sim.property(StrScr::Simulation::Parameters).property("length").toUInt32(); ++i) {
simulationContext.addParameter(ScriptObject::Parameter::fromScriptValue(sim.property(Strings::Script::Simulation::Parameters).property(i)));
}
for(quint32 i = 0; i < sim.property(StrScr::Simulation::Functions).property("length").toUInt32(); ++i) {
simulationContext.addFunction(ScriptObject::Function::fromScriptValue(sim.property(Strings::Script::Simulation::Functions).property(i)));
}
for(quint32 j = 0; j < sim.property(StrScr::Simulation::Operations).property("length").toUInt32(); ++j) {
simulationContext.addOperation(ScriptObject::Operation::fromScriptValue(sim.property(Strings::Script::Simulation::Operations).property(j)));
}
try {
simulationContext.prepare();
} catch(...) {
qDebug() << "ScriptLoader::scriptFunction:Simulation.run: simulation is invalid";
return engine->undefinedValue();
}
qDebug() << "ScriptLoader::scriptFunction:Simulation.run: starting simulation";
auto startTime = chrono::high_resolution_clock::now();
unsigned int threadCount = thread::hardware_concurrency()-1;
thread * threadPool[threadCount == 0 ? 1 : threadCount];
for(thread * & t : threadPool) {
t = new thread(threadLoop, ref(simulationContext));
}
threadLoop(simulationContext);
for(thread * & t : threadPool) {
t->join();
delete t;
}
vector<ScriptObject::Result> results = simulationContext.end();
auto endTime = chrono::high_resolution_clock::now();
qDebug() << "ScriptLoader::scriptFunction:Simulation.run: simulation ended succesfully ("
<< chrono::duration_cast<chrono::milliseconds>(endTime - startTime).count() << "ms)";
QScriptValue returnValue = engine->newArray(results.size());
for(unsigned int i = 0; i < results.size(); ++i) {
QScriptValue resultScriptObject = engine->evaluate("new Result()");
resultScriptObject.setProperty(Strings::Script::Result::Matches, engine->newFunction(ScriptObject::Result::scriptFunctionMatches));
resultScriptObject.setProperty(Strings::Script::Result::Export, engine->newFunction(ScriptObject::Result::scriptFunctionExport));
//resultScriptObject.setProperty(Strings::Script::Result::Erase, engine->newFunction(ScriptObject::Result::scriptFunctionErase));
resultScriptObject.setData(engine->newVariant(QVariant::fromValue(results.at(i))));
returnValue.setProperty(i, resultScriptObject);
}
return returnValue;
}
/*************************************************************************************************************
* Constructors
************************************************************************************************************/
QScriptEngine * ScriptLoader::p_engine = NULL;
/*************************************************************************************************************
* Public Members
************************************************************************************************************/
void ScriptLoader::init()
{
if(p_engine == nullptr) {
p_engine = new QScriptEngine();
// Add additional functions to the script environment
p_engine->globalObject().setProperty("loadScript", p_engine->newFunction(ScriptLoader::scriptFunctionLoadScript));
p_engine->globalObject().setProperty("Simulation", p_engine->newFunction(ScriptLoader::scriptFunctionSimulationConstructor));
// Load the setup file
QFile setupFile(":/Setup/setup.js");
setupFile.open(QIODevice::ReadOnly);
QString setupScript = QTextStream(&setupFile).readAll();
performSyntaxCheck(setupScript);
evaluate(setupScript);
}
}
bool ScriptLoader::load(QString script)
{
if(performSyntaxCheck(script) && evaluate(script)) {
return true;
}
return false;
}
/*************************************************************************************************************
* Private Members
************************************************************************************************************/
bool ScriptLoader::performSyntaxCheck(const QString & script)
{
QScriptSyntaxCheckResult syntaxCheck = p_engine->checkSyntax(script);
if(syntaxCheck.state() != QScriptSyntaxCheckResult::Valid)
{
qDebug() << "ScriptLoader::load: Syntax error in the script";
qDebug() << "Line" << syntaxCheck.errorLineNumber() << ": error:" << syntaxCheck.errorMessage();
return false;
}
return true;
}
bool ScriptLoader::evaluate(const QString & script)
{
p_engine->evaluate(script);
if(p_engine->hasUncaughtException())
{
qDebug() << "ScriptLoader::load: Uncaught exception within the script:";
qDebug() << "ScriptLoader::load:" << p_engine->uncaughtException().toString().toUtf8().data();
qDebug() << "ScriptLoader::load: Line" << p_engine->uncaughtExceptionLineNumber();
for(QString line : p_engine->uncaughtExceptionBacktrace())
qDebug() << "ScriptLoader::load:" << line.toUtf8().data();
return false;
}
return true;
}