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

131
LINES

< > BotCompany Repo | #1005772 // Try stuff on stuff 2 [works]

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

Libraryless. Click here for Pure Java version (4237L/27K/92K).

!752

static int maxObjects = 1000;

concepts.

concept Obj {
  O o;
  
  *() {}
  *(O *o) { change(); }
}

concept Function {
  S name;
  bool nonDet;
  
  *() {}
  *(S *name) { change(); }
}

concept Result {
  new Ref<Function> f;
  new Ref<Obj> argument;
  new Ref<Obj> result;
  PersistableThrowable error;
  long when, time;
}

Result > Application {}

Result > PreviousResult {}

static L<File> f_listFiles(File dir) {
  ret asList(listFiles(dir));
}

static long f_fileSize(File f) {
  ret f.length();
}

static L<S> safeFunctionsToTry = ll("f_listFiles", "f_fileSize");

static new LinkedHashSet roots;

p {
  concepts();
  
  addStuff(javaxDataDir());
  addStuff(userHome());
  
  for (S name : safeFunctionsToTry) uniq(Function, +name);
  
  int lastN = -1;
  while licensed {
    int n = countConcepts(Obj);
    if (n == lastN) {
      print("No new objects (have " + n + "), ending");
      break;
    }
    lastN = n;
    if (n >= maxObjects) {
      print("Got more than " + maxObjects + " objects (" + n + "), stopping");
      break;
    }
    tryStuffOnStuff(true);
  }
}

svoid tryStuffOnStuff(bool reapply) {
  for (Obj o)
    unpackStuff(o.o);
  
  for (Function f) {
    Method m = findMethodNamed(mc(), f.name);
    Class<?>[] types = m.getParameterTypes();
    if (l(types) == 1) {
      for (Obj argument : list(Obj)) {
        Application app = findConcept(Application, +f, +argument);
        if (!reapply && app != null) continue;
        O o = argument.o;
        if (isInstanceX(types[0], o)) {
          print("Calling " + f.name + " on object " + argument.id);
          long time = now();
          O out = null;
          Throwable error = null;
          try {
            out = m.invoke(null, o);
          } catch e {
            error = e;
          }
          time = now()-time;
          Obj result = findConcept(Obj, o := out);
          bool seen = result != null;
          if (result == null)
            result = cnew(Obj, o := out);
          
          // have previous result?
          bool prevResult = app != null;
          if (prevResult) {
            if (neq(app.result!, result)) {
              print("  differs");
              // Save previous result
              cnew(PreviousResult, +f, +argument, result := app.result, error := app.error, when := app.when, time := app.time);
              // Mark function non-deterministic
              cset(f, nonDet := true);
            }
          } else
            app = cnew(Application, +f, +argument);
          cset(app, +result, when := now(), +time, error := persistableThrowable(error));
          if (!prevResult)
            print(error != null ? "  error" : seen ? "  seen" : "  new");
        }
      }
    }
  }
}

svoid addStuff(O o) {
  if (o == null) ret;
  if (!hasConcept(Obj, +o)) {
    cnew(Obj, +o);
    print("Got: " + struct(o));
  }
}

svoid unpackStuff(O o) {
  if (o instanceof ArrayList)
    for (O x : (Collection) o)
      addStuff(x);
}

Author comment

Began life as a copy of #1005771

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1005772
Snippet name: Try stuff on stuff 2 [works]
Eternal ID of this version: #1005772/1
Text MD5: a9427af720147607d9f67127eff548f6
Transpilation MD5: cf9ccab4076abda61b3d1a9c17254dc9
Author: stefan
Category: javax
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-12-08 03:24:10
Source code size: 3101 bytes / 131 lines
Pitched / IR pitched: No / No
Views / Downloads: 655 / 764
Referenced in: [show references]