   (:require [aurora.btree :as btree]
             [aurora.join :as join])
   (:require-macros [aurora.macros :refer [apush set!!]]))
+
+;; FLOWS
+
+(deftype Flow [input-iter output-kinds output-names output-fields]
+  Object
+  (run [this kn]
+       (.reset input-iter)
+       (let [facts (btree/iterator->keys input-iter)]
+         (dotimes [i (alength output-kinds)]
+           (.add-facts kn (aget output-kinds i) (aget output-names i) (aget output-fields i) facts)))))
 
 ;; KNOWLEDGE
 

                                              key)
                                index (.get-or-create-index kn "know" name clause-keys)]
                            (join/magic-iterator index (into-array var-map))))))
              join-iter (join/join-iterator (into-array iterators))]

          ;; TODO outputs, ruleset
          )
    ))

(comment


       (.replace (js/uuid) (js/RegExp. "-" "gi") "_")
       (str "id-" (swap! next inc)))))

;; NOTE can't handle missing keys yet - requires a schema
(defn compile [kn]
  (let [clauses (btree/iterator->keys (btree/iterator (.get-or-create-index kn "know" "clauses" #js ["rule-id" "when|pretend|remember|forget" "clause-id" "name"])))
        fields (btree/iterator->keys (btree/iterator (.get-or-create-index kn "know" "clause-fields" #js ["clause-id" "constant|variable" "key" "val"])))
        rule-id->clauses (atom (into {} (for [[k vs] (group-by #(nth % 0) clauses)] [k (set (map vec vs))])))
        clause-id->fields (atom (into {} (for [[k vs] (group-by #(nth % 0) fields)] [k (set (map vec vs))])))]

