fpmas 1.6
graph.h
Go to the documentation of this file.
1#ifndef FPMAS_MODEL_SPATIAL_GRAPH_H
2#define FPMAS_MODEL_SPATIAL_GRAPH_H
3
10#include "spatial_model.h"
11
12namespace fpmas { namespace model {
13
25 friend nlohmann::adl_serializer<ReachableCell>;
27
28 protected:
35 std::set<DistributedId> reachable_cells;
36
37 public:
38
42 const std::set<DistributedId>& reachableCells() const {
43 return reachable_cells;
44 }
45
52 reachable_cells.insert(id);
53 }
54
61 reachable_cells.erase(id);
62 }
63
68 reachable_cells.clear();
69 }
70 };
71
82 template<typename GraphCellType, typename Derived = GraphCellType>
84 public CellBase<api::model::Cell, GraphCellType, GraphCellBase<GraphCellType, Derived>>,
85 public ReachableCell {
86 public:
91 };
92
96 struct GraphCell final : public GraphCellBase<GraphCell> {
98 };
99
126 /*
127 * TODO: How to avoid synchronize()?
128 * Event listeners might be added to GraphCells to automatically update the
129 * `reachable_cells` list when a GraphCell is linked / unlinked on the
130 * CELL_SUCCESSOR layer. However, such event handling is not yet supported
131 * by FPMAS 1.1.
132 */
133 template<typename GraphCellType = GraphCell>
134 class GraphRange : public api::model::Range<GraphCellType> {
135 static_assert(
136 std::is_base_of<api::model::Cell, GraphCellType>::value,
137 "The specified GraphCellType must implement api::model::Cell."
138 );
139 static_assert(
140 std::is_base_of<ReachableCell, GraphCellType>::value,
141 "The specified GraphCellType must extend ReachableCell"
142 );
143 private:
144 bool include_location;
145 public:
153 GraphRange(bool include_location = INCLUDE_LOCATION) : include_location(include_location) {
154 }
155
165 std::size_t radius(GraphCellType*) const override {
166 return 1;
167 }
168
179 bool contains(GraphCellType* root, GraphCellType* cell) const override {
180 if(include_location && root->node()->getId() == cell->node()->getId())
181 return true;
182 return root->reachableCells().count(cell->node()->getId()) > 0;
183 }
184
202 for(auto cell : model.cellGroup().localAgents()) {
203 auto graph_cell = dynamic_cast<ReachableCell*>(cell);
204 graph_cell->clearReachableCells();
205 for(auto neighbor : cell->node()->outNeighbors(api::model::CELL_SUCCESSOR))
206 graph_cell->addReachableCell(neighbor->getId());
207 }
208 model.graph().synchronize();
209 }
210
211 };
212}}
213
216
217namespace nlohmann {
221 template<>
222 struct adl_serializer<fpmas::model::ReachableCell> {
229 static void to_json(nlohmann::json& j, const fpmas::model::ReachableCell& cell);
236 static void from_json(const nlohmann::json& j, fpmas::model::ReachableCell& cell);
237 };
238
242 template<typename AgentType, typename Derived>
243 struct adl_serializer<fpmas::api::utils::PtrWrapper<fpmas::model::GraphCellBase<AgentType, Derived>>> {
263 static void to_json(nlohmann::json& j, const Ptr& ptr) {
264 // Derived serialization
266 const_cast<Derived*>(dynamic_cast<const Derived*>(ptr.get())));
267 j[1] = static_cast<const fpmas::model::ReachableCell&>(*ptr);
268 }
269
288 static Ptr from_json(const nlohmann::json& j) {
289 // Derived unserialization.
290 // The current base is implicitly default initialized
293 static_cast<fpmas::model::ReachableCell&>(*derived_ptr)
294 = j[1].get<fpmas::model::ReachableCell>();
295 //derived_ptr->reachable_nodes = j[1].get<std::set<fpmas::api::graph::DistributedId>>();
296 return derived_ptr.get();
297 }
298 };
299}
300
301namespace fpmas { namespace io { namespace json {
312 template<typename AgentType, typename Derived>
318
331 static void to_json(light_json& j, const Ptr& agent) {
332 // Derived serialization
334 j,
335 const_cast<Derived*>(dynamic_cast<const Derived*>(agent.get()))
336 );
337 }
338
351 static Ptr from_json(const light_json& j) {
352 // Derived unserialization.
353 // The current base is implicitly default initialized
354 PtrWrapper<Derived> derived_ptr
356 return derived_ptr.get();
357 }
358 };
359
360}}}
361
362namespace fpmas { namespace io { namespace datapack {
370 template<>
371 struct Serializer<model::ReachableCell> {
379 static std::size_t size(
380 const ObjectPack& p, const model::ReachableCell& cell);
381
388 static void to_datapack(
389 ObjectPack& pack, const model::ReachableCell& cell);
390
397 static model::ReachableCell from_datapack(
398 const ObjectPack& pack);
399 };
400
418 template<typename GraphCellType, typename Derived>
419 struct Serializer<PtrWrapper<fpmas::model::GraphCellBase<GraphCellType, Derived>>> {
424
429 static std::size_t size(const ObjectPack& p, const Ptr& ptr) {
430 return p.size(PtrWrapper<Derived>(
431 const_cast<Derived*>(dynamic_cast<const Derived*>(ptr.get()))))
432 + p.size(static_cast<const fpmas::model::ReachableCell&>(*ptr));
433 }
434
442 static void to_datapack(ObjectPack& pack, const Ptr& ptr) {
443 // Derived serialization
445 const_cast<Derived*>(dynamic_cast<const Derived*>(ptr.get())));
446 const fpmas::model::ReachableCell& reachable_cell
447 = static_cast<const fpmas::model::ReachableCell&>(*ptr);
448 pack.put(derived);
449 pack.put(reachable_cell);
450 }
451
468 static Ptr from_datapack(const ObjectPack& pack) {
469 // Derived unserialization.
470 // The current base is implicitly default initialized
471 PtrWrapper<Derived> derived_ptr = pack
473
474 // ReachableCell unserialization into derived_ptr
475 static_cast<fpmas::model::ReachableCell&>(*derived_ptr)
477
478 return derived_ptr.get();
479 }
480 };
481
482
502 template<typename GraphCellType, typename Derived>
503 struct LightSerializer<PtrWrapper<fpmas::model::GraphCellBase<GraphCellType, Derived>>> {
508
514 static std::size_t size(const LightObjectPack& pack, const Ptr& ptr) {
515 return pack.size(PtrWrapper<Derived>(
516 const_cast<Derived*>(dynamic_cast<const Derived*>(ptr.get()))
517 ));
518 }
519
532 static void to_datapack(LightObjectPack& pack, const Ptr& ptr) {
533 // Derived serialization
535 pack,
536 const_cast<Derived*>(dynamic_cast<const Derived*>(ptr.get()))
537 );
538 }
539
552 static Ptr from_datapack(const LightObjectPack& pack) {
553 // Derived unserialization.
554 // The current base is implicitly default initialized
555 PtrWrapper<Derived> derived_ptr
557 return derived_ptr.get();
558 }
559 };
560}}}
561#endif
Definition: distributed_id.h:89
virtual std::vector< Agent * > localAgents() const =0
virtual AgentGraph & graph()=0
Definition: spatial_model.h:121
Definition: spatial_model.h:479
virtual AgentGroup & cellGroup()=0
Definition: ptr_wrapper.h:21
T * get()
Definition: ptr_wrapper.h:58
Definition: datapack.h:301
std::size_t size() const
Definition: datapack.h:367
void put(const T &item)
Definition: datapack.h:447
T get() const
Definition: datapack.h:459
Definition: spatial_model.h:215
Definition: graph.h:85
GraphCellBase< GraphCellType, Derived > JsonBase
Definition: graph.h:90
Definition: graph.h:134
bool contains(GraphCellType *root, GraphCellType *cell) const override
Definition: graph.h:179
static void synchronize(api::model::SpatialModel< GraphCellType > &model)
Definition: graph.h:201
GraphRange(bool include_location=INCLUDE_LOCATION)
Definition: graph.h:153
std::size_t radius(GraphCellType *) const override
Definition: graph.h:165
Definition: graph.h:24
void addReachableCell(DistributedId id)
Definition: graph.h:51
std::set< DistributedId > reachable_cells
Definition: graph.h:35
void clearReachableCells()
Definition: graph.h:67
const std::set< DistributedId > & reachableCells() const
Definition: graph.h:42
void removeReachableCell(DistributedId id)
Definition: graph.h:60
#define FPMAS_DEFAULT_DATAPACK(AGENT)
Definition: datapack_serializer.h:154
#define FPMAS_DEFAULT_JSON(AGENT)
Definition: json_serializer.h:127
nlohmann::basic_json< std::map, std::vector, std::string, bool, std::int64_t, std::uint64_t, double, std::allocator, light_serializer > light_json
Definition: json.h:14
Definition: fpmas.cpp:3
static std::size_t size(const LightObjectPack &pack, const Ptr &ptr)
Definition: graph.h:514
PtrWrapper< fpmas::model::GraphCellBase< GraphCellType, Derived > > Ptr
Definition: graph.h:507
static void to_datapack(LightObjectPack &pack, const Ptr &ptr)
Definition: graph.h:532
Definition: datapack.h:1411
static void to_datapack(LightObjectPack &pack, const T &item)
Definition: datapack.h:1438
static T from_datapack(const LightObjectPack &pack)
Definition: datapack.h:1451
static void to_datapack(ObjectPack &pack, const Ptr &ptr)
Definition: graph.h:442
PtrWrapper< fpmas::model::GraphCellBase< GraphCellType, Derived > > Ptr
Definition: graph.h:423
static std::size_t size(const ObjectPack &p, const Ptr &ptr)
Definition: graph.h:429
Definition: datapack.h:55
PtrWrapper< fpmas::model::GraphCellBase< AgentType, Derived > > Ptr
Definition: graph.h:317
static void to_json(light_json &j, const Ptr &agent)
Definition: graph.h:331
static void from_json(const light_json &j, T &data)
Definition: json.h:123
static void to_json(light_json &j, const T &data)
Definition: json.h:107
Definition: graph.h:96
fpmas::api::utils::PtrWrapper< fpmas::model::GraphCellBase< AgentType, Derived > > Ptr
Definition: graph.h:247