 ;; create 10,000 whales and insert them into model
 (insert-n
  whales
- (let [num-whales 1000
+ (let [num-whales 10
        time (System/currentTimeMillis)
        #^Creator creator (.getCreator whale-metadata)
        max-length-x-100 (* max-length 100)

 org.dada.demo.whales
 (:use [org.dada.core])
 (:import [clojure.lang Symbol])
 (:import [org.dada.core
	   Average
	   Creator

;; demonstrate reduction
;;----------------------------------------

(let [metadata (class-metadata "org.dada.core.Sum" Object :key :version
			       [:key String :version Integer :sum Number])
      getter (.getAttributeGetter (.getMetadata heavier-whales-length) "length")
      accessor (fn [value] (.get getter value))
      new-value #(accessor (.getNewValue %))
      old-value #(accessor (.getOldValue %))]
  (def sum
       (proxy
	[Reducer$Strategy]
	[]
	(initialValue [] 0M)
	(initialType [type] type)
	(currentValue [key version value]
		      (println "currentValue: " key version value)
		      (.create metadata [key version value]))
	(reduce [insertions updates deletions]
		(-
		 (+
		  (reduce #(+ %1 (new-value %2)) 0 insertions)
		  (reduce #(+ %1 (- (new-value %2) (old-value %2))) 0 updates))
		 (reduce #(+ %1 (old-value %2)) 0 deletions))
		)
	(apply [currentValue delta] 
	       (println "apply: " currentValue  delta)
	       (+ currentValue delta))
	))

  (defn make-reducer [#^Model model #^Keyword key-key #^Reducer$Strategy strategy]
    (let [key (name key-key)]
      (Reducer. (str (.getName model) ".sum(" key ")") metadata key strategy)))
  )

(defn do-reduce [#^Model model #^Keyword key #^Reducer$Strategy reduction]
  (connect model (make-reducer model key reduction)))

;; sum, average, mean, mode, COUNT, minimum, maximum,...
(def heavier-whales-length-sum (do-reduce heavier-whales-length :length sum))
(insert *metamodel* heavier-whales-length-sum)

;;----------------------------------------
;; select - being refactored into filter, transform, aggregate, group, ...
