september 2001 a logic meta programming approach to deal with code scattering kris de volder...

35
september 2001 A Logic Meta Programming approach to deal with Code Scattering Kris De Volder Software Practices Lab. http://www.cs.ubc.ca/~kdvolder/ [email protected]

Post on 21-Dec-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: September 2001 A Logic Meta Programming approach to deal with Code Scattering Kris De Volder Software Practices Lab. kdvolder/ kdvolder@cs.ubc.ca

september 2001

A Logic Meta Programmingapproach to deal with

Code Scattering

A Logic Meta Programmingapproach to deal with

Code Scattering

Kris De Volder

Software Practices Lab.

http://www.cs.ubc.ca/~kdvolder/

[email protected]

Kris De Volder

Software Practices Lab.

http://www.cs.ubc.ca/~kdvolder/

[email protected]

Page 2: September 2001 A Logic Meta Programming approach to deal with Code Scattering Kris De Volder Software Practices Lab. kdvolder/ kdvolder@cs.ubc.ca

November 2002 Code Scattering and Logic Meta Programming 2

The Key Points of This Talk ...The Key Points of This Talk ...

1) Problem: Code Scattering

2) A Proposed Solution Strategy: Logic Meta Programming

3) Why Logic Meta Programming?

Page 3: September 2001 A Logic Meta Programming approach to deal with Code Scattering Kris De Volder Software Practices Lab. kdvolder/ kdvolder@cs.ubc.ca

November 2002 Code Scattering and Logic Meta Programming 3

The implementation of the feature will be scattered.The implementation of the feature will be scattered.

Code scattering occurs when a concern or feature of a software system mismatches with - existing modularity of the system - the programming language's modularity mechanisms

Code scattering occurs when a concern or feature of a software system mismatches with - existing modularity of the system - the programming language's modularity mechanisms

2) A Proposed Solution Strategy: Logic Meta Programming

1) Problem: Code Scattering

There is a growing awareness that:‘Black-box’ abstractions and ‘components’ have fundamental limitations. => Number of research tracks that follow AOP ideas.

There is a growing awareness that:‘Black-box’ abstractions and ‘components’ have fundamental limitations. => Number of research tracks that follow AOP ideas.

3) Why Logic Meta Programming?

The Key Points of This Talk ...The Key Points of This Talk ...

Page 4: September 2001 A Logic Meta Programming approach to deal with Code Scattering Kris De Volder Software Practices Lab. kdvolder/ kdvolder@cs.ubc.ca

November 2002 Code Scattering and Logic Meta Programming 4

2) A Proposed Solution Strategy: (Logic) Meta Programming

1) Problem: Code Scattering

At the meta-level the ‘cross-cutting’ feature/concern can be modularly expressed. => Code generation

At the meta-level the ‘cross-cutting’ feature/concern can be modularly expressed. => Code generation

Use logic meta programming to • express precisely why a code scattering pattern appears

- Underlying causes: knowledge about code, code structuring principles- Reasoning about underlying causes and the code

• Code generation

Use logic meta programming to • express precisely why a code scattering pattern appears

- Underlying causes: knowledge about code, code structuring principles- Reasoning about underlying causes and the code

• Code generation

3) Why Logic Meta Programming?

The Key Points of This Talk ...The Key Points of This Talk ...

Page 5: September 2001 A Logic Meta Programming approach to deal with Code Scattering Kris De Volder Software Practices Lab. kdvolder/ kdvolder@cs.ubc.ca

November 2002 Code Scattering and Logic Meta Programming 5

A logic language is particularly interesting because

• Its declarative nature.

• Inference rules

• Independent of base modularization

• The power of unification

A logic language is particularly interesting because

• Its declarative nature.

• Inference rules

• Independent of base modularization

• The power of unification

Q: Do we have to use a logic language?A: In principle any (meta) programming language can be used but...

Q: Do we have to use a logic language?A: In principle any (meta) programming language can be used but...

Separation of cross-cutting issues

Declare knowledge

Express reasoning

Describing repetitive patterns of code

3) Why Logic Meta Programming?

The Key Points of This Talk ...The Key Points of This Talk ...

Page 6: September 2001 A Logic Meta Programming approach to deal with Code Scattering Kris De Volder Software Practices Lab. kdvolder/ kdvolder@cs.ubc.ca

