source: PerfectFit/Dialog/src/main/java/tudelft/mentalhealth/perfectfit/Dialog.java

Last change on this file was 7, checked in by Wouter Pasman, 9 months ago

#124 release PerfectFit sources

File size: 2.9 KB
Line 
1package tudelft.mentalhealth.perfectfit;
2
3import java.io.IOException;
4import java.io.InputStream;
5
6import javax.swing.JEditorPane;
7import javax.swing.JOptionPane;
8
9import com.fasterxml.jackson.core.JsonParseException;
10import com.fasterxml.jackson.databind.JsonMappingException;
11import com.fasterxml.jackson.databind.ObjectMapper;
12
13import tudelft.dialogmanager.DialogState;
14import tudelft.dialogmanager.answertypes.AnswerType;
15import tudelft.dialogmanager.answertypes.InvalidAnswerException;
16import tudelft.dialogmanager.answertypes.SelectFromListAnswer;
17import tudelft.dialogmanager.stimuli.Stimulus;
18import tudelft.dialogmanager.stimuli.Textual;
19import tudelft.mentalhealth.perfectfit.updatefuncs.CheckDeadline;
20import tudelft.mentalhealth.perfectfit.updatefuncs.DateString;
21import tudelft.mentalhealth.perfectfit.updatefuncs.Example;
22import tudelft.mentalhealth.perfectfit.updatefuncs.StringLength;
23import tudelft.mentalhealth.perfectfit.updatefuncs.Time;
24import tudelft.mentalhealth.perfectfit.updatefuncs.TimeDifference;
25
26public class Dialog {
27 private final static ObjectMapper jackson = new ObjectMapper();
28
29 public Dialog()
30 throws JsonParseException, JsonMappingException, IOException {
31 jackson.registerSubtypes(Example.class, Time.class, CheckDeadline.class,
32 TimeDifference.class, StringLength.class, DateString.class);
33
34 // initialize from a demo dialog specification
35 InputStream stream = getClass().getClassLoader()
36 .getResourceAsStream("dialog.json");
37 DialogState state = jackson.readValue(stream, DialogState.class);
38
39 // run the demo, using popup dialogs to show questions
40 while (!state.isFinal()) {
41 System.out.println(state.getParameters().get("phase"));
42 state = state.withUpdate(true);
43 Stimulus stimulus = state.getCurrentPhase().getStimulation()
44 .substitute(state.getParameters());
45 if (!(stimulus instanceof Textual)) {
46 throw new IllegalStateException(
47 "The Demo App only supports Textual.");
48 }
49 String question = ((Textual) stimulus).getQuestion();
50 AnswerType answertype = state.getCurrentPhase().getAnswer();
51 if (answertype instanceof SelectFromListAnswer) {
52 // we just show the keys, values are for fancy dispyas but user
53 // has to type here.
54 question += ((SelectFromListAnswer) answertype).getOptions()
55 .keySet();
56 }
57
58 // we are going to present the question as html so replace newlines
59 question = question.replaceAll("\n", "<br/>");
60 JEditorPane questionarea = new JEditorPane("text/html", question);
61 String answer = JOptionPane.showInputDialog(questionarea);
62 try {
63 state = state.with(answer);
64 state = state.withUpdate(false);
65 } catch (Exception e) {
66 e.printStackTrace();
67 JOptionPane.showMessageDialog(null,
68 "That was not a valid answer! Please try again");
69 }
70
71 }
72
73 }
74
75 public static void main(String[] args) throws JsonParseException,
76 JsonMappingException, IOException, InvalidAnswerException {
77 new Dialog();
78 }
79}
Note: See TracBrowser for help on using the repository browser.