source: TraumaOntologies/src/main/java/tudelft/healthpsychology/traumaontologies/answerstate/AnswersDepthFirstState.java@ 5

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

Intermediate update

File size: 4.2 KB
Line 
1package tudelft.healthpsychology.traumaontologies.answerstate;
2
3import java.util.Arrays;
4import java.util.Collection;
5import java.util.LinkedList;
6import java.util.List;
7
8import com.fasterxml.jackson.annotation.JsonCreator;
9import com.fasterxml.jackson.annotation.JsonProperty;
10
11import tudelft.healthpsychology.traumaontologies.questiontypes.Bool;
12import tudelft.healthpsychology.traumaontologies.questiontypes.TypedQuestion;
13import tudelft.utilities.tree.Tree;
14
15/**
16 * {@link #node} points to a type of object of which there may be multiple
17 * relevant instances , eg "Persoon". This keeps track of asking the user all
18 * details about all instances.
19 *
20 * Contains the state of a set of answers for a given OntologyNode, handling
21 * depth first: first handle the Properties of a selected node, then deal with
22 * the next object of given node class.
23 *
24 */
25public class AnswersDepthFirstState implements AnswerState {
26
27 private final String nodelabel; // root node that this answer is about
28 private final List<OntoPropAnswerState> answerstates;
29 private final boolean isFinal;
30
31 private final Bool more; // eg "you have more items?"
32
33 /**
34 * Construct new state with one open OntologyAnswerState
35 *
36 * @param node the node that this answer is about.
37 * @param havemore the message to show to ask user if he has more items.
38 */
39 public AnswersDepthFirstState(String node, String havemore,
40 Tree<String, Collection<Property>, OntologyNode> tree) {
41 this(node,
42 Arrays.asList(new OntoPropAnswerState(
43 new OntologyAnswerState(tree.get(node), null), null)),
44 false, new Bool(havemore, "internal"));
45 }
46
47 /**
48 * Construct new state with a list of OntologyAnswerState, the last being
49 * possibly nonfinal If the last is final and askedForMore=true
50 *
51 * @param node the node label that this answer is about.
52 * @param answerstates the list of answer states. Must contain at least 1
53 * answerstate.
54 * @param askedMore true iff the state is final, no more new objects to
55 * be added and all answerstates are complete.
56 * @param fin true iff the state is final
57 * @param havemore the Bool question to show to ask user if he has more
58 * items.
59 */
60 @JsonCreator
61 public AnswersDepthFirstState(@JsonProperty("nodelabel") String node,
62 @JsonProperty("answerstates") List<OntoPropAnswerState> answerstates,
63 @JsonProperty("isFinal") boolean fin,
64 @JsonProperty("more") Bool havemore) {
65 this.nodelabel = node;
66 this.answerstates = answerstates;
67 this.isFinal = fin;
68 this.more = havemore;
69 }
70
71 @Override
72 public TypedQuestion getOptions(
73 Tree<String, Collection<Property>, OntologyNode> tree) {
74 OntoPropAnswerState lastState = answerstates
75 .get(answerstates.size() - 1);
76 TypedQuestion options = lastState.getOptions(tree);
77 if (options != null) {
78 return options;
79 }
80
81 if (!isFinal) {
82 return more;
83 }
84
85 return null;
86 }
87
88 @Override
89 public AnswersDepthFirstState with(String answer,
90 Tree<String, Collection<Property>, OntologyNode> tree) {
91 if (isFinal)
92 return this;
93 TypedQuestion opts = getOptions(tree);
94 if (!opts.fits(answer)) {
95 throw new IllegalArgumentException(
96 "Answer " + answer + " does not fit " + opts);
97 }
98 // check what the question was
99 OntoPropAnswerState lastState = answerstates
100 .get(answerstates.size() - 1);
101 TypedQuestion options = lastState.getOptions(tree);
102 if (options != null) {
103 // last question was about OntoProp
104 List<OntoPropAnswerState> newStates = new LinkedList<>(
105 answerstates);
106 newStates.set(newStates.size() - 1, lastState.with(answer, tree));
107 return new AnswersDepthFirstState(nodelabel, newStates, false,
108 more);
109 }
110
111 // current onto question is complete. So we asked boolean "more"
112 // question. We set explanation to "" as we already gave explanation the
113 // first time.
114 if (Bool.matchesYes(answer)) {
115 List<OntoPropAnswerState> newStates = new LinkedList<>(
116 answerstates);
117 newStates.add(new OntoPropAnswerState(
118 new OntologyAnswerState(tree.get(nodelabel), null), null));
119 return new AnswersDepthFirstState(nodelabel, newStates, false,
120 more);
121 }
122
123 // we're done. Set finished to true.
124 return new AnswersDepthFirstState(nodelabel, answerstates, true, more);
125 }
126
127}
Note: See TracBrowser for help on using the repository browser.