November 2002 Code Scattering and Logic Meta Programming 6

1) Code scattering: explaining the problem• a first simple example (no solution)

2) Introduction: Logic Meta Programming• LMP and code generation• The TyRuBa system

3) Examples of how to deal with code scattering• Repeat example from 1)• More complicated examples

4) Summary and Conclusions

Rest of This TalkRest of This Talk

Page 7: September 2001 A Logic Meta Programming approach to deal with Code Scattering Kris De Volder Software Practices Lab. kdvolder/ kdvolder@cs.ubc.ca

November 2002 Code Scattering and Logic Meta Programming 7

Example 1: A mismatch with inheritanceExample 1: A mismatch with inheritance

interface Searchable { public boolean contains(Element e)}

interface Searchable { public boolean contains(Element e)}

interface Enumerable { public Enumeration elements();}

interface Enumerable { public Enumeration elements();}

interface Enumeration { public boolean hasMoreElements(); public Object nextElement();}

interface Enumeration { public boolean hasMoreElements(); public Object nextElement();}

Todo: Implement the Searchable interface for all data structures that know how to enumerate their elements.

Page 8: September 2001 A Logic Meta Programming approach to deal with Code Scattering Kris De Volder Software Practices Lab. kdvolder/ kdvolder@cs.ubc.ca

November 2002 Code Scattering and Logic Meta Programming 8

Example 1: A mismatch with inheritanceExample 1: A mismatch with inheritance

public boolean contains(Object e) { boolean found = false; Enumeration elems = this.elements(); while (!found && (elems.hasMoreElements())) found = e.equal(elems.nextElement()); return found;}

public boolean contains(Object e) { boolean found = false; Enumeration elems = this.elements(); while (!found && (elems.hasMoreElements())) found = e.equal(elems.nextElement()); return found;}

The implementation:

But... where do we put this implementation in the program???

Todo: Implement the Searchable interface for all data structures that know how to enumerate their elements.

Page 9: September 2001 A Logic Meta Programming approach to deal with Code Scattering Kris De Volder Software Practices Lab. kdvolder/ kdvolder@cs.ubc.ca

November 2002 Code Scattering and Logic Meta Programming 9

Example 1: mismatch with inheritanceExample 1: mismatch with inheritance

Collection

Interval Array Canvas

EnumerableUIComp

Problem: There is no single place for this code=> Copy-paste the method implementation. => Code scattering

Problem: There is no single place for this code=> Copy-paste the method implementation. => Code scattering

Where do we put the implementation in the program???

Page 10: September 2001 A Logic Meta Programming approach to deal with Code Scattering Kris De Volder Software Practices Lab. kdvolder/ kdvolder@cs.ubc.ca

November 2002 Code Scattering and Logic Meta Programming 10

This is NOT an isolated example!This is NOT an isolated example!

• Design patterns• Synchronization• Persistence• Debugging, tracing, logging• Security • Efficiency...

More examples of code scattering:

Page 11: September 2001 A Logic Meta Programming approach to deal with Code Scattering Kris De Volder Software Practices Lab. kdvolder/ kdvolder@cs.ubc.ca

November 2002 Code Scattering and Logic Meta Programming 11

1) Code scattering: explaining the problem• a first simple example (no solution)

2) Introduction: Logic Meta Programming• LMP and code generation• The TyRuBa system

3) Examples of how to deal with code scattering• Repeat example from 1)• More complicated examples

4) Summary and Conclusions

Structure of This TalkStructure of This Talk

Page 12: September 2001 A Logic Meta Programming approach to deal with Code Scattering Kris De Volder Software Practices Lab. kdvolder/ kdvolder@cs.ubc.ca

November 2002 Code Scattering and Logic Meta Programming 12

ConditionConclusion

TyRuBa: The Core LanguageTyRuBa: The Core Language

TyRuBa core language= Logic Meta Language for Java= Prolog + “Java Text” terms.

TyRuBa core language= Logic Meta Language for Java= Prolog + “Java Text” terms.

mortal(?x) :- person(?x).

Facts

Rules

person(TheoDHondt).person(KrisDeVolder).

methodDecl(Stack,{ void push(Object e) contents[top++]=e; }).

Java Code can appear as text in rules or facts:

Page 13: September 2001 A Logic Meta Programming approach to deal with Code Scattering Kris De Volder Software Practices Lab. kdvolder/ kdvolder@cs.ubc.ca

November 2002 Code Scattering and Logic Meta Programming 13

Code Generation with LMPCode Generation with LMP

class Stack

Logic facts representationJava Programclass(Stack).

Idea: Code generation based on defining a representation for base programs using facts.

{

int top;

Stack(int i) {…body1…}

void push(Object e) {…body2…}

extends(Stack,Collection).

constructor(Stack,[int],[i], {…body1…}).

var(Stack,int,top).

method(Stack,void,push, [Object],[e], {…body2…}).

extends Collection

}

Page 14: September 2001 A Logic Meta Programming approach to deal with Code Scattering Kris De Volder Software Practices Lab. kdvolder/ kdvolder@cs.ubc.ca

November 2002 Code Scattering and Logic Meta Programming 14

Example

method(?Class,?RetType,get<?name>,[],[], {return ?name;}):- class(?Class), var(?Class,?RetType,?name).

Logic facts represent programs => logic rules generate code!

Code Generation with LMPCode Generation with LMP

Page 15: September 2001 A Logic Meta Programming approach to deal with Code Scattering Kris De Volder Software Practices Lab. kdvolder/ kdvolder@cs.ubc.ca

November 2002 Code Scattering and Logic Meta Programming 16

1) Code scattering: explaining the problem• a first simple example (no solution)

2) Introduction: Logic Meta Programming• LMP and code generation• The TyRuBa system

3) Examples of how to deal with code scattering• Repeat example from 1)• More complicated examples

4) Summary and Conclusions5) Status6) Other research projects

Structure of This TalkStructure of This Talk

Page 16: September 2001 A Logic Meta Programming approach to deal with Code Scattering Kris De Volder Software Practices Lab. kdvolder/ kdvolder@cs.ubc.ca

November 2002 Code Scattering and Logic Meta Programming 17

Repeat Example 1 in TyRuBaRepeat Example 1 in TyRuBa

Todo: Implement the Searchable interface for all Enumerable data structures.

Todo: Implement the Searchable interface for all Enumerable data structures.

Collection

Interval Array List

Enumerable

Two issues in dealing with code scattering:1) Separation: how to express separately2) Repetition: how to avoid repetitive code

Two issues in dealing with code scattering:1) Separation: how to express separately2) Repetition: how to avoid repetitive code

Page 17: September 2001 A Logic Meta Programming approach to deal with Code Scattering Kris De Volder Software Practices Lab. kdvolder/ kdvolder@cs.ubc.ca

November 2002 Code Scattering and Logic Meta Programming 18

1) separation1) separation

class Array extends Collectionimplements Enumerable, Searchable { Object [ ] contents ; Object at ( int i ) {

... } void atPut (int i,Object e) {

... } Enumeration elements() {...} boolean search(Object e) {...} }

class Array extends Collectionimplements Enumerable, Searchable { Object [ ] contents ; Object at ( int i ) {

... } void atPut (int i,Object e) {

... } Enumeration elements() {...} boolean search(Object e) {...} }

Separation is easy at the meta-level because each part of the implementation is a separately asserted fact.

Page 18: September 2001 A Logic Meta Programming approach to deal with Code Scattering Kris De Volder Software Practices Lab. kdvolder/ kdvolder@cs.ubc.ca

November 2002 Code Scattering and Logic Meta Programming 19

1) separation continued1) separation continued

Separation is easy because meta-level modularization is independent of base level modularization

Meta-level representation:

class(Array). extends(Array,Collection).implements(Array,Enumerable).implements(Array,Searchable).var(Array,contents,...).method(Array,Object,at,...).method(Array,...)...method(Array,boolean,search,...).

Put these factsin the ‘Searchable’

meta-modulePut these facts in the ‘Array meta-module’

Page 19: September 2001 A Logic Meta Programming approach to deal with Code Scattering Kris De Volder Software Practices Lab. kdvolder/ kdvolder@cs.ubc.ca

November 2002 Code Scattering and Logic Meta Programming 20

Meta-level modularization can ‘cross-cut’ base level modularization

QuickTime™ and aGIF decompressor

