First-Class State Change in Karl Naden with Joshua Sunshine, Sven - - PowerPoint PPT Presentation

first class state change in
SMART_READER_LITE
LIVE PREVIEW

First-Class State Change in Karl Naden with Joshua Sunshine, Sven - - PowerPoint PPT Presentation

First-Class State Change in Karl Naden with Joshua Sunshine, Sven Stork Jonathan Aldrich, and ric Tanter OOPSLA 10/27/2011 School of Computer Science States and State Change Things all around us are changing state Butterfly: Egg


slide-1
SLIDE 1

First-Class State Change in

Karl Naden with Joshua Sunshine, Sven Stork Jonathan Aldrich, and Éric Tanter OOPSLA 10/27/2011

School of Computer Science

slide-2
SLIDE 2
  • Things all around us are changing state
  • Butterfly: Egg  Caterpillar  Chrysalis  Imago

States and State Change

1

slide-3
SLIDE 3
  • Things all around us are changing state
  • Butterfly: Egg  Caterpillar  Chrysalis  Imago
  • You: Awake  Asleep

States and State Change

1

slide-4
SLIDE 4
  • Things all around us are changing state
  • Butterfly: Egg  Caterpillar  Chrysalis  Imago
  • You: Awake  Asleep
  • In Programming, too
  • File: Open  Closed

States and State Change

1

slide-5
SLIDE 5
  • Things all around us are changing state
  • Butterfly: Egg  Caterpillar  Chrysalis  Imago
  • You: Awake  Asleep
  • In Programming, too
  • File: Open  Closed
  • Java Exception: Cause Not Set  Cause Set

States and State Change

1

slide-6
SLIDE 6
  • Things all around us are changing state
  • Butterfly: Egg  Caterpillar  Chrysalis  Imago
  • You: Awake  Asleep
  • In Programming, too
  • File: Open  Closed
  • Java Exception: Cause Not Set  Cause Set
  • Different abilities depending on the state
  • Butterfly can only fly as an Imago
  • read() only available in the Open state of File

States and State Change

1

slide-7
SLIDE 7
  • Things all around us are changing state
  • Butterfly: Egg  Caterpillar  Chrysalis  Imago
  • You: Awake  Asleep
  • In Programming, too
  • File: Open  Closed
  • Java Exception: Cause Not Set  Cause Set
  • Different abilities depending on the state
  • Butterfly can only fly as an Imago
  • read() only available in the Open state of File
  • Error when the current state does not support the action

States and State Change

1

slide-8
SLIDE 8

Describing Objects with State

2

slide-9
SLIDE 9
  • An Object Protocol [Strom, Yemini ’86] dictates an order on

method calls:

  • Has a finite number of abstract states in which different

method calls are valid;

  • Specifies transitions between abstract states that occur as

a part of some method calls.

Describing Objects with State

2

slide-10
SLIDE 10
  • An Object Protocol [Strom, Yemini ’86] dictates an order on

method calls:

  • Has a finite number of abstract states in which different

method calls are valid;

  • Specifies transitions between abstract states that occur as

a part of some method calls.

  • State Charts [Harel ’87]: File
  • States: File, Open, Closed
  • Methods: read(), open(),

closed()

  • Transitions: close(), open()

Describing Objects with State

2

slide-11
SLIDE 11

Existing Support for States

3

slide-12
SLIDE 12
  • States exists
  • In documentation

Existing Support for States

3 /** @throws IllegalStateException if task was already scheduled or

* cancelled, timer was cancelled, or timer thread terminated. */ private void sched(TimerTask task, long time, long period) { … }

slide-13
SLIDE 13
  • States exists
  • In documentation
  • Encoded as lower-level constructs

Existing Support for States

3 /** @throws IllegalStateException if task was already scheduled or

* cancelled, timer was cancelled, or timer thread terminated. */ private void sched(TimerTask task, long time, long period) { … } if (task.state != TimerTask.VIRGIN) throw new IllegalStateException(…);

slide-14
SLIDE 14
  • States exists
  • In documentation
  • Encoded as lower-level constructs
  • Problems
  • States from design are obfuscated
  • Code difficult to understand
  • If checks forgotten, results difficult to debug, e.g.
  • Non-specific NullPointerException
  • Data corruption (overwritten TimerTask)

Existing Support for States

3 /** @throws IllegalStateException if task was already scheduled or

* cancelled, timer was cancelled, or timer thread terminated. */ private void sched(TimerTask task, long time, long period) { … } if (task.state != TimerTask.VIRGIN) throw new IllegalStateException(…);

slide-15
SLIDE 15

Common and Complex

4

slide-16
SLIDE 16
  • Common *Beckman ‘11+
  • 7% of Java classes define object protocols
  • 3x as many as define generics
  • 13% use them

Common and Complex

4

slide-17
SLIDE 17
  • Common *Beckman ‘11+
  • 7% of Java classes define object protocols
  • 3x as many as define generics
  • 13% use them
  • Complex:

Common and Complex

4

slide-18
SLIDE 18

Overview

  • programming language

–First class states and transitions

5

slide-19
SLIDE 19

Overview

  • programming language

–First class states and transitions

  • Overview

–Syntax and semantics of states and transitions –Trait-based reuse

5

slide-20
SLIDE 20

Two Encodings of File

6

slide-21
SLIDE 21

Two Encodings of File

6

class File { private FileResource filePtr = null; }

1) States

slide-22
SLIDE 22

Two Encodings of File

6

class File { private FileResource filePtr = null; }

1) States substate of file determined by null-ness of filePtr field

slide-23
SLIDE 23

Two Encodings of File

6

state Closed case of File { } state Open case of File { } class File { private FileResource filePtr = null; } state File { }

1) States

slide-24
SLIDE 24

Two Encodings of File

6

state Closed case of File { } state Open case of File { } class File { private FileResource filePtr = null; pubic int read() {…} public void close() {…} public void open() {…} } state File { }

2) Methods

slide-25
SLIDE 25

Two Encodings of File

6

state Closed case of File { method open() ,…- } state Open case of File { method read() ,…- method close() ,…- } class File { private FileResource filePtr = null; pubic int read() {…} public void close() {…} public void open() {…} } state File { }

2) Methods

slide-26
SLIDE 26

Two Encodings of File

6

state Closed case of File { method open() ,…- } state Open case of File { method read() ,…- method close() ,…- } class File { private FileResource filePtr = null; pubic int read() { if (filePtr == null) throw new IOException else … } public void close() {…} public void open() { if (filePtr == null) {…} } } state File { }

2) Methods

slide-27
SLIDE 27

Two Encodings of File

6

state Closed case of File { method open() ,…- } state Open case of File { val filePtr; method read() ,…- method close() ,…- } class File { private String filename; private FileResource filePtr = null; pubic int read() { if (filePtr == null) throw new IOException else … } public void close() {…} public void open() { if (filePtr == null) {…} } } state File { val filename; }

3) Representation

slide-28
SLIDE 28

Two Encodings of File

6

state Closed case of File { method open() ,…- } state Open case of File { val filePtr; method read() ,…- method close() ,…- } class File { private String filename; private FileResource filePtr = null; pubic int read() { if (filePtr == null) throw new IOException else … } public void close() {…; filePrt = null;} public void open() { if (filePtr == null) { filePtr = … } } } state File { val filename; }

4) Transitions

slide-29
SLIDE 29

Two Encodings of File

6

state Closed case of File { method open() { this  Open { val filePtr = … -; } } state Open case of File { val filePtr; method read() ,…- method close() ,…; this  Closed;} } class File { private String filename; private FileResource filePtr = null; pubic int read() { if (filePtr == null) throw new IOException else … } public void close() {…; filePrt = null;} public void open() { if (filePtr == null) { filePtr = … } } } state File { val filename; }

4) Transitions

slide-30
SLIDE 30
  • encoding advantages:

– Design salient in the code – Succinct: fewer explicit checks – Errors handled safely and informatively

Comparison

7

slide-31
SLIDE 31

Reuse and Composition

  • Goal: flexibility of Traits [Ducasse ‘06+ with benefits of

protocols

– Break protocols up into elemental protocols – Compose them 8

slide-32
SLIDE 32

Reuse and Composition

  • ReadStream = Position + Reader

9

slide-33
SLIDE 33

Reuse and Composition

  • ReadStream = Position + Reader

9 +

slide-34
SLIDE 34

Reuse and Composition

  • ReadStream = Position + Reader

9 + Protocols in two separate dimensions

slide-35
SLIDE 35

Reuse and Composition

  • ReadStream = Position + Reader

9

state Reading { method read() { /* read character */ this.next(); } }

+

slide-36
SLIDE 36

Reuse and Composition

  • ReadStream = Position + Reader

9

state Reading { method read() { /* read character */ this.next(); } } state NotEnd case of Position { method next() { /* move position forward */ if (/* at the end */) { this  End } } }

+

slide-37
SLIDE 37

+

Reuse and Composition

10

val rs = new Reading with NotEnd { val coll = *1,2+; … -;

1 2

slide-38
SLIDE 38

+

Reuse and Composition

10

val rs = new Reading with NotEnd { val coll = *1,2+; … -; rs.read();

1 2

slide-39
SLIDE 39

+

Reuse and Composition

10

val rs = new Reading with NotEnd { val coll = *1,2+; … -; rs.read(); //no state change

1 2

slide-40
SLIDE 40

+

Reuse and Composition

10

val rs = new Reading with NotEnd { val coll = *1,2+; … -; rs.read(); //no state change rs.read();

1 2

slide-41
SLIDE 41

+

Reuse and Composition

10

val rs = new Reading with NotEnd { val coll = *1,2+; … -; rs.read(); //no state change rs.read(); // this  End

1 2 No change in the Reader dimension!

slide-42
SLIDE 42

State Members

11 Solution: State Members allow the incoming state to be determined dynamically

slide-43
SLIDE 43

State Members

11

state Position { val endState = End; /* … */ }

Solution: State Members allow the incoming state to be determined dynamically

slide-44
SLIDE 44

State Members

11

state Position { val endState = End; /* … */ }

Solution: State Members allow the incoming state to be determined dynamically

state NotEnd case of Position { method next() { /* … */ if (/* at the end */) { this  this.endState } } }

slide-45
SLIDE 45

+

Reuse and Composition

12

val rs = new Reading with NotEnd { val coll = *1,2+; …; val endState = ReadEnd with End; }; rs.read(); //no state change rs.read();

1 2

slide-46
SLIDE 46

+

Reuse and Composition

12

val rs = new Reading with NotEnd { val coll = *1,2+; …; val endState = ReadEnd with End; }; rs.read(); //no state change rs.read(); // this  ReadEnd with End

1 2 Both dimensions change together!

slide-47
SLIDE 47

Reuse

  • ReadStream = Reader + Position

13 +

state Reading { method read() { /* read character */ this.next(); } } state ReadStream = Position { val endState = ReadEnd with End; } with Reader

slide-48
SLIDE 48

Reuse

  • Reuse as a WriteStream = Writer + Position

state Writing { method write() { /* write character */ this.next(); } } state WriteStream = Position { val endState = WriteEnd with End; } with Writer

+ 14

slide-49
SLIDE 49

Reuse

  • Or as a ReadWriteStream = Reader + Writer + Position

state ReadWriteStream = Position { val endState = ReadEnd with WriteEnd with End; } with Reader with Writer;

+ 15 +

slide-50
SLIDE 50

Conclusion

  • Object Protocols in Plaid are

– More concise, understandable, and safer – Reusable and composable

16

slide-51
SLIDE 51

Conclusion

  • Object Protocols in Plaid are

– More concise, understandable, and safer – Reusable and composable

  • Open up new possibilities

– Visualization tools – More helpful error messages – Static checking

16

slide-52
SLIDE 52

Conclusion

  • Object Protocols in Plaid are

– More concise, understandable, and safer – Reusable and composable

  • Open up new possibilities

– Visualization tools – More helpful error messages – Static checking

– Demo, 2pm Galleria III

16