1. Identifying Traits with
Formal Concept Analysis
Adrian Lienhard, Stéphane Ducasse and Gabriela Arévalo
Software Composition Group
University of Berne, Switzerland
2. Overview
“too high”...
...cancelled
duplicated
methods
ASE‘05 - Identifying Traits with FCA Adrian Lienhard - lienhard@iam.unibe.ch 2 / 16
3. Overview
“too high”...
...cancelled
duplicated
methods
ASE‘05 - Identifying Traits with FCA Adrian Lienhard - lienhard@iam.unibe.ch 2 / 16
6. Background: Traits in a Nutshell
Generic properties
GraphicalObject
Colored Bordered
Circle Polygon
ASE‘05 - Identifying Traits with FCA Adrian Lienhard - lienhard@iam.unibe.ch 3 / 16
7. Background: Traits in a Nutshell
Generic properties
GraphicalObject
Colored Bordered
Circle Polygon
ColoredCircle BorderedColoredPol.
Colored Bordered
Colored
ASE‘05 - Identifying Traits with FCA Adrian Lienhard - lienhard@iam.unibe.ch 3 / 16
8. Background: Traits in a Nutshell
Generic properties
GraphicalObject
Colored Bordered
Circle Polygon
ColoredCircle BorderedColoredPol. Single inheritance: limited
expressiveness
Colored Bordered
Multiple inheritance:
Colored
complexity
Mixin inheritance: fragility
Goal of traits: reuse mechanism due to linearization
avoiding complexity
ASE‘05 - Identifying Traits with FCA Adrian Lienhard - lienhard@iam.unibe.ch 3 / 16
9. Background: Traits in a Nutshell
Reuse mechanism Trait = “interface with implementation”
complementary to TColored
inheritance [ECOOP’03]
{ }
red rgb
provided green rgb: required
Today in Perl 6, Squeak, =
Scala, Fortress, ... methods methods
hash
...
... ...
ColoredCircle BorderedColoredPol.
trait composition
TColored TBordered
ASE‘05 - Identifying Traits with FCA Adrian Lienhard - lienhard@iam.unibe.ch 4 / 16
10. Roadmap
‣ FCA in a nutshell
‣ Approach overview
‣ Illustration of our approach on case study
‣ Evaluation of case study
‣ Conclusion
ASE‘05 - Identifying Traits with FCA Adrian Lienhard - lienhard@iam.unibe.ch 5 / 16
11. FCA in a Nutshell
properties
small far moon
Mercur x
elements
y
Mars x x
Jupiter x x
Pluto x x x
Concept: maximal group of elements
based on their common properties
ASE‘05 - Identifying Traits with FCA Adrian Lienhard - lienhard@iam.unibe.ch 6 / 16
12. FCA in a Nutshell
properties
small far moon
Mercur x
elements
y
Mars x x
Jupiter x x
Pluto x x x
Concept: maximal group of elements
based on their common properties
{Mars, Pluto, Jupiter},{moon}
ASE‘05 - Identifying Traits with FCA Adrian Lienhard - lienhard@iam.unibe.ch 6 / 16
13. FCA in a Nutshell
properties
small far moon
Mercur x
elements
y
Mars x x
Jupiter x x
Pluto x x x
Concept: maximal group of elements
based on their common properties
{Mars, Pluto, Jupiter},{moon}
{Pluto, Jupiter},{moon, far}
ASE‘05 - Identifying Traits with FCA Adrian Lienhard - lienhard@iam.unibe.ch 6 / 16
14. FCA in a Nutshell
properties Me, Ma, Ju, Pl
small far moon
Mercur x
elements
y
Mars x x Me, Ma, Pl Ma, Ju, Pl
Jupiter x x small moon
Pluto x x x
Ma, Pl Ju, Pl
small, moon moon, far
Concept: maximal group of elements
Pl
based on their common properties
small, moon, far
Concept Lattice
{Mars, Pluto, Jupiter},{moon}
{Pluto, Jupiter},{moon, far}
ASE‘05 - Identifying Traits with FCA Adrian Lienhard - lienhard@iam.unibe.ch 6 / 16
15. FCA in a Nutshell
properties Me, Ma, Ju, Pl
small far moon
Mercur x
elements
y
Mars x x Me, Ma, Pl Ma, Ju, Pl
Jupiter x x small moon
Pluto x x x
Ma, Pl Ju, Pl
small, moon moon, far
Concept: maximal group of elements
Pl
based on their common properties
small, moon, far
Concept Lattice
{Mars, Pluto, Jupiter},{moon} less elements
{Pluto, Jupiter},{moon, far} more properties
ASE‘05 - Identifying Traits with FCA Adrian Lienhard - lienhard@iam.unibe.ch 6 / 16
16. Approach Overview
Stage 1
input generation FCA application filtering manual selection
{
Stage 2
for each class
and trait
input generation FCA application manual selection
ASE‘05 - Identifying Traits with FCA Adrian Lienhard - lienhard@iam.unibe.ch 7 / 16
17. Case Study
‣ ST-80 Stream and Collection libraries
‣ Comparison with purely manual refactoring
[OOPSLA’03]
‣ Illustration of our approach on example of
Stream hierarchy...
ASE‘05 - Identifying Traits with FCA Adrian Lienhard - lienhard@iam.unibe.ch 8 / 16
18. Generating Input for FCA
Detecting cancellations and duplications Stage 1
Identify real interface of a class taking into account...
...
Stream
Idiom “too high”
next
boolean ‣ implement methods “too
sends message next
int32
...
high” in common superclass
‣ (implicitly) cancel methods
WriteStream ReadStream
in inappropriate subclasses
... ...
next next
... ....
ReadWriteStream
...
next Fact ST-80 Collection library:
... 131 methods too high (~10%), and
of those, 106 implicitly cancelled
ASE‘05 - Identifying Traits with FCA Adrian Lienhard - lienhard@iam.unibe.ch 9 / 16
19. Generating Input for FCA
Detecting cancellations and duplications Stage 1
Identify real interface of a class taking into account...
invokes ...
Stream
Idiom “too high”
cancelled next
‣ implement methods “too
boolean
method int32 sends message next
high” in common superclass
...
‣ (implicitly) cancel methods
WriteStream ReadStream
in inappropriate subclasses
... ...
next next
... ....
ReadWriteStream
...
next Fact ST-80 Collection library:
... 131 methods too high (~10%), and
of those, 106 implicitly cancelled
ASE‘05 - Identifying Traits with FCA Adrian Lienhard - lienhard@iam.unibe.ch 9 / 16
20. Applying FCA Stage 1
Elements: all concrete classes
Properties: all concrete methods (identifying duplications)
Mapping: method in the real interface of the class
WS, RWS, RS
atEnd
WS, RWS RWS, RS
atEnd, nextPut:, atEnd, next,
next:put: boolean
WS RWS RS
atEnd, nextPut:, atEnd, nextPut:, atEnd, next,
next:put:, next:put:, next, boolean,
contents1 boolean, contents2 contents3
atEnd, nextPut:,
next:put: next,
boolean, contents1,2,3
ASE‘05 - Identifying Traits with FCA Adrian Lienhard - lienhard@iam.unibe.ch 10 / 16
21. Filtering Stage 1
‣ reduce properties-sets to delta between
sub-/superconcept
‣ remove concepts with no classes
WS, RWS, RS
atEnd
WS, RWS RWS, RS
nextPut:,
next, boolean
next:put:
WS RWS RS
contents1 contents2 contents3
ASE‘05 - Identifying Traits with FCA Adrian Lienhard - lienhard@iam.unibe.ch 11 / 16
22. Selecting Traits and Hierarchy
Reconstruction Stage 1
Partly manual task: deciding whether a
concept becomes a class or a trait
Stream
WriteStream ReadWriteStream ReadStream
TWriteableStream TReadableStream
ASE‘05 - Identifying Traits with FCA Adrian Lienhard - lienhard@iam.unibe.ch 12 / 16
23. Invocation Analysis Stage 2
Generating input: analyzing transitive method
invocations of the class/trait
Applying FCA
‣ elements: all methods of the class/trait
‣ properties: (transitive) method invocations
‣ mapping: identifying invocations with methods
ASE‘05 - Identifying Traits with FCA Adrian Lienhard - lienhard@iam.unibe.ch 13 / 16
24. Selecting Traits Stage 2
Manual task of selecting traits.
Lattice provides interesting alternatives:
‣ Additional methods in super-concepts
‣ Fewer methods in sub-concepts
Stream
ReadStream ReadWriteStream WriteStream
TReadableStream TWriteableStream
TReadableMultiple TWriteablePutData
TReadableFileIn TWriteablePutCharacter
TWriteableMultiple
ASE‘05 - Identifying Traits with FCA Adrian Lienhard - lienhard@iam.unibe.ch 14 / 16
25. Comparison with
Manual Refactoring
Stream library
‣ almost identical result: inheritance reshaping + two
main traits
‣ difference: conceptual vs. functional sub-traits
Collection library
‣ Manual refactoring has identified more traits
‣ Different layers of traits with overriding behavior
‣ Our approach curative, manual approach speculative
ASE‘05 - Identifying Traits with FCA Adrian Lienhard - lienhard@iam.unibe.ch 15 / 16
26. Conclusion
‣ Succeeded in curing problems by
restructuring hierarchy with traits (stage 1)
‣ Successful identification of fine-grained traits
also when code is not shared (stage 2).
‣ Limitations:
• Detection based on conceptual meaning of
methods alone not possible
• Missing methods can cause too small traits
ASE‘05 - Identifying Traits with FCA Adrian Lienhard - lienhard@iam.unibe.ch 16 / 16
27. Conclusion
‣ Succeeded in curing problems by
restructuring hierarchy with traits (stage 1)
‣ Successful identification of fine-grained traits
also when code is not shared (stage 2).
‣ Limitations:
• Detection based on conceptual meaning of
methods alone not possible
• Missing methods can cause too small traits
Questions?
ASE‘05 - Identifying Traits with FCA Adrian Lienhard - lienhard@iam.unibe.ch 16 / 16
28. Refactored Collection
Hierarchy
Legend:
Collection inherits from
composed from
Set SequenceableCollection
SortedCollection OrderedCollection Array
TRemoving- TOrderedSorted- TSequenced-
Elements Common ElementAccess
ASE‘05 - Identifying Traits with FCA Adrian Lienhard - lienhard@iam.unibe.ch 17 / 16