-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCWGraphPlotDataSource.m
More file actions
182 lines (138 loc) · 4.58 KB
/
CWGraphPlotDataSource.m
File metadata and controls
182 lines (138 loc) · 4.58 KB
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
//
// CWGraphPlotDataSource.m
// MMCourseWork
//
// Created by Alexey Streltsow on 12/20/09.
// Copyright 2009 Karma World LLC. All rights reserved.
//
#import "CWGraphPlotDataSource.h"
#import "CWMethodExecutor.h"
@interface CWGraphPlotDataSource ( )
+ (NSString*) keyForArgument:(double)arg;
- (CWMethodOperation *) operationForArgument: (double) fval;
@property (nonatomic, retain) NSMutableDictionary* outputValues;
@property (nonatomic, retain) NSMutableDictionary* inputValues;
@end
@implementation CWGraphPlotDataSource
@synthesize outputValues = _outputValues, inputValues = _inputValues;
@synthesize outputKey = _outputKey;
@synthesize enabled = _enabled;
@synthesize resource = _resource;
@synthesize inputKey = _inputKey;
@synthesize plotColor = _plotColor;
@synthesize rangeBegin = _rangeBegin, rangeEnd = _rangeEnd, rangeStep = _rangeStep;
#pragma mark -
+ (NSString*) keyForArgument:(double)arg {
return [NSString stringWithFormat:@"%g", arg];
}
+ (id) dataSourceWithResourceClass: (Class)aClass inputValues:(NSDictionary*)inputs outputKey:(NSString*)aKey {
id dataSource = [[[self class] alloc] initWithResourceClass:aClass inputValues:inputs outputKey:aKey];
return [dataSource autorelease];
}
#pragma mark -
- (id) initWithResourceClass:(Class)aClass inputValues:(NSDictionary*)inputs outputKey:(NSString*)aKey {
if ( self = [super init] ) {
_resource = aClass;
self.inputValues = [NSMutableDictionary dictionaryWithDictionary:inputs];
self.outputValues = [NSMutableDictionary dictionary];
self.outputKey = aKey;
_rangeStep = 1.0;
_locker = [[NSLock alloc] init];
}
return self;
}
#pragma mark -
#pragma mark graph delegate
- (double) graphicPlotView:(CWGraphPlotView*)plotView valueForArgument:(double)arg {
id value = [_outputValues valueForKey:[CWGraphPlotDataSource keyForArgument:arg]];
if ( value ) {
return [[value valueForKey:_outputKey] doubleValue];
}
else {
// make it happen SYNCHRONOUSLY
if ( [_resource allowsSynchronousExecution] ) {
CWMethodOperation* op = [self operationForArgument:arg];
[op main];
return [[[op outputs] valueForKey:_outputKey] doubleValue];
}
else {
return 0.0;
}
}
}
- (CWMethodOperation *) operationForArgument: (double) fval {
CWMethodOperation* calcOperation = [[_resource alloc] init];
[calcOperation.inputs addEntriesFromDictionary:_inputValues];
[calcOperation.inputs setValue:[NSNumber numberWithDouble:fval] forKey:_inputKey];
return [calcOperation autorelease];
}
- (void)prepareDataInRangeBegin:(double)argBegin rangeEnd:(double)argEnd delegate:(id<CWOperationDelegate>)delegate {
_rangeBegin = argBegin;
_rangeEnd = argEnd;
if ( _rangeEnd < _rangeBegin ) {
_rangeEnd = argBegin;
_rangeBegin = argEnd;
}
if ( _rangeStep == 0 ) {
_rangeStep = (_rangeEnd - _rangeBegin) / 1000.0f;
}
_prepareDelegate = delegate; // we'll call him when everything's done
_operationsDone = 0;
_operationsTotal = 0;
[_outputValues removeAllObjects];
CWMethodOperation* generalOperation = [[CWMethodOperation alloc] init];
for (double fval =_rangeBegin; fval <= _rangeEnd; fval += _rangeStep) {
_operationsTotal++;
CWMethodOperation *calcOperation = [self operationForArgument: fval];
calcOperation.delegate = self;
//[generalOperation addDependency:calcOperation];
[[CWMethodExecutor sharedInstance] addOperation:calcOperation];
}
generalOperation.delegate = self;
//[[CWMethodExecutor sharedInstance] addOperation:generalOperation];
[generalOperation release];
}
- (int) progressPercent {
if ( _operationsTotal == 0 )
return 0;
return (_operationsDone * 1.0) / _operationsTotal * 100;
}
- (BOOL) canProvideDataForArgument:(double)arg {
if ( [_resource allowsSynchronousExecution] )
return YES;
return ( [_outputValues valueForKey:[CWGraphPlotDataSource keyForArgument:arg] ]!= nil );
}
#pragma mark -
#pragma mark operation delegate
- (void) operationSucceeded:(CWMethodOperation *)operation {
if ( [operation isKindOfClass:_resource] ) {
[_locker lock];
@try {
NSString* key = [[self class] keyForArgument:[[operation.inputs valueForKey:_inputKey] doubleValue]];
[_outputValues setValue:operation.outputs forKey:key];
_operationsDone++;
}
@finally {
[_locker unlock];
}
}
else {
[_prepareDelegate operationSucceeded:operation];
}
}
- (void) operationFailed:(CWMethodOperation *)operation {
}
- (NSString*) methodName {
return [_resource description];
}
#pragma mark -
- (void) dealloc {
[_locker release];
self.outputKey = nil;
self.inputValues = nil;
self.inputKey = nil;
self.outputValues = nil;
self.plotColor = nil;
[super dealloc];
}
@end