      (into precond-map 
            (cons [(action-var evar) factored-primitive]
                  (for [[pvar pval] (dissoc precond-map evar)]
-                   [(parent-var pvar) evar])))
+                   [(parent-var pvar evar) true])))
      (into effect-map 
            (cons [(action-var evar) nil]
-                 (for [[pvar pval] precond-map]
-                   [(parent-var pvar) nil])))
+                 (apply concat
+                  (for [[pvar pval] (dissoc precond-map evar)]
+                    [[(parent-var pvar evar) false]
+                     [(free-var evar) true]]))))
      0)))
 
-(defn make-set-parent-var-action [p-var new-parent]
+(defn make-set-parent-var-action [p-var c-var]
   (env/FactoredPrimitive 
-   [::SetParent p-var new-parent] 
-   {(parent-var p-var) nil} 
-   {(parent-var p-var) new-parent} 
+   [::SetParent p-var c-var] 
+   {(free-var p-var) true} 
+   {(free-var p-var) false (parent-var p-var c-var) true} 
    0))
 
-(declare ActivateVarHLA)
-
-(defn make-greedy-fire-plan
-  "Return an HLA to greedily fire, or nil if cannot greedily fire."
-  [hierarchy s effect-var]
-  (let [a  (->> effect-var action-var (env/get-var s))
-        _  (assert a)
-        pm (:precond-map a)
-        parent-map (env/get-vars s (map parent-var (keys (dissoc pm effect-var))))
-        [set-parents unset-parents] (util/separate parent-map (keys parent-map))]
-    (when (and (env/state-matches-map? s pm)
-               (every? #(= (parent-map %) effect-var) set-parents)
-               (every? #(nil? (env/get-var s (action-var (second %)))) unset-parents));; TODO: ugly
-      (let [fire (make-fire-action-action a)]
-        [(hierarchy/SimpleHLA [::GreedyFire ] (env/precondition-context fire s)
-          [(conj (vec (for [p unset-parents] (make-set-parent-var-action (second p) effect-var))) fire)])
-         (ActivateVarHLA hierarchy effect-var)]))))
-
-
-(defn active-action-vars [hierarchy s]
-  (filter #(env/get-var s (action-var %)) 
-          (util/safe-get hierarchy :vars)))
-
-(defn open-vars [hierarchy s]
-  (remove #(or (env/get-var s (action-var %)) (env/get-var s (parent-var %))) 
-          (util/safe-get hierarchy :vars)))
-
-(defn supported-vars [hierarchy s]
-  (mapcat (fn [v] (if-let [a (env/get-var s (action-var v))] 
-                    (for [[var val] (:precond-map a)
-                          :when (not (= (env/get-var s var) val))]
-                      var)
-                    (if-let [p (env/get-var s (parent-var v))] [p])))
-          (util/safe-get hierarchy :vars)))
-
-
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Flat Hierarchy HLAs ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
+
+; TODO: ideally, we should avoid cyclies in the value of effect-var,
+ ; NOT in whole state-abstracted states.
+
+;; TODO: add a way to stop, when we reach the right var val. 
+
+;; TODO: make sure top-down setup is right, e.g., we always add a target 
+; action, then we can work on the precondition
+
+;; TODO: is there an invariant that whenever a var is "truly active" it 
+; has an active var, or is at right val?  If so, we need to enforce this as 
+; we activate a top-down var, since it'll appear dead.  
+
+
+; Simplest way to express mechanics:
+;  If any action can greedily fire,
+;    fire it.
+;  Else, if any vars without actions but with explicit child with action and wrong value,
+;    add a directed action.
+;  Else, if any vars without actions or explicit children but have child with action,
+;    activate precondition
+;  Else, if any var without action but with explicit parent, 
+;    add an undirected action.
+
+;  Schematics:
+;  a => A  goes to  A => A  or a -> a
+;  a -> A  goes to  a => A
+;  a => a  goes to  a => A
+;  A => a  is not allowed ? 
+
+
+
+(defn current-child [hierarchy s p-var]
+  (some #(env/get-var s (parent-var p-var %))
+        (util/safe-get-in hierarchy [:child-var-map p-var])))
+
+(defn possible-next-actions-to [hierarchy var c-val d-val]
+  (apply concat 
+    (map (util/safe-get-in hierarchy [:dtgs var c-val])
+         ((:acyclic-edges-fn hierarchy) var c-val d-val))))
+
+(defn possible-next-actions [hierarchy var c-val]
+  (apply concat (vals (util/safe-get-in hierarchy [:dtgs var c-val]))))
+
+; Add any applicable action, using the target val if available.
+; If not, goal is to ease deadlock by using a parent.
+; TODO: use parent commitments to decide where to head ?
+
+; Ideally, we stop as soon as we've used up at least one parent.
+; One idea is just to stop after each action.
+; This means we must take care when doing top-down; activation may
+; not be required. 
+(comment 
 (deftype AddSomeActionHLA [hierarchy effect-var]
   env/Action
    (action-name [] [::AddSomeAction effect-var])
    (primitive?  [] false)
   env/ContextualAction 
-   (precondition-context [s]
-     (assert (not (or (env/get-var s (parent-var effect-var)) (env/get-var s (action-var effect-var)))))
-     #{effect-var})
+   (precondition-context [s] 
+     (let [cc (util/make-safe (current-child hierarchy s effect-var))]
+       #{effect-var (parent-var effect-var cc) (action-var cc)}))
   hierarchy/HighLevelAction
    (immediate-refinements- [s] 
-     (assert (not (or (env/get-var s (parent-var effect-var)) (env/get-var s (action-var effect-var)))))
-     (for [[eval actions] (util/safe-get-in hierarchy [:dtgs effect-var (env/get-var s effect-var)])
-           a actions]
-       [(make-add-action-action a)]))
+     (assert (not (env/get-var s (action-var effect-var))))
+     (let [cc    (util/make-safe (current-child hierarchy s effect-var))
+           c-val (env/get-var s effect-var)]
+       (if-let [ca (env/get-var s (action-var cc))] ;; driven bottom-up?
+         (let [d-val (util/safe-get-in ca :precond-map effect-var)]
+           (if (= c-val d-val)
+               [[]]) 
+             (for [a (possible-next-action-to hierarchy effect-var c-val d-val)]
+               [(make-add-action-action a)]))
+         (for [a (possible-next-action hierarchy effect-var c-val)]
+           [(make-add-action-action a)]))))
    (cycle-level-           [s] nil))
-
-
-;; 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 :parent-var-map) effect-var))))
-
-;; TODO: should not add actinos if only current val can be needed.
-
+ ;)
+
+; Choose a parent for a var; assumes any greedy vars have already
+; fired, so all parents are allowed.  Prereq for adding actions on a var.
 (deftype ActivateVarHLA [hierarchy effect-var]
   env/Action
-   (action-name [] [::PostFire effect-var])
+   (action-name [] [::ActivateVar effect-var])
    (primitive?  [] false)
   env/ContextualAction 
-   (precondition-context [s] 
-    (set (cons effect-var 
-               (apply concat 
-                 (for [v ((util/safe-get hierarchy :parent-var-map) effect-var)]
-                   [v (action-var v)])))))
+   (precondition-context [s] #{})
   hierarchy/HighLevelAction
    (immediate-refinements- [s]
-     (if (= effect-var sas/goal-var-name) [[]]
-      (cons [(AddSomeActionHLA hierarchy effect-var)]
-            (for [p (possible-parents hierarchy s effect-var)]
-              [(make-set-parent-var-action effect-var p)]))))
+     (assert (env/get-var s (free-var effect-var)))
+     (let [asa (AddSomeActionHLA hierarchy effect-var)]
+      (for [c (util/safe-get-in hierarchy [:child-var-map effect-var])]
+        [(make-set-parent-var-action effect-var c) asa])))
    (cycle-level-           [s] nil))
 
-; Note: ideally, when we add actions we care about support.  
- ; From below --> potentail for acyclic actions, only to desired val.
- ; From above --> also need support from below, in that
-  ; can use current val of child *first* by action on this var, driven by bottom-up.
-   ; (prunes action choice, can be recognized early to prune whole state.)
+
+;; TODO: assert is wrong.?
+
+(defn make-greedy-fire-plan
+  "Return an HLA to greedily fire, or nil if cannot greedily fire."
+  [hierarchy s effect-var]
+  (let [a            (->> effect-var action-var (env/get-var s))
+        _            (assert a)
+        pm           (:precond-map a)
+        precond-vars (keys (dissoc pm effect-var))
+        [free-pv unfree-pv] (util/separate #(env/get-var s (free-var %)) precond-vars)]
+    (when (and (env/state-matches-map? s pm)
+               (every? #(env/get-var s (parent-var % effect-var)) unfree-pv))
+      (assert (every? #(nil? (env/get-var s (action-var %))) precond-vars))
+      (let [fire (make-fire-action-action a)]
+        [(hierarchy/SimpleHLA [::GreedyFire ] 
+          (env/precondition-context fire s)
+          [(conj (vec (for [p free-pv] (make-set-parent-var-action p effect-var))) 
+                 fire
+                 (AddSomeActionHLA hierarchy effect-var))])]))))
+
+(defn make-a-greedy-fire-plan [hierarchy s]
+  (some #(make-greedy-fire-plan hierarchy s %) 
+        (filter #(env/get-var s (action-var %))
+                (util/safe-get hierarchy :vars))))
+
+
+(defn bottom-up-supported-vars [hierarchy s]
+  (mapcat (fn [v] 
+            (when-let [a (env/get-var s (action-var v))] 
+              (for [[var val] (:precond-map a)
+                    :when (not (= (env/get-var s var) val))]
+                var)))
+          (util/safe-get hierarchy :vars)))
+
+(defn top-down-supported-vars [hierarchy s]
+  (for [vp (util/safe-get hierarchy :vars)
+        vc (util/safe-get-in hierarchy [:child-var-map vp])
+        :when (env/get-var s (parent-var vp vc))]
+    vc))
 
 (defn choose-open-var 
   "Choose a var to start changing.  Var must be open, i.e. parent-var and action-var both nil.

   (precondition-context [s] pc)
  hierarchy/HighLevelAction
   (immediate-refinements- [s] 
     (if-let [actives (seq (active-action-vars hierarchy s))]
       (if-let [greedy (some #(make-greedy-fire-plan hierarchy s %) actives)]
         [(conj (vec greedy) this)]
         (when-let [open (choose-open-var hierarchy s)]
;  Note; this can cause suboptimalify if someone might want current val.           
;          [[(AddSomeActionHLA hierarchy open) this]]))

           [[(ActivateVarHLA hierarchy open) this]]))
       [[]]))
   (cycle-level-           [s] nil))

(defn make-flat-asplan-hla [hierarchy full-context]
  (FlatASPlanHLA hierarchy [::FlatASPlan] full-context))



;; TODO: cutoff when top-down and bottom-up meet, don't match ? 

;; TODO: note, should be able to use acylic dtgs for, e.g., single parent. 

;; TODO: Take advantage of "greedy-chain" condition, don't assign
;   val to parent too high up the chain too early. 


; Possible-next-map maps from var, val, to var2, to set of vals for var2 that 
; can be used next by an action that changes var, starting at val.


  [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)))))