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

100
LINES

< > BotCompany Repo | #1018362 // NewWoody

JavaX fragment (include)

1  
sclass NewWoody {
2  
  transient Map<S, BBRule> rulesByName;
3  
  transient new L<BBRule> badRules;
4  
5  
  Map<S, RuleData> ruleData = ciMap();
6  
  S lastOutput;
7  
  BBRule lastRule, activeRule, gotoRule;
8  
  S prefix = "BB | ";
9  
  
10  
  sclass RuleData {
11  
    int answerIndex;
12  
  }
13  
  
14  
  RuleData ruleData(BBRule rule) {
15  
    ret getOrCreate(ruleData, rule.listName, RuleData.class);
16  
  }
17  
18  
  *() {
19  
    if (!DynamicObject_isLoading())
20  
      rulesByName = indexFieldAsCIMap('listName, bb_allRules());
21  
  }
22  
  
23  
  *(NewWoody w) { initFrom(w); }
24  
  
25  
  void initFrom(NewWoody w) {
26  
    copyFields(w, this, 'rulesByName, 'badRules);
27  
  }
28  
  
29  
  void processInput(S input) {
30  
    lastRule = null;
31  
    lastOutput = null;
32  
33  
    temp tempSetTL(ctxContext_var(), nu(CtxContext, +input));
34  
    new L<BBRule> matching;
35  
    if (nempty(input))
36  
      for (BBRule r : valuesList(rulesByName))
37  
        if (ruleExpressionMatches(r))
38  
          matching.add(r);
39  
40  
    setActiveRule(or(first(matching), activeRule));
41  
    repeat 10 {
42  
      gotoRule = null;
43  
      doRule();
44  
      if (gotoRule == null) break;
45  
      setActiveRule(gotoRule);
46  
    }
47  
  }
48  
  
49  
  void doRule {
50  
    if (activeRule != null)
51  
      if (cic(activeRule.answerMode, 'random)) // TODO: mark seen
52  
        setOutput(random(activeRule.answers));
53  
      else if (cic(activeRule.answerMode, 'switch)) {
54  
        for (S s : activeRule.answers) {
55  
          LS l = splitAtDoubleArrow(s);
56  
          if (l(l) == 2 && print(first(l), ctxEvalCondition(first(l)))) {
57  
            setOutput(second(l));
58  
            break;
59  
          }
60  
        }
61  
      } else {
62  
        // loop
63  
        RuleData rd = ruleData(activeRule);
64  
        setOutput(get(activeRule.answers,
65  
          rd.answerIndex %= max(1, l(activeRule.answers))));
66  
        ++rd.answerIndex;
67  
      }
68  
  }
69  
70  
  bool ruleExpressionMatches(BBRule r) {  
71  
    if (empty(r.expression)) false;
72  
    try {
73  
      ret ctxEvalCondition(r.expression);
74  
    } catch print e {
75  
      badRules.add(r);
76  
      rulesByName.remove(r.listName);
77  
      false;
78  
    }
79  
  }
80  
  
81  
  L<BBRule> listsMatchingInput(S input) {
82  
    temp tempSetTL(ctxContext_var(), nu(CtxContext, +input));
83  
    ret [BBRule r : valuesList(rulesByName) | ruleExpressionMatches(r)];
84  
  }
85  
  
86  
  void setOutput(S s) {
87  
    new Matches m;
88  
    if (swic_trim(trim(lastLine(s)), "goto:", m)) {
89  
      S ruleName = prefix + m.rest();
90  
      gotoRule = rulesByName.get(ruleName);
91  
      print("Goto: " + ruleName + " (" + yesNo(gotoRule != null) + ")");
92  
      s = dropLastLine(s);
93  
    }
94  
    lastOutput = appendSpaceIfNempty(lastOutput) + s;
95  
  }
96  
  
97  
  void setActiveRule(BBRule rule) {
98  
    activeRule = lastRule = or(rule, activeRule);
99  
  }
100  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1018362
Snippet name: NewWoody
Eternal ID of this version: #1018362/35
Text MD5: a3515a5e35f74d3ab0bbf4b03ed9be0b
Author: stefan
Category: javax / a.i.
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-12-24 16:22:16
Source code size: 2709 bytes / 100 lines
Pitched / IR pitched: No / No
Views / Downloads: 385 / 1004
Version history: 34 change(s)
Referenced in: [show references]