File: EntityAggregator.m

package info (click to toggle)
paje.app 1.98-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,428 kB
  • sloc: objc: 24,517; ansic: 6,998; makefile: 134; sh: 42; java: 31
file content (613 lines) | stat: -rw-r--r-- 15,406 bytes parent folder | download | duplicates (3)
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
#include "EntityAggregator.h"
#include "../General/Macros.h"



@interface EventAggregator : EntityAggregator
@end

@interface StateAggregator : EntityAggregator <NSCopying>
{
    int imbricationLevel;
}

- (BOOL)addEntity:(PajeEntity *)entity;
- (PajeEntity *)aggregate;
@end

@interface VariableAggregator : EntityAggregator
@end

@interface OneLinkAggregator : EntityAggregator <NSCopying>
{
    NSDate *earliestEndTime;    // not retained
    NSDate *latestStartTime;    // not retained
}

- (BOOL)addEntity:(PajeEntity *)entity;
- (PajeEntity *)aggregateBefore:(NSDate *)limit;
@end
@interface LinkAggregator : EntityAggregator <NSCopying>
{
    // need one aggregator for each souce/dest container pair
    NSMutableDictionary *aggregators;
}

- (BOOL)addEntity:(PajeEntity *)entity;
- (PajeEntity *)aggregate;
- (PajeEntity *)aggregateBefore:(NSDate *)limit;
@end


#include "AggregateEvent.h"

@implementation EventAggregator
+ (Class)aggregatedEntityClass
{
    return [AggregateEvent class];
}
@end

#include "AggregateState.h"

@implementation StateAggregator

+ (Class)aggregatedEntityClass
{
    return [AggregateState class];
}

/* Try to aggregate one more state.
   Returns YES if state can be aggregated, NO if it cannot.
   States must be entered in endTime order;
   states with the same endTime must be entered in reverse imbricationLevel order.
*/
- (BOOL)addEntity:(PajeEntity *)entity
{
    int entityImbricationLevel;
    double newDuration;

    // cannot aggregate a too wide entity
    if ([entity duration] > aggregationDuration) {
        return NO;
    }

    // if there are no other entities, just add the new one
    if (earliestStartTime == nil) {
        earliestStartTime = [entity startTime];
        imbricationLevel = [entity imbricationLevel];
        [entities addObject:entity];
        return YES;
    }

    // if adding the new entity would make this too wide, it cannot yet be
    // added -> must aggregate some and try again 
    newDuration = [[entity endTime] timeIntervalSinceDate:earliestStartTime];
    if (newDuration > aggregationDuration) {
        return NO;
    }

    entityImbricationLevel = [entity imbricationLevel];

    if (entityImbricationLevel < imbricationLevel) {
        NSDate *entityStartTime = [entity startTime];
        PajeEntity *lastObject;
        // remove states that are inside new one (may happen when refilling)
        while ((lastObject = [entities lastObject]) != nil) {
            int lastImbricationLevel = [lastObject imbricationLevel];
            NSDate *lastStartTime = [lastObject startTime];
            if (lastImbricationLevel > entityImbricationLevel
                || (lastImbricationLevel == entityImbricationLevel 
                    && ![lastStartTime isEarlierThanDate:entityStartTime])) {
                [entities removeLastObject];
            } else {
                break;
            }
        }
        if ([entities count] == 0) {
            // new state will be first
            earliestStartTime = entityStartTime;
        }
        imbricationLevel = entityImbricationLevel;
        [entities addObject:entity];
        return YES;
    }

    if (entityImbricationLevel > imbricationLevel) {
        imbricationLevel = entityImbricationLevel;
        [entities addObject:entity];
        return YES;
    }

    // same imbrication level
    if ([entity isAggregate]) {
        // special case: reaggregating already aggregated state
        // (when refilling emptied chunk)
        NSDate *entityStartTime = [entity startTime];
        PajeEntity *lastObject;
        while ((lastObject = [entities lastObject]) != nil
               && ![[lastObject startTime] isEarlierThanDate:entityStartTime]) {
            [entities removeLastObject];
        }
        if ([entities count] == 0) {
            // new state will be first
            earliestStartTime = entityStartTime;
        }
    }
    [entities addObject:entity];
    return YES;
}


