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

81
LINES

< > BotCompany Repo | #1022074 // GazelleContextCache_v2 - keeps loaded rules & updates them individually

JavaX fragment (include)

1  
sclass GazelleContextCache_v2 extends F0<GazelleEvalContext> {
2  
  transient Lock lock = lock();
3  
  transient GazelleEvalContext cachedCtx;
4  
  DynModule module;
5  
  bool debug = true;
6  
  F0<L<T3<S>>> grabRules = func -> L<T3<S>> {
7  
    //dm_gazelle_allRulesWithComment(gazelle_requiredComment())
8  
    gazelle_allRuleTriples()
9  
  };
10  
  S mechListPrefix;
11  
  bool alwaysFullRefresh; // safer
12  
  // TODO: add or remove if condition changes on rule
13  
14  
  *() {}  
15  
  *(DynModule *module) {}
16  
  
17  
  GazelleContextCache_v2 fill() {
18  
    loadRules(callF(grabRules));
19  
    this;
20  
  }
21  
  
22  
  void loadRules(L<T3S> rules) {
23  
    lock lock;
24  
    cachedCtx = dm_gazelle_stdEvalContext(rules);
25  
    cachedCtx.mechListPrefix = unnull(mechListPrefix);
26  
    gazelle_addHelpers(cachedCtx);
27  
  }
28  
  
29  
  GazelleEvalContext get() {
30  
    lock lock;
31  
    ret gazelle_cloneCtx(cachedCtx);
32  
  }
33  
  
34  
  void ruleChanged(S ruleID) {
35  
    if (ruleID == null) ret;
36  
    dm_q(module, r {
37  
      lock lock;
38  
      if (alwaysFullRefresh) ret with fill();
39  
      if (cachedCtx == null) ret;
40  
      print("GazelleContextCache_v2: processing rule update " + ruleID);
41  
      RuleEngine2 engine = cachedCtx.engine;
42  
      RuleEngine2.Rule rule = engine.getRule(ruleID);
43  
      if (rule == null) print("GazelleContextCache_v2: rule not found, assuming it's new");
44  
      else engine.rules.remove(rule);
45  
      PairS p = dm_textAndCommentForRule(ruleID);
46  
      if (p == null) ret with print("GazelleContextCache_v2: no text found for rule");
47  
      engine.addRules2(ll(t3(p.a, p.b, ruleID)));
48  
      rule = cachedCtx.engine.getRule(ruleID);
49  
      if (rule == null) ret with print("GazelleContextCache_v2: rule not properly added");
50  
      gazelle_addHelpers(nu(GazelleEvalContext, engine := gazelle_dummyEngineWithRule(rule)));
51  
      gazelle_updateStatsForRule(cachedCtx, ruleID);
52  
      print("GazelleContextCache_v2: rule update complete");
53  
    });
54  
  }
55  
56  
  // works only when module != null
57  
  AutoCloseable listenToMessages() {
58  
    ret combineAutoCloseables(
59  
      vmBus_onMessage('gazellePleaseClearRuleCaches, r {
60  
        dm_q(module, r fill)
61  
      }),
62  
      vmBus_onMessage('gazelleRuleFeedbackChanged, vf<S> ruleChanged),
63  
      vmBus_onMessage('gazelleRuleChanged, voidfunc(O ruleObj) {
64  
        ruleChanged(getString globalID(ruleObj))
65  
      }),
66  
      vmBus_onMessage('gazelleRuleDeleted, voidfunc(O rule) {
67  
        S ruleID = getString globalID(rule);
68  
        dm_q(module, r {
69  
          lock lock;
70  
          if (cachedCtx == null) ret;
71  
          cachedCtx.engine.deleteRule(ruleID);
72  
          print("GazelleContextCache_v2: rule " + ruleID + " deleted");
73  
        })
74  
      }));
75  
  }
76  
  
77  
  void alwaysFullRefresh() {
78  
    set alwaysFullRefresh;
79  
    fill();
80  
  }
81  
}

Author comment

Began life as a copy of #1021911

download  show line numbers  debug dex  old transpilations   

Travelled to 8 computer(s): bhatertpkbcr, cfunsshuasjs, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, whxojlpjdney

No comments. add comment

Snippet ID: #1022074
Snippet name: GazelleContextCache_v2 - keeps loaded rules & updates them individually
Eternal ID of this version: #1022074/27
Text MD5: c1d0206c9ffe3d59a4dc565426f24081
Author: stefan
Category: javax / a.i.
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-03-23 04:47:11
Source code size: 2753 bytes / 81 lines
Pitched / IR pitched: No / No
Views / Downloads: 351 / 856
Version history: 26 change(s)
Referenced in: [show references]