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

154
LINES

< > BotCompany Repo | #1027566 // Chat Bot Frontend [backup v1]

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

Uses 911K of libraries. Click here for Pure Java version (16184L/86K).

1  
!7
2  
3  
concept Cmd {
4  
  S patterns; // for mmo_parsePattern
5  
  S exampleInputs; // line-separated
6  
  S cmd; // a star pattern recognized by the backend
7  
  S questionsForArguments; // line-separated
8  
  int index;
9  
  
10  
  transient MMOPattern parsedPattern;
11  
  void change { parsedPattern = null; super.change(); }
12  
  MMOPattern parsedPattern() {
13  
    if (parsedPattern == null) parsedPattern = mmo_parsePattern(patterns);
14  
    ret parsedPattern;
15  
  }
16  
  
17  
  sS _fieldOrder = "exampleInputs patterns cmd index";
18  
}
19  
20  
cmodule ChatBotFrontend > DynCRUD<Cmd> {
21  
  switchable S backendModuleLibID = "#1027443/Scenarios";
22  
  O currentAttractor;
23  
24  
  class HandleArgument implements IF1<S> {
25  
    S cmd;
26  
    LS argQuestions;
27  
    new LS arguments;
28  
29  
    *() {}
30  
    *(S *cmd, LS *argQuestions) {}
31  
32  
    // process an argument
33  
    public S get(S arg) {
34  
      arguments.add(arg);
35  
      if (l(arguments) >= countAsteriskTokens(cmd))
36  
        ret sendToBackend(format_quoteAll(cmd, asObjectArray(arguments)));
37  
      else
38  
        ret handleQuestionWithArguments(this);
39  
    }
40  
41  
    S currentQuestion() {
42  
      ret or2(_get(argQuestions, l(arguments)), "Need argument");
43  
    }
44  
  }
45  
46  
  runnable class Cancel { setField(currentAttractor := null); }
47  
48  
  class UndoHandler implements IF1<Bool, S> {
49  
    public S get(Bool yes) {
50  
      if (!yes) ret "OK"; // user said no
51  
      ret (S) sendToBackend("undo");
52  
    }
53  
  }
54  
55  
  start {
56  
    dm_watchFieldAndNow backendModuleLibID(r {
57  
      dm_setModuleName("Frontend for " + dm_moduleName(backend()))
58  
    });
59  
    
60  
    crud.multiLineFields = litset("exampleInputs", "questionsForArguments");
61  
    crud.sorter = func(Cl<Cmd> l) -> L<Cmd> { sortByField index(l) };
62  
    crud.formFixer = 48;
63  
  }
64  
65  
  S handleCommand(Cmd cmd) {
66  
    if (cmd == null) null;
67  
 
68  
    print("handleCommand " + cmd.cmd);
69  
    
70  
    if (match("undo after confirm", cmd.cmd)) {
71  
      O undo = dm_call(backend(), 'lastUndo);
72  
      if (undo == null) ret "Nothing to undo";
73  
      new WaitForAnswer_YesNo attractor;
74  
      attractor.question = "Undo " + undo + "?";
75  
      setField(currentAttractor := attractor);
76  
      print("Set attractor to: " + attractor);
77  
      attractor.processAnswer = new UndoHandler;
78  
      attractor.cancelSilently = new Cancel;
79  
      ret attractor.question;
80  
    }
81  
    
82  
    if (hasAsteriskTokens(cmd.cmd))
83  
      ret handleQuestionWithArguments(
84  
        new HandleArgument(cmd.cmd, tlft(cmd.questionsForArguments)));
85  
    else
86  
      ret sendToBackend(cmd.cmd);
87  
  }
88  
89  
  S handleQuestionWithArguments(HandleArgument handler) {
90  
    new WaitForName attractor;
91  
    attractor.question = handler.currentQuestion();
92  
    setField(currentAttractor := attractor);
93  
    print("Set attractor to: " + attractor);
94  
    attractor.processName = handler;
95  
    attractor.cancelSilently = new Cancel;
96  
    ret attractor.question;
97  
  }
98  
99  
  S sendToBackend(S cmd) {
100  
    ret (S) dm_call(backend(), 'answer, cmd);
101  
  }
102  
  
103  
  S backend() {
104  
    ret dm_require(backendModuleLibID);
105  
  }
106  
  
107  
  afterVisualize {
108  
    addComponents(crud.buttons,
109  
      jbutton("Talk to me", rThread talkToMe),
110  
      jPopDownButton_noText("Consistency check", rThread consistencyCheck));
111  
  }
112  
113  
  void talkToMe enter { dm_showConversationPopupForModule(); }
114  
115  
  void consistencyCheck enter {
116  
    reindex();
117  
    L<Cmd> cmds = list(Cmd);
118  
    L<MMOConsistencyError> errors = mmo_consistencyCheck(
119  
      map(cmds, cmd -> pair(cmd.exampleInputs, cmd.patterns)));
120  
    if (empty(errors)) infoBox("No consistency problems");
121  
    else
122  
      dm_showText(n2(errors, "consistency problem"), lines(errors));
123  
  }
124  
125  
  void reindex {
126  
    int index = 1;
127  
    for (Cmd cmd : conceptsSortedByField Cmd('index))
128  
      cset(cmd, index := index++);
129  
  }
130  
  
131  
  // API
132  
  
133  
  void copyCommandsFromBackend() {
134  
    for (S cmd : allIfQuotedPatterns(loadSnippet(beforeSlash(backendModuleLibID)))
135  
)
136  
      uniqConcept(+cmd);
137  
  }
138  
  
139  
  S answer(S s) {
140  
    if (currentAttractor != null) {
141  
      print("Sending to attractor " + currentAttractor + ": " + s);
142  
      S a = strOrNull(call(currentAttractor, 'answer, s));
143  
      if (a != null) {
144  
        print("Attractor said: " + a);
145  
        ret a;
146  
      }
147  
    }
148  
    Cmd cmd = mmo_matchMultiWithTypos(1, 
149  
      filter(conceptsSortedByField Cmd('index), c -> nempty(c.patterns)),
150  
      c -> c.parsedPattern(), s);
151  
    if (cmd != null) ret handleCommand(cmd);
152  
    ret sendToBackend(s);
153  
  }
154  
}

Author comment

Began life as a copy of #1027518

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: #1027566
Snippet name: Chat Bot Frontend [backup v1]
Eternal ID of this version: #1027566/1
Text MD5: 60e69050c041eb9a33afaa12a38dd0d3
Transpilation MD5: cf792c16a2110ff73851adbe25fd927f
Author: stefan
Category: javax
Type: JavaX source code (Dynamic Module)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-03-23 23:38:42
Source code size: 4454 bytes / 154 lines
Pitched / IR pitched: No / No
Views / Downloads: 152 / 207
Referenced in: [show references]