RepastHPC  2.3.1
Projection.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  *
35  * Projection.h
36  *
37  * Created on: Aug 5, 2010
38  * Author: nick
39  */
40 
41 #ifndef PROJECTION_H_
42 #define PROJECTION_H_
43 
44 #include <sstream>
45 #include <string>
46 #include <set>
47 #include <map>
48 #include <boost/shared_ptr.hpp>
49 #include <boost/noncopyable.hpp>
50 #include <boost/serialization/serialization.hpp>
51 #include <boost/serialization/export.hpp>
52 #include <boost/serialization/set.hpp>
53 
54 #include "AgentId.h"
55 
56 namespace repast {
57 
58 
65  friend class boost::serialization::access;
66 
67 public:
68  ProjectionInfoPacket(){} // For serialization
69  ProjectionInfoPacket(AgentId agentId): id(agentId){}
70  virtual ~ProjectionInfoPacket(){}
71 
72  template<class Archive>
73  void serialize(Archive& ar, const unsigned int version) {
74  ar & id;
75  }
76 
77  AgentId id;
78 
79  virtual bool isEmpty(){ return false; }
80 
81 };
82 
83 BOOST_SERIALIZATION_ASSUME_ABSTRACT(ProjectionInfoPacket);
84 
89 template<typename Datum>
91  friend class boost::serialization::access;
92 
93 public:
94 
95  SpecializedProjectionInfoPacket(){} // For serialization
97  SpecializedProjectionInfoPacket(AgentId agentId, std::vector<Datum> projectionData): ProjectionInfoPacket(agentId){
98  data.assign(projectionData.begin(), projectionData.end());
99  }
100  SpecializedProjectionInfoPacket(AgentId agentId, std::set<Datum> projectionData): ProjectionInfoPacket(agentId){
101  data.assign(projectionData.begin(), projectionData.end());
102  }
103 
105 
106  template<class Archive>
107  void serialize(Archive& ar, const unsigned int version) {
108  ar & boost::serialization::base_object<ProjectionInfoPacket>(*this);
109  ar & data;
110  }
111 
112  std::vector<Datum> data;
113 
114  virtual bool isEmpty(){ return (data.size() == 0); }
115 
116 };
117 
118 template<typename T>
119 class Context;
120 
124 template<typename T>
125 class Projection : public boost::noncopyable {
126 
127  friend class Context<T> ;
128 
129 protected:
130 
131  std::string name_;
132  virtual bool addAgent(boost::shared_ptr<T> agent) = 0;
133  virtual void removeAgent(T* agent) = 0;
134 
135 
136  // Beta (Protected)
137  std::set<int> filter;
138 
139 
140  virtual ProjectionInfoPacket* getProjectionInfo(AgentId id, bool secondaryInfo = false, std::set<AgentId>* secondaryIds = 0, int destProc = -1 ) = 0;
141 
142  virtual void updateProjectionInfo(ProjectionInfoPacket* pip, Context<T>* context) = 0;
143 
144 public:
145 
146  enum RADIUS{ PRIMARY, SECONDARY };
147 
154  Projection(std::string name) :
155  name_(name) {
156  }
157 
158  virtual ~Projection() {
159  }
160 
164  const std::string name() const {
165  return name_;
166  }
167 
168  // Beta (public)
169 
177  void addFilterVal(int type){
178  filter.insert(type);
179  }
180 
188  void removeFilterVal(int type){
189  filter.erase(type);
190  }
191 
196  void clearFilter(){
197  filter.clear();
198  }
199 
207  bool agentCanBeAdded(boost::shared_ptr<T> agent){
208  return ( (filter.size() == 0) ||
209  (filter.find(agent->getId().agentType()) != filter.end()));
210  }
211 
230  virtual bool keepsAgentsOnSyncProj() = 0;
231 
241  virtual bool sendsSecondaryAgentsOnStatusExchange() = 0;
242 
253  virtual void getInfoExchangePartners(std::set<int>& psToSendTo, std::set<int>& psToReceiveFrom) = 0;
254 
266  virtual void getAgentStatusExchangePartners(std::set<int>& psToSendTo, std::set<int>& psToReceiveFrom) = 0;
267 
273  virtual void getRequiredAgents(std::set<AgentId>& agentsToTest, std::set<AgentId>& agentsRequired, RADIUS radius = PRIMARY) = 0;
274 
281  virtual void getAgentsToPush(std::set<AgentId>& agentsToTest, std::map<int, std::set<AgentId> >& agentsToPush) = 0;
282 
283  // Note: Virtual because some child classes may be able to short-circuit this (like Graphs)
288  virtual void getProjectionInfo(std::vector<AgentId>& agents, std::vector<ProjectionInfoPacket*>& packets,
289  bool secondaryInfo = false, std::set<AgentId>* secondaryIds = 0, int destProc = -1);
290 
295  void updateProjectionInfo(std::vector<ProjectionInfoPacket*>& pips, Context<T>* context);
296 
297  virtual void cleanProjectionInfo(std::set<AgentId>& agentsToKeep) = 0;
298 
299  virtual void balance(){};
300 
301 };
302 
303 
304 template<typename T>
305 void Projection<T>::getProjectionInfo(std::vector<AgentId>& agents, std::vector<ProjectionInfoPacket*>& packets,
306  bool secondaryInfo, std::set<AgentId>* secondaryIds, int destProc){
307  for(std::vector<AgentId>::const_iterator iter = agents.begin(), iterEnd = agents.end(); iter != iterEnd; iter++){
308  ProjectionInfoPacket* packet = getProjectionInfo((*iter), secondaryInfo, secondaryIds, destProc);
309  if((packet != 0) && (!packet->isEmpty())) packets.push_back(packet);
310  }
311 }
312 
313 template<typename T>
314 void Projection<T>::updateProjectionInfo(std::vector<ProjectionInfoPacket*>& pips, Context<T>* context){
315  for(std::vector<ProjectionInfoPacket*>::const_iterator pipIter = pips.begin(), pipIterEnd = pips.end(); pipIter != pipIterEnd; pipIter++){
316  updateProjectionInfo(*pipIter, context);
317  }
318 }
319 
320 }
321 
322 #endif /* PROJECTION_H_ */
repast::Context
Collection of agents of type T with set semantics.
Definition: Context.h:82
repast::Projection::removeFilterVal
void removeFilterVal(int type)
Removes an entry from the list of agent types that can be added to this projection.
Definition: Projection.h:188
repast::ProjectionInfoPacket
Serializable packet that can contain projection information regardless of the type of projection (net...
Definition: Projection.h:64
repast::Projection::keepsAgentsOnSyncProj
virtual bool keepsAgentsOnSyncProj()=0
Should return true if the Projection implemented can 'keep' some (non-local) agents during a projecti...
repast::Projection::clearFilter
void clearFilter()
Clears the list of agent types that can be added to this projection; the result is that the filter is...
Definition: Projection.h:196
repast::AgentId
Agent identity information.
Definition: AgentId.h:60
repast::Projection::getAgentStatusExchangePartners
virtual void getAgentStatusExchangePartners(std::set< int > &psToSendTo, std::set< int > &psToReceiveFrom)=0
Gets the set of processes with which this Projection exchanges agent status info- that is,...
repast::Projection
Abstract base class for all Projections.
Definition: Projection.h:125
repast::Projection::sendsSecondaryAgentsOnStatusExchange
virtual bool sendsSecondaryAgentsOnStatusExchange()=0
Should return true if the Projection implemented will send secondary agents during a status exchange.
repast::SpecializedProjectionInfoPacket
Serializable packet that can contain projection information of a specific kind using the template par...
Definition: Projection.h:90
repast::Projection::getAgentsToPush
virtual void getAgentsToPush(std::set< AgentId > &agentsToTest, std::map< int, std::set< AgentId > > &agentsToPush)=0
Given a set of agents, gets the agents that this projection implementation must 'push' to other proce...
repast::Projection::Projection
Projection(std::string name)
Creates a projection with specified name.
Definition: Projection.h:154
repast::Projection::getInfoExchangePartners
virtual void getInfoExchangePartners(std::set< int > &psToSendTo, std::set< int > &psToReceiveFrom)=0
Gets the set of processes with which this Projection exchanges projection info.
repast::Projection::getRequiredAgents
virtual void getRequiredAgents(std::set< AgentId > &agentsToTest, std::set< AgentId > &agentsRequired, RADIUS radius=PRIMARY)=0
Given a set of agents to test, gets the subset that must be kept in order to fulfill the projection's...
repast::Projection::name
const std::string name() const
Gets the name of this projection.
Definition: Projection.h:164
repast::Projection::addFilterVal
void addFilterVal(int type)
Adds an entry to the list of agent types that can be added to this projection.
Definition: Projection.h:177
repast::Projection::agentCanBeAdded
bool agentCanBeAdded(boost::shared_ptr< T > agent)
Returns true if the agent can be added to the projection, which will be the case if the filter list i...
Definition: Projection.h:207