Skip to content

Commit 0e46a18

Browse files
author
Philip Guo
committed
created a separate jsPlumb instance for each ExecutionVisualizer object
1 parent ab2b739 commit 0e46a18

1 file changed

Lines changed: 56 additions & 49 deletions

File tree

‎PyTutorGAE/js/pytutor.js‎

Lines changed: 56 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,35 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2727
*/
2828

2929

30-
// Massive refactoring notes:
31-
// - does jsPlumb have a notion of "sets" of connectors so that we can reset a particular
32-
// set rather than ALL connections?
33-
// - or maybe each ExecutionVisualizer instance needs to keep track
34-
// of its own set of jsPlumb elements ...
35-
30+
var curVisualizerID = 1; // global to uniquely identify each ExecutionVisualizer instance
3631

3732
function ExecutionVisualizer(inputCode, traceData, startingInstruction, domRootID) {
3833
this.curInputCode = inputCode;
3934
this.curTrace = traceData;
4035
this.curInstr = startingInstruction;
4136

37+
this.visualizerID = curVisualizerID;
38+
curVisualizerID++;
39+
40+
41+
// cool, we can create a separate jsPlumb instance for each visualization:
42+
this.jsPlumbInstance = jsPlumb.getInstance({
43+
Endpoint: ["Dot", {radius:3}],
44+
EndpointStyles: [{fillStyle: lightGray}, {fillstyle: null} /* make right endpoint invisible */],
45+
Anchors: ["RightMiddle", "LeftMiddle"],
46+
PaintStyle: {lineWidth:1, strokeStyle: lightGray},
47+
48+
// bezier curve style:
49+
//Connector: [ "Bezier", { curviness:15 }], /* too much 'curviness' causes lines to run together */
50+
//Overlays: [[ "Arrow", { length: 14, width:10, foldback:0.55, location:0.35 }]],
51+
52+
// state machine curve style:
53+
Connector: [ "StateMachine" ],
54+
Overlays: [[ "Arrow", { length: 10, width:7, foldback:0.55, location:1 }]],
55+
EndpointHoverStyles: [{fillStyle: pinkish}, {fillstyle: null} /* make right endpoint invisible */],
56+
HoverPaintStyle: {lineWidth:2, strokeStyle: pinkish},
57+
});
58+
4259

4360
this.visitedLinesSet = null; // will change at every execution point
4461

@@ -1059,11 +1076,13 @@ ExecutionVisualizer.prototype.precomputeCurTraceLayouts = function() {
10591076
// of data structure aliasing. That is, aliased objects were rendered
10601077
// multiple times, and a unique ID label was used to identify aliases.
10611078
ExecutionVisualizer.prototype.renderDataStructures = function() {
1062-
var myViz = this; // to prevent confusion of 'this' inside of nested functions
1079+
// TODO: this is a known performance bottleneck (e.g., try to
1080+
// scroll quickly through the Towers of Hanoi example) since
1081+
// we are removing and redrawing ALL arrows on every call, so
1082+
// look into incrementally redrawing only what's changed between calls.
1083+
this.jsPlumbInstance.reset();
10631084

1064-
// VERY VERY IMPORTANT --- and reset ALL jsPlumb state to prevent
1065-
// weird mis-behavior!!!
1066-
jsPlumb.reset();
1085+
var myViz = this; // to prevent confusion of 'this' inside of nested functions
10671086

10681087
var curEntry = this.curTrace[this.curInstr];
10691088
var curToplevelLayout = this.curTraceLayouts[this.curInstr];
@@ -1079,9 +1098,14 @@ ExecutionVisualizer.prototype.renderDataStructures = function() {
10791098

10801099
// Key: CSS ID of the div element representing the stack frame variable
10811100
// (for stack->heap connections) or heap object (for heap->heap connections)
1082-
// the format is: 'heap_pointer_src_<src id>'
1101+
// the format is: '<this.visualizerID>__heap_pointer_src_<src id>'
10831102
// Value: CSS ID of the div element representing the value rendered in the heap
1084-
// (the format is: 'heap_object_<id>')
1103+
// (the format is: '<this.visualizerID>__heap_object_<id>')
1104+
//
1105+
// The reason we need to prepend this.visualizerID is because jsPlumb needs
1106+
// GLOBALLY UNIQUE IDs for use as connector endpoints. Actually this is no
1107+
// longer true since we can use this.jsPlumbInstance, but still it's nice
1108+
// to have unique div IDs
10851109
var connectionEndpointIDs = d3.map();
10861110
var heapConnectionEndpointIDs = d3.map(); // subset of connectionEndpointIDs for heap->heap connections
10871111

@@ -1216,24 +1240,29 @@ ExecutionVisualizer.prototype.renderDataStructures = function() {
12161240
// IE needs this div to be NON-EMPTY in order to properly
12171241
// render jsPlumb endpoints, so that's why we add an "&nbsp;"!
12181242

1219-
var srcDivID = 'heap_pointer_src_' + heap_pointer_src_id;
1243+
var srcDivID = myViz.visualizerID + '__heap_pointer_src_' + heap_pointer_src_id;
12201244
heap_pointer_src_id++; // just make sure each source has a UNIQUE ID
12211245
d3DomElement.append('<div id="' + srcDivID + '">&nbsp;</div>');
12221246

1247+
var dstDivID = myViz.visualizerID + '__heap_object_' + objID;
1248+
12231249
assert(!connectionEndpointIDs.has(srcDivID));
1224-
connectionEndpointIDs.set(srcDivID, 'heap_object_' + objID);
1250+
connectionEndpointIDs.set(srcDivID, dstDivID);
12251251

12261252
assert(!heapConnectionEndpointIDs.has(srcDivID));
1227-
heapConnectionEndpointIDs.set(srcDivID, 'heap_object_' + objID);
1253+
heapConnectionEndpointIDs.set(srcDivID, dstDivID);
12281254

12291255
return; // early return!
12301256
}
12311257

12321258

1259+
var heapObjID = myViz.visualizerID + '__heap_object_' + objID;
1260+
1261+
12331262
// wrap ALL compound objects in a heapObject div so that jsPlumb
12341263
// connectors can point to it:
1235-
d3DomElement.append('<div class="heapObject" id="heap_object_' + objID + '"></div>');
1236-
d3DomElement = myViz.domRoot.find('#heap_object_' + objID);
1264+
d3DomElement.append('<div class="heapObject" id="' + heapObjID + '"></div>');
1265+
d3DomElement = myViz.domRoot.find('#' + heapObjID);
12371266

12381267

12391268
renderedObjectIDs.set(objID, 1);
@@ -1424,7 +1453,8 @@ ExecutionVisualizer.prototype.renderDataStructures = function() {
14241453
curTr.find("td.stackFrameValue").append('<div id="' + varDivID + '">&nbsp;</div>');
14251454

14261455
assert(!connectionEndpointIDs.has(varDivID));
1427-
connectionEndpointIDs.set(varDivID, 'heap_object_' + getRefID(val));
1456+
var heapObjID = myViz.visualizerID + '__heap_object_' + getRefID(val);
1457+
connectionEndpointIDs.set(varDivID, heapObjID);
14281458
}
14291459
}
14301460
});
@@ -1512,7 +1542,9 @@ ExecutionVisualizer.prototype.renderDataStructures = function() {
15121542
curTr.find("td.stackFrameValue").append('<div id="' + varDivID + '">&nbsp;</div>');
15131543

15141544
assert(!connectionEndpointIDs.has(varDivID));
1515-
connectionEndpointIDs.set(varDivID, 'heap_object_' + getRefID(val));
1545+
1546+
var heapObjID = myViz.visualizerID + '__heap_object_' + getRefID(val);
1547+
connectionEndpointIDs.set(varDivID, heapObjID);
15161548
}
15171549
});
15181550
}
@@ -1521,12 +1553,12 @@ ExecutionVisualizer.prototype.renderDataStructures = function() {
15211553

15221554
// finally add all the connectors!
15231555
connectionEndpointIDs.forEach(function(varID, valueID) {
1524-
jsPlumb.connect({source: varID, target: valueID});
1556+
myViz.jsPlumbInstance.connect({source: varID, target: valueID});
15251557
});
15261558

15271559

15281560
function highlight_frame(frameID) {
1529-
var allConnections = jsPlumb.getConnections();
1561+
var allConnections = myViz.jsPlumbInstance.getConnections();
15301562
for (var i = 0; i < allConnections.length; i++) {
15311563
var c = allConnections[i];
15321564

@@ -1540,7 +1572,7 @@ ExecutionVisualizer.prototype.renderDataStructures = function() {
15401572
// then HIGHLIGHT IT!
15411573
c.setPaintStyle({lineWidth:1, strokeStyle: darkBlue});
15421574
c.endpoints[0].setPaintStyle({fillStyle: darkBlue});
1543-
c.endpoints[1].setVisible(false, true, true); // JUST set right endpoint to be invisible
1575+
//c.endpoints[1].setVisible(false, true, true); // JUST set right endpoint to be invisible
15441576

15451577
$(c.canvas).css("z-index", 1000); // ... and move it to the VERY FRONT
15461578
}
@@ -1549,15 +1581,15 @@ ExecutionVisualizer.prototype.renderDataStructures = function() {
15491581
// then HIGHLIGHT IT!
15501582
c.setPaintStyle({lineWidth:1, strokeStyle: darkBlue});
15511583
c.endpoints[0].setPaintStyle({fillStyle: darkBlue});
1552-
c.endpoints[1].setVisible(false, true, true); // JUST set right endpoint to be invisible
1584+
//c.endpoints[1].setVisible(false, true, true); // JUST set right endpoint to be invisible
15531585

15541586
$(c.canvas).css("z-index", 1000); // ... and move it to the VERY FRONT
15551587
}
15561588
else {
15571589
// else unhighlight it
15581590
c.setPaintStyle({lineWidth:1, strokeStyle: lightGray});
15591591
c.endpoints[0].setPaintStyle({fillStyle: lightGray});
1560-
c.endpoints[1].setVisible(false, true, true); // JUST set right endpoint to be invisible
1592+
//c.endpoints[1].setVisible(false, true, true); // JUST set right endpoint to be invisible
15611593

15621594
$(c.canvas).css("z-index", 0);
15631595
}
@@ -1714,28 +1746,3 @@ function getRefID(obj) {
17141746
return obj[1];
17151747
}
17161748

1717-
1718-
1719-
// global initializers (fortunately, jQuery allows multiple $(document).ready calls!)
1720-
$(document).ready(function() {
1721-
1722-
// set some sensible jsPlumb defaults
1723-
jsPlumb.Defaults.Endpoint = ["Dot", {radius:3}];
1724-
//jsPlumb.Defaults.Endpoint = ["Rectangle", {width:3, height:3}];
1725-
jsPlumb.Defaults.EndpointStyle = {fillStyle: lightGray};
1726-
jsPlumb.Defaults.Anchors = ["RightMiddle", "LeftMiddle"]; // for aesthetics!
1727-
jsPlumb.Defaults.PaintStyle = {lineWidth:1, strokeStyle: lightGray};
1728-
1729-
// bezier curve style:
1730-
//jsPlumb.Defaults.Connector = [ "Bezier", { curviness:15 }]; /* too much 'curviness' causes lines to run together */
1731-
//jsPlumb.Defaults.Overlays = [[ "Arrow", { length: 14, width:10, foldback:0.55, location:0.35 }]]
1732-
1733-
// state machine curve style:
1734-
jsPlumb.Defaults.Connector = [ "StateMachine" ];
1735-
jsPlumb.Defaults.Overlays = [[ "Arrow", { length: 10, width:7, foldback:0.55, location:1 }]];
1736-
1737-
jsPlumb.Defaults.EndpointHoverStyle = {fillStyle: pinkish};
1738-
jsPlumb.Defaults.HoverPaintStyle = {lineWidth:2, strokeStyle: pinkish};
1739-
1740-
});
1741-

0 commit comments

Comments
 (0)