SlideShare a Scribd company logo
3rd International Conference on
                              Software Language Engineering


                                     Subjective-C
        Bringing Context to Mobile Platform Programming

                                  ICTEAM / UCLouvain / Belgium


                           Sebastián González                Alfredo Cádiz
                            Nicolás Cardozo            Jean-Christophe Libbrecht
                               Kim Mens                      Julien Goffaux


                                           13 October 2010
Thursday 21 October 2010
3rd International Conference on
                                Software Language Engineering


                                      Subjective-C
        Bringing Context to Mobile Platform Programming

                                    ICTEAM / UCLouvain / Belgium


                           Sebastián González
                             Sebastián González               Alfredo Cádiz
                              Nicolás Cardozo           Jean-Christophe Libbrecht
                                Kim Mens
                                 Kim Mens                     Julien Goffaux


                                            13 October 2010
Thursday 21 October 2010
3rd International Conference on
                                Software Language Engineering


                                      Subjective-C
        Bringing Context to Mobile Platform Programming

                                    ICTEAM / UCLouvain / Belgium


                           Sebastián González
                             Sebastián González               Alfredo Cádiz
                              Nicolás Cardozo           Jean-Christophe Libbrecht
                                Kim Mens
                                 Kim Mens                     Julien Goffaux


                                            13 October 2010
Thursday 21 October 2010
Research Context




                                              2
Thursday 21 October 2010
Research Goal
     Programming Mindset                   Towards a Mindset Shift
      programming in isolation              programming in context

                               ?
           ?
                                       ?



       ?
                                   ?
                           ?

                                                                     3
Thursday 21 October 2010
Contemporary Solution
                                          conditional statements
                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( ) );
                           else
                             play( phone.ringTone( ), 10 );
                }




                                                                                      4
Thursday 21 October 2010
Contemporary Solution
                                          conditional statements
                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( ) );
                           else
                             play( phone.ringTone( ), 10 );
                }




                                                                                      4
Thursday 21 October 2010
Contemporary Solution
                                          conditional statements
                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( ) );
                           else
                             play( phone.ringTone( ), 10 );
                }




                                                                                          4
Thursday 21 October 2010
Contemporary Solution
                                          conditional statements
                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( ) );
                           else
                             play( phone.ringTone( ), 10 );
                }




                                                                                          4
Thursday 21 October 2010
Contemporary Solution
                                          conditional statements
                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( ) );
                           else
                             play( phone.ringTone( ), 10 );                           ∅
                }




                                                                                              4
Thursday 21 October 2010
Contemporary Solution
                                          conditional statements
                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( ) );
                           else
                             play( phone.ringTone( ), 10 );                           ∅
                }

                                                            Tangled
                                                            Scattered
                                                            Fixed                             4
Thursday 21 October 2010
Contemporary Solution
                                  special software architectures




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




                                                                   5
Thursday 21 October 2010
Contemporary Solution
                                  special software architectures

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

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

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




                                                                                                    5
Thursday 21 October 2010
Contemporary Solution
                                  special software architectures

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

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

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




                                                                                                    5
Thursday 21 October 2010
Contemporary Solution
                                  special software architectures

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

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

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


                                                Infrastructural burden
                                                Fixed adaptation points
                                                                                                5
Thursday 21 October 2010
Solution           Language engineering approach




                                                           6
Thursday 21 October 2010
Solution                          Language engineering approach




                     Multiple dispatch
                           Prototypes

                             PMD
                                   CLOS
                                          Self   Smalltalk                6
Thursday 21 October 2010
Solution                          Language engineering approach




              Subjective Programming
                     Multiple dispatch
                           Prototypes

                             PMD
                                   CLOS
                                          Self   Smalltalk                6
Thursday 21 October 2010
Solution                          Language engineering approach




                    Context Management
              Subjective Programming
                     Multiple dispatch
                           Prototypes

                             PMD
                                   CLOS
                                          Self   Smalltalk                6
Thursday 21 October 2010
Solution                          Language engineering approach




     Context-Oriented Programming
                    Context Management
              Subjective Programming
                     Multiple dispatch
                           Prototypes

                             PMD
                                   CLOS
                                          Self   Smalltalk                6
Thursday 21 October 2010
Solution                           Language engineering approach



                     Ambience             [DLS’07]


     Context-Oriented Programming
                    Context Management
              Subjective Programming
                     Multiple dispatch
                           Prototypes

                             PMD
                                   CLOS
                                            Self     Smalltalk             6
Thursday 21 October 2010
Solution                           Language engineering approach



                     Ambience             [DLS’07]


     Context-Oriented Programming
                    Context Management
              Subjective Programming
                     Multiple dispatch
                           Prototypes

                             PMD
                                   CLOS
                                            Self     Smalltalk             6
Thursday 21 October 2010
Solution                           Language engineering approach



                     Ambience             [DLS’07]


     Context-Oriented Programming
                    Context Management
              Subjective Programming
                     Multiple dispatch                           Single dispatch
                           Prototypes                            Open Classes

                             PMD                                 Objective-C
                                   CLOS                          C
                                            Self     Smalltalk                     6
Thursday 21 October 2010
Solution                           Language engineering approach



                     Ambience             [DLS’07]


     Context-Oriented Programming
                    Context Management
              Subjective Programming                        Subjective Programming
                     Multiple dispatch                           Single dispatch
                           Prototypes                            Open Classes

                             PMD                                 Objective-C
                                   CLOS                          C
                                            Self     Smalltalk                       6
Thursday 21 October 2010
Solution                           Language engineering approach



                     Ambience             [DLS’07]


     Context-Oriented Programming
                    Context Management                       Context Management
              Subjective Programming                        Subjective Programming
                     Multiple dispatch                           Single dispatch
                           Prototypes                            Open Classes

                             PMD                                 Objective-C
                                   CLOS                          C
                                            Self     Smalltalk                       6
