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)

!include #1003380 // Chat

sclass BotChat {
  Chat chat;
  O dialogBot, backend;
  JTextArea thoughtsArea;

  *(O dialogBot) { this(dialogBot, null); }
  
  *(O _dialogBot, O backend) {
    if (!isSubstanceLAF())
      blueSteel();

    if (backend is a S) backend = run((S) backend);
    this.backend = backend;
    dialogBot = _dialogBot;
    //callOpt(dialogBot, "noRewind");
    
    swingAndWait(r {
      thoughtsArea = new JTextArea;
      
      chat = new Chat;
      chat.logFile = getProgramFile("log.txt");
  
      chat.onEnter_post = r {
        try {
          fire(E.q(chat.input()));
        } catch e {
          postError(e);
        }
      };
      
      updateThoughts();
    
      JFrame frame = showFrame(/*"Chat with " +*/ programTitle(),
        hgrid(chat, withTitle("Thoughts", thoughtsArea)));
      setFrameWidth(frame, 800);
      chat.requestFocus();
    });
  }

  void postIt(final L<E> l) {
    try {
      for (int i = 0; i < l(l); i++) {
        E e = l.get(i);
        if (e.a())
          chatAppend("Bot: " + e.a + "\n");
        else if (e.state()) {
          chatAppend("[" + e.state + "]\n");
          final int _i = i+1;
          final Runnable l8r = r { awt { postIt(subList(l, _i)); } };
          if (isNewStyleBackend(backend)) {
            class TheBF implements BF {
              bool working, unknown;
              S value;
              
              pvoid working() { working = true; }
              pvoid done() { l8r.run(); }
              pvoid value(O o) { value = str(o); }
              pvoid unknown() { unknown = true; }
            };
            new TheBF bf;
            O exportedBF = proxy(_getClass(backend, "main.BF"), bf);
            print("Calling backend");
            call(backend, "action", e.state, exportedBF);
            if (bf.unknown) {
              print("Warning: Action unknown to backend: " + e.state);
            }
            if (bf.working) {
              print("Backend working");
              break;
            }
            if (bf.value != null)
              fire(E.state("result is " + bf.value));
          } else {
            if ((bool) call(backend, "action", e.state, l8r))
              break;
          }
        }
      }
      updateThoughts();
    } catch e {
      postError(e);
    }
  }
  
  void chatAppend(S s) {
    if (hideLine(s)) ret;
    chat.append(s);
  }
  
  // hide boring stuff
  bool hideLine(S s) {
    ret s.startsWith("[")
      && (matchStart("bot pauses", s)
       || matchStart("get", s));
  }
  
  void postError(Throwable e) {
    chat.append("= ERROR =\n" + getStackTrace(e) + "= ERROR =\n");
  }
  
  void updateThoughts() {
    S s;
    try {
      s = (S) callOpt(dialogBot, "thoughts");
    } catch e {
      s = getStackTrace(e);
    }
    if (empty(s)) s = "No thoughts in " + getClassName(dialogBot);   
    //print("Thoughts: " + s);
    thoughtsArea.setText(s);
  }
  
  void preset(S line) {
    chat.setInput(line);
  }
  
  void fire(E e) {
    call(dialogBot, "take", e);
    postIt(slurp(dialogBot, "getSingleOutput"));
  }
}

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: 663 / 2008
Referenced in: [show references]