are needed to see this picture.

Page 20: September 2001 A Logic Meta Programming approach to deal with Code Scattering Kris De Volder Software Practices Lab. kdvolder/ kdvolder@cs.ubc.ca

November 2002 Code Scattering and Logic Meta Programming 21

2) repetition2) repetition

implements(Array,Searchable).method(Array,boolean,search,[Object],{...}).

implements(List,Searchable).method(List,boolean,search,[Object],{...}).

implements(Canvas,Searchable).method(Canvas,boolean,search,[Object],{...}).

implements(Array,Searchable).method(Array,boolean,search,[Object],{...}).

implements(List,Searchable).method(List,boolean,search,[Object],{...}).

implements(Canvas,Searchable).method(Canvas,boolean,search,[Object],{...}).

Separation alone is not enough... also have to deal with repetition.

The ‘Searchable’ module:

repeated

Page 21: September 2001 A Logic Meta Programming approach to deal with Code Scattering Kris De Volder Software Practices Lab. kdvolder/ kdvolder@cs.ubc.ca

November 2002 Code Scattering and Logic Meta Programming 22

2) repetition2) repetition

implements(?Class,Searchable) :- implements(?Class,Enumerable).

method(?Class,boolean,contains,[Object],{ public boolean contains(Object e) { ... blah blah blah ... }}):- implements(?Class,Enumerable)

implements(?Class,Searchable) :- implements(?Class,Enumerable).

method(?Class,boolean,contains,[Object],{ public boolean contains(Object e) { ... blah blah blah ... }}):- implements(?Class,Enumerable)

• The condition of a rule expresses where the repeated code should be inserted.

• Variables capture generic structure.

Use rules rather than just facts.

Page 22: September 2001 A Logic Meta Programming approach to deal with Code Scattering Kris De Volder Software Practices Lab. kdvolder/ kdvolder@cs.ubc.ca

November 2002 Code Scattering and Logic Meta Programming 23

1) Code scattering: explaining the problem• a first simple example (no solution)

2) Introduction:• Logic Meta Programming in TyRuBa• Some background information about TyRuBa

3) Examples of how to deal with code scattering• Repeat: Implementation of Searchable • Example 2: The Visitor Design Pattern• Example 3: Synchronization Code

4) Summary and Conclusions

Structure of This TalkStructure of This Talk

Page 23: September 2001 A Logic Meta Programming approach to deal with Code Scattering Kris De Volder Software Practices Lab. kdvolder/ kdvolder@cs.ubc.ca

November 2002 Code Scattering and Logic Meta Programming 24

Example 2: The Visitor Design PatternExample 2: The Visitor Design Pattern

Design Patterns [Gamma&al.] capture solutions to common problems which are encountered while designing software.

Design Patterns [Gamma&al.] capture solutions to common problems which are encountered while designing software.

The implementation of a design pattern typically• spans several classes• must be repeated for every instance of the pattern

The implementation of a design pattern typically• spans several classes• must be repeated for every instance of the pattern

Code Scattering!

Example in this presentation: the visitor design pattern:

Visitor intends to separate • the basic implementation of an object structure • from operations over this structure.

Page 24: September 2001 A Logic Meta Programming approach to deal with Code Scattering Kris De Volder Software Practices Lab. kdvolder/ kdvolder@cs.ubc.ca

November 2002 Code Scattering and Logic Meta Programming 25

Example 2: The Visitor Design PatternExample 2: The Visitor Design Pattern

abstract class Tree { boolean isNode() { return false; } boolean isLeaf() { return false; } abstract Object accept(Visitor v);}class Node extends Tree { boolean isNode() {return true;} ... Object accept(Visitor v) { return v.visitNode(this); }}class Leaf extends AbstractTree { boolean isLeaf() {return false;} ... Object accept(Visitor v) { return v.visitLeaf(this); }}

abstract class TreeVisitor { abstract Object visitNode(Node node); abstract Object visitLeaf(Leaf leaf);}

abstract class Tree { boolean isNode() { return false; } boolean isLeaf() { return false; } abstract Object accept(Visitor v);}class Node extends Tree { boolean isNode() {return true;} ... Object accept(Visitor v) { return v.visitNode(this); }}class Leaf extends AbstractTree { boolean isLeaf() {return false;} ... Object accept(Visitor v) { return v.visitLeaf(this); }}

