Contextual Polymorphism?

最もよく知られているポリモーフィズムの形は、オブジェクト指向のもの。この場合、メソッドの振る舞いは、どのオブジェクトかに依存して決まる。

最近、Mezini らの Caesar 言語によって、AOP 言語における新しいポリモーフィズムの形が提案されている。簡単に言えば、Aspectual Polymorphism とは、オブジェクトの振る舞いは、さらに、どのアスペクトのがデプロイされているのかによって決まる。


さらには、どのクラス(or コンテキスト)内でそのメソッドを呼び出すかによって振る舞いが変わるようにできると便利かも?

たとえば、アルゴリズムAとアスゴリズムBがあって、実行結果をログしたいとする。ログの出力先のディレクトリは、AとBで異なるようにしたい。


public class AlgorithmA {
public void run() {
// ...
log(...);
}

private void log(...) {
String logDir = LogDirUtil.getDir() + "/AlgorithmA" + ;
String logFile = logDir + "/result.text";
// log
}
}

public class AlgorithmB {
public void run() {
// ...
log(...);
}

private void log(...) {
String logDir = LogDirUtil.getDir() + "/AlgorithmB" + ;
String logFile = logDir + "/result.text";
// log
}
}

アルゴリズムクラスは、どこが出力先のディレクトリなのかを知っている必要があるのか?


で、たとえば、アノテーションとか利用してコンテキストを明示的に表現できるように?


@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface LogContext {
public String value();
}

public aspect LogDirectoryContext {

String around(LogContext context) :
@within(context) && call(String LogDirUtil.getLogDir())
{
return proceed(context) + "/" + context.value();
}
}

@LogContext("AlgorithmA")
public class AlgorithmA {
public void run() {
// ...
log(...);
}

private void log(...) {
String logFile = LogDirUtil.getDir() + "/result.text";
// log
}
}

@LogContext("AlgorithmB")
public class AlgorithmB {
public void run() {
// ...
log(...);
}

private void log(...) {
String logFile = LogDirUtil.getDir() + "/result.text";
// log
}
}

単にコードがアノテーションに移っただけとも解釈できるけど、気分的にはすっきりする感じ?


Aspectual Polymorphism:

Mira Mezini and Klaus Ostermann
Conquering Aspects with Caesar
AOSD'03


Mira Mezini and Klaus Ostermann
Variability Management with Feature-Oriented Programming and Aspects
Foundations of Software Engineering (FSE-12), ACM SIGSOFT, 2004.


Christoph Bockisch, Michael Haupt, Mira Mezini and Klaus Ostermann
Virtual Machine Support for Dynamic Join Points
AOSD'04

DL: http://www.st.informatik.tu-darmstadt.de/public/Publications.jsp


Context Oriented Programming:

Pascal Costanza and Robert Hirschfeld

Language Constructs for Context-oriented Programming - An Overview of ContextL,

Dynamic Languages Symposium, co-located with OOPSLA'05, October 18, 2005, San Diego, California, USA.

DL: http://p-cos.net/research.html