Uses 911K of libraries. Click here for Pure Java version (18752L/102K).
| 1 | !7 | 
| 2 | |
| 3 | concept RewriteExample { | 
| 4 | S in, out; | 
| 5 | O trail; | 
| 6 | } | 
| 7 | |
| 8 | concept Result { | 
| 9 | S in, out; | 
| 10 | O trail; | 
| 11 | RewriteExample existing; | 
| 12 | } | 
| 13 | |
| 14 | cmodule RewriteDatabase { | 
| 15 | transient CRUD<RewriteExample> rewriteExamplesCRUD; | 
| 16 | transient CRUD<Result> resultsCRUD; | 
| 17 | transient SingleComponentPanel scpSuggestions; | 
| 18 | transient ReliableSingleThread rstMakeSuggestions = dm_rst(me(), r makeSuggestions).cancelBeforeTrigger(); | 
| 19 | transient ReliableSingleThread rstProcessInput = dm_rst(me(), r processInput).cancelBeforeTrigger(); | 
| 20 | S input; | 
| 21 | int selectedTab; | 
| 22 | switchable int maxResults = 1000; | 
| 23 | |
| 24 |   start { | 
| 25 | dbIndexingCI(RewriteExample, 'in); | 
| 26 | dbIndexingCI(Result, 'out); | 
| 27 | rewriteExamplesCRUD = CRUD(RewriteExample); | 
| 28 | resultsCRUD = CRUD(Result); | 
| 29 | } | 
| 30 | |
| 31 |   visualize { | 
| 32 | JComponent c = dm_rememberSelectedTab selectedTab(jtabs( | 
| 33 | "Rewrite Examples", | 
| 34 | centerAndSouthWithMargin( | 
| 35 | rewriteExamplesCRUD.visualize(), | 
| 36 |           jCenteredSection("Suggestions", jMinHeight(100, scpSuggestions = singleComponentPanel()))), | 
| 37 | "Input", | 
| 38 | northAndCenterWithMargins( | 
| 39 | withRightAlignedButtons(dm_centeredTextFieldAsSection input(), | 
| 40 | "Make rule...", rThreadEnter makeRuleDialog), | 
| 41 |           jCenteredSection("Results", resultsCRUD.visualize())) | 
| 42 | )); | 
| 43 |     resultsCRUD.addSelectionDependentButton("Accept", r { acceptResult(resultsCRUD.selected()) }); | 
| 44 |     resultsCRUD.addSelectionDependentButton("Use as new input", r { setField(input := resultsCRUD.selected().out) }); | 
| 45 | onTableSelectionChanged(rewriteExamplesCRUD.table(), rstMakeSuggestions); | 
| 46 |     onConceptChangeByClass_notOnAllChanged RewriteExample(r { rstMakeSuggestions.trigger(); rstProcessInput.trigger(); }); | 
| 47 | dm_watchFieldAndNow input(rstProcessInput); | 
| 48 | ret c; | 
| 49 | } | 
| 50 | |
| 51 |   void makeSuggestions { | 
| 52 | RewriteExample e = rewriteExamplesCRUD.selected(), ret if null; | 
| 53 | new L<JComponent> buttons; | 
| 54 | |
| 55 | S transferred = transferCurlyBracketPhrases(e.out, e.in); | 
| 56 | if (neq(transferred, e.in)) | 
| 57 |       buttons.add(jbutton("Change LHS to: " + transferred, r { cset(e, in := transferred) })); | 
| 58 | |
| 59 | Set<S> sharedPhrases = sharedCurlyBracketPhrases(e.in, e.out); | 
| 60 |     if (nempty(sharedPhrases)) { | 
| 61 | S var = firstUnusedCountingDollarVar(e.in); | 
| 62 |       for (S phrase : sharedPhrases) { | 
| 63 | S in2 = jreplaceIC_literal(e.in, curly(phrase), var); | 
| 64 | S out2 = jreplaceIC_literal(e.out, curly(phrase), var); | 
| 65 | if (!hasConceptIC RewriteExample(in := in2, out := out2)) | 
| 66 |           buttons.add(jbutton("Replace " + curly(phrase) + " with " + var, r { | 
| 67 |             addExample(in2, out2, ll('sharedCurlyBracketPhrases, e)) | 
| 68 | })); | 
| 69 | } | 
| 70 | } | 
| 71 | |
| 72 | setComponent(scpSuggestions, scrollableStackWithSpacing(buttons)); | 
| 73 | } | 
| 74 | |
| 75 |   void processInput { | 
| 76 | input = simplify(this.input); | 
| 77 |     print("Processing input: " + input); | 
| 78 | deleteConcepts Result(); | 
| 79 | |
| 80 | // flexMatch input with all examples | 
| 81 |     for ping (RewriteExample e) { | 
| 82 | if (containsDollarVars(e.in)) | 
| 83 | for (SS map : flexMatchDollarVarsIC_all(e.in, input)) | 
| 84 |           addResult(cnew Result(in := input, out := replaceVars(e.out, map), trail := ll('flexMatch, e))); | 
| 85 | } | 
| 86 | |
| 87 | // Offer grouping last tokens | 
| 88 | LS tok = javaTokWithAllBrackets_cached(input); | 
| 89 | for (S s : tok_groupAfterNTokens_all(tok)) | 
| 90 | addResult(cnew Result(in := input, out := s)); | 
| 91 | |
| 92 | // Offer grouping first tokens | 
| 93 | for (S s : tok_groupFirstNTokens_all(tok)) | 
| 94 | addResult(cnew Result(in := input, out := s)); | 
| 95 | } | 
| 96 | |
| 97 |   void acceptResult(Result r) { | 
| 98 | if (r == null || r.existing != null) ret; | 
| 99 | RewriteExample e = addExample(r.in, r.out, r.trail); | 
| 100 | if (e == null) ret; | 
| 101 | cset(r, existing := e); | 
| 102 | } | 
| 103 | |
| 104 |   void addResult(Result r) { | 
| 105 | cset(r, existing := conceptWhereCI RewriteExample(in := r.in, out := r.out)); | 
| 106 |     if (countConcepts Result() >= maxResults) quickFail("Maximum number of results reached"); | 
| 107 | } | 
| 108 | |
| 109 |   void makeRuleDialog { | 
| 110 | JTextField tfIn = jTextField(input); | 
| 111 | JTextField tfOut = jTextField(input); | 
| 112 |     showFormTitled2("Make rule", | 
| 113 | "In", tfIn, | 
| 114 | "Out", tfOut, | 
| 115 |       r { | 
| 116 | RewriteExample e = addExample(getText(tfIn), getText(tfOut), 'userDialog); | 
| 117 | infoBox(e == null ? "Rule exists" : "Rule made: " + e.id); | 
| 118 | }); | 
| 119 | } | 
| 120 | |
| 121 |   S simplify(S s) { | 
| 122 | ret simpleSpacesAndTrim(s); | 
| 123 | } | 
| 124 | |
| 125 |   RewriteExample addExample(S in, S out, O trail) { | 
| 126 | ret csetAndReturn(uniqIC_returnIfNew RewriteExample(in := simplify(in), out := simplify(out)), +trail); | 
| 127 | } | 
| 128 | } | 
download show line numbers debug dex old transpilations
Travelled to 7 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv
No comments. add comment
| Snippet ID: | #1028800 | 
| Snippet name: | Rewrite Database [OK] | 
| Eternal ID of this version: | #1028800/36 | 
| Text MD5: | b42b82d766d83de8e14fa0c0982d6a5b | 
| Transpilation MD5: | fc567f41f5622784c941b545da16d395 | 
| Author: | stefan | 
| Category: | |
| Type: | JavaX source code (Dynamic Module) | 
| Public (visible to everyone): | Yes | 
| Archived (hidden from active list): | No | 
| Created/modified: | 2020-07-09 14:25:04 | 
| Source code size: | 4437 bytes / 128 lines | 
| Pitched / IR pitched: | No / No | 
| Views / Downloads: | 396 / 4353 | 
| Version history: | 35 change(s) | 
| Referenced in: | [show references] |