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

1  
!752
2  
3  
static int maxObjects = 1000;
4  
5  
concepts.
6  
7  
concept Obj {
8  
  O o;
9  
  
10  
  *() {}
11  
  *(O *o) { change(); }
12  
}
13  
14  
concept Function {
15  
  S name;
16  
  bool nonDet;
17  
  
18  
  *() {}
19  
  *(S *name) { change(); }
20  
}
21  
22  
concept Result {
23  
  new Ref<Function> f;
24  
  new Ref<Obj> argument;
25  
  new Ref<Obj> result;
26  
  PersistableThrowable error;
27  
  long when, time;
28  
}
29  
30  
Result > Application {}
31  
32  
Result > PreviousResult {}
33  
34  
static L<File> f_listFiles(File dir) {
35  
  ret asList(listFiles(dir));
36  
}
37  
38  
static long f_fileSize(File f) {
39  
  ret f.length();
40  
}
41  
42  
static L<S> safeFunctionsToTry = ll("f_listFiles", "f_fileSize");
43  
44  
static new LinkedHashSet roots;
45  
46  
p {
47  
  concepts();
48  
  
49  
  addStuff(javaxDataDir());
50  
  addStuff(userHome());
51  
  
52  
  for (S name : safeFunctionsToTry) uniq(Function, +name);
53  
  
54  
  int lastN = -1;
55  
  while licensed {
56  
    int n = countConcepts(Obj);
57  
    if (n == lastN) {
58  
      print("No new objects (have " + n + "), ending");
59  
      break;
60  
    }
61  
    lastN = n;
62  
    if (n >= maxObjects) {
63  
      print("Got more than " + maxObjects + " objects (" + n + "), stopping");
64  
      break;
65  
    }
66  
    tryStuffOnStuff(true);
67  
  }
68  
}
69  
70  
svoid tryStuffOnStuff(bool reapply) {
71  
  for (Obj o)
72  
    unpackStuff(o.o);
73  
  
74  
  for (Function f) {
75  
    Method m = findMethodNamed(mc(), f.name);
76  
    Class<?>[] types = m.getParameterTypes();
77  
    if (l(types) == 1) {
78  
      for (Obj argument : list(Obj)) {
79  
        Application app = findConcept(Application, +f, +argument);
80  
        if (!reapply && app != null) continue;
81  
        O o = argument.o;
82  
        if (isInstanceX(types[0], o)) {
83  
          print("Calling " + f.name + " on object " + argument.id);
84  
          long time = now();
85  
          O out = null;
86  
          Throwable error = null;
87  
          try {
88  
            out = m.invoke(null, o);
89  
          } catch e {
90  
            error = e;
91  
          }
92  
          time = now()-time;
93  
          Obj result = findConcept(Obj, o := out);
94  
          bool seen = result != null;
95  
          if (result == null)
96  
            result = cnew(Obj, o := out);
97  
          
98  
          // have previous result?
99  
          bool prevResult = app != null;
100  
          if (prevResult) {
101  
            if (neq(app.result!, result)) {
102  
              print("  differs");
103  
              // Save previous result
104  
              cnew(PreviousResult, +f, +argument, result := app.result, error := app.error, when := app.when, time := app.time);
105  
              // Mark function non-deterministic
106  
              cset(f, nonDet := true);
107  
            }
108  
          } else
109  
            app = cnew(Application, +f, +argument);
110  
          cset(app, +result, when := now(), +time, error := persistableThrowable(error));
111  
          if (!prevResult)
112  
            print(error != null ? "  error" : seen ? "  seen" : "  new");
113  
        }
114  
      }
115  
    }
116  
  }
117  
}
118  
119  
svoid addStuff(O o) {
120  
  if (o == null) ret;
121  
  if (!hasConcept(Obj, +o)) {
122  
    cnew(Obj, +o);
123  
    print("Got: " + struct(o));
124  
  }
125  
}
126  
127  
svoid unpackStuff(O o) {
128  
  if (o instanceof ArrayList)
129  
    for (O x : (Collection) o)
130  
      addStuff(x);
131  
}

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: 657 / 767
Referenced in: [show references]