File: action.bs

package info (click to toggle)
storm-lang 0.7.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 51,836 kB
  • sloc: ansic: 261,420; cpp: 138,870; sh: 14,877; perl: 9,846; python: 2,525; lisp: 2,504; asm: 860; makefile: 678; pascal: 70; java: 52; xml: 37; awk: 12
file content (140 lines) | stat: -rw-r--r-- 3,396 bytes parent folder | download | duplicates (4)
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
use ui;
use core:geometry;
use core:io;
use progvis:net;

/**
 * Action to take when the ProblemsDlg is closed.
 *
 * Allows creating a suitable behavior for the main Ui.
 */
class Action on Ui {
	// The problem to solve.
	Problem problem;

	// Code for the implementation on disk.
	Url implCode;

	// Code for the test program on disk.
	Url testCode;

	// Create.
	init(progvis:Settings settings, Problem problem) {
		init {
			problem = problem;
			implCode = saveCode(settings, problem, "", problem.impl);
			testCode = saveCode(settings, problem, "_test", problem.test);
		}
	}

	// Create a behavior.
	progvis:Behavior behavior(progvis:MainWin window, Client client) : abstract;

	// Return a list of files to open.
	Url[] files() {
		[testCode, implCode];
	}

	// Save source code to disk, and retrieve an URL for it.
	private Url saveCode(progvis:Settings settings, Problem problem, Str suffix, Code code) : static {
		Str title = "${problem.id,f06}";
		if (problem.implId >= 0)
			title += "_p${problem.implId,f03}";
		if (problem.testId >= 0)
			title += "_t${problem.testId,f03}";

		Url file = settings.downloadFile("${title}${suffix}.${code.language}");
		Utf8Output out(file.write());
		out.autoFlush = false;
		out.write(code.src);
		out.flush();
		out.close();
		return file;
	}
}


/**
 * Action to debug a problem.
 */
class DebugAction extends Action {
	init(progvis:Settings settings, Problem problem) {
		init(settings, problem) {}
	}

	Url[] files() : override {
		// To prevent modifications to the test code, we put it in an in-memory store.
		MemoryProtocol mem;
		Url test = problem.test.put("test", mem);
		[implCode, test];
	}

	progvis:Behavior behavior(progvis:MainWin window, Client client) : override {
		return progvis:DebugProblemBehavior(window, client, this);
	}
}


/**
 * Action to submit a new problem, skips the "find error" part.
 */
class ImplAction extends Action {
	init(progvis:Settings settings, Problem problem) {
		init(settings, problem) {}
	}

	Url[] files() : override {
		// To prevent modifications to the test code, we put it in an in-memory store.
		MemoryProtocol mem;
		Url test = problem.test.put("test", mem);
		[implCode, test];
	}

	progvis:Behavior behavior(progvis:MainWin window, Client client) : override {
		Str error = if (error = problem.error.error) {
			error;
		} else {
			"<none, admin action>";
		};
		return progvis:ImproveBehavior(window, client, problem, testCode, implCode, error);
	}
}


/**
 * Test a problem (change the main function).
 */
class TestAction extends Action {
	init(progvis:Settings settings, Problem problem) {
		init(settings, problem) {}
	}

	Url[] files() : override {
		// To prevent modifications to the implementation, we put it in an in-memory store.
		MemoryProtocol mem;
		Url impl = problem.impl.put("impl", mem);
		[testCode, impl];
	}

	progvis:Behavior behavior(progvis:MainWin window, Client client) : override {
		return progvis:TestBehavior(window, client, problem, testCode, implCode);
	}
}


/**
 * Show a solution.
 */
// class ShowSolutionAction extends Action {
// 	Str solution;

// 	init(progvis:Settings settings, Problem problem, Solution solution) {
// 		init(settings, problem) {
// 			solution = solution.steps;
// 		}
// 	}

// 	progvis:Behavior behavior(progvis:MainWin window, Client client) : override {
// 		return progvis:ReplayBehavior(window, solution);
// 	}
// }