File: SimulContainer.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 (623 lines) | stat: -rw-r--r-- 16,015 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
614
615
616
617
618
619
620
621
622
623
/*
    Copyright (c) 1998, 1999, 2000, 2001, 2002, 2003, 2004 Benhur Stein
    
    This file is part of Paj.

    Paj is free software; you can redistribute it and/or modify it under
    the terms of the GNU Lesser General Public License as published by the
    Free Software Foundation; either version 2 of the License, or (at your
    option) any later version.

    Paj 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 Lesser General Public License
    for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Paj; if not, write to the Free Software Foundation, Inc.,
    59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*/
//
// PajeContainer
//
// superclass for containers
//

#include "SimulContainer.h"
#include "SimulChunk.h"
#include "../General/ChunkArray.h"
#include "UserState.h"
#include "UserLink.h"
#include "../General/Macros.h"
#include "PajeSimul.h"
#include "../General/UniqueString.h"

// auxiliary class to contain the internal state of a container
@interface SimulContainerState : NSObject
{
    NSDate *time;
    int logicalTime;
}
+ (SimulContainerState *)stateWithTime:(NSDate *)t
                           logicalTime:(int)l;
- (NSDate *)time;
- (int)logicalTime;
@end
@implementation SimulContainerState
- (id)initWithTime:(NSDate *)t
       logicalTime:(int)l
{
    self = [super init];
    if (self != nil) {
        Assign(time, t);
        logicalTime = l;
    }
    return self;
}

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

+ (SimulContainerState *)stateWithTime:(NSDate *)t
                           logicalTime:(int)l
{
    return [[[self alloc] initWithTime:t logicalTime:l] autorelease];
}

- (NSDate *)time
{
    return time;
}

- (int)logicalTime
{
    return logicalTime;
}
@end


@implementation SimulContainer

+ (SimulContainer *)containerWithType:(PajeEntityType *)type
                                 name:(NSString *)n
                                alias:(NSString *)a
                            container:(PajeContainer *)c
                         creationTime:(NSDate *)time
                                event:(PajeEvent *)event
                            simulator:(id)simul
{
    return [[[self alloc] initWithType:type
                                  name:n
                                 alias:a
                             container:c
                          creationTime:time
                                 event:event
                             simulator:simul] autorelease];
}

- (id)initWithType:(PajeEntityType *)type
              name:(NSString *)n
             alias:(NSString *)a
         container:(PajeContainer *)c
      creationTime:(NSDate *)time
             event:(PajeEvent *)event
         simulator:(id)simul
{
    self = [super initWithType:type
                          name:n
                     container:c];
    if (self != nil) {
        Assign(creationTime, time);
        simulator = simul;

        Assign(alias, a);
        Assign(userEntities, [NSMutableDictionary dictionary]);
        Assign(minValues, [NSMutableDictionary dictionary]);
        Assign(maxValues, [NSMutableDictionary dictionary]);
        Assign(extraFields, [event extraFields]);
    }
    
    return self;
}

- (void)dealloc
{
    Assign(creationTime, nil);
    Assign(alias, nil);
    Assign(lastTime, nil);
    Assign(userEntities, nil);
    Assign(minValues, nil);
    Assign(maxValues, nil);
    Assign(extraFields, nil);
    [super dealloc];
}

- (NSString *)alias
{
    return alias;
}

- (NSDate *)startTime
{
    return creationTime;
}

- (NSDate *)time
{
    return creationTime;
}

- (NSDate *)endTime
{
    if (lastTime != nil) {
        return lastTime;
    }
    if (container != nil) {
        return [container endTime];
    }
    return [simulator endTime];
}

- (void)setLastTime:(NSDate *)time
{
    Assign(lastTime, time);
}


- (id)copyWithZone:(NSZone *)zone
{
    return [self retain];
}