- (PajeEntity *)aggregate
{
    PajeEntity *entity = nil;
    unsigned count;
    
    if (earliestStartTime == nil) {
        return nil;
    }
    count = [entities count];
    if (count == 1) {
        entity = [entities objectAtIndex:0];
        if ([entity subCount] > 0) {
            entity = [AggregateState entityWithEntities:entities];
        }
        [entities removeAllObjects];
        earliestStartTime = nil;
        return entity;
    }

    int firstImbricationLevel;
    int lastIndex;
    
    firstImbricationLevel = [[entities objectAtIndex:0] imbricationLevel];
    if (firstImbricationLevel == imbricationLevel) {
        lastIndex = count - 1;
    } else {
        lastIndex = 0;
        while (lastIndex < count - 1) {
            entity = [entities objectAtIndex:lastIndex + 1];
            if ([entity imbricationLevel] != firstImbricationLevel) {
                break;
            }
            lastIndex++;
        }
    }
    if (lastIndex == count - 1) {
        entity = [AggregateState entityWithEntities:entities];
        [entities removeAllObjects];
        earliestStartTime = nil;
    } else {
        NSRange r = NSMakeRange(0, lastIndex + 1);
        earliestStartTime = [entity startTime];
        entity = [AggregateState entityWithEntities:[entities subarrayWithRange:r]];
        [entities removeObjectsInRange:r];
    }
    return entity;
}

- (id)copyWithZone:(NSZone *)z
{
    StateAggregator *new;
    new = [super copyWithZone:z];
    new->imbricationLevel = imbricationLevel;
    return new;
}
@end


#include "AggregateValue.h"

@implementation VariableAggregator
+ (Class)aggregatedEntityClass
{
    return [AggregateValue class];
}
@end

#include <math.h>
#include "AggregateLink.h"

@implementation OneLinkAggregator

+ (Class)aggregatedEntityClass
{
    return [AggregateLink class];
}

- (void)dealloc
{
    Assign(latestStartTime, nil);
    Assign(earliestEndTime, nil);
    [super dealloc];
}

- (BOOL)addEntity:(PajeEntity *)entity
{
    double newDuration;
    NSDate *entityStartTime;
    NSDate *entityEndTime;

    entityStartTime = [entity startTime];
    entityEndTime = [entity endTime];

    // if there are no other entities, just add the new one
    if (earliestStartTime == nil) {
        earliestStartTime = entityStartTime;
        Assign(latestStartTime, entityStartTime);
        Assign(earliestEndTime, entityEndTime);
        [entities addObject:entity];
        return YES;
    }

    // cannot aggregate if endTimes would span more than aggregationDuration
    newDuration = [entityEndTime timeIntervalSinceDate:earliestEndTime];
    if (newDuration > aggregationDuration) {
        return NO;
    }
    // cannot aggregate if startTimes would span more than aggregationDuration
    // entities are ordered by endTime, so startTime can come in any order
    newDuration = [entityStartTime timeIntervalSinceDate:earliestStartTime];
    if (fabs(newDuration) > aggregationDuration) {
        return NO;
    }
    newDuration = [entityStartTime timeIntervalSinceDate:latestStartTime];
    if (fabs(newDuration) > aggregationDuration) {
        return NO;
    }
    
    earliestStartTime = [earliestStartTime earlierDate:entityStartTime];
    Assign(latestStartTime, [latestStartTime laterDate:entityStartTime]);

    [entities addObject:entity];
    return YES;
}


- (PajeEntity *)aggregateBefore:(NSDate *)limit
{
    if (earliestStartTime == nil) {
        return nil;
    }
    if ([limit timeIntervalSinceDate:earliestEndTime] < aggregationDuration) {
        return nil;
    }

    return [self aggregate];
}

- (id)copyWithZone:(NSZone *)z
{
    OneLinkAggregator *new;
    new = [super copyWithZone:z];
    new->latestStartTime = [latestStartTime retain];
    new->earliestEndTime = [earliestEndTime retain];
    return new;
}
@end