Thursday 21 October 2010
Solution                           Language engineering approach



                     Ambience             [DLS’07]

                                                        Context-Oriented Programming
     Context-Oriented Programming
                    Context Management                       Context Management
              Subjective Programming                        Subjective Programming
                     Multiple dispatch                           Single dispatch
                           Prototypes                            Open Classes

                             PMD                                 Objective-C
                                   CLOS                          C
                                            Self     Smalltalk                         6
Thursday 21 October 2010
Solution                           Language engineering approach



                     Ambience             [DLS’07]          Subjective-C
                                                        Context-Oriented Programming
     Context-Oriented Programming
                    Context Management                       Context Management
              Subjective Programming                        Subjective Programming
                     Multiple dispatch                           Single dispatch
                           Prototypes                            Open Classes

                             PMD                                 Objective-C
                                   CLOS                          C
                                            Self     Smalltalk                         6
Thursday 21 October 2010
Solution                           Language engineering approach



                     Ambience             [DLS’07]          Subjective-C
                                                        Context-Oriented Programming
     Context-Oriented Programming
                    Context Management                       Context Management
              Subjective Programming                        Subjective Programming
                     Multiple dispatch                           Single dispatch
                           Prototypes                            Open Classes

                             PMD                                 Objective-C
                                   CLOS                          C
                                            Self     Smalltalk                         6
Thursday 21 October 2010
Subjective-C
                           1 General mindset
                           2 Context definition
                           3 Context-specific behaviour
                           4 Context relations
                           5 Implementation
                           6 Efficiency
                                                         7
Thursday 21 October 2010
1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency

                    Context-Aware System Architecture

                                            World
                           Sensors                        Actuators
                                                                           Subjective-C
                      Context                             Application
                      Discovery                           Behaviour
                                 external      internal                   context
                                                                           effect
                                       context
                                     information            arbitrated
                                                             context
                                   Context                  changes       Active
                                  Management                             Context
                                                                                          8
Thursday 21 October 2010
1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency


                                 What is context?
                 Computationally                     Well-defined situation
              accessible information                      (Contexts)
                           Z axis = 0.03                Landscape orientation
              Battery charge = 220 mAh                    Low battery charge
                     Idle cycles = 11 MHz                   High CPU load
             Location = 51°26′N 5°29′E                         Eindhoven

                           no semantics                   action can be taken

                                                        situations can be reified
                                                                                        9
Thursday 21 October 2010
1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency



                                 Case Study




                                                                                        10
Thursday 21 October 2010
1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency



                                     Case Study
                       UILabel class
                       drawTextInRect:
                       Draws the receiver’s text in the specified rectangle.
                       - (void)drawTextInRect:(CGRect)rect
                       Parameters
                       rect
                          The rectangle in which to draw the text.
                       Discussion
                       You 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.
                       Availability
                       Available in iOS 2.0 and later.
                       Declared In
                       UILabel.h
                                                                                        11
Thursday 21 October 2010
1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency



                                     Case Study
                       UILabel class
                       drawTextInRect:
                       Draws the receiver’s text in the specified rectangle.
                       - (void)drawTextInRect:(CGRect)rect
                       Parameters
                       rect
                          The rectangle in which to draw the text.
                       Discussion
                       You 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.
                       Availability
                       Available in iOS 2.0 and later.
                       Declared In
                       UILabel.h
                                                                                        11
Thursday 21 October 2010
1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency



                      Subjective-C: Look & Feel

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




                                                                                        12
Thursday 21 October 2010
1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency



                      Subjective-C: Look & Feel
                                                                       Open classes
                           @implementation UILabel (color)     Objective-C
                           #context Landscape
                           - (void)drawTextInRect:(CGRect)rect {
                             self.textColor = [UIColor greenColor];
                             [superContext drawTextInRect:rect];
                           }
                           @end




                                                                                        12
Thursday 21 October 2010
1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency



                      Subjective-C: Look & Feel
                                                                       Open classes
                           @implementation UILabel (color)     Objective-C
     COP                   #context Landscape
  Subjective-C
                           - (void)drawTextInRect:(CGRect)rect {
                             self.textColor = [UIColor greenColor];
                             [superContext drawTextInRect:rect];
                           }
                           @end




                                                                                        12
Thursday 21 October 2010
1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency



                      Subjective-C: Look & Feel
                                                                       Open classes
                            @implementation UILabel (color)     Objective-C
     COP                    #context Landscape
  Subjective-C
                            - (void)drawTextInRect:(CGRect)rect {
                              self.textColor = [UIColor greenColor];
                              [superContext drawTextInRect:rect];
                            }
                            @end

                  ✓        Adaptation of any existing component
                  ✓        No access to original source code needed
                  ✓        Adaptations can be cleanly modularised
                                                                                        12
Thursday 21 October 2010
1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency



                               Context Relations
       Strong Inclusion                Eindhoven                    Netherlands


         Weak Inclusion                 Museum                          Quiet

                Requirement             HDVideo                     BatteryHigh


                           Exclusion   Landscape                       Portrait

                                                                                        13
Thursday 21 October 2010
1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency


   Programmatic Context Definition
    // Create contexts
    Context*        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
Thursday 21 October 2010
1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency


          Context Declaration Language
                                               Contexts:      Relations:
    // Create contexts
    Context* Room = [[Context alloc] initWithName:@"Room"];
    Context* Bedroom = [[Context alloc] initWithName:@"Bedroom"]; >< Night
                                              Room            Day
    Context*                                  Livingroom
             Day = [[Context alloc] initWithName:@"Day"];     Sun >< Rain
    Context*                                  Kitchen
             Night = [[Context alloc] initWithName:@"Night"];
    ...                                       Bedroom         Livingroom -> TV
                                              Bathroom        Livingroom -> Window
    // Register contexts                      Day             Livingroom -> Luminosity
    [CONTEXT addContext:Room];
    [CONTEXT addContext:Bedroom];             Night           Livingroom -> Temperature
    [CONTEXT addContext:Day];                                 Kitchen -> Luminosity
    [CONTEXT addContext:Night];               TV              Bedroom -> Temperature
    ...                                       Window          Bedroom -> Luminosity
                                              Temperature     Bedroom -> Window
    // Establish context relations            Luminosity      Bathroom -> Temperature
    [Day addExclusionRelationWith:Night];
    [Bedroom addHardInclusionRelationWith:Room];              Bathroom -> Luminosity
    ...                                       Sun
                                              Rain            Bathroom => Room
                                              Electricity     Kitchen => Room
                                                              Livingroom => Room
                                                              Bedroom => Room

                                                              TV =< Electricity
                                                              Luminosity =< Electricity
                                                              Window =< Sun               14
Thursday 21 October 2010
1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency


          Context Declaration Language
    // Create contexts
    Context*        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
Thursday 21 October 2010
1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency


                           Other Generated Code
    // Create contexts
    Context*        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                  #context Bedroom
    [Day addExclusionRelationWith:Night];    -(Context*) getActiveRoom{
    [Bedroom addHardInclusionRelationWith:Room];
                                             ! return [CONTEXT getContext:@"Bedroom"];
    ...
                                                    }

    // Register context-specific methods
    [MANAGER addMethod:@selector(Context_Bedroom_getActiveRoom)
             forClass:[DomoticsServerViewController class]
             forContextNames:[[NSSet alloc] initWithObjects:@"Bedroom" ,nil]
             withDefautSel:@selector(getActiveRoom)
             withPriority:0];
    ...
                                                                                         14
Thursday 21 October 2010
1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency


                           Other Generated Code
    // Create contexts
    Context*        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];
    ...


    // Register context-specific methods
    [MANAGER addMethod:@selector(Context_Bedroom_getActiveRoom)
             forClass:[DomoticsServerViewController class]
             forContextNames:[[NSSet alloc] initWithObjects:@"Bedroom" ,nil]
             withDefautSel:@selector(getActiveRoom)
             withPriority:0];
    ...
                                                                                        14
Thursday 21 October 2010
1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency



                           Application Contexts
         @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
Thursday 21 October 2010
1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency



                           Application Contexts
         @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;
         }
                                                16
Thursday 21 October 2010
1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency



                           Application Contexts
         @implementation LowpassFilter
                                                            Contexts:

         #context LowPassFilter
                                                     Adaptive
         -(void)addAcceleration:(UIAcceleration*)accel
                                                     Paused
         {
                                                     LowPassFilter
         ! double alpha = [self getAlpha:accel];
                                                     HighPassFilter
         !
         ! x = accel.x * alpha + x * (1.0 - alpha); Filter
         ! y = accel.y * alpha + y * (1.0 - alpha); Relations:
         ! z = accel.z * alpha + z * (1.0 - alpha);
         }
                                                            LowPassFilter >< HighPassFilter
                                                            LowPassFilter => Filter
         #context !Adaptive
                                                            HighPassFilter => Filter
         -(double) getAlpha:(UIAcceleration*)accel{
         ! return filterConstant;
         }

         #context LowPassFilter Adaptive
         -(double) getAlpha:(UIAcceleration*)accel {
         ! return // big formula to calculate the alpha level;
         }
                                                16
Thursday 21 October 2010
1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency


                             Implementation
                           dynamic method pre-dispatch

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




                                                                                        17
Thursday 21 October 2010
1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency


                             Implementation
                           dynamic method pre-dispatch

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




                                                                                        17
Thursday 21 October 2010
1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency


                             Implementation
                           dynamic method pre-dispatch

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




                                                                                        17
Thursday 21 October 2010
1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency


                             Implementation
                           dynamic method pre-dispatch

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




                                                                                        17
Thursday 21 October 2010
1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency


                             Implementation
                           dynamic method pre-dispatch

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


        [CONTEXT activateContextWithName: @"Landscape"];




                                                                                        17
Thursday 21 October 2010
1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency


                             Implementation
                           dynamic method pre-dispatch

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


        [CONTEXT activateContextWithName: @"Landscape"];




                                                                                        17
Thursday 21 October 2010
1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency


                             Implementation
                           dynamic method pre-dispatch

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


        [CONTEXT activateContextWithName: @"Landscape"];
        [CONTEXT deactivateContextWithName: @"Landscape"];




                                                                                        17
Thursday 21 October 2010
1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency


                             Implementation
                           dynamic method pre-dispatch

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


        [CONTEXT activateContextWithName: @"Landscape"];
        [CONTEXT deactivateContextWithName: @"Landscape"];




                                                                                        17
Thursday 21 October 2010
1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency


                             Implementation
                           dynamic method pre-dispatch

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


        [CONTEXT activateContextWithName: @"Landscape"];
        [CONTEXT deactivateContextWithName: @"Landscape"];

        ‣ no additional cost for method invocations
        ‣ cost incurred at context switching time                                       17
Thursday 21 October 2010
1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency


                           What is more efficient?
         Subjective-C                             Objective-C
         context-specific methods                  conditional statements
                #context C1                       -(void) test:(int) mode {
                -(void) test
                { result = 1; }                       if (mode == 1)
                                                        result = 1;
                ...
                                                      ...
                #context CN
                -(void) test                          else if (mode == N)
                { result = N; }                         result = N;

                -(void) test                          else
                { result = 0; }                         result = 0;
                                                  }
                                                                                        18
