fipa_acl  1.4
role.cpp
Go to the documentation of this file.
1 #include "role.h"
2 #include <stdexcept>
3 #include <cassert>
4 #include <boost/regex.hpp>
5 #include <base-logging/Logging.hpp>
6 
7 namespace fipa {
8 namespace acl {
9 
10 const RoleId Role::UNDEFINED_ID = "__undefined__";
11 const RoleId Role::SELF_ID ="__self__";
12 
14  : mId(Role::UNDEFINED_ID)
15 {
16 }
17 
18 Role::Role(const RoleId& id)
19  : mId(id)
20 {
21 }
22 
23 Role::Role(const Role& other)
24 {
25  mId = other.mId;
26 }
27 
30 {}
31 
33  : Role(Role::SELF_ID)
34 {}
35 
37 {
38  addRole(SelfRole());
39  addExpectedAgent(SelfRole(), UndefinedAgentID());
40 }
41 
43 {
44  std::map<Role, AgentIDList>::iterator it = mExpectedAgentMapping.find(SelfRole());
45  // Internal error if AgentID for 'self' is not set
46  assert(!it->second.empty());
47 
48  if(it->second.front() == UndefinedAgentID())
49  {
50  it->second.clear();
51  addExpectedAgent(SelfRole(), id);
52  } else {
53  std::string msg = "Self is already set";
54  LOG_WARN_S << msg;
55  throw std::runtime_error(msg);
56  }
57 }
58 
60 {
61  std::map<Role, AgentIDList>::const_iterator it = mExpectedAgentMapping.find(SelfRole());
62  if(it != mExpectedAgentMapping.end())
63  {
64  return it->second.front();
65  } else {
66  // AgentID has never been set -- should never happen
67  assert(false);
68  }
69 
70  return UndefinedAgentID();
71 }
72 
73 bool RoleMapping::isSelf(const AgentID& id) const
74 {
75  return getSelf() == id;
76 }
77 
78 void RoleMapping::addRole(const Role& role)
79 {
80  mExpectedAgentMapping.insert( std::pair<Role, AgentIDList>(role, AgentIDList()));
81 }
82 
83 void RoleMapping::addExpectedAgent(const Role& role, const AgentID& agent)
84 {
85  std::map<Role, AgentIDList>::iterator it = mExpectedAgentMapping.find(role);
86  if(it == mExpectedAgentMapping.end())
87  {
88  std::string msg = "Unexpected role '" + role.toString() + "'.";
89  LOG_ERROR("%s", msg.c_str());
90  throw std::runtime_error(msg);
91  }
92 
93  AgentIDList& expectedAgents = it->second;
94  AgentIDList::iterator eit = std::find(expectedAgents.begin(), expectedAgents.end(), agent);
95  if(eit == expectedAgents.end())
96  {
97  expectedAgents.push_back(agent);
98  }
99 
100  return;
101 }
102 
104 {
105  std::map<Role, AgentIDList>::iterator it = mExpectedAgentMapping.find(role);
106  if(it != mExpectedAgentMapping.end())
107  {
108  it->second.clear();
109  }
110 }
111 
113 {
114  mExpectedAgentMapping.clear();
115 
116  addRole(SelfRole());
117  addExpectedAgent(SelfRole(), UndefinedAgentID());
118 }
119 
120 bool RoleMapping::isExpected(const Role& role, const AgentID& agent) const
121 {
122  // Check first if role regex matches agent name
123  // afterwards try to resolve roles
124  boost::regex roleRegex(role.getId());
125  if(regex_match(agent.getName(), roleRegex))
126  {
127  return true;
128  }
129 
130  const AgentIDList& expectedAgents = getExpectedAgents(role);
131 
132  bool expected = false;
133  // Indication of an unassigned role -- this should be valid
134  if(expectedAgents.empty())
135  {
136  return true;
137  }
138 
139  // Agent has to match against one in the list
140  AgentIDList::const_iterator eit = expectedAgents.begin();
141  for(; eit != expectedAgents.end(); ++eit)
142  {
143  boost::regex regex(eit->getName() + "$");
144  if(regex_match(agent.getName(), regex))
145  {
146  expected = true;
147  }
148  }
149 
150  return expected;
151 }
152 
154 {
155  std::map<Role, AgentIDList>::const_iterator it = mExpectedAgentMapping.find(role);
156  if(it == mExpectedAgentMapping.end())
157  {
158  std::string msg = "Unexpected role '" + role.toString() + "'.";
159  LOG_ERROR("%s", msg.c_str());
160  throw std::runtime_error(msg);
161  }
162  return it->second;
163 }
164 
165 std::string RoleMapping::toString() const
166 {
167  std::stringstream ss;
168  std::map<Role, AgentIDList>::const_iterator cit = mExpectedAgentMapping.begin();
169  for(; cit != mExpectedAgentMapping.end(); ++cit)
170  {
171  ss << "'" << cit->first.toString() << "': ";
172  AgentIDList list = cit->second;
173  ss << list;
174  }
175  return ss.str();
176 }
177 
178 }
179 } // end namespace fipa::acl
std::string RoleId
Definition: role.h:11
void clearExpectedAgents(const Role &role)
Definition: role.cpp:103
std::string toString() const
Definition: role.cpp:165
void setSelf(const AgentID &id)
Definition: role.cpp:42
const AgentIDList & getExpectedAgents(const Role &role) const
Definition: role.cpp:153
const std::string & getName() const
setter and getter methods for all fields; they do not result in deep-copies assignments/retreivals, but this can be easily changed if needed through the overloaded operator which do
Definition: agent_id.h:90
bool isSelf(const AgentID &id) const
Definition: role.cpp:73
std::vector< AgentID > AgentIDList
Definition: agent_id.h:20
void resetExpectedAgents()
Definition: role.cpp:112
AgentID getSelf() const
Definition: role.cpp:59
void addRole(const Role &role)
Definition: role.cpp:78
RoleId getId() const
Definition: role.h:52
static const RoleId UNDEFINED_ID
Definition: role.h:25
static const RoleId SELF_ID
Definition: role.h:26
void addExpectedAgent(const Role &role, const AgentID &agent)
Definition: role.cpp:83
bool isExpected(const Role &role, const AgentID &agent) const
Definition: role.cpp:120
Implements the general AgentID functionality, which is present throughout the fipa specifications(FIP...
Definition: agent_id.h:37
Foundation of Physical Intelligent Agents.
Definition: conversation.cpp:6
std::string toString() const
Definition: role.h:58