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

69
LINES

< > BotCompany Repo | #1027762 // NewEngine v1

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

Libraryless. Click here for Pure Java version (4456L/28K).

1  
// NewEngine takes facts which mix Java objects and sentence parts ("fragments")
2  
// e.g. ll(obj1, Fragment("leads to"), obj2)
3  
// short syntax: formatFrag("* leads to *", obj1, obj2)
4  
5  
// rules contain the same kind of construct on lhs and rhs with
6  
// dollar variables (e.g. "$x", not as a Fragment)
7  
8  
sclass NewEngine implements Steppable {
9  
  replace LogicRule with BasicLogicRule.
10  
11  
  transient new Map facts; // facts to trail
12  
  transient AllOnAllWithOperation<LogicRule, O> rulesAndFacts = new(lambda2 applyLogicRuleToFact);
13  
14  
  new DeepZipTools tools;
15  
  
16  
  bool verbose = true, printNonMatches;
17  
  
18  
  public bool step() { ret rulesAndFacts.step(); }
19  
20  
  void addLogicRule(LogicRule rule) {
21  
    rule = curryLHS_BasicLogicRule(rule);
22  
    if (verbose) print("New rule: " + rule);
23  
    rulesAndFacts.newA(rule);
24  
  }
25  
26  
  void addFact(O fact) { addFact(fact, "external"); }
27  
  
28  
  void addFact(O fact, O trail) {
29  
    if (trail == null) trail = "unknown";
30  
    if (facts.put(fact, trail) != null) ret;
31  
    if (verbose) print("New fact: " + fact);
32  
    rulesAndFacts.newB(fact);
33  
  }
34  
  
35  
  void addFormattedFact(S starPattern, O... args) {
36  
    addFact(formatWithFragments(starPattern, args));
37  
  }
38  
   
39  
  L factsInOrderOfDefinition() { ret rulesAndFacts.cloneBList(); }  
40  
  
41  
  void run {
42  
    stepAll(this);
43  
    print("done. " + nFacts(rulesAndFacts.bList) + ", " + nRules(rulesAndFacts.aList));
44  
  }
45  
  
46  
  void think { run(); }
47  
48  
  void applyLogicRuleToFact(LogicRule rule, O fact) {
49  
    new LogicRuleApplier ap;
50  
    ap.rule = rule;
51  
    ap.fact = fact;
52  
    ap.verbose = verbose;
53  
    ap.printNonMatches = printNonMatches;
54  
    ap.addRewrittenRHS = lambda2 addRewrittenRHS;
55  
    ap.tools = tools;
56  
    ap.run();
57  
  }
58  
59  
  void addRewrittenRHS(O o, O trail) {
60  
    if (o cast LogicRule) {
61  
      o.trail = trail;
62  
      addLogicRule(o);
63  
    } else if o is And(O a, O b) {
64  
      addRewrittenRHS(a, o);
65  
      addRewrittenRHS(b, o);
66  
    } else if (o != null)
67  
      addFact(o, trail);
68  
  }
69  
}

Author comment

Began life as a copy of #1027758

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: #1027762
Snippet name: NewEngine v1
Eternal ID of this version: #1027762/7
Text MD5: b122829728d7c8968204239e7a4d8ac5
Transpilation MD5: fb971c0369c41735b6cc0aa11eb6c3b2
Author: stefan
Category: javax / a.i.
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-06-20 15:48:55
Source code size: 2019 bytes / 69 lines
Pitched / IR pitched: No / No
Views / Downloads: 183 / 514
Version history: 6 change(s)
Referenced in: [show references]