Revision: 1780
Author: [email protected]
Date: Thu Dec 10 17:11:20 2009
Log: Add selection question type (allow multiple selections)
http://code.google.com/p/simal/source/detail?r=1780
Added:
/trunk/uk.ac.osswatch.simal.ssmm/src/main/uk/ac/osswatch/simal/ssmm/model/SelectionQuestion.java
/trunk/uk.ac.osswatch.simal.ssmm/src/test/uk/ac/osswatch/simal/ssmm/model/TestSelectionQuestion.java
Modified:
/trunk/uk.ac.osswatch.simal.ssmm/src/main/uk/ac/osswatch/simal/ssmm/SustainabilityRating.java
/trunk/uk.ac.osswatch.simal.ssmm/src/main/uk/ac/osswatch/simal/ssmm/model/MultipleChoiceQuestion.java
/trunk/uk.ac.osswatch.simal.ssmm/src/main/uk/ac/osswatch/simal/ssmm/model/Question.java
=======================================
--- /dev/null
+++
/trunk/uk.ac.osswatch.simal.ssmm/src/main/uk/ac/osswatch/simal/ssmm/model/SelectionQuestion.java
Thu Dec 10 17:11:20 2009
@@ -0,0 +1,101 @@
+package uk.ac.osswatch.simal.ssmm.model;
+
+import java.util.Iterator;
+import java.util.LinkedHashMap;
+import java.util.StringTokenizer;
+
+/**
+ * A question that allows zero or more options to be selected from a range
of options.
+ *
+ */
+public class SelectionQuestion extends MultipleChoiceQuestion {
+
+ LinkedHashMap<String, String> selectedOptions = new
LinkedHashMap<String,
String>();
+
+ public SelectionQuestion(String label, String text, String details,
+ LinkedHashMap<String, String> options) {
+ super(label, text, details, options);
+ }
+
+ /**
+ * Set a single answer as selected. Previous answers that have been set
remain selected.
+ *
+ * @TODO throw an exception on an illegal option
+ */
+ @Override
+ public void setAnswer(int index) {
+ LinkedHashMap<String, String> selected = getSelectedOptions();
+ String key = (String)getOptions().keySet().toArray()[index];
+ String value = (String)getOptions().values().toArray()[index];
+ selected.put(key, value);
+ setSelectedOptions(selected);
+ }
+
+ /**
+ * Set an answer as unselected, all other selections remain selected.
+ * @param index
+ */
+ public void unsetAnswer(int index) {
+ super.setAnswer(index);
+ }
+
+ /**
+ * Get a comma separated list of keys for options that are selected.
+ */
+ @Override
+ public String getAnswer() {
+ if (getSelectedOptions().size() == 0) {
+ return "";
+ } else {
+ StringBuilder sb = new StringBuilder();
+ Iterator<String> itr =
getSelectedOptions().keySet().iterator();
+ while (itr.hasNext()) {
+ sb.append(itr.next());
+ if (itr.hasNext()) {
+ sb.append(", ");
+ }
+ }
+ return sb.toString();
+ }
+ }
+
+ /**
+ * Set all selected options using a comma separated list of options.
+ * If any of the items in the list are not keys for an option then they
+ * are silently ignored.
+ *
+ * Previously selected items will remain selected.
+ *
+ * @TODO throw an exception on an illegal option
+ */
+ @Override
+ public void setAnswer(String answer) {
+ StringTokenizer st = new StringTokenizer(answer, ",");
+ while (st.hasMoreTokens()) {
+ String key = st.nextToken();
+ LinkedHashMap<String, String> selected =
getSelectedOptions();
+ if (!selected.containsKey(key)) {
+ if (getOptions().containsKey(key)) {
+ selected.put(key, getOptions().get(key));
+ }
+ }
+ }
+ }
+
+ /**
+ * Get a linked map containing all selected options.
+ *
+ * @return
+ */
+ public LinkedHashMap<String, String> getSelectedOptions() {
+ return selectedOptions;
+ }
+
+ /**
+ * Set the selected options with a linked map containing all selections.
+ * @return
+ */
+ public void setSelectedOptions(LinkedHashMap<String, String> selected) {
+ selectedOptions = selected;
+ }
+}
=======================================
--- /dev/null
+++
/trunk/uk.ac.osswatch.simal.ssmm/src/test/uk/ac/osswatch/simal/ssmm/model/TestSelectionQuestion.java
Thu Dec 10 17:11:20 2009
@@ -0,0 +1,67 @@
+package uk.ac.osswatch.simal.ssmm.model;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import java.util.LinkedHashMap;
+import java.util.Iterator;
+
+public class TestSelectionQuestion {
+ static LinkedHashMap<String, String> options;
+ static SelectionQuestion question;
+
+ @BeforeClass
+ public static void createQuestion() {
+ options = new LinkedHashMap<String, String>();
+ options.put("One", "Option one");
+ options.put("Two", "Option two");
+ options.put("Three", "Option three");
+ options.put("Four", "Option four");
+
+ question = new SelectionQuestion(TestQuestion.QUESTION_LABEL,
TestQuestion.QUESTION_TEXT, TestQuestion.QUESTION_DETAILS, options);
+ }
+
+ @Test
+ public void testOptionsSize() {
+ assertEquals("There is an incorrect number of options",
options.size(),
question.getOptions().size());
+ }
+
+ @Test
+ public void testOptionsOrder() {
+ Iterator<String> itr =
question.getOptions().keySet().iterator();
+ String key = itr.next();
+ assertEquals("First key is incorrect", "One", key);
+ key = itr.next();
+ assertEquals("Two key is incorrect", "Two", key);
+ key = itr.next();
+ assertEquals("Three key is incorrect", "Three", key);
+ key = itr.next();
+ assertEquals("Four key is incorrect", "Four", key);
+ }
+
+ @Test
+ public void testAddRemoveOption() {
+ int originalSize = question.getOptions().size();
+ question.addOption("Five", "Option five");
+ assertEquals("There is an incorrect number of options after
adding an
option", originalSize + 1, question.getOptions().size());
+
+ question.removeOption("Five");
+ assertEquals("There is an incorrect number of options after
removing an
option", originalSize, question.getOptions().size());
+ }
+
+ @Test
+ public void testAnswer() {
+ question.setAnswer(1);
+ assertEquals("The answer is not correctly set when using an
integer
index", "Two", question.getAnswer());
+
+ question.setAnswer(2);
+ assertEquals("The answer is not correctly set when using a
second
integer index", "Two, Three", question.getAnswer());
+
+
+ question.setAnswer("One, Two");
+ assertEquals("The answer is not correctly set when using a
second
integer index", "Two, Three, One", question.getAnswer());
+ }
+
+}
=======================================
---
/trunk/uk.ac.osswatch.simal.ssmm/src/main/uk/ac/osswatch/simal/ssmm/SustainabilityRating.java
Thu Dec 10 14:36:43 2009
+++
/trunk/uk.ac.osswatch.simal.ssmm/src/main/uk/ac/osswatch/simal/ssmm/SustainabilityRating.java
Thu Dec 10 17:11:20 2009
@@ -6,12 +6,14 @@
import uk.ac.osswatch.simal.ssmm.model.MultipleChoiceQuestion;
import uk.ac.osswatch.simal.ssmm.model.Question;
+import uk.ac.osswatch.simal.ssmm.model.SelectionQuestion;
public class SustainabilityRating {
private static final Object HELP_COMMAND = "help";
private static LinkedHashMap<String, Question> infoQuestions = new
LinkedHashMap<String, Question>();
private static LinkedHashMap<String, Question> legalQuestions = new
LinkedHashMap<String, Question>();
+ private static LinkedHashMap<String, Question> knowledgeQuestions = new
LinkedHashMap<String, Question>();
/**
* @param args
*/
@@ -22,10 +24,12 @@
askInfoQuestions();
askLegalQuestions();
+ askKnowledgeQuestions();
System.out.println("\nResponse summary\n");
reportAll(infoQuestions);
reportAll(legalQuestions);
+ reportAll(knowledgeQuestions);
}
private static void askInfoQuestions() {
@@ -39,10 +43,31 @@
askAll(infoQuestions);
}
+
+ private static void askKnowledgeQuestions() {
+ System.out.println("Knowledge Information");
+ System.out.println("=====================");
+
+ LinkedHashMap<String, String> options = new
LinkedHashMap<String,
String>();
+ options.put("User Docs", "User documentation section on
website");
+ options.put("Design Docs", "Design documentation");
+ options.put("Roadmap", "Managed project roadmap");
+ options.put("Metadata", "Machine readable meta-data");
+ options.put("wiki", "Publicly writeable wiki");
+ options.put("Revision control", "Version control system");
+ options.put("Discussion", "Email lists or online forums");
+ options.put("IM", "Instant messaging/IRC");
+ options.put("Issue Tracker", "Issue tracker for bug and feature
tracking");
+
+ SelectionQuestion question = new SelectionQuestion("Communication
Channels", "Which publicly available communication or dissemination
mechanisms does the project use?", "Multiple documentation and
communication components are indicative of at least the opportunity for
project knowledge to exist. There are certainly cases where too many
avenues of knowledge can hurt a project.", options);
+ knowledgeQuestions.put(question.getLabel(), question);
+
+ askAll(knowledgeQuestions);
+ }
private static void askLegalQuestions() {
- System.out.println("General Information");
- System.out.println("===================");
+ System.out.println("Legal Information");
+ System.out.println("=================");
LinkedHashMap<String, String> options = new
LinkedHashMap<String,
String>();
options.put("Don't know", "The licence model is not currently
understood.");
@@ -97,13 +122,52 @@
System.out.println(question.getText());
String answer;
- if (question instanceof MultipleChoiceQuestion) {
+ if (question instanceof SelectionQuestion) {
+ answer = getSelectionResponse((SelectionQuestion)
question);
+ } else if (question instanceof MultipleChoiceQuestion) {
answer =
getMultiChoiceResponse((MultipleChoiceQuestion) question);
} else {
answer = getFreeFormresponse(question);
}
return question.getAnswer();
}
+
+ private static String getSelectionResponse(SelectionQuestion question) {
+ System.out.println("Enter a single option at a time, enter
'end' to
finish making selections");
+
+ Iterator<String> keys =
question.getOptions().keySet().iterator();
+ int idx = 0;
+ while (keys.hasNext()) {
+ String key = keys.next();
+ if (question.getSelectedOptions().containsKey(key)) {
+ System.out.print("SELECTED ");
+ }
+ System.out.print(idx);
+ System.out.print(": ");
+ System.out.print(key);
+ System.out.print(" (");
+ System.out.print(question.getOptions().get(key));
+ System.out.println(")");
+ idx = idx + 1;
+ }
+
+ Scanner in = new Scanner(System.in);
+ String response = in.nextLine();
+ if (response.equals("end")) {
+ return question.getAnswer();
+ }
+ if (response.equals(HELP_COMMAND)) {
+ System.out.println(question.getDetails());
+ return ask(question);
+ }
+ int intAnswer = new Integer(response);
+ if (intAnswer < 1 || intAnswer >= idx) {
+ System.out.println("Please enter a value between 0 and
" + (idx - 1));
+ return ask(question);
+ }
+ question.setAnswer(intAnswer);
+ return getSelectionResponse(question);
+ }
/**
* Get the answer to a multichoice question.
=======================================
---
/trunk/uk.ac.osswatch.simal.ssmm/src/main/uk/ac/osswatch/simal/ssmm/model/MultipleChoiceQuestion.java
Thu Dec 10 14:36:43 2009
+++
/trunk/uk.ac.osswatch.simal.ssmm/src/main/uk/ac/osswatch/simal/ssmm/model/MultipleChoiceQuestion.java
Thu Dec 10 17:11:20 2009
@@ -60,6 +60,8 @@
* Set the recorded answer according to an index pointer into the
LinkedhashMap of options.
*
* @param index 0 base index into the options
+ *
+ * @TODO throw an exception on an illegal option
*/
public void setAnswer(int index) {
setAnswer((String)getOptions().keySet().toArray()[index]);
=======================================
---
/trunk/uk.ac.osswatch.simal.ssmm/src/main/uk/ac/osswatch/simal/ssmm/model/Question.java
Thu Dec 10 05:22:44 2009
+++
/trunk/uk.ac.osswatch.simal.ssmm/src/main/uk/ac/osswatch/simal/ssmm/model/Question.java
Thu Dec 10 17:11:20 2009
@@ -79,6 +79,8 @@
/**
* Set the answer (if any) provided for this question.
+ *
+ * @TODO throw an exception on an illegal option
*/
public void setAnswer(String answer) {
this.answer = answer;
--
You received this message because you are subscribed to the Google Groups
"Simal Commits" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/simal-commits?hl=en.