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

323
LINES

< > BotCompany Repo | #1008086 // Compare Bot 2 [almost works]

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

Download Jar. Uses 3874K of libraries. Click here for Pure Java version (10968L/78K).

!7

sbool killDB = false;
static int defaultMax = 1000; // steps to run

sS facts = [[
  We want to find out what is more correct than what.
  
  "not correct" is a candidate.
  "maybe quite correct" is a candidate.
  "really really correct" is a candidate.
  "buggy" is a candidate.
  "total garbage" is a candidate.
  "incorrect" is a candidate.
  "almost correct" is a candidate.
  "absolutely correct" is a candidate.
  "very correct" is a candidate.
  
  [nfhwfngqkgoqtngs] (no question) and (X is a candidate.) and (Y is a candidate.) and (X <> Y) and (There is no answer to (Is X # more correct than Y?).) => (Ask (Is X # more correct than Y?))
  // Note: The # is just a filler for parsing, we'll get rid of that in time. Also, you will be able to write stuff without brackets of course.
  
  Always apply rule nfhwfngqkgoqtngs.
  
  // Interpret answers
  [iprphzfmwithvdfb] (The answer to (Is X # Y than Z?) is yes.) => (X is Y than Z.)
  Always apply rule iprphzfmwithvdfb.
  [udsmtmbiedmxtxci] (The answer to (Is X # Y than Z?) is no.) => (Z is Y than X.)
  Always apply rule udsmtmbiedmxtxci.
  
  // Transitivity!
  [pglcuchrtxdmlfvh] (X is AA than Y.) and (Y is AA than Z.) => (X is AA than Z.)
  Always apply rule pglcuchrtxdmlfvh.
  
  [ijziyiplzanfwvqx] (Ask (X)) and (The answer to X is Y.) => (Remove (Ask (X)))
  Always apply rule ijziyiplzanfwvqx.
  
  no question :- There is no statement with operator fxxathfdlfzgmaov.
  // Just a pretty-printing statement
  omrvwjslbkffaxoj hszwyiyetuuhcfwz acwmbrxqrlaplyqu
]];

sS concepts = [[
  ufzlismnlhvbjqoj - (X) and (Y) and (Z) and (AA) and (AB) => (AC)
  phkdhqkhzytkwdpb - (X) and (Y) and (Z) and (AA) => (AB)
  txsysipmstxmjhmi - (X) and (Y) => (Z)
  wziyypnyayfphdxs - (X) => (Y)
  xikyminwmeahxiws - There is no answer to (X).
  tlwrjpzuteopednt - The answer to X is Y.
  cdzttdbxadjreorm - We want to find out what is X than what.
  ogqdgzepqbunuuht - X is a candidate.
  hszwyiyetuuhcfwz - Is X # Y than Z?
  acwmbrxqrlaplyqu - Is "X" Y than "Z"?
  qgxjwufpfzkqvmxb - X is Y than Z.
  ylvaoplzguvhegka - X is not Y than Z.
  zxucpbfzmexohoiv - It is not true that (X).
  buahjsodljsaxvaq - X <> Y
  fxxathfdlfzgmaov - Ask (X)
  chjllkocfhtwcgoj - Remove (X)
  mzdvauejerzefagk - I don't know if (X)
  omrvwjslbkffaxoj - Format operator X as template Y.
  houyieutcmfdvgmz - Execute (X)
  hzmtabxcmkrtffgh - Apply X
  zvknqzxlrmuobnzx - Always apply rule X.
  plprefzezbluijrw - no question
  jexyjrtbtilmsmfw - setQuestion(X)
  cuutddpitkuswthu - While (X) => (Y)
  sicrogpdrtkiptun - X :- Y
  zhoulgsatpswstfa - There is no statement with operator X.
  ltyqczktgtjxrtlf - more correct
  zveeggveflecjwvr - total garbage
  wolealpbqkkqkvqq - not correct
  tomkbzlqlpeiekvu - maybe quite correct
  dnxyynujqydgcxcd - really really correct
  trajdgvqepyhsumb - buggy
  lwikikhkydtewtjc - incorrect
  muxjpepnjiyjghwj - almost correct
  wtnpjovqqiswlyay - absolutely correct
  bmliokweerblfxco - very correct
]];

concept MyTruth {
  S globalID;
  Lisp term;
  S madeByRule;
  
  *() {}
  *(S *globalID, Lisp *term, S *madeByRule) { change(); }
}

static Map<S, F1<Lisp, Bool>> evaluators = new HashMap;
static Lisp currentQuestion;
  
p-tt {
  centerHigherConsole();
  substance();
  
  // customize parsing and printing
  set englishToConceptLanguage_unquote;
  set englishToConceptLanguage_simple_noXYZ;
  set formatXYZ_quotedVars;
  
  // load predefined & learned statements
  useConceptsAndStatements(concepts, facts);
  if (killDB) deleteDB();
  db();
  for (MyTruth t)
    lispAddLocalTruth(t.globalID, t.term);
  printLispStatements();
  
  // create evaluators
  
  // X <> Y
  evaluators.put("buahjsodljsaxvaq", new F1<Lisp,Bool>() {
    Bool get(Lisp l) {
      ret l.size() == 2 && neq(l.get(0), l.get(1));
    }
  });
  
  // I don't know if (X)
  evaluators.put("mzdvauejerzefagk", new F1<Lisp,Bool>() {
    Bool get(Lisp l) {
      ret l.size() == 1 && notKnownIf(l.get(0));
    }
  });
  
  // There is no statement with operator X.
  evaluators.put("zhoulgsatpswstfa", new F1<Lisp,Bool>() {
    Bool get(Lisp l) {
      ret l.size() == 1 && !hasTruthWithHead(l.raw(0));
    }
  });
  
  // There is no answer to (X).
  evaluators.put("xikyminwmeahxiws", new F1<Lisp,Bool>() {
    Bool get(Lisp l) {
      bool ok = l.size() == 1 && null == lispForward("tlwrjpzuteopednt", l.get(0));
      print("Checking for answer to " + l.get(0) + " => " + ok);
      ret ok;
    }
  });
  
  // :- rules
  for (final Lisp rule : lispTruth2("sicrogpdrtkiptun"))
    evaluators.put(rule.raw(0), new F1<Lisp,Bool>() {
    Bool get(Lisp l) {
      ret l.isLeaf() && matchCondition(rule.get(1));
    }
  });
  
  bot();
  
  //applyRule("nfhwfngqkgoqtngs");
  //applyAlwaysRules();
  //runInstruction("yrgkrfrcwubpevzu");
}

answer {
  if "lisp" { printLispStatements(); ret "ok"; }
  if "print" { printPrettyFacts(); ret "ok"; }
  if "rule *" { applyRule($1); ret "ok"; }
  if "step" { applyAlwaysRules_step(); ret "ok"; }
  if "run" { applyAlwaysRules(defaultMax); ret "ok"; }
  if "force" { force(defaultMax); ret "ok"; }
}

svoid showQuestion() {
  L<Lisp> l = lispTruth1("fxxathfdlfzgmaov");
  if (l(l) > 1) warn("Multiple open questions");
  if (nempty(l)) {
    final Lisp q = first(l).get(0);
    if (eq(currentQuestion, q)) ret;
    currentQuestion = q;
    print("Question: " + q);
    swing {
      showControls2(
        centerAndSouth(
          withBottomMargin(jcenteredBoldLabel(lispToEnglish_prettier(q))),
          jcenteredline(answerButton("Yes"), answerButton("No"), answerButton("I don't know")));
    }
  }
}

svoid runInstruction(S instructionID) {
  Lisp rule = getLispTruth(instructionID);
  if (rule == null) { print("Instruction not found: " + instructionID); ret; }
  runInstruction(rule);
}
  
svoid runInstruction(Lisp rule) {
  print("runInstruction: " + rule);
  if (rule.is("hzmtabxcmkrtffgh", 1)) // Apply X
    applyRule(rule.raw(0));
  else if (rule.is("cuutddpitkuswthu", 2)) { // While (X) => (Y)
    while (ping() && matchCondition(rule.get(0)))
      runInstruction(rule.get(1));
  } /*else if (rule.is("houyieutcmfdvgmz", 1))
    execute(rule.get(0));
  /*else if (rule.is("jexyjrtbtilmsmfw", 1)) // setQuestion(X)
    setQuestion(rule.get(0));*/
  else print("Unknown instruction type: " + rule);
}

svoid applyRule(S ruleID) {
  // process the rule
  Lisp rule = getLispTruth(ruleID);
  if (rule == null) { print("Rule not found: " + ruleID); ret; }
  //print("Applying rule " + ruleID);
  print_setPrefixForThread(ruleID + "> ");
  try {
    L<Lisp> conditions = dropLast(rule.args);
    Lisp out = last(rule.args);
    //for (Lisp cond : conditions) print("Condition: " + cond);
      
    new Map<S, Lisp> matches;
    int n = matchConditions_random(conditions, matches);
    if (n == -1) {
      print("Yo! " + struct(matches));
      out = lispReplaceVars(out, matches);
      if (lispTrue(out))
        print("Already had: " + out);
      else {
        print("Defining: " + lispToEnglish_prettier(out));
        emit(out, ruleID);
      }
    } else
      print("Only " + n + " of " + n(conditions, "condition") + " met");
  } finally {
    print_setPrefixForThread("");
  }
}

// if match: returns -1
// if no match: returns how many conditions were met
static int matchConditions_random(L<Lisp> conditions, Map<S, Lisp> m) {
  if (empty(conditions)) ret -1;
  Lisp condition = first(conditions);
  Map<S, Lisp> m2 = cloneMap(m);
  if (!matchCondition_random(condition, m2)) {
    print("Condition failed: " + condition);
    ret 0;
  }
  print("Condition matched, " + struct(m2));
  int n = matchConditions_random(dropFirst(conditions), m2);
  if (n == -1) {
    m.putAll(m2);
    ret -1;
  }
  ret n+1; // failure
}

sbool matchCondition(Lisp condition) {
  print("matchCondition " + condition);
  O evaluator = evaluators.get(condition.head);
  if (evaluator != null)
    ret isTrue(callF(evaluator, condition));
  ret lispTrue(condition);
}

static bool matchCondition_random(Lisp condition, Map<S, Lisp> m) {
  L<Lisp> facts = lispTruth();
  
  O evaluator = evaluators.get(condition.head);
  if (evaluator != null)
    ret isTrue(callF(evaluator, lispReplaceVars(condition, m)));

  L<Map<S, Lisp>> candidates = new L;
  for (Lisp fact : facts) {
    Map<S, Lisp> m2 = cloneMap(m);
    if (lispMatchIC_xyzVars_sub(condition, fact, m2))
      candidates.add(m2);
  }
  //print("Have " + n(candidates, "candidate"));
  if (empty(candidates)) false;
  m.putAll(random(candidates));
  true;
}

svoid force(long steps) {
  for (long step = 0; step < steps; step++)
    applyAlwaysRules_step();
}

svoid applyAlwaysRules(long maxSteps) {
  long count = 0, step = 0;
  do {
    count = lispChangeCount();
    applyAlwaysRules_step();
  } while (++step < maxSteps && lispChangeCount() != count);
}

svoid applyAlwaysRules_step() {
  for (Lisp l : lispTruth1("zvknqzxlrmuobnzx"))
    applyRule(l.raw(0)); // TODO: not only random application
  for (Lisp l : lispTruth1("chjllkocfhtwcgoj")) { // Remove (X)
    unemit(l);
    unemit(l.get(0));
  }
  for (Lisp l : lispTruth1("hzmtabxcmkrtffgh")) { // Apply X
    unemit(l);
    applyRule(l.raw(0));
  }
  showQuestion();
}

svoid emit(Lisp l, S madeByRule) {
  LispStatement s = lispAddLocalTruth(l);
  if (s != null)
    new MyTruth(s.globalID, l, madeByRule);
}

svoid unemit(Lisp l) {
  LispStatement s = findLispStatement(l);
  if (s == null) ret;
  removeLispStatement(s);
  deleteConcepts(MyTruth, globalID := s.globalID);
}

static JButton answerButton(fS text) {
  ret jbutton(text, r {
    emit(lisp("tlwrjpzuteopednt", currentQuestion, englishToLisp(text)), "button");
    applyAlwaysRules(defaultMax);
  });
}

Author comment

Began life as a copy of #1008082

download  show line numbers  debug dex  old transpilations   

Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1008086
Snippet name: Compare Bot 2 [almost works]
Eternal ID of this version: #1008086/88
Text MD5: 76ae6ee36f93de77a5ec4cb8c8839384
Transpilation MD5: a10ff1683f4e3d82049c58a4a4da626b
Author: stefan
Category: javax / a.i.
Type: JavaX source code (desktop)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-04-25 01:24:15
Source code size: 10015 bytes / 323 lines
Pitched / IR pitched: No / No
Views / Downloads: 547 / 1208
Version history: 87 change(s)
Referenced in: [show references]