1 | package tudelft.healthpsychology.traumaontologies.answerstate;
|
---|
2 |
|
---|
3 | import java.util.Collection;
|
---|
4 | import java.util.Collections;
|
---|
5 | import java.util.HashMap;
|
---|
6 | import java.util.Map;
|
---|
7 |
|
---|
8 | import com.fasterxml.jackson.annotation.JsonCreator;
|
---|
9 | import com.fasterxml.jackson.annotation.JsonProperty;
|
---|
10 |
|
---|
11 | import tudelft.healthpsychology.traumaontologies.questiontypes.TypedQuestion;
|
---|
12 | import tudelft.utilities.tree.Tree;
|
---|
13 |
|
---|
14 | /**
|
---|
15 | * Contains a answer to an ontology node: both the {@link OntologyAnswerState}
|
---|
16 | * and the {@link PropertiesAnswerState}. The answerstate can be partial, eg the
|
---|
17 | * OntologyAnswerState or PropertiesAnswerState may be not yet complete.
|
---|
18 | * <p>
|
---|
19 | * The node state has 2 main phases: 1. ontologyState is not final (has open
|
---|
20 | * options). then this is the next question to be asked. 2. ontologyState is
|
---|
21 | * final. Thus, he most accurate ontology node matching the user's idea is
|
---|
22 | * available. All questions pertinent to that node are asked.
|
---|
23 | * <p>
|
---|
24 | * Special substitutions are done on elements of the form [XXX] where XXX is
|
---|
25 | * some word string. If XXX is some property for which a value was already
|
---|
26 | * collected in the {@link #propertiesState}, then the [XXX] is replaced with
|
---|
27 | * that value.
|
---|
28 | */
|
---|
29 |
|
---|
30 | public class OntoPropAnswerState implements AnswerState {
|
---|
31 | private final OntologyAnswerState ontologyState;
|
---|
32 | private final PropertiesAnswerState propertiesState;
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * @param ontoState the state of the ontology question node.
|
---|
36 | * @param propState the OntologyProperties state. Can (but does not need to
|
---|
37 | * ) be null while ontoState has options (is not final)
|
---|
38 | */
|
---|
39 | @JsonCreator
|
---|
40 | public OntoPropAnswerState(
|
---|
41 | @JsonProperty("ontologyState") OntologyAnswerState ontoState,
|
---|
42 | @JsonProperty("propertiesState") PropertiesAnswerState propState) {
|
---|
43 | this.ontologyState = ontoState;
|
---|
44 | this.propertiesState = propState;
|
---|
45 | }
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * Convenience constructor that starts with a ontology node
|
---|
49 | *
|
---|
50 | * @param node the {@link OntologyNode} to start at.
|
---|
51 | * @pram nameOfObject a name of the object, to make the question to the user
|
---|
52 | * more specific which is especially relevant if this is part of a
|
---|
53 | * breath-first and the user entered some name or so some time before.
|
---|
54 | * If null, "het" ("it") is used.
|
---|
55 | */
|
---|
56 | public OntoPropAnswerState(OntologyNode node, String nameOfObject) {
|
---|
57 | this(new OntologyAnswerState(node, nameOfObject), null);
|
---|
58 | }
|
---|
59 |
|
---|
60 | /**
|
---|
61 | *
|
---|
62 | * @return current {@link OntologyAnswerState}
|
---|
63 | */
|
---|
64 | public OntologyAnswerState getOntologyAnswer() {
|
---|
65 | return ontologyState;
|
---|
66 | }
|
---|
67 |
|
---|
68 | /**
|
---|
69 | *
|
---|
70 | * @return current properties answer state, or null if the ontology answer
|
---|
71 | * has not been answered yet.
|
---|
72 | */
|
---|
73 | public PropertiesAnswerState getPropertyAnswer() {
|
---|
74 | return propertiesState;
|
---|
75 | }
|
---|
76 |
|
---|
77 | @Override
|
---|
78 | public TypedQuestion getOptions(
|
---|
79 | Tree<String, Collection<Property>, OntologyNode> tree) {
|
---|
80 | TypedQuestion options = ontologyState.getOptions(tree);
|
---|
81 | if (options == null) {
|
---|
82 | options = propertiesState.getOptions(tree);
|
---|
83 | }
|
---|
84 | if (options == null)
|
---|
85 | return null;
|
---|
86 | return options.substitute(getSubstitutions());
|
---|
87 | }
|
---|
88 |
|
---|
89 | @Override
|
---|
90 | public OntoPropAnswerState with(String answer,
|
---|
91 | Tree<String, Collection<Property>, OntologyNode> tree) {
|
---|
92 | TypedQuestion ontoOptions = ontologyState.getOptions(tree);
|
---|
93 | if (ontoOptions != null) {
|
---|
94 | if (!ontoOptions.fits(answer)) {
|
---|
95 | throw new IllegalArgumentException(
|
---|
96 | "answer " + answer + " does not fit " + ontoOptions);
|
---|
97 | }
|
---|
98 | OntologyAnswerState newOntoState = ontologyState.with(answer, tree);
|
---|
99 | PropertiesAnswerState newPropState = propertiesState;
|
---|
100 | if (newOntoState.getOptions(tree) == null) {
|
---|
101 | // onto is final, set up the prop state.
|
---|
102 | if (propertiesState == null) {
|
---|
103 | newPropState = new PropertiesAnswerState(
|
---|
104 | newOntoState.getNode());
|
---|
105 | } else {
|
---|
106 | // we already had prop answers, keep them.
|
---|
107 | newPropState = new PropertiesAnswerState(
|
---|
108 | newOntoState.getNode(),
|
---|
109 | propertiesState.getAnswers());
|
---|
110 | }
|
---|
111 | }
|
---|
112 | return new OntoPropAnswerState(newOntoState, newPropState);
|
---|
113 | }
|
---|
114 |
|
---|
115 | // if we get here, the ontostate is final and prop state has been set
|
---|
116 | TypedQuestion propOptions = propertiesState.getOptions(tree);
|
---|
117 | if (propOptions != null) {
|
---|
118 | if (!propOptions.fits(answer)) {
|
---|
119 | throw new IllegalArgumentException(
|
---|
120 | "answer " + answer + " does not fit " + propOptions);
|
---|
121 | }
|
---|
122 | return new OntoPropAnswerState(ontologyState,
|
---|
123 | propertiesState.with(answer, tree));
|
---|
124 | }
|
---|
125 | // both already final so this is a final state.
|
---|
126 | return this;
|
---|
127 | }
|
---|
128 |
|
---|
129 | /**
|
---|
130 | *
|
---|
131 | * @return substitution list based on the answers provided so far. For
|
---|
132 | * example if we collected the answer "Jaap" as an answer for a node
|
---|
133 | * labeled "Naam", we add a substitution "[Naam]"/"Jaap".
|
---|
134 | */
|
---|
135 | private Map<String, String> getSubstitutions() {
|
---|
136 | if (propertiesState == null)
|
---|
137 | return Collections.emptyMap();
|
---|
138 | Map<Property, String> props = propertiesState.getAnswers();
|
---|
139 | Map<String, String> substis = new HashMap<>();
|
---|
140 | for (Property prop : props.keySet()) {
|
---|
141 | substis.put("\\[" + prop.getQuestionType().getId() + "\\]",
|
---|
142 | props.get(prop));
|
---|
143 | }
|
---|
144 | return substis;
|
---|
145 | }
|
---|
146 |
|
---|
147 | }
|
---|