package tudelft.dialogmanager; import java.io.IOException; import java.io.InputStream; import java.util.Arrays; import java.util.List; import javax.swing.JOptionPane; import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.ObjectMapper; import tudelft.dialogmanager.answertypes.InvalidAnswerException; import tudelft.dialogmanager.parameters.BoolValue; import tudelft.dialogmanager.parameters.Parameters; import tudelft.dialogmanager.stimuli.Stimulus; import tudelft.dialogmanager.stimuli.Textual; import tudelft.dialogmanager.updatefunctions.UpdateFunction; public class DemoAppWithAdd { private final static ObjectMapper jackson = new ObjectMapper(); public DemoAppWithAdd() throws JsonParseException, JsonMappingException, IOException { jackson.registerSubtypes(MySumFunction.class); // initialize from a demo dialog specification InputStream stream = getClass().getClassLoader() .getResourceAsStream("dialogdemoWithAdd.json"); DialogState state = jackson.readValue(stream, DialogState.class); // run the demo, using popup dialogs to show questions while (!state.isFinal()) { state = state.withUpdate(true); Stimulus stimulus = state.getCurrentPhase().getStimulation() .substitute(state.getParameters()); if (!(stimulus instanceof Textual)) { throw new IllegalStateException( "The Demo App only supports Textual."); } String answer = JOptionPane .showInputDialog(((Textual) stimulus).getQuestion()); try { state = state.with(answer); } catch (Exception e) { e.printStackTrace(); JOptionPane.showMessageDialog(null, "That was not a valid answer! Please try again"); } state = state.withUpdate(false); } } public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException, InvalidAnswerException { new DemoAppWithAdd(); } } /** * Function that sets result= arg1+arg2 == arg3 (a boolean value). We register * this function with the deserializer (see main class above) so that the json * file can use "EqualsFunction" to refer to this. */ class MySumFunction implements UpdateFunction { private final String param1; private final String param2; private final String sumparam; private final String outcomeboolparam; @JsonCreator public MySumFunction(@JsonProperty("param1") String param1, @JsonProperty("param2") String param2, @JsonProperty("sumparam") String sumparam, @JsonProperty("outcomeboolparam") String outcomeboolparam) { this.param1 = param1; this.param2 = param2; this.sumparam = sumparam; this.outcomeboolparam = outcomeboolparam; } @Override public Parameters call(Parameters inparams) { return inparams .with(outcomeboolparam, new BoolValue(inparams.getDouble(param1) + inparams.getDouble(param2) == inparams .getDouble(sumparam))); } @Override public List getAssignedParameters() { return Arrays.asList(outcomeboolparam); } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((outcomeboolparam == null) ? 0 : outcomeboolparam.hashCode()); result = prime * result + ((param1 == null) ? 0 : param1.hashCode()); result = prime * result + ((param2 == null) ? 0 : param2.hashCode()); result = prime * result + ((sumparam == null) ? 0 : sumparam.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; MySumFunction other = (MySumFunction) obj; if (outcomeboolparam == null) { if (other.outcomeboolparam != null) return false; } else if (!outcomeboolparam.equals(other.outcomeboolparam)) return false; if (param1 == null) { if (other.param1 != null) return false; } else if (!param1.equals(other.param1)) return false; if (param2 == null) { if (other.param2 != null) return false; } else if (!param2.equals(other.param2)) return false; if (sumparam == null) { if (other.sumparam != null) return false; } else if (!sumparam.equals(other.sumparam)) return false; return true; } }