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
|
// Copyright 2006-2007 Nanorex, Inc. See LICENSE file for details.
#include "Nanorex/Interface/NXEntityManager.h"
namespace Nanorex {
/* CONSTRUCTOR */
NXEntityManager::NXEntityManager() {
importFileTypesString = "";
exportFileTypesString = "";
dataStoreInfo = new NXDataStoreInfo();
}
/* DESTRUCTOR */
NXEntityManager::~NXEntityManager() {
//delete rootMoleculeSet;
////delete dataImpExpPluginGroup;
}
/* FUNCTION: loadDataImportExportPlugins */
/**
* Loads and initializes all the import/export plugins.
*/
void NXEntityManager::loadDataImportExportPlugins(NXProperties* properties) {
int pluginIndex = 0;
string msg, pluginFormats;
string pluginKey = "ImportExport.0";
string pluginLibrary =
string(properties->getProperty(pluginKey + ".plugin"));
if (pluginLibrary.length() == 0) {
msg = "No Data Import/Export plugins to load.";
cout << "WARNING: " << msg << endl;
NXLOG_WARNING("NXEntityManager", msg);
}
NXDataImportExportPlugin* plugin;
dataImpExpPluginGroup = new NXPluginGroup();
while (pluginLibrary.length() != 0) {
plugin = 0;
if (dataImpExpPluginGroup->load(pluginLibrary.c_str()))
plugin =
(NXDataImportExportPlugin*)
(dataImpExpPluginGroup->instantiate(pluginLibrary.c_str()));
if (plugin == 0) {
msg =
"Couldn't load Data Import/Export plugin: " +
pluginLibrary;
NXLOG_WARNING("NXEntityManager", msg);
cout << "WARNING: " << msg << endl;
} else {
// Import formats registration
pluginFormats =
string(properties->getProperty(pluginKey +
".importFormats"));
if (importFileTypesString.length() == 0)
importFileTypesString = pluginFormats;
else
importFileTypesString.append(";;").append(pluginFormats);
int index1 = pluginFormats.find("(");
int index2 = 0;
string formats, format;
while (index1 > 0) {
index2 = pluginFormats.find(")", index1 + 1);
formats = pluginFormats.substr(index1 + 1, index2 - index1 - 1);
NXStringTokenizer tokenizer(formats, " ");
while (tokenizer.hasMoreTokens()) {
format = tokenizer.getNextToken();
// Remove the "*."
format = format.substr(2);
dataImportTable[format] = plugin;
}
index1 = pluginFormats.find("(", index2 + 1);
}
// Export formats registration
pluginFormats =
string(properties->getProperty(pluginKey +
".exportFormats"));
if (exportFileTypesString.length() == 0)
exportFileTypesString = pluginFormats;
else
exportFileTypesString.append(";;").append(pluginFormats);
index1 = pluginFormats.find("(");
index2 = 0;
while (index1 > 0) {
index2 = pluginFormats.find(")", index1 + 1);
formats = pluginFormats.substr(index1 + 1, index2 - index1 - 1);
NXStringTokenizer tokenizer(formats, " ");
while (tokenizer.hasMoreTokens()) {
format = tokenizer.getNextToken();
// Remove the "*."
format = format.substr(2);
dataExportTable[format] = plugin;
}
index1 = pluginFormats.find("(", index2 + 1);
}
}
pluginIndex++;
pluginKey =
string("ImportExport.") +
NXUtility::itos(pluginIndex);
pluginLibrary =
string(properties->getProperty(pluginKey + ".plugin"));
}
}
/* FUNCTION: importFromFile */
/**
* @param frameSetId If -1, import all frames into a new frame set,
* otherwise, resume importing into the given frame set.
*/
NXCommandResult* NXEntityManager::importFromFile(const string& filename,
int frameSetId,
bool inPollingThread) {
NXCommandResult* result;
//PR_Lock(importExportPluginsMutex);
string fileType = getFileType(filename);
map<string, NXDataImportExportPlugin*>::iterator iter =
dataImportTable.find(fileType);
if (iter != dataImportTable.end()) {
NXDataImportExportPlugin* plugin = iter->second;
if (frameSetId == -1)
frameSetId = addFrameSet();
dataStoreInfo->setFilename(filename, frameSetId);
int frameIndex = getFrameCount(frameSetId);
NXMoleculeSet* moleculeSet = new NXMoleculeSet();
try {
result =
plugin->importFromFile(moleculeSet,
dataStoreInfo, filename, frameSetId,
frameIndex);
if (result->getResult() == NX_CMD_SUCCESS) {
// Delete the molecule (and don't add the frame) if the
// molecule set wasn't populated.
if ((moleculeSet->childCount() == 0) &&
(moleculeSet->moleculeCount() == 0)) {
delete moleculeSet;
} else {
int idx = addFrame(frameSetId, moleculeSet);
if (inPollingThread) {
emit newFrameAdded(frameSetId, frameIndex, moleculeSet);
}
}
// TODO:
// Finish the current frame set, then examine the dataStoreInfo
// for additional files to import.
while ((!dataStoreInfo->isLastFrame(frameSetId)) &&
(result->getResult() == NX_CMD_SUCCESS)) {
frameIndex = addFrame(frameSetId);
result =
plugin->importFromFile(getRootMoleculeSet(frameSetId,
frameIndex),
dataStoreInfo, filename,
frameSetId, frameIndex);
if (inPollingThread) {
emit newFrameAdded(frameSetId, frameIndex,
getRootMoleculeSet(frameSetId,
frameIndex));
}
// TODO: handle result == failed
}
// Spawn a thread to keep reading incomplete data stores
// as necessary
if (!inPollingThread &&
!dataStoreInfo->storeIsComplete(frameSetId)) {
DataStorePollingThread* pollingThread =
new DataStorePollingThread(this, frameSetId);
pollingThread->start();
}
} else
delete moleculeSet;
} catch (...) {
delete moleculeSet;
string msg = fileType;
msg += "->importFromFile() threw exception";
NXLOG_SEVERE("NXEntityManager", msg);
result = new NXCommandResult();
result->setResult(NX_PLUGIN_CAUSED_ERROR);
vector<QString> resultVector;
resultVector.push_back("NXEntityManager");
QString pluginDescriptor = fileType.c_str();
pluginDescriptor.append("->importFromFile()");
resultVector.push_back(pluginDescriptor);
resultVector.push_back("threw exception");
result->setParamVector(resultVector);
}
} else {
string msg =
"importFromFile: no NXDataImportExportPlugin found to handle type: " +
fileType;
NXLOG_WARNING("NXEntityManager", msg);
result = new NXCommandResult();
result->setResult(NX_PLUGIN_NOT_FOUND);
vector<QString> resultVector;
resultVector.push_back("NXEntityManager");
resultVector.push_back(fileType.c_str());
resultVector.push_back("not found");
result->setParamVector(resultVector);
}
//PR_Unlock(importExportPluginsMutex);
return result;
}
/* FUNCTION: exportToFile */
/**
* Exports the system to the specified file with the appropriate
* import/export plugin.
*
* @param frameSetId The identifier of the frame set to export. If frameSetId
* is -1, write all framesets.
* @param frameIndex The frame to export. If frameIndex is -1, write all
* frames.
*/
NXCommandResult* NXEntityManager::exportToFile
(const string& filename, int frameSetId, int frameIndex) {
/*
* NOTE: We don't explicitly handle frameSetId == -1 (export all frame sets)
* yet. This would be File | Export | Entire results...
*/
NXCommandResult* result = 0;
//PR_Lock(importExportPluginsMutex);
// TODO: Abort if there are no molecule sets, general error checking.
string fileType = getFileType(filename);
map<string, NXDataImportExportPlugin*>::iterator iter =
dataExportTable.find(fileType);
if (iter != dataExportTable.end()) {
NXDataImportExportPlugin* plugin = iter->second;
try {
bool allFrameExport = (frameIndex == -1);
if (allFrameExport)
frameIndex = 0;
dataStoreInfo->setLastFrame
(frameSetId,
!allFrameExport ||
(frameIndex > (int)moleculeSets[frameSetId].size() - 2));
vector<NXMoleculeSet*>::iterator iter =
moleculeSets[frameSetId].begin();
result =
plugin->exportToFile(*iter, dataStoreInfo, filename,
frameSetId, frameIndex);
iter++;
frameIndex++;
while (allFrameExport &&
(iter != moleculeSets[frameSetId].end()) &&
(result->getResult() == NX_CMD_SUCCESS)) {
dataStoreInfo->setLastFrame
(frameSetId,
frameIndex > (int)moleculeSets[frameSetId].size() - 2);
result =
plugin->exportToFile(*iter, dataStoreInfo, filename,
frameSetId, frameIndex);
iter++;
frameIndex++;
}
if (result->getResult() != NX_CMD_SUCCESS)
NXLOG_SEVERE("NXEntityManager",
qPrintable(GetNV1ResultCodeString(result)));
} catch (...) {
string msg = fileType;
msg += "->exportToFile() threw exception";
NXLOG_SEVERE("NXEntityManager", msg);
result->setResult(NX_PLUGIN_CAUSED_ERROR);
std::vector<QString> resultVector;
resultVector.push_back("NXEntityManager");
QString pluginDescriptor = fileType.c_str();
pluginDescriptor.append("->exportToFile()");
resultVector.push_back(pluginDescriptor);
resultVector.push_back("threw exception");
result->setParamVector(resultVector);
}
} else {
std::string msg =
"exportToFile: no DataImportExportPlugin found to handle type: " +
fileType;
NXLOG_WARNING("NXEntityManager", msg);
result = new NXCommandResult();
result->setResult(NX_PLUGIN_NOT_FOUND);
// %1 Who is reporting
// %2 The name of the plugin that was not found
// %3 Why the plugin was not found
std::vector<QString> resultVector;
resultVector.push_back("NXEntityManager");
resultVector.push_back(fileType.c_str());
resultVector.push_back("not found");
result->setParamVector(resultVector);
}
//PR_Unlock(importExportPluginsMutex);
return result;
}
/* FUNCTION: getFileType */
string NXEntityManager::getFileType(const string& filename) {
int index = filename.rfind(".");
if (index > 0)
return filename.substr(index + 1);
else
return "";
}
} // Nanorex::
|