libmonaa  0.5.2
constraint.hh
1 #pragma once
2 
3 #include <algorithm>
4 #include <cstdint>
5 #include <ostream>
6 #include <vector>
7 
8 #include "common_types.hh"
9 
11 enum class Order { LT, EQ, GT };
12 
13 inline bool toBool(Order odr) { return odr == Order::EQ; }
14 
16 struct Constraint {
17  enum class Order { lt, le, ge, gt };
18 
19  ClockVariables x;
20  Order odr;
21  int c;
22 
23  bool satisfy(double d) const {
24  switch (odr) {
25  case Order::lt:
26  return d < c;
27  case Order::le:
28  return d <= c;
29  case Order::gt:
30  return d > c;
31  case Order::ge:
32  return d >= c;
33  }
34  return false;
35  }
36  using Interpretation = std::vector<double>;
37  ::Order operator()(Interpretation val) const {
38  if (satisfy(val.at(x))) {
39  return ::Order::EQ;
40  } else if (odr == Order::lt || odr == Order::le) {
41  return ::Order::GT;
42  } else {
43  return ::Order::LT;
44  }
45  }
46 };
47 
48 static inline std::ostream &operator<<(std::ostream &os,
49  const Constraint::Order &odr) {
50  switch (odr) {
51  case Constraint::Order::lt:
52  os << "<";
53  break;
54  case Constraint::Order::le:
55  os << "<=";
56  break;
57  case Constraint::Order::ge:
58  os << ">=";
59  break;
60  case Constraint::Order::gt:
61  os << ">";
62  break;
63  }
64  return os;
65 }
66 
67 static inline std::ostream &operator<<(std::ostream &os, const Constraint &p) {
68  os << "x" << int(p.x) << " " << p.odr << " " << p.c;
69  return os;
70 }
71 
72 // An interface to write an inequality constrait easily
74  ClockVariables x;
75 
76 public:
77  ConstraintMaker(ClockVariables x) : x(x) {}
78  Constraint operator<(int c) {
79  return Constraint{x, Constraint::Order::lt, c};
80  }
81  Constraint operator<=(int c) {
82  return Constraint{x, Constraint::Order::le, c};
83  }
84  Constraint operator>(int c) {
85  return Constraint{x, Constraint::Order::gt, c};
86  }
87  Constraint operator>=(int c) {
88  return Constraint{x, Constraint::Order::ge, c};
89  }
90 };
91 
95 static inline void widen(std::vector<Constraint> &guard) {
96  guard.erase(std::remove_if(guard.begin(), guard.end(),
97  [](Constraint g) {
98  return g.odr == Constraint::Order::ge ||
99  g.odr == Constraint::Order::gt;
100  }),
101  guard.end());
102 }
Definition: constraint.hh:73
A constraint in a guard of transitions.
Definition: constraint.hh:16