Created
October 21, 2010 13:56
-
-
Save davidcoallier/638515 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var sys = require('sys'); | |
var couch = require('couchdb'); | |
var graph = require('graph/Graph'); | |
var vertex = require('graph/Vertex'); | |
var edge = require('graph/Edge'); | |
var matrix = require('graph/matrices/adjacency'); | |
var client = couch.createClient(5984, 'localhost'); | |
var viewUrl = '/graphs/_design/graph/_view/vertices'; | |
var Graph = new graph.Graph(); | |
var edges = {}; | |
client.request(viewUrl, function(er, data) { | |
if (er !== null) { | |
// Error the error for now. | |
return false; | |
} | |
// Let's build the vertices of the graph. | |
if (data.rows !== undefined && data.rows.length > 0) { | |
for (var i in data.rows) { | |
var currentVertex = data.rows[i].key; | |
Graph.addVertex(new vertex.Vertex(currentVertex, {})); | |
for (var e in data.rows[i].value) { | |
if (edges[currentVertex] === undefined) { | |
edges[currentVertex] = []; | |
} | |
edges[currentVertex].push(data.rows[i].value[e]); | |
} | |
} | |
} | |
for (var e in edges) { | |
for (var ei in edges[e]) { | |
Graph.addEdge(new edge.Edge(e, edges[e][ei], {})); | |
} | |
} | |
var vertices = Graph.getVertices(); | |
var adjacencyMatrix = new matrix.Adjacency(vertices); | |
sys.puts(sys.inspect(Matrix)); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment