1 | package tudelft.mentalhealth.perfectfit;
|
---|
2 |
|
---|
3 | import java.io.IOException;
|
---|
4 | import java.io.InputStream;
|
---|
5 |
|
---|
6 | import javax.swing.JEditorPane;
|
---|
7 | import javax.swing.JOptionPane;
|
---|
8 |
|
---|
9 | import com.fasterxml.jackson.core.JsonParseException;
|
---|
10 | import com.fasterxml.jackson.databind.JsonMappingException;
|
---|
11 | import com.fasterxml.jackson.databind.ObjectMapper;
|
---|
12 |
|
---|
13 | import tudelft.dialogmanager.DialogState;
|
---|
14 | import tudelft.dialogmanager.answertypes.AnswerType;
|
---|
15 | import tudelft.dialogmanager.answertypes.InvalidAnswerException;
|
---|
16 | import tudelft.dialogmanager.answertypes.SelectFromListAnswer;
|
---|
17 | import tudelft.dialogmanager.stimuli.Stimulus;
|
---|
18 | import tudelft.dialogmanager.stimuli.Textual;
|
---|
19 | import tudelft.mentalhealth.perfectfit.updatefuncs.CheckDeadline;
|
---|
20 | import tudelft.mentalhealth.perfectfit.updatefuncs.DateString;
|
---|
21 | import tudelft.mentalhealth.perfectfit.updatefuncs.Example;
|
---|
22 | import tudelft.mentalhealth.perfectfit.updatefuncs.StringLength;
|
---|
23 | import tudelft.mentalhealth.perfectfit.updatefuncs.Time;
|
---|
24 | import tudelft.mentalhealth.perfectfit.updatefuncs.TimeDifference;
|
---|
25 |
|
---|
26 | public 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 | } |
---|