Download Jar. Uses 3874K of libraries. Click here for Pure Java version (15523L/114K).
1 | !7 |
2 | |
3 | // class AIConcept comes from #1007160 |
4 | |
5 | concept ImportedFrom { |
6 | S snippetID; |
7 | long lastTime; |
8 | } |
9 | |
10 | static JTable table; |
11 | static JCheckBox cbGrabImages; |
12 | volatile sbool grabImages; |
13 | volatile static double imageGrabberDelay = 1; // seconds, min 1 |
14 | static Q uploadQ; |
15 | |
16 | p { |
17 | grabImages = headless(); |
18 | dbIndexing(AIConcept, 'searchedForSuggestedImage); |
19 | framesBot(); // bot to activate my frames |
20 | methodsBot("AI Concepts Import.", "importFrom", "importFromAI1Lol", "export"); |
21 | uploadQ = new Q(true); |
22 | startImageGrabber(); |
23 | if (isHeadless()) ret; |
24 | |
25 | substance(); |
26 | swingMain(); |
27 | aiEnhancements(); |
28 | } |
29 | |
30 | svoid swingMain swing { |
31 | showConceptsTable_postProcess.set(f reversedList); |
32 | table = makeConceptsTable(AIConcept, func(AIConcept c) { |
33 | litorderedmap( |
34 | "Concept ID" := c.globalID, |
35 | "Name/Description" := c.name, |
36 | "Comments" := c.comment, |
37 | "Has Image?" := c.pngFile.has(), |
38 | //"Imported From" := c.importedFrom, |
39 | "Originating Universe" := c.originatingUniverse, |
40 | "Exportable" := c.exportable) |
41 | }); |
42 | showFrame(makeTitle(), tableWithSearcher(table)); |
43 | onEnter(tableSearchField(table), f newConcept); |
44 | onDoubleClick(table, r { editConcept(selectedConcept()) }); |
45 | addToWindow(table, withMargin(vstack( |
46 | jRightAlignedLine( |
47 | jbutton("New Concept...", f newConcept), |
48 | tableDependButton(table, "Edit", r { editConcept(selectedConcept()) }), |
49 | tableDependButton(table, "Duplicate", r { newConcept(selectedConcept()) }), |
50 | jbutton("Export", f doExport), |
51 | jbutton("Import...", f doImport)), |
52 | withTopMargin(jRightAlignedLine(cbGrabImages = jCheckBox("Google For Images", grabImages)))))); |
53 | |
54 | tablePopupMenu(table, voidfunc(JPopupMenu menu, final int row) { |
55 | final S globalID = (S) getTableCell(table, row, 0); |
56 | final AIConcept c = findConcept(AIConcept, +globalID); |
57 | addMenuItem(menu, "Edit...", r { editConcept(c) }); |
58 | addMenuItem(menu, "Delete", r { |
59 | withDBLock(r { deleteConcept(c) }); |
60 | }); |
61 | if (c.searchedForSuggestedImage) |
62 | addMenuItem(menu, "Search for image again", r { |
63 | deleteConcept(c.suggestedImage); |
64 | cset(c, suggestedImage := null, searchedForSuggestedImage := false) |
65 | }); |
66 | }); |
67 | |
68 | tableEnableTextDrag(table); |
69 | |
70 | awtOnConceptChanges(table, 1000, r { |
71 | setFrameTitle(table, makeTitle()) |
72 | }); |
73 | |
74 | addAISuiteMenu(table, "AI"); |
75 | hideConsole(); |
76 | } |
77 | |
78 | svoid editConcept(final AIConcept c) { |
79 | if (c == null) ret; |
80 | final JTextField a = jTextField(c.name); |
81 | final JTextField b = jTextField(c.comment); |
82 | final JTextField cc = jTextField(c.globalID); |
83 | final ImageChooser ic = new ImageChooser(c.pngFile.has() ? c.pngFile->getImageSafe() : null); |
84 | ic.setTitleForUpload(c.name); |
85 | PNGFile png = c.suggestedImage!; |
86 | final ImageChooser ic2 = new ImageChooser(png != null ? png.getImageSafe() : null); |
87 | final Var<S> md5 = new Var(md5OfPNGFile(png)); |
88 | awtOnConceptChanges(ic2, r { |
89 | ic2.setTitleForUpload(c.name); |
90 | PNGFile png = c.suggestedImage!; |
91 | S newMD5 = md5OfPNGFile(png); |
92 | if (png != null && neq(md5.get(), newMD5)) { |
93 | md5.set(newMD5); |
94 | ic2.setImage(png.getImageSafe()); |
95 | } |
96 | }, true); |
97 | Runnable r = r { |
98 | BufferedImage image = ic.getImage(); |
99 | if (image == null && c.pngFile.has()) { |
100 | //c.pngFile->delete(); |
101 | c.pngFile.clear(); |
102 | cset(c, imageMD5 := null); |
103 | } else if (image != null && |
104 | (!c.pngFile.has() || !imagesIdentical(c.pngFile->getImage(), image))) { |
105 | //if (c.pngFile.has()) c.pngFile->delete(); |
106 | setConceptImage(c, image); |
107 | } |
108 | cset(c, touched := now(), |
109 | name := getTextTrim(a), comment := getTextTrim(b), |
110 | globalID := getTextTrim(cc)); |
111 | }; |
112 | O[] params = { |
113 | "Description/Name", a, |
114 | "Comment (optional)", b, |
115 | "Global ID", cc, |
116 | "Example Image (optional)", |
117 | westAndEast(ic, withLeftMargin(20, northAndCenter(vstack( |
118 | jbutton("Accept", r { |
119 | ic.setImage(ic2.getImage()); |
120 | findButton(getFrame(ic), "Save").doClick(); |
121 | }), |
122 | jCenteredLabel("Suggested image:")), ic2))), |
123 | r }; |
124 | //for i over params: print("#" + i + ": " + params[i]); |
125 | renameSubmitButton("Save", showFormTitled("Edit concept " + c.globalID, params)); |
126 | } |
127 | |
128 | sS searcherInput() { |
129 | ret getTextTrim(tableSearchField(table)); |
130 | } |
131 | |
132 | svoid newConcept { newConcept(null); } |
133 | |
134 | svoid newConcept(final AIConcept c) { |
135 | final JTextField a = jTextField(c == null ? searcherInput() : c.name); |
136 | final JTextField b = jTextField(getString(c, "comment")); |
137 | final JTextField cc = jTextField(aGlobalID()); |
138 | final JLabel lbl = jCenteredLabel("Suggested image:"); |
139 | final new ImageChooser ic; |
140 | final new ImageChooser ic2; |
141 | final new Var<S> md5; |
142 | |
143 | awtCalcRegularly(ic2, 500, 0, new Runnable { |
144 | S lastTyped, lastSearched; |
145 | |
146 | public void run { |
147 | S s = getTextTrim(a); |
148 | if (nempty(s) && eq(s, lastTyped) && neq(s, lastSearched)) { |
149 | lastSearched = s; |
150 | lbl.setIcon(imageIcon(#1007255)); |
151 | try { |
152 | final BufferedImage img = quickVisualize(s); |
153 | if (img != null) swing { ic2.setImage(img); } |
154 | } finally { |
155 | lbl.setIcon(null); |
156 | } |
157 | } |
158 | lastTyped = s; |
159 | } |
160 | }); |
161 | Runnable r = r { |
162 | AIConcept c = cnew(AIConcept, touched := now(), |
163 | name := getTextTrim(a), comment := getTextTrim(b), globalID := getTextTrim(cc)); |
164 | setConceptImage(c, ic.getImage()); |
165 | }; |
166 | renameSubmitButton("Create", showFormTitled("New concept", |
167 | "Description/Name", a, |
168 | "Comment (optional)", b, |
169 | "Global ID", cc, |
170 | "Example Image (optional)", |
171 | westAndEast(ic, withLeftMargin(20, northAndCenter(vstack( |
172 | jbutton("Accept", r { ic.setImage(ic2.getImage()) }), |
173 | lbl), ic2))), |
174 | r)); |
175 | } |
176 | |
177 | static AIConcept selectedConcept() { |
178 | ret findConcept(AIConcept, globalID := selectedTableCell(table, 0)); |
179 | } |
180 | |
181 | svoid doImport { |
182 | ImportedFrom last = last(sortedByField("lastTime", list(ImportedFrom))); |
183 | final JTextField tf = jTextField(last == null ? #1007208 : last.snippetID); |
184 | showFormTitled("Import A. I. Concepts", |
185 | "Snippet ID", tf, r-thread { |
186 | loading { |
187 | importFrom(getTextTrim(tf)); |
188 | } |
189 | }); |
190 | } |
191 | |
192 | svoid importFromAI1Lol { |
193 | importFrom(freshConceptsFromAI1Lol(), "ai1.lol"); |
194 | } |
195 | |
196 | svoid importFrom(S snippetID) { |
197 | snippetID = fsI(snippetID); |
198 | cset(uniq(ImportedFrom, +snippetID), lastTime := now()); |
199 | S text = loadSnippet(snippetID); |
200 | Concepts newConcepts = new Concepts(snippetID).safeLoad(text); |
201 | importFrom(newConcepts, snippetID); |
202 | } |
203 | |
204 | svoid importFrom(Concepts newConcepts, S snippetID) { |
205 | print(n(list(newConcepts, "AIConcept"), "concept") + " loaded from " + snippetID); |
206 | |
207 | Concepts myConcepts = mainConcepts; |
208 | print("I have " + n(list(myConcepts, "AIConcept"), "concept"); |
209 | Map<S, AIConcept> myIDs = indexByField(list(myConcepts, AIConcept), "globalID"); |
210 | |
211 | int nNew = 0; |
212 | for (Concept c : list(newConcepts, "AIConcept")) { |
213 | S id = getString(c, "globalID"); |
214 | bool has = myIDs.containsKey(id); |
215 | print("ID: " + id + " - " + (has ? "I have it" : "I don't have it")); |
216 | if (!has) { |
217 | // Creating |
218 | |
219 | AIConcept c2 = cnew(AIConcept, importedFrom := snippetID, imported := now()); |
220 | importFields(c, c2); |
221 | myIDs.put(id, c2); |
222 | ++nNew; |
223 | } else { |
224 | // Updating |
225 | |
226 | AIConcept c2 = myIDs.get(id); |
227 | if (c2.touched > c2.imported) |
228 | print(" Concept touched locally! Not changing."); |
229 | else { |
230 | long time = now(); |
231 | cset(c2, imported := time, touched := time); |
232 | int changes = importFields(c, c2); |
233 | if (changes == 0) |
234 | print(" No change."); |
235 | else |
236 | print(" Updated concept! " + n(changes, "fields") + " changed"); |
237 | } |
238 | } |
239 | } |
240 | infoBox("Import done. " + n(nNew, "new concepts")); |
241 | } |
242 | |
243 | static int importFields(Concept c, AIConcept c2) { |
244 | c2.created = c.created; |
245 | ret copyConceptFields(c, c2, "globalID", "name", "comment", "imageMD5", "originatingUniverse"); |
246 | } |
247 | |
248 | svoid doExport { |
249 | swingExportConcepts("AI Concepts"); |
250 | } |
251 | |
252 | svoid export { |
253 | swingExportConcepts_impl("AI Concepts"); |
254 | } |
255 | |
256 | sS makeTitle() { |
257 | ret "My AI Concepts [" + countConcepts(AIConcept) + "]"; |
258 | } |
259 | |
260 | svoid startImageGrabber { |
261 | thread "Image Grabber" { |
262 | repeat with sleep imageGrabberDelay { |
263 | if (headless() ? grabImages : isChecked(cbGrabImages)) { |
264 | AIConcept c = findConcept(AIConcept, searchedForSuggestedImage := false); |
265 | if (c != null) { |
266 | cset(c, searchedForSuggestedImage := true); |
267 | if (c.suggestedImage != null || c.importedFrom != null) continue; |
268 | S query = c.name; |
269 | //Window box = infoBox("Visualizing: " + c.name); |
270 | BufferedImage img = quickVisualize(query); |
271 | //disposeWindow(box); |
272 | if (img != null) { |
273 | showAnimation(img, c.name, 4); |
274 | cset(c, suggestedImage := new PNGFile(quickVisualize_imagePath(query)), suggestedImageMD5 := md5OfBufferedImage(img)); |
275 | } else |
276 | infoBox("No image found"); |
277 | } |
278 | } |
279 | } |
280 | } |
281 | } |
282 | |
283 | svoid setConceptImage(final AIConcept c, final BufferedImage img) { |
284 | if (img == null) { |
285 | c.pngFile.clear(); |
286 | cset(c, imageMD5 := null); |
287 | } else { |
288 | c.pngFile.set(new PNGFile(img)); |
289 | cset(c, imageMD5 := c.pngFile->md5()); |
290 | uploadQ.add(r { uploadToImageServerIfNotThere(img, aiConceptToString(c)) }); |
291 | } |
292 | } |
Began life as a copy of #1006454
download show line numbers debug dex old transpilations
Travelled to 23 computer(s): afbdgwpgxusd, aoiabmzegqzx, bhatertpkbcr, bvmoasoxxqgd, cbybwowwnfue, cfunsshuasjs, fehiwqupcyrn, gwrvuhgaqvyk, ishqpsrjomds, jtubtzbbkimh, lpdgvwnxivlt, meaugxdlecrj, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, sawdedvomwva, sdspifkayxrz, tslmcundralx, tvejysmllsmz, vouqrxazstgt, wtqryiryparv, xeobevbjagfg, xinetxnxrdbb
No comments. add comment
Snippet ID: | #1006463 |
Snippet name: | My AI Concepts [WORKS] |
Eternal ID of this version: | #1006463/136 |
Text MD5: | ab3f5d229fb5b7e668af270cea124e58 |
Transpilation MD5: | 48b377c6af15c1c91d5dd89b866ed6aa |
Author: | stefan |
Category: | javax / a.i. |
Type: | JavaX source code (desktop) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2017-10-25 19:41:50 |
Source code size: | 9562 bytes / 292 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 1151 / 7075 |
Version history: | 135 change(s) |
Referenced in: | [show references] |