   "DSL form to create a new graph"
   (let [g (->
          (dbquery (str "create (g:`__Graph` {uid: apoc.create.uuid()}) return g"))
-         first second first first :data :uid)]
+         first :g :uid list)]
     (set-graph! g)
     g))
 

                  " RETURN _g "
                  (if (not (empty? nodesToReturnStr)) (str "," nodesToReturnStr))
                  (if (not (empty? edgesToReturnStr)) (str "," edgesToReturnStr)))]
   (->> (dbtquery qstr)
        (map (fn [row] (map (fn [col]
                              (assoc (select-keys (second col) [:data :metadata])
                                     :handle (first col)))
                            row))))))



(defn nodeRem->cypher [n]
  (let [id   (-> n (:metadata) (:id))
        handle (-> n (:handle))]
    (str  " MATCH(" handle ") WHERE ID( " handle ")=" id)))

(defn derive [g n pars redex]
  (let [r ((rules) n)
        nodesToRematch (filter (fn [x] (some #(= "__Node" %) (-> x :metadata :labels))) redex)
        nodesToRematchStr (apply str (map nodeRem->cypher nodesToRematch))
        edgesToRematch (filter (fn [x] (some #(= "__Edge" %) (-> x :metadata :labels))) redex)
        edgesToRematchStr (apply str (map nodeRem->cypher edgesToRematch))
        itemsDeleteStr (apply str (interpose " WITH * " (map delitem->cypher (:delete r))))
        nodesToRead (filter-elem 'node (-> r :read second :els))
        nodesToReadStr2 (apply str (interpose " WITH * " (map readnode->cypher2 nodesToRead)))
        nodesToCreate (filter-elem 'node (-> r :create second :els))
        nodesToCreateStr (apply str (interpose " WITH * " (map createnode->cypher nodesToCreate)))
        edgesToCreate (filter-elem 'edge (-> r :create second :els))
        edgesToCreateStr (apply str (interpose " WITH * " (map createedge->cypher edgesToCreate)))
        qstr (str "MATCH (_g:`__Graph` {uid:\"" g "\"}) "
                  nodesToRematchStr
                  " WITH * " edgesToRematchStr
                  " CREATE (_gn:`__Graph`{uid: apoc.create.uuid()})-[:prov{rule:\"" n "\"}]->(_g) "
                  nodesToCreateStr
                  " WITH * "
                  nodesToReadStr2
                  " WITH * "
                  edgesToCreateStr
                  " WITH * "
                  itemsDeleteStr
                  " RETURN _gn")]
    (let [g (->
             (dbquery qstr)
             first second first first :data :uid)]
      (when (not (empty? g)) (set-graph! g))
      g)))


(defn exec [g n par]
  (let [redexes (search g n par)]
    (if (empty? redexes)
      nil
      (derive g n par (first redexes)))))


(defn viewproc [g]
  "view the graph process"
  (let [p (dbquery (str "match (g1:`__Graph`)-[:prov*0..]-(:`__Graph`{uid:\"" g "\"}) OPTIONAL match (g2)-[e:prov]->(g1:`__Graph`) return g1,e,g2"))
        nodes (map step->dot p)
        nodeStr (apply str (interpose " \n " nodes))
        edges (map prov->dot p)
