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

94
LINES

< > BotCompany Repo | #1021448 // RuleEngine2_MatchedRule

JavaX fragment (include)

1  
sclass RuleEngine2_MatchedRule {
2  
  S globalID = aGlobalID();
3  
  RuleEngine2.Rule rule;
4  
  int iCond;
5  
  SS map;
6  
  new Map<S, Int> mappingQuality; // key in map to -1, 0 or 1
7  
  new LPair<LS> matchedLines;
8  
  S tokenizationFunction;
9  
  S outTokenizer; // tokenization function for output part of rule
10  
  LS remainingConditions;
11  
  L<TextAndType> remainingConditionsWithType;
12  
  Int qualitySum;
13  
  Int nPossibleVars;
14  
  S out;
15  
  SS inputsByType;
16  
  GInterpretable interpretable; // what we matched on
17  
18  
  int qualitySum() {
19  
    if (qualitySum == null)
20  
      qualitySum = intSum(values(mappingQuality))-l(map);
21  
    ret qualitySum;
22  
  }
23  
  
24  
  int nPossibleVars() {
25  
    if (nPossibleVars == null)
26  
      nPossibleVars = l(possibleVars());
27  
    ret nPossibleVars;
28  
  }
29  
  
30  
  bool isSpecialRule() {
31  
    ret rule instanceof RuleEngine2.SimplifyWithRule;
32  
  }
33  
  
34  
  double relativeQuality() {
35  
    ret isSpecialRule() ? 0 : doubleRatio(qualitySum(), nPossibleVars());
36  
  }
37  
  
38  
  // counts all tokens
39  
  Set<S> possibleVars() {
40  
    ret asCISet(codeTokens(tokenize(first(rule.in))));
41  
  }
42  
  
43  
  RuleEngine2.Rule applyMapping() {
44  
    new RuleEngine2.Rule r;
45  
    r.in = map(rule.in, f<S, S> applyMappingTo);
46  
    r.out = applyMappingTo(rule.out);
47  
    ret r;
48  
  }
49  
  
50  
  S appliedRuleText() {
51  
    ret applyMapping().asText();
52  
  }
53  
  
54  
  S outText() {
55  
    if (out == null)
56  
      out = join(ai_mapCodeTokensWithMap(outTokenize(rule.out), map));
57  
    ret out;
58  
  }
59  
  
60  
  S applyMappingTo(GRuleLine line) {
61  
    ret line == null ? null : join(ai_mapCodeTokensWithMap(tokenize(line), map));
62  
  }
63  
  
64  
  S applyMappingTo(S s) {
65  
    ret s == null ? null : join(ai_mapCodeTokensWithMap(tokenize(s), map));
66  
  }
67  
  
68  
  LS tokenize(S s) {
69  
    ret (LS) callAndMake(assertNotNull(tokenizationFunction), s);
70  
  }
71  
  
72  
  LS outTokenize(S s) {
73  
    ret (LS) callAndMake(assertNotNull(or(outTokenizer, tokenizationFunction)), s);
74  
  }
75  
  
76  
  LS tokenize(GRuleLine line) {
77  
    ret (LS) callAndMake(assertNotNull(or(line.tokenizer, tokenizationFunction)), line.text);
78  
  }
79  
  
80  
  
81  
  bool moreConditions() { ret rule != null && iCond < l(rule.in); }
82  
  
83  
  S ruleID() { ret rule.globalID; }
84  
  
85  
  void flush() {
86  
    // don't flush results of simplifiers
87  
    if (tokenizationFunction != null || outTokenizer != null)
88  
      out = null;
89  
  }
90  
  
91  
  toString {
92  
    ret "MatchedRule " + ruleID() + " with " + map + (moreConditions() ? " [unsatisfied]" : "");
93  
  }
94  
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1021448
Snippet name: RuleEngine2_MatchedRule
Eternal ID of this version: #1021448/26
Text MD5: 9319e53199225570fdf2ee996fd2eb9b
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-18 11:44:53
Source code size: 2462 bytes / 94 lines
Pitched / IR pitched: No / No
Views / Downloads: 466 / 975
Version history: 25 change(s)
Referenced in: [show references]