                                                                                              #js ["output-transitive-connected" "variable" "x" "xx"]
                                                                                              #js ["output-transitive-connected" "variable" "y" "zz"]])
 
-(compile kn)
-
-(.run (first (compile kn)) kn)
-
-(.get-or-create-index kn "know" "connected" #js ["x" "y"])
-
-(def f2 (second (compile kn)))
-
-(.run f2 kn)
-
-(.get-or-create-index kn "know" "connected" #js ["x" "y"])
-
-(.run f2 kn)
-
-(.get-or-create-index kn "know" "connected" #js ["x" "y"])
-
-(.run f2 kn)
-
-(.get-or-create-index kn "know" "connected" #js ["x" "y"])
-
-(.add-facts kn "know" "clauses" #js ["rule-id" "when|know|remember|forget" "clause-id" "name"] #js [#js ["x-function-edge" "when" "get-function-edge" "edge"]
-                                                                                                    #js ["x-function-edge" "when" "make-str" "=function"]
-                                                                                                    #js ["x-function-edge" "know" "know-str" "str-edge"]])
+(.add-facts kn "know" "clauses" #js ["rule-id" "when|know|remember|forget" "clause-id" "name"] #js [#js ["function-edge" "when" "get-function-edge" "edge"]
+                                                                                                    #js ["function-edge" "when" "make-str" "=function"]
+                                                                                                    #js ["function-edge" "know" "know-str" "str-edge"]])
 
 (.add-facts kn "know" "clause-fields" #js ["clause-id" "constant|variable" "key" "val"] #js [#js ["get-function-edge" "variable" "x" "xx"]
                                                                                              #js ["get-function-edge" "variable" "y" "yy"]

                                                                                             #js ["make-str" "constant" "js" "\"edge \" + xx + \" \" + yy"]
                                                                                             #js ["know-str" "variable" "name" "zz"]])

(.run (nth (compile kn) 2) kn)

(.get-or-create-index kn "know" "str-edge" #js ["name"])


;; NOTE can't handle missing keys yet - requires a schema
(defn compile [kn]
  (let [rule-id->flow #js {}
        clauses (.keys (.get-or-create-index kn "know" "clauses" #js ["rule-id" "when|know|remember|forget" "clause-id" "name"]))
        fields (.keys (.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))])))]
