fipa_acl  1.4
Public Member Functions | List of all members
fipa::acl::RoleMapping Class Reference

Mapping between roles and actual agent ids. More...

#include <role.h>

Public Member Functions

 RoleMapping ()
 
const std::map< Role,
AgentIDList > & 
getMapping () const
 
void setSelf (const AgentID &id)
 
bool isSelf (const AgentID &id) const
 
AgentID getSelf () const
 
void addRole (const Role &role)
 
void addExpectedAgent (const Role &role, const AgentID &agent)
 
void clearExpectedAgents (const Role &role)
 
void resetExpectedAgents ()
 
const AgentIDListgetExpectedAgents (const Role &role) const
 
bool isExpected (const Role &role, const AgentID &agent) const
 
std::string toString () const
 

Detailed Description

Mapping between roles and actual agent ids.

Definition at line 98 of file role.h.

Constructor & Destructor Documentation

fipa::acl::RoleMapping::RoleMapping ( )

Default constructor

Definition at line 36 of file role.cpp.

37 {
38  addRole(SelfRole());
39  addExpectedAgent(SelfRole(), UndefinedAgentID());
40 }
void addRole(const Role &role)
Definition: role.cpp:78
void addExpectedAgent(const Role &role, const AgentID &agent)
Definition: role.cpp:83

Member Function Documentation

void fipa::acl::RoleMapping::addExpectedAgent ( const Role role,
const AgentID agent 
)

Add an agent to the list of expected agents for a specific role

Definition at line 83 of file role.cpp.

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 }
std::vector< AgentID > AgentIDList
Definition: agent_id.h:20
void fipa::acl::RoleMapping::addRole ( const Role role)

Add a role to this rolemapping

Definition at line 78 of file role.cpp.

79 {
80  mExpectedAgentMapping.insert( std::pair<Role, AgentIDList>(role, AgentIDList()));
81 }
std::vector< AgentID > AgentIDList
Definition: agent_id.h:20
void fipa::acl::RoleMapping::clearExpectedAgents ( const Role role)

Clear list of expected agents for a specific role

Definition at line 103 of file role.cpp.

104 {
105  std::map<Role, AgentIDList>::iterator it = mExpectedAgentMapping.find(role);
106  if(it != mExpectedAgentMapping.end())
107  {
108  it->second.clear();
109  }
110 }
const AgentIDList & fipa::acl::RoleMapping::getExpectedAgents ( const Role role) const

Get the list of expected agents for a given role

Exceptions
std::runtime_errorif the role does not exist

Definition at line 153 of file role.cpp.

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 }
const std::map<Role, AgentIDList>& fipa::acl::RoleMapping::getMapping ( ) const
inline

Returns the mapping

Definition at line 114 of file role.h.

114 { return mExpectedAgentMapping; }
AgentID fipa::acl::RoleMapping::getSelf ( ) const

Retrieve the AgentID of self

Definition at line 59 of file role.cpp.

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 }
bool fipa::acl::RoleMapping::isExpected ( const Role role,
const AgentID agent 
) const

Check whether the given agent belongs to the list of expected receivers

Exceptions
Ifrole is completely unknown to this role mapping

Definition at line 120 of file role.cpp.

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 }
const AgentIDList & getExpectedAgents(const Role &role) const
Definition: role.cpp:153
std::vector< AgentID > AgentIDList
Definition: agent_id.h:20
bool fipa::acl::RoleMapping::isSelf ( const AgentID id) const

Test whether an agent id represents 'self'

Definition at line 73 of file role.cpp.

74 {
75  return getSelf() == id;
76 }
AgentID getSelf() const
Definition: role.cpp:59
void fipa::acl::RoleMapping::resetExpectedAgents ( )

Clear list of expected agents for all known roles

Note that the self role remains with will be reset to UndefinedAgentID

Definition at line 112 of file role.cpp.

113 {
114  mExpectedAgentMapping.clear();
115 
116  addRole(SelfRole());
117  addExpectedAgent(SelfRole(), UndefinedAgentID());
118 }
void addRole(const Role &role)
Definition: role.cpp:78
void addExpectedAgent(const Role &role, const AgentID &agent)
Definition: role.cpp:83
void fipa::acl::RoleMapping::setSelf ( const AgentID id)

Add the role 'self'

Definition at line 42 of file role.cpp.

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 }
void addExpectedAgent(const Role &role, const AgentID &agent)
Definition: role.cpp:83
std::string fipa::acl::RoleMapping::toString ( ) const

Stringify role mapping

Definition at line 165 of file role.cpp.

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 }
std::vector< AgentID > AgentIDList
Definition: agent_id.h:20

The documentation for this class was generated from the following files: