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

125
LINES

< > BotCompany Repo | #1002752 // LThread with SNLMatches and Engine link (CURRENT)

JavaX fragment (include)

// Logic thread

static interface IEngine {
  public boolean yo(Native n);
  public void memorize(Lisp statement);
  public void forget(Lisp statement);
}

static class LThread {
  IEngine engine;
  new L<Lisp> statements;
  new SNLMatches m; // current variable assignments
  L<Lisp> ops;
  int opIdx;
  new L<Lisp> output;
  L<S> log;
  
  *(Lisp code) {
    this(null, code);
  }
  
  *(IEngine *engine, Lisp code) {
    ops = snlSplitOps(code);
  }
  
  boolean done() {
    ret opIdx >= l(ops);
  }
  
  // returns false if failed
  boolean step() {
    Lisp code = ops.get(opIdx++);
    boolean ok = stepImpl(code);
    if (log != null)
      log.add((ok ? "[OK] ": "[FAIL] ") + " step " + snlFromTree(code) + " with " + structure(m));
    ret ok;
  }
  
  void say(Lisp b) {
    output.add(snlApply(b, m.map));
  }
  
  boolean stepImpl(Lisp code) {
    if (code.isic("if *", "and *", "assume *")) {
      Lisp a = code.get(0);
      Lisp b = snlApply(a, m.map);
      
      if (query(b))
        ret true;
      
      // Not matched, fail
      
      if (code.isic("assume *"))
        say(snlToTree("assumption < failed < : < [" + snlFromTree(b) + "]"));
            
      ret false;
    }
    
    if (code.isic("say *", "then *")) {
      Lisp b = code.get(0);
      //print ("m: " + structure (m) + ", b: " + structure (b));
      output.add(snlApply(b, m.map));
      ret true;
    }
    
    if (code.isic("memorize *")) {
      if (engine != null) {
        Lisp b = snlApply(code.get(0), m.map);
        engine.memorize(b);
      }
      ret true;
    }
    
    if (code.isic("forget *")) {
      if (engine != null) {
        Lisp b = snlApply(code.get(0), m.map);
        engine.forget(b);
      }
      ret true;
    }
    
    // unknown code
    say(snlToTree("unknown < instruction < : < [" + snlFromTree(code) + "]"));
    ret false;
  }
  
  // true if succeeded, false if failed
  boolean run() {
    while (!done())
      if (!step()) {
        print("fail");
        ret false;
      }
    ret true;
  }
  
  boolean query(Lisp b) {
    // try all statements in context
    
    for (Lisp s : statements) {
    /*  if (snlMatch2(b, s, m)) */

      if (snlMatch2(s, b, new SNLMatches))
        ret true;
    }

    // try the low-level engine
    
    try {
      if (engine != null) {
        Native n = new Native(b);
        if (engine.yo(n))
          ret m.addAll(n.m);
      }
    } catch (Exception e) {
      
      if (log != null) {
        log.add("ERROR: " + getStackTrace(e));
        print(e);
      }
    }
      
    ret false;
  }
}

Author comment

Began life as a copy of #1002750

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1002752
Snippet name: LThread with SNLMatches and Engine link (CURRENT)
Eternal ID of this version: #1002752/1
Text MD5: 5e1a9b7b58a78b2d6c697a217c844a04
Author: stefan
Category: eleu / nl
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-02-26 05:32:34
Source code size: 2707 bytes / 125 lines
Pitched / IR pitched: No / No
Views / Downloads: 680 / 1230
Referenced in: #1002751 - SNL Logic Engine 4, multiple returns, as class
#1002759 - class LogicEngine (LIVE!)
#1002781 - Rewriting Bot (LIVE)
#1002810 - LThread with backtracking (dev.)
#3000382 - Answer for ferdie (>> t = 1, f = 0)