File: ULSimulationCreator.m

package info (click to toggle)
adun.app 0.81-15
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 18,384 kB
  • sloc: objc: 70,952; ansic: 6,668; yacc: 394; python: 75; cpp: 36; makefile: 33; xml: 15; awk: 3
file content (404 lines) | stat: -rw-r--r-- 10,463 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
/* 
   Project: UL

   Copyright (C) 2007 Michael Johnston & Jordi Villa-Freixa

   Author: Michael Johnston

   This application 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 application 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
   Library General Public License for more details.
 
   You should have received a copy of the GNU General Public
   License along with this library; if not, write to the Free
   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA.
*/
#include "ULSimulationCreator.h"

@implementation ULSimulationCreator

- (BOOL) _checkInputData
{
	BOOL missingData = NO;
	NSArray* inputDataNames;
	NSEnumerator* externalNamesEnum;
	NSString* name;

	inputDataNames = [inputData allKeys];
	externalNamesEnum = [[simulationTemplate externalReferences]
				objectEnumerator];
	while((name = [externalNamesEnum nextObject]))
		if(![inputDataNames containsObject: name])
		{
			missingData = YES;
			break;
		}

	if(!missingData)
		return YES;
	else	
		return NO;
}

- (NSMutableDictionary*) _createCoreTemplate
{
	NSMutableDictionary* coreTemplate;
	NSDictionary* metadata, *checkpointing;

	coreTemplate = [simulationTemplate coreRepresentation];

	metadata = [NSDictionary dictionaryWithObject: 
				[simulationNameField stringValue]
			forKey: @"simulationName"];
	[coreTemplate setObject: metadata forKey: @"metadata"];

	checkpointing = [NSDictionary dictionaryWithObjectsAndKeys: 
				[energyField stringValue], @"energy",
				[configurationField stringValue], @"configuration",
				[energyDumpField stringValue], @"energyDump",
				nil];
	[coreTemplate setObject: checkpointing forKey: @"checkpoint"];
	[coreTemplate setObject: [NSDictionary dictionary]
		forKey: @"externalObjects"];

	return coreTemplate;
}

/*
Checks that the external references defined
by the template refer to data types we can handle -
i.e. AdDataSource or AdDataSet.
*/
- (BOOL) _checkTemplateReferences
{
	BOOL allowedReferenceTypes;
	NSEnumerator *externalReferenceEnum, *externalTypeEnum;
	NSArray* externalTypes;
	id type, externalReference;

	allowedReferenceTypes = YES;
	externalReferenceEnum = [[simulationTemplate externalReferences]
					objectEnumerator];
	while(allowedReferenceTypes && 
		(externalReference = [externalReferenceEnum nextObject]))
	{
		allowedReferenceTypes = NO;
		externalTypes = [[simulationTemplate externalReferenceTypes]
					objectForKey: externalReference];
		//Check the types allowed for this reference
		//If any of them are valid we set allowedReferenceTypes
		//to YES and continue. 
		//Otherwise its set to NO and the loop will end.
		externalTypeEnum = [externalTypes objectEnumerator];
		while((type = [externalTypeEnum nextObject]))
		{
			if([inputDataTypes containsObject: type])
				allowedReferenceTypes = YES;
		}		
	}			


	if(!allowedReferenceTypes)
		NSRunAlertPanel(@"Unable to load template",
			[NSString stringWithFormat:
				@"Template contains external reference to unsupported data.\nReference %@, type %@.",
				externalReference,
				type],
			@"Dismiss",
			nil,
			nil);

	return allowedReferenceTypes;
}


