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)

1  
sbool dm_gazelle_matchStep_statement(GazelleTree t, O... _) {
2  
  if (t.rule() == null) false;
3  
    
4  
  bool debug = boolPar debug(_) || boolPar matchStatementsDebug(_);
5  
  
6  
  RuleEngine2_MatchedRule mr = t.mr;
7  
  LS in = t.rule().in;
8  
  int iCond = t.mr.iCond;
9  
  if (debug) print("matchStatements: iCond=" + iCond + " in " + in);
10  
  if (iCond >= l(in)) false;
11  
  
12  
  LS conditions = dropFirst(iCond, in);
13  
  if (debug) print("Remaining conditions for " + t.ruleID() + " with " + t.mr.map + ": " + conditions);
14  
  new Matches m;
15  
  if (!matchAny("in " + (iCond+1) + " = statement *", t.rule().comments, m))
16  
    false;
17  
  S subRuleID = $1;
18  
  if (debug) print("Found subrule: " + subRuleID);
19  
  
20  
  // load all known fulfillments
21  
  
22  
  L fulfillments = dm_gazelle_fulfillmentsForRule(subRuleID);
23  
  
24  
  // find best fulfillment
25  
  
26  
  new Lowest<SS> bestMap;
27  
  if (debug) print("Have " + n2(fulfillments, "fulfillment") + ".");
28  
  for (O f : fulfillments) {
29  
    SS varMap = asCIMap(safeUnstructMap(getString varMap(f)));
30  
    if (debug) print("fulfillment => " + sfu(varMap));
31  
    
32  
    // self-vars hack to fix John Doe problem
33  
    bool selfVarsFix = boolPar selfVarsFix(_, true);
34  
    if (selfVarsFix) {
35  
      S cond = first(conditions);
36  
      for (S x : identifiersOnly(codeTokens(mr.tokenize(cond))))
37  
        if (!varMap.containsKey(x)) {
38  
          if (debug) print("Self-linking " + x);
39  
          varMap.put(x, x);
40  
        }
41  
    }
42  
      
43  
    if (debug) print("Original map: " + t.mr.map);
44  
    SS newMap = mergeMapsStrictlyIC(asCIMap(t.mr.map), varMap);
45  
    if (newMap == null) {
46  
      if (debug) printIndent("Can't merge maps");
47  
      continue;
48  
    }
49  
    if (debug) printIndent("Maps merged! => " + sfu(newMap));
50  
    newMap = ciMapWithoutKeysEqualToValues(newMap);
51  
    if (debug) printIndent("Map simplified => " + sfu(newMap));
52  
    
53  
    continue if gazelle_isRecentlyUsedMapping(t, newMap, _);
54  
    
55  
    bestMap.put(newMap, l(newMap));
56  
  }
57  
  
58  
  SS newMap = bestMap!;
59  
  if (newMap == null) {
60  
    if (debug) print("No matching statement found");
61  
    false;
62  
  }
63  
  
64  
  t.mr.map = newMap;
65  
  t.mr.iCond++;
66  
  t.mr.remainingConditions = dropFirst(t.mr.remainingConditions); // XX: also should rewrite with new vars
67  
  t.mr.remainingConditionsWithType = dropFirst(t.mr.remainingConditionsWithType);
68  
  t.mr.out = null;
69  
  t.line = t.mr.outText();
70  
  if (debug) print("Rewrote " + t.mr.rule.out + " to: " + t.line);
71  
  true;
72  
}

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: 310 / 330
Version history: 2 change(s)
Referenced in: [show references]