package tudelft.mentalhealth.perfectfit; import java.io.IOException; import java.io.InputStream; import javax.swing.JEditorPane; import javax.swing.JOptionPane; import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.ObjectMapper; import tudelft.dialogmanager.DialogState; import tudelft.dialogmanager.answertypes.AnswerType; import tudelft.dialogmanager.answertypes.InvalidAnswerException; import tudelft.dialogmanager.answertypes.SelectFromListAnswer; import tudelft.dialogmanager.stimuli.Stimulus; import tudelft.dialogmanager.stimuli.Textual; import tudelft.mentalhealth.perfectfit.updatefuncs.CheckDeadline; import tudelft.mentalhealth.perfectfit.updatefuncs.DateString; import tudelft.mentalhealth.perfectfit.updatefuncs.Example; import tudelft.mentalhealth.perfectfit.updatefuncs.StringLength; import tudelft.mentalhealth.perfectfit.updatefuncs.Time; import tudelft.mentalhealth.perfectfit.updatefuncs.TimeDifference; public class Dialog { private final static ObjectMapper jackson = new ObjectMapper(); public Dialog() throws JsonParseException, JsonMappingException, IOException { jackson.registerSubtypes(Example.class, Time.class, CheckDeadline.class, TimeDifference.class, StringLength.class, DateString.class); // initialize from a demo dialog specification InputStream stream = getClass().getClassLoader() .getResourceAsStream("dialog.json"); DialogState state = jackson.readValue(stream, DialogState.class); // run the demo, using popup dialogs to show questions while (!state.isFinal()) { System.out.println(state.getParameters().get("phase")); 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 question = ((Textual) stimulus).getQuestion(); AnswerType answertype = state.getCurrentPhase().getAnswer(); if (answertype instanceof SelectFromListAnswer) { // we just show the keys, values are for fancy dispyas but user // has to type here. question += ((SelectFromListAnswer) answertype).getOptions() .keySet(); } // we are going to present the question as html so replace newlines question = question.replaceAll("\n", "
"); JEditorPane questionarea = new JEditorPane("text/html", question); String answer = JOptionPane.showInputDialog(questionarea); try { state = state.with(answer); state = state.withUpdate(false); } catch (Exception e) { e.printStackTrace(); JOptionPane.showMessageDialog(null, "That was not a valid answer! Please try again"); } } } public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException, InvalidAnswerException { new Dialog(); } }