Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

899
LINES

< > BotCompany Repo | #1030958 // Gazelle BEA [backup before new compact include]

JavaX source code (Dynamic Module) [tags: use-pretranspiled] - run with: Stefan's OS

Uses 1059K of libraries. Click here for Pure Java version (41515L/267K).

1  
!7
2  
3  
!include once #1030833 // BEACalculations
4  
5  
set flag NoNanoHTTPD.
6  
7  
cmodule2 GazelleBEA > DynGazelleRocks {
8  
  switchable bool mirrorBEAObjects; // don't do it for now
9  
  switchable bool enableAutoRuns = true;
10  
  switchable bool enableNewBEAObjectNotis = true;
11  
  
12  
  transient ReliableSingleThread_Multi<BEAObject> rstAutoRuns = new(1000, lambda1 performAutoRuns);
13  
  transient Q notificationQ;
14  
  transient ReliableSingleThread_Multi<BEAObject> rstDistributeNewObject = new(1000, lambda1 distributeNewObject_impl);
15  
  transient Set<BEAObject> newObjectsDistributed = weakSet();
16  
17  
  transient BEACalculations calculations = new(this);
18  
  transient int newFieldsToShow = 3;
19  
  transient bool allowAnonymousInputUpload = true; // TODO
20  
  switchable int maxInputLength = 50000;
21  
22  
  // add more fields here
23  
  
24  
  //!include #1030883 // DB quickImport mix-in
25  
  
26  
  void init {
27  
    super.init();
28  
    botName = heading = adminName = "Gazelle BEA";
29  
    set enableVars;
30  
    set showTalkToBotLink;
31  
    unset phoneNumberSpecialInputField;
32  
  }
33  
34  
  void start {
35  
    set storeBaseClassesInStructure;
36  
    super.start();
37  
    set showCRUDToEveryone;
38  
    //set showOnlySelectedObject;
39  
40  
    if (!enabled) ret;
41  
42  
    // mirror all objects to be sure
43  
    rstUpdateBEAMirrors.addAll(list(BEAObject));
44  
    
45  
    newObjectsDistributed.addAll(list(BEAObject));
46  
    
47  
    /*onIndividualConceptChange_notOnAllChanged(BEAObject,
48  
      p -> { calculations.makeSyntacticPattern(p); });*/
49  
      
50  
    onIndividualConceptChange_notOnAllChanged(BEAObject,
51  
      o -> {
52  
        if (enableAutoRuns) rstAutoRuns.add(o);
53  
        if (enableNewBEAObjectNotis && newObjectsDistributed.add(o))
54  
          rstDistributeNewObject.add(o);
55  
      });
56  
      
57  
    notificationQ = dm_startQ();
58  
    
59  
    // Until the ref leak bug is fixed, fix things every 30...
60  
    
61  
    dm_doEvery(30.0, r {
62  
      print(ConceptsRefChecker(db_mainConcepts()).runAndFixAll());
63  
    });
64  
  }
65  
66  
  void makeIndices :: after {    
67  
    indexConceptFieldDesc(BEAObject, "_modified");
68  
    indexConceptFieldIC(BEAObject, "type");
69  
    
70  
    // TODO: don't index nulls, also maybe just use backRefs
71  
    indexConceptField(BEAObject, "input"); // e.g. for counting how many matches we have for a given input
72  
  }
73  
  
74  
  L<Class> crudClasses(Req req) {
75  
    ret listPlus(super.crudClasses(req), BEAObject);
76  
  }
77  
  
78  
  S authFormHeading() {
79  
    ret h3("Gazelle BEA");
80  
  }
81  
  
82  
  void makeFramer(Req req) {
83  
    super.makeFramer(req);
84  
    
85  
    req.framer.renderTitle = () -> h1(ahref(baseLink + "/", "Gazelle BEA")
86  
      + " " + htmlEncode2(req.framer.title));
87  
      
88  
    req.framer.addInHead(hjs_autoExpandingTextAreas());
89  
  }
90  
91  
  <A extends Concept> HCRUD_Concepts<A> crudData(Class<A> c, Req req) {
92  
    HCRUD_Concepts<A> cc = super.crudData(c, req);
93  
    
94  
    if (c == BEAObject) {
95  
      cc.humanizeFieldNames = false;
96  
      cc.convertConceptValuesToRefs = true;
97  
      cc.itemName = () -> "BEA Object";
98  
      
99  
      cc.onCreate.add(o ->
100  
        cset(o, createdBy := currentUser()));
101  
      
102  
      cc.getObjectForDuplication = id -> {
103  
        MapSO item = cc.getObjectForDuplication_base(id);
104  
        item.put(creator := req.auth.user!); // set default creator to current user
105  
        item.put(createdFrom := getConcept(toLong(id)));
106  
        ret item;
107  
      };
108  
      
109  
      cc.emptyObject = () -> {
110  
        MapSO item = cc.emptyObject_base();
111  
        item.put(type := "");
112  
        ret item;
113  
      };
114  
      
115  
      Set<S> deletableRefs = litciset("Match");
116  
      
117  
      cc.objectCanBeDeleted = id ->
118  
        all(findBackRefs(BEAObject, cc.conceptForID(id)),
119  
          o -> contains(deletableRefs, o.type()));
120  
121  
      cc.actuallyDeleteConcept = o -> {
122  
        deleteConcepts(filter(findBackRefs(BEAObject, o),
123  
          o2 -> contains(deletableRefs, o2.type())));
124  
        cdelete(o);
125  
      };
126  
    }
127  
128  
    ret cc;
129  
  }
130  
131  
  <A extends Concept> HCRUD makeCRUD(Class<A> c, Req req, HTMLFramer1 framer) {
132  
    HCRUD crud = super.makeCRUD(c, req, framer);
133  
    HCRUD_Concepts data = cast crud.data;
134  
    crud.showOnlySelected = true;
135  
    crud.showSearchField = true;
136  
    if (data.customFilter == null)
137  
      crud.descending = true; // show latest objects first by default except when searching
138  
    crud.cleanItemIDs = true;
139  
140  
    if (c == BEAObject) {
141  
      crud.cellColumnToolTips = true;
142  
      crud.unshownFields = litset("mirrorPost", "globalID");
143  
      crud.showTextFieldsAsAutoExpandingTextAreas = true;
144  
      HCRUD_Concepts cc = cast crud.data;
145  
      
146  
      S typeFilter = req.get("type");
147  
      if (nempty(typeFilter))
148  
        cc.addCIFilter(type := typeFilter);
149  
      
150  
      crud.renderCmds = map -> {
151  
        BEAObject o = getConcept BEAObject(crud.itemIDAsLong(map));
152  
        
153  
        new LS cmds;
154  
        
155  
        // special commands for BEA types or objects with certain fields
156  
        
157  
        if (nempty(o.text()))
158  
          cmds.add(
159  
            targetBlank(addParamsToURL(baseLink + "/query",
160  
              q := o.text(), algorithm := "process input"),
161  
              "Use text as query"));
162  
163  
        if (o.typeIs("Input")) {
164  
          cmds.add(ahref(addParamsToURL(crudLink(BEAObject),
165  
            cmd := "new",
166  
            title := "Add Pattern For Input",
167  
            f_type := "Pattern",
168  
            f_text := getStringOpt text(o),
169  
            f_shouldMatch := o.id, metaInfo_shouldMatch := "concept",
170  
          ), "Add pattern"));
171  
172  
          cmds.add(ahref(appendParamsToURL(baseLink + "/query", q := o.text(), algorithm := "Apply all text functions"),
173  
           "Apply all text functions"));
174  
        }
175  
176  
        if (o.typeIs("Pattern"))
177  
          cmds.add(ahref(appendParamsToURL(baseLink + "/query", q := o.id, algorithm := "Run pattern"),
178  
           "Run pattern against all inputs"));
179  
           
180  
        if (o.typeIs("Match"))
181  
          cmds.add(ahref(appendParamsToURL(baseLink + "/query", q := o.id, algorithm := "Find rewrites for match"),
182  
           "Find rewrites"));
183  
184  
        cmds.add(
185  
          ahref(addParamsToURL(baseLink + "/markUseful",
186  
            redirect := beaShortURL(o),
187  
            objectID := o.id),
188  
            "Mark useful"));
189  
        
190  
        cmds.add(ahref(addParamsToURL(crudLink(BEAObject),
191  
          cmd := "new",
192  
          title := "Add Comment",
193  
          f_type := "Comment",
194  
          f_on := o.id,
195  
          f_text := "",
196  
          metaInfo_on := "concept"), "Add comment"));
197  
          
198  
        if (o.typeIsOneOf("Script", "Step in script") || eqic(beforeVerticalBar(o.type()), "Instruction"))
199  
          cmds.add(
200  
            ahref(addParamsToURL(baseLink + "/runInstruction",
201  
              instruction := o.id),
202  
              "Run"));
203  
              
204  
        if (o.typeIs("Function Result") && eqic(cget resultType(o), "string"))
205  
          cmds.add(
206  
            ahref(addParamsToURL(baseLink + "/convertResultToInput",
207  
              result := o.id),
208  
              "Convert to input"));
209  
        
210  
        framer.addInHead(hjs_copyToClipboard());
211  
        
212  
        cmds.add(ahref_onClick(formatFunctionCall copyToClipboard(jsQuote(o.globalIDStr())), "Copy global ID [" + o.globalIDStr() + "]"));
213  
        
214  
        ret joinNemptiesWithVBar(
215  
          crud.renderCmds_base(map),
216  
          !o.typeIsOneOf("Input", "Pattern", "Syntactic Pattern")
217  
            ? null : addRewriteHTML(o),
218  
          hPopDownButton(cmds));
219  
      };
220  
      
221  
      cc.massageItemMapForList = (item, map) -> {
222  
        BEAObject o = cast item;
223  
        if (o.typeIs("Input")) {
224  
          Cl<BEAObject> matches = objectsWhereCI(findBackRefs(o, BEAObject), type := "match");
225  
          map/Map.put("Best Matches", HTML(hparagraphs(
226  
            lmap matchDescHTML(takeFirst(3, matches)))));
227  
        }
228  
        S idField = crud.idField();
229  
        O id = map/Map.get(idField);
230  
        if (id instanceof S)
231  
          map/Map.put(idField, HTML(ahref(beaShortURL(o), id)));
232  
        if (o.typeIs("Function Result")) {
233  
          O value = o~.result;
234  
          map/Map.put("result", HTML(javaValueToHTML(value)));
235  
        }
236  
      };
237  
      
238  
      crud.massageFormMatrix = (map, matrix) -> {
239  
        for (int i = 1; i <= newFieldsToShow; i++) {
240  
          S nf = "newField" + i;
241  
          
242  
          S refSelector =
243  
            crud.renderInput("\*nf*/_conceptValue",
244  
              cc.makeConceptsComboBox("\*nf*/_conceptValue", BEAObject), null)
245  
            + hjs([[$("[name=]] + nf + [[_conceptValue]").hide()]]);
246  
              
247  
          LS types = ll("String", "BEAObject", "Bool");
248  
              
249  
          S typeSelector = hselect_list(types, name := "\*nf*/_type",
250  
              onchange := [[
251  
                var value = this.value;
252  
                $("[name=]] + nf + [[_value]").toggle(value != "BEAObject");
253  
                $("#]] + nf + [[_refBox").toggle(value == "BEAObject");
254  
              ]]);
255  
          
256  
          matrix.add(ll("Add field:<br>" + htextfield("\*nf*/_name", title := "New field name"),
257  
            htmlTable2_noHtmlEncode(ll(ll(
258  
              // string input
259  
              htextfield("\*nf*/_value"),
260  
              span(refSelector, id := "\*nf*/_refBox", style := "display: none"),
261  
            "Type", typeSelector
262  
            )),
263  
            noHeader := true,
264  
            tableParams := litobjectarray(style := "width: 100%"))));
265  
          }
266  
        };
267  
      
268  
      crud.preprocessUpdateParams = params -> {
269  
        params = cloneMap(params);
270  
271  
        // drop empty strings
272  
        //removeFromMapWhereValue(params, v -> eq(v, ""));
273  
        params = mapValues(params, v -> eq(v, "") ? null : v);
274  
        
275  
        for (int i = 1; i <= max(newFieldsToShow, 10); i++) {
276  
          S nf = "newField" + i;
277  
          S name = params.get("\*nf*/_name"),
278  
            type = params.get("\*nf*/_type"),
279  
            refValue = params.get("\*nf*/_conceptValue"),
280  
            value = params.get("\*nf*/_value");
281  
  
282  
          if (eqic(type, "BEAObject")) {
283  
            value = refValue;
284  
            params.put("metaInfo_" + name, "concept");
285  
          } else if (eqic(type, "Bool"))
286  
            params.put("metaInfo_" + name, "bool");
287  
          
288  
          if (eq(value, "")) value = null;
289  
            
290  
          if (nempty(name) /*&& neqOneOf(value, null, "")*/)
291  
            params.put(crud.fieldPrefix + name, value);
292  
        }
293  
        
294  
        ret params;
295  
      };
296  
    }
297  
    
298  
    ret crud;
299  
  }
300  
  
301  
  O serveBotFunction(Req req, S function, Map data, User user) {
302  
    if (eq(function, "beaList")) {
303  
      long changedAfter = toLong(data.get("changedAfter"));
304  
      double pollFor = min(bot_maxPollSeconds, toLong(data.get("pollFor"))); // how long to poll (seconds)
305  
      long startTime = sysNow();
306  
307  
      // We're super-anal about catching all changes. This will probably never trigger
308  
      if (changedAfter > 0 && changedAfter == now()) sleep(1);
309  
      
310  
      Cl<BEAObject> objects;
311  
      while true {
312  
        objects = changedAfter == 0 ? list(BEAObject)
313  
          : conceptsWithFieldGreaterThan_sorted(BEAObject, _modified := changedAfter);
314  
315  
        // return when there are results, no polling or poll expired
316  
        if (nempty(objects) || pollFor == 0 || elapsedSeconds_sysNow(startTime) >= pollFor)
317  
          ret serveJSON_breakAtLevels(2, result := map(objects, obj ->
318  
            litorderedmap(gid := str(obj.globalID()), struct := obj.structureString())
319  
          ));
320  
321  
        // sleep and try again
322  
        sleep(bot_pollInterval);
323  
      }
324  
    }
325  
    
326  
    ret super.serveBotFunction(req, function, data, user);
327  
  }
328  
329  
  transient ReliableSingleThread_Multi<BEAObject> rstUpdateBEAMirrors = new(100, c -> c.updateMirrorPost());
330  
  
331  
  O serveOtherPage2(Req req) null {
332  
    printVars_str serveOtherPage2(uri := req.uri);
333  
    try object super.serveOtherPage2(req);
334  
    
335  
    S uri = dropTrailingSlashIfNemptyAfterwards(req.uri);
336  
    new Matches m;
337  
    
338  
    if (swic_notSame(uri, "/beaCRUD/", m))
339  
      ret renderBEAObjectTable(req, urldecode(m.rest()));
340  
    
341  
    if (eq(uri, "/inputs"))
342  
      ret renderBEAObjectTable(req, "input");
343  
    
344  
    if (eq(uri, "/syntacticPatternsWithoutRewrites")) {
345  
      HCRUD crud = makeBEAObjectCRUD(req, "Syntactic Pattern");
346  
      HCRUD_Concepts<BEAObject> data = cast crud.data;
347  
      
348  
      IF1 prev = data.customFilter;
349  
      data.customFilter = list -> {
350  
        list = filter(list, c -> empty(beaBackRefs(c, "Rewrite")));
351  
        ret postProcess(prev, list);
352  
      };
353  
      crud.customTitle = "Syntactic patterns without rewrites";
354  
      ret serveCRUD(req, BEAObject, crud);
355  
    }
356  
      
357  
    if (eq(uri, "/inputsWithoutRewrites")) {
358  
      HCRUD crud = makeBEAObjectCRUD(req, "input");
359  
      HCRUD_Concepts<BEAObject> data = cast crud.data;
360  
      
361  
      IF1 prev = data.customFilter;
362  
      data.customFilter = list -> {
363  
        list = filter(list, c -> empty(beaBackRefs(c, "Rewrite")));
364  
        ret postProcess(prev, list);
365  
      };
366  
      crud.customTitle = "Inputs without rewrites";
367  
      ret serveCRUD(req, BEAObject, crud);
368  
    }
369  
      
370  
    if (eq(uri, "/inputsWithoutMatches")) {
371  
      HCRUD crud = makeBEAObjectCRUD(req, "input");
372  
      HCRUD_Concepts<BEAObject> data = cast crud.data;
373  
      
374  
      IF1 prev = data.customFilter;
375  
      data.customFilter = list -> {
376  
        list = filter(list, c -> empty(beaBackRefs(c, "Match")));
377  
        ret postProcess(prev, list);
378  
      };
379  
      crud.customTitle = "Inputs without matches";
380  
      ret serveCRUD(req, BEAObject, crud);
381  
    }
382  
      
383  
    if (eq(uri, "/patterns"))
384  
      ret renderBEAObjectTable(req, "pattern");
385  
386  
    if (eq(uri, "/syntacticPatterns"))
387  
      ret renderBEAObjectTable(req, "Syntactic Pattern");
388  
389  
    if (eq(uri, "/matches"))
390  
      ret renderBEAObjectTable(req, "match");
391  
392  
    if (eq(uri, "/rewrites"))
393  
      ret renderBEAObjectTable(req, "rewrite");
394  
395  
    if (eq(uri, "/aiTasks"))
396  
      ret renderBEAObjectTable(req, "AI Task");
397  
398  
    if (eq(uri, "/query"))
399  
      ret calculations.serveQueryPage(req);
400  
      
401  
    if (eq(uri, "/saveInput")) {
402  
      S text = trim(req.get("text"));
403  
      S info = trim(req.get("info"));
404  
      if (empty(text)) ret subBot_serve500("Input empty");
405  
      if (l(text) > maxInputLength) ret subBot_serve500("Input too long");
406  
      BEAObject input = uniqCI BEAObject(type := "Input", +text, createdBy := user(req));
407  
      if (nempty(info))
408  
        uniqCI BEAObject(type := "Input Source", +input, source := info);
409  
      ret hrefresh(or2(req.get("redirect"), baseLink + "/"));
410  
    }
411  
      
412  
    if (eq(uri, "/markUseful")) {
413  
      BEAObject o = beaGet(req.get("objectID"));
414  
      if (o == null) ret subBot_serve500("Object not found");
415  
      uniqCI BEAObject(type := "Useful", object := o, createdBy := user(req));
416  
      ret hrefresh(or2(req.get("redirect"), baseLink + "/"));
417  
    }
418  
      
419  
    if (eq(uri, "/saveAnswer")) {
420  
      S text = trim(req.get("text"));
421  
      S rewriteType = or2(trim(req.get("rewriteType")), "Suggested Answer";
422  
      long inputID = toLong(req.get("inputID"));
423  
      BEAObject input = beaGet(inputID);
424  
      if (input == null) ret subBot_serve500("Input not found");
425  
      if (empty(text)) ret subBot_serve500("Text empty");
426  
      if (l(text) > maxInputLength) ret subBot_serve500("Text too long");
427  
      uniqCI BEAObject(type := "Rewrite", +text, isRewriteOf := input, +rewriteType, createdBy := user(req));
428  
      ret hrefresh(or2(req.get("redirect"), baseLink + "/"));
429  
    }
430  
      
431  
    // add more public URLs here
432  
      
433  
    if (!inMasterMode(req)) null;
434  
    
435  
    if (eq(uri, "/uploadInputs"))
436  
      ret serveUploadTexts(req, "Input");
437  
    
438  
    if (eq(uri, "/uploadPatterns"))
439  
      ret serveUploadTexts(req, "Pattern");
440  
      
441  
    if (eq(uri, "/analyzeInput"))
442  
      ret calculations.serveAnalyzeInput(req);
443  
      
444  
    if (eq(uri, "/allBEATypes")) {
445  
      MultiSet<S> ms = asCIMultiSet(collect type(list(BEAObject)));
446  
      ret h2_title("All BEA object types")
447  
        //+ hpre(renderMultiSetAsLines_byPopularity(ms));
448  
        + ul(mapPairs(multiSetToPairsByPopularity(ms),
449  
          (type, count) -> {
450  
            S _type = or2(type, "(no type)");
451  
            ret count + " " + ahref(baseLink + "/beaCRUD/" + urlencode(_type), htmlEncode2(_type));
452  
          }));
453  
    }
454  
    
455  
    if (eq(uri, "/storeMatch")) {
456  
      BEAObject pattern = beaGet(req.get("pattern"));
457  
      BEAObject input = beaGet(req.get("input"));
458  
      S label = req.get("label");
459  
      BEAObject match = calculations.reactInputWithPattern(input, pattern);
460  
      if (match == null) ret "Couldn't match";
461  
      cset(match, +label);
462  
      ret hrefresh(or2(req.get("redirect"), beaObjectURL(match)));
463  
    }
464  
    
465  
    if (eq(uri, "/saveSyntacticPattern")) {
466  
      S text = req.get("text");
467  
      BEAObject fromInput = beaGet(req.get("fromInput"));
468  
      BEAObject pat = uniqCI BEAObject(type := "Syntactic Pattern", +text, createdBy := user(req));
469  
      csetIfUnset(pat, +fromInput);
470  
      if (fromInput != null)
471  
        calculations.reactInputWithPattern(fromInput, pat);
472  
      ret hrefresh(or2(req.get("redirect"), baseLink + "/"));
473  
    }
474  
    
475  
    if (eq(uri, "/runInstruction")) {
476  
      BEAObject o = beaGet(req.get("instruction"));
477  
      runInstruction(o);
478  
      ret hrefresh(beaObjectURL(o));
479  
    }
480  
    
481  
    if (eq(uri, "/convertResultToInput")) {
482  
      BEAObject result = beaGet(req.get("result"));
483  
      if (result == null) ret subBot_serve500("Object not found");
484  
      S text = unquote(getString result(result));
485  
      if (l(text) > maxInputLength) ret subBot_serve500("Input too long");
486  
      BEAObject input = uniqCI BEAObject(type := "Input", +text);
487  
      uniqCI BEAObject(type := "Input Source", +input, source := result);
488  
      ret hrefresh(beaShortURL(input));
489  
    }
490  
      
491  
    // add more master-mode URLs here
492  
  }
493  
  
494  
  HCRUD makeBEAObjectCRUD(Req req, S type) {
495  
    HCRUD crud = makeCRUD(BEAObject, req);
496  
    crud.baseLink = req.uri();
497  
    HCRUD_Concepts data = cast crud.data;
498  
    data.itemName = () -> firstToUpper(type);
499  
    data.addCIFilter(type := eqic(type, "(no type)") ? null : type);
500  
    
501  
    if (eqicOneOf(type, "Input", "Pattern", "AI Task")) {
502  
      IF0<MapSO> prev = data.emptyObject;
503  
      data.emptyObject = () -> {
504  
        MapSO item = data.emptyObject_fallback(prev);
505  
        item.put(text := ""); // show text field when creating new objects
506  
        ret item;
507  
      };
508  
    }
509  
    ret crud;
510  
  }
511  
  
512  
  O renderBEAObjectTable(Req req, S type) {
513  
    HCRUD crud = makeBEAObjectCRUD(req, type);
514  
    ret serveCRUD(req, BEAObject, crud);
515  
  }
516  
  
517  
  O serveUploadTexts(Req req, S type) {
518  
    S inputs = req.get("text");
519  
    
520  
    new LS output;
521  
    
522  
    if (nempty(inputs)) {
523  
      for (S text : tlft(inputs)) {
524  
        Pair<BEAObject, Bool> p = uniqCI2_sync BEAObject(+type, +text);
525  
        if (cget uploadedBy(p.a) == null)
526  
          cset(p.a, uploadedBy := req.auth.user);
527  
        output.add(type + " " + (p.b ? "added" : "exists")
528  
          + " (ID " + p.a.id + "): " + text);
529  
      }
530  
    }
531  
    
532  
    ret h2("Upload " + plural(type))
533  
      + hpostform(
534  
          p(plural(type) + " (one per line):")
535  
        + p(htextarea(inputs, name := "text"))
536  
        + pIfNempty(htmlEncode_nlToBR(lines(output)))
537  
        + hsubmit("Upload " + plural(type)));
538  
  }
539  
  
540  
  Cl<BEAObject> beaObjectsOfType(S type) {
541  
    ret conceptsWhereCI BEAObject(+type);
542  
  }
543  
  
544  
  void reactAllInputsWithSomePatterns {
545  
    calculations.reactAllInputsWithSomePatterns();
546  
  }
547  
  
548  
  S navDiv() {
549  
    HCRUD crud = makeCRUD(BEAObject, currentReq!);
550  
551  
    int inputsWithoutRewrites = countPred(beaList("Input"), c -> empty(beaBackRefs(c, "Rewrite")));
552  
    int inputsWithoutMatches = countPred(beaList("Input"), c -> empty(beaBackRefs(c, "Match")));
553  
    int syntacticPatternsWithoutRewrites = countPred(beaList("Syntactic Pattern"), c -> empty(beaBackRefs(c, "Rewrite")));
554  
    
555  
    ret joinNemptiesWithVBar(
556  
      beaNavLink("Input", crud),
557  
      beaNavLink("Pattern", crud),
558  
      beaNavLink("Syntactic Pattern", crud),
559  
      ahref(baseLink + "/syntacticPatternsWithoutRewrites", n2(syntacticPatternsWithoutRewrites) + " Syntactic patterns w/o rewrites"),
560  
      beaNavLink("Match", crud),
561  
      beaNavLink("Rewrite", crud),
562  
      beaNavLink("AI Task", crud),
563  
      ahref(baseLink + "/query", "Query"),
564  
      ahref(baseLink + "/inputsWithoutMatches", n2(inputsWithoutMatches) + " Inputs w/o matches"),
565  
      ahref(baseLink + "/inputsWithoutRewrites", n2(inputsWithoutRewrites) + " Inputs w/o rewrites"),
566  
      hPopDownButtonWithText("Bot Forum", navLinks(flat := true, withStats := false)),
567  
      hPopDownButton(
568  
        ahref(baseLink + "/allBEATypes", "All object types"),
569  
        ahref(baseLink + "/stats", "Stats"),
570  
        !inMasterMode(currentReq!) ? null : ahref(baseLink + "/refchecker", "Reference checker"),
571  
      ));
572  
  }
573  
  
574  
  // crud is just the cached BEAObject crud to check for creation rights
575  
  S beaNavLink(S type, HCRUD crud, int count default beaCount(type)) {
576  
    S plural = firstToLower(plural(type));
577  
    S link = baseLink + "/" + camelCase(plural);
578  
    
579  
    ret ahref(link, count + " " + firstToUpper(plural)) + (!crud.actuallyAllowCreate() ? "" : " " + ahref(addParamToURL(link, cmd := "new"), "+"));
580  
  }
581  
  
582  
  S beaObjectURL(BEAObject o) {
583  
    ret conceptLink(o, currentReq!);
584  
    /*ret o == null ?:
585  
      addParamsToURL(baseLink + "/crud/BEAObject",
586  
        selectObj := o.id) + "#" + o.id;*/
587  
  }
588  
  
589  
  S matchDescHTML(BEAObject m) {
590  
    pcall {
591  
      BEAObject pat = cget pattern(m);
592  
      SS mapping = cast cget mapping(m);
593  
      ret ahref_undecorated(crudLink(m), htmlEncode2(quote(getString text(pat)))
594  
        + "<br>&nbsp; with " + renderEqualsCommaProperties(mapping));
595  
    }
596  
    ret htmlEncode2(str(m));
597  
  }
598  
  
599  
  int beaCount(S type) {
600  
    ret countConceptsWhereCI BEAObject(+type);
601  
  }
602  
  
603  
  Cl<BEAObject> beaList(S type) {
604  
    ret conceptsWhereCI BEAObject(+type);
605  
  }
606  
  
607  
  Cl<BEAObject> beaListAny(S... types) {
608  
    ret concatLists(lmap beaList(litciset(types)));
609  
  }
610  
  
611  
  BEAObject beaGet(long id) {
612  
    ret getConceptOpt BEAObject(id);
613  
  }
614  
  
615  
  BEAObject beaGet(S id) {
616  
    ret beaGet(parseFirstLong(id));
617  
  }
618  
619  
  BEAObject mapMethodLike cgetBEA(S field, BEAObject o) {
620  
    ret (BEAObject) cget(field, o);
621  
  }
622  
  
623  
  S beaLinkHTML(BEAObject o) {  
624  
    ret o == null ?: ahref(conceptLink(o), htmlEncode2_nlToBr(str(o));
625  
  }
626  
  
627  
  S beaToHTML(BEAObject o) { ret beaLinkHTML(o); }
628  
  S beaHTML(BEAObject o) { ret beaLinkHTML(o); }
629  
  
630  
  S beaShortURL(BEAObject o) {
631  
    ret o == null ?: baseLink + "/" + o.id;
632  
  }
633  
  
634  
  Cl<BEAObject> beaBackRefs(BEAObject o, S type) {
635  
    ret objectsWhereCI(findBackRefs BEAObject(o), +type);
636  
  }
637  
  
638  
  O serveDefaultPage(Req req) {
639  
    HTMLFramer1 framer = req.framer;
640  
    framer.add(hcenter3(hsnippetimg_scaleToWidth(200, #1102967, 200, 110, title := "Gazelle"), style := "margin-top: 100px"));
641  
    ret completeFrame(req);
642  
  }
643  
  
644  
  S html_loggedIn() {
645  
    User user = user(currentReq!);
646  
    ret user == null
647  
      ? /*"Not logged in"*/ ahref(baseLink + "/", "Log in")
648  
      : "Logged in as " + htmlEncode2(user.name);
649  
  }
650  
  
651  
  void distributeNewObject_impl(BEAObject o) {
652  
    if (o.typeIs("Match")) ret;
653  
    distributeNotification("New object: " + o);
654  
  }
655  
  
656  
  void performAutoRuns(BEAObject o) {
657  
    print("performAutoRuns", o);
658  
    
659  
    for (BEAObject autoRun : beaList("Auto Run")) {
660  
      if (!isTrue(getOpt enabled(autoRun)))
661  
        continue with print("Not enabled: " + autoRun);
662  
      
663  
      S type = getString onChangedObjectOfType(autoRun);
664  
      if (!o.typeIs(type)) continue with print("Wrong type: " + type;
665  
      
666  
      print("Running " + autoRun);
667  
      BEAObject procedure = cast cget procedure(autoRun);
668  
      
669  
      S internalCode = getString internalCode(procedure);
670  
      print(+internalCode);
671  
      
672  
      if (eqic(internalCode, "convertInputToPattern")) {
673  
        S text = o.text();
674  
        if (!containsAngleBracketVars(text)) continue with print("No angle bracket vars");
675  
        BEAObject p = uniqCI_returnIfNew BEAObject(type := "Pattern", +text);
676  
        cset(p, fromInput := o, byProcedure := procedure, byAutoRun := autoRun);
677  
        print(+p);
678  
      } else if (eqic(internalCode, "makeSyntacticPattern")) {
679  
        print(sp := calculations.makeSyntacticPattern(o));
680  
      } else
681  
        print("Unknown internal code");
682  
    }
683  
  }
684  
685  
  BEAObject findInput(S text) {
686  
    ret conceptWhereIC(BEAObject, type := "Input", +text);
687  
  }
688  
  
689  
  S addRewriteHTML(BEAObject o) {
690  
    ret ahref(addParamsToURL(crudLink(BEAObject),
691  
      cmd := "new",
692  
      title := "Add Rewrite",
693  
      f_type := "Rewrite",
694  
      f_text := getStringOpt text(o),
695  
      f_isRewriteOf := o.id, metaInfo_isRewriteOf := "concept",
696  
    ), "Add Rewrite");
697  
  }
698  
  
699  
  O serveIntegerLink(Req req, long id) {
700  
    BEAObject o = getConceptOpt BEAObject(id);
701  
    if (o != null)
702  
      ret htitle(str(o)) + hrefresh(conceptLink(o));
703  
    ret super.serveIntegerLink(req, id);
704  
  }
705  
  
706  
  void distributeTestNotification() {
707  
    distributeNotification("It is " + localTimeWithSeconds());
708  
  }
709  
  
710  
  void distributeNotification(S text) {
711  
    notificationQ.add(r {
712  
      /*for (User user)
713  
        if (nemptyAfterTrim(user.notificationSetting))
714  
          sendNotification(text);*/
715  
          
716  
      for (Pair<virtual WebSocket, WebSocketInfo> p : syncMapToPairs(webSockets)) {
717  
        // TODO: check user
718  
        S jsCode =
719  
          "window.createNotification({ theme: 'success', showDuration: 3000 })("
720  
          + jsonEncodeMap(message := text) + ");";
721  
        call(p.a, "send", jsonEncodeMap(eval := jsCode));
722  
      }
723  
    });
724  
  }
725  
  
726  
  void runInstruction(BEAObject o) {
727  
    if (o == null) ret;
728  
    try {
729  
      BEAObject instruction = o;
730  
      if (o.typeIs("Step in script"))
731  
        instruction = (BEAObject) cget(o, "instruction");
732  
      
733  
      if (instruction.typeIs("Instruction | List objects by type")) {
734  
        saveInstructionResult(o, beaList(getString typeToList(instruction)));
735  
        ret;
736  
      }
737  
      
738  
      if (instruction.typeIs("Instruction | List object types"))
739  
        ret with saveInstructionResult(o, distinctCIFieldValuesOfConcepts(BEAObject, "type"));
740  
741  
      if (instruction.typeIs("Instruction | Filter list by text starting with")) {
742  
        // find list made before
743  
        BEAObject scriptRun = cgetBEA scriptRun(o);
744  
        if (scriptRun == null) fail("Need to be run as part of script");
745  
        L<BEAObject> steps = scriptRunSteps(scriptRun);
746  
        int idx = indexOf(steps, o);
747  
        if (idx < 0) fail("Step not found in script run");
748  
        
749  
        L<BEAObject> list = firstNotNull(map(reversed(takeFirst(steps, idx)),
750  
          step -> optCast L(cget data(cgetBEA result(step)))));
751  
         
752  
        S prefix = getString prefix(instruction);
753  
        L<BEAObject> filtered = filter(list, obj -> swic(obj.text(), prefix);
754  
        saveInstructionResult(o, filtered);
755  
        ret;
756  
      }
757  
      
758  
      if (instruction.typeIs("Script")) {
759  
        BEAObject script = instruction;
760  
        BEAObject scriptRun = cnew BEAObject(type := "Script Run", +script);
761  
        
762  
        // Make an instance of all the instructions
763  
        
764  
        int i = 0;
765  
        new L<BEAObject> steps;
766  
        while not null (instruction = (BEAObject) cget(instruction, "step" + (++i))) {
767  
          BEAObject step = cnew(BEAObject, type := "Step in script", step := i, +scriptRun, +instruction);
768  
          steps.add(step);
769  
        }
770  
        
771  
        cset(scriptRun, +steps);
772  
        
773  
        // TODO: run steps?
774  
775  
        ret;
776  
      }
777  
      
778  
      cnew BEAObject(type := "Instruction Error", instruction := o, error := "Unknown instruction type");
779  
    } catch e {
780  
      cnew BEAObject(type := "Instruction Error", instruction := o, error := getStackTrace(e));
781  
    }
782  
  }
783  
784  
  BEAObject saveInstructionResult(BEAObject instruction, O data) {
785  
    BEAObject result = cnew BEAObject(type := "Instruction Result",
786  
      +instruction, +data);
787  
    cset(instruction, +result);
788  
    ret result;
789  
  }
790  
  
791  
  L<BEAObject> scriptRunSteps(BEAObject scriptRun) {
792  
    ret (L) cget steps(scriptRun);
793  
  }
794  
} // end of module
795  
796  
extend User {
797  
  S notificationSetting;
798  
}
799  
800  
concept BEAObject > ConceptWithGlobalID {
801  
  new Ref<UserPost> mirrorPost;
802  
803  
  void change :: after {
804  
    ((GazelleBEA) botMod()).rstUpdateBEAMirrors.add(this);
805  
  }
806  
807  
  void delete :: before {
808  
    cdelete(mirrorPost!);
809  
  }
810  
  
811  
  void updateMirrorPost {
812  
    GazelleBEA mod = cast botMod();
813  
    if (isDeleted() || !mod.mirrorBEAObjects) ret;
814  
815  
    if (!mirrorPost.has())
816  
      cset(this, mirrorPost := cnew UserPost(
817  
        type := "BEA Object",
818  
        creator := mod.internalUser(),
819  
        botInfo := "BEA Mirror Bot"));
820  
821  
    S text = structureString();
822  
823  
    cset(mirrorPost!, 
824  
      title := str(this),
825  
      +text);
826  
  }
827  
828  
  S structureString() {
829  
    S text = "Error";
830  
    pcall {
831  
      structure_Data data = new {
832  
        structure_ClassInfo newClass(Class c) {
833  
          structure_ClassInfo info = super.newClass(c);
834  
          if (c == Concept.Ref.class) {
835  
            info.special = true;
836  
            info.serializeObject = o -> {
837  
              Concept cc = cast deref((Concept.Ref) o);
838  
              //append("cu CRef " + (cc != null ? str(cc.id) : "null"), 3);
839  
              if (cc cast BEAObject)
840  
                append("CRef(gid=" + quote(cc.globalID()) + ")", 6);
841  
              else if (cc != null)
842  
                append("CRef(id=" + cc.id + ")", 6);
843  
              else
844  
                append("CRef", 1);
845  
            };
846  
          }
847  
          ret info;
848  
        }
849  
850  
        void setFields(structure_ClassInfo info, L<Field> fields) {
851  
          if (isSubclassOf(info.c, BEAObject)) {
852  
            // Don't serialize "refs" and "backRefs" fields
853  
            removeAll(fields,
854  
              getField(BEAObject, "refs"),
855  
              getField(BEAObject, "backRefs"));
856  
          }
857  
          super.setFields(info, fields);
858  
        }
859  
      };
860  
      
861  
      S struct = structure(this, data);
862  
      struct = dropLoadableUtilsPackageFromStruct(struct);
863  
      text = indentStructureString_firstLevels(1, struct);
864  
    }
865  
866  
    ret text;
867  
  }
868  
      
869  
  toString {
870  
    S type = strOrNull(cget type(this));
871  
    S s = super.toString();
872  
    
873  
    if (nempty(type)) {
874  
      s = type + " " + id;
875  
      if (eqic(type, "Match"))
876  
        s += appendBracketed(strOrNull(this~.label))
877  
          + " " + this.~mapping
878  
          + appendBracketed(this.~input + " + " + this~.pattern);
879  
    }
880  
    
881  
    bool enabled = eq(true, getOpt enabled(this));
882  
    if (enabled) s += " [enabled]";
883  
    
884  
    S text = or2(text(), getStringOpt name(this));
885  
    if (text != null)
886  
      s += " " + quote(text);
887  
      
888  
    O fc = this~.functionCalled;
889  
    if (fc != null) s += " " + fc;
890  
      
891  
    ret s;
892  
  }
893  
  
894  
  S type() { ret getStringOpt type(this); }
895  
  bool typeIs(S type) { ret eqic(type(), type); }
896  
  bool typeIsOneOf(S... types) { ret eqicOneOf(type(), types); }
897  
  
898  
  S text() { ret getStringOpt text(this); }
899  
} // end of BEAObject

Author comment

Began life as a copy of #1030602

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, vouqrxazstgt

No comments. add comment

Snippet ID: #1030958
Snippet name: Gazelle BEA [backup before new compact include]
Eternal ID of this version: #1030958/1
Text MD5: 020f0ed17458b8bfd5abf04202388c3b
Transpilation MD5: bc9aa5a11c85a5b575ceca5b45ecad36
Author: stefan
Category: javax
Type: JavaX source code (Dynamic Module)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-04-14 22:46:39
Source code size: 31371 bytes / 899 lines
Pitched / IR pitched: No / No
Views / Downloads: 84 / 116
Referenced in: [show references]