Thursday 21 October 2010
1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency

                           Benchmark Setup
                                                      Subjective-C
        #context C1
        -(void) test              for (int i = 0; i < 1000; i++) {
        { result = 1; }
                                      // switch context
        ...                           if (i % 2)
                                        [CONTEXT activateContextWithName:@"CK"];
        #context CK                   else
        -(void) test                    [CONTEXT deactivateContextWithName:@"CK"];
        { result = K; }
                                      // call test method repeatedly
        ...                           for (int j = 0; j < M; j++)
                                        [self test];
        #context CN               }
        -(void) test
        { result = N; }

        -(void) test
        { result = 0; }
                                                                                        19
Thursday 21 October 2010
1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency

                           Benchmark Setup
                                                      Subjective-C
        #context C1
        -(void) test              for (int i = 0; i < 1000; i++) {
        { result = 1; }
                                      // switch context
        ...                           if (i % 2)
                                        [CONTEXT activateContextWithName:@"CK"];
        #context CK                   else
        -(void) test                    [CONTEXT deactivateContextWithName:@"CK"];
        { result = K; }
                                      // call test method repeatedly
        ...                           for (int j = 0; j < M; j++)
                                        [self test];
        #context CN               }
        -(void) test
        { result = N; }
                                       N: number of variations
        -(void) test                   K: selected branch
        { result = 0; }
                                       M: method calls per mode switch                  19
Thursday 21 October 2010
1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency


                           Benchmark Setup
                                                       Objective-C
        -(void) test:(int) mode {
                                                for (int i = 0; i < 1000; i++) {
             if (mode == 1)
               result = 1;                          // toggle operation mode
                                                    if (i % 2)
             ...                                      mode = K;
                                                    else
             if (mode == K)                           mode = 0;
               result = K;
                                                    // call test method repeatedly
             ...                                    for (int j = 0; j < M; j++)
                                                      [self test: mode];
             else if (mode == N)                }
               result = N;

             else
                                      N: number of variations
               result = 0;            K: selected branch
        }
                                      M: method calls per mode switch                   20
Thursday 21 October 2010
e case of 1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency
          Fig. 6.

       N          K          M∗                                N: number of variations
     5           1       8230
     10          1       4708                                  K: selected branch
     50
     5
                 1
                 5
                         3405
                         4521                                  M: method calls per context switch
     10          10      2653
     50          50      1148       105
 ting point      M ∗ between Subjective-C and Objective-C.
                                              Subjective-C
                                                   104         Objective-C
parent in Fig. 6 because of the logarithmic scale.
                                                   103
                                   milliseconds




                                                                                            M∗
                                                   102

                                                   101

                                                   100

                                                  10−1
                                                         100        101       102         103        104   105
                                                                    method calls per context switch (M)

                                  Fig. 6. Performance comparison using logarithmic scale (N = 50, K = 50).
                                                                                         iPhone 3GS, iOS 4.0     21
  Thursday 21 October 2010
1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency

   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

Thursday 21 October 2010
1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency

   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)
                                                          Time for questions!
   ➡      Ensure behaviour consistency
                                                      http://ambience.info.ucl.ac.be
Thursday 21 October 2010

More Related Content

Similar to Subjective-C: Bringing Context to Mobile Platform Programming (20)

PDF
Hands on puremvc
diomampo
 
PDF
Ruby World
evanphx
 
PDF
Boost your-oop-with-fp
Uberto Barbini
 
PDF
Context-Oriented Programming
kim.mens
 
PPTX
Maritime Information Visualization
Vu Hung Nguyen
 
DOCX
Create two Java classes Phone.java and PhoneGUI.java. Phone.java wi.docx
taminklsperaw
 
PDF
REST & Hypermedia: the missing links and why you need them - Ruby Hoedown 2012
Phil Harvey
 
PPT
Trends in Programming Technology you might want to keep an eye on af Bent Tho...
InfinIT - Innovationsnetværket for it
 
PDF
Play concurrency
Justin Long
 
PDF
Week 5
EasyStudy3
 
PDF
Week 5
EasyStudy3
 
PDF
Akka scalaliftoff london_2010
Skills Matter
 
PDF
Appengine ja-night-10
John Woodell
 
PDF
Java Dating - JSR 310 Lightning Talk
Jim Gough
 
PDF
Automatic Android Telecaller App Using REST API
IRJET Journal
 
PDF
2012 icse program comprehension
Walid Maalej
 
PPTX
Caller ID Android Application
Smitakshi Sen
 
PDF
AmbientTalk Rfid Dais2010
alombide
 
KEY
Back to the future with Java 7 (Geekout June/2011)
Martijn Verburg
 
PDF
SCAM 2012 Keynote Slides on Cooperative Testing and Analysis by Tao Xie
Tao Xie
 
Hands on puremvc
diomampo
 
Ruby World
evanphx
 
Boost your-oop-with-fp
Uberto Barbini
 
Context-Oriented Programming
kim.mens
 
Maritime Information Visualization
Vu Hung Nguyen
 
Create two Java classes Phone.java and PhoneGUI.java. Phone.java wi.docx
taminklsperaw
 
REST & Hypermedia: the missing links and why you need them - Ruby Hoedown 2012
Phil Harvey
 
Trends in Programming Technology you might want to keep an eye on af Bent Tho...
InfinIT - Innovationsnetværket for it
 
Play concurrency
Justin Long
 
Week 5
EasyStudy3
 
Week 5
EasyStudy3
 
Akka scalaliftoff london_2010
Skills Matter
 
Appengine ja-night-10
John Woodell
 
Java Dating - JSR 310 Lightning Talk
Jim Gough
 
Automatic Android Telecaller App Using REST API
IRJET Journal
 
2012 icse program comprehension
Walid Maalej
 
Caller ID Android Application
Smitakshi Sen
 
AmbientTalk Rfid Dais2010
alombide
 
Back to the future with Java 7 (Geekout June/2011)
Martijn Verburg
 
SCAM 2012 Keynote Slides on Cooperative Testing and Analysis by Tao Xie
Tao Xie
 

More from kim.mens (20)

PDF
Software Maintenance and Evolution
kim.mens
 
PDF
Software Reuse and Object-Oriented Programming
kim.mens
 
PDF
Bad Code Smells
kim.mens
 
PDF
Object-Oriented Design Heuristics
kim.mens
 
PDF
Software Patterns
kim.mens
 
PDF
Code Refactoring
kim.mens
 
PDF
Domain Modelling
kim.mens
 
PDF
Object-Oriented Application Frameworks
kim.mens
 
PDF
Towards a Context-Oriented Software Implementation Framework
kim.mens
 
PDF
Towards a Taxonomy of Context-Aware Software Variabilty Approaches
kim.mens
 
PDF
Breaking the Walls: A Unified Vision on Context-Oriented Software Engineering
kim.mens
 
PDF
Basics of reflection
kim.mens
 
PDF
Advanced Reflection in Java
kim.mens
 
PDF
Basics of reflection in java
kim.mens
 
PDF
Reflection in Ruby
kim.mens
 
PDF
Introduction to Ruby
kim.mens
 
PDF
Introduction to Smalltalk
kim.mens
 
PDF
A gentle introduction to reflection
kim.mens
 
PDF
Managing the Evolution of Information Systems with Intensional Views and Rela...
kim.mens
 
PDF
Usage contracts (presented at SATToSE 2014 in L'Aquila, Italy)
kim.mens
 
Software Maintenance and Evolution
kim.mens
 
Software Reuse and Object-Oriented Programming
kim.mens
 
Bad Code Smells
kim.mens
 
Object-Oriented Design Heuristics
kim.mens
 
Software Patterns
kim.mens
 
Code Refactoring
kim.mens
 
Domain Modelling
kim.mens
 
Object-Oriented Application Frameworks
kim.mens
 
Towards a Context-Oriented Software Implementation Framework
kim.mens
 
Towards a Taxonomy of Context-Aware Software Variabilty Approaches
kim.mens
 
Breaking the Walls: A Unified Vision on Context-Oriented Software Engineering
kim.mens
 
Basics of reflection
kim.mens
 
Advanced Reflection in Java
kim.mens
 
Basics of reflection in java
kim.mens
 
Reflection in Ruby
kim.mens
 
Introduction to Ruby
kim.mens
 
Introduction to Smalltalk
kim.mens
 
A gentle introduction to reflection
kim.mens
 
Managing the Evolution of Information Systems with Intensional Views and Rela...
kim.mens
 
Usage contracts (presented at SATToSE 2014 in L'Aquila, Italy)
kim.mens
 
Ad

Recently uploaded (20)

PDF
Windsurf Meetup Ottawa 2025-07-12 - Planning Mode at Reliza.pdf
Pavel Shukhman
 
PDF
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
PDF
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
PPTX
UiPath Academic Alliance Educator Panels: Session 2 - Business Analyst Content
DianaGray10
 
PDF
Timothy Rottach - Ramp up on AI Use Cases, from Vector Search to AI Agents wi...
AWS Chicago
 
PDF
SFWelly Summer 25 Release Highlights July 2025
Anna Loughnan Colquhoun
 
PDF
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
PPTX
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
PDF
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
PDF
Blockchain Transactions Explained For Everyone
CIFDAQ
 
PDF
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
PDF
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
PPTX
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
PPTX
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
PDF
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
PDF
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
PPTX
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
PDF
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
Windsurf Meetup Ottawa 2025-07-12 - Planning Mode at Reliza.pdf
Pavel Shukhman
 
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
UiPath Academic Alliance Educator Panels: Session 2 - Business Analyst Content
DianaGray10
 
Timothy Rottach - Ramp up on AI Use Cases, from Vector Search to AI Agents wi...
AWS Chicago
 
SFWelly Summer 25 Release Highlights July 2025
Anna Loughnan Colquhoun
 
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
Blockchain Transactions Explained For Everyone
CIFDAQ
 
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
Ad

Subjective-C: Bringing Context to Mobile Platform Programming

  • 1. 3rd International Conference on Software Language Engineering Subjective-C Bringing Context to Mobile Platform Programming ICTEAM / UCLouvain / Belgium Sebastián González Alfredo Cádiz Nicolás Cardozo Jean-Christophe Libbrecht Kim Mens Julien Goffaux 13 October 2010 Thursday 21 October 2010
  • 2. 3rd International Conference on Software Language Engineering Subjective-C Bringing Context to Mobile Platform Programming ICTEAM / UCLouvain / Belgium Sebastián González Sebastián González Alfredo Cádiz Nicolás Cardozo Jean-Christophe Libbrecht Kim Mens Kim Mens Julien Goffaux 13 October 2010 Thursday 21 October 2010
  • 3. 3rd International Conference on Software Language Engineering Subjective-C Bringing Context to Mobile Platform Programming ICTEAM / UCLouvain / Belgium Sebastián González Sebastián González Alfredo Cádiz Nicolás Cardozo Jean-Christophe Libbrecht Kim Mens Kim Mens Julien Goffaux 13 October 2010 Thursday 21 October 2010
  • 4. Research Context 2 Thursday 21 October 2010
  • 5. Research Goal Programming Mindset Towards a Mindset Shift programming in isolation programming in context ? ? ? ? ? ? 3 Thursday 21 October 2010
  • 6. Contemporary Solution conditional statements 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( ) ); else play( phone.ringTone( ), 10 ); } 4 Thursday 21 October 2010
  • 7. Contemporary Solution conditional statements 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( ) ); else play( phone.ringTone( ), 10 ); } 4 Thursday 21 October 2010
  • 8. Contemporary Solution conditional statements 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( ) ); else play( phone.ringTone( ), 10 ); } 4 Thursday 21 October 2010
  • 9. Contemporary Solution conditional statements 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( ) ); else play( phone.ringTone( ), 10 ); } 4 Thursday 21 October 2010
  • 10. Contemporary Solution conditional statements 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( ) ); else play( phone.ringTone( ), 10 ); ∅ } 4 Thursday 21 October 2010
  • 11. Contemporary Solution conditional statements 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( ) ); else play( phone.ringTone( ), 10 ); ∅ } Tangled Scattered Fixed 4 Thursday 21 October 2010
  • 12. Contemporary Solution special software architectures class Phone { attribute strategy; method receive ( call ) { strategy.receive ( call ); } } 5 Thursday 21 October 2010
  • 13. Contemporary Solution special software architectures class DefaultStrategy { method receive ( call ) { ... } } class QuietStrategy class Phone { method receive ( call ) { ... } } · { attribute strategy; method receive ( call ) class OffHookStrategy { strategy.receive ( call ); } } { method receive ( call ) { ... } } class UnavailableStrategy { method receive ( call ) { ... } } 5 Thursday 21 October 2010
  • 14. Contemporary Solution special software architectures class DefaultStrategy { method receive ( call ) { ... } } class QuietStrategy class Phone { method receive ( call ) { ... } } · { attribute strategy; method receive ( call ) class OffHookStrategy { strategy.receive ( call ); } } { method receive ( call ) { ... } } class UnavailableStrategy { method receive ( call ) { ... } } 5 Thursday 21 October 2010
  • 15. Contemporary Solution special software architectures class DefaultStrategy { method receive ( call ) { ... } } class QuietStrategy class Phone { method receive ( call ) { ... } } { attribute strategy; method receive ( call ) class OffHookStrategy { strategy.receive ( call ); } } { method receive ( call ) { ... } } class UnavailableStrategy { method receive ( call ) { ... } } Infrastructural burden Fixed adaptation points 5 Thursday 21 October 2010
  • 16. Solution Language engineering approach 6 Thursday 21 October 2010
  • 17. Solution Language engineering approach Multiple dispatch Prototypes PMD CLOS Self Smalltalk 6 Thursday 21 October 2010
  • 18. Solution Language engineering approach Subjective Programming Multiple dispatch Prototypes PMD CLOS Self Smalltalk 6 Thursday 21 October 2010
  • 19. Solution Language engineering approach Context Management Subjective Programming Multiple dispatch Prototypes PMD CLOS Self Smalltalk 6 Thursday 21 October 2010
  • 20. Solution Language engineering approach Context-Oriented Programming Context Management Subjective Programming Multiple dispatch Prototypes PMD CLOS Self Smalltalk 6 Thursday 21 October 2010
  • 21. Solution Language engineering approach Ambience [DLS’07] Context-Oriented Programming Context Management Subjective Programming Multiple dispatch Prototypes PMD CLOS Self Smalltalk 6 Thursday 21 October 2010
  • 22. Solution Language engineering approach Ambience [DLS’07] Context-Oriented Programming Context Management Subjective Programming Multiple dispatch Prototypes PMD CLOS Self Smalltalk 6 Thursday 21 October 2010
  • 23. Solution Language engineering approach Ambience [DLS’07] Context-Oriented Programming Context Management Subjective Programming Multiple dispatch Single dispatch Prototypes Open Classes PMD Objective-C CLOS C Self Smalltalk 6 Thursday 21 October 2010
  • 24. Solution Language engineering approach Ambience [DLS’07] Context-Oriented Programming Context Management Subjective Programming Subjective Programming Multiple dispatch Single dispatch Prototypes Open Classes PMD Objective-C CLOS C Self Smalltalk 6 Thursday 21 October 2010
  • 25. Solution Language engineering approach Ambience [DLS’07] Context-Oriented Programming Context Management Context Management Subjective Programming Subjective Programming Multiple dispatch Single dispatch Prototypes Open Classes PMD Objective-C CLOS C Self Smalltalk 6 Thursday 21 October 2010
  • 26. Solution Language engineering approach Ambience [DLS’07] Context-Oriented Programming Context-Oriented Programming Context Management Context Management Subjective Programming Subjective Programming Multiple dispatch Single dispatch Prototypes Open Classes PMD Objective-C CLOS C Self Smalltalk 6 Thursday 21 October 2010
  • 27. Solution Language engineering approach Ambience [DLS’07] Subjective-C Context-Oriented Programming Context-Oriented Programming Context Management Context Management Subjective Programming Subjective Programming Multiple dispatch Single dispatch Prototypes Open Classes PMD Objective-C CLOS C Self Smalltalk 6 Thursday 21 October 2010
  • 28. Solution Language engineering approach Ambience [DLS’07] Subjective-C Context-Oriented Programming Context-Oriented Programming Context Management Context Management Subjective Programming Subjective Programming Multiple dispatch Single dispatch Prototypes Open Classes PMD Objective-C CLOS C Self Smalltalk 6 Thursday 21 October 2010
  • 29. Subjective-C 1 General mindset 2 Context definition 3 Context-specific behaviour 4 Context relations 5 Implementation 6 Efficiency 7 Thursday 21 October 2010
  • 30. 1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency Context-Aware System Architecture World Sensors Actuators Subjective-C Context Application Discovery Behaviour external internal context effect context information arbitrated context Context changes Active Management Context 8 Thursday 21 October 2010
  • 31. 1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency What is context? Computationally Well-defined situation accessible information (Contexts) Z axis = 0.03 Landscape orientation Battery charge = 220 mAh Low battery charge Idle cycles = 11 MHz High CPU load Location = 51°26′N 5°29′E Eindhoven no semantics action can be taken situations can be reified 9 Thursday 21 October 2010
  • 32. 1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency Case Study 10 Thursday 21 October 2010
  • 33. 1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency Case Study UILabel class drawTextInRect: Draws the receiver’s text in the specified rectangle. - (void)drawTextInRect:(CGRect)rect Parameters rect The rectangle in which to draw the text. Discussion You 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. Availability Available in iOS 2.0 and later. Declared In UILabel.h 11 Thursday 21 October 2010
  • 34. 1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency Case Study UILabel class drawTextInRect: Draws the receiver’s text in the specified rectangle. - (void)drawTextInRect:(CGRect)rect Parameters rect The rectangle in which to draw the text. Discussion You 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. Availability Available in iOS 2.0 and later. Declared In UILabel.h 11 Thursday 21 October 2010
  • 35. 1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency Subjective-C: Look & Feel @implementation UILabel (color) #context Landscape - (void)drawTextInRect:(CGRect)rect { self.textColor = [UIColor greenColor]; [superContext drawTextInRect:rect]; } @end 12 Thursday 21 October 2010
  • 36. 1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency Subjective-C: Look & Feel Open classes @implementation UILabel (color) Objective-C #context Landscape - (void)drawTextInRect:(CGRect)rect { self.textColor = [UIColor greenColor]; [superContext drawTextInRect:rect]; } @end 12 Thursday 21 October 2010
  • 37. 1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency Subjective-C: Look & Feel Open classes @implementation UILabel (color) Objective-C COP #context Landscape Subjective-C - (void)drawTextInRect:(CGRect)rect { self.textColor = [UIColor greenColor]; [superContext drawTextInRect:rect]; } @end 12 Thursday 21 October 2010
  • 38. 1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency Subjective-C: Look & Feel Open classes @implementation UILabel (color) Objective-C COP #context Landscape Subjective-C - (void)drawTextInRect:(CGRect)rect { self.textColor = [UIColor greenColor]; [superContext drawTextInRect:rect]; } @end ✓ Adaptation of any existing component ✓ No access to original source code needed ✓ Adaptations can be cleanly modularised 12 Thursday 21 October 2010
  • 39. 1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency Context Relations Strong Inclusion Eindhoven Netherlands Weak Inclusion Museum Quiet Requirement HDVideo BatteryHigh Exclusion Landscape Portrait 13 Thursday 21 October 2010
  • 40. 1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency Programmatic Context Definition // Create contexts Context* 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 Thursday 21 October 2010
  • 41. 1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency Context Declaration Language Contexts: Relations: // Create contexts Context* Room = [[Context alloc] initWithName:@"Room"]; Context* Bedroom = [[Context alloc] initWithName:@"Bedroom"]; >< Night Room Day Context* Livingroom Day = [[Context alloc] initWithName:@"Day"]; Sun >< Rain Context* Kitchen Night = [[Context alloc] initWithName:@"Night"]; ... Bedroom Livingroom -> TV Bathroom Livingroom -> Window // Register contexts Day Livingroom -> Luminosity [CONTEXT addContext:Room]; [CONTEXT addContext:Bedroom]; Night Livingroom -> Temperature [CONTEXT addContext:Day]; Kitchen -> Luminosity [CONTEXT addContext:Night]; TV Bedroom -> Temperature ... Window Bedroom -> Luminosity Temperature Bedroom -> Window // Establish context relations Luminosity Bathroom -> Temperature [Day addExclusionRelationWith:Night]; [Bedroom addHardInclusionRelationWith:Room]; Bathroom -> Luminosity ... Sun Rain Bathroom => Room Electricity Kitchen => Room Livingroom => Room Bedroom => Room TV =< Electricity Luminosity =< Electricity Window =< Sun 14 Thursday 21 October 2010
  • 42. 1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency Context Declaration Language // Create contexts Context* 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 Thursday 21 October 2010
  • 43. 1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency Other Generated Code // Create contexts Context* 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 #context Bedroom [Day addExclusionRelationWith:Night]; -(Context*) getActiveRoom{ [Bedroom addHardInclusionRelationWith:Room]; ! return [CONTEXT getContext:@"Bedroom"]; ... } // Register context-specific methods [MANAGER addMethod:@selector(Context_Bedroom_getActiveRoom) forClass:[DomoticsServerViewController class] forContextNames:[[NSSet alloc] initWithObjects:@"Bedroom" ,nil] withDefautSel:@selector(getActiveRoom) withPriority:0]; ... 14 Thursday 21 October 2010
  • 44. 1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency Other Generated Code // Create contexts Context* 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]; ... // Register context-specific methods [MANAGER addMethod:@selector(Context_Bedroom_getActiveRoom) forClass:[DomoticsServerViewController class] forContextNames:[[NSSet alloc] initWithObjects:@"Bedroom" ,nil] withDefautSel:@selector(getActiveRoom) withPriority:0]; ... 14 Thursday 21 October 2010
  • 45. 1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency Application Contexts @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 Thursday 21 October 2010
  • 46. 1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency Application Contexts @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; } 16 Thursday 21 October 2010
  • 47. 1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency Application Contexts @implementation LowpassFilter Contexts: #context LowPassFilter Adaptive -(void)addAcceleration:(UIAcceleration*)accel Paused { LowPassFilter ! double alpha = [self getAlpha:accel]; HighPassFilter ! ! x = accel.x * alpha + x * (1.0 - alpha); Filter ! y = accel.y * alpha + y * (1.0 - alpha); Relations: ! z = accel.z * alpha + z * (1.0 - alpha); } LowPassFilter >< HighPassFilter LowPassFilter => Filter #context !Adaptive HighPassFilter => Filter -(double) getAlpha:(UIAcceleration*)accel{ ! return filterConstant; } #context LowPassFilter Adaptive -(double) getAlpha:(UIAcceleration*)accel { ! return // big formula to calculate the alpha level; } 16 Thursday 21 October 2010
  • 48. 1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency Implementation dynamic method pre-dispatch UILabel @property NSString *text @property UIFont *font ... Portrait impl - (void)Portrait_drawTextInRect:(CGRect)rect - (void)Landscape_drawTextInRect:(CGRect)rect Landscape impl - (void)Default_drawTextInRect:(CGRect)rect - (void)drawTextInRect:(CGRect)rect Default impl 17 Thursday 21 October 2010
  • 49. 1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency Implementation dynamic method pre-dispatch UILabel @property NSString *text @property UIFont *font ... vtable Portrait impl - (void)Portrait_drawTextInRect:(CGRect)rect - (void)Landscape_drawTextInRect:(CGRect)rect Landscape impl - (void)Default_drawTextInRect:(CGRect)rect - (void)drawTextInRect:(CGRect)rect Default impl 17 Thursday 21 October 2010
  • 50. 1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency Implementation dynamic method pre-dispatch UILabel @property NSString *text @property UIFont *font ... Portrait impl - (void)Portrait_drawTextInRect:(CGRect)rect - (void)Landscape_drawTextInRect:(CGRect)rect Landscape impl - (void)Default_drawTextInRect:(CGRect)rect - (void)drawTextInRect:(CGRect)rect Default impl 17 Thursday 21 October 2010
  • 51. 1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency Implementation dynamic method pre-dispatch UILabel @property NSString *text @property UIFont *font ... Portrait impl - (void)Portrait_drawTextInRect:(CGRect)rect - (void)Landscape_drawTextInRect:(CGRect)rect Landscape impl - (void)Default_drawTextInRect:(CGRect)rect - (void)drawTextInRect:(CGRect)rect Default impl 17 Thursday 21 October 2010
  • 52. 1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency Implementation dynamic method pre-dispatch UILabel @property NSString *text @property UIFont *font ... Portrait impl - (void)Portrait_drawTextInRect:(CGRect)rect - (void)Landscape_drawTextInRect:(CGRect)rect Landscape impl - (void)Default_drawTextInRect:(CGRect)rect - (void)drawTextInRect:(CGRect)rect Default impl [CONTEXT activateContextWithName: @"Landscape"]; 17 Thursday 21 October 2010
  • 53. 1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency Implementation dynamic method pre-dispatch UILabel @property NSString *text @property UIFont *font ... Portrait impl - (void)Portrait_drawTextInRect:(CGRect)rect - (void)Landscape_drawTextInRect:(CGRect)rect Landscape impl - (void)Default_drawTextInRect:(CGRect)rect - (void)drawTextInRect:(CGRect)rect Default impl [CONTEXT activateContextWithName: @"Landscape"]; 17 Thursday 21 October 2010
  • 54. 1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency Implementation dynamic method pre-dispatch UILabel @property NSString *text @property UIFont *font ... Portrait impl - (void)Portrait_drawTextInRect:(CGRect)rect - (void)Landscape_drawTextInRect:(CGRect)rect Landscape impl - (void)Default_drawTextInRect:(CGRect)rect - (void)drawTextInRect:(CGRect)rect Default impl [CONTEXT activateContextWithName: @"Landscape"]; [CONTEXT deactivateContextWithName: @"Landscape"]; 17 Thursday 21 October 2010
  • 55. 1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency Implementation dynamic method pre-dispatch UILabel @property NSString *text @property UIFont *font ... Portrait impl - (void)Portrait_drawTextInRect:(CGRect)rect - (void)Landscape_drawTextInRect:(CGRect)rect Landscape impl - (void)Default_drawTextInRect:(CGRect)rect - (void)drawTextInRect:(CGRect)rect Default impl [CONTEXT activateContextWithName: @"Landscape"]; [CONTEXT deactivateContextWithName: @"Landscape"]; 17 Thursday 21 October 2010
  • 56. 1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency Implementation dynamic method pre-dispatch UILabel @property NSString *text @property UIFont *font ... Portrait impl - (void)Portrait_drawTextInRect:(CGRect)rect - (void)Landscape_drawTextInRect:(CGRect)rect Landscape impl - (void)Default_drawTextInRect:(CGRect)rect - (void)drawTextInRect:(CGRect)rect Default impl [CONTEXT activateContextWithName: @"Landscape"]; [CONTEXT deactivateContextWithName: @"Landscape"]; ‣ no additional cost for method invocations ‣ cost incurred at context switching time 17 Thursday 21 October 2010
  • 57. 1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency What is more efficient? Subjective-C Objective-C context-specific methods conditional statements #context C1 -(void) test:(int) mode { -(void) test { result = 1; } if (mode == 1) result = 1; ... ... #context CN -(void) test else if (mode == N) { result = N; } result = N; -(void) test else { result = 0; } result = 0; } 18 Thursday 21 October 2010
  • 58. 1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency Benchmark Setup Subjective-C #context C1 -(void) test for (int i = 0; i < 1000; i++) { { result = 1; } // switch context ... if (i % 2) [CONTEXT activateContextWithName:@"CK"]; #context CK else -(void) test [CONTEXT deactivateContextWithName:@"CK"]; { result = K; } // call test method repeatedly ... for (int j = 0; j < M; j++) [self test]; #context CN } -(void) test { result = N; } -(void) test { result = 0; } 19 Thursday 21 October 2010
  • 59. 1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency Benchmark Setup Subjective-C #context C1 -(void) test for (int i = 0; i < 1000; i++) { { result = 1; } // switch context ... if (i % 2) [CONTEXT activateContextWithName:@"CK"]; #context CK else -(void) test [CONTEXT deactivateContextWithName:@"CK"]; { result = K; } // call test method repeatedly ... for (int j = 0; j < M; j++) [self test]; #context CN } -(void) test { result = N; } N: number of variations -(void) test K: selected branch { result = 0; } M: method calls per mode switch 19 Thursday 21 October 2010
  • 60. 1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency Benchmark Setup Objective-C -(void) test:(int) mode { for (int i = 0; i < 1000; i++) { if (mode == 1) result = 1; // toggle operation mode if (i % 2) ... mode = K; else if (mode == K) mode = 0; result = K; // call test method repeatedly ... for (int j = 0; j < M; j++) [self test: mode]; else if (mode == N) } result = N; else N: number of variations result = 0; K: selected branch } M: method calls per mode switch 20 Thursday 21 October 2010
  • 61. e case of 1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency Fig. 6. N K M∗ N: number of variations 5 1 8230 10 1 4708 K: selected branch 50 5 1 5 3405 4521 M: method calls per context switch 10 10 2653 50 50 1148 105 ting point M ∗ between Subjective-C and Objective-C. Subjective-C 104 Objective-C parent in Fig. 6 because of the logarithmic scale. 103 milliseconds M∗ 102 101 100 10−1 100 101 102 103 104 105 method calls per context switch (M) Fig. 6. Performance comparison using logarithmic scale (N = 50, K = 50). iPhone 3GS, iOS 4.0 21 Thursday 21 October 2010
  • 62. 1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency 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 Thursday 21 October 2010
  • 63. 1 Mindset 2 Context 3 Behaviour 4 Relations 5 Implementation 6 Efficiency 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) Time for questions! ➡ Ensure behaviour consistency http://ambience.info.ucl.ac.be Thursday 21 October 2010