abstract class TreeVisitor { abstract Object visitNode(Node node); abstract Object visitLeaf(Leaf leaf);}

Tree

Node

isNode...accept

Leaf

isLeaf...accept

isNodeisLeafaccept

TreeVisitor

visitNodevisitLeaf

PrintVisitor

visitNodevisitLeaf

Page 25: September 2001 A Logic Meta Programming approach to deal with Code Scattering Kris De Volder Software Practices Lab. kdvolder/ kdvolder@cs.ubc.ca

November 2002 Code Scattering and Logic Meta Programming 26

Example 2: Generating Visitor

Tree

Node

isNode...accept

Leaf

isLeaf...accept

isNodeisLeafaccept

TreeVisitor

visitNodevisitLeaf

abstractmethod(?RootNode,accept,...) :- rootVisitedNode(?Visitor,?RootNode).

method(?Visited,accept,...,{...}) :- visitedNode(?Visitor,?Visited).

abstractmethod(?Visitor,visit<?Visited>,...) :- visitedNode(?Visitor,?Visited).

abstractmethod(?RootNode,accept,...) :- rootVisitedNode(?Visitor,?RootNode).

method(?Visited,accept,...,{...}) :- visitedNode(?Visitor,?Visited).

abstractmethod(?Visitor,visit<?Visited>,...) :- visitedNode(?Visitor,?Visited).

2) Visitor Code Generation

rootVisitedNode(TreeVisitor,Tree).

visitedNode(TreeVisitor,Node).visitedNode(TreeVisitor,Leaf).

rootVisitedNode(TreeVisitor,Tree).

visitedNode(TreeVisitor,Node).visitedNode(TreeVisitor,Leaf).

1) Specific facts about this particular visitor

Page 26: September 2001 A Logic Meta Programming approach to deal with Code Scattering Kris De Volder Software Practices Lab. kdvolder/ kdvolder@cs.ubc.ca

November 2002 Code Scattering and Logic Meta Programming 27

...3) Examples of how to deal with code scattering

• Repeat: Implementation of Searchable • Example 2: The Visitor Design Pattern• Example 3: Synchronization Code

4) Summary and Conclusions5) Status6) Other research projects

Structure of This TalkStructure of This Talk

Page 27: September 2001 A Logic Meta Programming approach to deal with Code Scattering Kris De Volder Software Practices Lab. kdvolder/ kdvolder@cs.ubc.ca

November 2002 Code Scattering and Logic Meta Programming 28

