Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Updated project.clj to 0.3.0 to denote next release. neo4j.clj - some…

… formatting changes, added new functions for finding nodes by their properties.
  • Loading branch information...
commit 9baf47159d7afe22cde5a7ae6a8d3ec9545e3673 1 parent 1285089
authored June 19, 2010

Showing 2 changed files with 68 additions and 10 deletions. Show diff stats Hide diff stats

  1. 2  project.clj
  2. 76  src/neo4j.clj
2  project.clj
... ...
@@ -1,4 +1,4 @@
1  
-(defproject clojure-neo4j "0.2.0" 
  1
+(defproject clojure-neo4j "0.3.0" 
2 2
   :description "The clojure-neo4j project provides a more lispy interface to Neo4j, a graph-structured on-disk transactional database." 
3 3
   :dependencies [[org.clojure/clojure "1.1.0-master-SNAPSHOT"] 
4 4
                 [org.clojure/clojure-contrib "1.0-SNAPSHOT"]
76  src/neo4j.clj
@@ -42,6 +42,7 @@
42 42
     (proxy [StopEvaluator] []
43 43
       (isStopNode [#^TraversalPosition pos]
44 44
                   (== (.depth pos) d)))))
  45
+
45 46
 (def end-of-graph StopEvaluator/END_OF_GRAPH)
46 47
 
47 48
 (def all ReturnableEvaluator/ALL)
@@ -88,7 +89,20 @@
88 89
 (defn relate [#^Node from #^Keyword type #^Node to]
89 90
   (.createRelationshipTo from to (relationship type)))
90 91
 
  92
+(defn new-child-node
  93
+  "Creates a node that is a child of the specified parent node along
  94
+  the specified relationship.  props is a map that defines the
  95
+  properties of the node."
  96
+  [parentnode relkey props]
  97
+  (let [node (new-node)]
  98
+    (relate parentnode relkey node)
  99
+    (properties node props)
  100
+    node))
  101
+
91 102
 (defn return-if [f]
  103
+  "Creates a ReturnableEvaluator for use with a traverser. Takes a
  104
+  function of one argument. The function will be passed the current
  105
+  position to act on."
92 106
   (proxy [ReturnableEvaluator] []
93 107
     (isReturnableNode [#^TraversalPosition p] (f p))))
94 108
 
@@ -123,20 +137,64 @@
123 137
 
124 138
 (defn traverse
125 139
   "Traverse the graph.  Starting at the given node, traverse the graph
126  
-in either bread-first or depth-first order, stopping when the stop-fn returns
127  
-true.  The filter-fn should return true for any node reached during the traversal
128  
-that is to be returned in the sequence.  The map of relationships and directions
129  
-is used to decide which edges to traverse."
130  
-  [#^Node start-node order stop-evaluator return-evaluator relationship-direction]
  140
+  in either bread-first or depth-first order, stopping when the
  141
+  stop-fn returns true.  The filter-fn should return true for any node
  142
+  reached during the traversal that is to be returned in the sequence.
  143
+  The map of relationships and directions is used to decide which
  144
+  edges to traverse."
  145
+  [#^Node start-node order stop-evaluator return-evaluator
  146
+   relationship-direction]
131 147
   (.getAllNodes (.traverse start-node 
132 148
                            order
133 149
                            stop-evaluator
134 150
                            return-evaluator
135  
-                           (into-array Object (mapcat identity (map (fn [[k v]] 
136  
-                                                                      [(relationship k) v]) 
137  
-                                                                    relationship-direction))))))
  151
+                           (into-array
  152
+			    Object
  153
+			    (mapcat identity (map (fn [[k v]] 
  154
+						    [(relationship k) v]) 
  155
+						  relationship-direction))))))
  156
+
138 157
 (defn lookup
139 158
   "Inside a transation, looks up nodes by index key for the given index."
140 159
   [idx & ids]
141 160
   (doall (map (fn [k] (.getSingleNodeFor idx k)) 
142  
-              ids)))
  161
+              ids)))
  162
+
  163
+
  164
+(defn new-props-evaluator
  165
+  "Creates a ReturnableEvaluator for use with a traverser that returns
  166
+  nodes that match the specified property values. propmap is a map that
  167
+  defines key value pairs that should ReturnableEvaluator should match
  168
+  on."
  169
+  [propmap]
  170
+  
  171
+  (return-if
  172
+   (fn [currentPos]
  173
+     (let [node (.currentNode currentPos)
  174
+	   nodeprops (properties node)
  175
+	   ks (keys propmap)
  176
+	   nks (count ks)]
  177
+       (cond
  178
+	(== nks 0) false
  179
+	(== nks 1) (and (contains? nodeprops (first ks))
  180
+			(= (get nodeprops (first ks))
  181
+			   (get propmap (first ks))))
  182
+	:else 
  183
+	(let [propcomp (fn [key] (= (get nodeprops key) (get propmap key)))]
  184
+	  (and (reduce #(and (contains? nodeprops %1)
  185
+			     (contains? nodeprops %2))
  186
+		       ks)
  187
+	       (reduce #(and (propcomp %1) (propcomp %2)) ks))))))))
  188
+
  189
+
  190
+(defn find-by-props
  191
+  "Finds a node by traversing outward from start-node along
  192
+  relationship relkey.  The function looks for nodes with property
  193
+  values matching the key value pairs in propmap."
  194
+  [start-node relkey propmap]
  195
+  (traverse start-node
  196
+	      breadth-first
  197
+	      end-of-graph
  198
+	      (new-props-evaluator propmap)
  199
+	      {relkey outgoing}))
  200
+

0 notes on commit 9baf471

Please sign in to comment.
Something went wrong with that request. Please try again.