Version0 /
1aAccessorsSpecificationThis page describes the specification for version 0.1a accessors. It is a working document. For background on accessors, see the Accessors white paper. An accessor specification has up to four parts: an interface, documentation, functionality, and composition, as explained below. Known implementations of hosts supporting this specification are: Accessors that are to be checked in the TerraSwarm A minimal accessor that takes a numeric input, doubles it, and sends the result to an output is given below: exports.setup = function () { input('input'); output('output'); }; exports.fire = function () { send('output', get('input') * 2); }; This accessor has two of the four parts, an interface definition in the exports.setup = function () { input('input'); output('output'); }; exports.initialize = function () { addInputHandler('input', function () { send('output', get('input') * 2); }); }; In this case, the function to be performed is given by an input handler function. More interesting examples are found on the TerraSwarm accessor repository. TO DO: Out of the box, JSLint and JSHint generate warnings for the above. We need to figure out how to ensure that it knows about top-level functions. This can be done in the file by adding InterfaceAn accessor interface specifies what it requires of an accessor host and what its inputs, outputs, and parameters are. An accessor interface definition may include the following elements in the accessor specification file:
DocumentationDocumentation and metadata such as author, version, etc., are given using JSDoc tags in comments in the JavaScript accessor specification file. For example, the above minimal accessor might be documented as follows: /** Double the value provided at *input* and send to *output*. * @accessor Minimal * @module Minimal * @author Edward A. Lee (eal@eecs.berkeley.edu) * @input {number} input A numeric input. * @output {number} output The output for the doubled value. */ The main body of the documentation is given after the first
See the Accessor JSDoc page for how to invoke JSDoc. FunctionalityA component accessor has an interface and some functionality given in JavaScript. The can define local state variables used by the accessor and functions to be invoked by the swarmlet host. The accessor's script will be executed in an accessor context provided by the host. The context provides some built-in functions and modules that are available for accessors to use. A swarmlet host must provide all the built-in functions and modules and may provide some or all of the optional modules. The set of required functions and modules is deliberately small, so that implementing a basic accessor host is easy.
CompositionThe contract between an accessor and its environment is one of the key questions that this research raises. In the Ptolemy II/Nashorn host, we use the discrete-event director to govern the interactions between an accessor and its environment. This has a number of attractive consequences. One is that setTimeout and setInterval are precise. For example, if two accessors produce data with the same interval using setInterval, then any downstream accessor will see data from these two accessors simultaneously. Also, if two uses of setTimeout have different time values, the order of invocation is guaranteed. This is not usually true in JavaScript programming environments. In the DE model of computation, all interactions between actors is via time-stamped events. The contract is that each actor will see events in time-stamp order. If there are multiple events with the same time stamp, then an actor will see them simultaneously (in the same handler or fire invocation). By default, the DE director in Ptolemy II does not synchronize with real time. It runs as quickly as possible, so time stamps will typically advance faster than real time. If you double click on the director and select synchronizeToRealTime, then the execution will slow so that time stamps do not advance more rapidly than real time. It may also be useful to uncheck stopWhenQueueIsEmpty so that the execution does not stop prematurely. This is useful, for example, when setTimeout() is being used to trigger an action in the future. For pure Nashorn, Node, or browser hosts, it remains an open question whether DE semantics should be required as part of the accessor definition. |