www.mooseframework.org
Classes | Public Member Functions | Private Member Functions | Private Attributes | List of all members
TheWarehouse Class Reference

TheWarehouse is a container for MooseObjects that allows querying/filtering over various customizeable attributes. More...

#include <TheWarehouse.h>

Classes

class  Query
 Query is a convenient way to construct and pass around (possible partially constructed) warehouse queries. More...
 

Public Member Functions

 TheWarehouse ()
 
 ~TheWarehouse ()
 
template<typename T , typename... Args>
unsigned int registerAttribute (const std::string &name, Args... dummy_args)
 registers a new "tracked" attribute of type T for the warehouse. More...
 
unsigned int attribID (const std::string &name)
 Returns a unique ID associated with the given attribute name - i.e. More...
 
void add (std::shared_ptr< MooseObject > obj, const std::string &system)
 add adds a new object to the warehouse and stores attributes/metadata about it for running queries/filtering. More...
 
void update (MooseObject *obj)
 update updates the metadata/attribute-info stored for the given object obj that must already exists in the warehouse. More...
 
void update (MooseObject *obj, const Attribute &extra)
 update updates the metadata/attribute-info stored for the given object obj that must already exists in the warehouse. More...
 
Query query ()
 query creates and returns an initialized a query object for querying objects from the warehouse. More...
 
size_t count (const std::vector< std::unique_ptr< Attribute >> &conds)
 count returns the number of objects that match the provided query conditions. More...
 
