Skip to content

Commit

Permalink
Add helper function to ignore hidden questions
Browse files Browse the repository at this point in the history
  • Loading branch information
sharon2719 committed Jan 14, 2025
1 parent 2fa4a9a commit b234512
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ public class QuestionnaireResponseGeneratorCommand implements Runnable {
defaultValue = ".")
private String outputFilePath;

@CommandLine.Option(
names = {"-ihq", "--ignore-hidden-questions"},
description = "Ignore hidden questions when generating responses",
defaultValue = "true")
private boolean ignoreHiddenQuestions;

private static final Random random = new Random();
private static final Faker faker = new Faker();

Expand All @@ -99,7 +105,8 @@ public void run() {
fhir_base_url,
apiKey,
aiModel,
maxTokens);
maxTokens,
ignoreHiddenQuestions);
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand Down Expand Up @@ -127,7 +134,8 @@ public static void generateResponse(
String fhir_base_url,
String apiKey,
String aiModel,
String maxTokens)
String maxTokens,
boolean ignoreHiddenQuestions)
throws IOException {
long start = System.currentTimeMillis();

Expand All @@ -141,7 +149,7 @@ public static void generateResponse(

String questionnaireResponseString =
(Objects.equals(mode, "populate"))
? populateMode(questionnaireData, fhir_base_url, extrasPath)
? populateMode(questionnaireData, fhir_base_url, extrasPath, ignoreHiddenQuestions)
: aiMode(questionnaireData, apiKey, aiModel, maxTokens);

// Write response to file
Expand Down Expand Up @@ -320,9 +328,15 @@ static JSONObject generateAnswer(
}
}

static JSONArray getAnswers(JSONArray questions, JSONArray responses, JSONObject extras) {
static JSONArray getAnswers(
JSONArray questions, JSONArray responses, JSONObject extras, boolean ignoreHiddenQuestions) {
for (int i = 0; i < questions.length(); i++) {
JSONObject current_question = questions.getJSONObject(i);

if (ignoreHiddenQuestions && isHiddenQuestion(current_question)) {
continue;
}

String question_type = current_question.getString("type");
String link_id = current_question.getString("linkId");

Expand All @@ -334,15 +348,38 @@ static JSONArray getAnswers(JSONArray questions, JSONArray responses, JSONObject
if (current_question.has("item")) {
JSONArray group_questions = current_question.getJSONArray("item");
JSONArray group_responses = responses.getJSONObject(i).getJSONArray("item");
getAnswers(group_questions, group_responses, extras);
getAnswers(group_questions, group_responses, extras, ignoreHiddenQuestions);
}
}
responses.getJSONObject(i).put("answer", answer_arr);
}
return responses;
}

static String populateMode(String questionnaireData, String fhir_base_url, String extrasPath)
private static boolean isHiddenQuestion(JSONObject question) {
boolean isHidden = true;

if (question.has("extension")) {
JSONArray extensions = question.getJSONArray("extension");
for (int i = 0; i < extensions.length(); i++) {
JSONObject extension = extensions.getJSONObject(i);
if (extension
.getString("url")
.equals("http://hl7.org/fhir/StructureDefinition/questionnaire-hidden")) {
isHidden = extension.optBoolean("valueBoolean", true);
break;
}
}
}

return isHidden;
}

static String populateMode(
String questionnaireData,
String fhir_base_url,
String extrasPath,
boolean ignoreHiddenQuestions)
throws IOException {
JSONObject resource = new JSONObject(questionnaireData);
String questionnaire_id = resource.getString("id");
Expand Down Expand Up @@ -376,7 +413,7 @@ static String populateMode(String questionnaireData, String fhir_base_url, Strin
String populate_endpoint =
String.join("/", fhir_base_url, resourceType, questionnaire_id, "$populate");
List<String> result = HttpClient.postRequest(params.toString(), populate_endpoint, null);

System.out.println("Hidden questions" + ignoreHiddenQuestions);
JSONObject questionnaire_response = new JSONObject(result.get(1));
FctUtils.printError("Debug: response from questionnaireResponse: " + questionnaire_response);

Expand All @@ -386,7 +423,8 @@ static String populateMode(String questionnaireData, String fhir_base_url, Strin
if (questionnaire_response.has("item")) {
JSONArray response = (JSONArray) questionnaire_response.get("item");
JSONArray questions = resource.getJSONArray("item");
JSONArray response_with_answers = getAnswers(questions, response, extras);
JSONArray response_with_answers =
getAnswers(questions, response, extras, ignoreHiddenQuestions);
questionnaire_response.put("item", response_with_answers);
}
return String.valueOf(questionnaire_response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ void validateStructureMap(String inputFilePath, String structureMapFilePath, boo
"http://localhost:8080/fhir",
"",
"",
"");
"",
true);

// Extract Resources using the StructureMap and the generated QuestionnaireResponse
String generatedQuestionnaireResponsePath =
Expand Down

0 comments on commit b234512

Please sign in to comment.