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

64
LINES

< > BotCompany Repo | #1021683 // Rule with helper table Spike [OK]

JavaX source code (desktop) [tags: use-pretranspiled] - run with: x30.jar

Download Jar. Libraryless. Click here for Pure Java version (16910L/97K).

!7

sS data = [[

  -- Rule
  
  What does a driver do? => He drives, dummie.
  
  -- Test
  
  What does a catcher do? => He catches, dummie.
  
  -- Helper Table
  
  driver, drive, drives, driving
  sleeper, sleep, sleeps, sleeping
  murderer, murder, murders, murdering
  catcher, catch, catches, catching

]];

p-exp {
  SS sections = minusSignSectionsCI(data);
  pnlStruct(sections);
  
  LPair<S> rules = ai_findDoubleArrowRulesAsPairs(sections.get("Rule"));
  GazelleEvalContext ctx = gazelle_stdEvalContext(map(rules, func(PairS inOut) -> T3<S> {
    t3(joinPairWithDoubleArrow(inOut), "", aGlobalID())
  }));
  pnlStruct(ctx.engine.rules);
  
  final LL<S> helperTable = map tok_splitAtComma(tlft(sections.get("Helper Table")));
  //pnlStruct(helperTable);
  final Map<S, Pair<Int>> helperIndex = indexTwoDArrayIC(helperTable);
  
  final RuleEngine2.Rule rule = first(ctx.engine.rules);
  rule.massageMap = func(SS map, LS tokC, LS tokI, RuleEngine2_MatchedRule matched) -> SS {
    print("Massaging: " + map + " " + sfu(tokC) + " / " + sfu(tokI));
    for (S t : codeTokens(matched.tokenize(rule.out))) {
      //print("Checking: " + t);
      if (map.containsKey(t)) continue;
      Pair<Int> idx = helperIndex.get(t);
      if (idx == null) continue;
      //print("Found: " + t);
      for (S key, val : cloneMap(map)) {
        int idx2 = indexOfIC(helperTable.get(idx.a), key);
        if (idx2 < 0) continue;
        Pair<Int> idxVal = helperIndex.get(val);
        print("Found: " + t + " <-> " + key);
        mapPut(map, t, _get(_get(helperTable, idxVal.a), idx.b));
      }
    }
    ret map;
  };
  
  new LS out;
  for (PairS inOut : ai_findDoubleArrowRulesAsPairs(sections.get("Test"))) {
    nlPrintNL(inOut.a);
    out.addAll(collect line(pnl(gazelle_getChildren(GazelleTree(ctx, inOut.a)))));
  }
  
  assertContainsIC(out, "He catches, dummie.");
  print("OK!");
}

download  show line numbers  debug dex  old transpilations   

Travelled to 8 computer(s): bhatertpkbcr, cfunsshuasjs, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, whxojlpjdney

No comments. add comment

Snippet ID: #1021683
Snippet name: Rule with helper table Spike [OK]
Eternal ID of this version: #1021683/24
Text MD5: 8230d100a71f3c83d8eb6b352b488ef2
Transpilation MD5: 0f7901d6e20817f58d95b1b0b1ce8679
Author: stefan
Category: javax / a.i.
Type: JavaX source code (desktop)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-02-24 14:09:28
Source code size: 1958 bytes / 64 lines
Pitched / IR pitched: No / No
Views / Downloads: 233 / 872
Version history: 23 change(s)
Referenced in: [show references]