-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
194 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
function cypher(model) { | ||
function props(element) { | ||
var props = {}; | ||
if (element.label()) props.label = element.label(); | ||
element.properties().list().forEach(function (property) { | ||
props[property.key] = property.value; | ||
}); | ||
return props; | ||
} | ||
|
||
function isIdentifier(name) { | ||
return /^[\w\d_]+$/.test(name); | ||
} | ||
|
||
function formatIdentifier(name) { | ||
return isIdentifier(name) ? name : "`" + name + "`"; | ||
} | ||
|
||
function render(props) { | ||
var res = ""; | ||
for (var key in props) { | ||
if (res.length > 0) res += ","; | ||
if (props.hasOwnProperty(key)) { | ||
res += formatIdentifier(key) + ":"; | ||
var value = props[key]; | ||
res += typeof value == "string" ? "'" + value + "'" : value; | ||
} | ||
} | ||
return res.length == 0 ? "" : "{" + res + "}"; | ||
} | ||
|
||
var statements = []; | ||
model.nodeList().forEach(function (node) { | ||
statements.push("(" + formatIdentifier("_"+node.id) +" "+ render(props(node)) + ")"); | ||
}); | ||
model.relationshipList().forEach(function (rel) { | ||
statements.push(formatIdentifier("_"+rel.start.id) + | ||
"-[:" + formatIdentifier(rel.label()||"RELATED_TO") + | ||
// " " + TODO render(props(rel)) + | ||
"]->"+ | ||
formatIdentifier("_"+rel.end.id) | ||
) | ||
}); | ||
if (statements.length==0) return ""; | ||
return "CREATE \n " + statements.join(",\n "); | ||
}; | ||
if (typeof exports != "undefined") exports.cypher=cypher | ||
gd.cypher=function(model) {return cypher(model || this.model());} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
var vows = require("vows"), | ||
assert = require("assert" ), | ||
d3 = require("d3"); | ||
|
||
var suite = vows.describe("graph model"); | ||
|
||
require("../../graph-diagram.js"); | ||
var cypher=require("../../cypher.js").cypher; | ||
|
||
suite.addBatch({ | ||
"format cypher": { | ||
"empty model": { | ||
topic: function() { | ||
var model = gd.model(); | ||
return cypher(model); | ||
}, | ||
"empty": function(statement) { | ||
assert.equal(statement, ""); | ||
} | ||
}, | ||
"one node": { | ||
topic: function() { | ||
var model = gd.model(); | ||
model.createNode("node_A").x(12).y(34).label("A" ).class("diagram-specific-class") | ||
.properties().set("first name", "Alistair").set("location", "London").set("number",1).set("male",true); | ||
|
||
return cypher(model); | ||
}, | ||
"one node": function(statement) { | ||
assert.match(statement,/CREATE/); | ||
}, | ||
"with node id": function(statement) { | ||
assert.match(statement,/\(_node_A /); | ||
}, | ||
"with label": function(statement) { | ||
assert.match(statement,/label:'A'/); | ||
}, | ||
"with properties": function(statement) { | ||
assert.match(statement,/`first name`:'Alistair'/); | ||
assert.match(statement,/location:'London'/); | ||
assert.match(statement,/number:1/); | ||
assert.match(statement,/male:true/); | ||
} | ||
}, | ||
"two nodes and one relationship": { | ||
topic: function() { | ||
var model = gd.model(); | ||
var nodeA = model.createNode("node_A"); | ||
var nodeB = model.createNode("node B"); | ||
model.createRelationship(nodeA, nodeB).label("RELATED TO" ).class("diagram-specific-class"); | ||
return cypher(model); | ||
}, | ||
"create statement": function(statement) { | ||
assert.match(statement,/CREATE/); | ||
}, | ||
"from A": function(statement) { | ||
assert.match(statement,/\(_node_A \)/); | ||
assert.match(statement,/_node_A-\[/); | ||
}, | ||
"to B": function(statement) { | ||
assert.match(statement,/\(`_node B` \)/); | ||
assert.match(statement,/\]->`_node B`/); | ||
}, | ||
"with label": function(statement) { | ||
assert.match(statement,/\[:`RELATED TO`/); | ||
} | ||
} | ||
} | ||
}); | ||
|
||
suite.export(module); |