!747 !awt { m { static new L tasks; static new AIEngine engine; !include #1001107 // AIEngine static class Task { S text; Task forTask; S state = "new"; Throwable error; *(S *text) {} boolean runnable() { return false; } void run() {} boolean ok() { return state.equals("done"); } } static class Show extends Task { Find find; *() { super("show java class"); } boolean runnable() { return find.result != null; } void run() { //showText("A Java class", find.result); JTextArea textArea = new JTextArea(find.result); makeFrame("A Java class", new JScrollPane(textArea)); engine.remember(engine.addObject(find.result) + " is visualized in " + engine.addObject(textArea)); } } static class Find extends Task { S result; *() { super("find a java class"); } boolean runnable() { return true; } void run() ctex { result = loadSnippet("#1001027"); // DerivedHashMap S classID = engine.addObject(result); engine.remember("the java class is " + classID); } } static class FindText extends Task { JTextArea textArea; *() { super("find text"); } boolean runnable() { return true; } void run() { textArea = engine.findObjectOfType(JTextArea.class); if (textArea == null) fail("no text area found"); } S getText() { return textArea.getText(); } void updateText(final S text) { awt { textArea.setText(text); } } } static class DeleteFirstLine extends Task { new FindText find; *() { super("delete first line"); tasks.add(find); } boolean runnable() { return find.ok(); } void run() { S text = find.getText(); int i = text.indexOf('\n'); text = i >= 0 ? text.substring(i+1) : ""; find.updateText(text); } } static class DeleteDoubleVariables extends Task { new FindText find; *() { super("delete double variables"); tasks.add(find); } boolean runnable() { return find.ok(); } void run() { S text = find.getText(); text = applyTranslator("#1001045", text); find.updateText(text); } } static class ReplaceTwoDigitNumbersWithNothing extends Task { new FindText find; *() { super("replace bla"); tasks.add(find); } boolean runnable() { return find.ok(); } void run() { S text = find.getText(); text = text.replaceAll("\\d\\d", ""); find.updateText(text); } } static class CountLines extends Task { new FindText find; int result; *() { super("count lines"); tasks.add(find); } boolean runnable() { return find.ok(); } void run() { S text = find.getText(); result = toLines(text).size(); } } static S taskResult(Task t) { return t.state.equals("new") ? "hm" : t.state.equals("done") ? "ok" : t.state.equals("error") ? "oops: " + t.error : "hmhm? " + t.state; // impossible state } p { makeAndroid("This is the Java programming android.", stringfunc { fallback(s, answer(s)) }); } static S fallback(S s, S answer) { if (answer != null) return answer; return "?"; } static synchronized S answer(S s) { if (match3("give me a java class", s) || match3("show me a java class", s)) { new Show show; new Find find; show.find = find; tasks.add(show); tasks.add(find); doStuff(); return taskResult(show); } if (match3("delete first line", s) || match3("delete the first line", s)) { new DeleteFirstLine t; process(t); return taskResult(t); } if (match3("count lines", s)) { new CountLines t; process(t); return t.ok() ? t.result + " line(s)" : taskResult(t); } if (match3("delete double variables", s)) { new DeleteDoubleVariables t; process(t); return taskResult(t); } if (match3("replace two digit numbers with nothing", s)) { new ReplaceTwoDigitNumbersWithNothing t; process(t); return taskResult(t); } return null; } static void process(Task t) { tasks.add(t); doStuff(); } static void showTasks() { for (Task t: tasks) print(t.state + ": " + t.text); } static void doStuff() { while (doStuff1()) {} showTasks(); engine.show(); } static boolean doStuff1() { boolean didAnything = false; for (int i = 0; i < tasks.size(); i++) { Task t = tasks.get(i); if (!t.state.equals("new")) continue; if (t.runnable()) { didAnything = true; try { t.run(); t.state = "done"; } catch (Throwable e) { e.printStackTrace(); t.error = e; t.state = "error"; } } } return didAnything; } // called by match3 static L parse(S s) { return dropPunctuation(javaTokPlusPeriod(s)); } static S applyTranslator(S translatorID, S text) { Class javax = getJavaX(); File X = cast call(javax, "TempDirMaker_make"); saveTextFile(new File(X, "main.java"), text); X = (File) call(javax, "applyTranslator", X, translatorID, "", new ArrayList()); return readTextFile(new File(X, "main.java")); } }