Skip to content

Instantly share code, notes, and snippets.

@tadast
Forked from chrisroos/README.md
Last active August 29, 2015 14:25
Show Gist options
  • Save tadast/6d9534f698f2822e5e27 to your computer and use it in GitHub Desktop.
Save tadast/6d9534f698f2822e5e27 to your computer and use it in GitHub Desktop.
Spike into ways of visualising Smart Answers

The print_graph method displays the Smart Answer as a tree.

The print_questions_and_possible_next_nodes method displays the questions and their possible next nodes.

The extraction of possible next nodes is horrible in the case where the Smart Answer uses the block syntax of next_node. I'm getting the source of the block and extracting things that look like symbols.

flow = SmartAnswer::FlowRegistry.instance.find('register-a-birth')
# SmartAnswer::FlowRegistry.instance.flows.each do |flow|
# ignored_flows = [
# 'am-i-getting-minimum-wage', # shared logic breaks things
# 'inherits-someone-dies-without-will', # stack level too deep?
# 'minimum-wage-calculator-employers' # shared logic breaks things
# ]
# next if ignored_flows.include?(flow.name)
ORDERED_QUESTIONS = flow.questions.map(&:name)
ORDERED_OUTCOMES = flow.outcomes.map(&:name)
NODES_AND_NEXT_NODES = {}
flow.questions.each do |question_node|
possible_next_nodes = if question_node.next_node_function_chain.any?
question_node.next_node_function_chain.map(&:first)
else
# defined using next_node with a block
next_node_block_source = question_node.instance_variable_get(:@default_next_node_function).source
symbols_as_strings = next_node_block_source.scan(/\:[^ ]+/).map(&:strip)
# Naive search for symbols
symbols_as_strings.map do |symbol_as_string|
symbol_as_string.sub(/^:/, '')
end.map(&:to_sym)
end
NODES_AND_NEXT_NODES[question_node.name] = possible_next_nodes.sort.uniq
end
def print_graph(node_name, indent: 0)
left_margin = ' ' * indent
question_number = ORDERED_QUESTIONS.index(node_name) + 1
puts "#{left_margin}* Q#{question_number} #{node_name}"
NODES_AND_NEXT_NODES[node_name].each do |possible_next_node|
if NODES_AND_NEXT_NODES[possible_next_node]
print_graph(possible_next_node, indent: indent + 2)
else
outcome_number = ORDERED_OUTCOMES.index(possible_next_node) + 1
left_margin = ' ' * (indent + 2)
puts "#{left_margin}* A#{outcome_number} #{possible_next_node}"
end
end
end
def print_questions_and_possible_next_nodes
NODES_AND_NEXT_NODES.each.with_index do |(question_node, possible_next_nodes), index|
puts "* Q#{index+1} #{question_node}"
possible_next_nodes.each do |possible_next_node|
if NODES_AND_NEXT_NODES[possible_next_node]
puts " * #{possible_next_node}"
end
end
end
end
first_question = ORDERED_QUESTIONS.first
print_graph(first_question)
# print_questions_and_possible_next_nodes
# end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment