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

108
LINES

< > BotCompany Repo | #1004778 // "?id music" (WORKS)

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

Transpiled version (2679L) is out of date.

1  
!759
2  
3  
!include #1004810 // Concepts etc.
4  
5  
//include #1004817 // externalize stuff (repeat less code)
6  
7  
// What we think the user chose
8  
sclass OptionChosen extends Concept {
9  
  new Ref<UserAnswer> userAnswer;
10  
  S choice;
11  
}
12  
13  
extend Answer {
14  
  new Ref<OptionChosen> inResponseTo;
15  
}
16  
17  
p {
18  
  loadConcepts();
19  
  autoSaveConcepts();
20  
  action();
21  
}
22  
23  
static void processNewUserLine(UserLine ul) {
24  
  S s = trim(ul.post.text);
25  
  
26  
  if (swic(s, "?id ")) { // assume "?id music"
27  
    // action: ask user what he means by "music"
28  
    
29  
    S q = trim(s.substring(2));
30  
    print("ul= " + structure(ul));
31  
    Question question = findBackRefOfType(ul, Question.class);
32  
    print("Question = " + struct(question));
33  
    
34  
    // did we ask this yet? if not: do it
35  
    
36  
    if (question == null) {
37  
      S text = "Uh... do you want some MUSIC or the MUSIC PLAYER?";
38  
      UserLine ulQ = postToChat(text);
39  
      if (ulQ == null) { print("Couldn't post!"); ret; }
40  
      question = new Question;
41  
      question.userLine.set(ulQ);
42  
      question = new Question(ul);
43  
    }
44  
  }
45  
}
46  
47  
static void dbActions {
48  
  // Scan through the answers we got from user and do something
49  
  // with them.
50  
  
51  
  L<UserAnswer> userAnswers = conceptsOfType(UserAnswer.class);
52  
  //printStructure("User Answers: ", userAnswers);
53  
  for (UserAnswer ua : userAnswers) {
54  
    // We only ask one question, so we know what this is about
55  
    
56  
    ELPost post = ua.userLine.get().post;
57  
    //print("Processing: " + post.text);
58  
    
59  
    OptionChosen option = findBackRef(ua, OptionChosen.class);
60  
    
61  
    if (option == null) {
62  
      // Interpret & save what user has said (-> "OptionChosen")
63  
    
64  
      option = new OptionChosen;
65  
      option.userAnswer.set(ua);
66  
      option.choice = getOption(trim(post.text));
67  
68  
      // VERIFY new link
69  
      
70  
      assertSame(option, findBackRef(ua, OptionChosen.class));
71  
    }
72  
    
73  
    // Check if we answered already.
74  
    
75  
    Answer ourAnswer = findBackRef(option, Answer.class);
76  
    
77  
    if (ourAnswer != null) {
78  
      //print("  We answered already.");
79  
    } else {
80  
      ourAnswer = new Answer;
81  
      ourAnswer.inResponseTo.set(option);
82  
83  
      // VERIFY that we solved the condition above
84  
      assertSame(ourAnswer, findBackRef(option, Answer.class));
85  
      
86  
      S text = makeAnswer(option);
87  
      UserLine ulA = postToChat(text);
88  
      if (ulA == null) { print("Couldn't post!"); ret; }
89  
      ourAnswer.userLine.set(ulA);
90  
    }
91  
  }
92  
}
93  
94  
// learnable! [interpret user input]
95  
// returns "music", "player" or null (any unknown input)
96  
static S getOption(S s) {
97  
  if (containsIgnoreCase(s, "player")) ret "player";
98  
  if (containsIgnoreCase(s, "music")) ret "music";
99  
  null;
100  
}
101  
102  
static S makeAnswer(OptionChosen option) {
103  
  if (eq(option.choice, "player"))
104  
    ret "Current Music Player Is: " + snippetWithTitle("#1003779");
105  
  if (eq(option.choice, "music"))
106  
    ret "Here's some polish music: " + snippetWithTitle("#1001093");
107  
  ret "I don't understand";
108  
}

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: #1004778
Snippet name: "?id music" (WORKS)
Eternal ID of this version: #1004778/1
Text MD5: b7ea29e164e94f174478df59b73423ec
Author: stefan
Category: javax / talking robots
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-09-02 14:03:35
Source code size: 3034 bytes / 108 lines
Pitched / IR pitched: No / No
Views / Downloads: 825 / 1180
Referenced in: [show references]