- (void)stopWithEvent:(PajeEvent *)event
{
    NSDate *stopTime;
    
    if (event != nil) {
        stopTime = [event time];
    } else {
        stopTime = [simulator currentTime];
    }
    if (lastTime == nil) {
        [self setLastTime:stopTime];
    }

    NSEnumerator *typeEnumerator;
    id type;

    typeEnumerator = [userEntities keyEnumerator];
    while ((type = [typeEnumerator nextObject]) != nil) {
        SimulChunk *chunk;
        chunk = [self chunkOfType:type];
        if ([chunk isActive]) {
            [chunk stopWithEvent:event];
            [chunk endOfChunkWithTime:stopTime];
            [simulator outputChunk:chunk];
        }
    }
    //[userEntities removeAllObjects];

    NSEnumerator *subEnum;
    SimulContainer *subContainer;

    subEnum = [subContainers objectEnumerator];
    while ((subContainer = [subEnum nextObject]) != nil) {
        [subContainer stopWithEvent:event];
    }
    isActive = NO;
}

- (BOOL)isStopped
{
    return lastTime != nil;
}

- (BOOL)isActive
{
    return isActive;
}

- (void)startChunk
{
    NSEnumerator *typeEnumerator;
    id type;

    isActive = ![simulator isReplaying];
    typeEnumerator = [userEntities keyEnumerator];
    while ((type = [typeEnumerator nextObject]) != nil) {
        SimulChunk *chunk;
        chunk = [self chunkOfType:type];
        if (chunk != nil && [chunk isZombie]) {
            SimulChunk *previousChunk;
            NSArray *stack;
            previousChunk = [self previousChunkOfType:type];
            if (previousChunk != nil) {
                stack = [previousChunk incompleteEntities];
            } else {
                stack = [NSArray array];
            }
            [chunk activate];
            [chunk setPreviousChunkIncompleteEntities:stack];
        }
        if ([chunk isActive]) {
            isActive = YES;
        }
    }
}

- (void)endOfChunkLast:(BOOL)last
{
    NSEnumerator *typeEnumerator;
    id type;

    if (![self isActive]) {
        return;
    }

    if (last) {
        [self stopWithEvent:nil];
        return;
    }
    NSDate *chunkEndTime = [simulator currentTime];
    typeEnumerator = [userEntities keyEnumerator];
    while ((type = [typeEnumerator nextObject]) != nil) {
        SimulChunk *chunk;
        chunk = [self chunkOfType:type];
        if ([chunk isActive]) {
            [chunk endOfChunkWithTime:chunkEndTime];
            [simulator outputChunk:chunk];
        }
    }
}

- (void)emptyChunk:(int)chunkNumber
{
    ChunkArray *chunkArray;
    NSEnumerator *chunkArrayEnumerator;
    
    chunkArrayEnumerator = [userEntities objectEnumerator];
    while ((chunkArray = [chunkArrayEnumerator nextObject]) != nil) {
        [[chunkArray chunkAtIndex:chunkNumber] empty];
    }
}

- (SimulChunk *)previousChunkOfType:(id)type
{
    ChunkArray *chunkArray;
    SimulChunk *chunk;
    int currentChunkNumber;

    currentChunkNumber = [simulator currentChunkNumber];    
    chunkArray = [userEntities objectForKey:type];
    
    chunk = (SimulChunk *)[chunkArray chunkAtIndex:currentChunkNumber - 1];
    return chunk;
}

- (SimulChunk *)chunkOfType:(id)type
{
    ChunkArray *chunkArray;
    SimulChunk *chunk;
    int currentChunkNumber;

    currentChunkNumber = [simulator currentChunkNumber];    
    chunkArray = [userEntities objectForKey:type];
    
    if (chunkArray == nil) {
        chunkArray = [[ChunkArray alloc] init];
        [chunkArray setFirstIndex:currentChunkNumber];
        [userEntities setObject:chunkArray forKey:type];
        chunk = [SimulChunk chunkWithEntityType:type
                                      container:self];
        [chunk setStartTime:[simulator currentTime]];
        [chunk setPreviousChunkIncompleteEntities:[NSArray array]];
        [chunkArray addChunk:chunk];
        [chunkArray release];
        isActive = YES;
    } else {
        chunk = (SimulChunk *)[chunkArray chunkAtIndex:currentChunkNumber];
        if (chunk == nil) {
            SimulChunk *previousChunk;
            previousChunk = (SimulChunk *)
                            [chunkArray chunkAtIndex:currentChunkNumber - 1];
            if (previousChunk == nil || [previousChunk isLastChunk]) {
                return nil;
            }
            chunk = [SimulChunk chunkWithEntityType:type
                                          container:self];
            [chunk setStartTime:[previousChunk endTime]];
            [chunk setPreviousChunkIncompleteEntities:
                                           [previousChunk incompleteEntities]];

            [chunkArray addChunk:chunk];
            isActive = YES;
        }
    }
    
    return chunk;
}


