9 #ifndef IMPKERNEL_MODEL_H
10 #define IMPKERNEL_MODEL_H
12 #include <IMP/kernel_config.h>
22 #include "internal/AttributeTable.h"
23 #include "internal/attribute_tables.h"
24 #include "internal/moved_particles_cache.h"
27 #include <boost/unordered_map.hpp>
28 #include <boost/unordered_set.hpp>
30 #include <boost/iterator/transform_iterator.hpp>
31 #include <boost/iterator/filter_iterator.hpp>
35 IMPKERNEL_BEGIN_NAMESPACE
41 #if !defined(SWIG) && !defined(IMP_DOXYGEN)
48 COMPUTING_DEPENDENCIES
74 #if !defined(SWIG) && !defined(IMP_DOXYGEN)
76 public internal::Masks,
79 public internal::FloatAttributeTable,
80 public internal::StringAttributeTable,
81 public internal::IntAttributeTable,
82 public internal::ObjectAttributeTable,
83 public internal::WeakObjectAttributeTable,
84 public internal::IntsAttributeTable,
85 public internal::FloatsAttributeTable,
86 public internal::ObjectsAttributeTable,
87 public internal::ParticleAttributeTable,
88 public internal::ParticlesAttributeTable
96 typedef boost::unordered_map<const ModelObject *, NodeInfo>
DependencyGraph;
97 DependencyGraph dependency_graph_;
98 boost::unordered_set<const ModelObject *> no_dependencies_;
99 boost::unordered_map<const ModelObject *, ScoreStatesTemp>
100 required_score_states_;
103 boost::unordered_map<FloatKey, FloatRange> ranges_;
112 void do_clear_required_score_states(
ModelObject *mo);
113 void do_check_required_score_states(
const ModelObject *mo)
const;
114 void do_check_update_order(
const ScoreState *ss)
const;
115 void do_check_inputs_and_outputs(
const ModelObject *mo)
const;
116 void do_check_readers_and_writers(
const ModelObject *mo)
const;
117 void do_check_not_in_readers_and_writers(
const ModelObject *mo)
const;
121 unsigned age_counter_;
125 unsigned dependencies_age_;
128 bool dependencies_saved_;
129 unsigned saved_dependencies_age_;
132 std::vector<ModelObject *> mos_added_since_save_, mos_removed_since_save_;
136 internal::MovedParticlesRestraintCache moved_particles_restraint_cache_;
138 internal::MovedParticlesParticleCache moved_particles_particle_cache_;
140 unsigned moved_particles_cache_age_;
143 void increase_age() {
145 if (age_counter_ == 0) {
150 template <
class MOType,
class MOVector>
151 void do_get_dependent(
ModelObject *mo, MOVector &ret) {
152 const auto &node = dependency_graph_.find(mo);
154 "Object " << mo->get_name()
155 <<
" does not have dependencies.");
157 "Node not in dependency_graph.");
158 MOType *r =
dynamic_cast<MOType *
>(mo);
162 for (
ModelObject *cur : node->second.get_outputs()) {
163 do_get_dependent<MOType, MOVector>(cur, ret);
165 for (
ModelObject *cur : node->second.get_readers()) {
166 do_get_dependent<MOType, MOVector>(cur, ret);
170 #if !defined(IMP_DOXYGEN) && !defined(SWIG)
177 internal::Stage cur_stage_;
180 const std::set<Restraint *> &get_dependent_restraints(
ParticleIndex pi) {
181 return moved_particles_restraint_cache_.get_dependent_restraints(pi);
186 return moved_particles_particle_cache_.get_dependent_particles(pi);
191 bool do_get_has_dependencies(
const ModelObject *mo)
const {
192 return no_dependencies_.find(mo) == no_dependencies_.end();
194 void do_set_has_dependencies(
const ModelObject *mo,
bool tf);
195 void do_set_has_all_dependencies(
bool tf);
197 void validate_computed_derivatives()
const {}
198 void set_has_all_dependencies(
bool tf);
199 bool get_has_all_dependencies()
const;
200 void check_dependency_invariants()
const;
201 void check_dependency_invariants(
const ModelObject *mo)
const;
208 internal::Stage get_stage()
const {
return cur_stage_; }
210 static void do_remove_score_state(
ScoreState *obj);
213 bool do_get_has_required_score_states(
const ModelObject *mo)
const;
214 void do_set_has_required_score_states(
ModelObject *mo,
bool tf);
218 "Doesn't have score states");
219 return required_score_states_.find(mo)->second;
226 Model(std::string name =
"Model %1%");
229 #if !defined(SWIG) && !defined(IMP_DOXYGEN)
230 IMP_MODEL_IMPORT(internal::FloatAttributeTable);
231 IMP_MODEL_IMPORT(internal::StringAttributeTable);
232 IMP_MODEL_IMPORT(internal::IntAttributeTable);
233 IMP_MODEL_IMPORT(internal::ObjectAttributeTable);
234 IMP_MODEL_IMPORT(internal::WeakObjectAttributeTable);
235 IMP_MODEL_IMPORT(internal::IntsAttributeTable);
236 IMP_MODEL_IMPORT(internal::FloatsAttributeTable);
237 IMP_MODEL_IMPORT(internal::ObjectsAttributeTable);
238 IMP_MODEL_IMPORT(internal::ParticleAttributeTable);
239 IMP_MODEL_IMPORT(internal::ParticlesAttributeTable);
253 #if !defined(IMP_DOXYGEN)
275 do_remove_score_state(obj));
300 void add_attribute(TypeKey attribute_key,
ParticleIndex particle, Type value);
304 void remove_attribute(TypeKey attribute_key,
ParticleIndex particle);
307 bool get_has_attribute(TypeKey attribute_key,
ParticleIndex particle)
const;
311 void set_attribute(TypeKey attribute_key,
ParticleIndex particle, Type value);
315 Type get_attribute(TypeKey attribute_key,
ParticleIndex particle);
328 void add_cache_attribute(TypeKey attribute_key,
ParticleIndex particle,
333 void set_is_optimized(TypeKey attribute_key,
ParticleIndex particle,
339 #define IMP_MODEL_ATTRIBUTE_METHODS(Type, Value) \
340 void add_attribute(Type##Key attribute_key, ParticleIndex particle, \
342 void remove_attribute(Type##Key attribute_key, ParticleIndex particle); \
343 bool get_has_attribute(Type##Key attribute_key, \
344 ParticleIndex particle) const; \
345 void set_attribute(Type##Key attribute_key, ParticleIndex particle, \
347 Value get_attribute(Type##Key attribute_key, ParticleIndex particle); \
348 void add_cache_attribute(Type##Key attribute_key, ParticleIndex particle, \
352 IMP_MODEL_ATTRIBUTE_METHODS(
Int,
Int);
354 IMP_MODEL_ATTRIBUTE_METHODS(
Ints,
Ints);
359 IMP_MODEL_ATTRIBUTE_METHODS(WeakObject,
Object *);
368 return particle_index_[p];
373 if (particle_index_.size() <= get_as_unsigned_int(p))
return false;
374 return particle_index_[p];
401 bool get_has_data(
ModelKey mk)
const;
439 if (trigger_age_.size() > tk.get_index()) {
440 return trigger_age_[tk.get_index()];
448 if (tk.get_index() >= trigger_age_.size()) {
449 trigger_age_.resize(tk.get_index() + 1, 0);
451 trigger_age_[tk.get_index()] = age_counter_;
464 dependencies_saved_ =
true;
465 saved_dependencies_age_ = dependencies_age_;
467 mos_added_since_save_.clear();
468 mos_removed_since_save_.clear();
487 if (dependencies_saved_) {
488 dependencies_saved_ =
false;
489 dependencies_age_ = saved_dependencies_age_;
492 std::sort(mos_added_since_save_.begin(), mos_added_since_save_.end());
493 std::sort(mos_removed_since_save_.begin(),
494 mos_removed_since_save_.end());
496 "ModelObjects added do not match those removed");
510 #if !defined(IMP_DOXYGEN)
515 IMPKERNEL_END_NAMESPACE
Particle * get_particle(ParticleIndex p) const
Get the particle from an index.
#define IMP_IF_CHECK(level)
Execute the code block if a certain level checks are on.
Used to hold a set of related restraints.
boost::graph DependencyGraph
Directed graph on the interactions between the various objects in the model.
The base class for undecorators.
#define IMP_OBJECT_METHODS(Name)
Define the basic things needed by any Object.
void restore_dependencies()
Restore ModelObject dependencies to previous restore point.
void add_particle(RMF::FileHandle fh, Particle *hs)
Macros to help in defining tuple classes.
virtual void clear_caches()
unsigned get_dependencies_updated()
Get the model age when ModelObject dependencies were last changed, or 0.
unsigned get_particles_size() const
Get an upper bound on the number of particles in the Model.
unsigned get_age()
Get the current 'model time'.
bool get_has_particle(ParticleIndex p) const
Check whether a given particle index exists.
Macros to define containers of objects.
unsigned get_trigger_last_updated(TriggerKey tk)
Get the time when the given trigger was last updated, or 0.
#define IMP_INTERNAL_CHECK(expr, message)
An assertion to check for internal errors in IMP. An IMP::ErrorException will be thrown.
Class for storing model, its restraints, constraints, and particles.
Base class for objects in a Model that depend on other objects.
virtual void do_destroy()
Common base class for heavy weight IMP objects.
ParticleIndexes get_particle_indexes(ParticlesTemp const &particles)
ScoreStates maintain invariants in the Model.
Implements a vector tied to a particular index of type Index<Tag>.
Base class for objects in a Model that depend on other objects.
Classes to handle individual model particles. (Note that implementation of inline functions is in int...
A nullptr-initialized pointer to an IMP Object.
void save_dependencies()
Mark a 'restore point' for ModelObject dependencies.
A shared base class to help in debugging and things.
Represents a scoring function on the model.
double Float
Basic floating-point value (could be float, double...)
Class to handle individual particles of a Model object.
bool get_has_dependencies() const
Return whether this object has dependencies computed.
#define IMP_USAGE_CHECK(expr, message)
A runtime test for incorrect usage of a class or method.
Abstract base class for all restraints.
int Int
Basic integer value.
void set_trigger_updated(TriggerKey tk)
Update the given trigger.
std::string String
Basic string value.
Class for adding derivatives from restraints to the model.