Substantial state API changes.

Moves Attribute identifiers (sort_priority, index_by, state_id) to
"static" class attributes. Furthermore, moves all nested class
definitions out into toplevel classes so that they can be more easily
pickled.