abstractmachine

15 June, 2007

abstract classes

Filed under: abstractmachine, atelier hypermedia, code — Douglas Edric Stanley @ 10:17 am

I saw Stéphane Cousot on my way to the atelier this morning and he suggested I use abstract classes instead of inheritance for my experiments. This too is a concept I know pretty well from other languages, but haven’t really used in Java yet. As it turns out, it’s pretty easy.

Abstract classes are classes that cannot be instantiated. I.e. you can’t turn an abstract class into an object directly. It’s sorta-kinda like a half-finished prefab house: parts of it are already built, but you still have to lay down your own foundation. In my example from yeasterday, the Animation class could have easily been an abstract class, since it was never transformed directly into an object: there was no Animation myAnimation = new Animation();, only Animation myAnimation = new Opening();, or Animation myAnimation = new Middle();.

To actually write an abstract class you just add the word abstract in the class declaration, as well as in the methods that you know will need to be added by the class that inherits it:

abstract class Animation {

  // this is a provided method that will work without creating
  // a new method inside of the subclass. it is not abstract:
  // as you can see it actually contains working code
  
  void setup() {
      background(255);
  }
  
  // this is absolutely necessary and is incomplete by design
  // meaning that the object that inherits this abstract class
  // will absolutely positively have to create its own draw()
  // method, or else the java compiler will yell at you
  
  abstract void draw();

}

If you try to create a concrete object out of your abstract class (Animation), the compiler will yell at you: Semantic Error: Attempt to instantiate an abstract class "Temporary9837080$Animation”. But you can very well tell it to create a new object out of a class that inherits from your abstract class:

void keyPressed() {

  switch(key) {

  case 'a':
    sketch = new SketchA();
    break;

  case 'b':
    sketch = new SketchB();
    break;

  }
  
}

14 June, 2007

Polymorphism

Filed under: abstractmachine, atelier hypermedia, code — Douglas Edric Stanley @ 23:09 pm

I’ll be working with Wolf Ka this week on a new choreography he’s preparing; it’s got a lot of promise, I’m quite happy with what it’s turning into. Anyway, I mention this because he asked me a question that I didn’t really have an answer to: how do you jump from sketch to sketch during a performance when running Processing, i.e. how to build a program-switcher if you will. This is essential for performances where you really want to have controllable chapters or sets or call-them-what-you-will to keep the performance moving.

When he first asked me about it, I made a quick look on the Processing forum but couldn’t find anything to sink my teeth into (let me know if I missed anything). So today we finally took the time to actually investigate the matter, and it turned out to be a pretty easy solve: polymorphism. I knew this method of programming from other languages, but not in Java which I’m only now really getting used to. And so far, at least syntax-wise, I find Java to (thankfully) be clear even enough to show students how it works, which we did this afternoon to my utter delight — and gosh golly, they seemed to get it.

If you don’t know what Polymorphism is, Daniel Shiffman has a great link to a very short page that perfectly explains the concept to anyone who knows a bit already about object-oriented programming: How My Dog Learned Polymorphism.

But if you don’t want to read even that, here’s my take: Polymorphism allows you to make code that calls an object without knowing exactly which object will be called, at least not when you write the code. It allows you to solve that problem later, while the actual code is running. In the case of Wolf’s project, the advantage is pretty clear: he needs the main draw() method to constantly call the draw() method of one of his objects, only he wants the object he calls to change from time to time, depending on the needs of the performance. Early in the performance he wants to tell object no.1 to draw itself, then later object no.2, then object no.3, and you get the point. All at 60 frames per second, which is why you need the program to do it all by itself.

Now, you could do it the ugly way, like this:

int which_sketch = 0;

void draw() {

   switch(which_sketch) {

      case 0:
         doDatThang();
         break;
      case 1:
         doDatUdderThang();
         break;
      case 2:
         goFreakOut();
         break;
      default:
         letItAllHangOut();
         break;

  }

}