class BoundedStack { private int COOLBUSYprint = 0;

private int COOLBUSYpop = 0;

private int COOLBUSYpush = 0;

private int COOLBUSYfull = 0;

private int COOLBUSYempty = 0;

private int COOLBUSYpeek = 03 int pos = 0; Object[] contents = new Object[MAX]; static final int MAX = 10; public BoundedStack() {} public void print() { synchronized (this) { while (!((COOLBUSYpush == 0) && (COOLBUSYpop == 0) && (COOLBUSYprint == 0))) { try { wait(); } catch (InterruptedException COOLe) {} } ++COOLBUSYprint; } try { System.out.print("["); for (int i=0;i<pos;i++) { System.out.print(contents[i]+" "); } System.out.print("]"); } finally { synchronized(this) { --COOLBUSYprint; notifyAll(); } } } public Object peek() { synchronized (this) { while (!((COOLBUSYpush == 0) && (COOLBUSYpop == 0))) { try { wait(); } catch (InterruptedException COOLe) {} } ++COOLBUSYpeek; } try { return contents[pos]; } finally { synchronized(this) { --COOLBUSYpeek; notifyAll(); } } } public Object pop() { synchronized (this) { while (!((!empty()) && (COOLBUSYpush == 0) && (COOLBUSYprint == 0) && (COOLBUSYfull == 0) && (COOLBUSYempty == 0) && (COOLBUSYpeek == 0) && (COOLBUSYpop == 0))) { try { wait(); } catch (InterruptedException COOLe) {} } ++COOLBUSYpop; } try { return contents[--pos]; } finally { synchronized(this) { --COOLBUSYpop; notifyAll(); } } }

public void push(Object e) { synchronized (this) { while (!((!full()) && (COOLBUSYpop == 0) && (COOLBUSYprint == 0) && (COOLBUSYfull == 0) && (COOLBUSYempty == 0) && (COOLBUSYpeek == 0) && (COOLBUSYpush == 0))) { try { wait(); } catch (InterruptedException COOLe) {} } ++COOLBUSYpush; } try { contents[pos++]=e; } finally { synchronized(this) { --COOLBUSYpush; notifyAll(); } } }

class BoundedStack { private int COOLBUSYprint = 0;

private int COOLBUSYpop = 0;

private int COOLBUSYpush = 0;

private int COOLBUSYfull = 0;

private int COOLBUSYempty = 0;

private int COOLBUSYpeek = 03 int pos = 0; Object[] contents = new Object[MAX]; static final int MAX = 10; public BoundedStack() {} public void print() { synchronized (this) { while (!((COOLBUSYpush == 0) && (COOLBUSYpop == 0) && (COOLBUSYprint == 0))) { try { wait(); } catch (InterruptedException COOLe) {} } ++COOLBUSYprint; } try { System.out.print("["); for (int i=0;i<pos;i++) { System.out.print(contents[i]+" "); } System.out.print("]"); } finally { synchronized(this) { --COOLBUSYprint; notifyAll(); } } } public Object peek() { synchronized (this) { while (!((COOLBUSYpush == 0) && (COOLBUSYpop == 0))) { try { wait(); } catch (InterruptedException COOLe) {} } ++COOLBUSYpeek; } try { return contents[pos]; } finally { synchronized(this) { --COOLBUSYpeek; notifyAll(); } } } public Object pop() { synchronized (this) { while (!((!empty()) && (COOLBUSYpush == 0) && (COOLBUSYprint == 0) && (COOLBUSYfull == 0) && (COOLBUSYempty == 0) && (COOLBUSYpeek == 0) && (COOLBUSYpop == 0))) { try { wait(); } catch (InterruptedException COOLe) {} } ++COOLBUSYpop; } try { return contents[--pos]; } finally { synchronized(this) { --COOLBUSYpop; notifyAll(); } } }

public void push(Object e) { synchronized (this) { while (!((!full()) && (COOLBUSYpop == 0) && (COOLBUSYprint == 0) && (COOLBUSYfull == 0) && (COOLBUSYempty == 0) && (COOLBUSYpeek == 0) && (COOLBUSYpush == 0))) { try { wait(); } catch (InterruptedException COOLe) {} } ++COOLBUSYpush; } try { contents[pos++]=e; } finally { synchronized(this) { --COOLBUSYpush; notifyAll(); } } }

Example 3: “synchronization” => ScatteringExample 3: “synchronization” => Scattering

class BoundedStack { int pos = 0; Object[] contents = new Object[MAX]; static final int MAX = 10; public BoundedStack() {} public void print() { System.out.print("["); for (int i=0;i<pos;i++) { System.out.print(contents[i]+" "); } System.out.print("]"); } public Object peek() { return contents[pos]; } public Object pop() { return contents[--pos]; } public void push(Object e) { contents[pos++]=e; } public boolean empty() { return pos==0;} } public boolean full() { return pos==MAX;} }}

class BoundedStack { int pos = 0; Object[] contents = new Object[MAX]; static final int MAX = 10; public BoundedStack() {} public void print() { System.out.print("["); for (int i=0;i<pos;i++) { System.out.print(contents[i]+" "); } System.out.print("]"); } public Object peek() { return contents[pos]; } public Object pop() { return contents[--pos]; } public void push(Object e) { contents[pos++]=e; } public boolean empty() { return pos==0;} } public boolean full() { return pos==MAX;} }}

public boolean empty() { synchronized (this) { while (!((COOLBUSYpush == 0) && (COOLBUSYpop == 0))) { try { wait(); } catch (InterruptedException COOLe) {} } ++COOLBUSYempty; } try { return pos==0;} finally { synchronized(this) { --COOLBUSYempty; notifyAll(); } } } public boolean full() { synchronized (this) { while (!((COOLBUSYpush == 0) && (COOLBUSYpop == 0))) { try { wait(); } catch (InterruptedException COOLe) {} } ++COOLBUSYfull; } try { return pos==MAX;} finally { synchronized(this) { --COOLBUSYfull; notifyAll(); } } }

public boolean empty() { synchronized (this) { while (!((COOLBUSYpush == 0) && (COOLBUSYpop == 0))) { try { wait(); } catch (InterruptedException COOLe) {} } ++COOLBUSYempty; } try { return pos==0;} finally { synchronized(this) { --COOLBUSYempty; notifyAll(); } } } public boolean full() { synchronized (this) { while (!((COOLBUSYpush == 0) && (COOLBUSYpop == 0))) { try { wait(); } catch (InterruptedException COOLe) {} } ++COOLBUSYfull; } try { return pos==MAX;} finally { synchronized(this) { --COOLBUSYfull; notifyAll(); } } }

A Stack A Stack with Syncronization

Page 28: September 2001 A Logic Meta Programming approach to deal with Code Scattering Kris De Volder Software Practices Lab. kdvolder/ kdvolder@cs.ubc.ca

November 2002 Code Scattering and Logic Meta Programming 29

Example 3: “synchronization” => Scattering

public Object peek ( ) { while ( true ) { synchronized ( this ) { if ( (BUSY_pop==0) && (BUSY_push==0) ) { ++ BUSY_peek ; break ; } } try { wait ( ) ; } catch ( InterruptedException COOLe ) { } } try { return contents [ pos ]; } finally { synchronized ( this ) { -- BUSY_peek ; notifyAll ( ) ; } } }

public Object peek ( ) { while ( true ) { synchronized ( this ) { if ( (BUSY_pop==0) && (BUSY_push==0) ) { ++ BUSY_peek ; break ; } } try { wait ( ) ; } catch ( InterruptedException COOLe ) { } } try { return contents [ pos ]; } finally { synchronized ( this ) { -- BUSY_peek ; notifyAll ( ) ; } } }

A closer look Wait for push and pop to exit.

Adminstration of busy flag.

Page 29: September 2001 A Logic Meta Programming approach to deal with Code Scattering Kris De Volder Software Practices Lab. kdvolder/ kdvolder@cs.ubc.ca

November 2002 Code Scattering and Logic Meta Programming 30

Separate out ‘synchronization aspect’ from basic functionality.Use a special purpose synchronization aspect language

Separate out ‘synchronization aspect’ from basic functionality.Use a special purpose synchronization aspect language

