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

38
LINES

< > BotCompany Repo | #1027754 // LogicRuleApplier

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (2872L/19K).

// applies a logic rule to a single fact
sclass LogicRuleApplier {
  // essential
  BasicLogicRule rule;
  O fact;
  
  // where does your output go
  swappable void addRewrittenRHS(O rhs, O trail) {
    print("Derived: " + rhs);
  }
  
  // optional
  new DeepZipTools tools;
  bool verbose, printNonMatches;

  run {
    // now we match the fact to our condition
    Map map = tools.deepZip(rule.lhs, fact);

    // Discard unless all keys are variables
    if (map != null && !all(keys(map), s -> tools.isVar(s))) map = null;
    
    if (map == null)
      ret with if (printNonMatches) print("Non-match: " + quote(rule.lhs) + " / " + quote(fact));

    // Now we have a proper mapping with the keys being variables!
    if (verbose) print("Match: " + quote(rule.lhs) + " / " + quote(fact));

    // Apply mapping to right hand side
    O rhs_replaced = tools.replaceVars(rule.rhs, map);
    if (verbose) print(+rhs_replaced);
    
    Map trail = litorderedmap(op := "applyLogicRuleToFact",
      +rule, +fact, +rhs_replaced);

    addRewrittenRHS(rhs_replaced, trail);
  }
}

download  show line numbers  debug dex  old transpilations   

Travelled to 7 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1027754
Snippet name: LogicRuleApplier
Eternal ID of this version: #1027754/9
Text MD5: a4c13bcd471262d2517590cd8570d889
Transpilation MD5: 046c9f9b6fde481e721d97aa371544ad
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-04-16 14:18:07
Source code size: 1117 bytes / 38 lines
Pitched / IR pitched: No / No
Views / Downloads: 187 / 526
Version history: 8 change(s)
Referenced in: [show references]