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

152
LINES

< > BotCompany Repo | #1003500 // Dialog Optimizer / What's the date

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

Libraryless. Click here for Pure Java version (3212L/22K/73K).

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

Author comment

Began life as a copy of #1003485

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: #1003500
Snippet name: Dialog Optimizer / What's the date
Eternal ID of this version: #1003500/1
Text MD5: 67efba8207bb6fb25959fc6544fc7062
Transpilation MD5: 18af00872c826c47e4faa217e9a0f045
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-10 01:03:16
Source code size: 3613 bytes / 152 lines
Pitched / IR pitched: No / No
Views / Downloads: 509 / 565
Referenced in: [show references]