www.mooseframework.org
DependencyResolverInterface.h
Go to the documentation of this file.
1 /****************************************************************/
2 /* DO NOT MODIFY THIS HEADER */
3 /* MOOSE - Multiphysics Object Oriented Simulation Environment */
4 /* */
5 /* (c) 2010 Battelle Energy Alliance, LLC */
6 /* ALL RIGHTS RESERVED */
7 /* */
8 /* Prepared by Battelle Energy Alliance, LLC */
9 /* Under Contract No. DE-AC07-05ID14517 */
10 /* With the U. S. Department of Energy */
11 /* */
12 /* See COPYRIGHT for full restrictions */
13 /****************************************************************/
14 
15 #ifndef DEPENDENCYRESOLVERINTERFACE_H
16 #define DEPENDENCYRESOLVERINTERFACE_H
17 
18 // STL includes
19 #include <string>
20 #include <set>
21 #include <iostream>
22 #include <algorithm>
23 
24 // MOOSE includes
25 #include "DependencyResolver.h"
26 
31 {
32 public:
37 
41  virtual const std::set<std::string> & getRequestedItems() = 0;
42 
46  virtual const std::set<std::string> & getSuppliedItems() = 0;
47 
51  template <typename T>
52  static void sort(typename std::vector<T> & vector);
53 
57  template <typename T>
58  static void cyclicDependencyError(CyclicDependencyException<T> & e, const std::string & header);
59 };
60 
61 template <typename T>
62 void
63 DependencyResolverInterface::sort(typename std::vector<T> & vector)
64 {
65  DependencyResolver<T> resolver;
66 
67  typename std::vector<T>::iterator start = vector.begin();
68  typename std::vector<T>::iterator end = vector.end();
69 
70  for (typename std::vector<T>::iterator iter = start; iter != end; ++iter)
71  {
72  const std::set<std::string> & requested_items = (*iter)->getRequestedItems();
73 
74  for (typename std::vector<T>::iterator iter2 = start; iter2 != end; ++iter2)
75  {
76  if (iter == iter2)
77  continue;
78 
79  const std::set<std::string> & supplied_items = (*iter2)->getSuppliedItems();
80 
81  std::set<std::string> intersect;
82  std::set_intersection(requested_items.begin(),
83  requested_items.end(),
84  supplied_items.begin(),
85  supplied_items.end(),
86  std::inserter(intersect, intersect.end()));
87 
88  // If the intersection isn't empty then there is a dependency here
89  if (!intersect.empty())
90  resolver.insertDependency(*iter, *iter2);
91  }
92  }
93 
94  // Sort based on dependencies
95  std::stable_sort(start, end, resolver);
96 }
97 
98 template <typename T>
99 void
101  const std::string & header)
102 {
103  std::ostringstream oss;
104 
105  oss << header << ":\n";
106  const typename std::multimap<T, T> & depends = e.getCyclicDependencies();
107  for (typename std::multimap<T, T>::const_iterator it = depends.begin(); it != depends.end(); ++it)
108  oss << (static_cast<T>(it->first))->name() << " -> " << (static_cast<T>(it->second))->name()
109  << "\n";
110  mooseError(oss.str());
111 }
112 
113 #endif // DEPENDENCYRESOLVERINTERFACE_H
void mooseError(Args &&...args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:182
virtual const std::set< std::string > & getRequestedItems()=0
Return a set containing the names of items requested by the object.
static void cyclicDependencyError(CyclicDependencyException< T > &e, const std::string &header)
A helper method for cyclic errors.
virtual const std::set< std::string > & getSuppliedItems()=0
Return a set containing the names of items owned by the object.
static void sort(typename std::vector< T > &vector)
Given a vector, sort using the getRequested/SuppliedItems sets.
const std::multimap< T, T > & getCyclicDependencies() const
Interface for sorting dependent vectors of objects.
void insertDependency(const T &key, const T &value)
Insert a dependency pair - the first value or the "key" depends on the second value or the "value"...