- (void)newEventWithType:(id)type
                   value:(id)value
               withEvent:(PajeEvent *)event
{
    SimulChunk *chunk;

    chunk = [self chunkOfType:type];
    if (![chunk isActive]) return;

    [chunk newEventEvent:event value:value];
}

- (void)setUserStateOfType:(PajeEntityType *)type
                   toValue:(id)value
                 withEvent:(PajeEvent *)event
{
    SimulChunk *chunk;

    chunk = [self chunkOfType:type];
    if (![chunk isActive]) return;

    [chunk setStateEvent:event value:value];
}

- (void)pushUserStateOfType:(PajeEntityType *)type
                      value:(id)value
                  withEvent:(PajeEvent *)event
{
    SimulChunk *chunk;

    chunk = [self chunkOfType:type];
    if (![chunk isActive]) return;

    [chunk pushStateEvent:event value:value];
}

- (void)popUserStateOfType:(PajeEntityType *)type
                 withEvent:(PajeEvent *)event
{
    SimulChunk *chunk;

    chunk = [self chunkOfType:type];
    if (![chunk isActive]) return;

    [chunk popStateEvent:event];
}

- (void)verifyMinMaxOfEntityType:(PajeEntityType *)t
                       withValue:(double)value
{
    NSNumber *oldMin;
    NSNumber *oldMax;
    id type = t;
    oldMin = [minValues objectForKey:type];
    oldMax = [maxValues objectForKey:type];

    if ((oldMin == nil) || (value < [oldMin doubleValue])) {
        [minValues setObject:[NSNumber numberWithDouble:value] forKey:type];
        [type possibleNewMinValue:value];
    }

    if ((oldMax == nil) || (value > [oldMax doubleValue])) {
        [maxValues setObject:[NSNumber numberWithDouble:value] forKey:type];
        [type possibleNewMaxValue:value];
    }
}

- (void)setUserVariableOfType:(PajeVariableType *)type
                toDoubleValue:(double)value
                    withEvent:(PajeEvent *)event
{
    SimulChunk *chunk;

    chunk = [self chunkOfType:type];
    if (![chunk isActive]) return;

    [chunk setVariableEvent:event
                doubleValue:value];
}

- (void)addUserVariableOfType:(PajeVariableType *)type
                  doubleValue:(double)value
                    withEvent:(PajeEvent *)event
{
    SimulChunk *chunk;

    chunk = [self chunkOfType:type];
    if (![chunk isActive]) return;

    [chunk addVariableEvent:event
                doubleValue:value];
}

- (void)subUserVariableOfType:(PajeVariableType *)type
                  doubleValue:(double)value
                    withEvent:(PajeEvent *)event
{
    SimulChunk *chunk;

    chunk = [self chunkOfType:type];
    if (![chunk isActive]) return;

    [chunk subVariableEvent:event
                doubleValue:value];
}

- (double)minValueForEntityType:(PajeEntityType *)type
{
    return [[minValues objectForKey:type] doubleValue];
}

- (double)maxValueForEntityType:(PajeEntityType *)type
{
    return [[maxValues objectForKey:type] doubleValue];
}


- (void)startUserLinkOfType:(PajeEntityType *)type
                      value:(id)value
            sourceContainer:(PajeContainer *)cont
                        key:(id)key
                  withEvent:(PajeEvent *)event
{
    SimulChunk *chunk;

    chunk = [self chunkOfType:type];
    if (![chunk isActive]) return;

    [chunk startLinkEvent:event
                    value:value
          sourceContainer:cont
                      key:key];
}

