Libraryless. Click here for Pure Java version (2872L/19K).
1 | // applies a logic rule to a single fact |
2 | sclass LogicRuleApplier { |
3 | // essential |
4 | BasicLogicRule rule; |
5 | O fact; |
6 | |
7 | // where does your output go |
8 | swappable void addRewrittenRHS(O rhs, O trail) { |
9 | print("Derived: " + rhs); |
10 | } |
11 | |
12 | // optional |
13 | new DeepZipTools tools; |
14 | bool verbose, printNonMatches; |
15 | |
16 | run { |
17 | // now we match the fact to our condition |
18 | Map map = tools.deepZip(rule.lhs, fact); |
19 | |
20 | // Discard unless all keys are variables |
21 | if (map != null && !all(keys(map), s -> tools.isVar(s))) map = null; |
22 | |
23 | if (map == null) |
24 | ret with if (printNonMatches) print("Non-match: " + quote(rule.lhs) + " / " + quote(fact)); |
25 | |
26 | // Now we have a proper mapping with the keys being variables! |
27 | if (verbose) print("Match: " + quote(rule.lhs) + " / " + quote(fact)); |
28 | |
29 | // Apply mapping to right hand side |
30 | O rhs_replaced = tools.replaceVars(rule.rhs, map); |
31 | if (verbose) print(+rhs_replaced); |
32 | |
33 | Map trail = litorderedmap(op := "applyLogicRuleToFact", |
34 | +rule, +fact, +rhs_replaced); |
35 | |
36 | addRewrittenRHS(rhs_replaced, trail); |
37 | } |
38 | } |
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: | 255 / 620 |
Version history: | 8 change(s) |
Referenced in: | [show references] |