 
 ;;;;;;;;;;;;;;;;;;;;;;;;;;      Tree Summarizers      ;;;;;;;;;;;;;;;;;;;;;;;;;;
 
-(declare sp-ts get-stub-output)
-
-;; Does not need to watch stub, that happens automatically to ensure ordering.
+(declare sp-ts get-stub-output norm-name)
+
 ;; TODO: now this will never be true; how to avoid extra connections ?
-
-(defprotocol TreeSummarizer (ts-stub [ts]))
+(defprotocol TreeSummarizer
+  (ts-stub [ts])
+  (subsuming-tss [ts]) ; True tree summarizers subsuming this one.
+  (add-subsuming-ts! [ts subsuming-ts]))  
+  
 
 (defn- make-tree-summarizer [stb]
-  (let [ret (traits/reify-traits [summaries/or-summarizable summaries/simple-cached-node]
-              TreeSummarizer (ts-stub [ts] stb))]
+  (let [subsuming-tss-list (ArrayList.)
+        ret (traits/reify-traits [summaries/or-summarizable summaries/simple-cached-node]
+              TreeSummarizer
+              (ts-stub [ts] stb)
+              (subsuming-tss [ts] (seq subsuming-tss-list))
+              (add-subsuming-ts! [ts subsuming-ts]
+                (util/assert-is (= (norm-name (stub-name stb)) (norm-name (stub-name (ts-stub subsuming-ts)))))                 
+                (.add subsuming-tss-list subsuming-ts)
+                (summaries/connect-subsumed! subsuming-ts ts)))]
     (when-not (get-stub-output stb)
       (summaries/connect! ret stb)
       (summaries/connect-subsumed! ret stb))


;;  (except ensuring consistency if we're asserting it...)
;; When we create a stub, we also get: tree summarizer, subsumption-thing.
;; (subsumption-children are parent tree-summarizers).
;; Subsumption-thing is subsumption-child of stub, summarizer, tree-summarizer.
;; Tree-summarizers also remember best bound, this gives us consistency
;; How do they pass to children, though?
;; Apparent cycle -- tree summarizer has children that are child tree-summarizers,
;; And, these children should have the parent TS as a subsumption child.
;; Key: this is only to preserve info from original SP, subsumption..
;; This info should go into subsumption-thing.
;; Subsumption-thing is just ordinary max over children, + self, + TS?
;; ->>(We rely on eager updates to make sure it gets updated?)

;; TDB should not actually go into TS, children should be directly bounded?
;; All children, including inner SP, should do it fine.
;; This means that every thing has exactly one subsumption parent ... which is nice.
;; How does consistency get approached ?
;; We just have the cycle, but would be great if we didn't have to go in child --> ts --> st --> children
;; cycle every time things to up -- maybe ST just doesn't update ord. parents.
;; TODO: figure this out later. 
;; TODO: tests ! 

;; Can goall the way down to nearest sum, I guess...
;; Subsumer gets attached to subsumer of any child (or inner child?)

;; What do we do about multiple ways to express a given plan ? ? ? ?
;; Can normalize or not, interesting question... start without. 

;; TODO: things are propagating in the wrong order !
;; Act SP gets added as output of Act stub.
;; This is watched by both Top SP and Act TS.
;; Top SP gets it first.
;; We need to make sure we present consistent picture.
;; This means putting add-stub-output in TS ?

;; TODO: need to wrap seqs too ?

;; TODO: remove refs to stubs in TS so dead weight can be GC'd?

;; TODO first: propagate subsumption links.
;; TODO second: add pessimistic variant. (primitives shared!)

;; TODO: bounding of pessimistic descriptions ? (assume consistency for now).

(set! *warn-on-reflection* true)


   [(simple-subproblem
     stub out-set terminal? ;; Note ni may have different context.
     (fn [s ni]
       (if (= ni (input-set stub))
         stub
         (let [subsumed-stub (ri-fn s ni)]
           (add-subsuming-ts! (tree-summarizer subsumed-stub) (tree-summarizer stub))
           subsumed-stub))))]
   summaries/Summarizable (summarize [s] (summary-fn s))))


