File: upload.bs

package info (click to toggle)
storm-lang 0.7.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 52,028 kB
  • sloc: ansic: 261,471; cpp: 140,432; sh: 14,891; perl: 9,846; python: 2,525; lisp: 2,504; asm: 860; makefile: 678; pascal: 70; java: 52; xml: 37; awk: 12
file content (386 lines) | stat: -rw-r--r-- 10,230 bytes parent folder | download
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
use ui;
use layout;
use progvis:net;
use core:geometry;
use core:io;

/**
 * Dialog for uploading new problems.
 */
dialog UploadDlg {
	layout Grid {
		expandCol: 1;
		Label nameLbl("Name:", HAlign:right, VAlign:center) {}
		Edit name("Name your problem") { colspan: 2; }
		nextLine;
		Label implLbl("Implementation:", HAlign:right, VAlign:center) {}
		Edit implName("") {}
		Button implButton("Select...") {}
		nextLine;
		Label testLbl("Test program:", HAlign:right, VAlign:center) {}
		Edit testName("") {}
		Button testButton("Select...") {}
		nextLine;
		Label refLbl("Reference implementation:", HAlign:right, VAlign:center) {}
		Edit refName("") {}
		Button refButton("Select...") {}
		nextLine;
		Label("Additional errors to consider:", HAlign:right) {}
		CheckButton checkLeaks("Memory leaks") { colspan: 2; checked: true; }
		nextLine;
		CheckButton checkTermination("Livelocks") { col: 1; colspan: 2; checked: true; }
		nextLine;
		CheckButton checkBusyWait("Busy wait") { col: 1; colspan: 2; checked: true; }
		nextLine;
		CheckButton checkIgnoreKnownBusyWait("Ignore calls to \"intentional_busy_wait()\"") { col: 1; colspan: 2; }
		nextLine;
		Label("Acceptable errors for initial attempt:", HAlign:right) {}
		CheckButton acceptLeaks("Memory leaks") { colspan: 2; checked: true; }
		nextLine;
		CheckButton acceptTermination("Livelocks") { col: 1; colspan: 2; }
		nextLine;
		CheckButton acceptBusyWait("Busy wait") { col: 1; colspan: 2; checked: true; }
		nextLine;
		Progress progress { col: 0; colspan: 2; }
		Button uploadButton("Upload") { col: 2; }
	}

	Client client;

	// Upload a new problem.
	init(Client client) {
		init("Submit a problem", Size(500, 10)) {
			client = client;
		}

		progress.visible = false;
		defaultChoice(uploadButton);
		uploadButton.onClick = &this.upload();

		implButton.onClick = &this.pickImpl();
		testButton.onClick = &this.pickTest();
		refButton.onClick = &this.pickRef();
	}

	void pickImpl() {
		unless (f = pickFile())
			return;

		implName.text = f.format;

		Str title = f.title;
		Str ext = f.ext;

		Url test = f.parent / (title + "_test." + ext);
		Url ref = f.parent / (title + "_ref." + ext);
		if (test.exists())
			testName.text = test.format;
		if (ref.exists())
			refName.text = ref.format;
	}

	void pickTest() {
		unless (f = pickFile())
			return;

		testName.text = f.format;
	}

	void pickRef() {
		unless (f = pickFile())
			return;

		refName.text = f.format;
	}

	private Url? pickFile() {
		FileTypes ft("Source code");
		for (k in progvis:program:Program:supportedFiles)
			ft.add("${k}-source", [k]);

		var picker = FilePicker:open(ft);
		if (!picker.show(this))
			return null;

		picker.result;
	}

	void upload() {
		unless (name.text != "") {
			showMessage(this, "No name", "You need to specify a name for the problem.");
			return;
		}

		unless (impl = loadCode(implName.text)) {
			showMessage(this, "File not found", "The file for the implementation does not exist!");
			return;
		}

		unless (test = loadCode(testName.text)) {
			showMessage(this, "File not found", "The file for the test program does not exist!");
			return;
		}

		unless (ref = loadCode(refName.text)) {
			showMessage(this, "File not found", "The file for the reference implementation does not exist!");
			return;
		}

		progress.visible = true;
		progress.wait();
		for (x in children)
			x.enabled = false;

		ProblemChecks c(checkTermination.checked, checkLeaks.checked, checkBusyWait.checked, checkIgnoreKnownBusyWait.checked);
		AcceptableErrors a(acceptTermination.checked, acceptLeaks.checked, acceptBusyWait.checked);

		(spawn checkCode(name.text, test, impl, ref, c, a)).detach();
	}

	private progvis:net:Code? loadCode(Str path) {
		Url url = parsePath(path);
		if (!url.exists)
			return null;

		Str contents = url.readAllText;
		if (contents.empty)
			return null;

		return progvis:net:Code(contents, url.ext);
	}

	void checkCode(Str name, progvis:net:Code test, progvis:net:Code impl, progvis:net:Code ref,
				ProblemChecks checks, AcceptableErrors acceptable) {
		MemoryProtocol memory;
		Url m = test.put("test", memory);
		Url i = impl.put("impl", memory);
		Url r = ref.put("ref", memory);

		try {
			if (error = checks.check([m, r])) {
				error("The test program does not follow the specification from the reference solution.\nIt produced the following error: ${error.error.type}");
				return;
			}
		} catch (core:lang:CodeError e) {
			error("Error while compiling the reference solution:\n" + e.message, e.pos);
			return;
		}

		Error currentError = Error:unknown();
		try {
			if (error = checks.check([m, i])) {
				currentError = Error:error(error.error.type);
			} else {
				currentError = Error:success();
			}
		} catch (core:lang:CodeError e) {
			error("Error while compiling the implementation:\n" + e.message, e.pos);
			return;
		}

		try {
			client.query(NewProblemRequest(name, impl, test, ref, checks, acceptable, currentError));
		} catch (Exception e) {
			error(e.message);
			return;
		}

		done();
		close(1);
	}

	void error(Str message, core:lang:SrcPos pos) {
		done();
		progvis:CompileErrorDialog(message, pos).show(this);
	}

	void error(Str message) {
		done();
		showMessage(this, "Error", message);
	}

	void done() {
		progress.visible = false;
		for (x in children)
			x.enabled = true;
	}

}


