CSC/ECE 517 Fall 2009/wiki2pthelm: Difference between revisions
No edit summary |
No edit summary |
||
(44 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
Reflection-Oriented Programming | == '''Reflection-Oriented Programming''' == | ||
__TOC__ | |||
Reflection is the | '''Reflection-Oriented Programming''' <b>(ROP)</b>, or "reflective programming", is defined as a programming model design with the intent to analyze and improve it's structure, performance or other implementation aspects during runtime (implying no prior knowledge of the [http://en.wikipedia.org/wiki/Source_code source code]), as opposed to compile time. ROP is a natural extension to [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming]. | ||
=='''''Concepts'''''== | |||
Metaprogramming | Two key concepts of ROP are [http://en.wikipedia.org/wiki/Metadata meta-information], which is the state of the program at execution time, and [http://en.wikipedia.org/wiki/Metaprogramming metaprogramming], which is the idea that a language can act as it's own [http://en.wikipedia.org/wiki/Reflection_%28computer_science%29 reflection]. In other words, a language can reflect (modify) itself. More precisely, we define reflection-oriented programming to be a programming style that uses any means available (reflection or otherwise) to extend the meta-information in order to avoid situations in which a local program's requirements leads to non-local rewriting of said program [4]. Additionally, the ROP concepts extends reflection, in that it can make decisions on the [http://en.wikipedia.org/wiki/Software_architecture architecture] of the system, rather than just [http://en.wikipedia.org/wiki/Method_%28computer_science%29 methods] and [http://en.wikipedia.org/wiki/Variable_%28programming%29 variables]. | ||
=='''''Reflection vs. ROP'''''== | |||
Reflection is the process by which a computer program can observe and modify it's own structure and behavior[1]. It allows an executing program to examine or "introspect" upon itself, and manipulate internal properties of the program[2]. [http://en.wikipedia.org/wiki/Java_%28software_platform%29 Java], [http://en.wikipedia.org/wiki/Ruby_%28programming_language%29 Ruby] and [http://en.wikipedia.org/wiki/.NET_Framework .NET] are among the most popular reflective languages, however there are many others[3]. Although reflection is the primary concept behind ROP, it does not necessarily preclude the existence of ROP. ROP is a concept of design; as with most designs, it is not necessarily dependent on the implementation. As an example, computational programming can also be used to implement the concepts of ROP, and does not assume the presence of source code at runtime, something that is essential to the concept of reflection[4]. | |||
The ROP metaprogramming model sequences are defined as either atomic (simple) or compound (multi-atomic). These sequences take advantage of meta-information that retains knowledge of program structure, which is different than a classic object-oriented program that can lose said structure after [http://en.wikipedia.org/wiki/Compiler compilation]. | |||
[1] Reflection (Computer Science), Wikipedia: http://en.wikipedia.org/wiki/Reflection_%28computer_science%29 | =='''''Meta-Information'''''== | ||
[2] Using Java Reflection, Sun: http://java.sun.com/developer/technicalArticles/ALT/Reflection/ | |||
[3] List of reflective programming languages and platforms, Wikipedia: http://en.wikipedia.org/wiki/List_of_reflective_programming_languages_and_platforms | Meta-Information, or metadata, is the state of the program at execution time. Phrased another way, Meta-information is information about information. For example, a if a document is considered to be information, its title, location, and subject are examples of meta-information[5]. Meta-Information is essential not only for reflection but for the concepts of ROP to be implemented correctly. | ||
[4] An Introduction to Reflection-Oriented Programming, Sobel & Friedman: http://www2.parc.com/csl/groups/sda/projects/reflection96/docs/sobel/sobel.pdf | |||
=='''''Metaprogramming'''''== | |||
Metaprogramming introduces the concept of "programs that write programs". Reflection is one type of metaprogramming, and therefore can be considered as an option when attempting to design a system or program based on the concepts defined by ROP. Metaprograms are written in [http://en.wikipedia.org/wiki/Metalanguage Metalanguage][6], which to be considered for ROP would be defined as the same or alternate language as the system or program to be implemented. | |||
=='''''Examples'''''== | |||
<u>''Simple Reflection''</u> | |||
As an example, first let us review what reflection looks like as an implementation. To see how reflection works, consider this simple example, taken from [http://java.sun.com/developer/technicalArticles/ALT/Reflection/ Using Java Reflection]: | |||
import java.lang.reflect.*; | |||
public class DumpMethods { | |||
public static void main(String args[]) | |||
{ | |||
try { | |||
Class c = Class.forName(args[0]); | |||
Method m[] = c.getDeclaredMethods(); | |||
for (int i = 0; i < m.length; i++) | |||
System.out.println(m[i].toString()); | |||
} | |||
catch (Throwable e) { | |||
System.err.println(e); | |||
} | |||
} | |||
} | |||
For an invocation of: | |||
java DumpMethods java.util.Stack | |||
the output is: | |||
public java.lang.Object java.util.Stack.push( | |||
java.lang.Object) | |||
public synchronized | |||
java.lang.Object java.util.Stack.pop() | |||
public synchronized | |||
java.lang.Object java.util.Stack.peek() | |||
public boolean java.util.Stack.empty() | |||
public synchronized | |||
int java.util.Stack.search(java.lang.Object) | |||
That is, the method names of class java.util.Stack are listed, along with their fully qualified parameter and return types. | |||
This program loads the specified class using class.forName, and then calls getDeclaredMethods to retrieve the list of methods defined in the class. java.lang.reflect.Method is a class representing a single class method. Reflection is useful because it supports dynamic retrieval of information about classes and data structures by name, and allows for their manipulation within an executing program[2]. | |||
<u>''ROP''</u> | |||
ROP is more than just simple reflection (although technically, it can be reflection alone), it is also analyzation or improvement at runtime without prior knowledge of source code. A good example of this concept is Measuring Computational Expense from the document [http://www2.parc.com/csl/groups/sda/projects/reflection96/docs/sobel/sobel.pdf An Introduction to Reflection-Oriented Programming]. To implement such behavior, we need only these definitions: | |||
define reflective <runtime> (<simple-reflective>) | |||
slot ticks, init-value: 0; | |||
end; | |||
define method atomic (v, outer :: <runtime>) | |||
update(outer, ticks: add1(outer.ticks)) | |||
end; | |||
define method compound-initial (outer :: <runtime>) | |||
update(outer, ticks: add1(outer.ticks)) | |||
end; | |||
It is convenient to have default behaviors for [http://en.wikipedia.org/wiki/Atomic_operation atomic], compound-initial (specifies what transition should take place upon entering the initial part of a compound computation), and compound-rest (specifies what meta-information gets passed on to the rest of a compound computation) that simply "thread" meta-information through the computations. The inherited definition of compound-rest suffices. Now suppose we want to evaluate some [http://en.wikipedia.org/wiki/Expression_%28programming%29 expression] E without counting its cost. We could use the reify and reflect operations like this: | |||
let r = reify(<runtime>); | |||
let result = E; | |||
reflect(<runtime>, r, result); | |||
Here we capture the expense meta-information before evaluating E and save it as r. After evaluating E, we restore the old meta-information. The result from evaluating E is returned. Of course, any time we want to know the cumulative expense for the program, we can examine reify(<runtime>).ticks[4]. | |||
Java and Ruby are two examples of popular programming languages that are inherently reflective, and therefore could be used to implement an ROP paradigm. | |||
'''''References''''' | |||
[1] Reflection (Computer Science), Wikipedia: http://en.wikipedia.org/wiki/Reflection_%28computer_science%29 <br> | |||
[2] Using Java Reflection, Sun: http://java.sun.com/developer/technicalArticles/ALT/Reflection/<br> | |||
[3] List of reflective programming languages and platforms, Wikipedia: http://en.wikipedia.org/wiki/List_of_reflective_programming_languages_and_platforms<br> | |||
[4] An Introduction to Reflection-Oriented Programming, Sobel & Friedman: http://www2.parc.com/csl/groups/sda/projects/reflection96/docs/sobel/sobel.pdf<br> | |||
[5] What is Metainformation?: http://searchoracle.techtarget.com/sDefinition/0,,sid41_gci900705,00.html<br> | |||
[6] Metaprogramming, Wikipedia: http://en.wikipedia.org/wiki/Metaprogramming |
Latest revision as of 19:14, 13 October 2009
Reflection-Oriented Programming
Reflection-Oriented Programming (ROP), or "reflective programming", is defined as a programming model design with the intent to analyze and improve it's structure, performance or other implementation aspects during runtime (implying no prior knowledge of the source code), as opposed to compile time. ROP is a natural extension to object-oriented programming.
Concepts
Two key concepts of ROP are meta-information, which is the state of the program at execution time, and metaprogramming, which is the idea that a language can act as it's own reflection. In other words, a language can reflect (modify) itself. More precisely, we define reflection-oriented programming to be a programming style that uses any means available (reflection or otherwise) to extend the meta-information in order to avoid situations in which a local program's requirements leads to non-local rewriting of said program [4]. Additionally, the ROP concepts extends reflection, in that it can make decisions on the architecture of the system, rather than just methods and variables.
Reflection vs. ROP
Reflection is the process by which a computer program can observe and modify it's own structure and behavior[1]. It allows an executing program to examine or "introspect" upon itself, and manipulate internal properties of the program[2]. Java, Ruby and .NET are among the most popular reflective languages, however there are many others[3]. Although reflection is the primary concept behind ROP, it does not necessarily preclude the existence of ROP. ROP is a concept of design; as with most designs, it is not necessarily dependent on the implementation. As an example, computational programming can also be used to implement the concepts of ROP, and does not assume the presence of source code at runtime, something that is essential to the concept of reflection[4].
The ROP metaprogramming model sequences are defined as either atomic (simple) or compound (multi-atomic). These sequences take advantage of meta-information that retains knowledge of program structure, which is different than a classic object-oriented program that can lose said structure after compilation.
Meta-Information
Meta-Information, or metadata, is the state of the program at execution time. Phrased another way, Meta-information is information about information. For example, a if a document is considered to be information, its title, location, and subject are examples of meta-information[5]. Meta-Information is essential not only for reflection but for the concepts of ROP to be implemented correctly.
Metaprogramming
Metaprogramming introduces the concept of "programs that write programs". Reflection is one type of metaprogramming, and therefore can be considered as an option when attempting to design a system or program based on the concepts defined by ROP. Metaprograms are written in Metalanguage[6], which to be considered for ROP would be defined as the same or alternate language as the system or program to be implemented.
Examples
Simple Reflection
As an example, first let us review what reflection looks like as an implementation. To see how reflection works, consider this simple example, taken from Using Java Reflection:
import java.lang.reflect.*; public class DumpMethods { public static void main(String args[]) { try { Class c = Class.forName(args[0]); Method m[] = c.getDeclaredMethods(); for (int i = 0; i < m.length; i++) System.out.println(m[i].toString()); } catch (Throwable e) { System.err.println(e); } } }
For an invocation of:
java DumpMethods java.util.Stack
the output is:
public java.lang.Object java.util.Stack.push( java.lang.Object) public synchronized java.lang.Object java.util.Stack.pop() public synchronized java.lang.Object java.util.Stack.peek() public boolean java.util.Stack.empty() public synchronized int java.util.Stack.search(java.lang.Object)
That is, the method names of class java.util.Stack are listed, along with their fully qualified parameter and return types.
This program loads the specified class using class.forName, and then calls getDeclaredMethods to retrieve the list of methods defined in the class. java.lang.reflect.Method is a class representing a single class method. Reflection is useful because it supports dynamic retrieval of information about classes and data structures by name, and allows for their manipulation within an executing program[2].
ROP
ROP is more than just simple reflection (although technically, it can be reflection alone), it is also analyzation or improvement at runtime without prior knowledge of source code. A good example of this concept is Measuring Computational Expense from the document An Introduction to Reflection-Oriented Programming. To implement such behavior, we need only these definitions:
define reflective <runtime> (<simple-reflective>) slot ticks, init-value: 0; end;
define method atomic (v, outer :: <runtime>) update(outer, ticks: add1(outer.ticks)) end;
define method compound-initial (outer :: <runtime>) update(outer, ticks: add1(outer.ticks)) end;
It is convenient to have default behaviors for atomic, compound-initial (specifies what transition should take place upon entering the initial part of a compound computation), and compound-rest (specifies what meta-information gets passed on to the rest of a compound computation) that simply "thread" meta-information through the computations. The inherited definition of compound-rest suffices. Now suppose we want to evaluate some expression E without counting its cost. We could use the reify and reflect operations like this:
let r = reify(<runtime>); let result = E; reflect(<runtime>, r, result);
Here we capture the expense meta-information before evaluating E and save it as r. After evaluating E, we restore the old meta-information. The result from evaluating E is returned. Of course, any time we want to know the cumulative expense for the program, we can examine reify(<runtime>).ticks[4].
Java and Ruby are two examples of popular programming languages that are inherently reflective, and therefore could be used to implement an ROP paradigm.
References
[1] Reflection (Computer Science), Wikipedia: http://en.wikipedia.org/wiki/Reflection_%28computer_science%29
[2] Using Java Reflection, Sun: http://java.sun.com/developer/technicalArticles/ALT/Reflection/
[3] List of reflective programming languages and platforms, Wikipedia: http://en.wikipedia.org/wiki/List_of_reflective_programming_languages_and_platforms
[4] An Introduction to Reflection-Oriented Programming, Sobel & Friedman: http://www2.parc.com/csl/groups/sda/projects/reflection96/docs/sobel/sobel.pdf
[5] What is Metainformation?: http://searchoracle.techtarget.com/sDefinition/0,,sid41_gci900705,00.html
[6] Metaprogramming, Wikipedia: http://en.wikipedia.org/wiki/Metaprogramming