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

383
LINES

< > BotCompany Repo | #1017770 // Rule Testing [usually OK]

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

Download Jar. Libraryless. Click here for Pure Java version (11385L/67K).

1  
!7
2  
3  
p-exp {
4  
  bool newEngine = contains(args, "new");
5  
  if (newEngine)
6  
    BakedBot.defaultSnippetID = #1018381;
7  
8  
  if (contains(args, "bake"))
9  
    if (newEngine)
10  
      ai_bakeABot(BakedBot.defaultSnippetID, ll(#1018382));
11  
    else
12  
      bakeTheBot();
13  
      
14  
  if (contains(args, 'advanced)) advancedTests();
15  
  
16  
  callTests([[
17  
    test1
18  
    test2
19  
    test3
20  
    test4
21  
    test5
22  
    test4b
23  
    test6
24  
    test7
25  
    test7b
26  
    test7c
27  
    test7d
28  
    test7e
29  
    test8
30  
    test8b
31  
    test8c
32  
    test8d
33  
    test9
34  
    test9b
35  
    test9c
36  
    test10
37  
    test11
38  
    test12
39  
    test13
40  
    test13b
41  
    test14
42  
    test14b
43  
    test14c
44  
    test15
45  
    test16
46  
    test17
47  
    test18
48  
  ]]);
49  
  consoleStatus(print("BASE TESTS DONE. DOING ADVANCED TESTS."));
50  
  advancedTests();
51  
  consoleStatus(print("BASE & ADVANCED TESTS OK"));
52  
}
53  
54  
svoid advancedTests {
55  
  callTests([[
56  
  ]]);
57  
}
58  
59  
svoid callTests(S s) {
60  
  for (S test : tlftj(s)) {
61  
    consoleStatus(test);
62  
    callMC(test);
63  
  }
64  
}
65  
66  
svoid test1 {
67  
  BakedBot bot = bakedBotWithRules([[
68  
    inputContainsTokens("=>")
69  
      && !inputStartsWith("!rule ")
70  
      => output(Should I store this rule?)
71  
  ]]);
72  
  
73  
  bot.setAuthorized(true);
74  
  bot.check("bla => blubb", "Should I store this rule?");
75  
  assertEqualsVerbose("", unnull(callAnswerMethod(bot, "bla <= blubb")));
76  
  bot.check("!rule bla => bla", "Rule saved as *. Have 2 rules.");
77  
}
78  
79  
svoid test2 {
80  
  BakedBot bot = bakedBotWithRules([[
81  
    input(what is $chicken $wire?) => output(a $wire made from $chicken)
82  
  ]]);
83  
  bot.check("what is pork sausage?", "a sausage made from pork");
84  
}
85  
86  
svoid test3 {
87  
  BakedBot bot = bakedBotWithRules([[
88  
    input(What is $gertrude $afraid_of?)
89  
      && fact($gertrude is $afraid_of $wolves)
90  
      => output($wolves)
91  
  ]]);
92  
  bot.setList("Fact-to-fact rules", [[
93  
    fact($gertrude is a $sheep)
94  
      && fact($sheep are $afraid_of_wolves)
95  
      => fact($gertrude is $afraid_of_wolves)
96  
  ]]);
97  
  bot.setList("Random Facts", [[
98  
    sheep are afraid of wolves
99  
    Cats are afraid of dogs.
100  
    Mice are afraid of cats.
101  
    klaus is a sheep
102  
  ]]);
103  
104  
  setOptAll(bot.bot, ai_deriveFacts_debug := true, ai_applyFactToFactRules_debug := true);
105  
  bot.answerAuthed("!deriveFacts");
106  
  assertEqualsVerbose_match("Klaus is afraid of wolves", bot.getList("Derived facts"));
107  
  pnl((L) call(bot.bot, 'mL_facts));
108  
  call(bot.bot, 'setDebug, true);
109  
  bot.check("What is Klaus afraid of", "Wolves");
110  
}
111  
112  
svoid test4 {
113  
  BakedBot bot = bakedBotWithRules([[
114  
    i(what are the features)
115  
    && allFacts($x is a feature|$x are a feature)
116  
    => o($x1, $x2 [...] and $x_n)
117  
  ]]);
118  
  bot.setList("Random Facts", [[
119  
    fun is a feature
120  
    love is a feature
121  
    drinks are a feature
122  
    music is a feature
123  
  ]]);
124  
  bot.check("what are the features", "fun, love, drinks and music");
125  
}
126  
127  
svoid test4b {
128  
  BakedBot bot = bakedBotWithRules([[
129  
    i(what is that one $feature again) && allFacts($x is a $feature|$x are a $feature) => o(do you mean $x1, $x2 [...] or $x_n?)
130  
  ]]);
131  
  bot.setList("Random Facts", [[
132  
    fun is a feature
133  
    love is a feature
134  
    drinks are a feature
135  
    music is a feature
136  
  ]]);
137  
  bot.check("what is that one feature again", "Do you mean fun, love, drinks or music?");
138  
}
139  
140  
svoid test5 {
141  
  S rule = [[
142  
    [id: qohpbitnzaqfexmz] i(what is rule $x) => evalOutput(trim(getString(call(ai_context(), 'getRule, $x), 'originalText)))
143  
  ]];
144  
145  
  BakedBot bot = bakedBotWithRules(rule);
146  
  bot.check("what is rule qohpbitnzaqfexmz", trim(rule));
147  
}
148  
149  
svoid test6 {
150  
  BakedBot bot = bakedBotWithRules([[i($John is $in_the_playground) && $John != what => o(yo)]]);
151  
  bot.check("bla is bla", "yo");
152  
  bot.check("what is bla", null);
153  
}
154  
155  
svoid test7 {
156  
  bakedBotWithRules([[i(bla) && $a, $b = eval(joinWithComma(pair("x", "y"))) => o($b and $a)]]).check("bla", "y and x");
157  
}
158  
159  
svoid test7b {
160  
  bakedBotWithRules([[i(bla) && $a, $b = eval(joinWithComma(pair("bla bla", "this and that"))) => o($b and $a)]]).check("bla", "this and that and bla bla");
161  
}
162  
163  
svoid test7c {
164  
  bakedBotWithRules([[i(bla) && $a, $b = eval(pair("bla bla", "this and that")) => o($b and $a)]]).check("bla", "this and that and bla bla");
165  
}
166  
167  
svoid test7d {
168  
  bakedBotWithRules([[i(bla) && $a, $b, $c = eval(ll("let's", "do", "it")) => o($c $b $a)]]).check("bla", "it do let's");
169  
}
170  
171  
svoid test7e {
172  
  bakedBotWithRules([[i(bla) && $a, $b, $c = eval(litorderedset("let's", "do", "it")) => o($c $b $a)]]).check("bla", "it do let's");
173  
}
174  
175  
sS mineralsRule = [[
176  
  [id: zrpcdxxfqzgdkzof] fact(I saw $a_man $lick $minerals)
177  
  && fact($_q_licked is past tense for $_q_lick)
178  
  => newID($the_man)
179  
  && fact($the_man was $a_man)
180  
  && fact(I saw $the_man)
181  
  && fact($the_man $licked $minerals)
182  
]];
183  
184  
svoid test8 {
185  
  bakedBotWithRules(mineralsRule).check("!factsFrom i saw a girl eat frogs", "No new facts found");
186  
    // no result because there is no past tense for "eat"
187  
}
188  
189  
svoid test8b {
190  
  bakedBotWithRulesAndFacts(mineralsRule,
191  
    [["licked" is past tense for "lick"]])
192  
    .check("!factsFrom i saw a girl eat frogs", "No new facts found");
193  
    // still no result because there is no past tense for "eat"
194  
}
195  
196  
svoid test8c {
197  
  BakedBot bot = bakedBotWithRulesAndFacts(mineralsRule,
198  
    [["licked" is past tense for "lick"]]);
199  
  setOptAll(bot.bot, ai_deriveFacts_debug := true, ai_applyFactToFactRules_debug := true);
200  
  S s = bot.answer_verbose("!factsFrom [debug] i saw a girl lick walls");
201  
  print("l(s)= " + l(s));
202  
  assertNotNull(printStruct(jmatchDollarVars([[
203  
    $x was a girl
204  
    I saw $x
205  
    $x $licked walls
206  
  ]], s));
207  
}
208  
209  
svoid test8d {
210  
  bakedBotWithRulesAndFacts(joinWithEmptyLines(mineralsRule, [[
211  
    [id: xxrmapvwhesonlyo] unresolvedFact($x is past tense for $_q_y)
212  
    && eval(ai_isVar($x) && !ai_isVar($y))
213  
    => output(What is the past tense of $_q_y?)
214  
  ]]),
215  
    [["licked" is past tense for "lick"]])
216  
    .check("!askBackAboutFact i saw a girl eat frogs", [[What is the past tense of "eat"?]]);
217  
}
218  
219  
// !deriveFacts with rules containing eval
220  
svoid test9 {
221  
  new BakedBot bot;
222  
  bot.setList("Fact-to-fact rules", [[
223  
    eval(1==1) => fact(ok)
224  
  ]]);
225  
226  
  setOptAll(bot.bot, ai_deriveFacts_debug := true, ai_applyFactToFactRules_debug := true);
227  
  bot.answerAuthed("!deriveFacts");
228  
  assertEqualsVerbose_match("ok", bot.getList("Derived facts"));
229  
}
230  
231  
// !deriveFacts with rules containing "$x in eval(...")
232  
svoid test9b {
233  
  new BakedBot bot;
234  
  bot.setList("Fact-to-fact rules", [[
235  
    $x in eval(ll("a", "b")) => fact($x is ok)
236  
  ]]);
237  
238  
  setOptAll(bot.bot, ai_deriveFacts_debug := true, ai_applyFactToFactRules_debug := true);
239  
  bot.answerAuthed("!deriveFacts");
240  
  assertEqualsVerbose_match([[
241  
    a is ok
242  
    b is ok
243  
  ]], bot.getList("Derived facts"));
244  
}
245  
246  
// !deriveFacts with rules containing "$x, $y in eval(...")
247  
svoid test9c {
248  
  new BakedBot bot;
249  
  bot.setList("Fact-to-fact rules", [[
250  
    $x, $y in eval(ll(pair("a", "b"), pair("c", "d"))) => fact($x and $y are ok)
251  
  ]]);
252  
253  
  setOptAll(bot.bot, ai_deriveFacts_debug := true, ai_applyFactToFactRules_debug := true);
254  
  bot.answerAuthed("!deriveFacts");
255  
  assertEqualsVerbose_match([[
256  
    a and b are ok
257  
    c and d are ok
258  
  ]], bot.getList("Derived facts"));
259  
}
260  
261  
svoid test10 {
262  
  BakedBot bot = bakedBotWithRulesAndFacts([[
263  
    i(is $love $nice) && eval(ai_isTrue($love + " is " + $nice)) => o(yo)
264  
  ]], [[love is nice]]);
265  
  bot.check("is love nice", "yo");
266  
  bot.check("is love green", "");
267  
}
268  
269  
svoid test11 {
270  
  BakedBot bot = bakedBotWithRules([[
271  
    i(yo $x) => o[javaTokNoQuotes](well: "$x")
272  
  ]]);
273  
  bot.check("yo test", [[well: "test"]]);
274  
}
275  
276  
svoid test12 {
277  
  BakedBot bot = bakedBotWithRules([[
278  
    anyInput() => outputEval("You said " + ai_input())
279  
  ]]);
280  
  bot.check("horse", "You said horse");
281  
}
282  
283  
svoid test13 {
284  
  BakedBot bot = bakedBotWithRules([[
285  
    input[re]([abc]{3}) => o(triggered)
286  
  ]]);
287  
  bot.check("aa", "");
288  
  bot.check("aaa", "triggered");
289  
  bot.check("bab", "triggered");
290  
  bot.check("bdb", "");
291  
  bot.check("xx ccc yy", "triggered");
292  
}
293  
294  
svoid test13b {
295  
  BakedBot bot = bakedBotWithRules([[
296  
    input[re](das ist #ADJ_BILLIG#) => o(ja)
297  
  ]]);
298  
  bot.lists.setText("Ctx Macros (German)", [[
299  
    ADJ_BILLIG = (_(billig|fast geschenkt|preiswert|(preis|)günstig)(|e|e[mnrs])_)
300  
  ]]);
301  
  
302  
  bot.check("das ist fast geschenkt yo", "ja");
303  
  bot.check("das ist billig", "ja");
304  
  bot.check("das ist teuer", "");
305  
}
306  
307  
svoid test14 {
308  
  BakedBot bot = bakedBotWithRules([[
309  
    phrase[words](hello $x)
310  
    => o(i am $x)
311  
  ]]);
312  
  bot.useIterate(true);
313  
  
314  
  bot.check("hello you", "i am you");
315  
  bot.check("yo hello guy yes", "i am guy");
316  
}
317  
318  
svoid test14b {
319  
  BakedBot bot = bakedBotWithRules([[
320  
    phrase(hello $x)
321  
    => o(i am $x)
322  
  ]]);
323  
  bot.useIterate(true);
324  
  
325  
  bot.check("hello you", "i am you");
326  
  bot.check("yo hello guy yes", "i am guy");
327  
}
328  
329  
svoid test14c {
330  
  BakedBot bot = bakedBotWithRules([[
331  
    phrase(alexa) && !phrase(stop)
332  
    => o(nice)
333  
  ]]);
334  
  bot.useIterate(true);
335  
  
336  
  bot.debug(true);
337  
  bot.check("alexa", "nice");
338  
  bot.check("alexa stop", "");
339  
}
340  
341  
svoid test15 {
342  
  BakedBot bot = bakedBotWithRules([[
343  
    i(test) => i(bla)
344  
    
345  
    i(bla) => o(ok)
346  
  ]]);
347  
  
348  
  bot.check("test", "ok");
349  
}
350  
351  
svoid test16 {
352  
  BakedBot bot = bakedBotWithRules([[
353  
    [id: jpltfkfmtbfiorxu] i(do i have any mails|do i have any mail|do i have any emails) => o(you have news from creator awards)
354  
355  
[id: fwobkcfddsldvrhb]
356  
  followingUpOn(jpltfkfmtbfiorxu)
357  
  && i(please open it|please open|open|open it|on the screen|on screen)
358  
  => o("here you go sir")
359  
  ]]);
360  
  
361  
  bot.check("do i have any mails", "you have news from creator awards");
362  
  bot.check("open it", "here you go sir");
363  
}
364  
365  
svoid test17 {
366  
  BakedBot bot = bakedBotWithRules([[
367  
    i(test) && evalTrue(print("hello")) => o(ok)
368  
  ]]);
369  
  bot.check("test", "ok");
370  
}
371  
372  
svoid test18 {
373  
  BakedBot bot = bakedBotWithRules([[
374  
    [id: zaycfgkuplemvmvb] input[flexMatch](say $x) => o($x)
375  
  ]]);
376  
  bot.useIterate(true);
377  
  bot.debug(true);
378  
  setOpt(bot.bot, nlLogic_flexMatch_iterate_debug := true);
379  
  bot.check("say", "");
380  
  bot.check("say yo", "yo");
381  
  bot.check("say yo yo", "yo yo");
382  
  bot.check("say oki doki", "oki doki");
383  
}

Author comment

Began life as a copy of #1017769

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1017770
Snippet name: Rule Testing [usually OK]
Eternal ID of this version: #1017770/90
Text MD5: dd284077c208ae884f37fc25509b5090
Transpilation MD5: e641afd362d90d84226dea84f6f0925f
Author: stefan
Category: javax / a.i.
Type: JavaX source code (desktop)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-09-30 02:54:18
Source code size: 10371 bytes / 383 lines
Pitched / IR pitched: No / No
Views / Downloads: 674 / 2582
Version history: 89 change(s)
Referenced in: [show references]