So that’s the wrong way. Why is it wrong? Well, it’s okay actually to do it that way, but it’s not good for switching from one program to the next. If you do it this way, you really need to control the flow of your entire code which makes it very sloppy if you want to change anything (which you will). The other problem is that the global variables of doDatThang() are probably going to get mixed up with the global variables of doDatUdderThang(), which is bad. Why don’t you just let the computer do all the work for you, and encapsulate all your variables for one part, or another part, of your program? Then let the computer do all the initialization of your variables for you, and finally let it get rid of those variables whenever you’re ready to move on to the next part of your program. I.e. polymorphism.

There are two ways to do this cleanly in object-oriented programming: through what is called « inheritance », or through what is called « interfacing ». The former is easier so I’ll only talk about it here, but the latter is just as cool so check it out if you’re looking to solve a similar problem. But to understand the later, you need to understand the former, so start at the beginning.

I don’t have the patience to explain everything here, but the idea is basically that you create a non-descript Object that knows two basic methods: void setup() and void draw() (I could have called it startup() and goDoIt(), but I like Processing’s terminology, so I’ll stick with that). Then, once you’ve created that vanilla-flavored Object, you tell all your little sub-programs to all inherit the functions of your vanilla-flavored object. The code looks a little like this:

class Animation {

   void setup() {
   }
   
   void draw() {
   }

}

class Opening extends Animation {
   
   void setup() {
     background(255); 
   }
   
   void draw() {
     // do stuff here 
   }
  
}

class Middle extends Animation {
   
   void draw() {
      background(random(255));
   }
  
}

class Ending extends Animation {
   
   void setup() { 
      background(0); // take inspiration from the Sopranos
   }
   
   void draw() {
      // draw stuff here  
   }
  
}

We now have three classes, all of them extensions of the class Animation. And all of them either inherit the method draw() or directly override the version of Animation with their own. Which means that we can call any of these classes and be sure that something will be drawn, all without error.

But the cool thing about Java is that we can create objects out of the Beginning, the Middle, or the Ending, but program them all as if they’re just another type of Animation, letting the program actually decide what type of Animation it will be at run-time.

Animation myAnimation;

void setup() {

   myAnimation = new Opening();

}

void draw() {

   myAnimation.draw();

}

void mousePressed() {

   int random_choice = (int)random(3);

   switch(random_choice) {

      case 0:
         myAnimation = new Opening();
         break;

      case 1:
         myAnimation = new Middle();
         break;

      case 2:
         myAnimation = new Ending();
         break;

   }

}

The cool thing there is that you just have to tell the program that you’ll be using one of three Objects, all of them containing at least one setup() and one draw(), only you’ll let the computer decide while the program is running which object will actually be the one to run.

The Java trick is to actually declare myAnimation as an Object of the class Animation, whereas in reality it is an Object of the class Beginning, Middle, or Ending. This is the polymorphism part. You are telling it that you don’t know which object you are going to be running at any one time in your program, but that at least all three of the different parts will adhere to the two methods available in Animation, namely setup() and draw().

The final nice thing about this trick is that the new Middle(); or new Ending(); takes care of creating and destroying all the variables for you. No need to get rid of all the variables you created with the first object, and no need to worry about global variables stepping on each other.

Well, re-reading all that, I realize it’s a bit dry. Also, be careful, it’s pseudo-code, as I don’t have Processing in front of my from this terminal (my computer just died). You’ll just have to trust me that I’ll make a full-scale class out of it later on. At least the main details are there, and I’m always willing to accept walks-in to explain it to you on-site, only then you’ll have to come to lovely Aix-en-Provence and I’m just about to leave for my summer vacation ;-)

Bad Apple

Filed under: abstractmachine — Douglas Edric Stanley @ 21:38 pm