- (id) init
{
	if((self = [super init]))
	{
		inputData = [NSMutableDictionary new];
		//Can add more in the future
		inputDataTypes = [NSArray arrayWithObjects: 		
					@"AdDataSource", 
					@"AdDataSet",
					nil];
		[inputDataTypes retain];		
		processManager = [ULProcessManager appProcessManager];
		pasteboard = [ULPasteboard appPasteboard]; 
		inputTypeDisplayNames = [NSDictionary dictionaryWithObjectsAndKeys:
						@"System", @"AdDataSource",
						@"DataSet", @"AdDataSet", nil];
		[inputTypeDisplayNames retain];				

	}

	return self;
}

- (void) dealloc
{
	[simulationTemplate release];
	[inputData release];
	[inputDataTypes release];
	[inputTypeDisplayNames release];
	[super dealloc];
}

- (void) awakeFromNib
{
	[selectHostButton removeAllItems];
	[selectHostButton addItemsWithTitles: [processManager hosts]];
	[selectHostButton selectItemAtIndex: 0];

	[tabView selectTabViewItemAtIndex: 0];
	[window setDelegate: self];
	[externalDataTable setDataSource: self];

/*	[sectionList removeAllItems];
	[sectionList addItemWithTitle: @"General"];
	[sectionList addItemWithTitle: @"Input Data"];
	[sectionList addItemWithTitle: @"Checkpointing"];
	[sectionList addItemWithTitle: @"Host"];
	[sectionList selectItemWithTitle: @"General"];*/
}

- (BOOL) validateMenuItem: (NSMenuItem*) menuItem
{
	NSString* action;

	action = NSStringFromSelector([menuItem action]);
	if([action isEqual: @"load:"])
	{
		if([pasteboard countOfObjectsForType: @"ULTemplate"] == 1)
			return YES;

		if([pasteboard availableTypeFromArray: inputDataTypes] != nil)
		{
			if(simulationTemplate == nil)
				return NO;
			else	
				return YES;
		}		
		else
			return NO;
	}		

	return YES;
}

/*
- (void) sectionDidChange: (id) sender
{
	NSString* section;
	
	section = [sectionList titleOfSelectedItem];
	[sectionList setNeedsDisplay: YES];
	[tabView selectTabViewItemWithIdentifier: section];
	//FIXME: Gnustep Bug - For some reason the above call 
	//stops the popup list from changing to section.
	//Have to force the change.
	[sectionList selectItemWithTitle: section];
}*/

- (void) createSimulation: (id) sender
{
	//NSFloatingWindowLevel is incompatible with
	//NSPopupButton on gnome so I have remove use
	//of the pop up button in favour of normal tabs.
	[window setLevel: NSFloatingWindowLevel];
	
	//[sectionList setNeedsDisplay: YES];
	[tabView selectTabViewItemWithIdentifier: @"General"];
	//[sectionList selectItemWithTitle: @"General"];
	[energyDumpField setStringValue: @"1000"];
	[templateField setStringValue: @""];
	[simulationNameField setStringValue: @"Output"];
	[window center];
	[window makeKeyAndOrderFront: self];
}

- (void) createProcess: (id) sender
{
	BOOL retVal, create;
	NSMutableDictionary* coreTemplate;

	create = YES;	
	
	if(![self _checkInputData])
	{
		retVal = NSRunAlertPanel(@"Alert",
				@"All input data slots not filled",
				@"Continue",
				@"Cancel",
				nil);
		if(retVal != NSOKButton)
			create = NO;
	}		

	if(create)
	{
		//Create the core template 
		//FIXME: Later may add all elements to template instace
		//and use that - Requires modification of ULProcess & ULProcessManager
		coreTemplate = [self _createCoreTemplate];
		host = [selectHostButton titleOfSelectedItem];
		[processManager newProcessWithInputData: [[inputData copy] autorelease]
			simulationTemplate: coreTemplate
			host: host];
	}	
}

