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

94
LINES

< > BotCompany Repo | #1005328 // searchConcept etc.

JavaX fragment (include) [tags: use-pretranspiled]

sinterface ISearchVar {
  bool set(O o);
  O get();
}

sclass MatchVar<A extends Concept> implements IRef<A>, ISearchVar {
  A value;
  Class<A> type;
  
  public A get() { ret value; }
  
  public S toString() { ret str(value); }
  
  bool canHold(O o) { ret type == null || isInstance(type, o); }
  
  public bool set(O o) { if (canHold(o)) { value = (A) o; true; } else false; }
}

sclass Subsearch {
  Class c;
  O[] params;

  *(Class *c, O[] params) {
    this.params = expandParams(c, params);
  }
}

static Subsearch some(Class<? extends Concept> c, O... params) {
  ret new Subsearch(c, params);
}

static <A extends Concept> A searchConcept(MatchVar<A> var, O... params) {
  assertNotNull(var.type); // must be typed to use in place of class
  ret var.value = searchConcept(var.type, params);
}

static <A extends Concept> A searchConcept(Class<A> c, O... params) {
  params = expandParams(c, params);
  // First, go through all the concepts of the primary type.
  for (A concept : list(c))
    if (searchConcept_checkParams(concept, params))
      ret concept; // Success!
  null;
}

static <A extends Concept> L<A> searchConcept_all(Class<A> c, O... params) {
  params = expandParams(c, params);
  new L<A> l;
  // First, go through all the concepts of the primary type.
  for (A concept : list(c))
    if (searchConcept_checkParams(concept, params))
      l.add(concept); // Success!
  ret l;
}

static bool searchConcept_checkParams(Concept c, O... params) {
  for (int i = 0; i+1 < l(params); i += 2) {
    S field = cast params[i];
    O val = derefRef(params[i+1]);
    O actual = cget(c, field);
    if (val instanceof Subsearch) {
      Subsearch s = cast val;
      if (!isInstance(s.c, actual)) false; // wrong type
      if (!searchConcept_checkParams((Concept) actual, s.params)) false; // bad params
    } else if (val instanceof ISearchVar) {
      ISearchVar v = cast val;
      if (!v.set(actual)) false;
    } else
      if (neq(actual, val)) false;
  }
  true; // all params match
}

sbool actually(Class c, O... params) {
  ret searchConcept(Actually, some(c, params)) != null;
}

/*sbool actually(MatchVar var, O... params) {
  ret searchConcept(Actually, some(var, params)) != null;
}*/

/*static L<Concept> listActually(MatchVar var, O... params) {
  ret map(func(Actually a) { a.what.get() }, searchConcept_all(Actually, some(var, params)));
}*/

static <A extends Concept> L<A> listActually(Class<A> c, O... params) {
  ret map(func(Actually a) { a.what.get() }, searchConcept_all(Actually, some(c, params)));
}

static <A extends Concept> MatchVar<A> typedVar(Class<A> c) {
  new MatchVar<A> var;
  var.type = c;
  ret var;
}

Author comment

Began life as a copy of #1005324

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: #1005328
Snippet name: searchConcept etc.
Eternal ID of this version: #1005328/2
Text MD5: b33ee7e26652decbb03600d671ba2474
Author: stefan
Category: javax / a.i.
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-08-05 22:34:09
Source code size: 2754 bytes / 94 lines
Pitched / IR pitched: No / No
Views / Downloads: 574 / 808
Version history: 1 change(s)
Referenced in: [show references]