An introductory lecture on context-oriented programming, part of a full course on Programming Paradigms at UCL university in Belgium, focussing on reflection and meta programming techniques in a variety of languages. This particular lecture was made by Sebastian Gonzalez in close collaboration with Kim Mens.
Physics Serway Jewett 6th edition for Scientists and Engineers
Context-oriented programming
1. Lecture 08 :
Context-Oriented Programming
Overview and Applications
LSINF 2335
Programming Paradigms
Prof. Kim Mens
Dr. Sebastián González
UCL / EPL / INGI
Disclaimer: any pictures used in this presentation remain
with the copyright of their original owner and were
included here for didactical purposes only. In case of any
suspected violation of copyright please contact me so
that I can remove or replace those pictures.
2. Why?
… through a programming
language engineering approach
enable context-driven behaviour
adaptability …
3. Hardware Phenomena 3
fixed (1980)
mainframes
servers
desktops
consoles
portable (1990)
laptops
netbooks
subnotebooks
mobile (2000)
handhelds
tablets
smartphones
‣ CPU load
‣ available memory (RAM)
‣ available storage (HD)
‣ date and time
‣ connected peripherals
‣ network peers
‣ touch screen
‣ geographical location
‣ GPS signal quality
‣ accelerometer
‣ wi-fi signal quality
‣ battery power
‣ camera
‣ microphone
‣ light sensor
+
+
…
4. text (1970)
BSD
SunOS
MS DOS
GNU/Linux
graphical (1980)
Mac OS
Amiga OS
Windows
KDE, GNOME
web (1990)
static
dynamic
web 2.0
mashups
mobile (2000)
Symbian OS
Windows CE
iOS
Android
Software Phenomena 4
‣ available libraries
‣ available hardware services
‣ available network services
‣ user task
‣ user expertise
‣ user preferences
‣ user privileges
‣ task urgency
‣ operation modes
‣ logging
‣ debugging
‣ degraded
‣ free trial
‣ partial failure
‣ domain specific
‣ [3D] wireframe / solid view
‣ [Maps] satellite / schematic
‣ ...
5. Applications should be aware of their execution context, and
should adapt dynamically to such context so that they can provide
a service that matches client needs to the best extent possible.
environmental properties
humidity, light, noise, lighting
network peers & services
projector, GPS, storage
internal state
load, time, battery
spatial state
position, orientation,
movement
location semantics
nearby objects & facilities
users
expertise, preferences
Context Is Key 5
6. take advantage of room projector for presentation
peer service
decrease playback quality when battery power is low
internal state
user task
show parking spots and gas stations (only) when driving
environmental conditions
give more detailed indications when visibility is low
disable phone ringtone in quiet places
location semantics
Adaptation Examples 6
7. Why Aren’t We There Yet? 7
Richard Gabriel, 2006
Software systems today are produced according to a
manufacturing model: a finished product is constructed
at the factory and shipped to its final destination where
it is expected to act like any other machine —reliable
but oblivious to its surroundings and its own welfare.
8. we still program this...
using the
programming models
conceived for this....
(2010) (1980)
Mindset Mismatch 8
9. Current programming techniques and design principles invite
programmers to think in a way that is mostly oblivious of the physical,
technical and human environment in which the software will be used.
Many chances of delivering improved services are thus missed.
programming in isolation
?
?
?
?
?
?
Current Mindset 9
10. programming with context
A new paradigm is needed that helps overcoming this limiting vision
by putting programmers in the right state of mind to build
dynamically adaptable applications from the ground up.
Needed Mindset 10
11. programming in isolation programming with context
forward!
?
?
?
?
?
?
Towards A Mindset Shift 11
37. class phone {
method receive ( call ) {
if ( ) then
else if ( ) then
else if ( ) then
else
}
Paradigmatic Shortcomings 14
conditional statements
38. class phone {
method receive ( call ) {
if ( ) then
else if ( ) then
else if ( ) then
else
}
Paradigmatic Shortcomings 14
conditional statements
Adaptable
39. default
phone user
phone environment
phone status
class phone {
method receive ( call ) {
if ( ) then
else if ( ) then
else if ( ) then
else
}
Paradigmatic Shortcomings 14
conditional statements
Adaptable
40. default
phone user
phone environment
phone status
class phone {
method receive ( call ) {
if ( ) then
else if ( ) then
else if ( ) then
else
}
Paradigmatic Shortcomings 14
conditional statements
Adaptable
Tangled
Scattered
Fixed
No reuse
Complex logic
48. Paradigmatic Shortcomings 16
Software rigidness
The variability points of the application are hard-
coded in its architecture. It is difficult to add new
variants non-invasively.
Lack of modularity
Tight coupling between core business logic and
infrastructural code to manage the variants makes the
software difficult to maintain and evolve.
Mindset mismatch
Programming tools make programmers oblivious of
the context in which their applications will run.
Programmers are not put in the right state of mind to
build adaptable software.
General Symptoms (Recap)
49. Hypothesis 17
current programming tools
adaptive systems
we need to reengineer our tools
A major obstacle for adaptability is the unavailability of appropriate
context-aware programming languages and related tool sets.
50. Approach 18
programming abstractions matter
tool 1: C#
using System;
public class Program
{
static long Factorial(long number)
{
if(number <= 1)
return 1;
else
return number * Factorial(number - 1);
}
static int Main(string[] args) {
Console.WriteLine(Factorial(5));
return 0;
}
}
tool 2: Ruby
def fact(n)
n <= 1 ? 1 : n * fact(n - 1)
end
fact(5)
n! =
{
1
n(n-1)!
if n = 0
if n > 0
maintainability
domain: math
51. Approach
A high-level language frees a program from
much of its accidental complexity; it eliminates
a whole level of complexity that was never
inherent in the program at all.
programming language engineering
19
Develop programming tools that reduce accidental complexity
in the expression of context-driven behaviour adaptation
Frederick Brooks, 1987
essential complexity ≠
accidental complexity
55. Context-Driven System Architecture 21
external context
effect
context
information
ActuatorsSensors
arbitrated
context
changes
World
Context
Discovery
Context
Management
Active
Context
internal
Application
Behaviour
56. Contexts As Situation Reifiers 22
computationally
accessible data
Battery charge = 220 mAh
Idle cycles = 100 MHz
User agent = “Mozilla/5.0...”
Z axis = 0.03
57. Contexts As Situation Reifiers 22
computationally
accessible data
no semantics
Battery charge = 220 mAh
Idle cycles = 100 MHz
User agent = “Mozilla/5.0...”
Z axis = 0.03
58. Contexts As Situation Reifiers 22
computationally
accessible data
no semantics
Battery charge = 220 mAh
Idle cycles = 100 MHz
User agent = “Mozilla/5.0...”
Z axis = 0.03
action can be taken
well-defined
situations
Context
Discovery
Low battery charge
High CPU load
Firefox
Landscape orientation
59. Contexts As Situation Reifiers 22
computationally
accessible data
no semantics
Battery charge = 220 mAh
Idle cycles = 100 MHz
User agent = “Mozilla/5.0...”
Z axis = 0.03
action can be taken
well-defined
situations
Context
Discovery
contexts are reified situations
for which adapted application behaviour can be defined
Low battery charge
High CPU load
Firefox
Landscape orientation
60. Contexts As Situation Reifiers 22
computationally
accessible data
no semantics
Battery charge = 220 mAh
Idle cycles = 100 MHz
User agent = “Mozilla/5.0...”
Z axis = 0.03
action can be taken
well-defined
situations
Context
Discovery
contexts are reified situations
for which adapted application behaviour can be defined
Low battery charge
High CPU load
Firefox
Landscape orientation
LowBattery = new Context();
window.addEventListener(‘batterystatus’,
function (battery) {
if (battery.level < 30)
LowBattery.activate();
else
LowBattery.deactivate(); });
… in JavaScript
61. Contexts As Situation Reifiers 22
computationally
accessible data
no semantics
Battery charge = 220 mAh
Idle cycles = 100 MHz
User agent = “Mozilla/5.0...”
Z axis = 0.03
action can be taken
well-defined
situations
Context
Discovery
contexts are reified situations
for which adapted application behaviour can be defined
Low battery charge
High CPU load
Firefox
Landscape orientation
LowBattery = new Context();
window.addEventListener(‘batterystatus’,
function (battery) {
if (battery.level < 30)
LowBattery.activate();
else
LowBattery.deactivate(); });
… in JavaScript
63. Standard Widget Spec
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
24
67. Main Idea 25
@implementation UILabel (color)
@contexts Landscape
- (void)drawTextInRect:(CGRect)rect {
self.textColor = [UIColor greenColor];
return @resend();
}
@end
Open classes
Objective-C
COP
Subjective-C
Application
Behaviour
68. Main Idea 25
@implementation UILabel (color)
@contexts Landscape
- (void)drawTextInRect:(CGRect)rect {
self.textColor = [UIColor greenColor];
return @resend();
}
@end
Open classes
Objective-C
COP
Subjective-C
✓ Adaptation of any existing component
✓ No access to original source code needed
✓ Adaptations can be cleanly modularised
Application
Behaviour
71. Avoiding Boilerplate 27
// 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];
…
Relations:
Day >< Night
Sun >< Rain
Livingroom -> TV
Livingroom -> Window
Livingroom -> Luminosity
Livingroom -> Temperature
Kitchen -> Luminosity
Bedroom -> Temperature
Bedroom -> Luminosity
Bedroom -> Window
Bathroom -> Temperature
Bathroom -> Luminosity
Bathroom => Room
Kitchen => Room
Livingroom => Room
Bedroom => Room
TV =< Electricity
Luminosity =< Electricity
Window =< Sun
Contexts:
Room
Livingroom
Kitchen
Bedroom
Bathroom
Day
Night
TV
Window
Temperature
Luminosity
Sun
Rain
Electricity
72. @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";
}
...
Case Study: Hard-Coded Operation Mode 28
73. @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";
}
...
Case Study: Hard-Coded Operation Mode 28
74. @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";
}
...
Case Study: Hard-Coded Operation Mode 28
Operation mode (intrinsic context)Adaptive
75. Case Study: Refactoring Using Logical Contexts 29
@implementation LowpassFilter
@contexts 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);
}
@contexts !Adaptive
-(double) getAlpha:(UIAcceleration*)accel{
return filterConstant;
}
@contexts Adaptive LowPassFilter
-(double) getAlpha:(UIAcceleration*)accel {
return // big formula to calculate the alpha level;
}
Adaptive
Adaptive
76. Case Study: Refactoring Using Logical Contexts 29
@implementation LowpassFilter
@contexts 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);
}
@contexts !Adaptive
-(double) getAlpha:(UIAcceleration*)accel{
return filterConstant;
}
@contexts Adaptive LowPassFilter
-(double) getAlpha:(UIAcceleration*)accel {
return // big formula to calculate the alpha level;
}
77. Case Study: Refactoring Using Logical Contexts 29
@implementation LowpassFilter
@contexts 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);
}
@contexts !Adaptive
-(double) getAlpha:(UIAcceleration*)accel{
return filterConstant;
}
@contexts Adaptive LowPassFilter
-(double) getAlpha:(UIAcceleration*)accel {
return // big formula to calculate the alpha level;
}
Relations:
LowPassFilter >< HighPassFilter
LowPassFilter => Filter
HighPassFilter => Filter
Contexts:
Adaptive
Paused
LowPassFilter
HighPassFilter
Filter
78. Logging
Context Scoping 30
Local contexts
Global contexts
Landscape
LowBattery
BusyCPU
Antwerp
• Affect all threads
@activate(LowBattery)
• Usually domain-agnostic
– extrinsic to the application
– based on physical phenomena
• Activation follows external events
• Local to one thread
@activate(Adaptive in Thread1)
• Usually domain-specific
– intrinsic to the application
– based on logical events
• Activation follows business logic
Adaptive
Off-Hook
HDVideo
79. Logging
Context Scoping 30
Local contexts
Global contexts
Landscape
LowBattery
BusyCPU
Antwerp
• Affect all threads
@activate(LowBattery)
• Usually domain-agnostic
– extrinsic to the application
– based on physical phenomena
• Activation follows external events
• Local to one thread
@activate(Adaptive in Thread1)
• Usually domain-specific
– intrinsic to the application
– based on logical events
• Activation follows business logic
Adaptive
Off-Hook
HDVideo
domain-agnostic
80. Logging
Context Scoping 30
Local contexts
Global contexts
Landscape
LowBattery
BusyCPU
Antwerp
• Affect all threads
@activate(LowBattery)
• Usually domain-agnostic
– extrinsic to the application
– based on physical phenomena
• Activation follows external events
• Local to one thread
@activate(Adaptive in Thread1)
• Usually domain-specific
– intrinsic to the application
– based on logical events
• Activation follows business logic
Adaptive
Off-Hook
HDVideo
domain-specific
domain-agnostic
81. Logging
Context Scoping 30
Local contexts
Global contexts
Landscape
LowBattery
BusyCPU
Antwerp
• Affect all threads
@activate(LowBattery)
• Usually domain-agnostic
– extrinsic to the application
– based on physical phenomena
• Activation follows external events
• Local to one thread
@activate(Adaptive in Thread1)
• Usually domain-specific
– intrinsic to the application
– based on logical events
• Activation follows business logic
Adaptive
Off-Hook
HDVideo
domain-specific
domain-agnostic
extrinsic
82. Logging
Context Scoping 30
Local contexts
Global contexts
Landscape
LowBattery
BusyCPU
Antwerp
• Affect all threads
@activate(LowBattery)
• Usually domain-agnostic
– extrinsic to the application
– based on physical phenomena
• Activation follows external events
• Local to one thread
@activate(Adaptive in Thread1)
• Usually domain-specific
– intrinsic to the application
– based on logical events
• Activation follows business logic
Adaptive
Off-Hook
HDVideo
domain-specific
intrinsic
domain-agnostic
extrinsic
83. Logging
Context Scoping 30
Local contexts
Global contexts
Landscape
LowBattery
BusyCPU
Antwerp
• Affect all threads
@activate(LowBattery)
• Usually domain-agnostic
– extrinsic to the application
– based on physical phenomena
• Activation follows external events
• Local to one thread
@activate(Adaptive in Thread1)
• Usually domain-specific
– intrinsic to the application
– based on logical events
• Activation follows business logic
Adaptive
Off-Hook
HDVideo
domain-specific
intrinsic
domain-agnostic
extrinsic
physical
84. Logging
Context Scoping 30
Local contexts
Global contexts
Landscape
LowBattery
BusyCPU
Antwerp
• Affect all threads
@activate(LowBattery)
• Usually domain-agnostic
– extrinsic to the application
– based on physical phenomena
• Activation follows external events
• Local to one thread
@activate(Adaptive in Thread1)
• Usually domain-specific
– intrinsic to the application
– based on logical events
• Activation follows business logic
Adaptive
Off-Hook
HDVideo
domain-specific
intrinsic
logical
domain-agnostic
extrinsic
physical
85. Summary 31
✓ Clean application logic
✓ Clean modularisation of adaptations
✓ Context reification and management
✓ Run-time behaviour adaptation of any component (incl. 3rd party)
✓ No need for recompilation or access to original source code
✓ Maximises adaptation points while avoiding architectural burden
✓ Scoped adaptations
language abstractions for adaptation to context … with sound technical underpinnings
@context Landscape
-id behaviour {
// context-specific logic
}
Objects
Open Classes
Reflection
Subjective Programming
Context-Oriented Programming
Subjective-C
116. Default Behaviour & Context 34
HTML.Element
addAttribute
getAttribute
...
style
title
T.HTML.Element
class
style
title
getAttribute
setAttribute
= XML.Element +
117. Default Behaviour & Context 34
= HTML.Element +
HTML.Anchor
addAttribute
...
href
type
target
T.HTML.Anchor
href
type
target
getAttribute
setAttribute
HTML.Element
addAttribute
getAttribute
...
style
title
T.HTML.Element
class
style
title
getAttribute
setAttribute
= XML.Element +
118. Default Behaviour & Context 34
= HTML.Element +
HTML.Anchor
addAttribute
...
href
type
target
T.HTML.Anchor
href
type
target
getAttribute
setAttribute
HTML.Element
addAttribute
getAttribute
...
style
title
T.HTML.Element
class
style
title
getAttribute
setAttribute
= XML.Element +
HTML.Element =
Trait.compose(XML.Element, T.HTML.Element);
HTML.Anchor =
Trait.compose(HTML.Element, T.HTML.Anchor);
... in JavaScript
119. Default Behaviour & Context 34
= HTML.Element +
HTML.Anchor
addAttribute
...
href
type
target
T.HTML.Anchor
href
type
target
getAttribute
setAttribute
Default
... for prototypical situations
Prototypical behaviour ...
HTML.Element
addAttribute
getAttribute
...
style
title
T.HTML.Element
class
style
title
getAttribute
setAttribute
= XML.Element +
( unperturbed system )
HTML.Element =
Trait.compose(XML.Element, T.HTML.Element);
HTML.Anchor =
Trait.compose(HTML.Element, T.HTML.Anchor);
... in JavaScript
120. Default Behaviour & Context 34
= HTML.Element +
HTML.Anchor
addAttribute
...
href
type
target
T.HTML.Anchor
href
type
target
getAttribute
setAttribute
Default
... for prototypical situations
Prototypical behaviour ...
HTML.Element
addAttribute
getAttribute
...
style
title
T.HTML.Element
class
style
title
getAttribute
setAttribute
= XML.Element +
( unperturbed system )
HTML.Element =
Trait.compose(XML.Element, T.HTML.Element);
HTML.Anchor =
Trait.compose(HTML.Element, T.HTML.Anchor);
... in JavaScript
121. Context + Traits 35
... in JavaScriptContext Traits
let Default ∈ C Default = new Context();
122. Context + Traits 35
... in JavaScriptContext Traits
let Default ∈ C Default = new Context();
a = new HTML.Anchor();let ida ∈ L
123. Context + Traits 35
... in JavaScriptContext Traits
let Default ∈ C Default = new Context();
a = new HTML.Anchor();let ida ∈ L
constitution: L ⨉ C → T
124. Context + Traits 35
W3Clet W3C ∈ C W3C = new Context();
... in JavaScriptContext Traits
let Default ∈ C Default = new Context();
a = new HTML.Anchor();let ida ∈ L
constitution: L ⨉ C → T
125. Context + Traits 35
W3Clet W3C ∈ C W3C = new Context();
... in JavaScriptContext Traits
let Default ∈ C Default = new Context();
a = new HTML.Anchor();let ida ∈ L
constitution: L ⨉ C → T
126. Context + Traits 35
W3Clet W3C ∈ C W3C = new Context();
... in JavaScriptContext Traits
let Default ∈ C Default = new Context();
a = new HTML.Anchor();let ida ∈ L
constitution: L ⨉ C → T
127. Context + Traits 35
W3Clet W3C ∈ C W3C = new Context();
... in JavaScriptContext Traits
let Default ∈ C Default = new Context();
a = new HTML.Anchor();let ida ∈ L
constitution: L ⨉ C → T
constitution( ida, W3C ) = W3C.adapt(a, T.ValidAnchor);
T.ValidAnchor
addAttribute
setAttribute
removeAttribute
parent
...
128. Context + Traits 35
W3Clet W3C ∈ C W3C = new Context();
... in JavaScriptContext Traits
Untrusted
let Default ∈ C Default = new Context();
a = new HTML.Anchor();let ida ∈ L
constitution: L ⨉ C → T
constitution( ida, W3C ) = W3C.adapt(a, T.ValidAnchor);
let Untrusted ∈ C Untrusted = new Context();
T.ValidAnchor
addAttribute
setAttribute
removeAttribute
parent
...
129. Context + Traits 35
W3C
constitution
( ida, Untrusted ) =
Untrusted.adapt
(a, T.SecureAnchor);
let W3C ∈ C W3C = new Context();
... in JavaScriptContext Traits
T.SecureAnchor
href
parent
visible
manager
...
Untrusted
let Default ∈ C Default = new Context();
a = new HTML.Anchor();let ida ∈ L
constitution: L ⨉ C → T
constitution( ida, W3C ) = W3C.adapt(a, T.ValidAnchor);
let Untrusted ∈ C Untrusted = new Context();
T.ValidAnchor
addAttribute
setAttribute
removeAttribute
parent
...
130. Context + Traits 35
W3C
constitution
( ida, Untrusted ) =
Untrusted.adapt
(a, T.SecureAnchor);
let W3C ∈ C W3C = new Context();
... in JavaScriptContext Traits
T.SecureAnchor
href
parent
visible
manager
...
Untrusted
let Default ∈ C Default = new Context();
a = new HTML.Anchor();let ida ∈ L
constitution: L ⨉ C → T
constitution( ida, W3C ) = W3C.adapt(a, T.ValidAnchor);
let Untrusted ∈ C Untrusted = new Context();
T.ValidAnchor
addAttribute
setAttribute
removeAttribute
parent
...
131. Context + Traits 35
W3C
constitution
( ida, Untrusted ) =
Untrusted.adapt
(a, T.SecureAnchor);
let W3C ∈ C W3C = new Context();
... in JavaScriptContext Traits
T.SecureAnchor
href
parent
visible
manager
...
Untrusted
let Default ∈ C Default = new Context();
a = new HTML.Anchor();let ida ∈ L
constitution: L ⨉ C → T
constitution( ida, W3C ) = W3C.adapt(a, T.ValidAnchor);
let Untrusted ∈ C Untrusted = new Context();
T.ValidAnchor
addAttribute
setAttribute
removeAttribute
parent
...
132. Context + Traits 35
W3C
constitution
( ida, Untrusted ) =
Untrusted.adapt
(a, T.SecureAnchor);
let W3C ∈ C W3C = new Context();
... in JavaScriptContext Traits
T.SecureAnchor
href
parent
visible
manager
...
Untrusted
let Default ∈ C Default = new Context();
a = new HTML.Anchor();let ida ∈ L
constitution: L ⨉ C → T
constitution( ida, W3C ) = W3C.adapt(a, T.ValidAnchor);
let Untrusted ∈ C Untrusted = new Context();
T.ValidAnchor
addAttribute
setAttribute
removeAttribute
parent
...
133. Run-Time Trait Recomposition 36
overloading of constitution: L ⨉ C → Tconstitution: L ⨉ 2C → 2T
constitution ( ida, { Default, Untrusted, W3C } )
= { HTML.Anchor, T.SecureAnchor, T.ValidAnchor }
134. Run-Time Trait Recomposition 36
overloading of constitution: L ⨉ C → Tconstitution: L ⨉ 2C → 2T
defined through composition policiescomposition: 2T → O
constitution ( ida, { Default, Untrusted, W3C } )
= { HTML.Anchor, T.SecureAnchor, T.ValidAnchor }
135. Run-Time Trait Recomposition 36
o
addAttribute
setAttribute
removeAttribute
href
parent
visible
type
target
HTML.Anchor
addAttribute
setAttribute
removeAttribute
href
parent
visible
type
target
T.ValidAnchor
addAttribute
setAttribute
removeAttribute
parent
...
T.SecureAnchor
href
parent
visible
manager
...
overloading of constitution: L ⨉ C → Tconstitution: L ⨉ 2C → 2T
defined through composition policiescomposition: 2T → O
constitution ( ida, { Default, Untrusted, W3C } )
= { HTML.Anchor, T.SecureAnchor, T.ValidAnchor }
composition ( { HTML.Anchor, T.SecureAnchor, T.ValidAnchor } ) =
▹ ▹
overriding operator
136. Run-Time Trait Recomposition 36
o
addAttribute
setAttribute
removeAttribute
href
parent
visible
type
target
HTML.Anchor
addAttribute
setAttribute
removeAttribute
href
parent
visible
type
target
T.ValidAnchor
addAttribute
setAttribute
removeAttribute
parent
...
T.SecureAnchor
href
parent
visible
manager
...
overloading of constitution: L ⨉ C → Tconstitution: L ⨉ 2C → 2T
defined through composition policiescomposition: 2T → O
constitution ( ida, { Default, Untrusted, W3C } )
= { HTML.Anchor, T.SecureAnchor, T.ValidAnchor }
composition ( { HTML.Anchor, T.SecureAnchor, T.ValidAnchor } ) =
▹ ▹
addAttribute
setAttribute
removeAttribute
href
parent
visible
type
target
href
parent
visible
addAttribute
setAttribute
removeAttribute
parent
137. Run-Time Trait Recomposition 36
o
addAttribute
setAttribute
removeAttribute
href
parent
visible
type
target
overloading of constitution: L ⨉ C → Tconstitution: L ⨉ 2C → 2T
defined through composition policiescomposition: 2T → O
constitution ( ida, { Default, Untrusted, W3C } )
= { HTML.Anchor, T.SecureAnchor, T.ValidAnchor }
composition ( { HTML.Anchor, T.SecureAnchor, T.ValidAnchor } ) =
addAttribute
setAttribute
removeAttribute
href
parent
visible
type
target
href
parent
visible
addAttribute
setAttribute
removeAttribute
parent
138. Run-Time Trait Recomposition 36
o
addAttribute
setAttribute
removeAttribute
href
parent
visible
type
target
overloading of constitution: L ⨉ C → Tconstitution: L ⨉ 2C → 2T
defined through composition policiescomposition: 2T → O
constitution ( ida, { Default, Untrusted, W3C } )
= { HTML.Anchor, T.SecureAnchor, T.ValidAnchor }
composition ( { HTML.Anchor, T.SecureAnchor, T.ValidAnchor } ) =
object = composition · constitution
addAttribute
setAttribute
removeAttribute
href
parent
visible
type
target
href
parent
visible
addAttribute
setAttribute
removeAttribute
parent
139. Run-Time Trait Recomposition 36
o
addAttribute
setAttribute
removeAttribute
href
parent
visible
type
target
overloading of constitution: L ⨉ C → Tconstitution: L ⨉ 2C → 2T
defined through composition policiescomposition: 2T → O
constitution ( ida, { Default, Untrusted, W3C } )
= { HTML.Anchor, T.SecureAnchor, T.ValidAnchor }
composition ( { HTML.Anchor, T.SecureAnchor, T.ValidAnchor } ) =
puts object composition in direct relationship
to the context of execution
object: L ⨉ 2C → O
object = composition · constitution
addAttribute
setAttribute
removeAttribute
href
parent
visible
type
target
href
parent
visible
addAttribute
setAttribute
removeAttribute
parent
140. Run-Time Trait Recomposition 36
o
addAttribute
setAttribute
removeAttribute
href
parent
visible
type
target
overloading of constitution: L ⨉ C → Tconstitution: L ⨉ 2C → 2T
defined through composition policiescomposition: 2T → O
constitution ( ida, { Default, Untrusted, W3C } )
= { HTML.Anchor, T.SecureAnchor, T.ValidAnchor }
composition ( { HTML.Anchor, T.SecureAnchor, T.ValidAnchor } ) =
o = TC1 · TC2 · ... · TCn
puts object composition in direct relationship
to the context of execution
object: L ⨉ 2C → O
object = composition · constitution
addAttribute
setAttribute
removeAttribute
href
parent
visible
type
target
href
parent
visible
addAttribute
setAttribute
removeAttribute
parent
141. Run-Time Trait Recomposition 36
o
addAttribute
setAttribute
removeAttribute
href
parent
visible
type
target
overloading of constitution: L ⨉ C → Tconstitution: L ⨉ 2C → 2T
defined through composition policiescomposition: 2T → O
constitution ( ida, { Default, Untrusted, W3C } )
= { HTML.Anchor, T.SecureAnchor, T.ValidAnchor }
composition ( { HTML.Anchor, T.SecureAnchor, T.ValidAnchor } ) =
o = TC1 · TC2 · ... · TCn
puts object composition in direct relationship
to the context of execution
object: L ⨉ 2C → O
object = composition · constitution
addAttribute
setAttribute
removeAttribute
href
parent
visible
type
target
href
parent
visible
addAttribute
setAttribute
removeAttribute
parent
142. Run-Time Trait Recomposition 36
o
addAttribute
setAttribute
removeAttribute
href
parent
visible
type
target
overloading of constitution: L ⨉ C → Tconstitution: L ⨉ 2C → 2T
defined through composition policiescomposition: 2T → O
constitution ( ida, { Default, Untrusted, W3C } )
= { HTML.Anchor, T.SecureAnchor, T.ValidAnchor }
composition ( { HTML.Anchor, T.SecureAnchor, T.ValidAnchor } ) =
o = TC1 · TC2 · ... · TCn
puts object composition in direct relationship
to the context of execution
object: L ⨉ 2C → O
object = composition · constitution
addAttribute
setAttribute
removeAttribute
href
parent
visible
type
target
href
parent
visible
addAttribute
setAttribute
removeAttribute
parent
143. Run-Time Trait Recomposition 36
o
addAttribute
setAttribute
removeAttribute
href
parent
visible
type
target
overloading of constitution: L ⨉ C → Tconstitution: L ⨉ 2C → 2T
defined through composition policiescomposition: 2T → O
constitution ( ida, { Default, Untrusted, W3C } )
= { HTML.Anchor, T.SecureAnchor, T.ValidAnchor }
composition ( { HTML.Anchor, T.SecureAnchor, T.ValidAnchor } ) =
o = TC1 · TC2 · ... · TCn
puts object composition in direct relationship
to the context of execution
object: L ⨉ 2C → O
object = composition · constitution
addAttribute
setAttribute
removeAttribute
href
parent
visible
type
target
href
parent
visible
addAttribute
setAttribute
removeAttribute
parent
144. Run-Time Trait Recomposition 36
o
addAttribute
setAttribute
removeAttribute
href
parent
visible
type
target
overloading of constitution: L ⨉ C → Tconstitution: L ⨉ 2C → 2T
defined through composition policiescomposition: 2T → O
constitution ( ida, { Default, Untrusted, W3C } )
= { HTML.Anchor, T.SecureAnchor, T.ValidAnchor }
composition ( { HTML.Anchor, T.SecureAnchor, T.ValidAnchor } ) =
o = TC1 · TC2 · ... · TCn
puts object composition in direct relationship
to the context of execution
object: L ⨉ 2C → O
object = composition · constitution
addAttribute
setAttribute
removeAttribute
href
parent
visible
type
target
href
parent
visible
addAttribute
setAttribute
removeAttribute
parent
145. Run-Time Trait Recomposition 36
o
addAttribute
setAttribute
removeAttribute
href
parent
visible
type
target
overloading of constitution: L ⨉ C → Tconstitution: L ⨉ 2C → 2T
defined through composition policiescomposition: 2T → O
constitution ( ida, { Default, Untrusted, W3C } )
= { HTML.Anchor, T.SecureAnchor, T.ValidAnchor }
composition ( { HTML.Anchor, T.SecureAnchor, T.ValidAnchor } ) =
o = TC1 · TC2 · ... · TCn
puts object composition in direct relationship
to the context of execution
object: L ⨉ 2C → O
object = composition · constitution
addAttribute
setAttribute
removeAttribute
href
parent
visible
type
target
href
parent
visible
addAttribute
setAttribute
removeAttribute
parent
146. Run-Time Trait Recomposition 36
o
addAttribute
setAttribute
removeAttribute
href
parent
visible
type
target
overloading of constitution: L ⨉ C → Tconstitution: L ⨉ 2C → 2T
defined through composition policiescomposition: 2T → O
constitution ( ida, { Default, Untrusted, W3C } )
= { HTML.Anchor, T.SecureAnchor, T.ValidAnchor }
composition ( { HTML.Anchor, T.SecureAnchor, T.ValidAnchor } ) =
o = TC1 · TC2 · ... · TCn
puts object composition in direct relationship
to the context of execution
object: L ⨉ 2C → O
object = composition · constitution
addAttribute
setAttribute
removeAttribute
href
parent
visible
type
target
href
parent
visible
addAttribute
setAttribute
removeAttribute
parent
147. Run-Time Trait Recomposition 36
o
addAttribute
setAttribute
removeAttribute
href
parent
visible
type
target
overloading of constitution: L ⨉ C → Tconstitution: L ⨉ 2C → 2T
defined through composition policiescomposition: 2T → O
constitution ( ida, { Default, Untrusted, W3C } )
= { HTML.Anchor, T.SecureAnchor, T.ValidAnchor }
composition ( { HTML.Anchor, T.SecureAnchor, T.ValidAnchor } ) =
o = TC1 · TC2 · ... · TCn
puts object composition in direct relationship
to the context of execution
object: L ⨉ 2C → O
object = composition · constitution
addAttribute
setAttribute
removeAttribute
href
parent
visible
type
target
href
parent
visible
addAttribute
setAttribute
removeAttribute
parent
148. Run-Time Trait Recomposition 36
o
addAttribute
setAttribute
removeAttribute
href
parent
visible
type
target
overloading of constitution: L ⨉ C → Tconstitution: L ⨉ 2C → 2T
defined through composition policiescomposition: 2T → O
constitution ( ida, { Default, Untrusted, W3C } )
= { HTML.Anchor, T.SecureAnchor, T.ValidAnchor }
composition ( { HTML.Anchor, T.SecureAnchor, T.ValidAnchor } ) =
o = TC1 · TC2 · ... · TCn
puts object composition in direct relationship
to the context of execution
object: L ⨉ 2C → O
object = composition · constitution
addAttribute
setAttribute
removeAttribute
href
parent
visible
type
target
href
parent
visible
addAttribute
setAttribute
removeAttribute
parent
149. Run-Time Trait Recomposition 36
o
addAttribute
setAttribute
removeAttribute
href
parent
visible
type
target
overloading of constitution: L ⨉ C → Tconstitution: L ⨉ 2C → 2T
defined through composition policiescomposition: 2T → O
constitution ( ida, { Default, Untrusted, W3C } )
= { HTML.Anchor, T.SecureAnchor, T.ValidAnchor }
composition ( { HTML.Anchor, T.SecureAnchor, T.ValidAnchor } ) =
o = TC1 · TC2 · ... · TCn
puts object composition in direct relationship
to the context of execution
object: L ⨉ 2C → O
object = composition · constitution
addAttribute
setAttribute
removeAttribute
href
parent
visible
type
target
href
parent
visible
addAttribute
setAttribute
removeAttribute
parent
150. Run-Time Trait Recomposition 36
o
addAttribute
setAttribute
removeAttribute
href
parent
visible
type
target
overloading of constitution: L ⨉ C → Tconstitution: L ⨉ 2C → 2T
defined through composition policiescomposition: 2T → O
constitution ( ida, { Default, Untrusted, W3C } )
= { HTML.Anchor, T.SecureAnchor, T.ValidAnchor }
composition ( { HTML.Anchor, T.SecureAnchor, T.ValidAnchor } ) =
o = TC1 · TC2 · ... · TCn
puts object composition in direct relationship
to the context of execution
object: L ⨉ 2C → O
object = composition · constitution
addAttribute
setAttribute
removeAttribute
href
parent
visible
type
target
href
parent
visible
addAttribute
setAttribute
removeAttribute
parent
151. Run-Time Trait Recomposition 36
o
addAttribute
setAttribute
removeAttribute
href
parent
visible
type
target
overloading of constitution: L ⨉ C → Tconstitution: L ⨉ 2C → 2T
defined through composition policiescomposition: 2T → O
constitution ( ida, { Default, Untrusted, W3C } )
= { HTML.Anchor, T.SecureAnchor, T.ValidAnchor }
composition ( { HTML.Anchor, T.SecureAnchor, T.ValidAnchor } ) =
o = TC1 · TC2 · ... · TCn
puts object composition in direct relationship
to the context of execution
object: L ⨉ 2C → O
object = composition · constitution
addAttribute
setAttribute
removeAttribute
href
parent
visible
type
target
href
parent
visible
addAttribute
setAttribute
removeAttribute
parent
204. Summary 41
✓ Contexts: frame of reference to define adaptations
✓ Traits: convenient units of adaptation
✓ Policies: flexible compositions (e.g. non-linear)
✓ Extensibility: independent behaviour extensions
✓ JavaScript: easy definition of contexts, traits, and composition
direct causal connection
composition of the computational system
situation in which the system executes
⇋
Context Traits
206. Method Pre-Dispatch 43
When a context is (de)activated,
for every class and selector the context adapts, find
all active methods
M(c, s)={ m1, m2, m3, ..., mn }
reorder them according to specificity
m1 < m2 < m3 < ... < mn
and deploy the first one
m1
‣m1 is the most specific implementation for the current context
‣ resend invokes the remaining methods in order
‣mn is (usually) the default implementation
207. Method Pre-Dispatch 43
When a context is (de)activated,
for every class and selector the context adapts, find
all active methods
M(c, s)={ m1, m2, m3, ..., mn }
reorder them according to specificity
m1 < m2 < m3 < ... < mn
and deploy the first one
m1
‣m1 is the most specific implementation for the current context
‣ resend invokes the remaining methods in order
‣mn is (usually) the default implementation
deploy
208. Method Pre-Dispatch 43
When a context is (de)activated,
for every class and selector the context adapts, find
all active methods
M(c, s)={ m1, m2, m3, ..., mn }
reorder them according to specificity
m1 < m2 < m3 < ... < mn
and deploy the first one
m1
‣m1 is the most specific implementation for the current context
‣ resend invokes the remaining methods in order
‣mn is (usually) the default implementation
structural reflection
deploy
209. Method Dispatch 44
When a message is received,
find all applicable methods for the given message
(with receiver r, selector s and arguments a)
M(r,s,a)={ m1, m2, m3, ..., mn }
reorder them according to specificity
m1 < m2 < m3 < ... < mn
and invoke the first one
m1
‣m1 is the most specific implementation for the current context
‣ resend invokes the remaining methods in order
‣mn is (usually) the default implementation
210. Method Dispatch 44
When a message is received,
find all applicable methods for the given message
(with receiver r, selector s and arguments a)
M(r,s,a)={ m1, m2, m3, ..., mn }
reorder them according to specificity
m1 < m2 < m3 < ... < mn
and invoke the first one
m1
‣m1 is the most specific implementation for the current context
‣ resend invokes the remaining methods in order
‣mn is (usually) the default implementation
message is received
211. Method Dispatch 44
When a message is received,
find all applicable methods for the given message
(with receiver r, selector s and arguments a)
M(r,s,a)={ m1, m2, m3, ..., mn }
reorder them according to specificity
m1 < m2 < m3 < ... < mn
and invoke the first one
m1
‣m1 is the most specific implementation for the current context
‣ resend invokes the remaining methods in order
‣mn is (usually) the default implementation
invoke
message is received
212. Method Dispatch 44
When a message is received,
find all applicable methods for the given message
(with receiver r, selector s and arguments a)
M(r,s,a)={ m1, m2, m3, ..., mn }
reorder them according to specificity
m1 < m2 < m3 < ... < mn
and invoke the first one
m1
‣m1 is the most specific implementation for the current context
‣ resend invokes the remaining methods in order
‣mn is (usually) the default implementation
[ behavioural reflection ]
invoke
message is received
213. Method Pre-Dispatch 45
Is the method order always defined?
• Could there be no applicable methods?
➡ default implementation
• Could there be non-comparable methods?
➡ the order should be total
➡ if not, we’re in trouble
M(r,s,a)={ m1, m2, m3, ..., mn }
M(c, s)={ m1, m2, m3, ..., mn }