Identifying Traits with Formal Concept Analysis

28
Identifying Traits with Formal Concept Analysis Adrian Lienhard, Stéphane Ducasse and Gabriela Arévalo Software Composition Group University of Berne, Switzerland

Transcript of Identifying Traits with Formal Concept Analysis

Page 1: Identifying Traits with Formal Concept Analysis

Identifying Traits with Formal Concept Analysis

Adrian Lienhard, Stéphane Ducasse and Gabriela Arévalo

Software Composition GroupUniversity of Berne, Switzerland

Page 2: Identifying Traits with Formal Concept Analysis

ASE‘05 - Identifying Traits with FCA / 16Adrian Lienhard - [email protected] 2

Overview

duplicatedmethods

...cancelled“too high”...

Page 3: Identifying Traits with Formal Concept Analysis

ASE‘05 - Identifying Traits with FCA / 16Adrian Lienhard - [email protected] 2

Overview

duplicatedmethods

...cancelled“too high”...

Page 4: Identifying Traits with Formal Concept Analysis

ASE‘05 - Identifying Traits with FCA / 16Adrian Lienhard - [email protected] 2

Overview

duplicatedmethods

...cancelled“too high”...

traits

Page 5: Identifying Traits with Formal Concept Analysis

ASE‘05 - Identifying Traits with FCA / 16Adrian Lienhard - [email protected] 2

Overview

duplicatedmethods

...cancelled“too high”...

traits

FCA

Page 6: Identifying Traits with Formal Concept Analysis

ASE‘05 - Identifying Traits with FCA / 16Adrian Lienhard - [email protected] 3

GraphicalObject

Circle Polygon

Colored Bordered

Generic properties

Background: Traits in a Nutshell

Page 7: Identifying Traits with Formal Concept Analysis

ASE‘05 - Identifying Traits with FCA / 16Adrian Lienhard - [email protected] 3

GraphicalObject

ColoredCircle

Colored

BorderedColoredPol.

Bordered

Colored

Circle Polygon

Colored Bordered

Generic properties

Background: Traits in a Nutshell

Page 8: Identifying Traits with Formal Concept Analysis

ASE‘05 - Identifying Traits with FCA / 16Adrian Lienhard - [email protected] 3

GraphicalObject

ColoredCircle

Colored

BorderedColoredPol.

Bordered

Colored

Circle Polygon

Colored Bordered

Generic properties

Background: Traits in a Nutshell

Single inheritance: limited expressiveness

Multiple inheritance: complexity

Mixin inheritance: fragility due to linearizationGoal of traits: reuse mechanism

avoiding complexity

Page 9: Identifying Traits with Formal Concept Analysis

ASE‘05 - Identifying Traits with FCA / 16Adrian Lienhard - [email protected]

Reuse mechanism complementary to inheritance [ECOOP’03]

Today in Perl 6, Squeak, Scala, Fortress, ...

4

Background: Traits in a Nutshell

TColored

redgreen=hash...

rgbrgb:provided

methods{ requiredmethods}

ColoredCircle BorderedColoredPol.

TColored TBordered

Trait = “interface with implementation”

trait composition

......

Page 10: Identifying Traits with Formal Concept Analysis

ASE‘05 - Identifying Traits with FCA / 16Adrian Lienhard - [email protected]

‣ FCA in a nutshell

‣ Approach overview

‣ Illustration of our approach on case study

‣ Evaluation of case study

‣ Conclusion

5

Roadmap

Page 11: Identifying Traits with Formal Concept Analysis

ASE‘05 - Identifying Traits with FCA / 16Adrian Lienhard - [email protected] 6

small far moonMercur

yx

Mars x xJupiter x xPluto x x x

elem

ents

properties

Concept: maximal group of elements based on their common properties

FCA in a Nutshell

Page 12: Identifying Traits with Formal Concept Analysis

ASE‘05 - Identifying Traits with FCA / 16Adrian Lienhard - [email protected] 6

small far moonMercur

yx

Mars x xJupiter x xPluto x x x

elem

ents

properties

{Mars, Pluto, Jupiter},{moon}

Concept: maximal group of elements based on their common properties

FCA in a Nutshell

Page 13: Identifying Traits with Formal Concept Analysis

ASE‘05 - Identifying Traits with FCA / 16Adrian Lienhard - [email protected] 6

small far moonMercur

yx

Mars x xJupiter x xPluto x x x

elem

ents

properties

{Mars, Pluto, Jupiter},{moon}

Concept: maximal group of elements based on their common properties

{Pluto, Jupiter},{moon, far}

FCA in a Nutshell

Page 14: Identifying Traits with Formal Concept Analysis

ASE‘05 - Identifying Traits with FCA / 16Adrian Lienhard - [email protected] 6

small far moonMercur

yx

Mars x xJupiter x xPluto x x x

elem

ents

properties

{Mars, Pluto, Jupiter},{moon}

Concept: maximal group of elements based on their common properties

{Pluto, Jupiter},{moon, far}

FCA in a Nutshell

Concept Lattice

Ma, Ju, Plmoon

Me, Ma, Ju, Pl

Me, Ma, Plsmall

Ju, Plmoon, far

Ma, Plsmall, moon

Plsmall, moon, far

Page 15: Identifying Traits with Formal Concept Analysis

ASE‘05 - Identifying Traits with FCA / 16Adrian Lienhard - [email protected] 6

small far moonMercur

yx

Mars x xJupiter x xPluto x x x

elem

ents

properties

{Mars, Pluto, Jupiter},{moon}

