-
Notifications
You must be signed in to change notification settings - Fork 2
/
clusterer.h
263 lines (249 loc) · 7.02 KB
/
clusterer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/****
Active Object compiled file
Copyright (C) 2006-2011 Werner Van Belle
Do not modify. Changes might be lost
--------------------------------------------
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
****/
#ifndef __CLUSTERER_H
#define __CLUSTERER_H
#include "./Active/active-objects.h"
using namespace std;
#include "cluster.h"
#include "song-metric.h"
class Clusterer;
class ActiveClusterer;
#ifdef TRACE_MESSAGES
#define ENTER_MSG cerr << "Start " << declaration() << "\n";
#define LEAVE_MSG cerr << "Stop " << declaration() << "\n";
#endif
#ifndef ENTER_MSG
#define ENTER_MSG ;
#endif
#ifndef LEAVE_MSG
#define LEAVE_MSG ;
#endif
//-------------------------------------
// Active Object base messaging classes
//-------------------------------------
/**
* Represents the basic message that is used to queue or deliver a call to
* the true ActiveClusterer. ActiveClusterer_msg_ has a run method which is invoked
* by ActiveObject whenever it wants to handle the message. For each
* declared method in the active object description, a specific subclass
* of ActiveClusterer_msg_ has been generated. See inheritance diagram.
* The message classes are automatically instantiated by the active object
* stub Clusterer
*/
class ActiveClusterer_msg_
{
public:
/**
* Called by ActiveObject to handle this queued message.
* %arg caller is the ActiveClusterer itself.
*/
virtual elementResult run(ActiveClusterer * /* caller */)
{
assert(0);
return Revisit;
}
/**
* Returns the name of this message. Since this is the message baseclass
* it has no identity and will return 'Unknown Message'
*/
virtual string declaration()
{
return "Unknown message";
}
/**
* The following is necessary so that child desctructors are invoked
* when a message is deleted in the handle_message loop.
*/
virtual ~ActiveClusterer_msg_()
{
}
};
//-------------------------------------
// Main object definition
//-------------------------------------
class ActiveClusterer: public ActiveObject< ActiveClusterer_msg_* >
{
friend class Clusterer;
Clusterer * self;
virtual elementResult handle( ActiveClusterer_msg_* cmd)
{
if (cmd) return cmd->run(this);
else return Done;
};
PointSet tocluster;
public: elementResult reset();
protected: void queue_reset();
public: elementResult add(Point* p);
protected: void queue_add(Point* p);
Couple* agglomerate_allsteps(Metriek* metriek);
public: elementResult agglomerate(SongMetriek metriek);
protected: void queue_agglomerate(SongMetriek metriek);
public: elementResult terminate();
protected: void queue_terminate();
protected:
ActiveClusterer(Clusterer* s, string name):
ActiveObject< ActiveClusterer_msg_ * >(name), self(s)
{
};
};
//-------------------------------------
// Specific messaging classes
//-------------------------------------
class ActiveClusterer_msg_reset: public ActiveClusterer_msg_
{
;
public:
ActiveClusterer_msg_reset()
{
};
virtual string declaration()
{
return "Clusterer::reset()";
}
virtual elementResult run(ActiveClusterer * ao)
{
ENTER_MSG;
elementResult res = ao->reset();
LEAVE_MSG;
return res;
};
};
class ActiveClusterer_msg_add: public ActiveClusterer_msg_
{
Point* p;
public:
ActiveClusterer_msg_add(Point* p) : p(p)
{
};
virtual string declaration()
{
return "Clusterer::add(Point* p)";
}
virtual elementResult run(ActiveClusterer * ao)
{
ENTER_MSG;
elementResult res = ao->add(p);
LEAVE_MSG;
return res;
};
};
class ActiveClusterer_msg_agglomerate: public ActiveClusterer_msg_
{
SongMetriek metriek;
public:
ActiveClusterer_msg_agglomerate(SongMetriek metriek) : metriek(metriek)
{
};
virtual string declaration()
{
return "Clusterer::agglomerate(SongMetriek metriek)";
}
virtual elementResult run(ActiveClusterer * ao)
{
ENTER_MSG;
elementResult res = ao->agglomerate(metriek);
LEAVE_MSG;
return res;
};
};
class ActiveClusterer_msg_terminate: public ActiveClusterer_msg_
{
;
public:
ActiveClusterer_msg_terminate()
{
};
virtual string declaration()
{
return "Clusterer::terminate()";
}
virtual elementResult run(ActiveClusterer * ao)
{
ENTER_MSG;
elementResult res = ao->terminate();
LEAVE_MSG;
return res;
};
};
//-------------------------------------
// Active Object wrapper
//-------------------------------------
/**
* Represents the stub that will transform each incoming call (method)
* to an object subclassed from type ActiveClusterer_msg_
* The stub itself has an instance of the true active object.
*/
class Clusterer
{
private:
/**
* The object that is covered for by this stub. The fact that the
* stub allocates the object ensures that only one of it exists and that any
* interaction must go through the stub. As such it is kept private.
*/
ActiveClusterer object;
public:
/**
* The constructor of the stub will also directly initalize the main
* object. Because object construction and delayed calls interfere
* somewhat we prohibit the actual implementation (and especially use)
* of a specialized Active Object constructor. Instead, simply the name
* is passed to the object. If you need to initialize the stub, you
* should consider adding an init message to the active object and
* calling (well sending a message to) it directly when the stub is
* generated.
*/
Clusterer(string name="Clusterer"): object(this, name) {};
public:
void reset()
{
object.queue_reset();
};
public:
void add(Point* p)
{
object.queue_add(p);
};
public:
void agglomerate(SongMetriek metriek)
{
object.queue_agglomerate(metriek);
};
public:
void terminate()
{
object.queue_terminate();
};
};
//-------------------------------------
// Active Object Methods
//-------------------------------------
inline void ActiveClusterer::queue_reset()
{
push(new ActiveClusterer_msg_reset());
};
inline void ActiveClusterer::queue_add(Point* p)
{
push(new ActiveClusterer_msg_add(p));
};
inline void ActiveClusterer::queue_agglomerate(SongMetriek metriek)
{
push(new ActiveClusterer_msg_agglomerate(metriek));
};
inline void ActiveClusterer::queue_terminate()
{
push(new ActiveClusterer_msg_terminate());
};
#endif // __CLUSTERER_H