// [KCPT] // The dgApi object contains everything needed to communicate back to the DG application. dgApi = {}; // For now we go through 'DG.currGameController.doCommand'. // Very shortly a development branch will be merged to the trunk // which simplifies that to 'DG.doCommand'. dgApi.gameController = window.parent.DG.currGameController; dgApi.doCommand = dgApi.gameController && dgApi.gameController.doCommand; // Mainly for debugging -- turns off creation of cases in DG dgApi.isEnabled = true; // Cases can be grouped into "Runs". Feel free to change to a better name. // I'm not sure where the best place to end a run is -- starting and stopping // the simulation doesn't seem like an appropriate place to end a run for instance. dgApi.runCount = 0; /** Initializes the communication between the model and DG. Passes information about the names of collection, names and types of attributes, etc. Quotes follow DG convention of using double quotes for user-focused strings and single quotes for strings used by the code. In general, changing a double-quoted string will affect what the user sees but not affect the operation of the program, whereas changing a single-quoted string can change the behavior of the program. Thus, changing the name of the game from "Simple Atoms Model" won't break anything, but changing 'initGame' to something else will prevent the proper initialization from occurring. */ dgApi.initGame = function() { if( !dgApi.doCommand) return; dgApi.doCommand.call( dgApi.gameController, { action: 'initGame', args: { name: "NetLogo GCC Data", dimensions: { width: 525, height: 625 }, collections: [ { // parent collection -- Runs name: "Runs", attrs: [ { name: "run", type: 'numeric', description: "The run number", precision: 0 } ], childAttrName: "run" }, { // Child collection -- Steps name: "Ticks", attrs: [ { name: "ticks", type: 'numeric', description: "Current tick", precision: 0 }, { name: "temperature", type: 'numeric', description: "Global Climate Temperature", precision: 2 } ], // default attributes for initial plot defaults: { xAttr: "ticks", yAttr: "temperature" } } ] } }); }; /** Begins a "Run", i.e. the collection of a set of related cases (Steps). Client should call endRun() when the Run is complete. */ dgApi.beginRun = function() { if( !dgApi.doCommand) return; var result = dgApi.doCommand.call( dgApi.gameController, { action: 'openCase', args: { collection: "Runs", // increment the runCount when we start a run values: [ ++dgApi.runCount ] } }); // Returns the ID of the parent case, which should be passed // in subsequent calls to create the child cases. if( result.success) dgApi.openRunID = result.caseID; return result; }; /** Ends a "Run". This is not currently called as I wasn't sure when to do so. */ dgApi.endRun = function() { if( !dgApi.doCommand) return; dgApi.doCommand.call( dgApi.gameController, { action: 'closeCase', args: { collection: "Runs", // Passes the case ID returned by the call to 'openCase' caseID: dgApi.openRunID } }); // Null out the openRunID so it can't get used inadvertently. dgApi.openRunID = null; }; /** Creates a DG case corresponding to the current state of the model. The model state supported currently is the five values returned by model.getStats(), but additional properties could be added as well. To do so, the corresponding attribute descriptions should be added to the 'initGame' call, and the code should be changed here to pass the corresponding value in the array passed to doCommand. @param {Object} iModel -- The current model state */ dgApi.addTick = function( data) { if( !dgApi.isEnabled || !dgApi.doCommand) return; dgApi.doCommand.call( dgApi.gameController, { action: 'createCase', args: { collection: "Ticks", parent: dgApi.openRunID, values: [ data.length, data[data.length-1] ] } }); }; // [/KCPT]