class Stack {public Object peek ( ) { return contents[pos];} public void push (Object e) { contents[++pos]=e; } ...

class Stack {public Object peek ( ) { return contents[pos];} public void push (Object e) { contents[++pos]=e; } ...

Basic functionality

mutex(Stack,push,pop)mutex(Stack,push,peek)...

mutex(Stack,push,pop)mutex(Stack,push,peek)...

Aspect declarationsWeaver

class Stack {public Object peek ( ) { while (true) { synchronized (this) { … } } try { return contents [ pos ];} finally { synchronized ( this ) { … }}public void push ( Object e ) { while (true) { synchronized (this) { … } } try { contents[++pos]=e; } finally { synchronized ( this ) { … }}

class Stack {public Object peek ( ) { while (true) { synchronized (this) { … } } try { return contents [ pos ];} finally { synchronized ( this ) { … }}public void push ( Object e ) { while (true) { synchronized (this) { … } } try { contents[++pos]=e; } finally { synchronized ( this ) { … }}

AOP Solution: Synchronization Aspect Language

AOP Solution: Synchronization Aspect Language

Page 30: September 2001 A Logic Meta Programming approach to deal with Code Scattering Kris De Volder Software Practices Lab. kdvolder/ kdvolder@cs.ubc.ca

November 2002 Code Scattering and Logic Meta Programming 31

Example 3: Using LMP for AOP

Logic program representing aspect

declarations

Weaver

Logic Program

Facts representingbasic functionality

code

+Java code

with synchronization

Basic Functionality Codein

Java

Parser

Page 31: September 2001 A Logic Meta Programming approach to deal with Code Scattering Kris De Volder Software Practices Lab. kdvolder/ kdvolder@cs.ubc.ca

November 2002 Code Scattering and Logic Meta Programming 32

Example 3: Synchronization

We can use an intuitive Logic Meta Program to capture the reasoning behind mutual exclusion patterns

We can use an intuitive Logic Meta Program to capture the reasoning behind mutual exclusion patterns

Q: Why do we make peek and push mutually exclusive?A: Because

• push modifies the state of the stack and• peek inspects the state of the stack.

capture this as a logic rule:

mutex(?class,?inspector,?modifier) :- inspects(?class,?inspector,?state), modifies(?class,?modifier,?state).

mutex(?class,?inspector,?modifier) :- inspects(?class,?inspector,?state), modifies(?class,?modifier,?state).

The key point of this example is :

Page 32: September 2001 A Logic Meta Programming approach to deal with Code Scattering Kris De Volder Software Practices Lab. kdvolder/ kdvolder@cs.ubc.ca

November 2002 Code Scattering and Logic Meta Programming 33

Example 3: The ‘Aspect Program’

modifies(Stack,push,this).modifies(Stack,pop,this).inspects(Stack,peek,this).inspects(Stack,empty,this).inspects(Stack,full,this).modifies(Stack,print,System.Out).inspects(Stack,print,this).

modifies(Stack,push,this).modifies(Stack,pop,this).inspects(Stack,peek,this).inspects(Stack,empty,this).inspects(Stack,full,this).modifies(Stack,print,System.Out).inspects(Stack,print,this).

mutex(?cl,?insp,?mod):- inspects(?cl,?insp,?state), modifies(?cl,?mod,?state).mutex(?cl,?insp,?mod):- modifies(?cl,?insp,?state), modifies(?cl,?mod,?state).

mutex(?cl,?insp,?mod):- inspects(?cl,?insp,?state), modifies(?cl,?mod,?state).mutex(?cl,?insp,?mod):- modifies(?cl,?insp,?state), modifies(?cl,?mod,?state).

Aspect Declarations:

Aspect meta program

The underlying causes behind

the aspect declarations

The reasoning which infers

mutual exclusion patterns from the

causes.

Page 33: September 2001 A Logic Meta Programming approach to deal with Code Scattering Kris De Volder Software Practices Lab. kdvolder/ kdvolder@cs.ubc.ca

November 2002 Code Scattering and Logic Meta Programming 35

...3) Examples of how to deal with code scattering

• Repeat: Implementation of Searchable • Example 2: The Visitor Design Pattern• Example 3: Synchronization Code

4) Summary and Conclusions

Structure of This TalkStructure of This Talk

Page 34: September 2001 A Logic Meta Programming approach to deal with Code Scattering Kris De Volder Software Practices Lab. kdvolder/ kdvolder@cs.ubc.ca

November 2002 Code Scattering and Logic Meta Programming 36

Logic meta-programming allows to1) Separately express why a code scattering pattern

appears.2) Generate code:

one rule => many instantiations of a code pattern

Logic meta-programming allows to1) Separately express why a code scattering pattern

appears.2) Generate code:

one rule => many instantiations of a code pattern

There are two issues in dealing with code scattering1) Separation => Dealing with cross-cutting modularity2) Repetition => Factoring out ‘generic’ repetitive code

There are two issues in dealing with code scattering1) Separation => Dealing with cross-cutting modularity2) Repetition => Factoring out ‘generic’ repetitive code

3) Why Logic Meta Programming?

1) Problem: Code Scattering

2) Solution Strategy: Logic Meta Programming

Recall: Key Points of This Talk ...Recall: Key Points of This Talk ...

Page 35: September 2001 A Logic Meta Programming approach to deal with Code Scattering Kris De Volder Software Practices Lab. kdvolder/ kdvolder@cs.ubc.ca

November 2002 Code Scattering and Logic Meta Programming 37

A logic language is particularly interesting because

• Its declarative nature.

• Inference rules

• Independent of base modularization

• The power of unification

A logic language is particularly interesting because

• Its declarative nature.

• Inference rules

• Independent of base modularization

• The power of unification

Q: Do we have to use a logic language?A: In principle any (meta) programming language can be used but...

Q: Do we have to use a logic language?A: In principle any (meta) programming language can be used but...

Separation of cross-cutting issues

Declare knowledge

Express reasoning

Describing repetitive patterns of code

3) Why Logic Meta Programming?

The Key Points of This Talk ...The Key Points of This Talk ...