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

254
LINES

< > BotCompany Repo | #1030762 // AGI Pattern Spike 1 v2 [OK]

JavaX source code (Dynamic Module) [tags: use-pretranspiled] - run with: Stefan's OS

Uses 1059K of libraries. Click here for Pure Java version (22727L/121K).

1  
!7
2  
3  
sinterface IBEAReactor {
4  
  BEAObject uniqResult(Class<? extends BEAObject> c, O... params);
5  
}
6  
7  
extend BEAObject {
8  
  bool canReactWith(BEAObject o) {
9  
    ret isInstanceOfAny(o, (Cl) reactableWithClasses());
10  
  }
11  
  
12  
  Cl<Class<? extends BEAObject>> reactableWithClasses() {
13  
    ret (Cl) collectFirstMethodArguments(findMethodsNamed_nonSynthetic(this, "reactWith"));
14  
  }
15  
}
16  
17  
beaConcept BReactor {}
18  
19  
beaConcept BInput {
20  
  S text;
21  
  
22  
  toString { ret "[\*id*/] Input " + quote(text); }
23  
}
24  
25  
beaConcept BOutput {
26  
  S text;
27  
  
28  
  toString { ret "[\*id*/] Output " + quote(text); }
29  
}
30  
31  
beaConcept BPattern {
32  
  S text;
33  
  
34  
  toString { ret "[\*id*/] " + shortDynName(this) + " " + quote(text); }
35  
  
36  
  void reactWith(BInput input, IBEAReactor reactor) {}
37  
  
38  
  @Override Map<S, FieldMigration> _fieldMigrations() {
39  
    ret litmap(pattern := new FieldMigration("text"));
40  
  }
41  
}
42  
43  
beaConcept BStarPattern > BPattern {
44  
  void reactWith(BInput input, IBEAReactor reactor) {
45  
    LS mapping = matchesToStringList(flexMatchIC_first(text, input.text));
46  
    if (mapping == null) ret;
47  
    reactor.uniqResult(BEAObject,
48  
      type := "match",
49  
      +input,
50  
      pattern := this,
51  
      +mapping);
52  
  }
53  
}
54  
55  
beaConcept BAngleVarPattern > BPattern {
56  
  void reactWith(BInput input, IBEAReactor reactor) {
57  
    SS mapping = flexMatchAngleBracketVarsIC_first(text, input.text);
58  
    if (mapping == null) ret;
59  
    reactor.uniqResult(BEAObject,
60  
      type := "match",
61  
      +input,
62  
      pattern := this,
63  
      +mapping);
64  
  }
65  
}
66  
67  
beaConcept BMatch {
68  
  new Ref<BPattern> pattern;
69  
  SS mapping;
70  
}
71  
72  
beaConcept BMatchToScenario {
73  
  void reactWith(BMatch match, IBEAReactor reactor) {
74  
    reactor.uniqResult(BEAObject,
75  
      type := "scenario",
76  
      text := mapToLines(match.mapping,
77  
        (a, b) -> format_curly("* is a *.", b, a));
78  
  }
79  
}
80  
81  
beaConcept BScenarioToOutput {
82  
  bool canReactWith(BEAObject o) {
83  
    ret o.typeIs("scenario");
84  
  }
85  
  
86  
  void reactWith(BEAObject scenario, IBEAReactor reactor) {
87  
    reactor.uniqResult(BOutput,
88  
      type := "Output",
89  
      text := getString text(scenario));
90  
  }
91  
}
92  
93  
concept Reactable {
94  
  BEAObject a, b;
95  
}
96  
97  
cmodule2 > DynCRUD_v2<BEAObject> {
98  
  S input = "UBports community delivers 'second-largest release of Ubuntu Touch ever'", output;
99  
  switchable int maxObjects = 1000;
100  
  
101  
  transient new BEAUtils beaUtils;
102  
  transient new Concepts reactableConcepts;
103  
  transient ReliableSingleThread rstUpdateReactables = dm_rstWithDelay(this, 100, r updateReactables);
104  
105  
  { tableFontSize = 15; }
106  
107  
  start {
108  
    ensureConceptClassIsIndexed(BEAObject);
109  
    crud.humanizeFieldNames = false;
110  
    crud.showBackRefs = true;
111  
    crud.specialFieldsForItem = a -> {
112  
      MapSO map = litorderedmap(crud.hID, str(a.id));
113  
      S j = crud.javaClassDescForItem(a);
114  
      mapPut(map, "Class/Type", joinUniqueNemptiesCI("/", j, a.type()));
115  
      ret map;
116  
    };
117  
    crud.itemToMap_inner = a -> {
118  
      MapSO map = crud.itemToMap_inner_base(a);
119  
      map.remove("type");
120  
      ret map;
121  
    };
122  
    
123  
    onConceptChangesAndNow(rstUpdateReactables);
124  
    
125  
    thread {
126  
      uniq BMatchToScenario();
127  
      uniq BScenarioToOutput();
128  
      //uniq BPattern(text := "* delivers *");
129  
      uniq BAngleVarPattern(text := "<company> delivers <product>");
130  
    }
131  
  }
132  
  
133  
  visualize {
134  
    JComponent c = jvsplit(0.2, jhsplit(jCenteredSection("Input",
135  
      withRightAlignedButtons(
136  
        dm_wordWrapTextArea input(),
137  
        "Load input", rThreadEnter loadInput,
138  
        "Load & process", rThreadEnter loadAndProcess)),
139  
      dm_wordWrapTextAreaAsSection output()),
140  
      jvsplit(0.75,
141  
        jCenteredSection("Objects", super.visualize()),
142  
        jCenteredSection("Reactables",
143  
          withRightAlignedButtons(
144  
            new JConceptsTable(reactableConcepts, Reactable),
145  
            "React all", rThreadEnter reactAll))));
146  
147  
    tablePopupMenuFirst(table(), (menu, row) -> {
148  
      BEAObject o = getItem(row);
149  
      print("Item: " + o);
150  
      if (o == null) ret;
151  
      
152  
      pcall {
153  
        L<BEAObject> partners = filter(list(BEAObject),
154  
          partner -> o.canReactWith(partner));
155  
        if (nempty(partners))
156  
          addScrollingSubMenu(menu, "React with", menu2 -> {
157  
            for (BEAObject p : partners)
158  
              addMenuItem(menu2, str(p), rThread {
159  
                new BEAReactor("User reacts " + o + " with " + p).react(o, p);
160  
              });
161  
          });
162  
      }
163  
    });
164  
    
165  
    addButton("Delete reactors", rThreadEnter deleteReactors);
166  
    
167  
    ret c;
168  
  } // end of visualize
169  
  
170  
  class BEAReactor {
171  
    BReactor reactorConcept;
172  
    
173  
    *(S purpose) {
174  
      reactorConcept = cnew BReactor(+purpose);
175  
    }
176  
    
177  
    void react(BEAObject a, BEAObject b) {
178  
      call(a, 'reactWith, b, new IBEAReactor {
179  
        public BEAObject uniqResult(Class<? extends BEAObject> c, O... params) {
180  
          BEAObject product = cnew(c, paramsPlus(params,
181  
            _fromReaction := ll(a, b),
182  
            _reactor := reactorConcept));
183  
          product = beaUtils.autoMigrate(product);
184  
          BEAObject existing = findExisting(product);
185  
          print("Existing for " + product + ": " + existing);
186  
          if (existing != null) {
187  
            cdelete(product);
188  
            ret existing;
189  
          } else {
190  
            onReactionProduct(product);
191  
            ret product;
192  
          }
193  
        }
194  
      });
195  
    }
196  
  }
197  
  
198  
  // find an existing reactor-made object with the same values
199  
  BEAObject findExisting(BEAObject o) {
200  
    MapSO map = allConceptFieldsAsMap(o);
201  
    removeAll(map, "_reactor", "globalID");
202  
    //print(+map);
203  
    ret firstThat(conceptsWhere(o.getClass(), mapToParams(map)),
204  
      a -> a.getClass() == o.getClass() && a != o && cget _reactor(a) != null);
205  
  }
206  
  
207  
  void onReactionProduct(BEAObject a) pcall {
208  
    if (a.typeIs("output"))
209  
      setField(output := getStringOpt text(a));
210  
  }
211  
  
212  
  void deleteReactors {
213  
    deleteConcepts(BEAObject,
214  
      o -> cget _reactor(o) != null || o.typeIs("reactor"));
215  
  }
216  
  
217  
  void updateReactables {
218  
    deleteConcepts(reactableConcepts);
219  
    L<BEAObject> objects = list(BEAObject);
220  
    for (BEAObject a : objects)
221  
      for (BEAObject b : filter(objects, b -> a.canReactWith(b)))
222  
        cnew(reactableConcepts, Reactable, +a, +b);
223  
  }
224  
  
225  
  void reactAll {
226  
    Cl<Reactable> l = list(reactableConcepts, Reactable);
227  
    if (empty(l)) ret;
228  
    
229  
    BEAReactor reactor = new("User clicks \"react all\"");
230  
231  
    for (Reactable r : l) pcall {
232  
      reactor.react(r.a, r.b);
233  
    }
234  
  }
235  
  
236  
  void reactUntilOutput {
237  
    while (!hasConcept(BOutput) && countConcepts(BEAObject) < maxObjects) {
238  
      rstUpdateReactables.waitUntilDone();
239  
      reactAll();
240  
    }
241  
  }
242  
  
243  
  void loadInput {
244  
    setField(output := "");
245  
    deleteReactors();
246  
    deleteConcepts(BInput);
247  
    uniq BInput(text := input);
248  
  }
249  
  
250  
  void loadAndProcess {
251  
    loadInput();
252  
    reactUntilOutput();
253  
  }
254  
}

Author comment

Began life as a copy of #1030723

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, vouqrxazstgt

No comments. add comment

Snippet ID: #1030762
Snippet name: AGI Pattern Spike 1 v2 [OK]
Eternal ID of this version: #1030762/47
Text MD5: 8827f54ecf1e11e30a854cb580023623
Transpilation MD5: ae6546167c8460cea2d8faa5d070d76c
Author: stefan
Category: javax
Type: JavaX source code (Dynamic Module)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-03-19 17:50:57
Source code size: 7058 bytes / 254 lines
Pitched / IR pitched: No / No
Views / Downloads: 260 / 613
Version history: 46 change(s)
Referenced in: [show references]