4 #include "../datastructures/maybe.hpp"
5 #include "../util/util.hpp"
13 #include <unordered_map>
19 class TraitViolatedError :
public std::runtime_error {
21 explicit TraitViolatedError(
const std::string & what);
22 explicit TraitViolatedError(
const char * what);
25 class TraitUnfulfilledError :
public std::runtime_error {
27 explicit TraitUnfulfilledError(
const std::string & what);
28 explicit TraitUnfulfilledError(
const char * what);
45 static const unsigned long NO_LAGS;
46 static const unsigned long LAGS_ONLY_SUCCESSORS;
47 static const unsigned long LAGS_ONLY_GREATER_DURATION;
48 static const unsigned long LAGS_ONLY_POSITIVE;
49 static const unsigned long LAGS_DAG;
51 static const unsigned long COMMON_RELEASE;
52 static const unsigned long COMMON_DEADLINE;
53 static const unsigned long COMMON_DURATION;
55 static const unsigned long CONSISTENT_WINDOWS;
57 static const unsigned long DUMMY_START_END;
59 static const unsigned long NO_DRAIN;
61 static const unsigned long NO_WINDOW_EXTENSION;
62 static const unsigned long WINDOW_EXTENSION_JOBS_UNLIMITED;
64 static const unsigned long FLAT_AVAILABILITY;
65 static const unsigned long ZERO_AVAILABILITY;
67 static const unsigned long FLAT_OVERSHOOT;
71 static const char *
const FLAG_NAMES[];
73 Traits(
unsigned long flags,
unsigned int max_resources,
74 std::set<double> allowed_overshoot_exponents,
75 std::set<double> allowed_investment_exponents);
78 bool fulfills(
const Traits & requirements)
const;
79 bool has_flag(
unsigned long flag)
const;
81 void add_flag(
unsigned long flag);
82 void remove_flag(
unsigned long flag);
88 template <
class _CharT,
class _Traits>
89 friend std::basic_ostream<_CharT, _Traits> &
90 operator<<(std::basic_ostream<_CharT, _Traits> & stream,
91 const Traits & traits);
95 unsigned int max_resources;
97 std::set<double> allowed_overshoot_exponents;
98 std::set<double> allowed_investment_exponents;
100 static const std::vector<std::pair<unsigned long, unsigned long>>
105 const unsigned long ALL_TRAIT_FLAGS[] = {
107 Traits::LAGS_ONLY_SUCCESSORS,
108 Traits::LAGS_ONLY_GREATER_DURATION,
109 Traits::LAGS_ONLY_POSITIVE,
111 Traits::COMMON_RELEASE,
112 Traits::COMMON_DEADLINE,
113 Traits::COMMON_DURATION,
114 Traits::DUMMY_START_END,
116 Traits::CONSISTENT_WINDOWS,
117 Traits::NO_WINDOW_EXTENSION,
118 Traits::WINDOW_EXTENSION_JOBS_UNLIMITED,
119 Traits::FLAT_AVAILABILITY,
120 Traits::ZERO_AVAILABILITY,
121 Traits::FLAT_OVERSHOOT};
123 class TraitsBuilder {
125 explicit TraitsBuilder(
const Instance & instance);
130 void check_no_lags();
131 void check_lag_durations();
132 void check_lag_dag();
133 void check_no_drain();
134 void check_deadline_release();
135 void check_consistent_windows();
136 void check_dummy_start_end();
137 void check_window_extension();
138 void check_availabilities();
139 void check_overshoot();
147 static const unsigned short WANT_MAYBE;
148 static const unsigned short WANT_YES;
149 static const unsigned short WANT_NO;
151 using trait_profile = std::array<bool, get_array_size(ALL_TRAIT_FLAGS)>;
152 using transform_profile =
153 std::array<
unsigned short, get_array_size(ALL_TRAIT_FLAGS)>;
155 explicit TraitsRouter(
const std::set<Transformer *> & transformers);
156 Maybe<std::vector<Transformer *>> get_path(
const Traits & from,
160 const std::set<Transformer *> & transformers;
162 std::vector<std::pair<transform_profile, Transformer *>> in_edges;
163 std::unordered_map<trait_profile, std::pair<Transformer *, trait_profile>>
165 std::deque<trait_profile> queue;
166 trait_profile final_profile;
169 void build_in_edges();
170 void do_bfs(
const Traits & from,
const Traits & to);
171 bool match(
const trait_profile & traits_p,
172 const transform_profile & trans_p)
const;
173 bool fulfills(
const trait_profile & profile,
174 const trait_profile & pattern)
const;
175 std::vector<Transformer *> find_matching(
const trait_profile & profile)
const;
176 trait_profile transform_flags(
const trait_profile & in_profile,
177 const transform_profile & transform)
const;
178 trait_profile traits_to_profile(
const Traits & traits)
const;
187 constexpr
unsigned int
188 flag_to_index(
unsigned long flag)
190 return (
unsigned int)clog2(flag);
193 template <
class _CharT,
class _Traits>
194 std::basic_ostream<_CharT, _Traits> &
195 operator<<(std::basic_ostream<_CharT, _Traits> & stream,
const Traits & traits)
197 stream <<
"Traits[ ";
200 for (
auto flag : ALL_TRAIT_FLAGS) {
201 if (traits.has_flag(flag)) {
205 stream << Traits::FLAG_NAMES[flag_to_index(flag)];
210 stream <<
" / " << traits.max_resources <<
" / {";
213 for (
auto exponent : traits.allowed_overshoot_exponents) {
224 for (
auto exponent : traits.allowed_investment_exponents) {