Concept: maximal group of elements based on their common properties

{Pluto, Jupiter},{moon, far}

less elementsmore properties

FCA in a Nutshell

Concept Lattice

Ma, Ju, Plmoon

Me, Ma, Ju, Pl

Me, Ma, Plsmall

Ju, Plmoon, far

Ma, Plsmall, moon

Plsmall, moon, far

Page 16: Identifying Traits with Formal Concept Analysis

ASE‘05 - Identifying Traits with FCA / 16Adrian Lienhard - [email protected] 7

Stage 1

Approach Overview

Stage 2

input generation FCA application filtering manual selection

input generation FCA application manual selection

for each class and trait{

Page 17: Identifying Traits with Formal Concept Analysis

ASE‘05 - Identifying Traits with FCA / 16Adrian Lienhard - [email protected]

‣ ST-80 Stream and Collection libraries

‣ Comparison with purely manual refactoring [OOPSLA’03]

‣ Illustration of our approach on example of Stream hierarchy...

8

Case Study

Page 18: Identifying Traits with Formal Concept Analysis

ASE‘05 - Identifying Traits with FCA / 16Adrian Lienhard - [email protected] 9

Generating Input for FCADetecting cancellations and duplications

Fact ST-80 Collection library:131 methods too high (~10%), and of those, 106 implicitly cancelled

Stream...

nextbooleanint32...

WriteStream...

...

ReadWriteStream...

next...

ReadStream...

next....

next

sends message next

Identify real interface of a class taking into account...

Idiom “too high”‣ implement methods “too

high” in common superclass‣ (implicitly) cancel methods

in inappropriate subclasses

Stage 1

Page 19: Identifying Traits with Formal Concept Analysis

ASE‘05 - Identifying Traits with FCA / 16Adrian Lienhard - [email protected] 9

Generating Input for FCADetecting cancellations and duplications

Fact ST-80 Collection library:131 methods too high (~10%), and of those, 106 implicitly cancelled

Stream...

nextbooleanint32...

WriteStream...

...

ReadWriteStream...

next...

ReadStream...

next....

next

sends message next

invokes cancelled method

Identify real interface of a class taking into account...

Idiom “too high”‣ implement methods “too

high” in common superclass‣ (implicitly) cancel methods

in inappropriate subclasses

Stage 1

Page 20: Identifying Traits with Formal Concept Analysis

ASE‘05 - Identifying Traits with FCA / 16Adrian Lienhard - [email protected] 10

Elements: all concrete classesProperties: all concrete methods (identifying duplications)Mapping: method in the real interface of the class

WS, RWS, RS

atEnd

WS

atEnd, nextPut:, next:put:,

contents1

RWS

atEnd, nextPut:, next:put:, next,

boolean, contents2

RS

atEnd, next, boolean,

contents3

WS, RWS

atEnd, nextPut:, next:put:

RWS, RS

atEnd, next, boolean

atEnd, nextPut:, next:put: next,

boolean, contents1,2,3

Applying FCAStage 1

Page 21: Identifying Traits with Formal Concept Analysis

ASE‘05 - Identifying Traits with FCA / 16Adrian Lienhard - [email protected]

Filtering

‣ reduce properties-sets to delta between sub-/superconcept

‣ remove concepts with no classes

11

WS, RWS, RS

atEnd

WS

contents1

RWS

contents2

RS

contents3

WS, RWS

nextPut:, next:put:

RWS, RS

next, boolean

Stage 1

Page 22: Identifying Traits with Formal Concept Analysis

ASE‘05 - Identifying Traits with FCA / 16Adrian Lienhard - [email protected]

Partly manual task: deciding whether a concept becomes a class or a trait

12

Selecting Traits and Hierarchy Reconstruction

WriteStream ReadWriteStream ReadStream

TWriteableStream TReadableStream

Stream

Stage 1

Page 23: Identifying Traits with Formal Concept Analysis

ASE‘05 - Identifying Traits with FCA / 16Adrian Lienhard - [email protected]

Invocation Analysis

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

13

Stage 2

Page 24: Identifying Traits with Formal Concept Analysis

ASE‘05 - Identifying Traits with FCA / 16Adrian Lienhard - [email protected]

Manual task of selecting traits.Lattice provides interesting alternatives:‣ Additional methods in super-concepts‣ Fewer methods in sub-concepts

14

Selecting Traits

ReadStream ReadWriteStream WriteStream

TReadableStream TWriteableStream

Stream

TWriteablePutData

TWriteablePutCharacter

TWriteableMultiple

TReadableFileIn

TReadableMultiple

Stage 2

Page 25: Identifying Traits with Formal Concept Analysis

ASE‘05 - Identifying Traits with FCA / 16Adrian Lienhard - [email protected]

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

15

Comparison with Manual Refactoring

Page 26: Identifying Traits with Formal Concept Analysis

ASE‘05 - Identifying Traits with FCA / 16Adrian Lienhard - [email protected]

‣ 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

16

Conclusion

Page 27: Identifying Traits with Formal Concept Analysis

ASE‘05 - Identifying Traits with FCA / 16Adrian Lienhard - [email protected]

‣ 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

16

Questions?

Conclusion

Page 28: Identifying Traits with Formal Concept Analysis

ASE‘05 - Identifying Traits with FCA / 16Adrian Lienhard - [email protected]

Refactored Collection Hierarchy

17

TOrderedSorted-Common

Array

TSequenced-ElementAccess

SequenceableCollection

Collection

SortedCollection OrderedCollection

Set

TRemoving-Elements

composed from

inherits from

Legend: