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

123
LINES

< > BotCompany Repo | #1003499 // class BotChat - Swing-based bot chat with dialog bot + backend (current)

JavaX fragment (include)

1  
!include #1003380 // Chat
2  
3  
sclass BotChat {
4  
  Chat chat;
5  
  O dialogBot, backend;
6  
  JTextArea thoughtsArea;
7  
8  
  *(O dialogBot) { this(dialogBot, null); }
9  
  
10  
  *(O _dialogBot, O backend) {
11  
    if (!isSubstanceLAF())
12  
      blueSteel();
13  
14  
    if (backend is a S) backend = run((S) backend);
15  
    this.backend = backend;
16  
    dialogBot = _dialogBot;
17  
    //callOpt(dialogBot, "noRewind");
18  
    
19  
    swingAndWait(r {
20  
      thoughtsArea = new JTextArea;
21  
      
22  
      chat = new Chat;
23  
      chat.logFile = getProgramFile("log.txt");
24  
  
25  
      chat.onEnter_post = r {
26  
        try {
27  
          fire(E.q(chat.input()));
28  
        } catch e {
29  
          postError(e);
30  
        }
31  
      };
32  
      
33  
      updateThoughts();
34  
    
35  
      JFrame frame = showFrame(/*"Chat with " +*/ programTitle(),
36  
        hgrid(chat, withTitle("Thoughts", thoughtsArea)));
37  
      setFrameWidth(frame, 800);
38  
      chat.requestFocus();
39  
    });
40  
  }
41  
42  
  void postIt(final L<E> l) {
43  
    try {
44  
      for (int i = 0; i < l(l); i++) {
45  
        E e = l.get(i);
46  
        if (e.a())
47  
          chatAppend("Bot: " + e.a + "\n");
48  
        else if (e.state()) {
49  
          chatAppend("[" + e.state + "]\n");
50  
          final int _i = i+1;
51  
          final Runnable l8r = r { awt { postIt(subList(l, _i)); } };
52  
          if (isNewStyleBackend(backend)) {
53  
            class TheBF implements BF {
54  
              bool working, unknown;
55  
              S value;
56  
              
57  
              pvoid working() { working = true; }
58  
              pvoid done() { l8r.run(); }
59  
              pvoid value(O o) { value = str(o); }
60  
              pvoid unknown() { unknown = true; }
61  
            };
62  
            new TheBF bf;
63  
            O exportedBF = proxy(_getClass(backend, "main.BF"), bf);
64  
            print("Calling backend");
65  
            call(backend, "action", e.state, exportedBF);
66  
            if (bf.unknown) {
67  
              print("Warning: Action unknown to backend: " + e.state);
68  
            }
69  
            if (bf.working) {
70  
              print("Backend working");
71  
              break;
72  
            }
73  
            if (bf.value != null)
74  
              fire(E.state("result is " + bf.value));
75  
          } else {
76  
            if ((bool) call(backend, "action", e.state, l8r))
77  
              break;
78  
          }
79  
        }
80  
      }
81  
      updateThoughts();
82  
    } catch e {
83  
      postError(e);
84  
    }
85  
  }
86  
  
87  
  void chatAppend(S s) {
88  
    if (hideLine(s)) ret;
89  
    chat.append(s);
90  
  }
91  
  
92  
  // hide boring stuff
93  
  bool hideLine(S s) {
94  
    ret s.startsWith("[")
95  
      && (matchStart("bot pauses", s)
96  
       || matchStart("get", s));
97  
  }
98  
  
99  
  void postError(Throwable e) {
100  
    chat.append("= ERROR =\n" + getStackTrace(e) + "= ERROR =\n");
101  
  }
102  
  
103  
  void updateThoughts() {
104  
    S s;
105  
    try {
106  
      s = (S) callOpt(dialogBot, "thoughts");
107  
    } catch e {
108  
      s = getStackTrace(e);
109  
    }
110  
    if (empty(s)) s = "No thoughts in " + getClassName(dialogBot);   
111  
    //print("Thoughts: " + s);
112  
    thoughtsArea.setText(s);
113  
  }
114  
  
115  
  void preset(S line) {
116  
    chat.setInput(line);
117  
  }
118  
  
119  
  void fire(E e) {
120  
    call(dialogBot, "take", e);
121  
    postIt(slurp(dialogBot, "getSingleOutput"));
122  
  }
123  
}

Author comment

Began life as a copy of #1003416

download  show line numbers  debug dex  old transpilations   

Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1003499
Snippet name: class BotChat - Swing-based bot chat with dialog bot + backend (current)
Eternal ID of this version: #1003499/1
Text MD5: 05143784b7243cf1bf77bb904b04dc67
Author: stefan
Category: javax / talking robots
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-07-19 16:48:45
Source code size: 3205 bytes / 123 lines
Pitched / IR pitched: No / No
Views / Downloads: 674 / 2022
Referenced in: [show references]