@implementation LinkAggregator

+ (Class)aggregatedEntityClass
{
    return [AggregateLink class];
}

- (id)initWithAggregationDuration:(double)duration
{
    self = [super initWithAggregationDuration:duration];
    if (self != nil) {
        aggregators = [[NSMutableDictionary alloc] init];
    }
    return self;

}

- (NSArray *)allAggregators
{
    NSMutableArray *array;

    array = [NSMutableArray array];

    NSEnumerator *dictEnum;
    NSDictionary *dict;
    dictEnum = [aggregators objectEnumerator];
    while ((dict = [dictEnum nextObject]) != nil) {
        [array addObjectsFromArray:[dict allValues]];
    }
    return array;
}

- (EntityAggregator *)aggregatorForEntity:(PajeEntity *)entity
                             inDictionary:(NSMutableDictionary *)aggDict
{
    PajeContainer *sourceContainer;
    PajeContainer *destContainer;
    EntityAggregator *agg;
    NSMutableDictionary *dict;
    

    sourceContainer = [entity sourceContainer];
    destContainer = [entity destContainer];

    dict = [aggDict objectForKey:sourceContainer];
    if (dict == nil) {
        dict = [NSMutableDictionary dictionary];
        [aggDict setObject:dict forKey:sourceContainer];
    }
    
    agg = [dict objectForKey:destContainer];
    if (agg == nil) {
        agg = [[OneLinkAggregator alloc]
                        initWithAggregationDuration:aggregationDuration];
        [dict setObject:agg forKey:destContainer];
        [agg release];
    }

    return agg;
}

- (EntityAggregator *)aggregatorForEntity:(PajeEntity *)entity
{
    return [self aggregatorForEntity:entity inDictionary:aggregators];
}

- (BOOL)addEntity:(PajeEntity *)entity
{
    EntityAggregator *aggregator;
    aggregator = [self aggregatorForEntity:entity];
    return [aggregator addEntity:entity];
}

- (PajeEntity *)aggregate
{
    NSEnumerator *aggEnum;
    EntityAggregator *aggregator;
    PajeEntity *entity = nil;

    aggEnum = [[self allAggregators] objectEnumerator];
    while ((aggregator = [aggEnum nextObject]) != nil) {
        if ((entity = [aggregator aggregate]) != nil) {
            break;
        }
    }
    return entity;
}

- (PajeEntity *)aggregateEntity:(PajeEntity *)entity
{
    EntityAggregator *aggregator;
    PajeEntity *aggregate;

    aggregator = [self aggregatorForEntity:entity];
    if (![aggregator addEntity:entity]) {
        aggregate = [aggregator aggregate];
        if (aggregate != nil) {
            return aggregate;
        } else {
            return entity;
        }
    }
    return nil;
}

- (PajeEntity *)aggregateBefore:(NSDate *)limit
{
    NSEnumerator *aggEnum;
    EntityAggregator *aggregator;
    PajeEntity *entity = nil;

    aggEnum = [[self allAggregators] objectEnumerator];
    while ((aggregator = [aggEnum nextObject]) != nil) {
        if ((entity = [aggregator aggregateBefore:limit]) != nil) {
            break;
        }
    }
    return entity;
}

- (int)entityCount
{
    int entityCount;
    NSEnumerator *aggEnum;
    EntityAggregator *agg;

    entityCount = 0;
    aggEnum = [[self allAggregators] objectEnumerator];
    while ((agg = [aggEnum nextObject]) != nil) {
        entityCount += [agg entityCount];
    }

    return entityCount;
}

- (id)copyWithZone:(NSZone *)z
{
    LinkAggregator *new;
    new = [super copyWithZone:z];

    new->aggregators = [NSMutableDictionary dictionary];
    id k1;
    NSEnumerator *k1en;

    k1en = [aggregators keyEnumerator];
    while ((k1 = [k1en nextObject]) != nil) {
        NSMutableDictionary *dict;
        NSMutableDictionary *newdict;
        dict = [aggregators objectForKey:k1];
        newdict = [NSMutableDictionary dictionary];
        [new->aggregators setObject:newdict forKey:k1];
        NSEnumerator *k2en;
        id k2;
        k2en = [dict keyEnumerator];
        while ((k2 = [k2en nextObject]) != nil) {
            id newagg;
            newagg = [[dict objectForKey:k2] copyWithZone:z];
            [newdict setObject:newagg forKey:k2];
            [newagg release];
        }
        
    }

    return new;
}
@end

