source: dialogmanager/src/test/java/tudelft/dialogmanager/DemoAppWithAdd.java@ 1108

Last change on this file since 1108 was 436, checked in by wouter, 23 months ago

#136 working on dialogdemowithadd.

File size: 4.3 KB
Line 
1package tudelft.dialogmanager;
2
3import java.io.IOException;
4import java.io.InputStream;
5import java.util.Arrays;
6import java.util.List;
7
8import javax.swing.JOptionPane;
9
10import com.fasterxml.jackson.annotation.JsonCreator;
11import com.fasterxml.jackson.annotation.JsonProperty;
12import com.fasterxml.jackson.core.JsonParseException;
13import com.fasterxml.jackson.databind.JsonMappingException;
14import com.fasterxml.jackson.databind.ObjectMapper;
15
16import tudelft.dialogmanager.answertypes.InvalidAnswerException;
17import tudelft.dialogmanager.parameters.BoolValue;
18import tudelft.dialogmanager.parameters.Parameters;
19import tudelft.dialogmanager.stimuli.Stimulus;
20import tudelft.dialogmanager.stimuli.Textual;
21import tudelft.dialogmanager.updatefunctions.UpdateFunction;
22
23public class DemoAppWithAdd {
24 private final static ObjectMapper jackson = new ObjectMapper();
25
26 public DemoAppWithAdd()
27 throws JsonParseException, JsonMappingException, IOException {
28 jackson.registerSubtypes(MySumFunction.class);
29
30 // initialize from a demo dialog specification
31 InputStream stream = getClass().getClassLoader()
32 .getResourceAsStream("dialogdemoWithAdd.json");
33 DialogState state = jackson.readValue(stream, DialogState.class);
34
35 // run the demo, using popup dialogs to show questions
36 while (!state.isFinal()) {
37 state = state.withUpdate(true);
38 Stimulus stimulus = state.getCurrentPhase().getStimulation()
39 .substitute(state.getParameters());
40 if (!(stimulus instanceof Textual)) {
41 throw new IllegalStateException(
42 "The Demo App only supports Textual.");
43 }
44 String answer = JOptionPane
45 .showInputDialog(((Textual) stimulus).getQuestion());
46 try {
47 state = state.with(answer);
48 } catch (Exception e) {
49 e.printStackTrace();
50 JOptionPane.showMessageDialog(null,
51 "That was not a valid answer! Please try again");
52 }
53 state = state.withUpdate(false);
54 }
55
56 }
57
58 public static void main(String[] args) throws JsonParseException,
59 JsonMappingException, IOException, InvalidAnswerException {
60 new DemoAppWithAdd();
61 }
62}
63
64/**
65 * Function that sets result= arg1+arg2 == arg3 (a boolean value). We register
66 * this function with the deserializer (see main class above) so that the json
67 * file can use "EqualsFunction" to refer to this.
68 */
69class MySumFunction implements UpdateFunction {
70
71 private final String param1;
72 private final String param2;
73 private final String sumparam;
74 private final String outcomeboolparam;
75
76 @JsonCreator
77 public MySumFunction(@JsonProperty("param1") String param1,
78 @JsonProperty("param2") String param2,
79 @JsonProperty("sumparam") String sumparam,
80 @JsonProperty("outcomeboolparam") String outcomeboolparam) {
81 this.param1 = param1;
82 this.param2 = param2;
83 this.sumparam = sumparam;
84 this.outcomeboolparam = outcomeboolparam;
85 }
86
87 @Override
88 public Parameters call(Parameters inparams) {
89 return inparams
90 .with(outcomeboolparam,
91 new BoolValue(inparams.getDouble(param1)
92 + inparams.getDouble(param2) == inparams
93 .getDouble(sumparam)));
94 }
95
96 @Override
97 public List<String> getAssignedParameters() {
98 return Arrays.asList(outcomeboolparam);
99 }
100
101 @Override
102 public int hashCode() {
103 final int prime = 31;
104 int result = 1;
105 result = prime * result + ((outcomeboolparam == null) ? 0
106 : outcomeboolparam.hashCode());
107 result = prime * result + ((param1 == null) ? 0 : param1.hashCode());
108 result = prime * result + ((param2 == null) ? 0 : param2.hashCode());
109 result = prime * result
110 + ((sumparam == null) ? 0 : sumparam.hashCode());
111 return result;
112 }
113
114 @Override
115 public boolean equals(Object obj) {
116 if (this == obj)
117 return true;
118 if (obj == null)
119 return false;
120 if (getClass() != obj.getClass())
121 return false;
122 MySumFunction other = (MySumFunction) obj;
123 if (outcomeboolparam == null) {
124 if (other.outcomeboolparam != null)
125 return false;
126 } else if (!outcomeboolparam.equals(other.outcomeboolparam))
127 return false;
128 if (param1 == null) {
129 if (other.param1 != null)
130 return false;
131 } else if (!param1.equals(other.param1))
132 return false;
133 if (param2 == null) {
134 if (other.param2 != null)
135 return false;
136 } else if (!param2.equals(other.param2))
137 return false;
138 if (sumparam == null) {
139 if (other.sumparam != null)
140 return false;
141 } else if (!sumparam.equals(other.sumparam))
142 return false;
143 return true;
144 }
145
146}
Note: See TracBrowser for help on using the repository browser.