31
Declarative Approach to Implementing Automata Classes in Imperative Programming Languages Artyom Astafurov, SPbSUITMO [email protected] m Anatoly Shalyto, Second Spring Young Researchers’ Colloquium on Software Engineering May 29 th 2008

Declarative Approach to Implementing Automata Classes in Imperative Programming Languages Artyom Astafurov, SPbSUITMO [email protected] Anatoly

  • View
    241

  • Download
    0

Embed Size (px)

Citation preview

Declarative Approach to Implementing Automata Classes

in Imperative Programming Languages

Artyom Astafurov, [email protected] Shalyto, SPbSUITMO

Second Spring Young Researchers’ Colloquium on Software EngineeringMay 29th 2008

Before We Start

• Automata based programming?• OOP and Automata?• Imperative?• Declarative?• Our framework• Example

What is “Automata based programming”?

OOP and Automata

Foundation

EventA()EventB()

State1 State2

State1 EventB

EventA

State2

Nesting

State1

EventB

EventA

State2

State1A State1B

EventA

EventB

Inheritance

State1

EventB

EventA

DerivedState2

State1A State1B

EventA

EventB

What is “Imperative”?

• Explicit instructions• Steps to take• Answers the question “How?”

What is “Declarative”?

• Defines the end result• Doesn’t contain steps to achieve the result• Answers the question “What?”

This HTML code

<html>

Input: <input value="Hello, world!">

</html>

Can produce…

What’s the catch?

Hybrid approach!

Imperative for functionality and logicDeclarative for the structure

or

+[InnerState(“State1A”)]public class State1{

// Events go here}

Add some reflection or instrumentalization

Result: working code that doesn’t have explicit delegation of calls to nested automatons and

has all the magic behind the inheritance implemented

Example

In a nutshell:

IpodPlayer

Sleeping PlayerMode

Play

PlayHold

public interface IAcceptsPlay{ void Play();}public interface IAcceptsPlayHold{ void PlayHold();}public interface IIpodPlayer : IAcceptsPlay, IAcceptsPlayHold{ }

public class Sleeping : State, IAcceptsPlay{

public void Play(){

//...}

}

public class PlayerMode : State, IAcceptsPlayHold{

public void PlayHold(){

//...}

}

[InitialState(“Sleeping”), InnerState(“PlayerMode”)]public class IpodPlayer : Automaton, IIpodPlayer{

// Play and PlayHold stubs go here}

More complex now:

NewPlayerMode

Paused Playing

Play

Play

public class Paused : State, IAcceptPlay{

public void Play() {

Container.SetState(“Playing”); }}

public class Playing : State, IAcceptPlay{

public void Play() {

Container.SetState(“Paused”); }}

[InitialState(“Paused”),State(“Playing”)]public class NewPlayerMode : PlayerMode{

// That’s it!}

NewIpodPlayer

Sleeping

Play

PlayHold

NewPlayerMode

Paused Playing

Play

Play

And the new player is:

[State(“PlayerMode”, “NewPlayerMode”)]public class NewIpodPlayer : IpodPlayer{

// That’s it!}

Spasibo!