Skip to content

Instantly share code, notes, and snippets.

@etosch

etosch/paths.clj

Last active Aug 29, 2015
Embed
What would you like to do?
Find paths through the survey
(defn get-dag
[blockList]
"Takes in a list of Blocks; returns a list of lists of Blocks."
(if (empty? blockList)
'(())
(let [^Block this-block (first blockList)]
(if-let [branchMap (and (.branchQ this-block) (.branchMap (.branchQ this-block)))]
(let [dests (set (vals branchMap))
blists (map (fn [^Block b] (drop-while #(not= % b) blockList)) (seq dests))]
(map #(flatten (cons this-block %)) (map get-dag blists))
)
(map #(flatten (cons this-block %)) (get-dag (rest blockList)))
)
)
)
)
(defn get-paths
"Returns paths through **blocks** in the survey. Top level randomized blocks are all listed last"
[^Survey survey]
(let [partitioned-blocks (Interpreter/partitionBlocks survey)
top-level-randomizable-blocks (Block/sort (.get partitioned-blocks true))
nonrandomizable-blocks (Block/sort (.get partitioned-blocks false))
dag (get-dag nonrandomizable-blocks)
]
(assert (coll? (first dag)) (type (first dag)))
(assert (= Block (type (ffirst dag))) (type (ffirst dag)))
(map #(concat % top-level-randomizable-blocks) dag)
)
)
(defn get-questions
"Returns possible questions answered when traversing this list of blocks"
[blockList]
(if (empty? blockList)
'()
(flatten (map (fn [^Block b]
(if (= (.branchParadigm b) Block$BranchParadigm/ALL)
(take 1 (shuffle (.questions b))) ;;all options should have the same arity
(concat (.questions b) (get-questions (.subBlocks b)))
)
)
blockList)
)
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment