 
         ;; Map of queries to their nodes in the network.
         query-to-nodes (into {} (for [[query-name query-node] query-nodes]
-                                  [(:query query-node) query-node])) 
+                                  [(:query query-node) query-node]))
 
         ;; Map of rules to their nodes in the network.
         rule-to-nodes (into {} (for [rule-node production-nodes]

            [schema.core :as s]
            [schema.macros :as sm]))

(defn- get-condition-matches 
  "Returns facts matching each condition"
  [beta-roots memory]
  ;; TODO: add inspection of query, test, and negation nodes?
  (let [join-nodes (for [beta-root beta-roots
                         beta-node (tree-seq :children :children beta-root)
                         :when (= :join (:node-type beta-node))]
                     beta-node)]
    (reduce 
     (fn [matches node]
       (update-in matches
                  [(:condition node)]

                            [rule (mem/get-tokens-all memory rule-node)]))

     :query-matches (into {}
                          (for [[query query-node] query-to-nodes]
                            [query (mem/get-tokens-all memory query-node)]))

     :condition-matches (get-condition-matches beta-tree memory)
     }))

(defn explain-token
  "Prints a human-readable explanation of the facts and conditions that created the Rete token."
  ([token] (explain-token token ""))
  ([token prefix]
      (doseq [[fact {:keys [type constraints]}] (:matches token) ]
        (println prefix fact)
        (println prefix "  is a" type)
        (println prefix "  where" constraints))))

(defn explain-activations
  "Prints a human-friend explanation of why rules and queries matched in the given session."
  [session]
  (doseq [[rule tokens] (:rule-matches (inspect session))]
    (println "rule"  (or (:name rule) (str "<" (:lhs rule) ">")))
    (println "  executed")
    (println "   " (:rhs rule))
    (doseq [token tokens]
      (println "  because")
      (explain-token token "    "))
    (println))

  (doseq [[rule tokens] (:query-matches (inspect session))]
    (println "query"  (or (:name rule) (str "<" (:lhs rule) ">")))
    (doseq [token tokens]
      (println "  qualified because")
      (explain-token token "    "))
    (println)))
