1 | !7 |
2 | |
3 | concept Rule > ConceptWithGlobalID { |
4 | S input, output, variables /* comma-separated */, flags, comment; |
5 | sS _fieldOrder = "globalID input output variables flags comment"; |
6 | |
7 | Set<S> vars() { ret asCISet(tok_splitAtComma(variables)); } |
8 | Set<S> flags() { ret asCISet(tok_splitAtComma(flags)); } |
9 | } |
10 | |
11 | concept PossibleExample > ConceptWithGlobalID { |
12 | Rule rule; |
13 | S input, output; |
14 | SS mapping; |
15 | Bool good; |
16 | sS _fieldOrder = "globalID rule good input output mapping"; |
17 | } |
18 | |
19 | concept Input > ConceptWithGlobalID { |
20 | S input; |
21 | } |
22 | |
23 | p { |
24 | db(); |
25 | indexConceptFields(PossibleExample, 'rule); |
26 | indexConceptFieldsCI(Input, 'input, Rule, 'input, PossibleExample, 'input); |
27 | indexConceptFieldsOrdered(Rule, 'globalID); |
28 | } |
29 | |
30 | static HCRUD rulesCRUD() { |
31 | ret new HCRUD(rawLink("admin"), new HCRUD_Concepts<Rule>(Rule) { |
32 | void massageItemMapForList(Rule r, MapSO map) { |
33 | Cl<PossibleExample> examples = conceptsWhere(PossibleExample, rule := r); |
34 | PossibleExample goodExample = random(objectsWhere(examples, good := true)); |
35 | map.put("Example Input" := goodExample != null ? goodExample.input : null); |
36 | map.put("Example Output" := goodExample != null ? goodExample.output : null); |
37 | map.put("Possible Examples" := l(examples)); |
38 | replaceKey(map, "input", "Pattern"); |
39 | replaceMap(map, putKeysFirst(map, "globalID", "Example Input", "Example Output", "Pattern")); |
40 | } |
41 | |
42 | Cl<Rule> listConcepts() { |
43 | ret conceptsSortedByFieldCI(Rule, 'input); |
44 | } |
45 | } .fieldHelp(variables := "comma-separated") |
46 | .fieldHelp(flags := "comma-separated") |
47 | ) { |
48 | S renderValue(S field, O value) { |
49 | S html = super.renderValue(field, value); |
50 | if (eq(field, 'globalID)) |
51 | ret ahref(rawLink(str(value)), html); |
52 | if (eqOneOf(field, 'input, "Pattern")) |
53 | ret b(html); |
54 | ret html; |
55 | } |
56 | }; |
57 | } |
58 | |
59 | static HCRUD inputsCRUD() { |
60 | ret new HCRUD(new HCRUD_Concepts<Input>(Input.class) { |
61 | void massageItemMapForList(Input i, MapSO map) { |
62 | map.put("Possible Examples" := l(conceptsWhereCI PossibleExample(input := i.input))); |
63 | map.put("Cmds", HTML(ahref( |
64 | appendQueryToURL("admin", cmd := "processInput", inputID := i.id), |
65 | "process"))); |
66 | } |
67 | }); |
68 | } |
69 | |
70 | static HCRUD possibleExamplesCRUD(S uri) { |
71 | ret new HCRUD(new HCRUD_Concepts<PossibleExample>(PossibleExample.class) { |
72 | void massageItemMapForList(PossibleExample e, MapSO map) { |
73 | if (e.rule != null) |
74 | map.put(rule := HTML(ahref(rawLink(e.rule.globalIDStr()), e.rule.globalID()))); |
75 | map.put(Cmds := HTML( |
76 | ahref(appendQueryToURL(rawLink(uri), cmd := "markGood", exampleID := e.id), "good") + " " + |
77 | ahref(appendQueryToURL(rawLink(uri), cmd := "markBad", exampleID := e.id), "bad"))); |
78 | } |
79 | }); |
80 | } |
81 | |
82 | set flag NoNanoHTTPD. html { |
83 | long startTime = sysNow(); |
84 | bool authed = webAuthed(params); |
85 | |
86 | if (empty(uri = dropSlashPrefix(uri))) |
87 | ret htitle_h1("Rules") |
88 | + (webAuthed(params) ? p(ahref(rawLink("admin"), "Admin")) : "") |
89 | + ul_noEncode(map(list(Rule), r -> ahref(rawLink(r.globalIDStr()), htmlEncode2(r.input + " => " + r.output)))); |
90 | |
91 | if (eqic(uri, "wordClassExamples")) |
92 | ret serveText(mapPairsToLines(multiSetMapToPairs(wordClassExamples()), (k, v) -> quote(v) + " is in word class " + quote(k))); |
93 | |
94 | // serve rule |
95 | if (possibleGlobalID(uri)) { |
96 | Rule r = conceptWhere(Rule, globalID := GlobalID(uri)); |
97 | if (r == null) ret subBot_serve404("Rule not found"); |
98 | S html = htitle_h1("Rule " + r.globalID) |
99 | + htmlTable2(mapToTwoElementMaps("Field", "Value", litorderedmap("Input" := r.input, "Output" := r.output, "Variables" := r.variables, "Comment" := r.comment))); |
100 | |
101 | HCRUD peCRUD = possibleExamplesCRUD(uri); |
102 | ((HCRUD_Concepts) peCRUD.data).addFilter(rule := r); |
103 | html += h3("Possible Examples") |
104 | + peCRUD.renderTable(false); |
105 | |
106 | //Cl<PossibleExample> examples = conceptsWhere PossibleExample(rule := r); |
107 | //ret possibleExamplesCRUD().render(false, ) |
108 | ret html; |
109 | } |
110 | |
111 | new Matches m; |
112 | if (swic_slash(uri, "admin", m)) |
113 | { |
114 | S _uri = uri; |
115 | HMultiCRUD multi = new(rawLink("admin"), Rule, Input, PossibleExample) { |
116 | HCRUD makeCRUD(S className) { |
117 | if (eq(className, "Rule")) ret rulesCRUD(); |
118 | if (eq(className, "Input")) ret inputsCRUD(); |
119 | if (eq(className, "PossibleExample")) ret possibleExamplesCRUD(_uri); |
120 | null; |
121 | } |
122 | |
123 | LS naviComponents() { |
124 | LS l = super.naviComponents(); |
125 | if (mutationRights) |
126 | l.add(ahref(appendQueryToURL(baseLink, cmd := "processInputs"), "process inputs")); |
127 | l.add(ahref(rawLink("wordClassExamples"), "word class examples")); |
128 | ret l; |
129 | } |
130 | } .mainTitle("Rules + Inputs") |
131 | .mutationRights(authed); |
132 | multi.downloadRights = authed; |
133 | HAbstractRenderable.MakeFrame mf = multi.makeFrame; |
134 | multi.makeFrame = (title, contents) -> mf.makeFrame(title, contents) + p(small(sysNow()-startTime + " ms"), align := "right"); |
135 | |
136 | S cmd = params.get('cmd); |
137 | if (nempty(cmd)) { |
138 | try object checkWebAuthed(params); |
139 | if (eqOneOf(cmd, "markGood", "markBad")) { |
140 | PossibleExample e = getConcept(PossibleExample, parseLong(params.get('exampleID))); |
141 | cset(e, good := eq(cmd, "markGood")); |
142 | } |
143 | |
144 | if (eq(cmd, "processInput")) { |
145 | Input i = getConcept(Input, toLong(params.get('inputID))); |
146 | ret serveText(hijackPrint_tee_pcall(() -> processInput(i.input))); |
147 | } |
148 | |
149 | if (eq(cmd, "processInputs")) { |
150 | time { |
151 | for (Input i) |
152 | processInput(i.input); |
153 | } |
154 | ret multi.refreshWithMsgs("Processed " + nInputs(countConcepts(Input)) + " in " + lastTiming() + " ms"); |
155 | } |
156 | } |
157 | |
158 | ret multi.renderPage(m.rest(), params); |
159 | } |
160 | |
161 | ret subBot_serve404(); |
162 | } |
163 | |
164 | static void processInput(S input) { |
165 | for (Rule r) { |
166 | new Set<PossibleExample> found; |
167 | print("Combining input " + quote(input) + " with rule " + quote(r.input)); |
168 | ITokenizer tokenizer = r.flags().contains("keep punctuation") |
169 | ? lambda1 javaTokWithBrackets_cached |
170 | : lambda1 javaTokNPunctuationWithBrackets_plusAsterisk_cached; |
171 | //for (SS mapping : arbitraryVarsFlexMatchIC_iterator(r.vars(), r.input, input)) { |
172 | for (SS mapping : arbitraryVarsFlexMatchIC_tok_iterator(r.vars(), Tok(tokenizer, r.input), Tok(tokenizer, input))) { |
173 | print(" Have mapping: " + mapping); |
174 | PossibleExample e = uniq PossibleExample(rule := r, +input, +mapping); |
175 | found.add(e); |
176 | S output = replaceVars_optRound2(r.output, mapping); |
177 | output = applyTransformers(output); |
178 | cset(found, +output); |
179 | } |
180 | deleteConcepts(listMinusSet(conceptsWhere(PossibleExample, rule := r, +input), found)); |
181 | } |
182 | } |
183 | |
184 | func transformers = mapGet_if1(litmap( |
185 | nominativ := (IF1<S>) lambda1 german_toNominativ, |
186 | switcheroo := (IF1<S>) lambda1 switcheroo |
187 | )); |
188 | |
189 | sS applyTransformers(S s) { |
190 | ret pcallOrKeep(s, () -> tok_applyCurlyTransformers(transformers(), s)); |
191 | } |
192 | |
193 | static MultiSetMap<S> wordClassExamples() { |
194 | MultiSetMap<S> mm = ciMultiCISetMap(); |
195 | for (PossibleExample e : conceptsWhere PossibleExample(good := true)) |
196 | for (S key, value : unnull(e.mapping)) |
197 | mm.put(key, value); |
198 | ret mm; |
199 | } |
download show line numbers debug dex old transpilations
Travelled to 6 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #1026196 |
Snippet name: | Rules + Inputs Web Bot [with functional programming primitives] |
Eternal ID of this version: | #1026196/3 |
Text MD5: | 87b625bc9f0148e34347e0b7ef992f18 |
Author: | stefan |
Category: | |
Type: | JavaX module (desktop) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2020-01-23 15:02:24 |
Source code size: | 7362 bytes / 199 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 304 / 465 |
Version history: | 2 change(s) |
Referenced in: | [show references] |