package tudelft.mentalhealth.motivatepersisting; import java.io.IOException; import java.util.Collections; import java.util.LinkedList; import java.util.List; import java.util.Locale; import java.util.stream.Collectors; /** * a motivational answer for some situation. This answer contans random aspects * and is generated only once. Create a new object if you need a new answer. */ public class MotivationalAnswer { private final Situation situation; private final String text; private final ProbOfCategoriesPerSituation probabilities = ProbOfCategoriesPerSituation .instance(); private final List selectedStatements; private final Statements allStatements; /** * * @param situation the situation of the patient. * @param locale the language to use towards the patient. Currently * supported are Locale("nl","NL") and Locale("en",""). */ public MotivationalAnswer(Situation situation, Locale locale) { this.situation = situation; this.allStatements = new Statements(locale); this.selectedStatements = createStatements(); this.text = createText(selectedStatements); } /** * * @return the ext for this motivational answer. This text will remain * constant once the MotivationalAnswer has been created. Create a * new MotivationalAnswer to get a new text. */ public String getText() { return text; } @Override public String toString() { return " MotivationalAnswer(" + text + ")"; } /** * * @return the statements selected for this motivational answer. */ public List getStatements() { return Collections.unmodifiableList(selectedStatements); } /***************** internals ***************/ /** * @param stats the statements to be used for the text. * @return text generated. * @throws IOException */ private String createText(List stats) { String text = ""; for (StatementSelection statement : stats) { text = text + allStatements.getText(statement.getCategory(), statement.getSubcategory()) + " "; } return text; } private List createStatements() { long nstatements = Math .round(MeanNrStatements.instance().getMean(situation)); List selection = selectBasic(); while (selection.size() < nstatements) { List excludes = selection.stream() .map(sel -> sel.getCategory()).collect(Collectors.toList()); selection.add(randomSelection(excludes)); } return selection; } /** * Pick a random StatementSelection in a {@link Category} that is not yet * selected, * * @param excludes the {@link Categories} excluded of selection * @return random {@link StatementSelection}, excluding the excludes */ protected StatementSelection randomSelection(List excludes) { Category category = probabilities.getWeightedRandomCategory(excludes, situation); return new StatementSelection(category, probabilities.selectSubcategory(category, situation)); } /** * @return one Selection from each category that has p(ALL)>0.5 */ protected List selectBasic() { List selection = new LinkedList<>(); for (Category cat : Category.values()) { if (probabilities.get(cat, Subcategory.ALL, situation) > 0.5) { selection.add(new StatementSelection(cat, probabilities.selectSubcategory(cat, situation))); } } return selection; } }