 		       pivot-key
 		       src-metamodel
 		       (fn [tgt-metamodel src-model & extra-values]
+			   (println "PIVOT DATA:" extra-values)
 			   (connect src-model tgt-model)))]
     (insert *metamodel* tgt-model)
-    (insert tgt-metamodel [tgt-model])
-    [tgt-metamodel src-metadata extra-keys]))
+    (insert tgt-metamodel [tgt-model '()])
+    [tgt-metamodel src-metadata (str prefix ".PIVOT()") extra-keys]))
 
 ;;--------------------------------------------------------------------------------
 
 (def all-whales (metamodel whales-model))
 (def counted-whales (ccount all-whales))
 
-(def #^NavigableSet years
-     (TreeSet.
-      (collection
-       (Date. 0 0 1)
-       (Date. 1 0 1)
-       (Date. 2 0 1)
-       (Date. 3 0 1)
-       (Date. 4 0 1)
-       (Date. 5 0 1)
-       (Date. 6 0 1)
-       (Date. 7 0 1)
-       (Date. 8 0 1)
-       (Date. 9 0 1))))
+(def #^NavigableSet years (TreeSet. (apply collection (map #(Date. % 0 1) (range num-years)))))
 
 (def whales-by-type
-     (union
+     ;;(union
       (split
        all-whales
        :type
        nil
        #(pivot
-	  (ccount
-	   (split
-	    %
-	    :time
-	    (fn [time] (list (or (.lower years time) time)))
-	    ))
+	 (ccount
+	  (split
+	   %
+	   :time
+	   (fn [time] (list (or (.lower years time) time)))
+	   )
+	  )
 	 :time
 	 years
 	 (keyword (count-value-key nil)))
        )
-      "HEHE"))
+;;      "HEHE")
+)
 
 ;;(def counted-whales-by-type (ccount whales-by-type))
 ;;(def grouped-counted-whales-by-type (union counted-whales-by-type "Whales.split(:type).count()"))

			     count-key
			     extra-values)]
	    (insert *metamodel* count-model)
	    (insert tgt-metamodel (list* count-model extra-values))
	    (connect src-model count-model))))
     tgt-metadata
     extra-keys]))


(defn union [[#^Model src-metamodel #^Metadata src-metadata #^Collection extra-keys] & [#^String prefix]]
  (let [tgt-model (model (str (or prefix (name (gensym "UNION"))) ".union()") nil src-metadata)
	tgt-metamodel (meta-view ".union()" src-metamodel (fn [tgt-metamodel src-model & extra-values] (connect src-model tgt-model)))]
    (insert *metamodel* tgt-model)
    (insert tgt-metamodel [tgt-model])
    [tgt-metamodel src-metadata extra-keys]))

(defn pivot-metadata [#^Metadata src-metadata #^Collection keys #^Collection pivot-values value-key]
  (let [value-type (.getAttributeType src-metadata value-key)]

;;----------------------------------------------------------------------------------------------------------------------

(def types [
	    "blue" "grey" "sei"
	    ;; "blue whale"
	    ;; "sei whale"
	    ;; "bottlenose dolphin"
	    ;; "killer whale"
	    ;; "false killer whale"
	    ;; "amazon river dolphin"
	    ;; "narwhal"
	    ;; "sperm whale"
	    ;; "pilot whale"
	    ;; "beluga whale"
	    ;; "humpback whale"
	    ;; "fin whale"
	    ;; "right whale"
	    ;; "bowhead whale"
	    ;; "brydes whale"
	    ;; "minke whale"
	    ;; "gray whale"
	    ;; "spinner dolphin"
	    ;; "melon headed whale"
	    ;; "beaked whale"
	    ])

(def num-years 3)

(def max-weight 172) ;; metric tons
(def max-length 32.9) ;; metres

(defn rnd [seq] (nth seq (rand-int (count seq))))

(if true
  (def *metamodel*
       (new org.dada.core.MetaModelImpl
	    "Cetacea"
