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

119
LINES

< > BotCompany Repo | #1022804 // AttractorBot - trying to make persistable (attractors and preprocessors are transient)

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (7210L/48K).

//!include once #1022817 // Attractor Classes

abstract sclass AttractorBot extends AbstractSayAndInputBot implements Runnable {
  transient LS inputQueue = synchroLinkedList();
  transient Set<Thread> activeThreads = synchroSet();
  
  // functions taking a string and returning a string or collection of strings
  transient L preprocessors = synchroList();
  
  transient F1<S, Bool> isYes = f<S, Bool> isYes_1;
  new LinkedHashSet<S> facts;
  new LinkedHashSet<S> assumptions;
  
  transient L<Attractor> standardAttractors = synchroList(); // permanent
  transient L<Attractor> standardLowPrioAttractors = synchroList(); // permanent
  transient L<Attractor> activeAttractors = synchroList();   // temporary
  transient L<Attractor> lowPrioAttractors = synchroList();  // temporary
  
  S line;
    
  *() {
    input = f<S> getInput;
    standardPreprocessors();
  }
    
  void remember(S fact) {
    if (facts.add(fact))
      print("[Bot learned:] " + fact);
  }
  
  void assume(S fact) {
    if (assumptions.add(fact))
      print("[Bot assumes:] " + fact);
  }
  
  S getInput() {
    while (empty(inputQueue))
      sleep(10);
    ret trim(popFirst(inputQueue));
  }
  
  bool yes() {
    ret callF(isYes, input());
  }
  
  // call this instead of run() so thread is marked
  void run_public() {
    temp tempAddToCollection(activeThreads, currentThread());
    run();
  }
  
  void addInput(S s) {
    inputQueue.add(s);
    line = s;
    if (empty(activeThreads))
      attractorBattle();
  }
  
  void attractorBattle {
    if (empty(inputQueue)) ret;
    S input = getInput();
    print("Input: " + input);
    
    // high prio active, then standing attractors, then low prio active
    L<Attractor> attractors = getAndClearList(activeAttractors);
    addAll(attractors, standardAttractors);
    addAll(attractors, getAndClearList(lowPrioAttractors));
    addAll(attractors, standardLowPrioAttractors);
    
    if (empty(attractors)) ret with print("No attractors, ignoring input");
    new L<Attractor> matchingAttractors;
    for (Attractor a : attractors) pcall {
      if (a.matches_public(input)) {
        matchingAttractors.add(a);
      }
    }
    if (empty(matchingAttractors))
      ret with print("No matching attractors, ignoring input");
    print("Got " + n2(matchingAttractors, "matching attractor") + ": " + matchingAttractors);
    Attractor winner = first(matchingAttractors);
    if (l(matchingAttractors) > 1) print("Choosing first attractor: " + winner);
    runAttractor(winner);
  }
  
  // attractors can use AttractorBot.this.say() [old] or emitAnswer() [preferred]
  void runAttractor(Attractor a) {
    say(getVars(getEmittedAnswers(a)));
  }
  
  void addAttractor(Attractor a) { addAttractors(a); }
  void tempAttractor(Attractor a) { addAttractors(a); }
  void addAttractors(Attractor... attractors) {
    addAll(activeAttractors, attractors);
  }
  
  void addLowPrioAttractor(Attractor a) { add(lowPrioAttractors, a); }
  void lowPrioAttractor(Attractor a) { addLowPrioAttractor(a); }
  void defaultAttractor(Attractor a) { addLowPrioAttractor(a); }
  
  void addStandardAttractor(Attractor a) { add(standardAttractors, a); }
  void standardAttractor(Attractor a) { addStandardAttractor(a); }
  
  void standardAttractors(Attractor... attractors) {
    addAll(standardAttractors, attractors);
  }
  
  void standardLowPrioAttractor(Attractor a) { add(standardLowPrioAttractors, a); }
  
  void addPreprocessors(O... l) {
    addAll(preprocessors, l);
  }
  
  void standardPreprocessors() {
    addPreprocessors(f ai_dropLeadingAndTrailingAppellations_sortedByLength);
  }
  
  // override me for start-up behavior
  run {}
}

Author comment

Began life as a copy of #1022801

download  show line numbers  debug dex  old transpilations   

Travelled to 7 computer(s): bhatertpkbcr, cfunsshuasjs, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1022804
Snippet name: AttractorBot - trying to make persistable (attractors and preprocessors are transient)
Eternal ID of this version: #1022804/44
Text MD5: d61f21ddd511af25aaa02aaf984dea58
Transpilation MD5: 0c602e70ad9c5a9af1ddb45443f96379
Author: stefan
Category: javax / a.i.
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-06-24 17:15:48
Source code size: 3795 bytes / 119 lines
Pitched / IR pitched: No / No
Views / Downloads: 351 / 912
Version history: 43 change(s)
Referenced in: [show references]