template<typename T >
std::vector< T * > & queryInto (const std::vector< std::unique_ptr< Attribute >> &conds, std::vector< T *> &results)
 queryInto takes the given conditions (i.e. More...
 

Private Member Functions

size_t queryID (const std::vector< std::unique_ptr< Attribute >> &conds)
 
template<typename T >
std::vector< T * > & queryInto (int query_id, std::vector< T *> &results, bool show_all=false)
 
int prepare (std::vector< std::unique_ptr< Attribute >> conds)
 prepares a query and returns an associated query_id (i.e. for use with the query function). More...
 
const std::vector< MooseObject * > & query (int query_id)
 callers of this function must lock _obj_cache_mutex as long as a reference to the returned vector is being used. More...
 
void readAttribs (const MooseObject *obj, const std::string &system, std::vector< std::unique_ptr< Attribute >> &attribs)
 

Private Attributes

std::unique_ptr< Storage_store
 
std::vector< std::shared_ptr< MooseObject > > _objects
 
std::unordered_map< MooseObject *, size_t > _obj_ids
 
std::vector< std::vector< MooseObject * > > _obj_cache
 
std::unordered_map< std::vector< std::unique_ptr< Attribute > >, int > _query_cache
 
std::unordered_map< std::string, unsigned int > _attrib_ids
 
std::vector< std::unique_ptr< Attribute > > _attrib_list
 
std::mutex _obj_mutex
 
std::mutex _query_cache_mutex
 
std::mutex _obj_cache_mutex
 

Detailed Description

TheWarehouse is a container for MooseObjects that allows querying/filtering over various customizeable attributes.

The meta-data about the objects is read/stored when the objects are added to the warehouse - updates to objects' state will not be reflected in query results unless the object is explicitly updated through the warehouse interface. The warehouse object can safely be queried concurrently from multiple threads.

Once Query and Attribute objects have been constructed, they are tied to the specific warehouse they were created with. They must not be used for different warehouses or the attribute ID they store internally will be wrong and that is bad.

Definition at line 146 of file TheWarehouse.h.

Constructor & Destructor Documentation

◆ TheWarehouse()

TheWarehouse::TheWarehouse ( )

Definition at line 88 of file TheWarehouse.C.

88 : _store(new VecStore()) {}
std::unique_ptr< Storage > _store
Definition: TheWarehouse.h:314

◆ ~TheWarehouse()

TheWarehouse::~TheWarehouse ( )

Definition at line 89 of file TheWarehouse.C.

89 {}

Member Function Documentation

◆ add()

void TheWarehouse::add ( std::shared_ptr< MooseObject obj,
const std::string &  system 
)

add adds a new object to the warehouse and stores attributes/metadata about it for running queries/filtering.

The warehouse will maintain a pointer to the object indefinitely.

Definition at line 94 of file TheWarehouse.C.

Referenced by FEProblemBase::addUserObject().

95 {
96  isValid(obj.get());
97 
98  size_t obj_id = 0;
99  {
100  std::lock_guard<std::mutex> lock(_obj_mutex);
101  _objects.push_back(obj);
102  obj_id = _objects.size() - 1;
103  _obj_ids[obj.get()] = obj_id;
104 
105  // reset/invalidate the query cache since query results may have been affected by this warehouse
106  // insertion.
107  _obj_cache.clear();
108  _query_cache.clear();
109  }
110 
111  std::vector<std::unique_ptr<Attribute>> attribs;
112  readAttribs(obj.get(), system, attribs);
113  _store->add(obj_id, std::move(attribs));
114 }
void isValid(MooseObject *obj)
Definition: TheWarehouse.C:249
nl system()
std::vector< std::shared_ptr< MooseObject > > _objects
Definition: TheWarehouse.h:315
std::unique_ptr< Storage > _store
Definition: TheWarehouse.h:314
std::unordered_map< std::vector< std::unique_ptr< Attribute > >, int > _query_cache
Definition: TheWarehouse.h:324
std::mutex _obj_mutex
Definition: TheWarehouse.h:329
std::unordered_map< MooseObject *, size_t > _obj_ids
Definition: TheWarehouse.h:316
void readAttribs(const MooseObject *obj, const std::string &system, std::vector< std::unique_ptr< Attribute >> &attribs)
Definition: TheWarehouse.C:228
std::vector< std::vector< MooseObject * > > _obj_cache
Definition: TheWarehouse.h:320

◆ attribID()

unsigned int TheWarehouse::attribID ( const std::string &  name)
inline

Returns a unique ID associated with the given attribute name - i.e.

an attribute and name that were previously registered via calls to registerAttribute. Users should generally not need to use this function.

Definition at line 244 of file TheWarehouse.h.

245  {
246  auto it = _attrib_ids.find(name);
247  if (it != _attrib_ids.end())
248  return it->second;
249  mooseError("no ID exists for unregistered attribute '", name, "'");
250  }
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:208
std::unordered_map< std::string, unsigned int > _attrib_ids
Definition: TheWarehouse.h:326

◆ count()

size_t TheWarehouse::count ( const std::vector< std::unique_ptr< Attribute >> &  conds)

count returns the number of objects that match the provided query conditions.

This requires executing a full query operation (i.e. as if calling queryInto). A Query object should generally be used via the query() member function instead.

Definition at line 215 of file TheWarehouse.C.

Referenced by TheWarehouse::Query::count().

216 {
217  auto query_id = queryID(conds);
218  std::lock_guard<std::mutex> lock(_obj_cache_mutex);
219  auto & objs = query(query_id);
220  size_t count = 0;
221  for (auto obj : objs)
222  if (obj->enabled())
223  count++;
224  return count;
225 }
std::mutex _obj_cache_mutex
Definition: TheWarehouse.h:331
size_t queryID(const std::vector< std::unique_ptr< Attribute >> &conds)
Definition: TheWarehouse.C:198
Query query()
query creates and returns an initialized a query object for querying objects from the warehouse...
Definition: TheWarehouse.h:266
size_t count(const std::vector< std::unique_ptr< Attribute >> &conds)
count returns the number of objects that match the provided query conditions.
Definition: TheWarehouse.C:215

◆ prepare()

int TheWarehouse::prepare ( std::vector< std::unique_ptr< Attribute >>  conds)
private

prepares a query and returns an associated query_id (i.e. for use with the query function).

Definition at line 141 of file TheWarehouse.C.

Referenced by queryID().

142 {
143  auto obj_ids = _store->query(conds);
144 
145  std::lock_guard<std::mutex> lock(_obj_cache_mutex);
146  _obj_cache.push_back({});
147  auto query_id = _obj_cache.size() - 1;
148  auto & vec = _obj_cache.back();
149  {
150  std::lock_guard<std::mutex> lock(_query_cache_mutex);
151  _query_cache[std::move(conds)] = query_id;
152  }
153 
154  std::lock_guard<std::mutex> o_lock(_obj_mutex);
155  for (auto & id : obj_ids)
156  vec.push_back(_objects[id].get());
157 
158  if (!vec.empty() && dynamic_cast<DependencyResolverInterface *>(vec[0]))
159  {
160  std::vector<DependencyResolverInterface *> dependers;
161  for (auto obj : vec)
162  {
163  auto d = dynamic_cast<DependencyResolverInterface *>(obj);
164  if (!d)
165  {
166  dependers.clear();
167  break;
168  }
169  dependers.push_back(d);
170  }
171 
172  try
173  {
175  }
177  {
178  DependencyResolverInterface::cyclicDependencyError<GeneralUserObject *>(
179  e, "Cyclic dependency detected in object ordering");
180  }
181 
182  for (unsigned int i = 0; i < dependers.size(); i++)
183  vec[i] = dynamic_cast<MooseObject *>(dependers[i]);
184  }
185 
186  return query_id;
187 }
std::mutex _obj_cache_mutex
Definition: TheWarehouse.h:331
std::mutex _query_cache_mutex
Definition: TheWarehouse.h:330
std::vector< std::shared_ptr< MooseObject > > _objects
Definition: TheWarehouse.h:315
std::unique_ptr< Storage > _store
Definition: TheWarehouse.h:314
std::unordered_map< std::vector< std::unique_ptr< Attribute > >, int > _query_cache
Definition: TheWarehouse.h:324
std::mutex _obj_mutex
Definition: TheWarehouse.h:329
static void sort(typename std::vector< T > &vector)
Given a vector, sort using the getRequested/SuppliedItems sets.
Interface for sorting dependent vectors of objects.
std::vector< std::vector< MooseObject * > > _obj_cache
Definition: TheWarehouse.h:320

◆ query() [1/2]

Query TheWarehouse::query ( )
inline

◆ query() [2/2]

const std::vector< MooseObject * > & TheWarehouse::query ( int  query_id)
private

callers of this function must lock _obj_cache_mutex as long as a reference to the returned vector is being used.

Definition at line 190 of file TheWarehouse.C.

191 {
192  if (static_cast<size_t>(query_id) >= _obj_cache.size())
193  throw std::runtime_error("unknown query id");
194  return _obj_cache[query_id];
195 }
std::vector< std::vector< MooseObject * > > _obj_cache
Definition: TheWarehouse.h:320

◆ queryID()

size_t TheWarehouse::queryID ( const std::vector< std::unique_ptr< Attribute >> &  conds)
private

Definition at line 198 of file TheWarehouse.C.

Referenced by count(), and queryInto().

199 {
200  {
201  std::lock_guard<std::mutex> lock(_query_cache_mutex);
202  auto it = _query_cache.find(conds);
203  if (it != _query_cache.end())
204  return it->second;
205  }
206 
207  std::vector<std::unique_ptr<Attribute>> conds_clone;
208  conds_clone.resize(conds.size());
209  for (size_t i = 0; i < conds.size(); i++)
210  conds_clone[i] = conds[i]->clone();
211  return prepare(std::move(conds_clone));
212 }
std::mutex _query_cache_mutex
Definition: TheWarehouse.h:330
int prepare(std::vector< std::unique_ptr< Attribute >> conds)
prepares a query and returns an associated query_id (i.e. for use with the query function).
Definition: TheWarehouse.C:141
std::unordered_map< std::vector< std::unique_ptr< Attribute > >, int > _query_cache
Definition: TheWarehouse.h:324

◆ queryInto() [1/2]

template<typename T >
std::vector<T *>& TheWarehouse::queryInto ( const std::vector< std::unique_ptr< Attribute >> &  conds,
std::vector< T *> &  results 
)
inline

queryInto takes the given conditions (i.e.

Attributes holding the values to filter/match over) and filters all objects in the warehouse that match all conditions (i.e. "and"ing the conditions together) and stores them in the results vector. All result objects must be castable to the templated type T. This function filters out disabled objects on the fly - only returning enabled ones.

Definition at line 277 of file TheWarehouse.h.

Referenced by TheWarehouse::Query::queryInto().

279  {
280  return queryInto(queryID(conds), results);
281  }
size_t queryID(const std::vector< std::unique_ptr< Attribute >> &conds)
Definition: TheWarehouse.C:198
std::vector< T * > & queryInto(const std::vector< std::unique_ptr< Attribute >> &conds, std::vector< T *> &results)
queryInto takes the given conditions (i.e.
Definition: TheWarehouse.h:277

◆ queryInto() [2/2]

template<typename T >
std::vector<T *>& TheWarehouse::queryInto ( int  query_id,
std::vector< T *> &  results,
bool  show_all = false 
)
inlineprivate

Definition at line 287 of file TheWarehouse.h.

288  {
289  std::lock_guard<std::mutex> lock(_obj_cache_mutex);
290  auto & objs = query(query_id);
291  results.clear();
292  results.reserve(objs.size());
293  for (unsigned int i = 0; i < objs.size(); i++)
294  {
295  auto obj = objs[i];
296  mooseAssert(dynamic_cast<T *>(obj), "queried object has incompatible c++ type");
297  if (show_all || obj->enabled())
298  results.push_back(dynamic_cast<T *>(obj));
299  }
300  return results;
301  }
std::mutex _obj_cache_mutex
Definition: TheWarehouse.h:331
Query query()
query creates and returns an initialized a query object for querying objects from the warehouse...
Definition: TheWarehouse.h:266

◆ readAttribs()

void TheWarehouse::readAttribs ( const MooseObject obj,
const std::string &  system,
std::vector< std::unique_ptr< Attribute >> &  attribs 
)
private

Definition at line 228 of file TheWarehouse.C.

Referenced by add(), and update().

231 {
232  std::unique_ptr<AttribSystem> sys;
233  if (!system.empty())
234  sys.reset(new AttribSystem(*this, system));
235  for (auto & ref : _attrib_list)
236  {
237  if (sys && (ref->id() == sys->id()))
238  {
239  attribs.push_back(std::move(sys));
240  continue;
241  }
242 
243  attribs.emplace_back(ref->clone());
244  attribs.back()->initFrom(obj);
245  }
246 }
nl system()
std::vector< std::unique_ptr< Attribute > > _attrib_list
Definition: TheWarehouse.h:327

◆ registerAttribute()

template<typename T , typename... Args>
unsigned int TheWarehouse::registerAttribute ( const std::string &  name,
Args...  dummy_args 
)
inline

registers a new "tracked" attribute of type T for the warehouse.

args are all arguments necessary to create an instance of the T class excluding the warehouse reference/pointer which is assumed to be first and automatically inserted. An instance of every registered attribute will be created for and initialized to each object added to the warehouse allowing queries to be executed over specific values the attribute may take on. Attributes must be registered before objects are added to the warehouse. A unique ID associated with the registered attribute is returned - which is generally not needed used by users.

As an example, to register a class with the constructor "YourAttribute(TheWarehouse& w, int foo)", you would call "registerAttribute<YourAttribute>("your_attrib_name", constructor_arg1, ...)". Custom attribute classes are required to pass an attribute name (i.e. "your_attrib_name") to the Attribute base class. The dummy args are forwarded to the attrib class' constructor. The name passed here into registerAttribute must be the same string as the name passed to the Attribute base class's constructor.

Definition at line 230 of file TheWarehouse.h.

231  {
232  auto it = _attrib_ids.find(name);
233  if (it != _attrib_ids.end())
234  return it->second;
235 
236  _attrib_ids[name] = _attrib_list.size();
237  _attrib_list.push_back(std::unique_ptr<Attribute>(new T(*this, dummy_args...)));
238  return _attrib_list.size() - 1;
239  }
std::unordered_map< std::string, unsigned int > _attrib_ids
Definition: TheWarehouse.h:326
std::vector< std::unique_ptr< Attribute > > _attrib_list
Definition: TheWarehouse.h:327

◆ update() [1/2]

void TheWarehouse::update ( MooseObject obj)

update updates the metadata/attribute-info stored for the given object obj that must already exists in the warehouse.

Call this if an object's state has changed in such a way that its warehouse attributes have become stale/incorrect.

Definition at line 129 of file TheWarehouse.C.

Referenced by groupUserObjects().

130 {
131  std::vector<std::unique_ptr<Attribute>> attribs;
132  readAttribs(obj, "", attribs);
133  _store->set(_obj_ids[obj], std::move(attribs));
134  // reset/invalidate the query cache since query results may have been affected by this object
135  // attribute modification.
136  _obj_cache.clear();
137  _query_cache.clear();
138 }
std::unique_ptr< Storage > _store
Definition: TheWarehouse.h:314
std::unordered_map< std::vector< std::unique_ptr< Attribute > >, int > _query_cache
Definition: TheWarehouse.h:324
std::unordered_map< MooseObject *, size_t > _obj_ids
Definition: TheWarehouse.h:316
void readAttribs(const MooseObject *obj, const std::string &system, std::vector< std::unique_ptr< Attribute >> &attribs)
Definition: TheWarehouse.C:228
std::vector< std::vector< MooseObject * > > _obj_cache
Definition: TheWarehouse.h:320

◆ update() [2/2]

void TheWarehouse::update ( MooseObject obj,
const Attribute extra 
)

update updates the metadata/attribute-info stored for the given object obj that must already exists in the warehouse.

Call this if an object's state has changed in such a way that its warehouse attributes have become stale/incorrect. Any attribute specified in extra overwrites/trumps one read from the object's current state.

Definition at line 117 of file TheWarehouse.C.

118 {
119  std::vector<std::unique_ptr<Attribute>> attribs;
120  attribs.push_back(extra.clone());
121  _store->set(_obj_ids[obj], std::move(attribs));
122  // reset/invalidate the query cache since query results may have been affected by this object
123  // attribute modification.
124  _obj_cache.clear();
125  _query_cache.clear();
126 }
std::unique_ptr< Storage > _store
Definition: TheWarehouse.h:314
std::unordered_map< std::vector< std::unique_ptr< Attribute > >, int > _query_cache
Definition: TheWarehouse.h:324
std::unordered_map< MooseObject *, size_t > _obj_ids
Definition: TheWarehouse.h:316
std::vector< std::vector< MooseObject * > > _obj_cache
Definition: TheWarehouse.h:320
virtual std::unique_ptr< Attribute > clone() const =0
clone creates and returns and identical (deep) copy of this attribute - i.e.

Member Data Documentation

◆ _attrib_ids

std::unordered_map<std::string, unsigned int> TheWarehouse::_attrib_ids
private

Definition at line 326 of file TheWarehouse.h.

Referenced by attribID(), and registerAttribute().

◆ _attrib_list

std::vector<std::unique_ptr<Attribute> > TheWarehouse::_attrib_list
private

Definition at line 327 of file TheWarehouse.h.

Referenced by readAttribs(), and registerAttribute().

◆ _obj_cache

std::vector<std::vector<MooseObject *> > TheWarehouse::_obj_cache
private

Definition at line 320 of file TheWarehouse.h.

Referenced by add(), prepare(), query(), and update().

◆ _obj_cache_mutex

std::mutex TheWarehouse::_obj_cache_mutex
private

Definition at line 331 of file TheWarehouse.h.

Referenced by count(), prepare(), and queryInto().

◆ _obj_ids

std::unordered_map<MooseObject *, size_t> TheWarehouse::_obj_ids
private

Definition at line 316 of file TheWarehouse.h.

Referenced by add(), and update().

◆ _obj_mutex

std::mutex TheWarehouse::_obj_mutex
private

Definition at line 329 of file TheWarehouse.h.

Referenced by add(), and prepare().

◆ _objects

std::vector<std::shared_ptr<MooseObject> > TheWarehouse::_objects
private

Definition at line 315 of file TheWarehouse.h.

Referenced by add(), and prepare().

◆ _query_cache

std::unordered_map<std::vector<std::unique_ptr<Attribute> >, int> TheWarehouse::_query_cache
private

Definition at line 324 of file TheWarehouse.h.

Referenced by add(), prepare(), queryID(), and update().

◆ _query_cache_mutex

std::mutex TheWarehouse::_query_cache_mutex
private

Definition at line 330 of file TheWarehouse.h.

Referenced by prepare(), and queryID().

◆ _store

std::unique_ptr<Storage> TheWarehouse::_store
private

Definition at line 314 of file TheWarehouse.h.

Referenced by add(), prepare(), and update().


The documentation for this class was generated from the following files: