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

72
LINES

< > BotCompany Repo | #1021998 // dm_gazelle_matchStatements_v2 - take smallest mapping (old)

JavaX fragment (include)

1  
svoid dm_gazelle_matchStatements_v2(L<GazelleTree> l, O... _) {
2  
  bool debug = boolPar debug(_) || boolPar matchStatementsDebug(_);
3  
  for (GazelleTree t : l) {
4  
    if (t.rule() == null) continue;
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)) continue;
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  
      continue;
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  
      continue;
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  
  }
72  
}

Author comment

Began life as a copy of #1021772

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: #1021998
Snippet name: dm_gazelle_matchStatements_v2 - take smallest mapping (old)
Eternal ID of this version: #1021998/12
Text MD5: 7d974ce210bf2edb698f7918f1f07506
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:02:30
Source code size: 2626 bytes / 72 lines
Pitched / IR pitched: No / No
Views / Downloads: 199 / 242
Version history: 11 change(s)
Referenced in: [show references]