Skip to content

Commit 320b4a0

Browse files
authored
Merge pull request #15 from YJack0000/docs/uml
2 parents 08281ba + 8aaedbd commit 320b4a0

File tree

2 files changed

+210
-0
lines changed

2 files changed

+210
-0
lines changed

doc/uml/core.puml

+142
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
@startuml
2+
skinparam classAttributeIconSize 0
3+
4+
package core {
5+
6+
7+
8+
abstract EnvironmentObject {
9+
-boost::uuids::uuid id
10+
-std::pair<float, float> position
11+
12+
+EnvironmentObject(float x, float y)
13+
+boost::uuids::uuid getId() const
14+
+{abstract} ~EnvironmentObject() = default
15+
+{abstract} void postIteration()
16+
+{abstract} std::pair<float, float> getPosition() const
17+
+{abstract} void setPosition(float x, float y)
18+
}
19+
20+
21+
enum FoodState {
22+
FRESH
23+
EATEN
24+
}
25+
26+
class Food implements EnvironmentObject {
27+
- FoodState state
28+
29+
+ Food()
30+
+ bool canBeEaten()
31+
+ void eaten()
32+
+ int getEnergy() const
33+
}
34+
35+
Food --> FoodState: is at state
36+
37+
38+
39+
class Environment {
40+
-int width
41+
-int height
42+
-std::string type
43+
-std::unique_ptr<ISpatialIndex<boost::uuids::uuid>> spatialIndex
44+
-std::unordered_map<boost::uuids::uuid, std::shared_ptr<EnvironmentObject>> objectsMapper
45+
-std::vector<std::shared_ptr<Organism>> deadOrganisms
46+
-unsigned long foodConsumption
47+
48+
+Environment(int width, int height, std::string type = "default")
49+
+int getWidth() const
50+
+int getHeight() const
51+
+void add(const std::shared_ptr<Organism>& organism, float x, float y)
52+
+void add(const std::shared_ptr<Food>& food, float x, float y)
53+
+void remove(const std::shared_ptr<Organism>& organism)
54+
+void remove(const std::shared_ptr<Food>& food)
55+
+void reset()
56+
+void simulateIteration(int, std::function<void(const Environment&)> on_each_iteration = nullptr)
57+
58+
+std::vector<std::shared_ptr<Organism>> getAllOrganisms() const
59+
+std::vector<std::shared_ptr<Food>> getAllFoods() const
60+
+std::vector<std::shared_ptr<EnvironmentObject>> getAllObjects() const
61+
+std::vector<std::shared_ptr<Organism>> getDeadOrganisms() const
62+
+unsigned long getFoodConsumptionInIteration() const
63+
64+
-void checkBounds(float x, float y) const
65+
-void updatePositionsInSpatialIndex()
66+
-void handleInteractions()
67+
-void postIteration()
68+
-void cleanUp()
69+
-void removeDeadOrganisms()
70+
}
71+
72+
class Genes {
73+
- char dna[4]
74+
- MutationFunction mutationLogic
75+
76+
+ Genes(const char *dnaStr)
77+
+ Genes(const char *dnaStr, MutationFunction customMutationLogic)
78+
+ void mutate()
79+
+ char getDNA(int index) const
80+
+ static void defaultMutationLogic(char dna[4])
81+
}
82+
83+
class MutationFunction <std::function<void(char[4])>> {
84+
}
85+
86+
Genes +-- "{public}" MutationFunction: inner using alias
87+
88+
89+
90+
class Organism implements EnvironmentObject {
91+
- Genes genes
92+
- LifeConsumptionCalculator lifeConsumptionCalculator
93+
- float lifeSpan
94+
- std::pair<float, float> movement
95+
- int reactionCounter = 0
96+
97+
+ Organism()
98+
+ Organism(const Genes &genes)
99+
+ Organism(const Genes &genes, LifeConsumptionCalculator lifeConsumptionCalculator)
100+
+ float getSpeed() const
101+
+ float getSize() const
102+
+ float getAwareness() const
103+
+ float getLifeConsumption() const
104+
+ float getLifeSpan() const
105+
+ float getReactionRadius() const
106+
+ void killed()
107+
+ bool isAlive() const
108+
+ bool canReproduce() const
109+
+ void react(std::vector<std::shared_ptr<EnvironmentObject>> &reactableObjects)
110+
+ void interact(std::vector<std::shared_ptr<EnvironmentObject>> &interactableObjects)
111+
+ std::shared_ptr<Organism> reproduce()
112+
+ void postIteration() override
113+
- double calculateDistance(std::shared_ptr<EnvironmentObject> object)
114+
- void makeMove()
115+
116+
}
117+
118+
class LifeConsumptionCalculator <std::function<uint32_t(const Organism &)>>{
119+
}
120+
121+
Organism +-- "{public}" LifeConsumptionCalculator: inner using alias
122+
123+
124+
125+
126+
127+
Environment [uuid] o-down-> "0..*" EnvironmentObject: contain
128+
Organism o-- Genes
129+
Environment o-- "{dead}" Organism
130+
Environment --> "0..*" Food : add,remove,get
131+
Environment --> "0..*" Organism: add,remove,get
132+
133+
Organism --> EnvironmentObject: interact and react with
134+
Organism --> Organism: reproduce
135+
136+
}
137+
138+
139+
140+
141+
142+
@enduml

