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).

// NewEngine takes facts which mix Java objects and sentence parts ("fragments")
// e.g. ll(obj1, Fragment("leads to"), obj2)
// short syntax: formatFrag("* leads to *", obj1, obj2)

// rules contain the same kind of construct on lhs and rhs with
// dollar variables (e.g. "$x", not as a Fragment)

sclass NewEngine implements Steppable {
  replace LogicRule with BasicLogicRule.

  transient new Map facts; // facts to trail
  transient AllOnAllWithOperation<LogicRule, O> rulesAndFacts = new(lambda2 applyLogicRuleToFact);

  new DeepZipTools tools;
  
  bool verbose = true, printNonMatches;
  
  public bool step() { ret rulesAndFacts.step(); }

  void addLogicRule(LogicRule rule) {
    rule = curryLHS_BasicLogicRule(rule);
    if (verbose) print("New rule: " + rule);
    rulesAndFacts.newA(rule);
  }

  void addFact(O fact) { addFact(fact, "external"); }
  
  void addFact(O fact, O trail) {
    if (trail == null) trail = "unknown";
    if (facts.put(fact, trail) != null) ret;
    if (verbose) print("New fact: " + fact);
    rulesAndFacts.newB(fact);
  }
  
  void addFormattedFact(S starPattern, O... args) {
    addFact(formatWithFragments(starPattern, args));
  }
   
  L factsInOrderOfDefinition() { ret rulesAndFacts.cloneBList(); }  
  
  void run {
    stepAll(this);
    print("done. " + nFacts(rulesAndFacts.bList) + ", " + nRules(rulesAndFacts.aList));
  }
  
  void think { run(); }

  void applyLogicRuleToFact(LogicRule rule, O fact) {
    new LogicRuleApplier ap;
    ap.rule = rule;
    ap.fact = fact;
    ap.verbose = verbose;
    ap.printNonMatches = printNonMatches;
    ap.addRewrittenRHS = lambda2 addRewrittenRHS;
    ap.tools = tools;
    ap.run();
  }

  void addRewrittenRHS(O o, O trail) {
    if (o cast LogicRule) {
      o.trail = trail;
      addLogicRule(o);
    } else if o is And(O a, O b) {
      addRewrittenRHS(a, o);
      addRewrittenRHS(b, o);
    } else if (o != null)
      addFact(o, trail);
  }
}

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: 181 / 510
Version history: 6 change(s)
Referenced in: [show references]