- (void) load: (id) sender
{
	BOOL allowedReferenceTypes;
	int selectedRow;
	NSString* availableType, *name; 
	NSArray *dataTypes;
	id data;

	availableType = [pasteboard availableTypeFromArray: 
				[NSArray arrayWithObjects: @"ULTemplate",
					@"AdDataSource", @"AdDataSet", nil]];
	if([inputDataTypes containsObject: availableType])
	{
		/*
		 * Get selected externalDataTable entry
		 * Check if types are compatible
		 * Load data and add name to the table
		 * Get rid of anything that was previously there.
		 * Update input data dictionary
		 */
		selectedRow = [externalDataTable selectedRow];
		if(selectedRow < 0)
		{
			NSRunAlertPanel(@"Alert",
				@"No input data row selected",
				@"Dismiss",
				nil,
				nil);
			return;
		}	
		name = [[simulationTemplate externalReferences] 
				objectAtIndex: selectedRow];
		dataTypes = [[simulationTemplate externalReferenceTypes] 
				objectForKey: name];
		if([dataTypes containsObject: availableType])
		{
			data = [pasteboard objectForType: availableType];
			[inputData setObject: data forKey: name];
			[externalDataTable reloadData];
		}
		else
			NSRunAlertPanel(@"Alert",
				@"Types are not compatible",
				@"Dismiss",
				nil,
				nil);
	}
	else if([availableType isEqual: @"ULTemplate"])
	{
		[simulationTemplate release];
		simulationTemplate = [pasteboard objectForType: @"ULTemplate"];
		[simulationTemplate retain];

		//Check the external reference data types
		//We can only handle external references to AdDataSets & AdDataSources
		//at the moment
		allowedReferenceTypes = [self _checkTemplateReferences];
			
		if(allowedReferenceTypes)
		{
			[templateField setStringValue: [simulationTemplate name]];
			[externalDataTable reloadData];
		}
		else
		{
			[simulationTemplate release];
			simulationTemplate = nil;
		}
	}
}

- (void) displayTemplate: (id) sender
{
	if(simulationTemplate != nil)
		[[ULTemplateViewController templateViewController]
			displayTemplate: simulationTemplate];
}

- (void) close: (id) sender
{
	[simulationTemplate release];
	simulationTemplate = nil;
	[inputData removeAllObjects];
	[externalDataTable reloadData];
	[energyDumpField setStringValue: @"1000"];
	[configurationField setStringValue: @"100"];
	[energyField setStringValue: @"100"];
}

- (void) closeWindow: (id) sender
{
	[self close: sender];
	[window close];
}

- (int)numberOfRowsInTableView:(NSTableView *)aTableView
{	
	return [[simulationTemplate externalReferences] count];
}

- (id)tableView:(NSTableView *)aTableView
	 objectValueForTableColumn:(NSTableColumn *)aTableColumn 
	row:(int)rowIndex
{
	id columnId, data, type;
	NSString* name;
	NSArray* types;
	NSMutableArray* allowedTypes = [NSMutableArray array];
	NSEnumerator* inputTypesEnum;

	columnId = [aTableColumn identifier];	
	name = [[simulationTemplate externalReferences] objectAtIndex: rowIndex];

	if([columnId isEqual: @"name"])
		return name;
	else if([columnId isEqual: @"dataType"])
	{
		types = [[simulationTemplate externalReferenceTypes] 
				objectForKey: name];
		//We can only handle AdDataSet & AdDataSource
		//so we make sure thats all we display
		inputTypesEnum = [inputDataTypes objectEnumerator];
		while((type = [inputTypesEnum nextObject]))
		{
			if([types containsObject: type])
				[allowedTypes addObject:
					[inputTypeDisplayNames objectForKey: type]];
		}			
		
		return [allowedTypes componentsJoinedByString: @", "];
	}	
	else if([columnId isEqual: @"data"])
	{
		data = [inputData objectForKey: name];
		if(data != nil)
			return [(AdModelObject*)data name];
		else
			return @"None loaded";
	}		
	return nil; // Not reached.
}

- (void) windowWillClose: (id) sender
{
	[self close: self];
}

@end