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

72
LINES

< > BotCompany Repo | #1022399 // dm_gazelle_matchStep_statement - take smallest mapping

JavaX fragment (include)

sbool dm_gazelle_matchStep_statement(GazelleTree t, O... _) {
  if (t.rule() == null) false;
    
  bool debug = boolPar debug(_) || boolPar matchStatementsDebug(_);
  
  RuleEngine2_MatchedRule mr = t.mr;
  LS in = t.rule().in;
  int iCond = t.mr.iCond;
  if (debug) print("matchStatements: iCond=" + iCond + " in " + in);
  if (iCond >= l(in)) false;
  
  LS conditions = dropFirst(iCond, in);
  if (debug) print("Remaining conditions for " + t.ruleID() + " with " + t.mr.map + ": " + conditions);
  new Matches m;
  if (!matchAny("in " + (iCond+1) + " = statement *", t.rule().comments, m))
    false;
  S subRuleID = $1;
  if (debug) print("Found subrule: " + subRuleID);
  
  // load all known fulfillments
  
  L fulfillments = dm_gazelle_fulfillmentsForRule(subRuleID);
  
  // find best fulfillment
  
  new Lowest<SS> bestMap;
  if (debug) print("Have " + n2(fulfillments, "fulfillment") + ".");
  for (O f : fulfillments) {
    SS varMap = asCIMap(safeUnstructMap(getString varMap(f)));
    if (debug) print("fulfillment => " + sfu(varMap));
    
    // self-vars hack to fix John Doe problem
    bool selfVarsFix = boolPar selfVarsFix(_, true);
    if (selfVarsFix) {
      S cond = first(conditions);
      for (S x : identifiersOnly(codeTokens(mr.tokenize(cond))))
        if (!varMap.containsKey(x)) {
          if (debug) print("Self-linking " + x);
          varMap.put(x, x);
        }
    }
      
    if (debug) print("Original map: " + t.mr.map);
    SS newMap = mergeMapsStrictlyIC(asCIMap(t.mr.map), varMap);
    if (newMap == null) {
      if (debug) printIndent("Can't merge maps");
      continue;
    }
    if (debug) printIndent("Maps merged! => " + sfu(newMap));
    newMap = ciMapWithoutKeysEqualToValues(newMap);
    if (debug) printIndent("Map simplified => " + sfu(newMap));
    
    continue if gazelle_isRecentlyUsedMapping(t, newMap, _);
    
    bestMap.put(newMap, l(newMap));
  }
  
  SS newMap = bestMap!;
  if (newMap == null) {
    if (debug) print("No matching statement found");
    false;
  }
  
  t.mr.map = newMap;
  t.mr.iCond++;
  t.mr.remainingConditions = dropFirst(t.mr.remainingConditions); // XX: also should rewrite with new vars
  t.mr.remainingConditionsWithType = dropFirst(t.mr.remainingConditionsWithType);
  t.mr.out = null;
  t.line = t.mr.outText();
  if (debug) print("Rewrote " + t.mr.rule.out + " to: " + t.line);
  true;
}

Author comment

Began life as a copy of #1021998

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: #1022399
Snippet name: dm_gazelle_matchStep_statement - take smallest mapping
Eternal ID of this version: #1022399/3
Text MD5: 85b70f8524d9aa365922f932f25bd1a6
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-16 20:01:31
Source code size: 2459 bytes / 72 lines
Pitched / IR pitched: No / No
Views / Downloads: 308 / 327
Version history: 2 change(s)
Referenced in: [show references]