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

64
LINES

< > BotCompany Repo | #1012676 // ActualThoughtSpace with original blocking (very flexible but slow)

JavaX fragment (include)

sclass ActualThoughtSpace extends AbstractThoughtSpace {
  new TripleIndex index;
  int limit; // 0 = no limit
  L<Triple<S>> blockedPatterns = listWithNotify(r { blocked.clear(); });
  Map<TripleWeb, Bool> blocked = newWeakHashMap();

  *() {}
  *(int *limit) {}
  *(AbstractThoughtSpace parent) { super(parent); }
  
  // existence of triple has already been checked
  public GlobalID postTriple(T3<S> t) {
    if (limitReached()) fail("Thought space limit reached");
    TripleWeb w = tripleWeb(t);
    index.addTriple(w);
    ret w.globalID();
  }
  
  bool limitReached() {
    ret limit > 0 && size() >= limit;
  }
  
  L<TripleRef<S>> get(S s) {
    ret ai_triplesToTripleRefs_lazyList(s, getTriples(s));
  }
  
  L<TripleWeb> getTriples(S s) {
    L<TripleWeb> l = filterNonBlockedTriples(parent.getTriples(s));
    ret combineLists(l, index.getTriples(ai_shortenForIndex(s)));
  }
  
  L<TripleRef<S>> filterNonBlocked(L<TripleRef<S>> l) {
    if (empty(blockedPatterns)) ret l;
    L<TripleRef<S>> out = emptyListWithCapacity(l);
    for (TripleRef<S> ref : l)
      if (!isBlocked((TripleWeb) ref.triple))
        out.add(ref);
    ret out;
  }
  
  L<TripleWeb> filterNonBlockedTriples(L<TripleWeb> l) {
    if (empty(blockedPatterns)) ret l;
    L<TripleWeb> out = emptyListWithCapacity(l);
    for (TripleWeb w : l)
      if (!isBlocked(w))
        out.add(w);
    ret out;
  }
  
  bool isBlocked(TripleWeb t) {
    Bool b = blocked.get(t);
    if (b != null) ret b;
    b = false;
    for (T3<S> pat : blockedPatterns)
      if (simpleMatchTriple_dollarVars(pat, t) != null) {
        b = true;
        break;
      }
    blocked.put(t, b);
    ret b;
  }
  
  int size() { ret index.size(); }
}

Author comment

Began life as a copy of #1012640

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: #1012676
Snippet name: ActualThoughtSpace with original blocking (very flexible but slow)
Eternal ID of this version: #1012676/3
Text MD5: 5922e919b7fb84edc1974256f702b39e
Author: stefan
Category: javax / a.i.
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-12-10 02:53:14
Source code size: 1774 bytes / 64 lines
Pitched / IR pitched: No / No
Views / Downloads: 359 / 557
Version history: 2 change(s)
Referenced in: [show references]