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

121
LINES

< > BotCompany Repo | #1001195 // Lisp/NL logic engine tester (developing)

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

Libraryless. Compilation Failed (1836L/12K).

!747

m {
  static S inputID = "#1001191";
  static S input;
  static S engineID = "#1001192";
  static Class engine; // logic engine
  static int score, total;
    
  !include #1001196 // Lisp engine
  
  p {
    if (input == null)
      input = loadSnippet(inputID); // Images & pixels
    else
      inputID = "direct";
    
    Lisp list = lisplist();
    for (S line : toLinesFullTrim(input)) {
      if (line.equals("**")) {
        testCase(list);
        list = lisplist();
      } else if (line.startsWith("!")) {
        line = line.substring(1).trim();
        Lisp l;
        if (line.indexOf('|') >= 0) {
          l = or();
          for (S x : line.split("\\|"))
            l.add(x.trim());
        } else
          l = new Lisp(line);
        list.add(answer(l));
      } else {
        if (line.startsWith("?"))
          line = line.substring(1).trim();
        list.add(enter(line));
      }
    }
    
    testCase(list);
    
    print("Score for " + engineID + " on " + inputID + ": " + score + " / " + total + ".");
  }
  
  static void testCase(Lisp list) {
    if (list.isEmpty()) return;
    
    if (engine == null) {
      engine = hotwire(engineID);
      callOpt(engine, "init");
    }
 
    for (int i = 0; i < list.size(); i++) {
      Lisp a = list.get(i);
      if (!a.is("answer")) continue;
      print("Expected: " + a);
      S answer = checkEngine(list.subList(0, i));
      boolean ok = isAnswerOK(a.get(0), answer);
      ++total;
      if (ok) ++score;
      print(ok ? "OK!" : "Not OK...");
    }
  }
  
  static boolean isAnswerOK(Lisp a, S answer) {
    if (answer == null) return false;
    if (a.is("or")) {
      for (Lisp s : a)
        if (sentenceEq(s.head, answer))
          return true;
      return false;
    } else
      return sentenceEq(a.head, answer);
  }
  
  static S checkEngine(Lisp list) {
    O exportedList = export(list, engine);
    //print("exportedList: " + structure(exportedList));
    S answer = (S) call(engine, "chimeIn", exportedList);
    print("Engine said: " + answer);
    return answer;
  }
  
  static O export(O o, Class dest) ctex {
    if (o == null || o instanceof String || o instanceof Number) return o;
    
    if (o instanceof List) {
      List l = cast o;
      List destO = new ArrayList(l.size());
      for (int i = 0; i < l.size(); i++)
        destO.add(export(l.get(i), dest));
      return destO;
    }
    
    if (o instanceof Lisp) {
      Class destClass = dest.getClassLoader().loadClass(o.getClass().getName());
      //print(o.getClass() + " => " + destClass);
      
      if (o.getClass() == destClass)
        return o; // no export necessary
        
      // actually make a new object, copy fields
      
      O destO = nuObject(destClass);
      
      // TODO: go to superclasses too
      Field[] fields = o.getClass().getDeclaredFields();
      for (Field field : fields) {
        if ((field.getModifiers() & Modifier.STATIC) != 0)
          continue;
        field.setAccessible(true);
        Object value = field.get(o);
        setOpt(destO, field.getName(), export(value, dest));
      }
      
      return destO;
    }
    
    throw fail(o.getClass());
  }
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1001195
Snippet name: Lisp/NL logic engine tester (developing)
Eternal ID of this version: #1001195/1
Text MD5: 38093e81c814ac9390a22dd75079425b
Transpilation MD5: 5c427601ea2aa87fb4b40563b512c273
Author: stefan
Category: logic code
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-04-17 22:14:15
Source code size: 3308 bytes / 121 lines
Pitched / IR pitched: No / No
Views / Downloads: 659 / 712
Referenced in: #3000382 - Answer for ferdie (>> t = 1, f = 0)
#3000383 - Answer for funkoverflow (>> t=1, f=0 okay)