1
0
Fork 0

Add some graph generators and a CSV_exporter.

This commit is contained in:
Max Neunhoeffer 2015-04-25 12:09:56 -07:00 committed by Michael Hackstein
parent 84f1ae1659
commit cbd61804df
5 changed files with 227 additions and 0 deletions

View File

@ -0,0 +1,126 @@
// This is a generic CSV exporter for collections.
//
// Usage: Run with arangosh like this:
// arangosh --javascript.execute <CollName> [ <Field1> <Field2> ... ]
//
// This will take all documents in the collection with name <CollName>
// and export a CSV file with a row for each document. Fields are
// specified by either simply giving their name, or in the form
// <name>:<somestring>
// in which case the column header is the whole string but only the
// attribute with name <name> will be taken. This is in particular
// intended for data for neo4j, where there are type attributes after
// the colon. If the first letter of <name> is a "!", then it does
// not refer to an attribute name and the constant value of the string
// after the "!" is put into the corresponding column.
// If no fields are specified, then the tool guesses fields by
// inspecting the collection and a random document. It does know
// about edge collections in that case and does "something sensible"
// for export to neo4j in that case.
function format(x) {
if (typeof(x) === "string") {
return '"'+x+'"';
}
else if (typeof(x) === "object") {
return '"'+JSON.stringify(x)+'"';
}
else if (typeof(x) === "undefined") {
return "undefined";
}
else {
return x.toString();
}
}
function type(x) {
if (typeof(x) === "string") {
return "string";
}
else if (typeof(x) === "number") {
return "double";
}
else if (typeof(x) === "boolean") {
return "boolean";
}
else {
return "string";
}
}
function CSVExport (collName, fields, names) {
var l = "";
for (i = 0; i < fields.length; i++) {
if (i > 0) {
l += ",";
}
l += names[i];
}
print(l);
var q = db._query('FOR d IN '+collName+' RETURN d');
var n;
while (q.hasNext()) {
n = q.next();
if (fields[0][0] === "!") {
l = format(fields[0].substr(1));
}
else {
l = format(n[fields[0]]);
}
for (i = 1; i < fields.length; i++) {
if (fields[i][0] === "!") {
l += "," + fields[i].substr(1);
}
else {
l += "," + format(n[fields[i]]);
}
}
print(l);
}
}
var collName = ARGUMENTS[0];
var fields = [];
var names = [];
var i;
if (ARGUMENTS.length > 1) {
for (i = 1; i < ARGUMENTS.length; i++) {
var f = ARGUMENTS[i];
var pos = f.indexOf(":");
if (pos < 0) {
fields.push(f);
names.push(f);
}
else {
names.push(f);
fields.push(f.substr(0, pos));
}
}
}
else { // try to be clever:
var c = db._collection(collName);
var d = c.any();
var itsFields = Object.keys(d);
itsFields = itsFields.filter(function(s) { return s[0] !== "_"; });
if (c.type() === 2) { // document-collection
fields.push("_id");
names.push("_id:ID");
}
else { // edge-collection
fields.push("_from");
names.push("_from:START_ID");
fields.push("_to");
names.push("_to:END_ID");
fields.push("!EDGE");
names.push(":TYPE");
}
for (i = 0; i < itsFields.length; i++) {
fields.push(itsFields[i]);
names.push(itsFields[i]+":"+type(d[itsFields[i]]));
}
}
CSVExport(collName, fields, names);

View File

@ -0,0 +1,15 @@
function makeCircle(Vname, Ename, prefix, N) {
var db = require("internal").db;
db._drop(Vname);
db._drop(Ename);
var V = db._create(Vname);
var E = db._createEdgeCollection(Ename);
var i;
for (i = 1; i <= N; i++) {
V.insert({_key: prefix + i, nr: i});
}
for (i = 1; i < N; i++) {
E.insert(Vname+"/"+prefix+i, Vname+"/"+prefix+(i+1), {_key:prefix+i});
}
E.insert(Vname+"/"+prefix+N, Vname+"/"+prefix+1, {_key: prefix+N});
}

View File

@ -0,0 +1,32 @@
function makeName (x, y) {
return "X"+x+"Y"+y;
}
function makeGrid (Vname, Ename, n) {
var db = require("internal").db;
db._drop(Vname);
db._drop(Ename);
var V = db._create(Vname);
var E = db._createEdgeCollection(Ename);
var x, y;
for (x = 0; x < n; x++) {
for (y = 0; y < n; y++) {
V.insert({x:x, y:y, _key: makeName(x, y)});
}
}
// The horizontal edges:
for (y = 0; y < n; y++) {
for (x = 0; x < n-1; x++) {
E.insert(Vname+"/"+makeName(x,y), Vname+"/"+makeName(x+1,y),
{_key: "H"+makeName(x,y)});
}
}
// The vertical edges:
for (x = 0; x < n; x++) {
for (y = 0; y < n-1; y++) {
E.insert(Vname+"/"+makeName(x,y), Vname+"/"+makeName(x,y+1),
{_key: "V"+makeName(x,y)});
}
}
}

View File

@ -0,0 +1,18 @@
function makeRandomGraph (Vname, Ename, nV, nE) {
var db = require("internal").db;
db._drop(Vname);
db._drop(Ename);
var V = db._create(Vname);
var E = db._createEdgeCollection(Ename);
var x, y;
var i;
for (i = 0; i < nV; i++) {
V.insert({_key: "X"+i});
}
for (i = 0;i < nE; i++) {
x = Math.floor(Math.random() * nV);
y = Math.floor(Math.random() * nV);
E.insert(Vname+"/X"+x, Vname+"/X"+y, {_key: "Y"+i});
}
}

View File

@ -0,0 +1,36 @@
function makeTree(Vname, Ename, prefix, N, rev) {
var db = require("internal").db;
var V = db._collection(Vname);
var E = db._collection(Ename);
var i;
for (i = 1; i <= Math.pow(2,N)-1; i++) {
V.insert({_key: prefix + i, nr: i});
}
for (i = 1; i <= Math.pow(2,N-1)-1; i++) {
[2*i, 2*i+1].forEach(function(j) {
if (j < Math.pow(2,N)) {
if (! rev) {
E.insert(Vname+"/"+prefix+i, Vname+"/"+prefix+j,
{_key: prefix + j });
}
else {
E.insert(Vname+"/"+prefix+j, Vname+"/"+prefix+i,
{_key: prefix + j });
}
}
});
}
}
function makeTreeGraph (Vname, Ename, prefix, n) {
var db = require("internal").db;
db._drop(Vname);
db._drop(Ename);
var V = db._create(Vname);
var E = db._createEdgeCollection(Ename);
makeTree(Vname, Ename, prefix+"_out", 2*n, false);
makeTree(Vname, Ename, prefix+"_in", 2*n, true);
E.insert(Vname+"/"+prefix+"_out"+(Math.pow(2,n)-1),
Vname+"/"+prefix+"_in"+(Math.pow(2,n)-1),
{_key:"special"});
}