doc/uml/index.puml

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
@startuml
2+
skinparam classAttributeIconSize 0
3+
interface ISpatialIndex<T> {
4+
+ {abstract} void insert(const T& object, float x, float y)
5+
+ {abstract} std::vector<T> query(float x, float y, float range)
6+
+ {abstract} void update(const T& object, float newX, float newY)
7+
+ {abstract} void remove(const T& object)
8+
+ {abstract} void clear()
9+
}
10+
11+
class SpatialObject<T> {
12+
- T object
13+
- std::pair<float, float> position
14+
15+
+ SpatialObject(const T& object, float x, float y)
16+
+ T getObject() const
17+
+ std::pair<float, float> getPosition() const
18+
+ void setPosition(float x, float y)
19+
}
20+
21+
class DefaultSpatialIndex<T> extends ISpatialIndex<T> {
22+
- std::vector<SpatialObject<T>> spatialObjects
23+
- typename std::vector<SpatialObject<T>>::iterator findObject(const T& object)
24+
25+
+ DefaultSpatialIndex()
26+
+ void insert(const T& object, float x, float y)
27+
+ std::vector<T> query(float x, float y, float range)
28+
+ void update(const T& object, float newX, float newY)
29+
+ void remove(const T& object)
30+
+ void clear()
31+
}
32+
33+
class OptimizedSpatialIndex<T> extends ISpatialIndex<T> {
34+
- float size
35+
- bool isSubdivided
36+
- std::unique_ptr<OptimizedSpatialIndex<T>> children[4]
37+
- std::pair<float, float> offset
38+
- {static} const int MAX_OBJECTS
39+
- {static} const int MIN_SIZE
40+
41+
+ OptimizedSpatialIndex(float size)
42+
+ void insert(const T& object, float x, float y)
43+
+ std::vector<T> query(float x, float y, float range)
44+
+ void update(const T& object, float newX, float newY)
45+
+ void remove(const T& object)
46+
+ void clear()
47+
+ ~OptimizedSpatialIndex() override = default
48+
+ std::vector<std::shared_ptr<SpatialObject<T>>> spatialObjects
49+
50+
- void _query(float x, float y, float range, std::vector<T>& result)
51+
- bool inBounds(const std::pair<float, float>& pos) const
52+
- void setOffset(float offsetX, float offsetY)
53+
- void subdivide()
54+
- bool canMerge() const
55+
- void merge()
56+
- bool isEmpty() const
57+
- bool intersectsRange(float min_x, float max_x, float min_y, float max_y) const
58+
- int getChildIndex(float x, float y) const
59+
- float getDistance(float x1, float y1, float x2, float y2) const
60+
}
61+
62+
DefaultSpatialIndex --> "0..*" SpatialObject
63+
64+
OptimizedSpatialIndex --> "0..4" OptimizedSpatialIndex: children
65+
OptimizedSpatialIndex --> "0..*" SpatialObject
66+
67+
68+
@enduml

0 commit comments

Comments
 (0)