Skip to content

Depth by length; compareEdge option; options to trace #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 33 additions & 28 deletions lib/graph.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// const passConditionTrue = () => true;

module.exports = (function() {

'use strict';
Expand Down Expand Up @@ -118,31 +120,29 @@ module.exports = (function() {

opts = opts || {};

return this._search(node, opts.compare, opts.count, opts.direction, opts.minDepth, opts.maxDepth);

return this._search(node, opts);
}

trace(fromNode, toNode, direction) {
const compare = node => node === toNode;
const opts = typeof direction === 'object' ?
Object.assign({}, direction, { compare }) :
{ compare, direction };

let passCondition = function(node) {
return node === toNode;
};

return this._search(fromNode, passCondition, 1, direction)[0] || new Path([]);

return this._search(fromNode, opts)[0] || new Path([], false);
}

_search(node, passCondition, count, direction, minDepth, maxDepth) {

passCondition = (typeof passCondition === 'function') ? passCondition : function(node) {
return true;
};

direction |= 0;
count = Math.max(0, count | 0);
minDepth = Math.max(0, minDepth | 0);
maxDepth = Math.max(0, maxDepth | 0);

_search(node, opts) {
const {
compare: passCondition, // = passConditionTrue, // for bw compat
compareNode: passConditionNode, // = passConditionTrue,
compareEdge: passConditionEdge, // = passConditionTrue,
count = 0,
direction = 0,
minDepth = 0,
maxDepth = 0,
byLength = false
} = opts;
let nodePath = Object.create(null);
nodePath[node.__uniqid__] = [node];

Expand Down Expand Up @@ -189,24 +189,29 @@ module.exports = (function() {
for (let i = 0, len = edges.length; i < len; i++) {

let edge = edges[i];
let depth = curDepth + edge.distance;

if (maxDepth && depth > maxDepth) {
continue;
}
if (!passConditionEdge || passConditionEdge(edge)) {

let depth = curDepth + ( byLength ? 1 : edge.distance );

if (maxDepth && depth > maxDepth) {
continue;
}

let tnode = edge.oppositeNode(node);

let tnode = edges[i].oppositeNode(node);
if (!nodePath[tnode.__uniqid__] && (!passConditionNode || passConditionNode(tnode))) {

if (!nodePath[tnode.__uniqid__]) {
nodePath[tnode.__uniqid__] = [edge, tnode];
enqueue(tnode, depth);

nodePath[tnode.__uniqid__] = [edge, tnode];
enqueue(tnode, depth);
}

}

}

if (curDepth >= minDepth && passCondition(node)) {
if (curDepth >= minDepth && (!passCondition || passCondition(node)) && (!passConditionNode || passConditionNode(node))) {
return new Path(getPath(node, nodePath));
}

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
"mocha": "~2.2.4"
},
"scripts": {
"test": "mocha ./test/runner.js --harmony_classes",
"check": "node ./test/check.js --harmony_classes"
"test": "mocha ./test/runner.js ",
"check": "node ./test/check.js "
},
"repository": {
"type": "git",
Expand Down
159 changes: 159 additions & 0 deletions test/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -418,11 +418,14 @@ describe('Test Suite', function() {
graph.createNode('person', {id: 4, name: 'Kelly'});
graph.createNode('person', {id: 5, name: 'Trevor'});
graph.createNode('person', {id: 6, name: 'Arthur'});
graph.createNode('person', {id: 7, name: 'Lance'});

graph.createNode('food', {id: 1, name: 'Pizza'});
graph.createNode('food', {id: 2, name: 'Kale'});
graph.createNode('food', {id: 3, name: 'Ice Cream'});
graph.createNode('food', {id: 4, name: 'Meatballs'});
graph.createNode('food', {id: 5, name: 'Cilantro Salad'});
graph.createNode('food', {id: 6, name: 'Chocolate Bacon'});

graph.createEdge('likes').link(graph.nodes('person').find(1), graph.nodes('food').find(1));
graph.createEdge('likes').link(graph.nodes('person').find(1), graph.nodes('food').find(4));
Expand All @@ -442,6 +445,15 @@ describe('Test Suite', function() {
// graph.createEdge('likes').link(graph.nodes('person').find(6), graph.nodes('food').find(3));
graph.createEdge('likes').link(graph.nodes('person').find(6), graph.nodes('food').find(4));

graph.createEdge('dislikes').link(graph.nodes('person').find(7), graph.nodes('food').find(5));
graph.createEdge('dislikes').link(graph.nodes('person').find(7), graph.nodes('food').find(6));

graph.createEdge('dislikes').link(graph.nodes('person').find(6), graph.nodes('food').find(5));
graph.createEdge('dislikes').link(graph.nodes('person').find(6), graph.nodes('food').find(6));

graph.createEdge('dislikes').link(graph.nodes('person').find(5), graph.nodes('food').find(5));
graph.createEdge('dislikes').link(graph.nodes('person').find(5), graph.nodes('food').find(6));

describe('Graph#trace', function() {

let nodes = [
Expand Down Expand Up @@ -480,6 +492,88 @@ describe('Test Suite', function() {

});

describe('Graph#traceObjOpts', function() {

let nodes = [
graph.nodes('person').find(1),
graph.nodes('food').find(4),
graph.nodes('person').find(5),
graph.nodes('food').find(2),
graph.nodes('person').find(3)
];

const opts = {}

let path = graph.trace(graph.nodes('person').find(1), graph.nodes('person').find(3), opts);

it('should have the correct starting point', function() {

expect(path.start()).to.equal(nodes[0]);

});

it('should have the correct ending point', function() {

expect(path.end()).to.equal(nodes[4]);

});

it('should have the correct length', function() {

expect(path.length()).to.equal(4);

});

it('should have the correct distance', function() {

expect(path.distance()).to.equal(4);

});

});

describe('Graph#traceObjOptsDir', function() {

let nodes = [
graph.nodes('person').find(1),
graph.nodes('food').find(4),
graph.nodes('person').find(5),
graph.nodes('food').find(2),
graph.nodes('person').find(3)
];

const opts = {
direction: 1
}

let path = graph.trace(graph.nodes('person').find(1), graph.nodes('person').find(3), opts);

it('should have the correct starting point', function() {

expect(path.length()).to.be.equal(0);

});

it('should have the correct ending point', function() {

expect(path.length()).to.be.equal(0);

});

it('should have the correct length', function() {

expect(path.length()).to.be.equal(0);

});

it('should have the correct distance', function() {

expect(path.length()).to.be.equal(0);

});

});

describe('Graph#closest', function() {

let paths = graph.closest(graph.nodes('person').find(2), {
Expand Down Expand Up @@ -533,9 +627,57 @@ describe('Test Suite', function() {

expect(cnodes.indexOf(paths[1].end())).to.be.above(-1);
expect(cnodes.indexOf(paths[2].end())).to.be.above(-1);
});

it('should obey the compareNode function (all nodes in path must match)', function() {

let paths = graph.closest(graph.nodes('person').find(2), {
compareNode: function(node) { return node.entity === 'person'; },
count: 0,
direction: 0,
minDepth: 0,
maxDepth: 0
});

expect(paths.length).to.equal(1);

expect(paths[0].length()).to.equal(0);
expect(paths[0].end()).to.equal(graph.nodes('person').find(2));
expect(paths[0].start()).to.equal(graph.nodes('person').find(2));

});

it('should obey the compareEdge function (all edges in path must match)', function() {

let paths = graph.closest(graph.nodes('person').find(7), {
compareEdge: function(edge) { return edge.entity === 'dislikes'; },
count: 0,
direction: 0,
minDepth: 0,
maxDepth: 0
});

expect(paths.length).to.equal(5);

expect(paths[0].length()).to.equal(0); // person7 to self

expect(paths[1].length()).to.equal(1); // person7 dislikes food5
expect(paths[1].start()).to.equal(graph.nodes('person').find(7));
expect(paths[1].end()).to.equal(graph.nodes('food').find(5));

expect(paths[2].length()).to.equal(1); // person7 dislikes food6
expect(paths[2].start()).to.equal(graph.nodes('person').find(7));
expect(paths[2].end()).to.equal(graph.nodes('food').find(6));

expect(paths[3].length()).to.equal(2); // person7 dislikes the same food as person6
expect(paths[3].start()).to.equal(graph.nodes('person').find(7));
expect(paths[3].end()).to.equal(graph.nodes('person').find(6));

expect(paths[4].length()).to.equal(2); // person7 dislikes the same food as person5
expect(paths[4].start()).to.equal(graph.nodes('person').find(7));
expect(paths[4].end()).to.equal(graph.nodes('person').find(5));
});

it('should obey the count attribute', function() {

let paths = graph.closest(graph.nodes('person').find(2), {
Expand Down Expand Up @@ -595,6 +737,23 @@ describe('Test Suite', function() {

});

it('should obey the maxDepth attribute by length', function() {

let paths = graph.closest(graph.nodes('person').find(2), {
compare: function() { return true; },
count: 0,
direction: 0,
minDepth: 0,
maxDepth: 1,
byLength: true
});

expect(paths.length).to.equal(2);
expect(paths[0].end()).to.equal(graph.nodes('person').find(2));
expect(paths[1].end()).to.equal(graph.nodes('food').find(1));

});

});

});
Expand Down