@implementation EntityAggregator
+ (EntityAggregator *)aggregatorForEntityType:(PajeEntityType *)entityType
                          aggregationDuration:(double)duration;
{
    Class subclass;
    switch ([entityType drawingType]) {
    case PajeEventDrawingType:
        subclass = [EventAggregator class];
        break;
    case PajeStateDrawingType:
        subclass = [StateAggregator class];
        break;
    case PajeVariableDrawingType:
        subclass = [VariableAggregator class];
        break;
    case PajeLinkDrawingType:
        subclass = [LinkAggregator class];
        break;
    default:
        NSWarnMLog(@"No support for creating aggregator of type %@", entityType);
        subclass = Nil;
    }

    return [[[subclass alloc] initWithAggregationDuration:duration] autorelease];
}

- (id)initWithAggregationDuration:(double)duration
{
    self = [super init];
    if (self != nil) {
        entities = [[NSMutableArray alloc] init];
        aggregationDuration = duration;
        aggregatedEntityClass = [[self class] aggregatedEntityClass];
    }
    return self;

}

+ (Class)aggregatedEntityClass
{
    [self _subclassResponsibility:_cmd];
    return Nil;
}

- (double)aggregationDuration
{
    return aggregationDuration;
}

- (void)dealloc
{
    Assign(entities, nil);
    [super dealloc];
}

- (BOOL)addEntity:(PajeEntity *)entity
{
    double newDuration;

    // cannot aggregate a too wide entity
    if ([entity duration] > aggregationDuration) {
        return NO;
    }

    // if there are no other entities, just add the new one
    if (earliestStartTime == nil) {
        earliestStartTime = [entity startTime];
        [entities addObject:entity];
        return YES;
    }

    // if adding the new entity would make this too wide, it cannot yet be
    // added -> must aggregate some and try again 
    newDuration = [[entity endTime] timeIntervalSinceDate:earliestStartTime];
    if (newDuration > aggregationDuration) {
        return NO;
    }

    [entities addObject:entity];
    return YES;
}


- (PajeEntity *)aggregate
{
    PajeEntity *entity;
    unsigned count;
    
    if (earliestStartTime == nil) {
        return nil;
    }

    count = [entities count];
    NSAssert(count != 0, NSInternalInconsistencyException);
    if (count > 1) {
        entity = [aggregatedEntityClass entityWithEntities:entities];
    } else {
        // FIXME: is this garanteed to be retained elsewhere?
        entity = [entities objectAtIndex:0];
        [[entity retain] autorelease];
    }
    [entities removeAllObjects];
    earliestStartTime = nil;
    return entity;
}


- (PajeEntity *)aggregateEntity:(PajeEntity *)entity
{
    PajeEntity *aggregate;

    if (![self addEntity:entity]) {
        aggregate = [self aggregate];
        if (aggregate != nil) {
            return aggregate;
        } else {
            return entity;
        }
    }
    return nil;
}


- (PajeEntity *)aggregateBefore:(NSDate *)limit
{
    if (earliestStartTime == nil) {
        return nil;
    }
    if ([limit timeIntervalSinceDate:earliestStartTime] < aggregationDuration) {
        return nil;
    }

    return [self aggregate];
}

- (NSArray *)entities
{
    return entities;
}

- (int)entityCount
{
    return [entities count];
}

- (id)copyWithZone:(NSZone *)z
{
    EntityAggregator *new;
    new = [[[self class] allocWithZone:z] init];
    new->entities = [entities mutableCopyWithZone:z];
    new->earliestStartTime = earliestStartTime;
    new->aggregationDuration = aggregationDuration;
    return new;
}

@end