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

115
LINES

< > BotCompany Repo | #1003401 // Dialog Optimizer (dev.)

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

Libraryless. Click here for Pure Java version (2870L/20K/66K).

1  
!759
2  
3  
!include #1003405 // LooseBot v2
4  
5  
static OccTree<E> tree, botTree;
6  
static O makeBot;
7  
8  
p {
9  
  L<Dialog> dialogs = loadDialogs("Color Showing Bot");
10  
  tree = dialogs2occTree(dialogs);
11  
  botTree = goodDialogs2occTree(dialogs);
12  
  print("Tree size: " + tree.size() + ", bot tree size: " + botTree.size());
13  
  makeBot = func { new LooseBot(botTree) };
14  
  if (!canWalkTree(tree, call(makeBot))) {
15  
    print("Engine not working, abort.");
16  
    ret;
17  
  }
18  
  print("Tree walked! " + inputFed + " -> " + outputGot);
19  
  print("Engine works! Trying to shrink the tree.");
20  
  
21  
  while (canShrinkTree()) {
22  
    print("Tree shrunk, trying it again.");
23  
  }
24  
  
25  
  print("Done shrinking tree (new size: " + botTree.size() + ")");
26  
}
27  
28  
static int inputFed, outputGot;
29  
30  
sbool canShrinkTree() {
31  
  L<OccTree<E>> leaves = botTree.allLeaves();
32  
  print("Leaves found: " + l(leaves));
33  
  
34  
  bool anyChange = false;
35  
  for (OccTree<E> leaf : leaves) {
36  
    Reattach reattach = removeLeaf(leaf);
37  
    if (canWalkTree()) {
38  
      print("Leaf removed successfully!");
39  
      anyChange = true;
40  
    } else
41  
      reattach.reattach();
42  
  }
43  
  
44  
  ret anyChange;
45  
}
46  
47  
sclass Reattach {
48  
  OccTree<E> node, leaf;
49  
  E key;
50  
  
51  
  void reattach() {
52  
    leaf.parent = node;
53  
    node.followUp.put(key, leaf);
54  
  }
55  
}
56  
57  
static Reattach removeLeaf(OccTree<E> leaf) {
58  
  new Reattach r;
59  
  r.leaf = leaf;
60  
  r.node = leaf.parent;
61  
  r.key = reverseLookup(leaf.parent.followUp, leaf);
62  
  leaf.parent.followUp.remove(r.key);
63  
  leaf.parent = null;
64  
  ret r;
65  
}
66  
67  
sbool canWalkTree() {
68  
  ret canWalkTree(tree, call(makeBot));
69  
}
70  
71  
sbool canWalkTree(OccTree<E> tree, O bot) {
72  
  try { walkTree(tree, bot); true; } catch { ret false; }
73  
}
74  
75  
svoid walkTree(OccTree<E> tree, O bot) {
76  
  if (isViable(tree)) {
77  
    for (E e : keys(tree.followUp)) {
78  
      if (!contains(ll("good", "bad"), e.state)) {
79  
        //print("Step   " + structure(e));
80  
        if (isInput(e))
81  
          feed(bot, e);
82  
        else
83  
          checkOutput(e, getOutput(bot));
84  
        walkTree(tree.followUp(e), bot);
85  
        //print("Rewind " + structure(e));
86  
        rewind(bot);
87  
      }
88  
    }
89  
  }
90  
}
91  
92  
svoid rewind(O bot) {
93  
  call(bot, "rewind"); // rewind one step
94  
}
95  
96  
svoid feed(O bot, E e) {
97  
  call(bot, "take", e);
98  
  ++inputFed;
99  
}
100  
101  
static E getOutput(O bot) {
102  
  E e = cast call(bot, "getSingleOutput");
103  
  if (e != null) ++outputGot;
104  
  ret e;
105  
}
106  
107  
svoid checkOutput(E e, E output) {
108  
  assertEquals(e, output);
109  
}
110  
111  
static bool isInput(E e) {
112  
  ret e.q()
113  
    //|| (e.state() && !matchStart("bot", e.state));
114  
    ;
115  
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1003401
Snippet name: Dialog Optimizer (dev.)
Eternal ID of this version: #1003401/1
Text MD5: 9eef431ac339e8bb41a8e372c0c07749
Transpilation MD5: b0f7810165f45a040ea919b6c34529ae
Author: stefan
Category: javax / talking robots
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-06-30 20:50:30
Source code size: 2605 bytes / 115 lines
Pitched / IR pitched: No / No
Views / Downloads: 634 / 666
Referenced in: [show references]