Skip to content

Instantly share code, notes, and snippets.

@sarmbruster
Created July 25, 2012 16:28
Show Gist options
  • Save sarmbruster/3177079 to your computer and use it in GitHub Desktop.
Save sarmbruster/3177079 to your computer and use it in GitHub Desktop.
neo4j poc
@Grab(group = 'org.neo4j', module='neo4j-community', version='1.8.M06')
import org.neo4j.kernel.EmbeddedGraphDatabase
import org.neo4j.cypher.ExecutionEngine
import org.neo4j.cypher.ExecutionResult
import org.neo4j.graphdb.Direction
import org.neo4j.graphdb.Node
import org.neo4j.tooling.GlobalGraphOperations
def dir = '/tmp/poc'
new File(dir).deleteDir(); // clean up directory from prev. runs
def graphDb = new EmbeddedGraphDatabase(dir)
def engine = new ExecutionEngine(graphDb)
engine.execute("""create
(p29500 { type:"portfolio", name: "port29500"}),
(p377200 { type:"portfolio", name: "port377200"}),
(p19400 { type:"portfolio", name: "port19400"}),
(i1 {name: 'i1', type:"instrument", price:100}),
(i2 {name: 'i2', type:"instrument", price:10}),
(i3 {name: 'i3', type:"derivedInstrument", price:5.5}),
(i4 {name: 'i4', type:"instrument", price:12}),
(i5 {name: 'i5', type:"derivedInstrument", price:89}),
(i6 {name: 'i6', type:"instrument", price:32}),
(i7 {name: 'i7', type:"derivedInstrument", price:0.5}),
(i8 {name: 'i8', type:"derivedInstrument", price:89}),
(i9 {name: 'i9', type:"instrument", price:1000}),
p29500-[:HAS {quantity: 5000, label:'p3'}]->i3,
p29500-[:HAS {quantity: 4000, label:'p7'}]->i7,
i3-[:CONTAINS]->p377200,
i7-[:CONTAINS]->p19400,
p377200-[:HAS {quantity: 200, label:'p1'}]->i1,
p377200-[:HAS {quantity: 100, label:'p4'}]->i4,
p377200-[:HAS {quantity: 300, label:'p5'}]->i5,
i5-[:CHILD_OF]->i8,
p19400-[:HAS {quantity: 100, label:'p2'}]->i1,
p19400-[:HAS {quantity: 200, label:'p6'}]->i6,
(index {type:'index', name:'index1304500'}),
index-[:HAS {quantity: 1000}]->i1,
index-[:HAS {quantity: 450}]->i2,
index-[:HAS {quantity: 1200}]->i9,
i8-[:REPRESENTS]->index""")
//GlobalGraphOperations.at(graphDb).allNodes.each {
// println "$it.id, ${it.getProperty("name", null)}"
//}
def resultByRecursion = calcValueOfSubTree(graphDb.getNodeById(1))
println "sum via recursion: ${resultByRecursion}"
ExecutionResult res = engine.execute("start n=node(1) match p=n-[:HAS|CONTAINS|CHILD_OF|REPRESENTS*]->instrument where instrument.type='instrument' return extract(r in relationships(p) : coalesce(r.quantity?,1)) as quantities, instrument.price, instrument.name")
def resultByCypher = res.javaIterator().sum() { ele ->
def result = ele["instrument.price"]
ele["quantities"].each { result *= it}
result
}
println "sum via cypher: $resultByCypher"
assert resultByRecursion == resultByCypher
calcWeights(graphDb.getNodeById(1))
calcWeights(graphDb.getNodeById(2))
calcWeights(graphDb.getNodeById(3))
graphDb.shutdown()
def calcValueOfSubTree(Node node) {
if (node.hasRelationship(Direction.OUTGOING)) {
node.getRelationships(Direction.OUTGOING).sum {
it.getProperty("quantity", 1) * calcValueOfSubTree(it.endNode)
}
} else {
node.getProperty("price", 0)
}
}
def calcWeights(Node node) {
def values = node.getRelationships(Direction.OUTGOING).collectEntries {
[it, it.getProperty("quantity") * calcValueOfSubTree(it.endNode)]
}
def sum = values.values().sum()
println "weights for ${node.getProperty('name')}"
values.each {
println " - weight for ${it.key.getProperty('label')}: ${it.value/sum}"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment