phetsims / energy-forms-and-changes

"Energy Forms And Changes" is an educational simulation in HTML5, by PhET Interactive Simulations.
MIT License
5 stars 5 forks source link

PhET-iO instrumentation #216

Open chrisklus opened 5 years ago

chrisklus commented 5 years ago

We are planning on having a design meeting for PhET-iO instrumentation of this sim while it's still fresh for @jbphet and me.

chrisklus commented 5 years ago

Whoops!

ariel-phet commented 5 years ago

@chrisklus is going to implement with basic tandems and then request a design meeting (current target sept 12 design meeting)

chrisklus commented 5 years ago

@ariel-phet when I said two weeks during our discussion I was intending for 09/05/19 design meeting, if that's a possibility. A dev test has been opened, so if nothing crazy comes from that then I'll be able to do initial tandem passing before Thursday next week.

chrisklus commented 5 years ago

Before instrumenting

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

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.

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.

Feature Support

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 https://github.com/phetsims/beers-law-lab/issues/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 to implement a proper toStateObject in the TypeIO will result in a hard fail when trying to send instances of that type to the wrapper side. The error will likely look something like this: "postMessage error from phetioCommandProcessor.send" (from phetioCommandProcessor.send()).

The Data Stream

Support dynamic state

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. 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".

Details about how to support dynamic state. Note: TANDEM/Group is under development in https://github.com/phetsims/phet-io/issues/1454 to make it easier to instrument dynamic elements. Until that is complete, Beer's Law Lab and Charges and Fields demonstrates how this may be done. A container class defines two methods: `clearChildInstances` which empties a container and `addChildInstance` which repopulates a container one element at a time. For example, see ShakerParticlesIO in the beers-law-lab instrumentation. When state is set, first the container is cleared, then children are created. Child states can be obtained from `toStateObject` and set back with `fromStateObject`, with an additional call to `setValue` in case additional data is supplied, or custom code can be used. Dispose must be implemented properly on all dynamic instances, or else it will result in stale values in the playback sim. For example, if a simulation is sending the position of a particle as a property, if the particle position property hasn't been disposed of, the simulation will try to create a new property with the same id and hence throw an assertion error because that tandem is already registered. On January 11, 2017 ControlPoints were not being disposed correctly in Energy-skate-park-basics, causing a mysterious bug (impossible set state), make sure that children are being disposed correctly before creating them in the downstream sim! Other tips and tricks for "impossible set state": * addChildInstance must return the instance, it is used as a flag to determine whether addition was successful * the given tandems must be reused. Do not use GroupTandem to assign a new tandem, use the specified tandem so the object can be addressed the same way 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.

Two types of serialization

Data type serialization For example, numbers, strings, Vector2 instances fall into this category. These values are instantiated by fromStateObject.

Reference type serialization For example, Nodes and Properties. For example, if a simulation has one heightProperty that exists for the lifetime of the sim then 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 the PhET-iO library calls setValue() to update the dynamic characteristics of the heightProperty without dealing with all of Property's many attributes. The static setValue methods on TypeIOs are automatically called by PhET-iO to restore dynamic characteristics of reference-type serialized instances. Search for toStateObject in *IO.js files for examples.

Post Instrumentation and Checks

Tips, Tricks, Notes, Misc

Review and Publication

Maintaining

chrisklus commented 5 years ago

Basic tandem passing is complete on branch phetioInstrumentation.

chrisklus commented 5 years ago

First dev version: https://phet-dev.colorado.edu/html/energy-forms-and-changes/1.2.0-phetioInstrumentation.1/phet-io/wrappers/studio/

Some things I've noticed to discuss for 09/12/19 design meeting:

Intro Screen

Systems Screen

chrisklus commented 5 years ago

We will investigate adding a pre-launch dialog to select which elements you want to include, then update the model to respect those query params. These query params would actually remove items when setting up the sim at startup, not just hide them.

@zepumph suggested moving this customization as an optional menu dialog located in the sim along with a button to relaunch the sim once the customizations are applied. This would allow for the customization to be confined to one place, instead of every entry point for the sim. In Studio, we would need to find a way to relaunch Studio, not just the sim itself.

This pattern would also make customizing for phet brand simpler, because the website's sim page wouldn't need to know about which query params are available for a given sim version.

We'll bring this up in design meeting and discuss further.

samreid commented 5 years ago

@zepumph what do you think about bypassing the normal sim construction and going straight to the query parameter UI via a query parameter, like: sim.html?configure . This would still version and deploy within the sim HTML.

zepumph commented 5 years ago

That sounds like it is worth investigation! In the sim feels like it is most robust, and will be easiest to maintain. I still reserve the right to be outvoted if people think that this belongs outside the sim.

chrisklus commented 5 years ago

@zepumph what do you think about bypassing the normal sim construction and going straight to the query parameter UI via a query parameter, like: sim.html?configure . This would still version and deploy within the sim HTML.

Would this menu delay normal startup of the sim until the configure menu was submitted? If so, it seems nice that the sim wouldn't have to be reloaded for the customization to take place. It feels a little weird that you add a query parameter in order to add more query parameters, but I can picture an alternate link that would be labeled for customizations, so the client still doesn't need to know about query parameters.

samreid commented 5 years ago

How about a separate HTML page that is deployed alongside the sim HTML?

chrisklus commented 4 years ago

Latest dev: https://phet-dev.colorado.edu/html/energy-forms-and-changes/1.2.0-dev.6/phet-io/

Some features added since last comment:

chrisklus commented 4 years ago

Work done since 11/20/19 phet-io design meeting:

Notes for future work:

Latest dev:https://phet-dev.colorado.edu/html/energy-forms-and-changes/1.2.0-dev.7/phet-io/

jbphet commented 4 years ago

EFAC with phet-io was published in 1.4.0. I think a lot of things can probably be checked off in this issue now, and we should consider just closing it once it has been updated.

jbphet commented 4 years ago

Oops, I didn't mean to actually close it. It should probably be updated a bit before we do.