www.mooseframework.org
DependencyResolverInterface.h
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
3 //*
4 //* All rights reserved, see COPYRIGHT for full restrictions
5 //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6 //*
7 //* Licensed under LGPL 2.1, please see LICENSE for details
8 //* https://www.gnu.org/licenses/lgpl-2.1.html
9 
10 #pragma once
11 
12 // STL includes
13 #include <string>
14 #include <set>
15 #include <iostream>
16 #include <algorithm>
17 
18 // MOOSE includes
19 #include "DependencyResolver.h"
20 
25 {
26 public:
31 
35  virtual const std::set<std::string> & getRequestedItems() = 0;
36 
40  virtual const std::set<std::string> & getSuppliedItems() = 0;
41 
45  template <typename T>
46  static void sort(typename std::vector<T> & vector);
47 
51  template <typename T>
52  static void cyclicDependencyError(CyclicDependencyException<T> & e, const std::string & header);
53 };
54 
55 template <typename T>
56 void
57 DependencyResolverInterface::sort(typename std::vector<T> & vector)
58 {
59  DependencyResolver<T> resolver;
60 
61  typename std::vector<T>::iterator start = vector.begin();
62  typename std::vector<T>::iterator end = vector.end();
63 
64  for (typename std::vector<T>::iterator iter = start; iter != end; ++iter)
65  {
66  const std::set<std::string> & requested_items = (*iter)->getRequestedItems();
67 
68  for (typename std::vector<T>::iterator iter2 = start; iter2 != end; ++iter2)
69  {
70  if (iter == iter2)
71  continue;
72 
73  const std::set<std::string> & supplied_items = (*iter2)->getSuppliedItems();
74 
75  std::set<std::string> intersect;
76  std::set_intersection(requested_items.begin(),
77  requested_items.end(),
78  supplied_items.begin(),
79  supplied_items.end(),
80  std::inserter(intersect, intersect.end()));
81 
82  // If the intersection isn't empty then there is a dependency here
83  if (!intersect.empty())
84  resolver.insertDependency(*iter, *iter2);
85  }
86  }
87 
88  // Sort based on dependencies
89  std::stable_sort(start, end, resolver);
90 }
91 
92 template <typename T>
93 void
95  const std::string & header)
96 {
97  std::ostringstream oss;
98 
99  oss << header << ":\n";
100  const typename std::multimap<T, T> & depends = e.getCyclicDependencies();
101  for (typename std::multimap<T, T>::const_iterator it = depends.begin(); it != depends.end(); ++it)
102  oss << (static_cast<T>(it->first))->name() << " -> " << (static_cast<T>(it->second))->name()
103  << "\n";
104  mooseError(oss.str());
105 }
106 
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:207
virtual const std::set< std::string > & getRequestedItems()=0
Return a set containing the names of items requested by the object.
const std::multimap< T, T > & getCyclicDependencies() const
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.
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"...