source: TraumaOntologies/src/main/java/tudelft/healthpsychology/traumaontologies/SimpleGUI.java@ 4

Last change on this file since 4 was 4, checked in by Bart Vastenhouw, 5 years ago

Added traumaontologies

File size: 8.6 KB
Line 
1package tudelft.healthpsychology.traumaontologies;
2
3import java.awt.BorderLayout;
4import java.awt.HeadlessException;
5import java.io.FileNotFoundException;
6import java.io.IOException;
7import java.util.Arrays;
8
9import javax.swing.BoxLayout;
10import javax.swing.JButton;
11import javax.swing.JComboBox;
12import javax.swing.JFrame;
13import javax.swing.JLabel;
14import javax.swing.JOptionPane;
15import javax.swing.JPanel;
16import javax.swing.JScrollPane;
17import javax.swing.JTextArea;
18import javax.swing.JTextField;
19import javax.swing.SwingUtilities;
20
21import org.semanticweb.owlapi.apibinding.OWLManager;
22import org.semanticweb.owlapi.model.OWLOntology;
23import org.semanticweb.owlapi.model.OWLOntologyCreationException;
24import org.semanticweb.owlapi.model.OWLOntologyManager;
25
26import com.fasterxml.jackson.core.JsonProcessingException;
27import com.fasterxml.jackson.databind.ObjectMapper;
28
29import tudelft.healthpsychology.traumaontologies.answerstate.AnswerState;
30import tudelft.healthpsychology.traumaontologies.answerstate.AnswerStateExplanationDecorator;
31import tudelft.healthpsychology.traumaontologies.answerstate.AnswersBreathFirstState;
32import tudelft.healthpsychology.traumaontologies.answerstate.AnswersDepthFirstState;
33import tudelft.healthpsychology.traumaontologies.answerstate.ListOfAnswersState;
34import tudelft.healthpsychology.traumaontologies.answerstate.OntoPropAnswerState;
35import tudelft.healthpsychology.traumaontologies.answerstate.PropertiesAnswerState;
36import tudelft.healthpsychology.traumaontologies.owltree.OwlOntologyNode;
37import tudelft.healthpsychology.traumaontologies.owltree.OwlTree;
38import tudelft.healthpsychology.traumaontologies.owltree.OwlTreeReasoner;
39import tudelft.healthpsychology.traumaontologies.questiontypes.QuestionType;
40import tudelft.utilities.translator.FragmentCsvTranslator;
41import tudelft.utilities.translator.TranslationFailedException;
42
43@SuppressWarnings("serial")
44public class SimpleGUI extends JFrame {
45
46 private final JComboBox<String> languagesel = new JComboBox<String>(
47 new String[] { "EN", "NL" });
48 private JButton answerbutton;
49 private final JTextArea textArea = new JTextArea(5, 20);
50 private final JTextField answerArea = new JTextField();
51 private AnswerState dialogState;// tracks the user's answers
52 private FragmentCsvTranslator translator;
53 private final JButton savebutton = new JButton("'Save'");
54 private final ObjectMapper jackson = new ObjectMapper();
55 private final String[] owlfiles = new String[] { "Child Sexual Abusa.owl",
56 "War.owl", "War Afghanistan.owl", "War Bosnia.owl",
57 "War Libya.owl" };
58 JComboBox<String> treatmentCombo = new JComboBox<>(owlfiles);
59 private OwlTree tree;
60
61 public SimpleGUI() throws OWLOntologyCreationException {
62 setDefaultCloseOperation(EXIT_ON_CLOSE);
63
64 JPanel panel = new JPanel();
65 panel.setLayout(new BorderLayout());
66 panel.add(new JLabel("Select treatment"), BorderLayout.NORTH);
67 panel.add(treatmentCombo, BorderLayout.CENTER);
68
69 getContentPane().add(panel);
70
71 treatmentCombo.addActionListener(select -> initPanelContents());
72
73 pack();
74 setVisible(true);
75 }
76
77 /**
78 * The main interaction panel. This removes the initial question to select
79 * the treatment and start the treatment itself.
80 */
81 private void initPanelContents() {
82 getContentPane().removeAll();
83 setTranslator("EN");
84 try {
85 initAnswerState();
86 } catch (OWLOntologyCreationException e) {
87 e.printStackTrace();
88 return;
89 }
90 answerbutton = new JButton(tr("Antwoord"));
91 JPanel panel = new JPanel();
92 panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
93 panel.add(textArea);
94 textArea.setLineWrap(true);
95 textArea.setWrapStyleWord(true);
96 textArea.setEditable(false);
97 panel.add(answerArea);
98 panel.add(answerbutton);
99 panel.add(languagesel);
100 panel.add(savebutton);
101 getContentPane().add(panel);
102 pack();
103 updateQuestion();
104
105 answerbutton.addActionListener(action -> answer());
106 languagesel.addActionListener(action -> languageSelected());
107 savebutton.addActionListener(action -> save());
108
109 }
110
111 private void save() {
112 try {
113 JTextArea showtextarea = new JTextArea(
114 jackson.writeValueAsString(dialogState), 20, 80);
115 showtextarea.setLineWrap(true);
116 showtextarea.setEditable(false);
117 JScrollPane scrollPane1 = new JScrollPane(showtextarea);
118 JFrame saveframe = new JFrame(
119 "This data would be saved if this were no demo");
120 saveframe.getContentPane().add(scrollPane1, BorderLayout.CENTER);
121 saveframe.pack();
122 saveframe.setVisible(true);
123
124 } catch (HeadlessException | JsonProcessingException e) {
125 e.printStackTrace();
126 }
127 }
128
129 private void languageSelected() {
130 setTranslator((String) languagesel.getSelectedItem());
131 updateQuestion();
132 answerbutton.setText(tr("Antwoord"));
133 }
134
135 private void setTranslator(String lang) {
136 try {
137 translator = new FragmentCsvTranslator(
138 getClass().getResourceAsStream("/" + lang + ".csv"));
139 } catch (IOException e) {
140 e.printStackTrace();
141 }
142
143 }
144
145 public static void main(String[] args) throws OWLOntologyCreationException,
146 FileNotFoundException, IOException, TranslationFailedException {
147 new SimpleGUI();
148 }
149
150 /**
151 * This constructs the dialog process: start with asking the geographical
152 * location, then the location type, then the objects in depth first order,
153 * then the persons in breath first order. You can modify this as needed.
154 *
155 * @throws OWLOntologyCreationException
156 */
157 private void initAnswerState() throws OWLOntologyCreationException {
158 OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
159 OWLOntology ontology = manager.loadOntologyFromOntologyDocument(
160 getClass().getResourceAsStream(
161 "/" + treatmentCombo.getSelectedItem()));
162 OwlTreeReasoner reas = new OwlTreeReasoner(ontology);
163 tree = new OwlTree(reas);
164
165 AnswerState geoanswer = new PropertiesAnswerState(
166 "Geografische_locatie");
167 AnswerState loctypeanswer = new AnswerStateExplanationDecorator(
168 new OntoPropAnswerState(
169 new OwlOntologyNode("Type_Locatie", reas), null),
170 "In wat voor soort locatie was U?");
171 AnswerState objectsanswer = new AnswerStateExplanationDecorator(
172 new AnswersDepthFirstState("Object",
173 "Zijn er nog meer relevante objecten?", tree),
174 "We verzamelen de relevante objecten op de locatie. We gaan een voor een door alle relevante objecten, beginnend bij het eerste object.");
175 AnswerState personsanswer = new AnswerStateExplanationDecorator(
176 new AnswersBreathFirstState(
177 new OwlOntologyNode("Persoon", reas), "Naam",
178 "Zijn er nog meer relevante personen?"),
179 "We verzamelen de relevante personen op de locatie. We verzamelen eerst de namen.");
180
181 dialogState = new ListOfAnswersState(Arrays.asList(geoanswer,
182 loctypeanswer, objectsanswer, personsanswer));
183 }
184
185 /**
186 * Used to translate fixed general texts. It's a bug if the given text does
187 * not translate but we can't throw as we are in handlers usually. So we
188 * print stacktrace in that case.
189 *
190 * @param text text to be translated.
191 * @return translted text
192 */
193 private String tr(String text) {
194 try {
195 return translator.forward(text);
196 } catch (TranslationFailedException e) {
197 e.printStackTrace();
198 return "ERROR";
199 }
200 }
201
202 private void answer() {
203 try {
204 String answer = answerArea.getText();
205 if (answer.isEmpty()) {
206 JOptionPane.showMessageDialog(this,
207 tr("Je antwoord is leeg. Probeer nog eens."),
208 tr("Probeer nogmaals"), 1);
209 return;
210
211 }
212 QuestionType type = dialogState.getOptions(tree);
213 if (type.isTranslated()) {
214 try {
215 answer = translator.reverse(answer);
216 } catch (TranslationFailedException e) {
217 JOptionPane.showMessageDialog(this, tr("Je antwoord "
218 + answer
219 + " kan niet vertaald worden. Kan U uw spelling controleren."),
220 tr("Vertaling mislukt"), 1);
221 return;
222 }
223 }
224 if (!type.fits(answer)) {
225 JOptionPane.showMessageDialog(this, tr(
226 "Uw antwoord is geen goed antwoord op deze vraag. Kan U de vraag nogmaals beantwoorden."),
227 tr("Antwoord past niet bij vraag"), 1);
228 return;
229 }
230
231 dialogState = dialogState.with(answer, tree);
232 updateQuestion();
233 answerArea.setText("");
234 } catch (Exception e) {
235 e.printStackTrace();
236 }
237 }
238
239 private void updateQuestion() {
240 SwingUtilities.invokeLater(new Runnable() {
241
242 @Override
243 public void run() {
244 QuestionType opts = dialogState.getOptions(tree);
245 if (opts == null) {
246 // we're done!
247 JOptionPane.showMessageDialog(SimpleGUI.this,
248 tr("We zijn klaar!"), tr("klaar"), 1);
249 setVisible(false);
250 return;
251 }
252 try {
253 textArea.setText(translator.forwardFragments(
254 dialogState.getOptions(tree).getQuestion()));
255 } catch (TranslationFailedException e) {
256 e.printStackTrace();
257 }
258 }
259 });
260 }
261
262}
Note: See TracBrowser for help on using the repository browser.