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

95
LINES

< > BotCompany Repo | #1012640 // ActualThoughtSpace [LIVE]

JavaX fragment (include)

sclass ActualThoughtSpace extends AbstractThoughtSpace implements AutoCloseable {
  new TripleIndex index;
  int limit; // 0 = no limit
  Map<TripleWeb, Bool> blocked = newWeakHashMap();
  Set<Symbol> blockedA, blockedB;

  *() {}
  *(int *limit) {}
  *(AbstractThoughtSpace parent) { super(parent); }
  
  public GlobalID postTriple(T3<Symbol> t, bool verified) {
    print("Posting: " + t + " / " + verified);
    if (limitReached()) fail("Thought space limit reached");
    TripleWeb w = tripleWeb(t);
    w.verified(verified);
    index.addTriple(w);
    ret w.globalID();    
  }
  
  // existence of triple has already been checked
  public GlobalID postTriple(T3<Symbol> 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;
  }
  
  /*public bool hasTriple(Symbol a, Symbol b, Symbol c) {
    ret index.getExact(a, b, c) != null;
  }*/
  
  L<TripleRef<Symbol>> get(Symbol s) {
    ret ai_triplesToTripleRefs_lazyList(s, getTriples(s));
  }
  
  L<TripleRef<Symbol>> get(Symbol s, int position) {
    ret ai_triplesToTripleRefs_lazyList(s, getTriples(s, position));
  }
  
  L<TripleWeb> getTriples(Symbol s) {
    L<TripleWeb> l = parent == null ? null : filterNonBlockedTriples_lazy(parent.getTriples(s));
    ret combineLists(l, index.getTriples(ai_shortenForIndex(s)));
  }
  
  L<TripleWeb> getTriples(Symbol s, int position) {
    L<TripleWeb> l = parent == null ? null : filterNonBlockedTriples_lazy(parent.getTriples(s, position));
    ret combineLists(l, index.getTriples(ai_shortenForIndex(s), position));
  }
  
  L<TripleWeb> getOneTwo(Symbol a, Symbol b) {
    ret index.getOneTwo(a, b);
  }
  
  L<TripleWeb> filterNonBlockedTriples_lazy(final L<TripleWeb> l) {
    class FilterList extends LazyList<TripleWeb> {
      final int n = l(l);
      public int size() { ret n; }
      public TripleWeb get(int i) {
        TripleWeb w = syncGet(l, i);
        if (w != null && isBlocked(w)) null;
        ret w;
      }
    }
    ret new FilterList;
  }
  
  L<TripleWeb> filterNonBlockedTriples(L<TripleWeb> l) {
    if (empty(blockedA) && empty(blockedB)) ret l;
    L<TripleWeb> out = emptyListWithCapacity(l);
    for (TripleWeb w : l)
      if (!isBlocked(w))
        out.add(w);
    ret out;
  }
  
  bool isBlocked(TripleWeb t) {
    ret contains(blockedA, t.a) || contains(blockedB, t.b);
  }
  
  int size() { ret index.size(); }

  public void close() {  
    thoughtSpaceDone(this);
  }
  
  Collection<TripleWeb> allTriples() { ret index.allTriples(); }
  
  void setBlockedA(Collection<? extends CharSequence> l) {
    blockedA = asSymbolSet(allToSymbol(l));
  }
}

Author comment

Began life as a copy of #1012051

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: #1012640
Snippet name: ActualThoughtSpace [LIVE]
Eternal ID of this version: #1012640/42
Text MD5: 5e3033968fdab463f6684128c8d26f02
Author: stefan
Category: javax / a.i.
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-01-13 21:13:29
Source code size: 2847 bytes / 95 lines
Pitched / IR pitched: No / No
Views / Downloads: 521 / 1094
Version history: 41 change(s)
Referenced in: [show references]