Last active
August 29, 2015 14:01
-
-
Save etosch/a3d8f3329251152f64fd to your computer and use it in GitHub Desktop.
Find paths through the survey
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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