+ All Categories
Home > Technology > Subjective-C: Bringing Context to Mobile Platform Programming

Subjective-C: Bringing Context to Mobile Platform Programming

Date post: 28-Nov-2014
Category:
Upload: kimmens
View: 1,308 times
Download: 1 times
Share this document with a friend
Description:
Thanks to steady advances in hardware, mobile computing platforms are nowadays much more connected to their physical and logical environment than ever before. To ease the construction of adaptable applications that are smarter with respect to their execution environment, the context-oriented programming paradigm has emerged. However, up until now there has been no proof that this emerging paradigm can be implemented and used effectively on mobile devices, probably the kind of platform which is most subject to dynamically changing contexts. In this presentation we show how to effectively achieve core context-oriented abstractions on top of Objective-C, a mainstream language for mobile device programming. The result is Subjective-C, a language which goes beyond currently existing context-oriented languages by providing a rich encoding of context interdependencies. Our initial validation cases and efficiency benchmarks make us confident that context-oriented programming can become mainstream in mobile application development.
63
Subjective-C Bringing Context to Mobile Platform Programming ICTEAM / UCLouvain / Belgium Alfredo Cádiz Jean-Christophe Libbrecht Julien Goffaux Software Language Engineering Sebastián González Nicolás Cardozo Kim Mens 3rd International Conference on 13 October 2010 Thursday 21 October 2010
Transcript
Page 1: Subjective-C: Bringing Context to Mobile Platform Programming

Subjective-CBringing Context to Mobile Platform Programming

ICTEAM / UCLouvain / Belgium

Alfredo Cádiz Jean-Christophe Libbrecht

Julien Goffaux

Software Language Engineering

Sebastián GonzálezNicolás Cardozo

Kim Mens

3rd International Conference on

13 October 2010Thursday 21 October 2010

Page 2: Subjective-C: Bringing Context to Mobile Platform Programming

Subjective-CBringing Context to Mobile Platform Programming

ICTEAM / UCLouvain / Belgium

Alfredo Cádiz Jean-Christophe Libbrecht

Julien Goffaux

Software Language Engineering

Sebastián GonzálezNicolás Cardozo

Kim Mens

3rd International Conference on

Sebastián González

Kim Mens

13 October 2010Thursday 21 October 2010

Page 3: Subjective-C: Bringing Context to Mobile Platform Programming

Subjective-CBringing Context to Mobile Platform Programming

ICTEAM / UCLouvain / Belgium

Alfredo Cádiz Jean-Christophe Libbrecht

Julien Goffaux

Software Language Engineering

Sebastián GonzálezNicolás Cardozo

Kim Mens

3rd International Conference on

Sebastián González

Kim Mens

13 October 2010Thursday 21 October 2010

Page 4: Subjective-C: Bringing Context to Mobile Platform Programming

2

Research Context

Thursday 21 October 2010

Page 5: Subjective-C: Bringing Context to Mobile Platform Programming

??

??

?

?

programming in isolation programming in contextProgramming Mindset Towards a Mindset Shift

3

Research Goal

Thursday 21 October 2010

Page 6: Subjective-C: Bringing Context to Mobile Platform Programming

