File: shiftModel.C

package info (click to toggle)
ball 1.5.0%2Bgit20180813.37fc53c-6
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 239,888 kB
  • sloc: cpp: 326,149; ansic: 4,208; python: 2,303; yacc: 1,778; lex: 1,099; xml: 958; sh: 322; makefile: 95
file content (293 lines) | stat: -rw-r--r-- 6,733 bytes parent folder | download | duplicates (4)
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
// -*- Mode: C++; tab-width: 2; -*-
// vi: set ts=2:
//

#include <BALL/NMR/shiftModel.h>
#include <BALL/CONCEPT/factory.h>
#include <BALL/FORMAT/parameterSection.h>
#include <BALL/NMR/johnsonBoveyShiftProcessor.h>
#include <BALL/NMR/haighMallionShiftProcessor.h>
#include <BALL/NMR/EFShiftProcessor.h>
#include <BALL/NMR/anisotropyShiftProcessor.h>
#include <BALL/NMR/randomCoilShiftProcessor.h>
#include <BALL/NMR/HBondShiftProcessor.h>
#include <BALL/NMR/empiricalHSShiftProcessor.h>

using namespace std;

namespace BALL
{	
	const char* ShiftModel::MODULE_LIST_SECTION = "ShiftModules";

	ShiftModel::ShiftModel()
		:	ShiftModule(),
			parameters_(),
			modules_(),
			valid_(false)
	{
		registerStandardModules_();
	}

	ShiftModel::ShiftModel(const String& filename)
		:	ShiftModule(),
			parameters_(filename),
			modules_(),
			registered_modules_(),
			valid_(false)
	{
		registerStandardModules_();
		init_();
	}

	ShiftModel::ShiftModel(const ShiftModel& model)
		:	ShiftModule(),
			parameters_(model.parameters_),
			modules_(),
			registered_modules_(model.registered_modules_),
			valid_(false)
	{
		init_();
	}

	void ShiftModel::clear()
	{
		// model is invalid
		valid_ = false;

		// delete modules
		ModuleList::iterator it = modules_.begin();
		for (; it != modules_.end(); ++it)
		{
			delete *it;
		}		
		modules_.clear();

		// reset the list of registerd modules
		registered_modules_.clear();
		registerStandardModules_();

		// clear parameters
		parameters_.clear();

		// clear options
		options.clear();
	}

	ShiftModel::~ShiftModel()
	{
		clear();
	}

	Parameters& ShiftModel::getParameters()
	{
		return parameters_;
	}

	ShiftModel::ModuleList& ShiftModel::getModuleList()	
	{
		return modules_;
	}

	void ShiftModel::setFilename(const String& filename)
	{
		// set the parameter filename 
		parameters_.setFilename(filename);

		// ...and initialize!
		init_();
	}
	
	const String& ShiftModel::getFilename() const	
	{
		return parameters_.getFilename();
	}

	bool ShiftModel::isValid() const
		
	{
		return valid_;
	}

	const ShiftModel& ShiftModel::operator = (const ShiftModel& model)	
	{
		// clear the old contents
		clear();

		// copy the parameters and options
		parameters_ = model.parameters_;
		options = model.options;

		// copy the registered modules
		registered_modules_ = model.registered_modules_;
		
		// and try to set up the same model 
		init_();

		return *this;
	}

	const ShiftModel& ShiftModel::operator = (const String& filename)
	{
		// clear the old contents,
		clear();

		// read the parameters, and initialize
		setFilename(filename);

		return *this;
	}

	bool ShiftModel::init_()
	{
		// inivalidate object
		valid_ = false;

		parameters_.init();
		if (parameters_.isValid() && parameters_.getParameterFile().hasSection(MODULE_LIST_SECTION))
		{
			ParameterSection module_section;
			module_section.extractSection(parameters_, MODULE_LIST_SECTION);

			if (module_section.hasVariable("name"))
			{
				// the section contains the columns "name" and "type", let's construct
				// the corresponding modules 
				Position name_col = module_section.getColumnIndex("name");
				for (Position i = 0; i < module_section.getNumberOfKeys(); i++)
				{
					String type = module_section.getKey(i);
					String name = module_section.getValue(i, name_col);

					if (registered_modules_.has(type))
					{
						// call the corresponding create method to construct the ShiftModule
						ShiftModule* module = createModule_(type, name);

						// and insert the pointer into the module list
						modules_.push_back(module);
					}
					else
					{
						// complain!
						Log.error() << "ShiftModel::init_: could not create module of type " 
												<< type << " for module " << name << " for shift model " 
												<< parameters_.getFilename() << ". Please use the registerModule method"
												<< " to associate a create method for this module type!" 
												<< std::endl;
					}
				}
			}

			valid_ = true;
		}

		// return the current state
		return valid_;
	}
	

	bool ShiftModel::isRegistered(const String& name) const
	{
		return registered_modules_.has(name);
	}

	void ShiftModel::registerModule(const String& name, CreateMethod create_method)
	{
		// check that we did not get something strange
		if (create_method == 0)
		{
			throw Exception::NullPointer(__FILE__, __LINE__);
		}

		// insert the module into the map
		registered_modules_[name] = create_method;
	}

	void ShiftModel::unregisterModule(const String& name)
	{
		registered_modules_.erase(name);
	}

	ShiftModule* ShiftModel::createModule_(const String& type, const String& name) const
	{
		ShiftModule* module = 0;
		if (registered_modules_.has(type))
		{
			// if the name is registered, call the
			// corresponding create method from the hash map
			module = (ShiftModule*)(registered_modules_[type])();
			if (module != 0)
			{
				// If we constructed a module, set its name and the parameters,
				// then initialize the shift module.
				module->setParameters(const_cast<Parameters&>(parameters_));
				module->setName(name);
				module->init();
			}
		}

		return module;
	}

	void ShiftModel::registerStandardModules_()
	{
		registerModule("JohnsonBovey", Factory<JohnsonBoveyShiftProcessor>::createVoid);
		registerModule("HaighMallion", Factory<HaighMallionShiftProcessor>::createVoid);
		registerModule("ElectricField", Factory<EFShiftProcessor>::createVoid);
		registerModule("Anisotropy", Factory<AnisotropyShiftProcessor>::createVoid);
		registerModule("RandomCoil", Factory<RandomCoilShiftProcessor>::createVoid);
		registerModule("HBond", Factory<HBondShiftProcessor>::createVoid);
		registerModule("EmpiricalShiftHyperSurfaces", Factory<EmpiricalHSShiftProcessor>::createVoid);
	}

	Processor::Result ShiftModel::operator () (Composite& composite)
	{
		// Clear previsously assigned shifts and...
		Atom* atom = dynamic_cast<Atom*>(&composite);
		if (atom != 0)
		{
			atom->clearProperty(ShiftModule::PROPERTY__SHIFT);
		}
			
		// ...call operator () for every module.
		Processor::Result result = Processor::CONTINUE;
		ModuleList::iterator it = modules_.begin();
		for (; it != modules_.end(); ++it)
		{
			// abort if any of the modules returns Processor::ABORT
			result = (*it)->operator () (composite);
			if (result == Processor::ABORT)
			{
				break;
			}
		}
		
		return result;
	}


	bool ShiftModel::start()
	{
		ModuleList::iterator it = modules_.begin();
		for (; it != modules_.end(); ++it)
		{
			if (!(*it)->start()) return false;
		}
		
		return true;
	}


	bool ShiftModel::finish()
	{
		ModuleList::iterator it = modules_.begin();
		for (; it != modules_.end(); ++it)
		{
			if (!(*it)->finish()) return false;
		}
		
		return true;
	}
}