Crap. My MacBook just died. I’ll have to take it in tomorrow. Luckily it’s got a 3-year warranty on it. Great machine, don’t get me wrong, quite brilliant. But hard-disks really are a daily gamble, especially considering how much of our lives we put on it. Too fragile. We need to move to solid-state. Anyway it sure sounds like the hard-disk died — yeah, go ahead and laugh, sure I played the Dr. Dougie and put my ear up to the damn thing. It sounds like its the mechanism itself, and not even a bad sector which could be easily fixed — but we’ll see what the magician says tomorrow. I have far less fairy dust (and spare time) than those fellows who are by the way paid to figure it out anyway…

So, long story short, anyone who just sent me email, know that my computer died right after it grabbed about 100 emails off my server. So unfortunately, I have know way of knowing what you just said, nor that you even said it. Sorry, but that’s part of the equation: missives in general must structurally be designed to occasionally lose themselves, otherwise no message would ever find its way…

12 June, 2007

seconde nature

Filed under: abstractmachine, atelier hypermedia, code — Douglas Edric Stanley @ 20:35 pm

The Arborescence and Territoires Electroniques festivals have just combined to form a new festival, a new permanent exhibition space, a new name — Seconde Nature — all at their new home within the Fondation Vasarely in Aix-en-Provence. The opening exhibition, Natural Digital, runs from the 1st of June to the 15th of July, 2007.

Seconde Nature logo

By the very nature of the previous phrase, you should be able to read between the lines and see that this combination is above all strategic : take a bankrupt arts center with its former president on the run in Togo from prison time for embezzlement; add the historical weight of Vasarely who desperately needs to be re-plugged back into the art genealogy that has led us more recently to code-based art (to cite but one example); mix in two up-and-coming festivals of digital arts and digital music run by a very dynamic young group of curators, artists and musicians; all this in the context of an upcoming mayoral campaign that should be, ahem, colorful. But despite this local peplum, it is still a good sign that Seconde Nature has finally become a reality. Both festivals were ready to move on to the next round, and a permanent location was a necessary part of this evolution. Both have been playing with the Vasarely/generative-art mix in interesting ways (cf. Marius Watz’s Electro Plastique from the Processing Exhibition) and I‘m curious to see where this might lead.

We have not yet decided on anything definitive in terms of how I might participate in Seconde Nature, but it is a project that I will absolutely be involved in, in some form or another.

The opening exhibit is fairly small, but with two pieces that I’m quite fond of : Edmond Couchot and Michel Bret’s Les pissenlits and Eduardo Kac’s An Essay Concerning Human Understanding. There are also works from far lesser artists such as this one who nevertheless, thanks to some fancy footwork by his developers, has somehow transformed from what used to be the epitome of the french digital-art caricature into what is now at least a very pretty caricature of what makes digital art, art.

6 June, 2007

re-synchronize

Filed under: abstractmachine, atelier hypermedia, code, plot — Douglas Edric Stanley @ 22:21 pm

Thanks to the dozen or so beta testers that have been playing around with the abstractmachine:synchronizer over the past few days, I’ve been able to spot a few bugs, most of them server-side and therefore requiring no client-side updates. However, this round of beta tests left me somewhat frustrated: while I could see, via server activity, how many people were using the service, none of the users (or « synchronizers ») had any idea how many other people were connected to their browser.

So I added a counter to let users know how many other co-synchronizers are online.

abstractmachine synchronizer user count

It is amazing that I forgot this little detail. Back when I started the abstractmachine project, I was all over this idea and it was almost rule no.1 during the many workshops I gave on networked objects: make the network visible, tangeable. If you can’t feel the networkedness of the network, what’s the point? If you can’t see at least a number on how many people are participating, it’s just surveillance. For your system to become shared, you need some sort of feedback on the scale of your collective interaction. One-on-one is cool, as long as that other person has some context, otherwise it’s just a random passerby. There are of course exceptions to this « rule », but those exceptions are mostly dependant on the project itself.

Unfortunately, this update will require a synchronizer update, but this is all automatic via Firefox’s update system.

abstractmachine synchronizer add-on menu

abstractmachine synchronizer update