diff --git a/js/module/graph.js b/js/module/graph.js index e09903dad0a2e8e1a6a2f0e77cda20fbc9b62dc0..20a8f7baf0bd128f6d01ba1bae6178d858c4e8f9 100644 --- a/js/module/graph.js +++ b/js/module/graph.js @@ -1,10 +1,10 @@ -var s = null, graph = null, graphMode = "default"; +var s = null, graph = null, graphMode = null; -function GraphTracer() { - Tracer.call(this, GraphTracer); +GraphTracer.graphMode = "default"; - if (s && graph && graphMode == "default") return; - initSigma(); +function GraphTracer(module) { + Tracer.call(this, module || GraphTracer); + return initGraph(this.module); } GraphTracer.prototype = Object.create(Tracer.prototype); @@ -26,9 +26,8 @@ GraphTracer.prototype.reset = function () { // Override GraphTracer.prototype.setData = function (G) { - Tracer.prototype.setData.call(this, G); + if (Tracer.prototype.setData.call(this, arguments)) return; - this.G = G; graph.clear(); var nodes = []; var edges = []; @@ -73,11 +72,11 @@ Tracer.prototype.clear = function () { }; Tracer.prototype.visit = function (targetNode, sourceNode) { - this.pushStep({type: 'visit', node: targetNode, parent: sourceNode}, true); + this.pushStep({type: 'visit', node: targetNode, Tracer: sourceNode}, true); }; Tracer.prototype.leave = function (targetNode, sourceNode) { - this.pushStep({type: 'leave', node: targetNode, parent: sourceNode}, true); + this.pushStep({type: 'leave', node: targetNode, Tracer: sourceNode}, true); }; GraphTracer.prototype.processStep = function (step, options) { @@ -92,15 +91,15 @@ GraphTracer.prototype.processStep = function (step, options) { var node = graph.nodes(n(step.node)); var color = visit ? graphColor.visited : graphColor.left; node.color = color; - if (step.parent !== undefined) { - var edgeId = e(step.parent, step.node); + if (step.Tracer !== undefined) { + var edgeId = e(step.Tracer, step.node); var edge = graph.edges(edgeId); edge.color = color; graph.dropEdge(edgeId).addEdge(edge); } - var parent = step.parent; - if (parent === undefined) parent = ''; - printTrace(visit ? parent + ' -> ' + step.node : parent + ' <- ' + step.node); + var Tracer = step.Tracer; + if (Tracer === undefined) Tracer = ''; + printTrace(visit ? Tracer + ' -> ' + step.node : Tracer + ' <- ' + step.node); break; } }; @@ -187,7 +186,11 @@ var e = function (v1, v2) { return 'e' + v1 + '_' + v2; }; -var initSigma = function () { +var initGraph = function (module) { + if (s && graph && graphMode == module.graphMode) return false; + graphMode = module.graphMode; + + $('.visualize_container').empty(); s = new sigma({ renderer: { container: $('.visualize_container')[0], @@ -256,4 +259,6 @@ var initSigma = function () { }); }; sigma.plugins.dragNodes(s, s.renderers[0]); + + return true; }; \ No newline at end of file diff --git a/js/module/weighted_graph.js b/js/module/weighted_graph.js new file mode 100644 index 0000000000000000000000000000000000000000..988b9b47d2b5efabc95861a1e025d1dd43a34c04 --- /dev/null +++ b/js/module/weighted_graph.js @@ -0,0 +1,58 @@ +WeightedGraphTracer.graphMode = "weighted"; + +function WeightedGraphTracer(module) { + if (GraphTracer.call(this, module || WeightedGraphTracer)) { + initWeightedGraph(); + return true; + } + return false; +} + +WeightedGraphTracer.prototype = Object.create(GraphTracer.prototype); +WeightedGraphTracer.prototype.constructor = WeightedGraphTracer; + +// Override +WeightedGraphTracer.prototype.setData = function (G) { + if (Tracer.prototype.setData.call(this, arguments)) return; + + graph.clear(); + var nodes = []; + var edges = []; + var unitAngle = 2 * Math.PI / G.length; + var currentAngle = 0; + for (var i = 0; i < G.length; i++) { + currentAngle += unitAngle; + nodes.push({ + id: n(i), + label: '' + i, + x: .5 + Math.sin(currentAngle) / 2, + y: .5 + Math.cos(currentAngle) / 2, + size: 1, + color: graphColor.default + }); + for (var j = 0; j < G[i].length; j++) { + edges.push({ + id: e(i, G[i][j]), + source: n(i), + target: n(G[i][j]), + color: graphColor.default, + size: 1 + }) + } + } + + graph.read({ + nodes: nodes, + edges: edges + }); + s.camera.goTo({ + x: 0, + y: 0, + angle: 0, + ratio: 1 + }); + this.refresh(); +}; + +var initWeightedGraph = function () { +}; \ No newline at end of file diff --git a/js/tracer.js b/js/tracer.js index 5fab3d431a560bf4f9318c4b1040e1b46ff06925..c60fff5cdb7ff6f542138bb68e09060e46cc44f7 100644 --- a/js/tracer.js +++ b/js/tracer.js @@ -1,4 +1,5 @@ var timer = null; +var lastModule = null, lastData = null; var Tracer = function (module) { this.module = module; @@ -6,6 +7,7 @@ var Tracer = function (module) { this.pause = false; this.traceOptions = null; this.traceIndex = -1; + this.lastData = null; }; Tracer.prototype.resize = function () { @@ -17,7 +19,12 @@ Tracer.prototype.reset = function () { $('#tab_trace .wrapper').empty(); }; -Tracer.prototype.setData = function () { +Tracer.prototype.setData = function (arguments) { + var data = JSON.stringify(arguments); + if (lastModule == this.module && lastData == data) return true; + lastModule = this.module; + lastData = data; + return false; }; Tracer.prototype.pushStep = function (step, delay) {