      -from below by open precondition of action on potential parent-var, or
      -from above by parent-var link to this var."
   [hierarchy s]
-  (first (filter (set (supported-vars hierarchy s)) (open-vars hierarchy s))))
+  (first (filter #(env/get-var s (free-var %)) 
+                 (concat (bottom-up-supported-vars hierarchy s)
+                         (top-down-supported-vars hierarchy s)))))
 
 (deftype FlatASPlanHLA [hierarchy name pc] :as this
   env/Action

                 [sas :as sas] [sas-analysis :as sas-analysis]]))



; For now, pretend analysis is free, just brute-force it. 

; Put stuff previous in HLAs into state, so we can keep track of, e.g.,
; actions added while accomplishing some other action. 
; For each var, a second var pointing to: pending action, :frozen, or :open
;  This doesn't play with usual state abstraction.
;  Instead, can have var-in-use, var-action, latter always nil if not in-use.
;    This allows us to abstract away action if we want.  
;    Annoying: we now have to have primitives for changing this extra state. 
;  Except, we also need to keep track of *user* of frozen far w/o action.



; Question: do we additionally need to keep token to guide refs?

; We have simple, dijkstra-shared-goal options, with same acylic options.

; Can refine until *particular* action resolved, or *any* current action resolved. 
;   (not just any action, steps are too small).


;; TODO: should we allow action to fire even when there is already action scheduled on precond ?

;; TODO: think about variable orderings more.  i.e., in infinite taxi, DAG order is perfect.


;; TODO: also look at using landmarks to structure search ? 
; (but where's the state abstraction?)

;;;;;;;;;;;;;;;;;;;;;;;;;;;; Types, Protocols, Setup ;;;;;;;;;;;;;;;;;;;;;;;;;;;


(defn goal-action [dtgs]
  (-> dtgs
      (get-in [sas/goal-var-name sas/goal-false-val sas/goal-true-val])
      util/safe-singleton))

(defn action-var [v] ['action v])
(defn parent-var [v] ['parent v])

(defn expand-initial-state 
  "Expand the initial state with lots more stuff.  Each var can have an active action
   (which changes only that var), OR a parent (in which case it must eventually be used
   to fire an active action on that var), OR neither (but not both), in which case it
   is open."
  [init goal-action]
  (let [vars (env/list-vars init)]
    (env/set-var 
     (env/set-vars init
                   (concat (for [var vars] [(action-var var) nil])
                           (for [var vars] [(parent-var var) nil])))
     (action-var sas/goal-var-name) goal-action)))

(defn make-add-action-action [{:keys [name precond-map effect-map reward] :as factored-primitive}]
  (let [[evar eval] (util/safe-singleton (seq effect-map))]
    (env/FactoredPrimitive
     [::Add name]
     {; (parent-var evar) nil  ;skip this for efficiency ...
      (action-var evar) nil evar (precond-map evar)}
     {(action-var evar) factored-primitive}
     reward)))

; Here, is question of if we *require* reservations or not.
 ; You *must* have them, or you may cancel other reservations, bye correctness.
(defn make-fire-action-action [{:keys [name precond-map effect-map reward] :as factored-primitive}]
  (let [[evar eval] (util/safe-singleton (seq effect-map))]
    (env/FactoredPrimitive

  [dtgs]
  (into {} (for [[var dtg] dtgs] [var (compute-var-possible-next-map var dtg)])))


  ;; TODO: this precludes state abstraction ? 
 (defn can-use-next? [hierarchy use-var use-val user-var user-val]
   (contains? (get-in (:possible-next-map hierarchy) [user-var user-val use-var]) use-val))

 ; Only need vars that could use current val *next* (with support from below, ignore for now).
 (defn possible-parents [hierarchy s effect-var]
   (let [eval (env/get-var s effect-var)]
     (filter
      (fn [p]
        (if-let [pa (env/get-var s (action-var p))]
          (condp = (get (:precond-map pa) effect-var)
            nil  (can-use-next? hierarchy effect-var eval p (get (:effect-map pa) p))
            eval true
            false)
          (can-use-next? hierarchy effect-var eval p (env/get-var s p))))
      ((util/safe-get hierarchy :child-var-map) effect-var)))))