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

94
LINES

< > BotCompany Repo | #1021448 // RuleEngine2_MatchedRule

JavaX fragment (include)

sclass RuleEngine2_MatchedRule {
  S globalID = aGlobalID();
  RuleEngine2.Rule rule;
  int iCond;
  SS map;
  new Map<S, Int> mappingQuality; // key in map to -1, 0 or 1
  new LPair<LS> matchedLines;
  S tokenizationFunction;
  S outTokenizer; // tokenization function for output part of rule
  LS remainingConditions;
  L<TextAndType> remainingConditionsWithType;
  Int qualitySum;
  Int nPossibleVars;
  S out;
  SS inputsByType;
  GInterpretable interpretable; // what we matched on

  int qualitySum() {
    if (qualitySum == null)
      qualitySum = intSum(values(mappingQuality))-l(map);
    ret qualitySum;
  }
  
  int nPossibleVars() {
    if (nPossibleVars == null)
      nPossibleVars = l(possibleVars());
    ret nPossibleVars;
  }
  
  bool isSpecialRule() {
    ret rule instanceof RuleEngine2.SimplifyWithRule;
  }
  
  double relativeQuality() {
    ret isSpecialRule() ? 0 : doubleRatio(qualitySum(), nPossibleVars());
  }
  
  // counts all tokens
  Set<S> possibleVars() {
    ret asCISet(codeTokens(tokenize(first(rule.in))));
  }
  
  RuleEngine2.Rule applyMapping() {
    new RuleEngine2.Rule r;
    r.in = map(rule.in, f<S, S> applyMappingTo);
    r.out = applyMappingTo(rule.out);
    ret r;
  }
  
  S appliedRuleText() {
    ret applyMapping().asText();
  }
  
  S outText() {
    if (out == null)
      out = join(ai_mapCodeTokensWithMap(outTokenize(rule.out), map));
    ret out;
  }
  
  S applyMappingTo(GRuleLine line) {
    ret line == null ? null : join(ai_mapCodeTokensWithMap(tokenize(line), map));
  }
  
  S applyMappingTo(S s) {
    ret s == null ? null : join(ai_mapCodeTokensWithMap(tokenize(s), map));
  }
  
  LS tokenize(S s) {
    ret (LS) callAndMake(assertNotNull(tokenizationFunction), s);
  }
  
  LS outTokenize(S s) {
    ret (LS) callAndMake(assertNotNull(or(outTokenizer, tokenizationFunction)), s);
  }
  
  LS tokenize(GRuleLine line) {
    ret (LS) callAndMake(assertNotNull(or(line.tokenizer, tokenizationFunction)), line.text);
  }
  
  
  bool moreConditions() { ret rule != null && iCond < l(rule.in); }
  
  S ruleID() { ret rule.globalID; }
  
  void flush() {
    // don't flush results of simplifiers
    if (tokenizationFunction != null || outTokenizer != null)
      out = null;
  }
  
  toString {
    ret "MatchedRule " + ruleID() + " with " + map + (moreConditions() ? " [unsatisfied]" : "");
  }
}

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: 386 / 897
Version history: 25 change(s)
Referenced in: [show references]