- (void)endUserLinkOfType:(PajeEntityType *)type
                    value:(id)value
            destContainer:(PajeContainer *)cont
                      key:(id)key
                withEvent:(PajeEvent *)event
{
    SimulChunk *chunk;

    chunk = [self chunkOfType:type];
    if (![chunk isActive]) return;

    [chunk endLinkEvent:event
                  value:value
          destContainer:cont
                    key:key];
}

- (int)logicalTime
{
    return logicalTime;
}

- (void)setLogicalTime:(int)lt
{
    logicalTime = lt;
}

- (void)reset
{
    //[userEntities removeAllObjects];
}

- (NSEnumerator *)enumeratorOfEntitiesTyped:(PajeEntityType *)type
                                   fromTime:(NSDate *)start
                                     toTime:(NSDate *)end
{
    ChunkArray *chunkArray;
    NSEnumerator *enumerator;
    chunkArray = [userEntities objectForKey:type];
    BOOL foundAllData;

    foundAllData = NO;
    while (!foundAllData) {
NS_DURING
        if (![self isStopped] && [end isLaterThanDate:[self endTime]]) {
            [simulator getChunksUntilTime:end];
        }
        enumerator = [chunkArray enumeratorOfEntitiesFromTime:start
                                                       toTime:end];
        foundAllData = YES;
NS_HANDLER
        NSString *exceptionName;
        exceptionName = [localException name];
        if ([exceptionName isEqual:@"PajeMissingChunkException"]) {
            int chunkNo;
            chunkNo = [[[localException userInfo]
                                objectForKey:@"ChunkNumber"] intValue];
            [simulator notifyMissingChunk:chunkNo];
        } else {
            [localException raise];
	}
NS_ENDHANDLER
    }
    //[SimulChunk emptyLeastRecentlyUsedChunks];
    return enumerator;
}

- (NSEnumerator *)enumeratorOfCompleteEntitiesTyped:(PajeEntityType *)type
                                           fromTime:(NSDate *)start
                                             toTime:(NSDate *)end
{
    ChunkArray *chunkArray;
    NSEnumerator *enumerator;
    chunkArray = [userEntities objectForKey:type];
    BOOL foundAllData;

    foundAllData = NO;
    while (!foundAllData) {
NS_DURING
        if (![self isStopped] && [end isLaterThanDate:[self endTime]]) {
            [simulator getChunksUntilTime:end];
        }
        enumerator = [chunkArray enumeratorOfCompleteEntitiesFromTime:start
                                                            untilTime:end];
        foundAllData = YES;
NS_HANDLER
        NSString *exceptionName;
        exceptionName = [localException name];
        if ([exceptionName isEqual:@"PajeMissingChunkException"]) {
            int chunkNo;
            chunkNo = [[[localException userInfo]
                                objectForKey:@"ChunkNumber"] intValue];
            [simulator notifyMissingChunk:chunkNo];
        } else {
            [localException raise];
	}
NS_ENDHANDLER
    }
    //[SimulChunk emptyLeastRecentlyUsedChunks];
    return enumerator;
}


- (id)chunkState
{
    return [SimulContainerState stateWithTime:lastTime logicalTime:logicalTime];
}

- (void)setChunkState:(id)obj
{
    SimulContainerState *state = (SimulContainerState *)obj;

    [self setLastTime:[state time]];
    [self setLogicalTime:[state logicalTime]];
}

- (void)encodeWithCoder:(NSCoder *)coder
{
    [NSException raise:@"SimulContainer should not be encoded" format:nil];
}

- (NSArray *)fieldNames
{
    NSArray *fieldNames;
    fieldNames = [super fieldNames];
    if (extraFields != nil) {
        fieldNames = [fieldNames arrayByAddingObjectsFromArray:[extraFields allKeys]];
    }
    return fieldNames;
}

- (id)valueOfFieldNamed:(NSString *)fieldName
{
    id value = nil;

    if (extraFields != nil) {
        value = [extraFields objectForKey:fieldName];
    }

    if (value == nil) {
        value = [super valueOfFieldNamed:fieldName];
    }

    return value;
}
@end