/**
 * Dialog for uploading improved implementations or tests.
 */
dialog SolutionUploadDlg {
	layout Grid {
		expandCol: 1;
		Label status("Checking your solution...") { colspan: 3; }
		nextLine;
		Progress progress { col: 0; colspan: 2; }
		Button uploadButton("Submit") { col: 2; }
	}

	private Client client;
	private Problem original;
	private progvis:net:Code? newTest;
	private progvis:net:Code? newImpl;
	private Request? request;

	// Cancel auto-grading.
	private Bool cancel;

	init(Client client, Problem original, progvis:net:Code? newTest, progvis:net:Code? newImpl) {
		init("Submit your solution", Size(400, 10)) {
			client = client;
			original = original;
			newTest = newTest;
			newImpl = newImpl;
			cancel = false;
		}

		progress.wait();
		uploadButton.enabled = false;
		defaultChoice = uploadButton;
		uploadButton.onClick = &this.upload();

		(spawn checkCode()).detach();
	}

	void onDestroy(Int code) : override {
		cancel = true;
	}

	void upload() {
		unless (request)
			return;

		uploadButton.enabled = false;
		(spawn doUpload(request)).detach();
	}

	private void doUpload(Request request) {
		try {
			client.query(request);
			close(1);
		} catch (Exception e) {
			showMessage(this, "Error", e.message);
			uploadButton.enabled = true;
		}
	}

	void checkCode() {
		MemoryProtocol memory;
		Url m = (if (newTest) { newTest; } else { original.test; }).put("test", memory);
		Url i = (if (newImpl) { newImpl; } else { original.impl; }).put("impl", memory);
		Url r = original.ref.put("ref", memory);

		UiProgress progress(this);

		if (newTest.any) {
			try {
				if (error = original.checks.check([m, r], progress)) {
					error("The test program does not follow the specification of the problem.");
					return;
				}
			} catch (core:lang:CodeError e) {
				if (file = e.pos.file) {
					if (file == m) {
						error("Error while compiling your test program with the reference implementation:\n" + e.message, e.pos);
						return;
					}
				}
				error("Error while compiling your test program with the reference implementation:\n" + e.message);
			}
		}

		if (newImpl) {
			// Check if the new implementation is different from the original one.
			if (newImpl.signature == original.impl.signature) {
				error("You have not modified the implementation. Make some modifications and click \"Reload\" before submitting.");
				return;
			}
		}

		if (cancel)
			return;

		Error currentError = Error:unknown();
		try {
			if (error = original.checks.check([m, i])) {
				currentError = Error:error(error.error.type);
			} else {
				currentError = Error:success();
			}
		} catch (core:lang:CodeError e) {
			error("Error while compiling the implementation:\n" + e.message, e.pos);
			return;
		}

		if (newTest.any) {
			if (error = currentError.error) {
				done("You found an error of type ${error}, click \"Submit\" to proceed!");
			} else {
				error("Your test program did not find an error in the current implementation, try again!");
			}
			size = minSize();
		} else {
			if (error = currentError.error) {
				Bool refuseError = original.testId < 0;
				if (refuseError)
					refuseError = !original.acceptableErrors.check(error);

				if (refuseError) {
					// Require a solution initially.
					error("Your solution does not solve all errors with this test program.\nI found an error of type: ${error}\nYou need to solve this error for the initial problem. Try again!");
				} else {
					// We allow uploading the problems again, but with reduced reward. Thus, one student
					// may solve one issue, but another may solve another issue. We check so that the
					// code is not identical as the first part here.
					done("Your solution does not solve all errors with this test program.\nI found an error of type: ${error}\nYou may still upload your solution, but you will not receive full points for it.");
				}
			} else {
				done("You fixed the current issues, click \"Submit\" to proceed!");
			}
		}

		if (uploadButton.enabled) {
			if (newTest) {
				request = NewTest(original.id, newTest, original.implId, currentError);
			} else if (newImpl) {
				request = NewImplementation(original.id, newImpl, original.testId, currentError);
			}
		}
	}

	class UiProgress extends progvis:check:Progress {
		init(SolutionUploadDlg owner) {
			init { owner = owner; }
		}

		SolutionUploadDlg owner;

		void progress(Nat depth, Nat states, Nat edges, Nat unexplored) : override {
			if (owner.cancel)
				throw progvis:program:ExitError();
		}
	}

	private void error(Str message, core:lang:SrcPos pos) {
		error("Compilation error, try again.");
		progvis:CompileErrorDialog(message, pos).show(this);
	}

	void error(Str message) {
		status.text = message;
		progress.visible = false;
		size = minSize();
	}

	void done(Str message) {
		status.text = message;
		progress.visible = false;
		uploadButton.enabled = true;
		size = minSize();
	}

}