Permalink
Browse files

(pipe (route tg v out-e)) seems to work

  • Loading branch information...
1 parent 70d2303 commit 92d49d03ff5f01005015a1e06789e7245eac0c6d Darrick Wiebe committed Aug 26, 2012
Showing with 96 additions and 68 deletions.
  1. +44 −0 src/pacer.clj
  2. +42 −0 src/pacer/graph.clj
  3. +0 −68 src/pacer/pacer.clj
  4. +10 −0 src/pacer/step.clj
View
44 src/pacer.clj
@@ -0,0 +1,44 @@
+(ns pacer
+ (:import
+ (com.tinkerpop.blueprints.impls.tg TinkerGraph)
+ (com.tinkerpop.gremlin.pipes.transform InEdgesPipe))
+ (:use [clojure.pprint :only [pprint]]
+ pacer.step))
+
+(defn simple-encoder [] { :encode nil :decode nil })
+
+(defn show
+ "Show an easy to read"
+ [route]
+ (->> route
+ (map describe-step)
+ (clojure.string/join " -> ")))
+
+(defn- pipe-from-step [in step]
+ (cond
+ (:pipe step) (doto ((:pipe step) in)
+ (.setStarts (:pipe in (:source in))))
+ (:iterator step) ((:iterator step) (:pipe in (:source in)))
+ :else (throw (Exception. "Don't know how to build step"))))
+
+(defn build-pipe [[source & route]]
+ (if route
+ (reduce (fn [in step]
+ (check-step in step)
+ { :pipe (pipe-from-step in step)
+ :type (:type step (:type in))
+ :route (conj (:route in) step)})
+ { :source source, :type (:type source), :route [] }
+ route)
+ source))
+
+(defn pipe
+ "Build a pipe from a route definition"
+ [route]
+ (:pipe (build-pipe route)))
+
+(defn route [& steps]
+ (vec steps))
+
+(defmacro route [& steps]
+ (vec (map #(if (list? %) % (list %)) steps)))
View
42 src/pacer/graph.clj
@@ -0,0 +1,42 @@
+(ns pacer.graph
+ (:import
+ (com.tinkerpop.blueprints.impls.tg TinkerGraph)
+ (com.tinkerpop.gremlin.pipes.transform InEdgesPipe OutEdgesPipe))
+ (:use [clojure.pprint :only [pprint]]
+ pacer.step))
+
+(defn tg []
+ { :source true
+ :type :graph
+ :name "TinkerGraph"
+ :raw-graph (com.tinkerpop.blueprints.impls.tg.TinkerGraph.)
+ :encoder (pacer/simple-encoder)})
+
+(defn v []
+ { :source-type :graph
+ :type :vertex
+ :name "V"
+ :iterator (fn iterator [source]
+ (.. (:raw-graph source) getVertices iterator)) })
+
+(defn e []
+ { :source-type :graph
+ :type :edge
+ :name "E"
+ :iterator (fn iterator [source]
+ (.. (:raw-graph source) getEdges iterator)) })
+
+(defn out-e [& labels]
+ { :source-type :vertex
+ :type :edge
+ :name "OutE"
+ :pipe (fn pipe [in]
+ (OutEdgesPipe. (into-array String (map str labels)))) })
+
+(defn in-e [& labels]
+ { :source-type :vertex
+ :type :edge
+ :name "InE"
+ :pipe (fn pipe [in]
+ (InEdgesPipe. (into-array String (map str labels)))) })
+
View
68 src/pacer/pacer.clj
@@ -1,68 +0,0 @@
-(ns pacer
- (:import
- (com.tinkerpop.blueprints.impls.tg TinkerGraph))
- (:use [clojure.pprint :only [pprint]]))
-
-(defn simple-encoder [] { :encode nil :decode nil })
-(defn tg []
- { :source true
- :type :graph
- :name "TinkerGraph"
- :raw-graph (com.tinkerpop.blueprints.impls.tg.TinkerGraph.)
- :encoder (simple-encoder)})
-
-(defn describe-step [step]
- (:name step))
-
-(defn show
- "Show an easy to read"
- [route]
- (->> route
- (map describe-step)
- (clojure.string/join " -> ")))
-(defn v
- ([]
- [{ :source-type :graph
- :type :vertex
- :name "V"
- :iterator (fn v [source] (.. (:raw-graph source) getVertices iterator)) }])
- ([graph]
- (when (= :graph (:type graph))
- (conj [graph] (first (v))))))
-
-(defn e
- ([]
- [{ :source-type :graph
- :type :edge
- :name "E"
- :iterator (fn e [source] (.. (:raw-graph source) getEdges iterator)) }])
- ([graph]
- (when (= :graph (:type graph))
- (conj [graph] (first (e))))))
-
-(defn- check-step [in step]
- (when (not= (:source-type step) (:type in))
- (throw (Exception. (str (describe-step step) " expects type "
- (:source-type step) " but got " (:type in))))))
-
-(defn- pipe-from-step [in step]
- (cond
- (:pipe step) (doto (:pipe step)
- (.setStarts (:pipe in (:source in))))
- (:iterator step) ((:iterator step) (:pipe in (:source in)))
- :else (throw (Exception. "Don't know how to build step"))))
-
-(defn pipe
- "Build a pipe from a route definition"
- [[source & route]]
- (if route
- (reduce (fn [in step]
- (check-step in step)
- { :pipe (pipe-from-step in step)
- :type (:type step (:type in))
- :route (conj (:route in) step)})
- { :source source, :type (:type source), :route [] }
- route)
- source))
-
-
View
10 src/pacer/step.clj
@@ -0,0 +1,10 @@
+(ns pacer.step)
+
+(defn describe-step [step]
+ (:name step))
+
+(defn check-step [in step]
+ (when (not= (:source-type step) (:type in))
+ (throw (Exception. (str (describe-step step) " expects type "
+ (:source-type step) " but got " (:type in))))))
+

0 comments on commit 92d49d0

Please sign in to comment.