TuringSim
C++ framework to simulate abstract computing models
mConfigurationMap.h
1 #pragma once
2 
3 #include <utils/printer.h>
4 #include <state/mConfiguration/mConfiguration.h>
5 
7  template<typename NodeType, typename V>
8  class MConfigurationMap;
9 
15  template <typename T, typename V>
17  {
18  public:
22 
30  std::string message,
31  const MConfiguration<T>& key,
32  const V& value,
34  MessageException(message),
35  key(key),
36  value(value),
37  map(map)
38  {}
39 
40  protected:
41  virtual std::string makeFullMessage() const override {
42  using Utils::Debug::operator<<;
43  std::ostringstream ss;
44  ss << this->msg << std::endl;
45  ss << " pattern: " << key << std::endl;
46  ss << " state: " << value << std::endl;
47  ss << " map: " << map.debug() << std::endl;
48  return ss.str();
49  }
50 
54 
57  V value;
58 
62  };
63 
69  template<typename NodeType, typename V>
71  public:
76 
80  typedef typename MConf::Unification Unification;
81 
84  MConfigurationMap() : pattern_map() {}
85 
90  void insert(const MConf& key, const V& val) {
91  if(key.isVariable()) {
92  throw PureVariableException<NodeType, V>("Cannot add a pure variable in a MConfigurationMap", key, val, *this);
93  }
94  const NodeType& root = key.getNode();
95  size_t arity = key.getArity();
96  typename std::map<std::pair<NodeType, int>, std::map<MConf, std::set<V>>>::iterator it = pattern_map.find({root, arity});
97  if(it == pattern_map.end()) {
98  pattern_map[{root, arity}] = {{key, {val}}};
99  }
100  else if (it->second.count(key) > 0) {
101  it->second[key].insert(val);
102  }
103  else {
104  it->second.insert({key, {val}});
105  }
106  }
107 
112  const std::map<MConf, std::set<V>>& getCandidates(const MConf& state) const {
113  const NodeType& root = state.getNode();
114  size_t arity = state.getArity();
115  return pattern_map.at({root, arity});
116  }
117 
122  std::map<MConf, std::set<std::pair<V, Unification>>> getMatching(const std::shared_ptr<const MConf>& state) const {
123  const NodeType& root = state->getNode();
124  size_t arity = state->getArity();
125  typename std::map<std::pair<NodeType, int>, std::map<MConf, std::set<V>>>::const_iterator it = pattern_map.find({root, arity});
126  if(it == pattern_map.end()) {
127  return std::map<MConf, std::set<std::pair<V, Unification>>>();
128  }
129  const std::map<MConf, std::set<V>>& candidates = it->second;
130 
131  std::map<MConf, std::set<std::pair<V, Unification>>> matching_map;
132  for(const auto& [pattern, values] : candidates) {
133  for(const V& val: values) {
134  std::optional<Unification> unification_opt = pattern.unifyWithMConf_opt(state);
135  if(unification_opt) {
136  matching_map[pattern].insert({val, *unification_opt});
137  }
138  }
139  }
140  return matching_map;
141  }
142 
148  template<typename CharT = char, typename Traits = std::char_traits<CharT>>
149  std::function<std::basic_ostream<CharT, Traits>&(std::basic_ostream<CharT, Traits>&)> debug() const {
150  return Utils::Debug::debug<decltype(pattern_map), CharT, Traits>(pattern_map);
151  }
152 
153  private:
154  std::map<
155  std::pair<NodeType, int>, // Root node of the mConfiguration, the arity
156  std::map<MConf, std::set<V>>
157  > pattern_map;
158  };
159 }
TuringSim::Utils::MessageException::MessageException
MessageException()=delete
a message exception must have an explicative message.
TuringSim::State::MConfiguration::MConfigurationMap::insert
void insert(const MConf &key, const V &val)
Add a new binding in the map.
Definition: mConfigurationMap.h:90
TuringSim::State::MConfiguration::MConfigurationMap< T, V >
TuringSim::Utils::MessageException::msg
std::string msg
The error message.
Definition: messageException.h:78
TuringSim::State::MConfiguration::MConfiguration< T >
TuringSim::State::MConfiguration::PureVariableException::PureVariableException
PureVariableException()=delete
Deleted default constructor: a PureVariable must contains interesting information.
TuringSim::Utils::MessageException
The Base class for all custom exceptions.
Definition: messageException.h:12
TuringSim::State::MConfiguration::MConfigurationMap::MConfigurationMap
MConfigurationMap()
Builds an empty MConfigurationMap.
Definition: mConfigurationMap.h:84
TuringSim::State::MConfiguration::MConfigurationMap::getMatching
std::map< MConf, std::set< std::pair< V, Unification > > > getMatching(const std::shared_ptr< const MConf > &state) const
Gets all bindings with successful unification.
Definition: mConfigurationMap.h:122
TuringSim::State::MConfiguration::PureVariableException::makeFullMessage
virtual std::string makeFullMessage() const override
Build the full error message. It should be overridden by derived class that adds data to the exceptio...
Definition: mConfigurationMap.h:41
TuringSim::State::MConfiguration::MConfigurationMap::MConf
MConfiguration< NodeType > MConf
The type of keys.
Definition: mConfigurationMap.h:75
TuringSim::State::MConfiguration::PureVariableException::map
const MConfigurationMap< T, V > & map
the map in which the exception was raised.
Definition: mConfigurationMap.h:61
TuringSim::State::MConfiguration::PureVariableException::key
MConfiguration< T > key
Definition: mConfigurationMap.h:53
TuringSim::State::MConfiguration::PureVariableException::value
V value
The value we tried to associate with the key.
Definition: mConfigurationMap.h:57
TuringSim::State::MConfiguration::PureVariableException::PureVariableException
PureVariableException(std::string message, const MConfiguration< T > &key, const V &value, const MConfigurationMap< T, V > &map)
Builds a PureVariableException.
Definition: mConfigurationMap.h:29
TuringSim::State::MConfiguration::MConfigurationMap::getCandidates
const std::map< MConf, std::set< V > > & getCandidates(const MConf &state) const
Gets all binding with same arity and root as state.
Definition: mConfigurationMap.h:112
TuringSim::State::MConfiguration::MConfigurationMap::Unification
MConf::Unification Unification
Type of unifications used when looking for matching states.
Definition: mConfigurationMap.h:80
TuringSim::State::MConfiguration::PureVariableException
Exception thrown when trying to add a pure variable in a MConfigurationMap.
Definition: mConfigurationMap.h:17
TuringSim::State::MConfiguration::MConfiguration::getNode
constexpr const NodeType & getNode() const
Returns the content of the node.
Definition: mConfiguration.h:630
TuringSim::State::MConfiguration::MConfiguration::Unification
std::map< NodeType, std::shared_ptr< const MConfiguration< NodeType > > > Unification
The unification type between a pattern and a m-configuration.
Definition: mConfiguration.h:30
TuringSim::State::MConfiguration::MConfiguration::isVariable
constexpr bool isVariable() const
Returns whether the node is a variable.
Definition: mConfiguration.h:645
TuringSim::State::MConfiguration::MConfiguration::getArity
constexpr size_t getArity() const
Returns the arity of the node.
Definition: mConfiguration.h:635
TuringSim::State::MConfiguration
The namespace of everything about m-configurations.
TuringSim::State::MConfiguration::MConfigurationMap::debug
std::function< std::basic_ostream< CharT, Traits > &(std::basic_ostream< CharT, Traits > &)> debug() const
Debug printer.
Definition: mConfigurationMap.h:149