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

137
LINES

< > BotCompany Repo | #1002751 // SNL Logic Engine 4, multiple returns, as class

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Libraryless. Click here for Pure Java version (4368L/28K/90K).

!759

static S codeSnippetID = "#1002745";
static Lisp code;
static new L<Lisp> memory;
static LogicEngine engine;

p {
  code = snlToTree(loadSnippet(codeSnippetID));
  print(getProgramID() + ": parse result = " + code);
  load("memory");
}

static class LogicEngine implements IEngine {
  Lisp code;
  new L<Lisp> memory;
  new L<S> log;
  new L<Lisp> stm;
  boolean fireOneRuleOnly = true;
  
  *() {}
  *(Lisp *code, L<Lisp> *memory) {}
  *(S codeText, L<Lisp> *memory) {
    code = snlToTree(codeText);
  }
  
  void stmAdd(Lisp statement) {
    stm.add(statement);
  }

  Lisp snlMatchFirst(Lisp pat, L<Lisp> list, SNLMatches m) {
    for (Lisp l : list)
      if (snlMatch2(pat, l, m))
        ret l;
    ret null;
  }
  
  Lisp scanMem(Lisp pat, SNLMatches m) {
    ret snlMatchFirst(pat, memory, m);
  }
  
  Lisp scanMem(S pat, SNLMatches m) {
    ret snlMatchFirst(snlToTree_cached(pat), memory, m);
  }
  
  public void memorize(Lisp l) {
    if (!memory.contains(l)) {
      memory.add(l);
      saveMemory();
    }
  }
  
  void saveMemory() {} // override this for persistence
  
  public boolean yo(Lisp snl, SNLMatches m) {
    if (scanMem(snl, m) != null)
      ret true;
  
    if (snlMatch2("i < don't < know < X", snl, m))
      ret scanMem("* < said < X < is < *", m) == null; // TODO: vars probably don't work
  
    ret false;
  }
  
  L<Lisp> runSingle(Lisp code) {
    log.add("runSingle " + code);
    LThread thread = new LThread(this, code);
    thread.log = log;
    thread.statements.addAll(stm);
    //print("Statements: " + structure(thread.statements));
    ret thread.run() ? thread.output : null;
  }
  
  // run multiple rules
  
  L<Lisp> runMulti(Lisp code) {
    assertTrue(isJuxta(code));
    new L<Lisp> out;
    for (Lisp sub : code) {
      L<Lisp> l = runSingle(sub);
      if (l != null) { // success
        out.addAll(l);
        if (fireOneRuleOnly)
          break;
      }
    }
    ret out;
  }
  
  L<Lisp> run() {
    ret isJuxta(code)
      ? runMulti(code) 
      : runSingle(code);
  }
}

!include #1002752 // newest LThread

static S allToText(L<Lisp> trees) {
  new L<S> l;
  for (Lisp tree : trees)
    l.add(snlToText(tree));
  ret joinLines(l);
}

synchronized answer {
  if (!attn()) ret null;
  
  if (match("log " + parseSnippetID(programID()), s))
    ret engine == null ? "nullie haven't run yet": slackSnippet(fromLines(engine.log));
  
  if (match("memory " + parseSnippetID(programID()), s))
    ret slackSnippet(allToText(memory));
  
  // convert to SNL first
  s = or((S) callOpt/*Safe*/(getBot ("#1002700"), "naiveSNL", s), s);
  Lisp in = snlToTree(s);
  
  Lisp statement = snlToTree("user < says < [" + snlFromTree(in) + "]");
  engine = new LogicEngine(code, memory) {
    void saveMemory() {
      save("memory");
    }
  };
  engine.stmAdd(statement);
  
  L<Lisp> out = engine.run();
  if (nempty(out))
    ret "[SNL: " + snlFromTree(in) + "])\n" + allToText(out);
}

static Lisp quickrun(L<Lisp> memory, S userInput) {
  engine = new LogicEngine(code, memory);
  Lisp statement = snlToTree("user < says < [" + userInput + "]");
  engine.stmAdd(statement);
  ret snlMakeJuxta(engine.run());
}

Author comment

Began life as a copy of #1002746

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: #1002751
Snippet name: SNL Logic Engine 4, multiple returns, as class
Eternal ID of this version: #1002751/1
Text MD5: 82db206ce219bb2da015fc0fcb39d043
Transpilation MD5: 260b6191efb645079ed2e140640e3ea3
Author: stefan
Category: eleu / nl
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-02-20 04:19:03
Source code size: 3309 bytes / 137 lines
Pitched / IR pitched: No / No
Views / Downloads: 621 / 1080
Referenced in: [show references]