Wednesday, May 23, 2007

Context and Multiple Dispatching

PWB has a mechanism for multiple dispatching.

Some background:

In an Object Oriented language, when you send a message to an object, which method is executed is defined by the class of the receiver.

class ObjectA{function a(){echo 'a';}}
class ObjectB{function a(){echo 'b';}}

$a = new ObjectA();
$a->a();//echoes 'a';

$b = new ObjectB();
$b->a(); //echoes 'b';

As you can see, same message + different class -> different method. This is single dispatching.

PWB provides a way to use Multiple Dispatching. That is, to choose the method based on the types of the parameters to the function. It provides the macro defmdf ("define multiple dispatch function"), and the function mdcall.

#@defmdf assign($user:User, $area:Area){
$user->addArea($area);
}@#

#@defmdf assign($user:User, $group:Group){
$user->addGroup($group);
}@#

#@defmdf assign($file:File, $group:Group){
$file->addGroup($group);$group->attachFile($file);
}@#

And then, to use them:
mdcall('assign', array($user, $group));

Context dispatching, is a similar concept, but based on the types of the context instead of the type of the parameters:

#@defmdf showNews [NewsList] ($news:News){
echo $news->title; echo $news->author->name;
}@#

#@defmdf showNews [NewsView] ($news:News){
echo $news->title; echo $news->title; echo $news->body;
}@#

#@defmdf showNews [AuthorView<-NewsList] ($news:News){
echo $news->title;
}@#

In this case, we show news.
Showing a news article, when you are showing a list of news, is just showing the title, and the author.
Showing a news article, when you are showing only a news article, is showing the title, the author, and the body.
Showing a news article, when you are showing a list of news, in the context of "showing an author", is just showing the title (we already know the author's name from the context).

So, we would call the function:

mdcompcall('showNews',array($context, $news));

the first parameter to showNews (the first element in the array) is the context to use.

This techniques are tholoughly used in YATTAA!

YATTAA!

YATTAA is a new part of PWB. It is basically, a set of classes for building CRUD-based applications, but aiming at making complex applications (having CRUD related tasks simplified).

- CRUD with Create, Update and Delete based on PWB's transparent persistence.
- Object Lists, with automatically generated filters and orders (but redefinable).
- Modules based application, with Landmark pattern for accesing modules, and ActiveReference for accesing module's navigation history.
- Navigation and Operation menus for Components.

Actually, we built a complete and very interesting contact list, creating the class Contact, and using as root of the ContactListModule:

new ObjectsAdmin(#@select Contact@#);

YATTAA stands for Yet Another Toolkit for Template Application Assembly (Obiously, a backronym, based on Heroes' character Hiro).