1 /**
2 * UGENE - Integrated Bioinformatics Tools.
3 * Copyright (C) 2008-2021 UniPro <ugene@unipro.ru>
4 * http://ugene.net
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 * MA 02110-1301, USA.
20 */
21
22 #include "ScriptWorker.h"
23
24 #include <QScriptEngineDebugger>
25
26 #include <U2Core/AppContext.h>
27 #include <U2Core/DNAAlphabet.h>
28 #include <U2Core/DNATranslation.h>
29 #include <U2Core/FailTask.h>
30 #include <U2Core/Log.h>
31 #include <U2Core/U2SafePoints.h>
32
33 #include <U2Designer/DelegateEditors.h>
34
35 #include <U2Lang/ActorContext.h>
36 #include <U2Lang/ActorPrototypeRegistry.h>
37 #include <U2Lang/BaseActorCategories.h>
38 #include <U2Lang/BaseSlots.h>
39 #include <U2Lang/BaseTypes.h>
40 #include <U2Lang/CoreLibConstants.h>
41 #include <U2Lang/IncludedProtoFactory.h>
42 #include <U2Lang/ScriptEngineUtils.h>
43 #include <U2Lang/ScriptLibrary.h>
44 #include <U2Lang/SequencePrototype.h>
45 #include <U2Lang/WorkflowEnv.h>
46
47 namespace U2 {
48 namespace LocalWorkflow {
49
50 const QString ScriptWorkerFactory::ACTOR_ID("Script-");
51
52 const static QString INPUT_PORT_TYPE("input-for-");
53 const static QString OUTPUT_PORT_TYPE("output-for-");
54
55 static const QString IN_PORT_ID("in");
56 static const QString OUT_PORT_ID("out");
57
ScriptWorkerTask(WorkflowScriptEngine * _engine,AttributeScript * _script)58 ScriptWorkerTask::ScriptWorkerTask(WorkflowScriptEngine *_engine, AttributeScript *_script)
59 : Task(tr("Script worker task"), AppContext::isGUIMode() ? TaskFlag_RunInMainThread : TaskFlag_None), engine(_engine), script(_script) {
60 WorkflowScriptLibrary::initEngine(engine);
61 }
62
run()63 void ScriptWorkerTask::run() {
64 QMap<QString, QScriptValue> scriptVars;
65 foreach (const Descriptor &key, script->getScriptVars().uniqueKeys()) {
66 assert(!key.getId().isEmpty());
67 if (!(script->getScriptVars().value(key)).isNull()) {
68 scriptVars[key.getId()] = engine->newVariant(script->getScriptVars().value(key));
69 } else {
70 scriptVars[key.getId()] = engine->newVariant(engine->globalObject().property(key.getId().toLatin1().data()).toVariant());
71 }
72 }
73 QScriptValue scriptResultValue = ScriptTask::runScript(engine, scriptVars, script->getScriptText(), stateInfo);
74 if (engine->hasUncaughtException()) {
75 scriptResultValue = engine->uncaughtException();
76 QString message = scriptResultValue.toString();
77 stateInfo.setError(tr("Error in line ") + QString::number(engine->uncaughtExceptionLineNumber()) + ":" + message.split(":").last());
78 }
79 result = scriptResultValue.toVariant();
80
81 if (stateInfo.cancelFlag) {
82 if (!stateInfo.hasError()) {
83 stateInfo.setError("Script task canceled");
84 }
85 }
86 }
87
getResult() const88 QVariant ScriptWorkerTask::getResult() const {
89 return result;
90 }
91
getEngine()92 WorkflowScriptEngine *ScriptWorkerTask::getEngine() {
93 return engine;
94 }
95
composeRichDoc()96 QString ScriptPromter::composeRichDoc() {
97 return target->getProto()->getDocumentation();
98 }
99
init(QList<DataTypePtr> input,QList<DataTypePtr> output,QList<Attribute * > attrs,const QString & name,const QString & description,const QString & actorFilePath)100 bool ScriptWorkerFactory::init(QList<DataTypePtr> input,
101 QList<DataTypePtr> output,
102 QList<Attribute *> attrs,
103 const QString &name,
104 const QString &description,
105 const QString &actorFilePath) {
106 ActorPrototype *proto = IncludedProtoFactory::getScriptProto(
107 input, output, attrs, name, description, actorFilePath);
108 WorkflowEnv::getProtoRegistry()->registerProto(BaseActorCategories::CATEGORY_SCRIPT(), proto);
109 IncludedProtoFactory::registerScriptWorker(ACTOR_ID + name);
110 return true;
111 }
112
createWorker(Actor * a)113 Worker *ScriptWorkerFactory::createWorker(Actor *a) {
114 return new ScriptWorker(a);
115 }
116
ScriptWorker(Actor * a)117 ScriptWorker::ScriptWorker(Actor *a)
118 : BaseWorker(a), input(nullptr), output(nullptr), taskFinished(false) {
119 script = a->getScript();
120 engine = nullptr;
121 }
122
init()123 void ScriptWorker::init() {
124 input = ports.value(IN_PORT_ID);
125 output = ports.value(OUT_PORT_ID);
126 engine = new WorkflowScriptEngine(context);
127 if (AppContext::isGUIMode()) { // add script debugger
128 engine->setProcessEventsInterval(50);
129 QScriptEngineDebugger *scriptDebugger = new QScriptEngineDebugger(engine);
130 scriptDebugger->setAutoShowStandardWindow(true);
131 scriptDebugger->attachTo(engine);
132 }
133 }
134
bindPortVariables()135 void ScriptWorker::bindPortVariables() {
136 foreach (IntegralBus *bus, ports.values()) {
137 assert(bus != nullptr);
138 if (actor->getPort(bus->getPortId())->isOutput()) { // means that it is bus for output port
139 continue;
140 }
141
142 QVariantMap busData = bus->lookMessage().getData().toMap();
143 foreach (const QString &slotId, busData.keys()) {
144 QString attrId = "in_" + slotId;
145 if (script->hasVarWithId(attrId)) {
146 script->setVarValueWithId(attrId, busData.value(slotId));
147 }
148 }
149 }
150 }
151
bindAttributeVariables()152 void ScriptWorker::bindAttributeVariables() {
153 QMap<QString, Attribute *> attrs = actor->getParameters();
154 QMap<QString, Attribute *>::iterator it;
155 for (it = attrs.begin(); it != attrs.end(); it++) {
156 Attribute *attr = it.value();
157 if (script->hasVarWithId(attr->getId())) {
158 script->setVarValueWithId(attr->getId(), attr->getAttributePureValue());
159 }
160 }
161 }
162
isNeedToBeDone() const163 bool ScriptWorker::isNeedToBeDone() const {
164 bool result = false;
165 if (actor->getInputPorts().isEmpty()) {
166 result = taskFinished;
167 } else {
168 bool hasNotEnded = false;
169 foreach (Port *port, actor->getInputPorts()) {
170 IntegralBus *input = ports[port->getId()];
171 SAFE_POINT(nullptr != input, "NULL input bus", false);
172 if (!input->isEnded()) {
173 hasNotEnded = true;
174 break;
175 }
176 }
177 result = !hasNotEnded;
178 }
179 return result;
180 }
181
isNeedToBeRun() const182 bool ScriptWorker::isNeedToBeRun() const {
183 bool result = true;
184 if (actor->getInputPorts().isEmpty()) {
185 result = !taskFinished;
186 } else {
187 foreach (Port *port, actor->getInputPorts()) {
188 IntegralBus *input = ports[port->getId()];
189 SAFE_POINT(nullptr != input, "NULL input bus", false);
190 if (!input->hasMessage()) {
191 result = false;
192 break;
193 }
194 }
195 }
196 return result;
197 }
198
setDone()199 void ScriptWorker::setDone() {
200 BaseWorker::setDone();
201 foreach (Port *port, actor->getOutputPorts()) {
202 IntegralBus *output = ports[port->getId()];
203 SAFE_POINT(nullptr != output, "NULL output bus", );
204 output->setEnded();
205 }
206 }
207
tick()208 Task *ScriptWorker::tick() {
209 if (script->isEmpty()) {
210 coreLog.error(tr("no script text"));
211 return new FailTask(tr("no script text"));
212 }
213
214 if (isNeedToBeRun()) {
215 // WorkflowScriptLibrary::initEngine(engine);
216 // engine->globalObject().setProperty("ctx", ActorContext::createContext(this, engine), QScriptValue::ReadOnly);
217 bindPortVariables();
218 bindAttributeVariables();
219 foreach (Port *port, actor->getInputPorts()) {
220 getMessageAndSetupScriptValues(ports[port->getId()]);
221 }
222 Task *t = new ScriptWorkerTask(engine, script);
223 connect(t, SIGNAL(si_stateChanged()), SLOT(sl_taskFinished()));
224 return t;
225 } else if (isNeedToBeDone()) {
226 setDone();
227 }
228 return nullptr;
229 }
230
sl_taskFinished()231 void ScriptWorker::sl_taskFinished() {
232 taskFinished = true;
233 ScriptWorkerTask *t = qobject_cast<ScriptWorkerTask *>(sender());
234 if (t->getState() != Task::State_Finished || t->hasError() || t->isCanceled()) {
235 return;
236 }
237
238 QString name = actor->getProto()->getDisplayName();
239 DataTypeRegistry *dtr = WorkflowEnv::getDataTypeRegistry();
240 assert(dtr);
241 DataTypePtr ptr = dtr->getById(OUTPUT_PORT_TYPE + name);
242
243 if (ptr->getAllDescriptors().size() == 1 && ptr->getAllDescriptors().first().getId() == BaseTypes::MULTIPLE_ALIGNMENT_TYPE()->getId()) {
244 if (input != nullptr && !input->isEnded()) {
245 return;
246 }
247 }
248
249 QVariantMap map;
250 bool hasSeqArray = false;
251 foreach (const Descriptor &desc, ptr->getAllDescriptors()) {
252 QString varName = "out_" + desc.getId();
253 QScriptValue value = t->getEngine()->globalObject().property(varName.toLatin1().data());
254 if (BaseSlots::DNA_SEQUENCE_SLOT().getId() == desc.getId()) {
255 if (value.isArray()) {
256 hasSeqArray = true;
257 continue;
258 }
259 SharedDbiDataHandler seqId = ScriptEngineUtils::getDbiId(t->getEngine(), value, SequenceScriptClass::CLASS_NAME);
260 if (!seqId.constData() || !seqId.constData()->isValid()) {
261 continue;
262 }
263 map[desc.getId()] = qVariantFromValue(seqId);
264 } else {
265 map[desc.getId()] = value.toVariant();
266 }
267 }
268 if (output) {
269 if (hasSeqArray) {
270 QString varName = "out_" + BaseSlots::DNA_SEQUENCE_SLOT().getId();
271 QScriptValue value = t->getEngine()->globalObject().property(varName.toLatin1().data());
272 for (int i = 0; i < value.property("length").toInt32(); i++) {
273 SharedDbiDataHandler seqId = ScriptEngineUtils::getDbiId(t->getEngine(), value.property(i), SequenceScriptClass::CLASS_NAME);
274 if (seqId.constData() && seqId.constData()->isValid()) {
275 map[BaseSlots::DNA_SEQUENCE_SLOT().getId()] = qVariantFromValue(seqId);
276 output->put(Message(ptr, map));
277 }
278 }
279 } else {
280 QVariant scriptResult = t->getResult();
281 if (!map.isEmpty()) {
282 output->put(Message(ptr, map));
283 }
284 }
285 }
286 }
287
cleanup()288 void ScriptWorker::cleanup() {
289 delete engine;
290 }
291
292 } // namespace LocalWorkflow
293 } // namespace U2
294