Transpiled version (38931L) is out of date.
1 | srecord noeq G22ChallengePanel(G22Challenge challenge) extends HasChangeListeners is Swingable { |
2 | settableWithChange long randomSeed = 1; //randomUnsignedInt(); |
3 | |
4 | //transient JPanel imagesPanel = jline(); |
5 | transient DynamicHStack imagesPanel; |
6 | transient L<Entry> entries = syncL(); |
7 | transient JComponent imagesSection; |
8 | transient GazelleV_LeftArrowScript.Script parsedScript; |
9 | settableWithChange Set<Class> solutionTypes = syncSet(); |
10 | transient JButton btnShowSolutions; |
11 | transient G22LAScriptIDE<G22RecognizerScript> recognizerIDE; |
12 | |
13 | transient settable G22Utils g22utils; |
14 | settable bool autoRunChallenge; |
15 | settable double timeoutToMakeAnImage = 10.0; |
16 | settable double timeoutToRecognizeAnImage = 10.0; |
17 | settable int imagesToPaint = 3; |
18 | settable int interval = 100; |
19 | settable int imageDisplaySize = 100; |
20 | settable Color imageBorderColor1 = Color.green; |
21 | settable Color imageBorderColor2 = Color.red; |
22 | settable int imageBorderSize = 1; |
23 | |
24 | void init { |
25 | recognizerIDE = new G22LAScriptIDE(g22utils); |
26 | //recognizerIDE.sectionTitle("Recognizer " + roundBracket(recognizerIDE.sectionTitle)); |
27 | |
28 | recognizerIDE.modifyIDE = ide -> { |
29 | ide.runScript = -> { |
30 | var compileResult = ide.freshCompileResult(); |
31 | var script = compileResult.parsedScript; |
32 | if (script == null) ret; |
33 | |
34 | var bg = g22utils.backgroundProcessesUI(); |
35 | temp bg?.tempAdd("Run recognizer on " + challenge); |
36 | |
37 | new FlexibleVarContext ctx; |
38 | |
39 | try { |
40 | L<OKOrError> results = syncL(); |
41 | long time = sysNow(); |
42 | for (Entry entry : filter(entries, e -> e.image != null)) { |
43 | ctx.put(image := entry.image); |
44 | var result = okOrError(-> |
45 | ide.callCompiledObjectWithTimeout( |
46 | timeoutToRecognizeAnImage, |
47 | script, ctx)); |
48 | results.add(result); |
49 | entry.computerResult(result); |
50 | } |
51 | time = sysNow()-time; |
52 | ide.showScriptResult(OKOrError.ok("Got " + nResults(results) + " in " + formatSeconds(time, 3) + " s")); |
53 | } catch print e { |
54 | ide.showScriptResult(OKOrError.error(e)); |
55 | } |
56 | }; |
57 | }; |
58 | } |
59 | |
60 | visualize { |
61 | if (recognizerIDE == null) init(); |
62 | |
63 | imagesPanel = (DynamicHStack) dynamicHStack().spacing(20); |
64 | awtCalcEvery(imagesPanel, interval, r _paintAnImage); |
65 | |
66 | ret withCenteredBoldTitle("Challenge: " + challenge, |
67 | withTopMargin(jvsplit( |
68 | northCenterAndSouthWithMargin( |
69 | centerAndEastWithMargin( |
70 | centeredLiveValueLabel( |
71 | liveValue_hasChangeListeners(this, |
72 | -> "Looking for: " + solutionTypeDesc())), |
73 | swing(-> JPopDownButton(menu -> |
74 | menu.add(jMenuItem("Random seed " + n2(randomSeed))))) |
75 | ), |
76 | imagesSection = jCenteredRaisedSection("Images", jBorderlessHigherScrollPane(jfullcenter(imagesPanel))), |
77 | withBottomMargin(jCenteredLine(btnShowSolutions = jbutton("Show solutions", l0 showSolutions))) |
78 | /*jRightAlignedLine( |
79 | withLabel("Random seed:", |
80 | liveValueLabel(liveValue_hasChangeListeners(this, -> randomSeed)))),*/ |
81 | ), |
82 | recognizerIDE.visualize() |
83 | ))); |
84 | } |
85 | |
86 | void _paintAnImage { |
87 | if (!autoRunChallenge) ret; |
88 | if (l(entries) >= imagesToPaint) ret; |
89 | |
90 | int index = l(entries)+1; |
91 | Entry entry = new(index, (int) (randomSeed+index-1)); |
92 | entries.add(entry); |
93 | |
94 | try { |
95 | if (parsedScript == null) { |
96 | var parser = g22utils.leftArrowParser(); |
97 | var cleared = challenge.clearedForAutoRun(); |
98 | parsedScript = parser.parse(cleared?!); |
99 | } |
100 | |
101 | new FlexibleVarContext ctx; |
102 | var returnedObject = evalWithTimeoutOrTypedException(timeoutToMakeAnImage, -> { |
103 | temp tempSetRandomSeed((int) entry.seed); |
104 | ret parsedScript.get(ctx); |
105 | }); |
106 | |
107 | // can return image or put in variables "image" or "img" |
108 | |
109 | if (returnedObject == null) |
110 | returnedObject = ctx.get("image"); |
111 | if (returnedObject == null) |
112 | returnedObject = ctx.get("img"); |
113 | |
114 | // convert to BufferedImage |
115 | |
116 | if (returnedObject instanceof MakesBufferedImage |
117 | || returnedObject instanceof Image) |
118 | returnedObject = toBufferedImage(returnedObject); |
119 | |
120 | if (returnedObject cast BufferedImage) { |
121 | entry.solution(ctx.get("solution")); |
122 | updateSolutionType(entry.solution); |
123 | addImage(entry, returnedObject); |
124 | } else if (returnedObject == null) |
125 | addImage(entry, null, jcenteredlabel("No image"); |
126 | else |
127 | addImage(entry, null, jcenteredlabel("Not an image: " + shortClassName(returnedObject))); |
128 | } catch print e { |
129 | entry.error(e); |
130 | addImage(entry, null, jSmallErrorView(e)); |
131 | } finally { |
132 | entry.calculating(false); |
133 | } |
134 | } |
135 | |
136 | void addImage(Entry entry, BufferedImage image, JComponent component) { |
137 | entry.image(image).component(component); |
138 | addAndRevalidate(imagesPanel, component); |
139 | setSectionTitle(imagesSection, firstToUpper(nImages(entries)) + " calculated"); |
140 | } |
141 | |
142 | void addImage(Entry entry, BufferedImage image) { |
143 | entry.image(image); |
144 | addImage(entry, image, new EntryComponent(entry).visualize()); |
145 | } |
146 | |
147 | record noeq EntryComponent(Entry entry) is Swingable { |
148 | // !customConstructor |
149 | *(Entry *entry) { entry.entryComponent(this); } |
150 | |
151 | SingleComponentPanel scpSolution = singleComponentPanel(); |
152 | SingleComponentPanel scpComputerResult = singleComponentPanel(); |
153 | SingleComponentPanel scpCheckmark = singleComponentPanel(); |
154 | |
155 | visualize { |
156 | var image = entry.image; |
157 | double scale = calcFitScale(image, imageDisplaySize); |
158 | var image2 = scaleImageWithOp(image, scale, |
159 | AffineTransformOp.TYPE_NEAREST_NEIGHBOR); |
160 | for (color : ll(imageBorderColor1, imageBorderColor2)) |
161 | image2 = addBorderToImage(image2, color, imageBorderSize); |
162 | S toolTip = "Image " + n2(entry.index) + " of " + challenge + " (seed " + n2(entry.seed) + ", " + image.getWidth() + "*" + image.getHeight() + " px)"; |
163 | JButton b = withToolTip(toolTip, jimageButton(image2)); |
164 | setMargin(10, b); |
165 | ret centerAndSouthWithMargin(b, |
166 | vstackWithSpacing(scpCheckmark, scpSolution, scpComputerResult)); |
167 | } |
168 | |
169 | JComponent answerToComponent(O answer) { |
170 | JComponent c; |
171 | try { |
172 | c = jCenteredLabel(shorten(g22utils.stringify(answer))); |
173 | } catch e { |
174 | c = jSmallErrorView(e); |
175 | } |
176 | ret jMaxWidth(imageDisplaySize+20, c); |
177 | } |
178 | |
179 | void showSolution { |
180 | scpSolution.set(withCenteredTitle("Solution", answerToComponent(entry.solution))); |
181 | } |
182 | |
183 | void computerResult(OKOrError result) { |
184 | scpComputerResult.set(withCenteredTitle("Computer Result", |
185 | result.isOK() |
186 | ? answerToComponent(result) |
187 | : jSmallErrorView(result.error()))); |
188 | |
189 | scpCheckmark.set(entry.computerIsCorrect() ? jImage_scaledToHeight(32, checkmarkIconID(), "Computer is correct!!") : null); |
190 | } |
191 | } |
192 | |
193 | class Entry { |
194 | int index; // index in sequence starting with 1 |
195 | long seed; // seed for randomizer |
196 | settable BufferedImage image; |
197 | settable Throwable error; |
198 | settable O solution; |
199 | OKOrError computerResult; |
200 | settable bool calculating = true; |
201 | settable JComponent component; |
202 | settable EntryComponent entryComponent; |
203 | settable bool computerIsCorrect; |
204 | |
205 | *(int *index, long *seed) {} |
206 | |
207 | void computerResult(OKOrError result) { |
208 | this.computerResult = result; |
209 | computerIsCorrect(result.isOK() && eq(solution, result!)); |
210 | entryComponent?.computerResult(result); |
211 | } |
212 | } |
213 | |
214 | void updateSolutionType(O solution) { |
215 | Class c = _getClass(solution); |
216 | if (solutionTypes.add(c)) change(); |
217 | } |
218 | |
219 | S solutionTypeDesc() { |
220 | LS types = map solutionTypeDesc(solutionTypes); |
221 | ret textOut_or(types, "Calculating..."); |
222 | } |
223 | |
224 | S solutionTypeDesc(Class c) { |
225 | if (c == null) |
226 | ret "no solution given"; |
227 | if (c == Rect) |
228 | ret "a rectangular area in the image"; |
229 | ret aOrAn(shortClassName(c)); |
230 | } |
231 | |
232 | void showSolutions { |
233 | disableButton(btnShowSolutions); |
234 | for (e : cloneList(entries)) |
235 | if (e.entryComponent != null) |
236 | e.entryComponent.showSolution(); |
237 | } |
238 | } |
download show line numbers debug dex old transpilations
Travelled to 4 computer(s): bhatertpkbcr, ekrmjmnbrukm, mowyntqkapby, mqqgnosmbjvj
No comments. add comment
Snippet ID: | #1034214 |
Snippet name: | G22ChallengePanel |
Eternal ID of this version: | #1034214/110 |
Text MD5: | a6aa7a0a26042d15fe1348c9f60e7ebe |
Author: | stefan |
Category: | javax / gazelle v |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2022-04-28 22:47:05 |
Source code size: | 8554 bytes / 238 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 368 / 899 |
Version history: | 109 change(s) |
Referenced in: | [show references] |