1 / 35

A Logic Meta Programming approach to deal with Code Scattering

A Logic Meta Programming approach to deal with Code Scattering Kris De Volder Software Practices Lab. http://www.cs.ubc.ca/~kdvolder/ kdvolder@cs.ubc.ca The Key Points of This Talk ... 1) Problem: Code Scattering 2) A Proposed Solution Strategy: Logic Meta Programming

Jimmy
Download Presentation

A Logic Meta Programming approach to deal with Code Scattering

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. A Logic Meta Programmingapproach to deal with Code Scattering Kris De Volder Software Practices Lab. http://www.cs.ubc.ca/~kdvolder/ kdvolder@cs.ubc.ca

  2. The Key Points of This Talk ... 1) Problem: Code Scattering 2) A Proposed Solution Strategy: Logic Meta Programming 3) Why Logic Meta Programming? Code Scattering and Logic Meta Programming 2

  3. The Key Points of This Talk ... 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. 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 The implementation of the feature will be scattered. 2) A Proposed Solution Strategy: Logic Meta Programming 3) Why Logic Meta Programming? Code Scattering and Logic Meta Programming 3

  4. The Key Points of This Talk ... 1) Problem: Code Scattering 2) A Proposed Solution Strategy: (Logic) Meta Programming 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 At the meta-level the ‘cross-cutting’ feature/concern can be modularly expressed. => Code generation 3) Why Logic Meta Programming? Code Scattering and Logic Meta Programming 4

  5. Express reasoning The Key Points of This Talk ... 3) Why Logic Meta Programming? Q: Do we have to use a logic language? A: In principle any (meta) programming language can be used but... • A logic language is particularly interesting because • Its declarative nature. • Inference rules • Independent of base modularization • The power of unification Declare knowledge Separation of cross-cutting issues Describing repetitive patterns of code Code Scattering and Logic Meta Programming 5

  6. Rest of This Talk 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 Code Scattering and Logic Meta Programming 6

  7. Example 1: A mismatch with inheritance Todo: Implement the Searchable interface for all data structures that know how to enumerate their elements. interface Searchable { public boolean contains(Element e) } interface Enumerable { public Enumeration elements(); } interface Enumeration { public boolean hasMoreElements(); public Object nextElement(); } Code Scattering and Logic Meta Programming 7

  8. Example 1: A mismatch with inheritance Todo: Implement the Searchable interface for all data structures that know how to enumerate their elements. The implementation: public boolean contains(Object e) { boolean found = false; Enumeration elems = this.elements(); while (!found && (elems.hasMoreElements())) found = e.equal(elems.nextElement()); return found; } But... where do we put this implementation in the program??? Code Scattering and Logic Meta Programming 8

  9. Enumerable UIComp Canvas Example 1: mismatch with inheritance Where do we put the implementation in the program??? Collection Interval Array Problem: There is nosingle place for this code => Copy-paste the method implementation. => Code scattering Code Scattering and Logic Meta Programming 9

  10. This is NOT an isolated example! More examples of code scattering: • Design patterns • Synchronization • Persistence • Debugging, tracing, logging • Security • Efficiency ... Code Scattering and Logic Meta Programming 10

  11. Structure of This Talk 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 Code Scattering and Logic Meta Programming 11

  12. TyRuBa: The Core Language TyRuBa core language = Logic Meta Language for Java = Prolog + “Java Text” terms. Facts person(TheoDHondt). person(KrisDeVolder). Conclusion Condition Rules mortal(?x) :- person(?x). Java Code can appear as text in rules or facts: methodDecl(Stack,{ void push(Object e) contents[top++]=e; } ). Code Scattering and Logic Meta Programming 12

  13. Code Generation with LMP Idea: Code generation based on defining a representation for base programs using facts. Java Program Logic facts representation class Stack class(Stack). extends(Stack,Collection). extends Collection { var(Stack,int,top). int top; constructor(Stack,[int],[i], {…body1…}). Stack(int i) {…body1…} void push(Object e) {…body2…} method(Stack,void,push, [Object],[e], {…body2…}). } Code Scattering and Logic Meta Programming 13

  14. Code Generation with LMP Logic facts represent programs => logic rules generate code! Example method(?Class,?RetType,get<?name>,[],[], {return ?name;}) :- class(?Class), var(?Class,?RetType,?name). Code Scattering and Logic Meta Programming 14

  15. Structure of This Talk 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 5) Status 6) Other research projects Code Scattering and Logic Meta Programming 16

  16. List Repeat Example 1 in TyRuBa Todo: Implement the Searchable interface for all Enumerable data structures. Collection Enumerable Interval Array • Two issues in dealing with code scattering: • 1) Separation: how to express separately • 2) Repetition: how to avoid repetitive code Code Scattering and Logic Meta Programming 17

  17. 1) separation class Array extends Collection implements 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. Code Scattering and Logic Meta Programming 18

  18. Put these facts in the ‘Searchable’ meta-module Put these facts in the ‘Array meta-module’ 1) separation continued 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,...). Separation is easy because meta-level modularization is independent of base level modularization Code Scattering and Logic Meta Programming 19

  19. Meta-level modularization can ‘cross-cut’ base level modularization Code Scattering and Logic Meta Programming 20

  20. 2) repetition The ‘Searchable’ module: implements(Array,Searchable). method(Array,boolean,search,[Object],{...}). implements(List,Searchable). method(List,boolean,search,[Object],{...}). implements(Canvas,Searchable). method(Canvas,boolean,search,[Object],{...}). repeated Separation alone is not enough... also have to deal with repetition. Code Scattering and Logic Meta Programming 21

  21. 2) repetition Use rules rather than just facts. • The condition of a rule expresses where the repeated code should be inserted. • Variables capture generic structure. implements(?Class,Searchable) :- implements(?Class,Enumerable). method(?Class,boolean,contains,[Object],{ public boolean contains(Object e) { ... blah blah blah ... } }) :- implements(?Class,Enumerable) Code Scattering and Logic Meta Programming 22

  22. Structure of This Talk 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 Code Scattering and Logic Meta Programming 23

  23. Example 2: The Visitor Design Pattern Design Patterns [Gamma&al.] capture solutions to common problems which are encountered while designing software. • The implementation ofa 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. Code Scattering and Logic Meta Programming 24

  24. Node Leaf isNode ... accept isLeaf ... accept Example 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); } Tree isNode isLeaf accept TreeVisitor visitNode visitLeaf PrintVisitor visitNode visitLeaf Code Scattering and Logic Meta Programming 25

  25. Tree isNode isLeaf accept Node Leaf isNode ... accept isLeaf ... accept TreeVisitor visitNode visitLeaf Example 2: Generating Visitor 1) Specific facts about this particular visitor rootVisitedNode(TreeVisitor,Tree). visitedNode(TreeVisitor,Node). visitedNode(TreeVisitor,Leaf). 2) Visitor Code Generation abstractmethod(?RootNode,accept,...) :- rootVisitedNode(?Visitor,?RootNode). method(?Visited,accept,...,{...}) :- visitedNode(?Visitor,?Visited). abstractmethod(?Visitor,visit<?Visited>,...) :- visitedNode(?Visitor,?Visited). Code Scattering and Logic Meta Programming 26

  26. Structure of This Talk ... 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 5) Status 6) Other research projects Code Scattering and Logic Meta Programming 27

  27. Example 3: “synchronization” => Scattering A Stack A Stack with Syncronization 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 { 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(); } } } 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(); } } } Code Scattering and Logic Meta Programming 28

  28. Wait for push and pop to exit. Adminstration of busy flag. Example 3: “synchronization” => Scattering A closer look 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 ( ) ; } } } Code Scattering and Logic Meta Programming 29

  29. AOP Solution: Synchronization Aspect Language Separate out ‘synchronization aspect’ from basic functionality. Use a special purpose synchronization aspect language 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 ) { … } } Basic functionality class Stack { public Object peek ( ) { return contents[pos];} public void push (Object e) { contents[++pos]=e; } ... Weaver Aspect declarations mutex(Stack,push,pop) mutex(Stack,push,peek) ... Code Scattering and Logic Meta Programming 30

  30. Example 3: Using LMP for AOP Basic Functionality Code in Java Parser Java code with synchronization Weaver Logic Program Facts representing basic functionality code + Logic program representing aspect declarations Code Scattering and Logic Meta Programming 31

  31. Example 3: Synchronization The key point of this example is : 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). Code Scattering and Logic Meta Programming 32

  32. Example 3: The ‘Aspect Program’ Aspect Declarations: 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). The underlying causes behind the aspect declarations Aspect meta program mutex(?cl,?insp,?mod):- inspects(?cl,?insp,?state), modifies(?cl,?mod,?state). mutex(?cl,?insp,?mod):- modifies(?cl,?insp,?state), modifies(?cl,?mod,?state). The reasoning which infers mutual exclusion patterns from the causes. Code Scattering and Logic Meta Programming 33

  33. Structure of This Talk ... 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 Code Scattering and Logic Meta Programming 35

  34. Recall: Key Points of This Talk ... 1) Problem: Code Scattering • There are two issues in dealing with code scattering • 1) Separation => Dealing with cross-cutting modularity • 2) Repetition => Factoring out ‘generic’ repetitive code 2) Solution Strategy: Logic Meta Programming • Logic meta-programming allows to • 1) Separately express why a code scattering pattern • appears. • 2) Generate code: • one rule => many instantiations of a code pattern 3) Why Logic Meta Programming? Code Scattering and Logic Meta Programming 36

  35. Express reasoning The Key Points of This Talk ... 3) Why Logic Meta Programming? Q: Do we have to use a logic language? A: In principle any (meta) programming language can be used but... • A logic language is particularly interesting because • Its declarative nature. • Inference rules • Independent of base modularization • The power of unification Declare knowledge Separation of cross-cutting issues Describing repetitive patterns of code Code Scattering and Logic Meta Programming 37

More Related