Identifying Traits with Formal Concept Analysis

Post on 29-May-2015

831 views 1 download

Tags:

transcript

Identifying Traits with Formal Concept Analysis

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

Software Composition GroupUniversity of Berne, Switzerland

ASE‘05 - Identifying Traits with FCA / 16Adrian Lienhard - lienhard@iam.unibe.ch 2

Overview

duplicatedmethods

...cancelled“too high”...

ASE‘05 - Identifying Traits with FCA / 16Adrian Lienhard - lienhard@iam.unibe.ch 2

Overview

duplicatedmethods

...cancelled“too high”...

ASE‘05 - Identifying Traits with FCA / 16Adrian Lienhard - lienhard@iam.unibe.ch 2

Overview

duplicatedmethods

...cancelled“too high”...

traits

ASE‘05 - Identifying Traits with FCA / 16Adrian Lienhard - lienhard@iam.unibe.ch 2

Overview

duplicatedmethods

...cancelled“too high”...

traits

FCA

ASE‘05 - Identifying Traits with FCA / 16Adrian Lienhard - lienhard@iam.unibe.ch 3

GraphicalObject

Circle Polygon

Colored Bordered

Generic properties

Background: Traits in a Nutshell

ASE‘05 - Identifying Traits with FCA / 16Adrian Lienhard - lienhard@iam.unibe.ch 3

GraphicalObject

ColoredCircle

Colored

BorderedColoredPol.

Bordered

Colored

Circle Polygon

Colored Bordered

Generic properties

Background: Traits in a Nutshell

ASE‘05 - Identifying Traits with FCA / 16Adrian Lienhard - lienhard@iam.unibe.ch 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

ASE‘05 - Identifying Traits with FCA / 16Adrian Lienhard - lienhard@iam.unibe.ch

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

......

ASE‘05 - Identifying Traits with FCA / 16Adrian Lienhard - lienhard@iam.unibe.ch

‣ FCA in a nutshell

‣ Approach overview

‣ Illustration of our approach on case study

‣ Evaluation of case study

‣ Conclusion

5

Roadmap

ASE‘05 - Identifying Traits with FCA / 16Adrian Lienhard - lienhard@iam.unibe.ch 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

ASE‘05 - Identifying Traits with FCA / 16Adrian Lienhard - lienhard@iam.unibe.ch 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

ASE‘05 - Identifying Traits with FCA / 16Adrian Lienhard - lienhard@iam.unibe.ch 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

ASE‘05 - Identifying Traits with FCA / 16Adrian Lienhard - lienhard@iam.unibe.ch 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

ASE‘05 - Identifying Traits with FCA / 16Adrian Lienhard - lienhard@iam.unibe.ch 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

ASE‘05 - Identifying Traits with FCA / 16Adrian Lienhard - lienhard@iam.unibe.ch 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{

ASE‘05 - Identifying Traits with FCA / 16Adrian Lienhard - lienhard@iam.unibe.ch

‣ 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

ASE‘05 - Identifying Traits with FCA / 16Adrian Lienhard - lienhard@iam.unibe.ch 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

ASE‘05 - Identifying Traits with FCA / 16Adrian Lienhard - lienhard@iam.unibe.ch 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

ASE‘05 - Identifying Traits with FCA / 16Adrian Lienhard - lienhard@iam.unibe.ch 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

ASE‘05 - Identifying Traits with FCA / 16Adrian Lienhard - lienhard@iam.unibe.ch

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

ASE‘05 - Identifying Traits with FCA / 16Adrian Lienhard - lienhard@iam.unibe.ch

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

ASE‘05 - Identifying Traits with FCA / 16Adrian Lienhard - lienhard@iam.unibe.ch

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

ASE‘05 - Identifying Traits with FCA / 16Adrian Lienhard - lienhard@iam.unibe.ch

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

ASE‘05 - Identifying Traits with FCA / 16Adrian Lienhard - lienhard@iam.unibe.ch

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

ASE‘05 - Identifying Traits with FCA / 16Adrian Lienhard - lienhard@iam.unibe.ch

‣ 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

ASE‘05 - Identifying Traits with FCA / 16Adrian Lienhard - lienhard@iam.unibe.ch

‣ 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

ASE‘05 - Identifying Traits with FCA / 16Adrian Lienhard - lienhard@iam.unibe.ch

Refactored Collection Hierarchy

17

TOrderedSorted-Common

Array

TSequenced-ElementAccess

SequenceableCollection

Collection

SortedCollection OrderedCollection

Set

TRemoving-Elements

composed from

inherits from

Legend: