-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PhET-iO Instrumentation #138
Labels
Comments
jonathanolson
added a commit
to phetsims/tandem
that referenced
this issue
Jan 31, 2020
jonathanolson
added a commit
that referenced
this issue
Jan 31, 2020
jonathanolson
added a commit
to phetsims/perennial
that referenced
this issue
Feb 4, 2020
Tandems are validating, and I'm at a point where I'll want a phet-io meeting or a review from a phet-io developer before continuing. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to Instrument a PhET Simulation for PhET-iO
Before instrumenting
description (top issue comment) for tracking. Link back to this checklist via
/blob/<SHA>/
so that the specific guideyou used is preserved. (https://github.com/phetsims/phet-io/blob/afa18dc81d5054235cafe4063b470fc38ac1be52/doc/how-to-instrument-a-phet-simulation-for-phet-io.md)
have been introduced during instrumentation, or were pre-existing. This also creates a benchmark to reference against
for memory-leaks, sim size, performance, etc. Document the dev release in the sim's phet-io github issue.
including the link to Graphing Quadratics 1.1. Visit all of the linked wrappers and docs. Test each wrapper,
investigate, report bugs, ask questions!
PhetioObject
is a class in thetandem
repo. All instrumented instances arePhetioObject
s.PhetioObject
that is interoperable across the PhET-iO messageframework. So every PhET-iO element is a
PhetioObject
in code, but not allPhetioObject
s are PhET-iO elements. Itmust have a
Tandem
(and hence a unique string name called aphetioID
) and aphetioType
dictating how toserialize the
PhetioObject
across the frame. In the PhET-iO Studio wrapper, every interoperable object in the treeis a PhET-iO element.
PhetioObject
instantiated in the sim along with itsassociated TypeIO. For example,
SCENERY/Node
extendsPhetioObject
and its default TypeIO isNodeIO
. Messagescan be sent to the object via its IO type to customize it.
it can be good to familiarize yourself with basic wrapper making code to understand how PhET-iO is meant to be used.
To get started see the template example in the wrapper index.
track it in an issue. For example, see how-to-design-phet-io-features-for-a-simulation.md.
Think about how a researcher or 3rd party may wish to configure the simulation or collect data from it, and make sure
that is supported by the instrumentation. For example, some simulations will need custom higher-level events (such as
whether the user created a parallel circuit), for events that are useful, easy to compute in simulation code and
difficult to compute in wrapper code. Or a simulation may need to be configurable in a way that is not already supported
by the instrumentation you have already completed. These features should be determined in the PhET-iO design meeting.
Sometimes it is preferred to have a skeleton, or developer's "best guess" before this meeting so that there is more to
play with in studio. Use your judgement!
insight into the structure, history, trade-offs and other important details of the simulation implementation
that will facilitate the instrumentation. If the responsible developer is not available for instrumentation, even a
consulting role would be helpful.
Code Review
A high-quality code review will make instrumentation easier, promote long term maintainability for the
simulation, and protect the simulation from a volatile API. If the simulation is already in good shape, the review
will not take too long. If the simulation is not in good shape, then it needs your help.
Instrumentation
Now that the simulation is in good shape and the PhET-iO design meeting is complete, we are ready to instrument the simulation.
Follow the checklist below, and if you have questions you can review Faraday's Law or Graphing Quadratics and their
PhET-iO instrumentations, or reach out to teammates who may have come this way before.
Initial Setup
package.json
. The~/github/perennial$ grunt generate-data
script on Bayes will automatically add the simulation to the list of phet-io simulations. This will make it possible to
use phetmarks to launch wrappers for testing. This also will add it to continuous fuzz testing. If this process is
offline (if it doesn't automatically update within 10 minutes), you can run the task yourself and reach out to a member
of the PhET-iO subteam to see if the process is failing. More documentation is available in PERENNIAL/generateData.js
grunt update
in the sim repo. This will generate the needed phet-io api preload files (baseline + overrides)to run in phet-io brand. Note that while validating the api, these preloads will need to be kept in sync with the current
state of the repo. This may be more trouble than it's worth while iterating. You can use
?phetioValidateAPI=false
toturn off this validation during iteration. However, if you are iterating in studio, you must have an up-to-date baseline
file, since studio leverages it for metadata.
Tandem
tomain.js
, seefaradays-law-main.js
for an example.tandem
instances to each screen usingtandem.createTandem(...)
instrumentation. It displays a list of all "PhET-iO" elements and has controls to interoperate with them. Please note
that studio does not demonstrate the entire suite of phet-io features, and thorough testing of all wrapper suite wrappers
is vital to understanding the intricacies of the instrumentation process and goals (see wrapper index for entire list).
You can remove the
?phetioValidateTandems
query string to temporarily get past "Tandem was required but not supplied" issues.Visit Objects that Should be Instrumented
Consult the PhET-iO design issue to see what features the sim should support. See
PhetioObject.js for the
supported PhetioObject options. Not every node in the hierarchy must be instrumented, but every leaf is instrumented.
For example the
view
is rarely instrumented.tandem
s and otherPhetioObject
options into objects that should be instrumented. Do notinstrument objects that are "implementation details" and do not over-instrument. The goal is to design an API that
balances the power of a broad feature set while still being maintainable.
This does not necessarily include "implementation details" that should be hidden from the public API; again a design
meeting may be needed here. (Note that some Property sub-classes utilize options specific for use with PhET-IO,
units
in
NumberProperty
for example, and should be passed where appropriate.)PhetioObject
when you need to add features not already covered by existing types. Be careful not toshadow pre-existing attributes in
PhetioObject
such astandem
,isDisposed
, andlinkedElements
.Creating and Naming Tandems
Well-designed tandem names are important. Once the PhET-iO simulation is published, the API becomes public and therefore
difficult to change. Sometimes PhET-iO design meetings can also help come up tandem names. NOTE: "Tandem" is a PhET
internal name, publicly to clients the full strings are known as "phetioIDs" referring to PhET-iO elements.
vars should be renamed to match tandem names. Tandem names for some PhET-iO Objects such as Properties and Screens
must end with the appropriate suffix, and are checked automatically with assertions.
model
andview
.tandem args from Should tandem be in options or required param? joist#489 (comment):
@param tandem
for constructors that don't have an options parameter. This typically includes top-level modeland view types that are specific to the sim.
Tandem.REQUIRED
for constructors that already have an options parameter. This defaultcan be helpful for identifying cases where you have neglected to pass a tandem in (because
Tandem.REQUIRED
will errorloudly if validating tandems). If a tandem is used transitively (by a dependency) but not in a class itself, then
the option can be declared in the dependency and does not need to be repeated in the class itself. Sometimes including
the tandem option can be clearest, even if it is not used in the class (for example, to indicate whether something should be
instrumented).
PhetioGroup
orPhetioCapsule
. See note belowon dynamic elements.
See BeersLawSolution.js for an example.
global
section, seeTandem.GLOBAL
. Tandemsin the
general
section are the same for all simulations, likeactiveProperty
.Feature Support
Tandem
instance, eachPhetioObject
should be provided a TypeIO. The TypeIO for aPhetioObject
indicates the public api for thatPhetioObject
instance. Most instrumented common code Types alreadyhave a TypeIO provided as a default option for
phetioType
.Property
instances to make it possible to get/set a value, so valuechanges will appear on the data stream and so the item can be stored and restored in save/load. This is preferred to
creating a new TypeIO and implementing get/set within that type.
validator
static attribute that can be used to validate the type. When instrumenting aProperty
,Emitter
, or other type that validates parameters in which that instance providedvalueType
for validation,in most cases the TypeIO's
validator
will be redundant to thevalueType
field. If this is the case, thevalueType
can and should be removed to keep the code simpler and more maintainable.
by checking whether it extends PhetioObject and whether it supplies any PhetioObject options. To instrument a new common
code component, you may need to add instrumented
Property
orEmitter
elements by composition, or subclassPhetioObject
.Run phetmarks=>aqua=>Test Sims(Fast Build) with PhET-iO checked. This will help catch any simulations using the component
you just instrumented.
tandem: Tandem.REQUIRED
ortandem: Tandem.OPTIONAL
to the options accordingly. Here are some conventions toguide this decision:
Tandem.REQUIRED
at the subtype level in sim specific code too.
tandem: Tandem.REQUIRED
to default options wherever youintend to pass tandem via options.
Node
already extendsPhetioObject
--itsPhetioObject
options should be provided to the constructor ormutate
but not both.
phetioPrintMissingTandems
flag if you want to collect a list of all required, optional, and uninstrumentedcommon code classes instead of erroring out on the first missing tandem. Each occurrence is numbered to give a better
idea of how many the sim has to do.
the About dialog is showing should not be part of the save state of the simulation. Omit these instances from state with
phetioState:false
.phetioDocumentation
option:view coordinates" is appropriate even though it is just a subject.
phetioDocumentation
that is client facing to start with a capital letter.parameters support tambo but not vibe.
DEFAULT_OPTIONS
or at least be very careful about how it is done, see the concernsmentioned in https://github.com/phetsims/phet-io/issues/1179
Property
viaaddLinkedElement
Create new TypeIOs
If necessary, create new TypeIOs to support desired feature set. Generally we don't want to be locked in to coupling
TypeIOs to sim types. Instead, we decided that we want the PhET-iO API to be able to vary independently from the sim
implementation instead of leaking sim implementation details (like MultilineText vs Text should both just be TextIO).
Still, for a well-designed simulation, TypeIOs will often match closely with the sim types. To ensure good IO type
inheritance hierarchies follow these principles:
See phetsims/beers-law-lab#213 for more context on prior problems in this area and discussion
about it.
Also note that the since work completed in https://github.com/phetsims/phet-io/issues/1398, PhET-iO interframe
communications run on structured cloning (via
PostMessage
), and not just JSON strings. This means that a failure toimplement a proper
toStateObject
in the TypeIO will result in a hard fail when trying to send instances of that typeto the wrapper side. The error will likely look something like this: "Something in message was uncloneable when sent
with PostMessage."
The Data Stream
Emitter
instances as appropriate to augment the data stream.Emitters
andProperty
instances naturally emit to a structured data stream and are probably what you need.If you need something more custom, you can call
PhetioObject.phetioStartEvent
andPhetioObject.phetioEndEvent
directly.See https://github.com/phetsims/phet-io/issues/282
Emitter.addListener
instead ofEvents.onStatic
Emitter.emit
argument, see thephetioPrivate
option inAction.js
.phetioEventType: 'user'
for pointer events, keyboard events and UI events(like checkbox toggled, button pressed), and
phetioEventType: 'model'
for model actions/responses. This is easiest totest in the console: colorized wrapper. Model events will be logged in black, and user events will be logged blue. You can also
go to the data-stream wrapper to see events in JSON form. If your simulation only leverages existing model types (like
Property/Emitter) and UI types (like sun components), then you will not be instrumenting new types.
Dynamically created PhET-iO Elements
For simulations that have static content (such as a fixed number of objects and properties), instrumentation
is mostly complete and you can skip this section. Not all objects in a phet sim are created on startup. For PhET-iO
features and instrumentation, it is much easier to support interoperability for statically created PhET-iO elements,
but we also support dynamic elements. For simulations that have a dynamic number of objects, such as Circuit
Construction Kit circuits or Molecules and Light photons, the containers and elements must be instrumented.
This is currently tricky with PhET-iO. Some sims may wish to avoid this entire hassle by pre-allocating all of the
instrumented instances. Consider adding flags to indicate whether the objects are "alive" or "in the pool".
Dynamic elements are now supported through
PhetioGroup
andPhetioCapsule
. These container classes manage validation,api tracking, and dynamic state for their dynamic element(s).
Here is an ordered list of how to approach instrumenting an element that is dynamically created:
a component of their parent (instead of being instrumented themselves)
when supporting PhET-iO state and API validation.
PhetioGroup
andPhetioCapsule
. If you have a use case that is notaddressed by one or both of those, then please consult with the PhET-iO subteam, and potentially create a new IO type
suitable for your simulation.
Model vs View dynamic elements
When instrumenting dynamic elements, there are two common cases that the PhET-iO has run into in terms of MVC state
and serialization support. The first is that the model items need to be instrumented, but the view does not hold any
extra data, and so it doesn't need PhET-iO instrumentation. In this case create a
PhetioGroup
(orPhetioCapsule
)for the model, and have the view just listen to the creation and disposal of that element—as should likely already be
occurring. A great example of this is john-travoltage, where there are electrons in the model, but the view elements
just listen to their respective model element.
If the view for a model element does need to be instrumented, then it is recommended to set up the instrumentation in
a specific way.
PhetioGroup
or another container, as they are interoperable dynamic elements.Instead of being instrumented with a a special IO type though, instrument them as
ReferenceIO
, such that the stateengine won't be responsible for recreating them when setting the state.
elements are created and disposed. So the view should just clear when the model
PhetioGroup
is cleared (first step ofPhET-iO setting state). Then each new model element dynamically created should trigger a view element to be created.
This is not special PhET-iO code, but rather just the same sim code that MVC normally relies on.
The PhET-iO team found success with this pattern while working on charges-and-fields. See that sim for example usages of
PhetioGroup
.Dispose
Dispose must be implemented properly on all dynamic instances, or else it will result in stale values in the
downstream sim. If this is not done correctly, the most common errors will be "phetioID already registered" and
"PhetioObject can only be disposed once" errors.
Dispose functions must be added to types that are instrumented. But that's only half of the memory management issue. The
other half is revisiting memory management for all instances that don't exist for the lifetime of the sim, and verifying
that tandems are properly cleaned up.
PhET-iO State
The state of the simulation has a few uses. Its main purpose is to support saving and loading the simulation. The
state of the simulation should be able hold all useful data to get a sim into a useful state. To test the state feature,
you can use the "Launch" button in studio, which loads a fresh copy of the sim, and sets the state of the studio
customized sim to it. Also in the state wrapper you can play with the upstream sim, and see that state set to the dummy
downstream simulation. In both cases, setting the state not only sets the current values, but it also sets the initial
values so that resetting the sim/scene/Property will return to the configured initial value rather than the default
un-customized simulation state.
PhetioObjects supporting state are serialized via their IO type's
toStateObject
method. This converts their state toJSON. The
fromStateObject
deserializes the components of state object (still returning an object literal). Anadditional call to
setValue
can be done if deserializing a reference type. SeeTANDEM/ObjectIO
for more documentation.For PhET-iO state we often refer to the sim instance that is creating the state as the "upstream" sim, and the sim
instance that is having state set to it as the "downstream" sim. Note that these could be the same runtime, with a
getState
call followed by asetState
call.Two types of (de)serialization
Data type serialization For example numbers, strings, Vector2 instances fall into this category. These values
are instantiated and returned by
fromStateObject
.Reference type serialization For example Nodes and Properties. If a simulation has one
heightProperty
that exists for the lifetime of the sim, when we save the state of the sim, we save the dynamic characteristics of
the
heightProperty
(rather than trying to serialize the entire list of listeners and phet-io metadata). Then thePhET-iO library calls
setValue()
to update the dynamic characteristics of theheightProperty
without dealing withall of Property's many attributes. The static
setValue
methods on TypeIOs are automatically called by PhET-iO torestore dynamic characteristics of reference-type serialized instances. In this case, the
fromStateObject
will notreturn an instantiated data type, but instead a new object with deserialized components (like a Property's value); this
can then be used by
setValue
.Search for
toStateObject
in *IO.js files for examples of both types.Using Studio to specify metadata
Once the simulation is sufficiently instrumented, tandems are mostly stabilized and appropriate metadata is specified in
the code, PhET-iO designers can use studio to specify any remaining metadata.
PhET-iO Studio Instructions describes
how to run studio to generate an override file. Some metadata should be provided through the code (like when something
really should be phetioReadOnly because it is read only in the code).
Post Instrumentation and Checks
dispose
d, which unregisters the tandem.dt
values are used instead ofDate.now()
or other Date functions. This is necessary forreproducible playback via input events. Perhaps try
phet.joist.elapsedTime
.phet.joist.random
, and all doing so after modules are declared (non-statically)? Forexample, the following methods (and perhaps others) should not be used:
Math.random
,_.shuffle
,_.sample
,_.random
.undefined
values are omitted in the state--consider this when determining whethertoStateObject
should usenull
orundefined
values.difference between the state at startup and the current state of the sim. Before continuing make sure that the changed
state makes sense. If it looks like initial values are leaking into the changed state, then it is possible that initialization
has not completed by the time the sim says that construction has ended. In most cases this is a code smell, and could
also be a sneaky bug because we want to make sure that by the time the wrapper gets the onSimInitialized callback, that
the sim has actually been initialized. If there is animating on startup, which causes changed state, that is expected an
alright. See https://github.com/phetsims/phet-io/issues/1555 for more discussion.
http://localhost/phet-io-wrappers/index/?sim={{simulation-name}}
and test all the links. To further understand what eachwrapper exemplifies, read the description for it in the wrapper index, and launch that wrapper with a sim already
completely PhET-iO instrumented like Faraday's Law.
grunt --brands=phet-io
and test the built version by launching the compiled wrapper index atbuild/phet-io/
, and testing all the links.tandem disposal. PhET-iO instantiates different objects and wires up listeners that are not present in the PhET-branded
simulation. It needs to be tested separately for memory leaks. Use
?ea&brand=phet-io&phetioStandalone&fuzz
torun with assertions, PhET-iO brand and fuzzing.
component you just instrumented. Next you will need to pass tandems for those cases.
Tips, Tricks, Notes, Misc
reason that testing without iframes, using the `Data: colorized" wrapper is sometimes preferable.
see https://github.com/phetsims/phet-io/issues/107
a while of testing it can be annoying to have the extra step:
phetmarks --> index --> desired wrapper
. Instead youcan use phetmarks to launch any individual wrapper. Note that the wrapper index in the build version is at the top level
of the build dir (
build/phet-io/
).Review and Publication
Maintaining
PhET-iO api after it has been created.
Happy instrumenting!
The text was updated successfully, but these errors were encountered: