RepastHPC  2.3.1
Edge.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  * Edge.h
36  *
37  * Created on: Jun 4, 2009
38  * Author: nick
39  */
40 
41 #ifndef EDGE_H_
42 #define EDGE_H_
43 
44 #include "AgentId.h"
45 #include "Context.h"
46 #include "RepastProcess.h"
47 
48 #include <boost/serialization/access.hpp>
49 
50 namespace repast {
51 
57 template<typename V>
58 class RepastEdge {
59 
60 public:
61  enum MASTER_NODE{ DEFAULT, SOURCE, TARGET };
62 
63 private:
64  double _weight;
65  V* _source, *_target;
66  bool _useTargetAsMaster;
67  bool _conflicted;
68 
69  bool defaultTarget(int sourceRank, int targetRank, MASTER_NODE useTargetAsMaster){
70  int rank = repast::RepastProcess::instance()->rank();
71  return (useTargetAsMaster == SOURCE ? false :
72  useTargetAsMaster == TARGET ? true :
73  ((_source->getId().currentRank() != rank) &&
74  (_target->getId().currentRank() == rank)));
75  }
76 
77 public:
78 
79  // no arg constructor for serialization
80  RepastEdge() : _weight(1), _source(0), _target(0), _useTargetAsMaster(false), _conflicted(false){ }
81  ~RepastEdge(){ }
82 
90  RepastEdge(V* source, V* target, MASTER_NODE useTargetAsMaster = DEFAULT);
91 
100  RepastEdge(V* source, V* target, double weight, MASTER_NODE useTargetAsMaster = DEFAULT);
101 
109  RepastEdge(boost::shared_ptr<V> source, boost::shared_ptr<V> target, MASTER_NODE useTargetAsMaster = DEFAULT);
110 
119  RepastEdge(boost::shared_ptr<V> source, boost::shared_ptr<V> target, double weight, MASTER_NODE useTargetAsMaster = DEFAULT);
120 
124  RepastEdge(const RepastEdge& edge);
125 
131  V* source() const {
132  return _source;
133  }
134 
140  V* target() const {
141  return _target;
142  }
143 
144  // sets the target. NON USER API
145  void target(V* target) {
146  _target = target;
147  }
148 
149  // sets the source. NON USER API
150  void source(V* source) {
151  _source = source;
152  }
153 
159  double weight() const {
160  return _weight;
161  }
162 
163  void weight(double wt){
164  _weight = wt;
165  }
166 
167  bool usesTargetAsMaster(){ return _useTargetAsMaster; }
168 
169  void markConflicted(){ _conflicted = true; }
170  void clearConflicted(){ _conflicted = false; }
171  bool isConflicted(){ return _conflicted; }
172 
173 };
174 
175 template<typename V>
176 RepastEdge<V>::RepastEdge(boost::shared_ptr<V> source, boost::shared_ptr<V> target, MASTER_NODE useTargetAsMaster) :
177  _source(source.get()), _target(target.get()), _weight(1), _conflicted(false) {
178  _useTargetAsMaster = defaultTarget(_source->getId().currentRank(), _target->getId().currentRank(), useTargetAsMaster);
179 }
180 
181 template<typename V>
182 RepastEdge<V>::RepastEdge(V* source, V* target, MASTER_NODE useTargetAsMaster) :
183  _source(source), _target(target), _weight(1), _conflicted(false){
184  _useTargetAsMaster = defaultTarget(_source->getId().currentRank(), _target->getId().currentRank(), useTargetAsMaster);
185 }
186 
187 template<typename V>
188 RepastEdge<V>::RepastEdge(V* source, V* target, double weight, MASTER_NODE useTargetAsMaster) :
189  _source(source), _target(target), _weight(weight), _conflicted(false){
190  _useTargetAsMaster = defaultTarget(_source->getId().currentRank(), _target->getId().currentRank(), useTargetAsMaster);
191 }
192 
193 template<typename V>
194 RepastEdge<V>::RepastEdge(boost::shared_ptr<V> source, boost::shared_ptr<V> target, double weight, MASTER_NODE useTargetAsMaster) :
195  _source(source.get()), _target(target.get()), _weight(weight), _conflicted(false){
196  _useTargetAsMaster = defaultTarget(_source->getId().currentRank(), _target->getId().currentRank(), useTargetAsMaster);
197 }
198 
199 template<typename V>
201  _source(edge._source), _target(edge._target), _weight(edge._weight),
202  _useTargetAsMaster(edge._useTargetAsMaster), _conflicted(edge._conflicted) { }
203 
204 template<typename V>
205 std::ostream& operator<<(std::ostream& os, const RepastEdge<V>& edge) {
206  os << (*edge.source()) << (edge._useTargetAsMaster ? "" : "(M)") << " -- " << (*edge.target() << (edge._useTargetAsMaster ? "(M)" : ""));
207  return os;
208 }
209 
210 
211 
218 template<typename V>
220 
221  friend class boost::serialization::access;
222 
223  template<class Archive>
224  void serialize(Archive& ar, const unsigned int version) {
225  ar & usesTargetAsMaster;
226  ar & weight;
227  ar & source;
228  ar & target;
229  }
230 
231  AgentId source;
232  AgentId target;
233  double weight;
234  bool usesTargetAsMaster;
235 
236  RepastEdgeContent(){} // For serialization
237 
239  source(edge->source()->getId()),
240  target(edge->target()->getId()),
241  weight(edge->weight()),
242  usesTargetAsMaster(edge->usesTargetAsMaster()){}
243 };
244 
245 
252 template<typename V>
254 
255 public:
257  virtual ~RepastEdgeContentManager(){}
258 
259  RepastEdge<V>* createEdge(RepastEdgeContent<V>& content, Context<V>* context){
260  return new RepastEdge<V>(context->getAgent(content.source), context->getAgent(content.target), content.weight,
261  (content.usesTargetAsMaster ? repast::RepastEdge<V>::TARGET : repast::RepastEdge<V>::SOURCE) );
262  }
263 
264  RepastEdgeContent<V>* provideEdgeContent(RepastEdge<V>* edge){
265  return new RepastEdgeContent<V>(edge);
266  }
267 
268 };
269 
270 }
271 
272 #endif /* EDGE_H_ */
repast::Context
Collection of agents of type T with set semantics.
Definition: Context.h:82
repast::RepastProcess::rank
int rank() const
Gets the rank of this process.
Definition: RepastProcess.h:378
repast::RepastEdgeContentManager
Class for creating RepastEdges from RepastEdgeContent, and vice versa.
Definition: Edge.h:253
repast::AgentId
Agent identity information.
Definition: AgentId.h:60
repast::RepastEdge::target
V * target() const
Gets the target of this RepastEdge.
Definition: Edge.h:140
repast::Context::getAgent
T * getAgent(const AgentId &id)
Gets the specified agent.
Definition: Context.h:861
repast::RepastEdge::weight
double weight() const
Gets the weight of this RepastEdge.
Definition: Edge.h:159
repast::RepastEdge::source
V * source() const
Gets the source of this RepastEdge.
Definition: Edge.h:131
repast::RepastProcess::instance
static RepastProcess * instance()
Gets this RepastProcess.
Definition: RepastProcess.cpp:126
repast::RepastEdgeContent
Serializable; also, does not include agent content, only agent IDs.
Definition: Edge.h:219
repast::RepastEdge
Default graph / network edge implementation.
Definition: Edge.h:58