lundi 29 septembre 2008

Java to W-Language. The Propagator Pattern

Today we'll pick up this programming perl written in Java and port it into the W-Language.

The Java code implements the Propagator pattern.

The Propagator pattern belongs to a family of patterns for consistently updating objects in a dependency network. The propagator patterns are found in such diverse applications as MAKE, WWW, spreadsheets, GUIs, reactive control systems, simulation systems, distributed file systems, distributed databases, workflow systems and multilevel caches.

Let's start with the Java code :

public abstract class StrictPropagator {
private Vector dependents;

// The constructor
StrictPropagator(int initialCapacity) {

dependents = new Vector(initialCapacity);
}

abstract synchronized void update(StrictPropagator from);

public synchronized void notifyDependents(){
Enumeration e = dependents.elements();
StrictPropagator d;
while (e.hasMoreElements()) {
d = (StrictPropagator)e.nextElement();
d.update(this);
d.notifyDependents();
}
}

public synchronized void addDependent(StrictPropagator d) {
dependents.addElement(d);
}

public synchronized void removeDependent(StrictPropagator d) {

dependents.removeElement(d);
}
}


Now let's have a look at the W-Language code :

StrictPropagator is class
PRIVATE
dependents is array of 0 StrictPropagator
END

PROCEDURE Constructor( )
// We have no need to pre-allocate our array/ java vector

PROCEDURE VIRTUAL update(from is StrictPropagator)

PROCEDURE notifyDependents()
CriticalSectionStart()
d is StrictPropagator dynamic
FOR EACH ELEMENT d OF :dependents
d:update( object )
d:notifyDependents()
END
CriticalSectionEnd()

PROCEDURE addDependents(D is StrictPropagator)
CriticalSectionStart()
ArrayAdd( :dependents, D )
CriticalSectionEnd()

PROCEDURE removeDependents(d is StrictPropagator)
CriticalSectionStart()
ind is int = ArraySeek(:dependents, asLinearFirst, d)
IF ind <> -1 THEN ArrayDelete(:dependents, ind)
CriticalSectionEnd()

Unfortunately the W-Language has no special keyword for
abstract classes.
Don't care too much about that. Just keep in mind that you should not instantiate from an abstract class.

- A Java Vector is in W-Language terminology a dynamic array

- The
synchronized statement can be replaced as shown :
CriticalSectionStart() / CriticalSectionEnd()

- The Java keyword this, has (in this case) the same meaning as the W-language object keyword.

That's it for the moment ... I hope my code coloring makes sense to you.

In the next blog I'll talk about how you can use this pattern in GUI applications, show the UML etc. Stay tuned!

The Propagator pattern is based on a work of :
Peter H. Feiler
Software Engineering Institute
Carnegie Mellon University

Walter F. Tichy
Institute for Informatics
University of Karlsruhe



samedi 27 septembre 2008

What's the story mother ?


It's
about Alien languages, evil APIs, and OOPs .

More details please.

I'll pick up interesting questions regarding OOP, API programming, using C/C++ libraries, porting C/Java code into the W-language.

... show some code, share my ideas and solutions.

Stay tuned!