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]

1  
sinterface ISearchVar {
2  
  bool set(O o);
3  
  O get();
4  
}
5  
6  
sclass MatchVar<A extends Concept> implements IRef<A>, ISearchVar {
7  
  A value;
8  
  Class<A> type;
9  
  
10  
  public A get() { ret value; }
11  
  
12  
  public S toString() { ret str(value); }
13  
  
14  
  bool canHold(O o) { ret type == null || isInstance(type, o); }
15  
  
16  
  public bool set(O o) { if (canHold(o)) { value = (A) o; true; } else false; }
17  
}
18  
19  
sclass Subsearch {
20  
  Class c;
21  
  O[] params;
22  
23  
  *(Class *c, O[] params) {
24  
    this.params = expandParams(c, params);
25  
  }
26  
}
27  
28  
static Subsearch some(Class<? extends Concept> c, O... params) {
29  
  ret new Subsearch(c, params);
30  
}
31  
32  
static <A extends Concept> A searchConcept(MatchVar<A> var, O... params) {
33  
  assertNotNull(var.type); // must be typed to use in place of class
34  
  ret var.value = searchConcept(var.type, params);
35  
}
36  
37  
static <A extends Concept> A searchConcept(Class<A> c, O... params) {
38  
  params = expandParams(c, params);
39  
  // First, go through all the concepts of the primary type.
40  
  for (A concept : list(c))
41  
    if (searchConcept_checkParams(concept, params))
42  
      ret concept; // Success!
43  
  null;
44  
}
45  
46  
static <A extends Concept> L<A> searchConcept_all(Class<A> c, O... params) {
47  
  params = expandParams(c, params);
48  
  new L<A> l;
49  
  // First, go through all the concepts of the primary type.
50  
  for (A concept : list(c))
51  
    if (searchConcept_checkParams(concept, params))
52  
      l.add(concept); // Success!
53  
  ret l;
54  
}
55  
56  
static bool searchConcept_checkParams(Concept c, O... params) {
57  
  for (int i = 0; i+1 < l(params); i += 2) {
58  
    S field = cast params[i];
59  
    O val = derefRef(params[i+1]);
60  
    O actual = cget(c, field);
61  
    if (val instanceof Subsearch) {
62  
      Subsearch s = cast val;
63  
      if (!isInstance(s.c, actual)) false; // wrong type
64  
      if (!searchConcept_checkParams((Concept) actual, s.params)) false; // bad params
65  
    } else if (val instanceof ISearchVar) {
66  
      ISearchVar v = cast val;
67  
      if (!v.set(actual)) false;
68  
    } else
69  
      if (neq(actual, val)) false;
70  
  }
71  
  true; // all params match
72  
}
73  
74  
sbool actually(Class c, O... params) {
75  
  ret searchConcept(Actually, some(c, params)) != null;
76  
}
77  
78  
/*sbool actually(MatchVar var, O... params) {
79  
  ret searchConcept(Actually, some(var, params)) != null;
80  
}*/
81  
82  
/*static L<Concept> listActually(MatchVar var, O... params) {
83  
  ret map(func(Actually a) { a.what.get() }, searchConcept_all(Actually, some(var, params)));
84  
}*/
85  
86  
static <A extends Concept> L<A> listActually(Class<A> c, O... params) {
87  
  ret map(func(Actually a) { a.what.get() }, searchConcept_all(Actually, some(c, params)));
88  
}
89  
90  
static <A extends Concept> MatchVar<A> typedVar(Class<A> c) {
91  
  new MatchVar<A> var;
92  
  var.type = c;
93  
  ret var;
94  
}

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: 576 / 811
Version history: 1 change(s)
Referenced in: [show references]