Skip to content

Commit 3a24da4

Browse files
committed
Add name/description properties to DirectedGraph (enhancement).
... fix for issue #43.
1 parent 47f5e24 commit 3a24da4

8 files changed

+86
-13
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,8 @@ The `DirectedGraph` container object created by this process models "a graph" ge
275275

276276
#### DirectedGraph container methods
277277

278+
- `get/setGraphName()` - get/set the name of the graph
279+
- `get/setGraphDescription()` - set/set the description of the graph
278280
- `verticesCount()` - obtain the count of vertices in the container
279281
- `getVertices()` - retrieve an array of ID strings for all vertices in the container
280282
- `edgesCount()` - obtain the count of edges in the container

src/digraph-algorithm-common-request.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*/
1212

1313
var helperFunctions = require('./helper-functions');
14-
var createBreadthFirstTraverseContext = require('./digraph-algorithm-common-context');
14+
var TRAVERSE_CONTEXT = require('./digraph-algorithm-common-context');
1515

1616
/*
1717
request = {
@@ -46,7 +46,7 @@ module.exports = function (request_) {
4646
var inBreakScope = false;
4747

4848
var createTraverseContext = function() {
49-
var response = createBreadthFirstTraverseContext({ digraph: nrequest.digraph });
49+
var response = TRAVERSE_CONTEXT({ digraph: nrequest.digraph });
5050
var result = null;
5151
if (response.error) {
5252
errors.unshift(response.error);

src/digraph-json-export.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ var DigraphDataExporter = module.exports = {};
2121

2222
DigraphDataExporter.exportObject = function (digraph_) {
2323
var digraphState = {
24+
name: digraph_.getGraphName(),
25+
description: digraph_.getGraphDescription(),
2426
vlist: [],
2527
elist: []
2628
};

src/digraph-json-import.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,32 @@ module.exports = function (digraph_, jsonOrObject_) {
8484
break;
8585
}
8686

87+
type = getType(jsonParse.name);
88+
switch (type) {
89+
case '[object Undefined]':
90+
jsonParse.name = "";
91+
break;
92+
case '[object String]':
93+
break;
94+
default:
95+
errors.unshift("JSON semantics error: Expected 'name' to be a string but found '" + type + "'.");
96+
break;
97+
}
98+
digraph_.setGraphName(jsonParse.name);
99+
100+
type = getType(jsonParse.description);
101+
switch (type) {
102+
case '[object Undefined]':
103+
jsonParse.description = "";
104+
break;
105+
case '[object String]':
106+
break;
107+
default:
108+
error.unshift("JSON semantics error: Expected 'description' to be a string but found '" + type + "'.");
109+
break;
110+
}
111+
digraph_.setGraphDescription(jsonParse.description);
112+
87113
type = getType(jsonParse.vlist);
88114
switch (type) {
89115
case '[object Undefined]':

src/digraph.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ var digraphExport = require('./digraph-json-export');
2424

2525
var DirectedGraph = (function() {
2626
function DirectedGraph(jsonOrObject_) {
27+
28+
// Meta methods
29+
this.getGraphName = __bind(this.getGraphName, this);
30+
this.setGraphName = __bind(this.setGraphName, this);
31+
this.getGraphDescription = __bind(this.getGraphDescription, this);
32+
this.setGraphDescription = __bind(this.setGraphDescription, this);
33+
2734
// Vertex-scope methods
2835
this.isVertex = __bind(this.isVertex, this);
2936
this.addVertex = __bind(this.addVertex, this);
@@ -62,6 +69,8 @@ var digraphExport = require('./digraph-json-export');
6269

6370
// DirectedGraph container private runtime state.
6471
this._private = {
72+
name: "",
73+
description: "",
6574
vertexMap: {},
6675
rootMap: {},
6776
leafMap: {},
@@ -76,6 +85,38 @@ var digraphExport = require('./digraph-json-export');
7685
}
7786
}
7887

88+
// META METHODS
89+
90+
DirectedGraph.prototype.getGraphName = function() {
91+
return this._private.name;
92+
};
93+
94+
DirectedGraph.prototype.setGraphName = function(string_) {
95+
var response = { error: null, result: null };
96+
if (helperFunctions.JSType(string_) === '[object String]') {
97+
this._private.name = string_;
98+
response.result = true;
99+
} else {
100+
response.error = "Invalid graph name specified. Expected '[object String]'.";
101+
}
102+
return response;
103+
};
104+
105+
DirectedGraph.prototype.getGraphDescription = function() {
106+
return this._private.description;
107+
};
108+
109+
DirectedGraph.prototype.setGraphDescription = function(string_) {
110+
var response = { error: null, result: null };
111+
if (helperFunctions.JSType(string_) === '[object String]') {
112+
this._private.description = string_;
113+
response.result = true;
114+
} else {
115+
response.error = "Invalid graph name specified. Expected '[object String]'.";
116+
}
117+
return response;
118+
};
119+
79120
// VERTEX-SCOPE METHODS
80121

81122
DirectedGraph.prototype.isVertex = function (vertexId_) {

test/test-digraph-algorithm-common-request.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ testTraverseRequestNormalizer({
5252
request: { digraph: digraph, visitor: nullVisitor },
5353
expectedResults: {
5454
error: '',
55-
json: '{"digraph":"{\\"vlist\\":[],\\"elist\\":[{\\"e\\":{\\"u\\":\\"VARMIT\\",\\"v\\":\\"RAT\\"}},{\\"e\\":{\\"u\\":\\"VARMIT\\",\\"v\\":\\"MOUSE\\"}},{\\"e\\":{\\"u\\":\\"VARMIT\\",\\"v\\":\\"VOLE\\"}}]}","visitor":{},"options":{"startVector":["VARMIT"],"allowEmptyStartVector":false,"signalStart":true,"traverseContext":{"searchStatus":"pending","colorMap":{"VARMIT":0,"RAT":0,"MOUSE":0,"VOLE":0},"undiscoveredMap":{"VARMIT":true,"RAT":true,"MOUSE":true,"VOLE":true}}}}'
55+
json: '{"digraph":"{\\"name\\":\\"\\",\\"description\\":\\"\\",\\"vlist\\":[],\\"elist\\":[{\\"e\\":{\\"u\\":\\"VARMIT\\",\\"v\\":\\"RAT\\"}},{\\"e\\":{\\"u\\":\\"VARMIT\\",\\"v\\":\\"MOUSE\\"}},{\\"e\\":{\\"u\\":\\"VARMIT\\",\\"v\\":\\"VOLE\\"}}]}","visitor":{},"options":{"startVector":["VARMIT"],"allowEmptyStartVector":false,"signalStart":true,"traverseContext":{"searchStatus":"pending","colorMap":{"VARMIT":0,"RAT":0,"MOUSE":0,"VOLE":0},"undiscoveredMap":{"VARMIT":true,"RAT":true,"MOUSE":true,"VOLE":true}}}}'
5656
}
5757
});
5858

@@ -61,7 +61,7 @@ testTraverseRequestNormalizer({
6161
request: { digraph: digraph, visitor: nullVisitor, options: { signalStart: true }},
6262
expectedResults: {
6363
error: '',
64-
json: '{"digraph":"{\\"vlist\\":[],\\"elist\\":[{\\"e\\":{\\"u\\":\\"VARMIT\\",\\"v\\":\\"RAT\\"}},{\\"e\\":{\\"u\\":\\"VARMIT\\",\\"v\\":\\"MOUSE\\"}},{\\"e\\":{\\"u\\":\\"VARMIT\\",\\"v\\":\\"VOLE\\"}}]}","visitor":{},"options":{"signalStart":true,"startVector":["VARMIT"],"allowEmptyStartVector":false,"traverseContext":{"searchStatus":"pending","colorMap":{"VARMIT":0,"RAT":0,"MOUSE":0,"VOLE":0},"undiscoveredMap":{"VARMIT":true,"RAT":true,"MOUSE":true,"VOLE":true}}}}'
64+
json: '{"digraph":"{\\"name\\":\\"\\",\\"description\\":\\"\\",\\"vlist\\":[],\\"elist\\":[{\\"e\\":{\\"u\\":\\"VARMIT\\",\\"v\\":\\"RAT\\"}},{\\"e\\":{\\"u\\":\\"VARMIT\\",\\"v\\":\\"MOUSE\\"}},{\\"e\\":{\\"u\\":\\"VARMIT\\",\\"v\\":\\"VOLE\\"}}]}","visitor":{},"options":{"signalStart":true,"startVector":["VARMIT"],"allowEmptyStartVector":false,"traverseContext":{"searchStatus":"pending","colorMap":{"VARMIT":0,"RAT":0,"MOUSE":0,"VOLE":0},"undiscoveredMap":{"VARMIT":true,"RAT":true,"MOUSE":true,"VOLE":true}}}}'
6565
}
6666
});
6767

@@ -70,7 +70,7 @@ testTraverseRequestNormalizer({
7070
request: { digraph: digraph, visitor: nullVisitor, options: { signalStart: false }},
7171
expectedResults: {
7272
error: '',
73-
json: '{"digraph":"{\\"vlist\\":[],\\"elist\\":[{\\"e\\":{\\"u\\":\\"VARMIT\\",\\"v\\":\\"RAT\\"}},{\\"e\\":{\\"u\\":\\"VARMIT\\",\\"v\\":\\"MOUSE\\"}},{\\"e\\":{\\"u\\":\\"VARMIT\\",\\"v\\":\\"VOLE\\"}}]}","visitor":{},"options":{"signalStart":false,"startVector":["VARMIT"],"allowEmptyStartVector":false,"traverseContext":{"searchStatus":"pending","colorMap":{"VARMIT":0,"RAT":0,"MOUSE":0,"VOLE":0},"undiscoveredMap":{"VARMIT":true,"RAT":true,"MOUSE":true,"VOLE":true}}}}'
73+
json: '{"digraph":"{\\"name\\":\\"\\",\\"description\\":\\"\\",\\"vlist\\":[],\\"elist\\":[{\\"e\\":{\\"u\\":\\"VARMIT\\",\\"v\\":\\"RAT\\"}},{\\"e\\":{\\"u\\":\\"VARMIT\\",\\"v\\":\\"MOUSE\\"}},{\\"e\\":{\\"u\\":\\"VARMIT\\",\\"v\\":\\"VOLE\\"}}]}","visitor":{},"options":{"signalStart":false,"startVector":["VARMIT"],"allowEmptyStartVector":false,"traverseContext":{"searchStatus":"pending","colorMap":{"VARMIT":0,"RAT":0,"MOUSE":0,"VOLE":0},"undiscoveredMap":{"VARMIT":true,"RAT":true,"MOUSE":true,"VOLE":true}}}}'
7474
}
7575
});
7676

@@ -88,7 +88,7 @@ testTraverseRequestNormalizer({
8888
request: { digraph: digraph, visitor: nullVisitor, options: { startVector: "someVertexMayBeInvalid"}},
8989
expectedResults: {
9090
error: '',
91-
json: '{"digraph":"{\\"vlist\\":[],\\"elist\\":[{\\"e\\":{\\"u\\":\\"VARMIT\\",\\"v\\":\\"RAT\\"}},{\\"e\\":{\\"u\\":\\"VARMIT\\",\\"v\\":\\"MOUSE\\"}},{\\"e\\":{\\"u\\":\\"VARMIT\\",\\"v\\":\\"VOLE\\"}}]}","visitor":{},"options":{"startVector":["someVertexMayBeInvalid"],"allowEmptyStartVector":false,"signalStart":true,"traverseContext":{"searchStatus":"pending","colorMap":{"VARMIT":0,"RAT":0,"MOUSE":0,"VOLE":0},"undiscoveredMap":{"VARMIT":true,"RAT":true,"MOUSE":true,"VOLE":true}}}}'
91+
json: '{"digraph":"{\\"name\\":\\"\\",\\"description\\":\\"\\",\\"vlist\\":[],\\"elist\\":[{\\"e\\":{\\"u\\":\\"VARMIT\\",\\"v\\":\\"RAT\\"}},{\\"e\\":{\\"u\\":\\"VARMIT\\",\\"v\\":\\"MOUSE\\"}},{\\"e\\":{\\"u\\":\\"VARMIT\\",\\"v\\":\\"VOLE\\"}}]}","visitor":{},"options":{"startVector":["someVertexMayBeInvalid"],"allowEmptyStartVector":false,"signalStart":true,"traverseContext":{"searchStatus":"pending","colorMap":{"VARMIT":0,"RAT":0,"MOUSE":0,"VOLE":0},"undiscoveredMap":{"VARMIT":true,"RAT":true,"MOUSE":true,"VOLE":true}}}}'
9292
}
9393
});
9494

@@ -97,7 +97,7 @@ testTraverseRequestNormalizer({
9797
request: { digraph: digraph, visitor: nullVisitor, options: { startVector: [ "someVertexMayBeInvalid", "apple", "orange", "RAT" ]}},
9898
expectedResults: {
9999
error: '',
100-
json: '{"digraph":"{\\"vlist\\":[],\\"elist\\":[{\\"e\\":{\\"u\\":\\"VARMIT\\",\\"v\\":\\"RAT\\"}},{\\"e\\":{\\"u\\":\\"VARMIT\\",\\"v\\":\\"MOUSE\\"}},{\\"e\\":{\\"u\\":\\"VARMIT\\",\\"v\\":\\"VOLE\\"}}]}","visitor":{},"options":{"startVector":["someVertexMayBeInvalid","apple","orange","RAT"],"allowEmptyStartVector":false,"signalStart":true,"traverseContext":{"searchStatus":"pending","colorMap":{"VARMIT":0,"RAT":0,"MOUSE":0,"VOLE":0},"undiscoveredMap":{"VARMIT":true,"RAT":true,"MOUSE":true,"VOLE":true}}}}'
100+
json: '{"digraph":"{\\"name\\":\\"\\",\\"description\\":\\"\\",\\"vlist\\":[],\\"elist\\":[{\\"e\\":{\\"u\\":\\"VARMIT\\",\\"v\\":\\"RAT\\"}},{\\"e\\":{\\"u\\":\\"VARMIT\\",\\"v\\":\\"MOUSE\\"}},{\\"e\\":{\\"u\\":\\"VARMIT\\",\\"v\\":\\"VOLE\\"}}]}","visitor":{},"options":{"startVector":["someVertexMayBeInvalid","apple","orange","RAT"],"allowEmptyStartVector":false,"signalStart":true,"traverseContext":{"searchStatus":"pending","colorMap":{"VARMIT":0,"RAT":0,"MOUSE":0,"VOLE":0},"undiscoveredMap":{"VARMIT":true,"RAT":true,"MOUSE":true,"VOLE":true}}}}'
101101
}
102102
});
103103

@@ -123,7 +123,7 @@ testTraverseRequestNormalizer({
123123
request: { digraph: digraph, visitor: nullVisitor, options: { traverseContext: traverseContext }} ,
124124
expectedResults: {
125125
error: '',
126-
json: '{"digraph":"{\\"vlist\\":[],\\"elist\\":[{\\"e\\":{\\"u\\":\\"VARMIT\\",\\"v\\":\\"RAT\\"}},{\\"e\\":{\\"u\\":\\"VARMIT\\",\\"v\\":\\"MOUSE\\"}},{\\"e\\":{\\"u\\":\\"VARMIT\\",\\"v\\":\\"VOLE\\"}}]}","visitor":{},"options":{"traverseContext":{"searchStatus":"TEST-VALUE","colorMap":{"VARMIT":0,"RAT":0,"MOUSE":0,"VOLE":0},"undiscoveredMap":{"VARMIT":true,"RAT":true,"MOUSE":true,"VOLE":true}},"startVector":["VARMIT"],"allowEmptyStartVector":false,"signalStart":true}}'
126+
json: '{"digraph":"{\\"name\\":\\"\\",\\"description\\":\\"\\",\\"vlist\\":[],\\"elist\\":[{\\"e\\":{\\"u\\":\\"VARMIT\\",\\"v\\":\\"RAT\\"}},{\\"e\\":{\\"u\\":\\"VARMIT\\",\\"v\\":\\"MOUSE\\"}},{\\"e\\":{\\"u\\":\\"VARMIT\\",\\"v\\":\\"VOLE\\"}}]}","visitor":{},"options":{"traverseContext":{"searchStatus":"TEST-VALUE","colorMap":{"VARMIT":0,"RAT":0,"MOUSE":0,"VOLE":0},"undiscoveredMap":{"VARMIT":true,"RAT":true,"MOUSE":true,"VOLE":true}},"startVector":["VARMIT"],"allowEmptyStartVector":false,"signalStart":true}}'
127127
}
128128
});
129129

@@ -150,7 +150,7 @@ testTraverseRequestNormalizer({
150150
request: { digraph: digraph, visitor: nullVisitor, options: { allowEmptyStartVector: true }},
151151
expectedResults: {
152152
error: '',
153-
json: '{"digraph":"{\\"vlist\\":[],\\"elist\\":[{\\"e\\":{\\"u\\":\\"A\\",\\"v\\":\\"B\\"}},{\\"e\\":{\\"u\\":\\"B\\",\\"v\\":\\"A\\"}}]}","visitor":{},"options":{"allowEmptyStartVector":true,"startVector":[],"signalStart":true,"traverseContext":{"searchStatus":"pending","colorMap":{"A":0,"B":0},"undiscoveredMap":{"A":true,"B":true}}}}'
153+
json: '{"digraph":"{\\"name\\":\\"\\",\\"description\\":\\"\\",\\"vlist\\":[],\\"elist\\":[{\\"e\\":{\\"u\\":\\"A\\",\\"v\\":\\"B\\"}},{\\"e\\":{\\"u\\":\\"B\\",\\"v\\":\\"A\\"}}]}","visitor":{},"options":{"allowEmptyStartVector":true,"startVector":[],"signalStart":true,"traverseContext":{"searchStatus":"pending","colorMap":{"A":0,"B":0},"undiscoveredMap":{"A":true,"B":true}}}}'
154154
}
155155
});
156156

test/test-digraph-container-create.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ testCreateDirectedGraph({
124124
testName: "Default construction", validConfig: true,
125125
expectedResults: {
126126
error: '',
127-
result: '{"vlist":[],"elist":[]}'
127+
result: '{"name":"","description":"","vlist":[],"elist":[]}'
128128
}
129129
});
130130

@@ -147,7 +147,7 @@ testCreateDirectedGraph({
147147
},
148148
expectedResults: {
149149
error: '',
150-
result: '{"vlist":[{"u":"all","p":"whatever vertex property"},{"u":"work","p":{"x":6}}],"elist":[{"e":{"u":"all","v":"work"},"p":"whatever edge property"},{"e":{"u":"work","v":"and"}},{"e":{"u":"and","v":"no"}},{"e":{"u":"no","v":"play"},"p":"leads to superior code :)"}]}'
150+
result: '{"name":"","description":"","vlist":[{"u":"all","p":"whatever vertex property"},{"u":"work","p":{"x":6}}],"elist":[{"e":{"u":"all","v":"work"},"p":"whatever edge property"},{"e":{"u":"work","v":"and"}},{"e":{"u":"and","v":"no"}},{"e":{"u":"no","v":"play"},"p":"leads to superior code :)"}]}'
151151
}
152152
});
153153

test/test-digraph-container.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ describe("DirectedGraph container object tests", function() {
5151
});
5252

5353
it("graph export JSON should match expected control string", function() {
54-
var expectedJSON = '{"vlist":[{"u":"foo","p":{"k":"foo"}},{"u":"bar","p":{"k":"bar"}},{"u":"baz","p":{"k":"baz"}}],"elist":[{"e":{"u":"foo","v":"bar"}},{"e":{"u":"foo","v":"baz"}},{"e":{"u":"bar","v":"foo"}},{"e":{"u":"bar","v":"baz"}},{"e":{"u":"baz","v":"foo"}},{"e":{"u":"baz","v":"bar"}}]}';
54+
var expectedJSON = '{"name":"","description":"","vlist":[{"u":"foo","p":{"k":"foo"}},{"u":"bar","p":{"k":"bar"}},{"u":"baz","p":{"k":"baz"}}],"elist":[{"e":{"u":"foo","v":"bar"}},{"e":{"u":"foo","v":"baz"}},{"e":{"u":"bar","v":"foo"}},{"e":{"u":"bar","v":"baz"}},{"e":{"u":"baz","v":"foo"}},{"e":{"u":"baz","v":"bar"}}]}';
5555
assert.equal(digraph.toJSON(), expectedJSON);
5656
});
5757

@@ -103,11 +103,13 @@ describe("DirectedGraph container object tests", function() {
103103
describe("Test JSON export algorith's vertex skip facility", function() {
104104

105105
var testGraph = null;
106-
var expectedJSON = '{"vlist":[{"u":"strawberry","p":"has a property"},{"u":"not-mentioned-no-property"}],"elist":[{"e":{"u":"apple","v":"orange"}},{"e":{"u":"bannana","v":"apple"}},{"e":{"u":"pineapple","v":"orange"}},{"e":{"u":"strawberry","v":"blueberry"},"p":"link"}]}';
106+
var expectedJSON = '{"name":"test","description":"test","vlist":[{"u":"strawberry","p":"has a property"},{"u":"not-mentioned-no-property"}],"elist":[{"e":{"u":"apple","v":"orange"}},{"e":{"u":"bannana","v":"apple"}},{"e":{"u":"pineapple","v":"orange"}},{"e":{"u":"strawberry","v":"blueberry"},"p":"link"}]}';
107107
var actualJSON = null;
108108

109109
before(function() {
110110
var digraph = new DirectedGraph({
111+
name: 'test',
112+
description: 'test',
111113
vlist: [
112114
{ u: 'apple' },
113115
{ u: 'orange' },

0 commit comments

Comments
 (0)