scp tud
DESCRIPTION
sptudslidesTRANSCRIPT
![Page 1: scp tud](https://reader031.vdocuments.us/reader031/viewer/2022020417/563db7c9550346aa9a8df39e/html5/thumbnails/1.jpg)
Software Composition ParadigmsSommersemester 2015
Radu Muschevici
Software Engineering Group, Department of Computer Science
2015-06-02
1 / 32
![Page 2: scp tud](https://reader031.vdocuments.us/reader031/viewer/2022020417/563db7c9550346aa9a8df39e/html5/thumbnails/2.jpg)
Context-OrientedProgramming
2 / 32
![Page 3: scp tud](https://reader031.vdocuments.us/reader031/viewer/2022020417/563db7c9550346aa9a8df39e/html5/thumbnails/3.jpg)
Context AwarenessContext is the physical, technical and human environment in which asoftware application is used.
Software…▶ needs to be increasingly aware of the context in which it runs,▶ should adapt dynamically to such context,▶ to provide a service that matches client needs to the best extent
possible.
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
3 / 32
![Page 4: scp tud](https://reader031.vdocuments.us/reader031/viewer/2022020417/563db7c9550346aa9a8df39e/html5/thumbnails/4.jpg)
Problem
There is little explicit support for context awareness in mainstreamprogramming languages.
We still program this… using the programmingmodels conceived
for this…we still program this...
using the programming models
conceived for this....
(2010) (1980)
Mindset Mismatch !8
(2015) (1980)
4 / 32
![Page 5: scp tud](https://reader031.vdocuments.us/reader031/viewer/2022020417/563db7c9550346aa9a8df39e/html5/thumbnails/5.jpg)
Programming with Contextprogramming 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
A new paradigm is needed that puts programmers in the right state ofmind to build dynamically adaptable applications from the ground up.
5 / 32
![Page 6: scp tud](https://reader031.vdocuments.us/reader031/viewer/2022020417/563db7c9550346aa9a8df39e/html5/thumbnails/6.jpg)
Context: ExamplesApplications 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 / 32
![Page 7: scp tud](https://reader031.vdocuments.us/reader031/viewer/2022020417/563db7c9550346aa9a8df39e/html5/thumbnails/7.jpg)
Context Adaptation Examples
take advantage of room projector for presentationpeer service
decrease playback quality when battery power is lowinternal state
user taskshow parking spots and gas stations (only) when driving
environmental conditionsgive more detailed indications when visibility is low
disable phone ringtone in quiet placeslocation semantics
Adaptation Examples !6
7 / 32
![Page 8: scp tud](https://reader031.vdocuments.us/reader031/viewer/2022020417/563db7c9550346aa9a8df39e/html5/thumbnails/8.jpg)
Contexts as Situation Reifiers
▶ Any information that is computationally accessible can be context.▶ To act based on context, the meaning of that information needs to
be well-defined.
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
Contexts are reified situationsfor which adapted application behaviour can be defined.
8 / 32
![Page 9: scp tud](https://reader031.vdocuments.us/reader031/viewer/2022020417/563db7c9550346aa9a8df39e/html5/thumbnails/9.jpg)
Manual Context OrientationAny language can be use to build software that can adapt its behaviourbased on dynamically changing context.Context-independent behaviourclass Person {
String name, address;Employer employer;void display() {
println(name); println(address); println(employer);}
}
Context-dependent behaviourclass Person {
String name, address;Employer employer;void display(boolean showAddress, boolean showEmployer) {
println(name);if (showAddress) println(address);if (showEmployer) println(employer);
}}
9 / 32
![Page 10: scp tud](https://reader031.vdocuments.us/reader031/viewer/2022020417/563db7c9550346aa9a8df39e/html5/thumbnails/10.jpg)
Manual Context Orientation
But manual context adaptation leads to code that is…▶ Tangled▶ Scattered▶ Complex, hard to comprehend▶ Difficult to change/refactor/extend/evolve▶ Difficult to reuse▶ Error-prone
10 / 32
![Page 11: scp tud](https://reader031.vdocuments.us/reader031/viewer/2022020417/563db7c9550346aa9a8df39e/html5/thumbnails/11.jpg)
Context-Oriented Programming (COP)
Problem▶ Context is increasingly important for many application domains:
pervasive and mobile computing, business applications, security,personalisation, internationalisation, …
Solution: Context-Oriented Programming▶ Make context an explicit element of the programming language.▶ Provide mechanism to dynamically adapt behaviour in reaction to
changing context.
11 / 32
![Page 12: scp tud](https://reader031.vdocuments.us/reader031/viewer/2022020417/563db7c9550346aa9a8df39e/html5/thumbnails/12.jpg)
COP Languages
▶ Context-oriented programming languages existin several shapes and sizes1.
▶ ContextJ, ContextL, ContextR, ContextJS, ContextPy,PyContext, ContextG, …
▶ Example code will be in ContextJ (Java variant)2
1[Appeltauer, Hirschfeld, Haupt, Lincke, et al. 2009]2[Appeltauer, Hirschfeld, Haupt, and Masuhara 2011]
12 / 32
![Page 13: scp tud](https://reader031.vdocuments.us/reader031/viewer/2022020417/563db7c9550346aa9a8df39e/html5/thumbnails/13.jpg)
ContextJ Example
class Person {private String name, address;private Employer employer;Person(String name, String address, Employer employer) {
this.name = name;this.address = address;this.employer = employer;
}String toString() { return(name); }
layer Address {String toString() {
return(proceed() + ", Address: " + address);}
}layer Employment {
String toString() {return(proceed() + "; Employer: " + employer);
}}
}
13 / 32
![Page 14: scp tud](https://reader031.vdocuments.us/reader031/viewer/2022020417/563db7c9550346aa9a8df39e/html5/thumbnails/14.jpg)
ContextJ Example (cont.)
class Employer {private String name, address;Employer(String name, String address) {
this.name = name;this.address = address;
}String toString() {
return(name);}
layer Address {String toString() {
return(proceed() + ", Address: " + address);}
}}
14 / 32
![Page 15: scp tud](https://reader031.vdocuments.us/reader031/viewer/2022020417/563db7c9550346aa9a8df39e/html5/thumbnails/15.jpg)
COP: Programming with Layers in ContextJ
▶ Layers add behavioural variations3 to classes▶ Layers are first class language entities▶ Layers are defined within scope of a class,
thus can access private fields▶ Issue: layer functionality scattered across classes
3[Hirschfeld, Costanza, and Nierstrasz 2008]15 / 32
![Page 16: scp tud](https://reader031.vdocuments.us/reader031/viewer/2022020417/563db7c9550346aa9a8df39e/html5/thumbnails/16.jpg)
Behavioural Variations
▶ Add, modify or remove behaviour of objects.▶ Are expressed as partial definitions of methods.▶ Example:
String toString() {return(proceed() + "; Employer: " + employer);
}
▶ Method definition is partial because (in general) it is not complete:call to proceed requires the existence of a base method definition.
▶ Base method defined outside of layer.
16 / 32
![Page 17: scp tud](https://reader031.vdocuments.us/reader031/viewer/2022020417/563db7c9550346aa9a8df39e/html5/thumbnails/17.jpg)
Layers
▶ Layers represent contexts▶ Modularisation mechanism, orthogonal to classes.▶ Group related context-dependent behavioural variations.
17 / 32
![Page 18: scp tud](https://reader031.vdocuments.us/reader031/viewer/2022020417/563db7c9550346aa9a8df39e/html5/thumbnails/18.jpg)
Dynamic Layer Activation & Deactivation
▶ with and without statements
▶ with activates a layer (if required) and moves it to the position ofmost recently activated.
▶ without deactivates a layer.
▶ Activation is dynamically scoped:with(L) { stmt; without(L) { stmt; } }
Layers are only active for the dynamic extent of the with block.
▶ Layer (de)activations can be nested.
18 / 32
![Page 19: scp tud](https://reader031.vdocuments.us/reader031/viewer/2022020417/563db7c9550346aa9a8df39e/html5/thumbnails/19.jpg)
Dynamic Layer Activation
Person ben = new Person("Ben", "Darmstadt",new Employer("Lufthansa", "Frankfurt"));
with (Address) { println(ben); }// Ben, Address: Darmstadt
with (Employment) { println(ben); }// Ben; Employer: Lufthansa
with (Employment) { with (Address) { println(ben); } }// Ben; Employer: Lufthansa, Frankfurt, Darmstadt
with (Address) { with (Employment) { println(ben); } }// Ben, Address: Darmstadt; Employer: Lufthansa, Frankfurt
19 / 32
![Page 20: scp tud](https://reader031.vdocuments.us/reader031/viewer/2022020417/563db7c9550346aa9a8df39e/html5/thumbnails/20.jpg)
Calling Sequence
toString
Layer Employment
Layer Address
Base
with((Address)({(with((Employment)({({(println(ben);(}(}
toString
toString
proceedproceed
toString
Person Employer
toString
proceed
proceeddispatch
20 / 32
![Page 21: scp tud](https://reader031.vdocuments.us/reader031/viewer/2022020417/563db7c9550346aa9a8df39e/html5/thumbnails/21.jpg)
Calling Sequence (cont.)
▶ The given layer order determines the order in which partialmethods definitions are traversed: The most recently activatedlayer is first in the proceed chain.
▶ A partial method definition proceeds to a corresponding partialmethod in the next-in-order active layer, or to the base method.
with(Address, Employer) {println(ben);
}
proceed chain: Employer → Address → base method definition
with(Employer) { with(Address) {println(ben);
} }
proceed chain: Address → Employer → base method definition
21 / 32
![Page 22: scp tud](https://reader031.vdocuments.us/reader031/viewer/2022020417/563db7c9550346aa9a8df39e/html5/thumbnails/22.jpg)
Dynamic Scoping
▶ Layers change the semantics of message dispatch for a givendynamic scope.
▶ The scope in which layers are active or inactive can be controlledexplicitly.
▶ The same behavioural variation may be simultaneously active andinactive within different scopes of the same running application.
22 / 32
![Page 23: scp tud](https://reader031.vdocuments.us/reader031/viewer/2022020417/563db7c9550346aa9a8df39e/html5/thumbnails/23.jpg)
Dynamic Scoping (cont.)
Behavioural variation of class Person in layer Address:String toString() {
return(proceed() + ", Address: " + address);}
Create a Person object:Person ben = new Person("Ben", "Darmstadt",
new Employer("Lufthansa", "Frankfurt"));
Behavioural variation is active:with (Address) { println(ben); }// Ben, Address: Darmstadt
Inactive:without (Address) { println(ben); }// Ben
23 / 32
![Page 24: scp tud](https://reader031.vdocuments.us/reader031/viewer/2022020417/563db7c9550346aa9a8df39e/html5/thumbnails/24.jpg)
Multi-dimensional Method Dispatch
One-dimensional dispatch In procedural programming, procedure callsare dispatched only based on the procedure’s name.
Two-dimensional dispatch Object-oriented programming dispatchesmessages based on message name and the receiverobject.
Three-dimensional dispatch Subjective programming4 dispatchesmessages based on message name, the receiver and themessage sender.
Four-dimensional dispatch Context-oriented Programming adds afourth message dispatch dimension: the context of themessage send.
4[Smith and Ungar 1996]24 / 32
![Page 25: scp tud](https://reader031.vdocuments.us/reader031/viewer/2022020417/563db7c9550346aa9a8df39e/html5/thumbnails/25.jpg)
Code Modularisation: Layer Declaration Strategies
Layer inside Classclass C1 {
layer L1 {...}layer L2 {...}
}class C2 {
layer L1 {...}layer L2 {...}
}
▶ Classes are completelyspecified
▶ Layers scattered acrossclasses
▶ Can access privates
Class inside Layerlayer L1 {
class C1 {...}class C2 {...}
}layer L2 {
class C1 {...}class C2 {...}
}
▶ Layers are completelyspecified
▶ Class is scattered acrosslayers
▶ Should privates be accessible?
25 / 32
![Page 26: scp tud](https://reader031.vdocuments.us/reader031/viewer/2022020417/563db7c9550346aa9a8df39e/html5/thumbnails/26.jpg)
Code Modularisation (cont.)
Layer-in-Class vs. Class-in-Layer. What is better for…▶ Code analysis, readability?▶ Code evolution & extension?
▶ Some COP languages (e.g. ContextL) allow both⇒ programmer decides
▶ Alternative: Independent layer-class fragments
26 / 32
![Page 27: scp tud](https://reader031.vdocuments.us/reader031/viewer/2022020417/563db7c9550346aa9a8df39e/html5/thumbnails/27.jpg)
Stateful Layers
▶ Problem: Fields used in layer-specific behaviour need to bedeclared and can be accessed outside the layer.class Person {
private String name, address;private Employer employer;Person(String name, String address, Employer employer) {
this.name = name;this.address = address;this.employer = employer;
}
layer Address {public String getAddress() { return address; }
}layer Employer {
public Employer getEmployer() { return employer; }}
}
▶ Some languages (e.g. ContextL) allow introduction of new statevia layers.
27 / 32
![Page 28: scp tud](https://reader031.vdocuments.us/reader031/viewer/2022020417/563db7c9550346aa9a8df39e/html5/thumbnails/28.jpg)
A Comparison to Aspect-Oriented Programming
Cross-cutting concerns Both aspects and context layers modularisecross-cutting concerns.
Code scattering Aspects prevent code scattering by collecting code ina single location; Context layers are spread acrossclasses (with a layer-in-class model); With a class-in-layermodel, class fragments are scattered across layers.
Dynamicity Aspects are composed (weaved) statically; Layers areactivated and deactivated dynamically – in response todynamic changes in context.
28 / 32
![Page 29: scp tud](https://reader031.vdocuments.us/reader031/viewer/2022020417/563db7c9550346aa9a8df39e/html5/thumbnails/29.jpg)
Context-Oriented Programming: Summary
Benefits▶ Context is modularised using layers.▶ Dynamic activation & deactivation of layers in response to dynamic
context change.▶ Order of layer activation is explicit (user-defined).▶ Objects of the same class behave differently, according to context.
Issues▶ How to modularise context: layer-in-class, class-in-layer, both?▶ Code scattering
29 / 32
![Page 30: scp tud](https://reader031.vdocuments.us/reader031/viewer/2022020417/563db7c9550346aa9a8df39e/html5/thumbnails/30.jpg)
This Week’s Reading Assignment
▶ Hirschfeld, R., Costanza, P., and Nierstrasz, O. Context-orientedProgramming. Journal of Object Technology 7, 3 (Mar. 2008),125–151.
▶ Download link:http://www.jot.fm/issues/issue_2008_03/article4/
30 / 32
![Page 31: scp tud](https://reader031.vdocuments.us/reader031/viewer/2022020417/563db7c9550346aa9a8df39e/html5/thumbnails/31.jpg)
References I
Appeltauer, Malte, Robert Hirschfeld, Michael Haupt, Jens Lincke, andMichael Perscheid (2009). “A Comparison of Context-orientedProgramming Languages”. In: International Workshop onContext-Oriented Programming. COP ’09. ACM Press, 6:1–6:6.
Appeltauer, Malte, Robert Hirschfeld, Michael Haupt, andHidehiko Masuhara (2011). “ContextJ: Context-orientedProgramming with Java”. In: Information and Media Technologies6.2, pp. 399–419.
Hirschfeld, Robert, Pascal Costanza, and Oscar Nierstrasz (2008).“Context-oriented Programming”. In: Journal of Object Technology7.3, pp. 125–151.
Smith, Randall B. and David Ungar (1996). “A Simple and UnifyingApproach to Subjective Objects”. In: Theory and Practice of ObjectSystems: Special issue on subjectivity in object-orientedsystems 2.3, pp. 161–178.
31 / 32
![Page 32: scp tud](https://reader031.vdocuments.us/reader031/viewer/2022020417/563db7c9550346aa9a8df39e/html5/thumbnails/32.jpg)
References IISlides 3–8 are based on the course LSINF2335 – “Programmingparadigms and applications” by Kim Mens, Université Catholique deLouvain.
32 / 32