Libraryless. Click here for Pure Java version (17967L/131K).
1 | !7 |
2 | |
3 | !include once #1017126 // transpileRaw |
4 | |
5 | sclass DevelopFromExamples > DynTextArea { |
6 | S whatToTry; |
7 | transient ReliableSingleThread rst = new(r think); |
8 | transient volatile PlanInMotion<Runnable> currentPlan; |
9 | |
10 | transient int delay = 0; //2000; // to see stuff |
11 | |
12 | JComponent visualize() { |
13 | final JLabel lblStatus = jlabel(); |
14 | awtEvery(lblStatus, 100, r { main.setText(lblStatus, planStatus()) }); |
15 | ret withCenteredButtons(centerAndSouthWithMargins( |
16 | jhsplit(super.visualize(), dm_printLogComponent()), |
17 | vstackWithSpacing( |
18 | jsection("Stuff to try", onEnter(dm_fieldTextField('whatToTry), rst)), |
19 | lblStatus |
20 | )), "Fresh", rThread fresh, "Think", rst, "[disabled] Cancel", r cancel); |
21 | } |
22 | |
23 | S planStatus() { |
24 | PlanInMotion plan = currentPlan; |
25 | if (plan == null) ret " "; |
26 | ret "Steps done: " + l(plan.doneSteps) + ". Steps planned: " + l(plan.plannedSteps); |
27 | } |
28 | |
29 | void cancel { |
30 | PlanInMotion plan = currentPlan; |
31 | if (plan != null) plan.cancelled = true; |
32 | } |
33 | |
34 | void fresh { |
35 | dm_refreshTranspiler(); |
36 | forgetTranspiledStandardFunctions(words(whatToTry)); |
37 | loadFunctions_clearCache(); |
38 | rst.trigger(); |
39 | } |
40 | |
41 | void think { |
42 | if (currentPlan != null) ret; |
43 | new PlanInMotion<Runnable> plan; |
44 | currentPlan = plan; |
45 | temp tempAfterwards(r { currentPlan = null; }); |
46 | temp tempEnableButton(findButton(dm_vis(), "Cancel")); |
47 | |
48 | clearPrintLog(); |
49 | loadFunctions_preferDiskCache(); |
50 | |
51 | S text = rtrimAll(getText()); |
52 | Pair<File, Bool> examplesFile = ai_makeExamplesFile(text); |
53 | if (examplesFile.b) print("Wrote: " + f2s(examplesFile.a)); |
54 | final new L<Pair<S>> examples; |
55 | bool eqic = false; |
56 | |
57 | L<S> lines = tlftj(text); |
58 | if (eq(first(lines), "splitAtEmptyLines")) |
59 | lines = splitAtEmptyLines(text); |
60 | |
61 | for (S line : lines) pcall { |
62 | if (eqic(line, "eqic")) eqic = true; |
63 | L<S> l = splitAtDoubleArrow_bothDirections(line); |
64 | if (l(l) == 2) addPair(examples, |
65 | tok_multiLineQuotesToStandardQuotes(first(l)), |
66 | safeCanonicalizeStructure(tok_multiLineQuotesToStandardQuotes(second(l)))); |
67 | } |
68 | |
69 | final File logFile = replaceExtension(examplesFile.a, ".log"); |
70 | final LineBuffer lineBuffer = new(voidfunc(S line) { appendToFile(logFile, line + "\n") }); |
71 | temp tempInterceptPrint(func(S s) -> Bool { lineBuffer.append(s); true; }); |
72 | |
73 | print(localDateWithMilliseconds()); |
74 | print("Thinking about " + n2(examples, "example")); |
75 | |
76 | final new LinkedHashSet<S> thingsThatWork; |
77 | final F2<O, O, Bool> equalsFunction = eqic |
78 | ? func(O a, O b) -> Bool { eqic((S) a, (S) b) } |
79 | : func(O a, O b) -> Bool { eq(a, b) }; |
80 | |
81 | // Make plan |
82 | for (fS s : words(whatToTry)) if (isStandardFunction(s)) plan.add(r { |
83 | sleep(delay); |
84 | F1<S, S> f = func(S in) -> S { struct(callAndMake_orDirect(s, unstruct(in))) }; |
85 | T3<S, S, O> counterExample = worksOnAllExamples_returnCounterExample(f, examples, equalsFunction); |
86 | if (counterExample == null) |
87 | print("WORKS: " + addAndReturn(thingsThatWork, s)); |
88 | else { |
89 | print("Nope: " + s); |
90 | bool exc = counterExample.c instanceof Throwable; |
91 | print((exc ? "Exception on: " |
92 | : "Wrong result on: ") + sfu(counterExample.a)); |
93 | print( "Expected: " + sfu(counterExample.b)); |
94 | if (exc) |
95 | printStackTrace((Throwable) counterExample.c); |
96 | else |
97 | print( " Result: " + sfu(counterExample.c)); |
98 | } |
99 | }); |
100 | |
101 | for (Runnable r : plan.master()) |
102 | callF(r); |
103 | |
104 | print("\n" + (plan.cancelled ? "Cancelled. " : "Done. ") + (empty(thingsThatWork) ? (plan.cancelled ? "Nothing worked so far." : "Nothing works.") : (plan.cancelled ? "Things that worked so far: " : "Things that work: ") + joinWithComma(thingsThatWork))); |
105 | print(); |
106 | } |
107 | } |
Began life as a copy of #1016856
download show line numbers debug dex old transpilations
Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #1017185 |
Snippet name: | Develop From Examples v2 [with plan, OK] |
Eternal ID of this version: | #1017185/22 |
Text MD5: | 6b8cdb6f67402c679cd53388281a1ae5 |
Transpilation MD5: | 5057c43f35b67cae6280ce701fdf2615 |
Author: | stefan |
Category: | javax / a.i. |
Type: | JavaX source code (Dynamic Module) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2018-07-13 18:13:50 |
Source code size: | 3971 bytes / 107 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 391 / 571 |
Version history: | 21 change(s) |
Referenced in: | [show references] |