sclass RuleEngine2_MatchedRule { S globalID = aGlobalID(); RuleEngine2.Rule rule; int iCond; SS map; new Map mappingQuality; // key in map to -1, 0 or 1 new LPair matchedLines; S tokenizationFunction; S outTokenizer; // tokenization function for output part of rule LS remainingConditions; L 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 possibleVars() { ret asCISet(codeTokens(tokenize(first(rule.in)))); } RuleEngine2.Rule applyMapping() { new RuleEngine2.Rule r; r.in = map(rule.in, f 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]" : ""); } }