RepastHPC  2.3.1
AgentRequest.h
1 /*
2  * Repast for High Performance Computing (Repast HPC)
3  *
4  * Copyright (c) 2010 Argonne National Laboratory
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with
8  * or without modification, are permitted provided that the following
9  * conditions are met:
10  *
11  * Redistributions of source code must retain the above copyright notice,
12  * this list of conditions and the following disclaimer.
13  *
14  * Redistributions in binary form must reproduce the above copyright notice,
15  * this list of conditions and the following disclaimer in the documentation
16  * and/or other materials provided with the distribution.
17  *
18  * Neither the name of the Argonne National Laboratory nor the names of its
19  * contributors may be used to endorse or promote products derived from
20  * this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
25  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE TRUSTEES OR
26  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
32  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  *
34  * AgentRequest.h
35  *
36  * Created on: Aug 19, 2010
37  * Author: nick
38  */
39 
40 #ifndef AGENTREQUEST_H_
41 #define AGENTREQUEST_H_
42 
43 #include <iostream>
44 #include <boost/serialization/access.hpp>
45 #include <boost/serialization/vector.hpp>
46 #include <set>
47 
48 #include "AgentId.h"
49 
50 
51 namespace repast {
52 
59 class AgentRequest {
60 
61  friend std::ostream& operator<<(std::ostream& os, const AgentRequest& request);
62  friend class boost::serialization::access;
63 
64  // To allow AgentImporters to edit agent requests before sending them out
65  friend class Importer_LIST;
66  friend class Importer_SET;
67  friend class Importer_MAP_int;
68 
69 private:
70  int source, target;
71  std::vector<AgentId> requestedAgents_;
72  std::vector<AgentId> cancellations_;
73 
74  template<class Archive>
75  void serialize(Archive& ar, const unsigned int version) {
76  ar & source;
77  ar & target;
78  ar & requestedAgents_;
79  ar & cancellations_;
80  }
81 
82 public:
83  // No-arg constructor for serialization
84  AgentRequest() :
85  source(-1), target(-1) {
86  }
87 
94 
104 
111  void addAll(const AgentRequest& req);
112 
120  void addAllRequests(const AgentRequest& req);
121 
129  void addAllCancellations(const AgentRequest& req);
130 
136  const std::vector<AgentId>& requestedAgents() const {
137  return requestedAgents_;
138  }
139 
146  const std::vector<AgentId>& cancellations() const {
147  return cancellations_;
148  }
149 
162  bool remove(const AgentId& id, bool removeAllInstances = true);
163 
176  bool removeRequest(const AgentId& id, bool removeAllInstances = true);
177 
190  bool removeCancellation(const AgentId& id, bool removeAllInstances = true);
191 
198  void targets(std::set<int>& targets);
199 
206  void targetsOfRequests(std::set<int>& targets);
207 
214  void targetsOfCancellations(std::set<int>& targets);
215 
222  void addRequest(const AgentId& id);
223 
231  void addCancellation(const AgentId& id);
232 
243  int requestCount() const {
244  return requestedAgents_.size() + cancellations_.size();
245  }
246 
253  int requestCountRequested() const {
254  return requestedAgents_.size();
255  }
256 
264  return cancellations_.size();
265  }
266 
277  bool contains(const AgentId& id);
278 
287  bool containsInRequests(const AgentId& id);
288 
297  bool containsInCancellations(const AgentId& id);
298 
305  int sourceProcess() const {
306  return source;
307  }
308 
314  int targetProcess() const {
315  return target;
316  }
317 
318 };
319 
323 std::ostream& operator<<(std::ostream& os, const AgentRequest& request);
324 
325 
326 }
327 
328 #endif /* AGENTREQUEST_H_ */
repast::AgentRequest::addAll
void addAll(const AgentRequest &req)
Adds all the agent ids (both requests and cancellations) in req to this AgentRequest.
Definition: AgentRequest.cpp:112
repast::AgentRequest::requestCountCancellations
int requestCountCancellations() const
Gets the number of agents requested, counting only the cancellations and not the requests.
Definition: AgentRequest.h:263
repast::AgentRequest::requestCount
int requestCount() const
Gets the number agents requested.
Definition: AgentRequest.h:243
repast::AgentRequest::addAllRequests
void addAllRequests(const AgentRequest &req)
Adds all the agent ids in req to this request, including only the ids that are requests and not those...
Definition: AgentRequest.cpp:117
repast::AgentId
Agent identity information.
Definition: AgentId.h:60
repast::AgentRequest::targetsOfRequests
void targetsOfRequests(std::set< int > &targets)
Puts the targets of all the requests into the set, including only the requests and not the cancellati...
Definition: AgentRequest.cpp:130
repast::Importer_MAP_int
Importer that maintains a map of agents being sent from each sending process and a count of the numbe...
Definition: AgentImporterExporter.h:510
repast::AgentRequest::requestCountRequested
int requestCountRequested() const
Gets the number of agents requested, counting only the requests and not the cancellations.
Definition: AgentRequest.h:253
repast::AgentRequest::addAllCancellations
void addAllCancellations(const AgentRequest &req)
Adds all the agent ids in req to this request, including only the ids that are cancellations and not ...
Definition: AgentRequest.cpp:121
repast::AgentRequest::targetsOfCancellations
void targetsOfCancellations(std::set< int > &targets)
Puts the targets of all the requests into the set, including only the requests and not the cancellati...
Definition: AgentRequest.cpp:138
repast::AgentRequest::operator<<
friend std::ostream & operator<<(std::ostream &os, const AgentRequest &request)
Prints the specified AgentRequest to the specified ostream.
Definition: AgentRequest.cpp:147
repast::Importer_SET
Importer that maintains a set of agents being sent from each sending process.
Definition: AgentImporterExporter.h:458
repast::AgentRequest::removeCancellation
bool removeCancellation(const AgentId &id, bool removeAllInstances=true)
Definition: AgentRequest.cpp:98
repast::AgentRequest
Encapsulates a request made by one process for agents in another.
Definition: AgentRequest.h:59
repast::Importer_LIST
Importer that maintains a list of the agents being sent from each sending process.
Definition: AgentImporterExporter.h:400
repast::AgentRequest::addRequest
void addRequest(const AgentId &id)
Adds the specified agent to the collection agents being requested.
Definition: AgentRequest.cpp:56
repast::AgentRequest::remove
bool remove(const AgentId &id, bool removeAllInstances=true)
Removes the specified id from the lists of requested agents, including both requests and cancellation...
Definition: AgentRequest.cpp:76
repast::AgentRequest::targetProcess
int targetProcess() const
If the requested agent ids are all on the same process then target process will identify that process...
Definition: AgentRequest.h:314
repast::AgentRequest::cancellations
const std::vector< AgentId > & cancellations() const
Gets a reference to the vector of cancellations.
Definition: AgentRequest.h:146
repast::AgentRequest::removeRequest
bool removeRequest(const AgentId &id, bool removeAllInstances=true)
Definition: AgentRequest.cpp:83
repast::AgentRequest::contains
bool contains(const AgentId &id)
Returns true if this AgentRequest contains a request for the specified id (either a request or a canc...
Definition: AgentRequest.cpp:64
repast::AgentRequest::containsInRequests
bool containsInRequests(const AgentId &id)
Returns true if the list of requests contains the specified id (the list of cancellations is ignored)
Definition: AgentRequest.cpp:68
repast::AgentRequest::containsInCancellations
bool containsInCancellations(const AgentId &id)
Returns true if the list of cancellations contains the specified id (the list of requests is ignored)
Definition: AgentRequest.cpp:72
repast::AgentRequest::sourceProcess
int sourceProcess() const
Gets the source process of these requests, that is, the process making the request.
Definition: AgentRequest.h:305
repast::AgentRequest::requestedAgents
const std::vector< AgentId > & requestedAgents() const
Gets a reference to the vector of requested agents.
Definition: AgentRequest.h:136
repast::AgentRequest::addCancellation
void addCancellation(const AgentId &id)
Adds the specified agent to the collection of agents for which a previous request is being cancelled.
Definition: AgentRequest.cpp:60
repast::AgentRequest::targets
void targets(std::set< int > &targets)
Puts the targets of all the requests into the set.
Definition: AgentRequest.cpp:125