                   [fact 100 :address  200           2]
 
                   [fact 101 :type     :person       2]
-                  [fact 101 :name     "bar"         2]
+                  [fact 101 :name     "bar1"        2]
                   [fact 101 :email    "bar@foo.com" 2]
-                  [fact 101 :address  200           2])]
+                  [fact 101 :address  200           2]
+                  [fact 101 :name     "bar2"        4]
+                  [fact 101 :name     "bar3"        3])]
 
-      (cldb/with-db facts1
+      #_(cldb/with-db facts1
         (cl/run* [q]
           (cl/fresh [pid a v]
             (fact pid :type :person (cl/lvar))

            (fact pid a v (cl/lvar))
            (cl/== q [pid a v]))))

      (to-entities facts1))))

;;; (cltest2)


(defn cltest2 []
  (cldb/db-rel fact e a v t)

  (letfn [(entities [db]
            (let [lres (cldb/with-db db
                         (cl/run* [q]
                           (cl/fresh [e a v t]
                             (fact e a v t)
                             (cl/== q [e a v t]))))]
              (when (not-empty lres)
                (reduce (fn [s [e a v t]]
                          (update-in s [e a]
                                     (fn [tmap]
                                       (if tmap
                                         (assoc tmap t v)
                                         (sorted-map-by #(* -1 (compare %1 %2)) t v)))))
                        {}
                        lres))))]

