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
|
use ui;
use layout;
use progvis:net;
use progvis:program;
use core:geometry;
use core:io;
/**
* Behavior for revisiting an already solved problem.
*/
class RevisitBehavior extends DefaultBehavior {
private Str title;
init(MainWin window, Str title) {
init(window) { title = title; }
}
Str? allowSpawnThread() : override { "Can not spawn new threads this way when solving problems."; }
Str? allowReload() : override { "Can not reload the current problem. Use Run -> Restart instead, or open a new problem."; }
Str? allowModelCheck() : override { "The model checker is disabled while looking at other solutions."; }
Bool allowTrackMemory() : override { false; }
ProblemPanel? panel() : override {
ProblemPanel("Find an error in \"${title}\" (can not submit a solution)");
}
}
/**
* Behavior asking the student to find and correct an error in the implementation.
*/
class DebugProblemBehavior extends Behavior {
private MainWin window;
private Client client;
private Problem problem;
private Url testCode;
private Url implCode;
private StrBuf operations;
private Bool done;
init(MainWin window, Client client, problems:Action action) {
init() {
window = window;
client = client;
problem = action.problem;
testCode = action.testCode;
implCode = action.implCode;
}
}
ProblemPanel? panel() : override {
ProblemPanel p("Find an error in \"${problem.title}\"!");
p.button("Restart", &this.onRestartClicked());
p.button("Open test", &this.onOpenMain());
p.button("Open implementation", &this.onOpenImpl());
p;
}
private void onOpenImpl() {
(spawn window.openEditor(implCode)).detach();
}
private void onOpenMain() {
(spawn window.openEditor(testCode)).detach();
}
private void onRestartClicked() {
(spawn window.onRestart()).detach();
}
void onProgramError(ProgramError error) : override {
if (done)
return;
done = true;
(spawn submitError(error)).detach();
}
private void submitError(ProgramError error) {
(spawn submitAttempt(operations.toS, error.type)).detach();
window.updateBehavior(ImproveBehavior(window, client, problem, testCode, implCode, error.type));
}
void onUserAction(Str action) : override {
operations << action << ";";
}
void onRestart() : override {
// Save the solution!
Str ops = operations.toS();
if (ops.any) {
(spawn submitAttempt(ops, null)).detach();
}
operations = StrBuf();
}
private void submitAttempt(Str operations, Str? error) {
client.query(NewAttemptRequest(problem.impl.id, problem.test.id, operations, error));
}
Str? allowSpawnThread() : override { "Can not spawn new threads this way when solving problems."; }
Str? allowModelCheck() : override { "Try to find the error yourself using the step buttons for each thread."; }
Str? allowReload() : override { "Can not reload the current problem. Use Run -> Restart instead, or open a new problem."; }
Bool allowTrackMemory() : override { false; }
}
/**
* Shared base for ImproveBehavior and TestBehavior.
*/
class UploadableBehavior extends DefaultBehavior {
protected Client client;
protected Problem problem;
protected Url testCode;
protected Url implCode;
protected Bool submitted;
init(MainWin window, Client client, Problem problem, Url testCode, Url implCode) {
init(window) {
client = client;
problem = problem;
testCode = testCode;
implCode = implCode;
submitted = false;
}
}
// We encourage reloads!
Str? allowReload() : override { null; }
// Don't allow spawning threads arbitrarily.
Str? allowSpawnThread() : override { "Can not spawn new threads this way when solving problems."; }
// Don't allow model checking, that would be too easy.
Str? allowModelCheck() : override { "The model checker is disabled for online problems."; }
// Don't change 'track memory'.
Bool allowTrackMemory() : override { false; }
// Add a panel that explains it all.
ProblemPanel? panel() : override {
ProblemPanel p(panelText());
p.button("Open test", &this.onOpenMain());
p.button("Open impl.", &this.onOpenImpl());
p.button("Reload", &this.onReload());
p.button("Submit", &this.onSubmit());
p;
}
// Title for the panel.
protected Str panelText() : abstract;
private void onSubmit() {
// Avoid double submissions.
if (submitted)
return;
submitted = true;
(spawn doSubmitHelp()).detach();
}
private void doSubmitHelp() {
// Possibly re-set 'submitted' if the user cancelled the dialog.
submitted = doSubmit();
}
// Returns true if submission suceeded.
protected Bool doSubmit() : abstract;
private void onReload() {
// Needed due to a bug in App on Windows.
(spawn window.onReload()).detach();
}
private void onOpenImpl() {
(spawn doOpenImpl()).detach();
}
protected void doOpenImpl() {
window.openEditor(implCode);
}
private void onOpenMain() {
(spawn doOpenMain()).detach();
}
protected void doOpenMain() {
window.openEditor(testCode);
}
}
/**
* Behavior that lets the user modify the problem and submit an improved version.
*/
class ImproveBehavior extends UploadableBehavior {
init(MainWin window, Client client, Problem problem, Url testCode, Url implCode, Str errorType) {
init(window, client, problem, testCode, implCode) {}
StrBuf msg;
msg << "You found a problem of type: " << errorType << "\n\n";
msg << "The next step is to fix the problem you found:\n";
msg << "1. Select \"Open impl.\" if you have not done so already.\n";
msg << "2. Modify the code and save the file in your editor.\n";
msg << "3. Select \"Reload\" to load your changes and see if they work.\n";
msg << "4. Repeat steps 2-3 as necessary.\n";
msg << "5. Click \"Submit\" in the green box when you are done.\n";
msg << "Note: You can only modify the implementation, any chainges to the test file will be ignored.\n";
showMessage(window, "Improve the code", msg.toS);
}
protected Str panelText() : override {
"Try to fix the problem you found!";
}
protected Bool doSubmit() : override {
Code implCode(window.currentSource(), problem.impl.language);
progvis:problems:SolutionUploadDlg dlg(client, problem, null, implCode);
if (dlg.show(window) >= 0) {
window.updateBehavior(DefaultBehavior(window));
return true;
}
return false;
}
protected void doOpenMain() {
showMessage(window, "Information", "Note: You can not modify the test program in this problem. Any changes you make will be ignored.");
super:doOpenMain();
}
}
/**
* Behavior similar to "ImproveBehavior" above, but that modifies the main file.
*/
class TestBehavior extends UploadableBehavior {
init(MainWin window, Client client, Problem problem, Url testCode, Url implCode) {
init(window, client, problem, testCode, implCode) {}
(spawn infoMessage()).detach();
}
private void infoMessage() {
sleep(200 ms);
StrBuf msg;
msg << "This implementation currently works as expected for the current test\n";
msg << "program, but that does not mean the implementation is correct. Modify\n";
msg << "the test program so that it finds a bug in the implementation:\n";
msg << "1. Select \"Open test\" to show the test program.\n";
msg << "2. Edit and save the file in your editor.\n";
msg << "3. Select \"Reload\" to load your changes and see what happens.\n";
msg << "4. Repeat steps 2-3 as necessary.\n";
msg << "5. Click \"Submit\" in the green box when you are done.\n";
showMessage(window, "Improve the test program", msg.toS);
}
protected Str panelText() : override {
"Improve the test program to find a bug!";
}
protected Bool doSubmit() : override {
Code testCode(window.currentSource(), problem.test.language);
progvis:problems:SolutionUploadDlg dlg(client, problem, testCode, null);
if (dlg.show(window) >= 0) {
window.updateBehavior(DefaultBehavior(window));
return true;
}
return false;
}
protected void doOpenImpl() : override {
showMessage(window, "Information", "Note: You can not modify the implementation in this problem. Any changes you make will be ignored.");
super:doOpenImpl();
}
}
|