class phone {

method receive ( call ) {

if ( phone.isOffHook( ) )play( phone.callWaitingSignal( ), 2 );

else if ( phone.environment( ).acoustics( ).isQuiet( ) )phone.vibrate( 5 );

else if ( phone.user( ).isUnavailable( ) )forwardCall( call, phone.forwardNumber( ) );

elseplay( phone.ringTone( ), 10 );

}

conditional statements

4

Contemporary Solution

Thursday 21 October 2010

Page 7: Subjective-C: Bringing Context to Mobile Platform Programming

class phone {

method receive ( call ) {

if ( phone.isOffHook( ) )play( phone.callWaitingSignal( ), 2 );

else if ( phone.environment( ).acoustics( ).isQuiet( ) )phone.vibrate( 5 );

else if ( phone.user( ).isUnavailable( ) )forwardCall( call, phone.forwardNumber( ) );

elseplay( phone.ringTone( ), 10 );

}

conditional statements

4

Contemporary Solution

Thursday 21 October 2010

Page 8: Subjective-C: Bringing Context to Mobile Platform Programming

class phone {

method receive ( call ) {

if ( phone.isOffHook( ) )play( phone.callWaitingSignal( ), 2 );

else if ( phone.environment( ).acoustics( ).isQuiet( ) )phone.vibrate( 5 );

else if ( phone.user( ).isUnavailable( ) )forwardCall( call, phone.forwardNumber( ) );

elseplay( phone.ringTone( ), 10 );

}

conditional statements

4

Contemporary Solution

·

Thursday 21 October 2010

Page 9: Subjective-C: Bringing Context to Mobile Platform Programming

class phone {

method receive ( call ) {

if ( phone.isOffHook( ) )play( phone.callWaitingSignal( ), 2 );

else if ( phone.environment( ).acoustics( ).isQuiet( ) )phone.vibrate( 5 );

else if ( phone.user( ).isUnavailable( ) )forwardCall( call, phone.forwardNumber( ) );

elseplay( phone.ringTone( ), 10 );

}

conditional statements

4

Contemporary Solution

·

Thursday 21 October 2010

Page 10: Subjective-C: Bringing Context to Mobile Platform Programming

class phone {

method receive ( call ) {

if ( phone.isOffHook( ) )play( phone.callWaitingSignal( ), 2 );

else if ( phone.environment( ).acoustics( ).isQuiet( ) )phone.vibrate( 5 );

else if ( phone.user( ).isUnavailable( ) )forwardCall( call, phone.forwardNumber( ) );

elseplay( phone.ringTone( ), 10 );

}

conditional statements

4

Contemporary Solution

·

Thursday 21 October 2010

Page 11: Subjective-C: Bringing Context to Mobile Platform Programming

class phone {

method receive ( call ) {

if ( phone.isOffHook( ) )play( phone.callWaitingSignal( ), 2 );

else if ( phone.environment( ).acoustics( ).isQuiet( ) )phone.vibrate( 5 );

else if ( phone.user( ).isUnavailable( ) )forwardCall( call, phone.forwardNumber( ) );

elseplay( phone.ringTone( ), 10 );

}

conditional statements

Tangled

Fixed 4

Scattered

Contemporary Solution

·

Thursday 21 October 2010

Page 12: Subjective-C: Bringing Context to Mobile Platform Programming

class Phone{ attribute strategy; method receive ( call ) { strategy.receive ( call ); } }

special software architecturesContemporary Solution

5Thursday 21 October 2010

Page 13: Subjective-C: Bringing Context to Mobile Platform Programming

class UnavailableStrategy{ method receive ( call ) { ... } }

class QuietStrategy{ method receive ( call ) { ... } }

class OffHookStrategy{ method receive ( call ) { ... } }

class DefaultStrategy{ method receive ( call ) { ... } }

class Phone{ attribute strategy; method receive ( call ) { strategy.receive ( call ); } }

UnavailableStrategy

OffHookStrategy

special software architecturesContemporary Solution

5

QuietStrategyPhone

DefaultStrategy

·

Thursday 21 October 2010

Page 14: Subjective-C: Bringing Context to Mobile Platform Programming

class UnavailableStrategy{ method receive ( call ) { ... } }

class QuietStrategy{ method receive ( call ) { ... } }

class OffHookStrategy{ method receive ( call ) { ... } }

class DefaultStrategy{ method receive ( call ) { ... } }

class Phone{ attribute strategy; method receive ( call ) { strategy.receive ( call ); } }

UnavailableStrategy

OffHookStrategy

special software architecturesContemporary Solution

5

QuietStrategyPhone

DefaultStrategy

·

Thursday 21 October 2010

Page 15: Subjective-C: Bringing Context to Mobile Platform Programming

class UnavailableStrategy{ method receive ( call ) { ... } }

class QuietStrategy{ method receive ( call ) { ... } }

class OffHookStrategy{ method receive ( call ) { ... } }

class DefaultStrategy{ method receive ( call ) { ... } }

class Phone{ attribute strategy; method receive ( call ) { strategy.receive ( call ); } }

UnavailableStrategy

OffHookStrategy

special software architecturesContemporary Solution

5

QuietStrategyPhone

DefaultStrategy

Infrastructural burdenFixed adaptation points

Thursday 21 October 2010

Page 16: Subjective-C: Bringing Context to Mobile Platform Programming

Solution Language engineering approach

6

Thursday 21 October 2010

Page 17: Subjective-C: Bringing Context to Mobile Platform Programming

Solution Language engineering approach

Prototypes

Multiple dispatch

PMD

6SmalltalkSelfCLOS

Thursday 21 October 2010

Page 18: Subjective-C: Bringing Context to Mobile Platform Programming

Solution Language engineering approach

Prototypes

Multiple dispatch

PMD

Subjective Programming

6SmalltalkSelfCLOS

Thursday 21 October 2010

Page 19: Subjective-C: Bringing Context to Mobile Platform Programming

Context Management

Solution Language engineering approach

Prototypes

Multiple dispatch

PMD

Subjective Programming

6SmalltalkSelfCLOS

Thursday 21 October 2010

Page 20: Subjective-C: Bringing Context to Mobile Platform Programming

Context Management

Solution Language engineering approach

Prototypes

Multiple dispatch

Context-Oriented Programming

PMD

Subjective Programming

6SmalltalkSelfCLOS

Thursday 21 October 2010

Page 21: Subjective-C: Bringing Context to Mobile Platform Programming

Context Management

Solution

Ambience

Language engineering approach

Prototypes

Multiple dispatch

Context-Oriented Programming

PMD

[DLS’07]

Subjective Programming

6SmalltalkSelfCLOS

Thursday 21 October 2010

Page 22: Subjective-C: Bringing Context to Mobile Platform Programming

Context Management

Solution

Ambience

Language engineering approach

Prototypes

Multiple dispatch

Context-Oriented Programming

PMD

[DLS’07]

Subjective Programming

6SmalltalkSelfCLOS

Thursday 21 October 2010

Page 23: Subjective-C: Bringing Context to Mobile Platform Programming

Context Management

Solution

Ambience

Language engineering approach

Prototypes

Multiple dispatch

Context-Oriented Programming

Open Classes

Single dispatch

PMD

[DLS’07]

Objective-C

Subjective Programming

6SmalltalkSelf CCLOS

Thursday 21 October 2010

Page 24: Subjective-C: Bringing Context to Mobile Platform Programming

Context Management

Solution

Ambience

Language engineering approach

Prototypes

Multiple dispatch

Context-Oriented Programming

Open Classes

Single dispatch

Subjective Programming

PMD

[DLS’07]

Objective-C

Subjective Programming

6SmalltalkSelf CCLOS

Thursday 21 October 2010

Page 25: Subjective-C: Bringing Context to Mobile Platform Programming

Context ManagementContext Management

Solution

Ambience

Language engineering approach

Prototypes

Multiple dispatch

Context-Oriented Programming

Open Classes

Single dispatch

Subjective Programming

PMD

[DLS’07]

Objective-C

Subjective Programming

6SmalltalkSelf CCLOS

Thursday 21 October 2010

Page 26: Subjective-C: Bringing Context to Mobile Platform Programming

Context ManagementContext Management

Solution

Ambience

Language engineering approach

Prototypes

Multiple dispatch

Context-Oriented Programming

Open Classes

Single dispatch

Subjective Programming

PMD

[DLS’07]

Context-Oriented Programming

Objective-C

Subjective Programming

6SmalltalkSelf CCLOS

Thursday 21 October 2010

Page 27: Subjective-C: Bringing Context to Mobile Platform Programming

Context ManagementContext Management

Solution

Ambience

Language engineering approach

Prototypes

Multiple dispatch

Context-Oriented Programming

Subjective-C

Open Classes

Single dispatch

Subjective Programming

PMD

[DLS’07]

Context-Oriented Programming

Objective-C

Subjective Programming

6SmalltalkSelf CCLOS

Thursday 21 October 2010

Page 28: Subjective-C: Bringing Context to Mobile Platform Programming

Context ManagementContext Management

Solution

Ambience

Language engineering approach

Prototypes

Multiple dispatch

Context-Oriented Programming

Subjective-C

Open Classes

Single dispatch

Subjective Programming

PMD

[DLS’07]

Context-Oriented Programming

Objective-C

Subjective Programming

6SmalltalkSelf CCLOS

Thursday 21 October 2010

Page 29: Subjective-C: Bringing Context to Mobile Platform Programming

1 General mindset

2 Context definition

3 Context-specific behaviour

4 Context relations

5 Implementation

6 Efficiency

Subjective-C

7Thursday 21 October 2010

Page 30: Subjective-C: Bringing Context to Mobile Platform Programming

external

Context-Aware System Architecture

Subjective-C

context effectcontext

information

ActuatorsSensors

arbitratedcontext changes

World

Context Discovery

Context Management

Active Context

8

internal

Application Behaviour

1 Mindset1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency

Thursday 21 October 2010

Page 31: Subjective-C: Bringing Context to Mobile Platform Programming

Low battery charge

High CPU load

Battery charge = 220 mAh

Computationally accessible information

Idle cycles = 11 MHz

action can be taken

Well-defined situation(Contexts)

Location = 51°26′N 5°29′E Eindhoven

no semantics

What is context?

9

Z axis = 0.03 Landscape orientation

situations can be reified

2 Context1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency

Thursday 21 October 2010

Page 32: Subjective-C: Bringing Context to Mobile Platform Programming

10

1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency

Case Study3 Behaviour

Thursday 21 October 2010

Page 33: Subjective-C: Bringing Context to Mobile Platform Programming

1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency

UILabel classdrawTextInRect:Draws the receiver’s text in the specified rectangle.- (void)drawTextInRect:(CGRect)rectParametersrect

The rectangle in which to draw the text.DiscussionYou should not call this method directly. This method should only be overridden by subclasses that want to modify the default drawing behavior for the label’s text.AvailabilityAvailable in iOS 2.0 and later.Declared InUILabel.h

Case Study

11

3 Behaviour

Thursday 21 October 2010

Page 34: Subjective-C: Bringing Context to Mobile Platform Programming

1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency

UILabel classdrawTextInRect:Draws the receiver’s text in the specified rectangle.- (void)drawTextInRect:(CGRect)rectParametersrect

The rectangle in which to draw the text.DiscussionYou should not call this method directly. This method should only be overridden by subclasses that want to modify the default drawing behavior for the label’s text.AvailabilityAvailable in iOS 2.0 and later.Declared InUILabel.h

Case Study

11

3 Behaviour

Thursday 21 October 2010

Page 35: Subjective-C: Bringing Context to Mobile Platform Programming

Subjective-C: Look & Feel

@implementation UILabel (color)#context Landscape- (void)drawTextInRect:(CGRect)rect { self.textColor = [UIColor greenColor]; [superContext drawTextInRect:rect];}@end

12

1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency3 Behaviour

Thursday 21 October 2010

Page 36: Subjective-C: Bringing Context to Mobile Platform Programming

Subjective-C: Look & Feel

@implementation UILabel (color)#context Landscape- (void)drawTextInRect:(CGRect)rect { self.textColor = [UIColor greenColor]; [superContext drawTextInRect:rect];}@end

Open classesObjective-C

12

1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency3 Behaviour

Thursday 21 October 2010

Page 37: Subjective-C: Bringing Context to Mobile Platform Programming

Subjective-C: Look & Feel

@implementation UILabel (color)#context Landscape- (void)drawTextInRect:(CGRect)rect { self.textColor = [UIColor greenColor]; [superContext drawTextInRect:rect];}@end

Open classesObjective-CCOP

Subjective-C

12

1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency3 Behaviour

Thursday 21 October 2010

Page 38: Subjective-C: Bringing Context to Mobile Platform Programming

Subjective-C: Look & Feel

@implementation UILabel (color)#context Landscape- (void)drawTextInRect:(CGRect)rect { self.textColor = [UIColor greenColor]; [superContext drawTextInRect:rect];}@end

Open classesObjective-CCOP

Subjective-C

✓ Adaptation of any existing component✓ No access to original source code needed✓ Adaptations can be cleanly modularised

12

1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency3 Behaviour

Thursday 21 October 2010

Page 39: Subjective-C: Bringing Context to Mobile Platform Programming

Context Relations

HDVideo BatteryHighRequirement

Museum Quiet

Eindhoven NetherlandsStrong Inclusion

Weak Inclusion

Landscape PortraitExclusion

13

1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency4 Relations

Thursday 21 October 2010

Page 40: Subjective-C: Bringing Context to Mobile Platform Programming

// Create contextsContext* Room = [[Context alloc] initWithName:@"Room"];Context* Bedroom = [[Context alloc] initWithName:@"Bedroom"];Context* Day = [[Context alloc] initWithName:@"Day"];Context* Night = [[Context alloc] initWithName:@"Night"];...

// Register contexts[CONTEXT addContext:Room];[CONTEXT addContext:Bedroom];[CONTEXT addContext:Day];[CONTEXT addContext:Night];...

// Establish context relations[Day addExclusionRelationWith:Night];[Bedroom addHardInclusionRelationWith:Room];...

Programmatic Context Definition

14

1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency4 Relations

Thursday 21 October 2010

Page 41: Subjective-C: Bringing Context to Mobile Platform Programming

// Create contextsContext* Room = [[Context alloc] initWithName:@"Room"];Context* Bedroom = [[Context alloc] initWithName:@"Bedroom"];Context* Day = [[Context alloc] initWithName:@"Day"];Context* Night = [[Context alloc] initWithName:@"Night"];...

// Register contexts[CONTEXT addContext:Room];[CONTEXT addContext:Bedroom];[CONTEXT addContext:Day];[CONTEXT addContext:Night];...

// Establish context relations[Day addExclusionRelationWith:Night];[Bedroom addHardInclusionRelationWith:Room];...

Context Declaration Language

14

Relations:

Day >< NightSun >< Rain

Livingroom -> TVLivingroom -> WindowLivingroom -> LuminosityLivingroom -> TemperatureKitchen -> LuminosityBedroom -> TemperatureBedroom -> LuminosityBedroom -> WindowBathroom -> TemperatureBathroom -> Luminosity

Bathroom => RoomKitchen => RoomLivingroom => RoomBedroom => Room

TV =< ElectricityLuminosity =< ElectricityWindow =< Sun

Contexts:

RoomLivingroomKitchenBedroomBathroomDayNight

TVWindowTemperatureLuminosity

SunRainElectricity

1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency4 Relations

Thursday 21 October 2010

Page 42: Subjective-C: Bringing Context to Mobile Platform Programming

// Create contextsContext* Room = [[Context alloc] initWithName:@"Room"];Context* Bedroom = [[Context alloc] initWithName:@"Bedroom"];Context* Day = [[Context alloc] initWithName:@"Day"];Context* Night = [[Context alloc] initWithName:@"Night"];...

// Register contexts[CONTEXT addContext:Room];[CONTEXT addContext:Bedroom];[CONTEXT addContext:Day];[CONTEXT addContext:Night];...

// Establish context relations[Day addExclusionRelationWith:Night];[Bedroom addHardInclusionRelationWith:Room];...

Context Declaration Language

14

1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency4 Relations

Thursday 21 October 2010

Page 43: Subjective-C: Bringing Context to Mobile Platform Programming

// Register context-specific methods[MANAGER addMethod:@selector(Context_Bedroom_getActiveRoom) forClass:[DomoticsServerViewController class] forContextNames:[[NSSet alloc] initWithObjects:@"Bedroom" ,nil] withDefautSel:@selector(getActiveRoom) withPriority:0];...

// Create contextsContext* Room = [[Context alloc] initWithName:@"Room"];Context* Bedroom = [[Context alloc] initWithName:@"Bedroom"];Context* Day = [[Context alloc] initWithName:@"Day"];Context* Night = [[Context alloc] initWithName:@"Night"];...

// Register contexts[CONTEXT addContext:Room];[CONTEXT addContext:Bedroom];[CONTEXT addContext:Day];[CONTEXT addContext:Night];...

// Establish context relations[Day addExclusionRelationWith:Night];[Bedroom addHardInclusionRelationWith:Room];...

14

1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency4 Relations

#context Bedroom-(Context*) getActiveRoom{! return [CONTEXT getContext:@"Bedroom"];}

Other Generated Code

Thursday 21 October 2010

Page 44: Subjective-C: Bringing Context to Mobile Platform Programming

// Register context-specific methods[MANAGER addMethod:@selector(Context_Bedroom_getActiveRoom) forClass:[DomoticsServerViewController class] forContextNames:[[NSSet alloc] initWithObjects:@"Bedroom" ,nil] withDefautSel:@selector(getActiveRoom) withPriority:0];...

// Create contextsContext* Room = [[Context alloc] initWithName:@"Room"];Context* Bedroom = [[Context alloc] initWithName:@"Bedroom"];Context* Day = [[Context alloc] initWithName:@"Day"];Context* Night = [[Context alloc] initWithName:@"Night"];...

// Register contexts[CONTEXT addContext:Room];[CONTEXT addContext:Bedroom];[CONTEXT addContext:Day];[CONTEXT addContext:Night];...

// Establish context relations[Day addExclusionRelationWith:Night];[Bedroom addHardInclusionRelationWith:Room];...

14

1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency4 Relations

Other Generated Code

Thursday 21 October 2010

Page 45: Subjective-C: Bringing Context to Mobile Platform Programming

@implementation LowpassFilter

-(void)addAcceleration:(UIAcceleration*)accel{! double alpha = filterConstant;!! if(adaptive)! ! alpha = ... // big formula to calculate the alpha level!! x = accel.x * alpha + x * (1.0 - alpha);! y = accel.y * alpha + y * (1.0 - alpha);! z = accel.z * alpha + z * (1.0 - alpha);}

-(NSString*)name{! return adaptive ? @"Adaptive Lowpass Filter" : @"Lowpass Filter";}

...

15

1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency

Application Contexts

Thursday 21 October 2010

Page 46: Subjective-C: Bringing Context to Mobile Platform Programming

16

1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency

@implementation LowpassFilter

#context LowPassFilter-(void)addAcceleration:(UIAcceleration*)accel{! double alpha = [self getAlpha:accel];!! x = accel.x * alpha + x * (1.0 - alpha);! y = accel.y * alpha + y * (1.0 - alpha);! z = accel.z * alpha + z * (1.0 - alpha);}

#context !Adaptive-(double) getAlpha:(UIAcceleration*)accel{! return filterConstant;}

#context LowPassFilter Adaptive-(double) getAlpha:(UIAcceleration*)accel {! return // big formula to calculate the alpha level;}

Application Contexts

Thursday 21 October 2010

Page 47: Subjective-C: Bringing Context to Mobile Platform Programming

16

1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency

@implementation LowpassFilter

#context LowPassFilter-(void)addAcceleration:(UIAcceleration*)accel{! double alpha = [self getAlpha:accel];!! x = accel.x * alpha + x * (1.0 - alpha);! y = accel.y * alpha + y * (1.0 - alpha);! z = accel.z * alpha + z * (1.0 - alpha);}

#context !Adaptive-(double) getAlpha:(UIAcceleration*)accel{! return filterConstant;}

#context LowPassFilter Adaptive-(double) getAlpha:(UIAcceleration*)accel {! return // big formula to calculate the alpha level;}

Relations:

LowPassFilter >< HighPassFilter LowPassFilter => FilterHighPassFilter => Filter

Contexts:

AdaptivePausedLowPassFilterHighPassFilterFilter

Application Contexts

Thursday 21 October 2010

Page 48: Subjective-C: Bringing Context to Mobile Platform Programming

Implementation

17

dynamic method pre-dispatch

UILabel@property NSString *text@property UIFont *font...- (void)Portrait_drawTextInRect:(CGRect)rect- (void)Landscape_drawTextInRect:(CGRect)rect- (void)Default_drawTextInRect:(CGRect)rect- (void)drawTextInRect:(CGRect)rect

1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency5 Implementation

Default impl

Landscape impl

Portrait impl

Thursday 21 October 2010

Page 49: Subjective-C: Bringing Context to Mobile Platform Programming

Implementation

17

dynamic method pre-dispatch

UILabel@property NSString *text@property UIFont *font...- (void)Portrait_drawTextInRect:(CGRect)rect- (void)Landscape_drawTextInRect:(CGRect)rect- (void)Default_drawTextInRect:(CGRect)rect- (void)drawTextInRect:(CGRect)rect

1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency5 Implementation

Default impl

Landscape impl

Portrait implvtable

Thursday 21 October 2010

Page 50: Subjective-C: Bringing Context to Mobile Platform Programming

Implementation

17

dynamic method pre-dispatch

UILabel@property NSString *text@property UIFont *font...- (void)Portrait_drawTextInRect:(CGRect)rect- (void)Landscape_drawTextInRect:(CGRect)rect- (void)Default_drawTextInRect:(CGRect)rect- (void)drawTextInRect:(CGRect)rect

1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency5 Implementation

Default impl

Landscape impl

Portrait impl

Thursday 21 October 2010

Page 51: Subjective-C: Bringing Context to Mobile Platform Programming

Implementation

17

dynamic method pre-dispatch

UILabel@property NSString *text@property UIFont *font...- (void)Portrait_drawTextInRect:(CGRect)rect- (void)Landscape_drawTextInRect:(CGRect)rect- (void)Default_drawTextInRect:(CGRect)rect- (void)drawTextInRect:(CGRect)rect

1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency5 Implementation

Default impl

Landscape impl

Portrait impl

Thursday 21 October 2010

Page 52: Subjective-C: Bringing Context to Mobile Platform Programming

Implementation

17

dynamic method pre-dispatch

UILabel@property NSString *text@property UIFont *font...- (void)Portrait_drawTextInRect:(CGRect)rect- (void)Landscape_drawTextInRect:(CGRect)rect- (void)Default_drawTextInRect:(CGRect)rect- (void)drawTextInRect:(CGRect)rect

[CONTEXT activateContextWithName: @"Landscape"];

1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency5 Implementation

Default impl

Landscape impl

Portrait impl

Thursday 21 October 2010

Page 53: Subjective-C: Bringing Context to Mobile Platform Programming

Implementation

17

dynamic method pre-dispatch

UILabel@property NSString *text@property UIFont *font...- (void)Portrait_drawTextInRect:(CGRect)rect- (void)Landscape_drawTextInRect:(CGRect)rect- (void)Default_drawTextInRect:(CGRect)rect- (void)drawTextInRect:(CGRect)rect

[CONTEXT activateContextWithName: @"Landscape"];

1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency5 Implementation

Default impl

Landscape impl

Portrait impl

Thursday 21 October 2010

Page 54: Subjective-C: Bringing Context to Mobile Platform Programming

Implementation

17

dynamic method pre-dispatch

UILabel@property NSString *text@property UIFont *font...- (void)Portrait_drawTextInRect:(CGRect)rect- (void)Landscape_drawTextInRect:(CGRect)rect- (void)Default_drawTextInRect:(CGRect)rect- (void)drawTextInRect:(CGRect)rect

[CONTEXT activateContextWithName: @"Landscape"];

[CONTEXT deactivateContextWithName: @"Landscape"];

1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency5 Implementation

Default impl

Landscape impl

Portrait impl

Thursday 21 October 2010

Page 55: Subjective-C: Bringing Context to Mobile Platform Programming

Implementation

17

dynamic method pre-dispatch

UILabel@property NSString *text@property UIFont *font...- (void)Portrait_drawTextInRect:(CGRect)rect- (void)Landscape_drawTextInRect:(CGRect)rect- (void)Default_drawTextInRect:(CGRect)rect- (void)drawTextInRect:(CGRect)rect

[CONTEXT activateContextWithName: @"Landscape"];

[CONTEXT deactivateContextWithName: @"Landscape"];

1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency5 Implementation

Default impl

Landscape impl

Portrait impl

Thursday 21 October 2010

Page 56: Subjective-C: Bringing Context to Mobile Platform Programming

Implementation

17

dynamic method pre-dispatch

UILabel@property NSString *text@property UIFont *font...- (void)Portrait_drawTextInRect:(CGRect)rect- (void)Landscape_drawTextInRect:(CGRect)rect- (void)Default_drawTextInRect:(CGRect)rect- (void)drawTextInRect:(CGRect)rect

‣ no additional cost for method invocations‣ cost incurred at context switching time

[CONTEXT activateContextWithName: @"Landscape"];

[CONTEXT deactivateContextWithName: @"Landscape"];

1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency5 Implementation

Default impl

Landscape impl

Portrait impl

Thursday 21 October 2010

Page 57: Subjective-C: Bringing Context to Mobile Platform Programming

-(void) test:(int) mode {

if (mode == 1) result = 1;

...

else if (mode == N) result = N;

else result = 0;}

Objective-C

#context C1-(void) test{ result = 1; }

...

#context CN-(void) test{ result = N; }

-(void) test{ result = 0; }

Subjective-C

18

conditional statementscontext-specific methods

What is more efficient?1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency6 Efficiency

Thursday 21 October 2010

Page 58: Subjective-C: Bringing Context to Mobile Platform Programming

#context C1-(void) test{ result = 1; }

...

#context CK-(void) test{ result = K; }

...

#context CN-(void) test{ result = N; }

-(void) test{ result = 0; }

Subjective-Cfor (int i = 0; i < 1000; i++) {

// switch context if (i % 2) [CONTEXT activateContextWithName:@"CK"]; else [CONTEXT deactivateContextWithName:@"CK"];

// call test method repeatedly for (int j = 0; j < M; j++) [self test];}

19

Benchmark Setup1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency6 Efficiency

Thursday 21 October 2010

Page 59: Subjective-C: Bringing Context to Mobile Platform Programming

#context C1-(void) test{ result = 1; }

...

#context CK-(void) test{ result = K; }

...

#context CN-(void) test{ result = N; }

-(void) test{ result = 0; }

Subjective-Cfor (int i = 0; i < 1000; i++) {

// switch context if (i % 2) [CONTEXT activateContextWithName:@"CK"]; else [CONTEXT deactivateContextWithName:@"CK"];

// call test method repeatedly for (int j = 0; j < M; j++) [self test];}

19

Benchmark Setup

N: number of variationsK: selected branchM: method calls per mode switch

1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency6 Efficiency

Thursday 21 October 2010

Page 60: Subjective-C: Bringing Context to Mobile Platform Programming

-(void) test:(int) mode {

if (mode == 1) result = 1;

...

if (mode == K) result = K;

...

else if (mode == N) result = N;

else result = 0;}

Objective-Cfor (int i = 0; i < 1000; i++) {

// toggle operation mode if (i % 2) mode = K; else mode = 0;

// call test method repeatedly for (int j = 0; j < M; j++) [self test: mode];}

20

Benchmark Setup

N: number of variationsK: selected branchM: method calls per mode switch

1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency6 Efficiency

Thursday 21 October 2010

Page 61: Subjective-C: Bringing Context to Mobile Platform Programming

10−1

100

101

102

103

104

105

100 101 102 103 104 105

milliseconds

method calls per context switch (M)

M∗

Subjective-C

Objective-C

Fig. 6. Performance comparison using logarithmic scale (N = 50, K = 50).

From the benchmarks we can conclude that the efficiency of Subjective-C

is highly dependent on its usage. Most benefits are obtained for contexts that

are not switched too often with respect to the rate of usage of affected methods.

For many common cases this condition will hold: context switches will usually be

sparse enough in time with respect to affected method calls. In particular, in each

of the three case studies described in Section 5, the Subjective-C implementation

did not give raise to apparent performance penalties.

Note that although gained efficiency for some use cases is a desirable side

effect of using Subjective-C, the more important benefits remain in the realm

of software engineering: improved modularisation, reusability, extensibility, and

dynamic adaptability.

7 Discussion, Limitations and Future Work

Even though Subjective-C is usable for application development on mobile de-

vices as suggested in Section 5, it still has rough edges we need to iron out. This

section describes the most salient ones, starting with the more technical and

going over to the more conceptual.

Super-Context Translation A caveat of the implementation presented in

Section 4.3 is the impossibility to retrieve the return value from a superContextmessage. Our current solution consists in having a different syntax when the

return value is needed, which complements the definition of the superContextconstruct given in Section 2.4:

[superContext selector] => variable;[superContext keyword: argument ...] => variable;

-(void) test:(int) mode {

if (mode == 1)

result = 1;

else if (mode == 2)

result = 2;

...

else if (mode == N)

result = N;

else

result = 0;

}

-(void) test {

result = 0;

}

#context C1

-(void) test {

result = 1;

}

...

#context CN

-(void) test {

result = N;

}

Snippet 11. Dummy test methods in Objective-C (left) and Subjective-C (right) with

N + 1 behavioural variants. The choice of the variant depends on the application’s

current operating mode and the application’s current execution context, respectively.

for (int i = 0; i < 1000; i++) {

if (i % 2)

[CONTEXT activateContextWithName:@"CK"]; // mode = K;

else

[CONTEXT deactivateContextWithName:@"CK"]; // mode = 0;

for (int j = 0; j < M; j++)

[self test];

}

Snippet 12. Code to measure the relative cost of context changes with respect to

context-dependent method invocation in Subjective-C; the Objective-C counterpart is

analogous and is therefore just suggested as comments.

calls per context switch. Beyond this point, Subjective-C is more efficient than

Objective-C; the execution time in both approaches will tend to grow linearly,

although Objective-C will have a considerably higher slope due to conditional

statement execution,7 besides the cost of method invocation which is incurred in

both approaches. Table 4 summarises the intersection points for various values

of N and K, including the case of Fig. 6.

N K M∗

5 1 8230

10 1 4708

50 1 3405

5 5 4521

10 10 2653

50 50 1148

Table 4. Efficiency meeting point M∗between Subjective-C and Objective-C.

7This difference is not apparent in Fig. 6 because of the logarithmic scale.

21

N: number of variationsK: selected branchM: method calls per context switch

iPhone 3GS, iOS 4.0

1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency6 Efficiency

Thursday 21 October 2010

Page 62: Subjective-C: Bringing Context to Mobile Platform Programming

Subjective-C✓ Context-oriented programming for a C-flavoured language✓ Runs on mobile phones✓ Run-time behaviour adaptation of any component (incl. 3rd party)✓ Permits clean modularisation of adaptations✓ Maximises adaptation points while avoiding architectural burden✓ Context interdependency system✓ Context declaration language✓ Can be more efficient than dynamic dispatch➡ Thread-local adaptations➡ Improving efficiency➡ Usability (IDE integration)➡ Ensure behaviour consistency

1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency

Thursday 21 October 2010

Page 63: Subjective-C: Bringing Context to Mobile Platform Programming

Subjective-C✓ Context-oriented programming for a C-flavoured language✓ Runs on mobile phones✓ Run-time behaviour adaptation of any component (incl. 3rd party)✓ Permits clean modularisation of adaptations✓ Maximises adaptation points while avoiding architectural burden✓ Context interdependency system✓ Context declaration language✓ Can be more efficient than dynamic dispatch➡ Thread-local adaptations➡ Improving efficiency➡ Usability (IDE integration)➡ Ensure behaviour consistency

http://ambience.info.ucl.ac.be

1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency

Time for questions!

Thursday 21 October 2010


Recommended