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

97
LINES

< > BotCompany Repo | #1002759 // class LogicEngine (LIVE!)

JavaX fragment (include)

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();
    }
  }
  
  public void forget(Lisp pat) {
    int l = l(memory);
    for (int i = 0; i < l(memory); i++)
      if (snlMatch(pat, memory.get(i), new HashMap))
        memory.remove(i--);
    if (l(memory) != 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;
  }
  
  boolean runSingle(Lisp code, L<Lisp> out) {
    log.add("runSingle " + code);
    LThread thread = new LThread(this, code);
    thread.log = log;
    thread.statements.addAll(stm);
    //print("Statements: " + structure(thread.statements));
    boolean success = thread.run();
    out.addAll(thread.output);
    ret success;
  }
  
  // run multiple rules
  
  boolean runMulti(Lisp code, L<Lisp> out) {
    assertTrue(isJuxta(code));
    boolean success = false;
    for (Lisp sub : code) {
      if (runSingle(sub, out)) { // success
        success = true;
        if (fireOneRuleOnly)
          break;
      }
    }
    ret success;
  }
  
  L<Lisp> run() {
    new L<Lisp> out;
    if (isJuxta(code))
      runMulti(code, out);
    else
      runSingle(code, out);
    ret out;
  }
}

!include #1002752 // newest LThread

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: #1002759
Snippet name: class LogicEngine (LIVE!)
Eternal ID of this version: #1002759/1
Text MD5: 8ceae4f0c4fae0ecc3794ddf6ec30a8a
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-02-20 21:44:36
Source code size: 2268 bytes / 97 lines
Pitched / IR pitched: No / No
Views / Downloads: 587 / 762
Referenced in: [show references]