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

151
LINES

< > BotCompany Repo | #1003408 // Dialog Optimizer 2 (works!)

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

Libraryless. Click here for Pure Java version (2999L/21K/67K).

1  
!759
2  
3  
static S theme =
4  
  //"Color Showing Bot";
5  
  "Yo robot";
6  
7  
//include #1003412 // LooseBot v4
8  
!include #1003413 // LooseBot v5
9  
!include #1003407 // OccTree2
10  
11  
static OccTree2 tree, botTree;
12  
static O makeBot;
13  
sbool walkTree_debug;
14  
sbool useAdapter = true;
15  
16  
p {
17  
  L<Dialog> dialogs = loadDialogs(theme);
18  
  tree = goodDialogs2occTree2(dialogs);
19  
  botTree = goodDialogs2occTree2(dialogs);
20  
  printOccTree2(botTree);
21  
  print("Tree size: " + tree.size() + ", bot tree size: " + botTree.size());
22  
  makeBot = func { new LooseBot(botTree, func { new WordAdapter }) };
23  
  //walkTree_debug = true;
24  
  try {
25  
    walkTree();
26  
  } catch e {
27  
    print("Engine not working, abort.");
28  
    printStackTrace(e);
29  
    ret;
30  
  }
31  
  print("Tree walked! " + inputFed + " -> " + outputGot);
32  
  
33  
  int origSize = botTree.size()-1;
34  
  print("Engine works! Trying to shrink the tree.");
35  
  
36  
  while (canShrinkTree()) {
37  
    print("Tree shrunk, trying it again.");
38  
  }
39  
  
40  
  int newSize = botTree.size()-1;
41  
  printOccTree2(botTree);
42  
  if (newSize < origSize) {
43  
    long percent = newSize == 0 ? 9999 : round(origSize*100.0/newSize);
44  
    print("Done shrinking tree (" + origSize + " -> " + newSize + " entries - " + percent + "% shrinkage!)");
45  
  } else
46  
    print("Tree unshrinkable.");
47  
}
48  
49  
static int inputFed, outputGot;
50  
51  
sbool canShrinkTree() {
52  
  L<OccTree2> leaves = botTree.allLeaves();
53  
  print("Leaves found: " + l(leaves));
54  
  
55  
  bool anyChange = false;
56  
  for (OccTree2 leaf : leaves) {
57  
    print("Trying to remove leaf: " + leaf.e + " (level " + leaf.level() + ")");
58  
    Reattach reattach = removeLeaf(leaf);
59  
    //printOccTree2(botTree);
60  
    if (canWalkTree()) {
61  
      print("Leaf removed successfully!");
62  
      anyChange = true;
63  
    } else
64  
      reattach.reattach();
65  
  }
66  
  
67  
  ret anyChange;
68  
}
69  
70  
sclass Reattach {
71  
  OccTree2 node, leaf;
72  
  int idx;
73  
74  
  void reattach() {
75  
    leaf.parent = node;
76  
    node.next.add(idx, leaf);
77  
  }
78  
}
79  
80  
static Reattach removeLeaf(OccTree2 leaf) {
81  
  new Reattach r;
82  
  r.leaf = leaf;
83  
  r.node = leaf.parent;
84  
  r.idx = leaf.parent.next.indexOf(leaf);
85  
  leaf.parent.next.remove(r.idx);
86  
  leaf.parent = null;
87  
  ret r;
88  
}
89  
90  
sbool canWalkTree() {
91  
  ret canWalkTree(tree, call(makeBot));
92  
}
93  
94  
sbool canWalkTree(OccTree2 tree, O bot) {
95  
  try { walkTree(tree, bot); true; } catch e {
96  
    if (walkTree_debug)
97  
      printStackTrace(e);
98  
    ret false;
99  
  }
100  
}
101  
102  
svoid walkTree() {
103  
  walkTree(tree, call(makeBot));
104  
}
105  
106  
svoid walkTree(OccTree2 tree, O bot) {
107  
  if (tree.isLeaf())
108  
    assertNull(getOutput(bot));
109  
  else
110  
    for (E e : tree.followUpKeys()) {
111  
      if (!contains(ll("good", "bad"), e.state)) {
112  
        bool input = isInput(e);
113  
        if (walkTree_debug)
114  
          print((input ? "FEED " : "CHECK ") + structure(e));
115  
        if (input) {
116  
          assertNull(getOutput(bot));
117  
          feed(bot, e);
118  
        } else
119  
          checkOutput(e, getOutput(bot));
120  
        walkTree(tree.followUp(e), bot);
121  
        if (walkTree_debug)
122  
          print("Rewind " + structure(e));
123  
        rewind(bot);
124  
      }
125  
    }
126  
}
127  
128  
svoid rewind(O bot) {
129  
  call(bot, "rewind"); // rewind one step
130  
}
131  
132  
svoid feed(O bot, E e) {
133  
  call(bot, "take", e);
134  
  ++inputFed;
135  
}
136  
137  
static E getOutput(O bot) {
138  
  E e = cast call(bot, "getSingleOutput");
139  
  if (e != null) ++outputGot;
140  
  ret e;
141  
}
142  
143  
svoid checkOutput(E e, E output) {
144  
  assertEquals(e, output);
145  
}
146  
147  
static bool isInput(E e) {
148  
  ret e.q()
149  
    //|| (e.state() && !matchStart("bot", e.state));
150  
    ;
151  
}

Author comment

Began life as a copy of #1003401

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: #1003408
Snippet name: Dialog Optimizer 2 (works!)
Eternal ID of this version: #1003408/1
Text MD5: 6d902feb81a077ac1e4bab0b2632a4ba
Transpilation MD5: 45821d108c4d8f4dc47b68d04052a4d4
Author: stefan
Category: javax / talking robots
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-07-05 17:47:45
Source code size: 3550 bytes / 151 lines
Pitched / IR pitched: No / No
Views / Downloads: 675 / 825
Referenced in: [show references]