Skip to content

Commit

Permalink
..
Browse files Browse the repository at this point in the history
  • Loading branch information
leshy committed Mar 4, 2012
1 parent 72db354 commit 513aa1f
Showing 1 changed file with 108 additions and 0 deletions.
108 changes: 108 additions & 0 deletions index.js
@@ -0,0 +1,108 @@
var xmlrpc = require('xmlrpc');
var async = require('async');
var _ = require("underscore");

// Creates an XML-RPC client. Passes the host information on where to
// make the XML-RPC calls.
var client = xmlrpc.createClient({ host: 'localhost', port: 20738, path: '/RPC2'});

function cb(callback,error,value) {
if (callback) { callback(error,value); }
}

function ubi(method,args,callback) {
if (!args) { args = []; }
client.methodCall('ubigraph.' + method, args, function (error, value) {
// console.log('>> ' + method + ' ' + JSON.stringify(args) + ' : ',error,value);
if (callback) { callback(error,value); }
});
}


function vertexstyle(callback) {
ubi('new_vertex_style',[0],function(error,styleid) {
async.series([
function(cb) { ubi('set_vertex_style_attribute',[0,'size','1.0'],cb); },
function(cb) { ubi('set_vertex_style_attribute',[0,'fontcolor','#809c21'],cb); },
function(cb) { ubi('set_vertex_style_attribute',[0,'fontfamily','Fixed'],cb); },
function(cb) { ubi('set_vertex_style_attribute',[0,'color','#405c71'],cb); }
], function(err,bla) {
callback(null,true);
});
});
}


function edgestyle(callback) {
ubi('new_edge_style',[0],function(error,edgeid) {
edgeid = 0;
ubi('set_edge_style_attribute',[edgeid,'color','#445544']);
// ubi('set_edge_style_attribute',[edgeid,'spline','true']);
ubi('set_edge_style_attribute',[edgeid,'arrow','true']);
ubi('set_edge_style_attribute',[edgeid,'arrow_radius','1.5']);
ubi('set_edge_style_attribute',[edgeid,'arrow_length','2.0']);
ubi('set_edge_style_attribute',[edgeid,'strength','0.1']);
ubi('set_edge_style_attribute',[edgeid,'fontfamily','Fixed'],function() { cb(callback,null,edgeid); });
});
}


function addvertex(label,callback) {
console.log("adding vertex",label);
ubi('new_vertex', undefined, function(err,vertexid) {
ubi('change_vertex_style',[vertexid,0], function() {
ubi('set_vertex_attribute',[vertexid,'label',label], function() { callback(null,vertexid) }); });
});
}


function addedge(node1,node2,callback) {
console.log('connecting',node1.name(),'->',node2.name());
ubi('new_edge', [node2.vertexid, node1.vertexid],function(err,edgeid) {
ubi('change_edge_style',[edgeid,0],callback);
});
}



// adds vertexes for nodes
function addnodes(nodes,callback) {
var nodefunctions = _.map(nodes, function(node) {
return function(callback) { addvertex(node.name,function(err,id) { node.vertexid = id; callback(err,id); }); };
});

async.parallel(nodefunctions,callback);
}

function breadthf_addnodes(node,callback) {

addvertex(node.name(),function(err,id) {
node.vertexid = id;

var nodefunctions = _.map(node.getchildren(), function(child) {
return function(callback) {
breadthf_addnodes(child,function() {
addedge(node,child,callback);
});
}
});
async.parallel(nodefunctions,callback);
});
}


// requires only that each node has name property and parents() call
exports.visualise = function(node,callback) {
async.series([

// clear graph
function(callback) { ubi('clear',undefined,callback); },

// parallelly define vertex & edge styles
function(callback) { async.parallel([vertexstyle,edgestyle], callback); },

// add nodes
function(callback) { breadthf_addnodes(node,callback) }
], callback );
}

0 comments on commit 513aa1f

Please sign in to comment.