TreeModel ZERO, a taste of life as it should be

If bugmasters are allowed to blog wishlists, then developers should also be allowed to write them! Which is why I wrote my wishlist!

Gtk.TreeModel was in my humble opinion designed wrong. In API design should an interface be just one thing.

A little bit of history

Many framework designers have repeated this in the past. Two of the best framework designers that we have on this planet, Krysztof Cwalina and Brad Abrams from Microsoft, added the meme to one of their books. It would be unfair to only mention those two guys and not the other people at Microsoft, and before that at the Delphi team at Borland. Brian Pepin notes at page 83 of Framework Design Guidelines: “Another sign that you’ve got a well defined interface is that the interface does exactly one thing. If you have an interface that has a grab bag of functionality, that’s a warning sign.”

The problem

What are the things a Gtk.TreeModel are or represent?

  • It’s something that is iterable
  • It’s something that is an iterator
  • It’s apparently something that has columns, which should have been at the View’s side of the story
  • It’s something that can be a tree
  • It’s something that emits row changes

That’s not one thing, and therefore we have a warning sign. If I count it correctly that’s at least five things, so that’s a big warning sign.

I’m sure I can come up with a few other things that a Gtk.TreeModel actually represents. For example its unref_node and ref_node make me think that it’s a garbage collector or something, too.

This is absolutely not good. I believe it is what makes the interface shockingly complicated. Because none of those five things can be made reusable this way.

What I think would be the right way

A prerequisite for this, and presumably also the reason why Gtk+ developers decided to do Gtk.TreeModel the way they did it a few years ago, is a collection framework.

Sadly is this proposal being ~ignored by the current GLib maintainers. Understandable because everybody is overloaded and busy, but in my opinion it’s nonetheless blocking us from heading in the right direction.

There are by the way quite a lot of other reasons mentioned on the proposal. This is just one of them.

interface GLib.Iterable {
	Iterator iterator();
}

interface GLib.Iterator {
	bool next ();
	object current;
}

Next would be recursive iterators or trees. There are many ways to represent these, but I’ll just take a simple route. Remember that when picking an API design, the most simple idea is often the most right one. But yeah, you can probably improve this.

interface GLib.TreeIterable : GLib.Iterable {
	GLib.TreeIterable get_children ();
	int n_children;
	bool has_child (GLib.TreeIterable e);
	GLib.TreeIterable parent;
}

In Gtk+ we would have the view, of course. It would hold the columns, as it should be.

class Gtk.TreeView {
	int n_columns;
	GLib.Type get_column_type (int n);
	GLib.TreeModel model;
	Gtk.ColumnBinding binding;
	Gtk.TreeView (GLib.TreeModel m);
	GLib.ColumnBinding column_binding;
}

We don’t have guaranteed introspection in Gtk+. To do the binding between a column in the view and a property of an instance in the model we need some code. In Gtk.TreeModel this is the get_value function.

It shouldn’t be part of the Gtk.TreeModel: That way it ain’t reusable and will it require each person implementing a Gtk.TreeModel to reinvent the code.

abstract class GLib.ColumnBinding {
	abstract GLib.Value get_value (GLib.TreeModel model,
	                               GLib.TreeIterable e,
	                               int column);
}

Let’s have some concrete column bindings:

class Gtk.TreeStoreColumnBinding : GLib.ColumnBinding {
}

class Gtk.ListStoreColumnBinding : Gtk.TreeStoreColumnBinding {
}

If we do have introspection we can do the same thing .NET offers: Link up the column number with a property name that can be found in the type of the instances that the model holds.

class GI.IntrospectColumnBinding : GLib.ColumnBinding {
	void add_column (int column, string prop_name);
}

These wouldn’t change at all, except that they implement GLib.TreeModel instead of Gtk.TreeModel

class Gtk.TreeStore : GLib.TreeModel {
}

class Gtk.ListStore : GLib.TreeModel {
}

And then we are at Gtk.TreeModel, of course. Well just take everything that we don’t do yet. That’s the row change emissions, right? Personally I think rows are too specific. A model is something that can be iterated. Being iterable doesn’t mean that you have rows, it just means that you have things that the consumer, the view in a model’s case, can iterate to. Let’s call them nodes.

Gtk.TreePath sounds to me like serializing and deserializing a location. It’s nothing special, just a way to formulate pointing to a node in the tree. It’s the model that exposes this capability.

I’m not sure about flags. Maybe it should just be moved to Gtk.TreeView. I don’t get the point of the flags anyway. Both ITERS_PERSIST and LIST_ONLY sound like an implementation detail to me: not something you want to expose to the API anyway. But fine, for sake of completeness I’ll put it here.

interface GLib.TreeModel : GLib.TreeIterable {
	signal node_changed (GLib.TreeIterable e);
	signal node_inserted (GLib.TreeIterable e);
	signal node_deleted  (GLib.TreeIterable e);
	signal node_reordered (GLib.TreeIterable e);
	GLib.TreeModelFlags flags;
	GLib.TreePath get_path (GLib.TreeIterable e);
	GLib.TreeIterable get_node (GLib.TreePath p);
}

Who’ll start GLib 4.0? Let’s do this stuff while the desktop guys play with GNOME 3.0? Why not?

The impact of a highly improbable event

Being in free time mode today I decided to continue reading Nassim Nicholas Taleb’s Black Swan book. Nassim Taleb is about as arrogant as I am, so I’m enjoying reading his book a lot.

I for example enjoyed reading how he’s pissed at today’s academic philosophers for having become exercisers in linguistics rather than getting to the point of thinking. Nassim Taleb himself needed about 350 pages of text to basically say that the Gauss curve is useless in extremistan, but usually useful in mediocristan, and that Mandelbrot’s fractals are a little bit more useful for extremistan. But not really.

Anyway, he also wrote things that we should consider thinking about. For example: We no longer believe in papal infal­li­bil­ity; we seem to believe in the infal­li­bil­ity of the Nobel prize winners. That’s a good point.

One more chapter and I’m relieved of this book. Apparently I enjoy the distress of reading Nassim Taleb’s books.

He’s going to tell me in this chapter how to deal with these highly improbable events that have a great impact, referred to as black swans. I have to congratulate Nassim Taleb for succeeding making me a hyperskeptic, which was a highly improbable event. But then again, being a software developer I’m into bottom-up acquisition of knowledge. Which means that for me it’s more easy to be Fat Tony, than to be Dr. John. You need to read the book’s chapter 17 to understand Fat Tony. I was quite a skeptic before I started reading the book. But not (always) about the kind of things Nassim Taleb asks us to be skeptic .

Pat Condell on ultra tolerant liberal left people

Not watching youtubers very often I almost forgot about Pat Condell’s video blog. Today I decided to take a look at his latest video material.

Pat Condell is, just like me, an outspoken atheist who enjoys exercising his freedom of speech to criticize various religions. Fairly often he criticizes Islam.

Before I continue I’ll remind people that, like Pat Condell, I have nothing in particular against Islam. I don’t have anything against peaceful people in general. Christian, Muslim, atheist, Buddhist or whatever: I don’t care that much. I don’t believe any of those fairy tales, but it’s your freedom to do! I do care about it when, in for example Western countries, countless Christians try to expunge you from society because “you don’t believe in anything”. For many of them not believing is worse than believing in the wrong God, or being a Satanist, or being a sadist. I want to criticize religions and I want to stress the importance of having the right to criticize religions.

Pat takes on the ultra tolerant liberal left people in this video. Just like Pat I used to be on the liberal left. And just like Pat, because I believe in things like social justice, tolerance and respect, I am no longer on the liberal left. Here’s a quote from the video:

You people have certainly reminded me , as if I needed reminding, why my political views have changed in recent years. You see.. foolishly, perhaps, I used to take freedom for granted.

But now thanks to ultra tolerant self hating-multicultural lemmings like you, I don’t.

Politically I used to always be on the liberal left. Because I believe in things like social justice, tolerance and respect. You know, the good things in life. I still believe in those things, which is why I’m no longer on the liberal left.

Apologists for evil

In this video Pat talks about banning the burka. Given that wearing a burka in Western countries is most definitely only done to make a pathetic political statement, I think it is indeed a good idea to ban burkas. Besides you’re not allowed to wear ski masks when you enter a bank either. You’re not allowed to walk naked in the streets. Yet countless people are trying to claim that these women should have a right to wear burkas. Framing it that way is of course utter bullshit: the debate isn’t about women rights at all. Claiming that it is, is being intellectually dishonest. The debate is about the right for a Islamist husband to claim ownership over a woman or a girl. This isn’t a right in Western countries. The fact that it isn’t, is a good thing.

Pat also points out that Western feminists are rather silent about women rights in Islam. Usually feminists are assertive and confident but this time, apparently, feminists are muted on the issue. Why is that? Where are they?

Ban the burka

For the person who recently debated religion with me (you know who you are): I recently read “Letter to A Christian Nation” by Sam Harris. Very interesting read. I recommend it!

Async with the mainloop

A technique that we started using in Tracker is utilizing the mainloop to do asynchronous functions. We decided that avoiding threads is often not a bad idea.

Instead of instantly falling back to throwing work to a worker thread we try to encapsulate the work into a GSource’s callback, then we let the callback happen until all of the work is done.

An example

You probably know sqlite3’s backup API? If not, it’s fairly simple: you do sqlite3_backup_init, followed by a bunch of sqlite3_backup_step calls, finalizing with sqlite3_backup_finish. How does that work if we don’t want to block the mainloop?

I removed all error handling for keeping the code snippet short. If you want that you can take a look at the original code.

static gboolean
backup_file_step (gpointer user_data)
{
  BackupInfo *info = user_data; int i;
  for (i = 0; i < 100; i++) {
    if ((info->result = sqlite_backup_step(info->backup_db, 5)) != SQLITE_OK)
        return FALSE;
  }
  return TRUE;
}

static void
backup_file_finished (gpointer user_data)
{
  BackupInfo *info = user_data;
  GError *error = NULL;
  if (info->result != SQLITE_DONE) {
    g_set_error (&error, _DB_BACKUP_ERROR,
                 DB_BACKUP_ERROR_UNKNOWN,
                 "%s", sqlite3_errmsg (
                    info->backup_db));
  }
  if (info->finished)
    info->finished (error, info->user_data);
  if (info->destroy)
    info->destroy (info->user_data);
  g_clear_error (&error);
  sqlite3_backup_finish (info->backup);
  sqlite3_close (info->db);
  sqlite3_close (info->backup_db);
  g_free (info);
}

void
my_function_make_backup (const gchar *dbf, OnBackupFinished finished,
                         gpointer user_data, GDestroyNotify destroy)
{
  BackupInfo *info = g_new0(BackupInfo, 1);
  info->user_data = user_data;
  info->destroy = destroy;
  info->finished = finished;
  info->db = db;
  sqlite3_open_v2 (dbf, &info->db, SQLITE_OPEN_READONLY, NULL);
  sqlite3_open ("/tmp/backup.db", &info>backup_db);
  info->backup = sqlite3_backup_init (info->backup_db, "main",
                                      info->db, "main");
  g_idle_add_full (G_PRIORITY_DEFAULT, backup_file_step,
                   info, backup_file_finished);
}

Note that I’m not suggesting to throw away all your threads and GThreadPool uses now.
Note that just like with threads you have to be careful about shared data: this way you’ll allow that other events on the mainloop will interleave your backup procedure. This is async(ish), it’s precisely what you want, of course.

More introduction to RDF and SPARQL

Introduction

I plan to give an introduction to features like COUNT, FILTER REGEX and GROUP BY which are supported by Tracker‘s SPARQL engine. We support more such features but I have to start the introduction somewhere. And overloading people with introductions to all features wont help me much with explaining things.

Since my last introduction to RDF and SPARQL I have added a few relationships and actors to the game.

We have Morrel, Max and Sasha being dogs, Sheeba and Query are cats, Picca is still a parrot, Fred and John are contacts. Fred claims that John is his friend. I changed the ontology to allow friendships between the animals too: Sasha claims that Morrel and Max are her friends. Sheeba claims Query is her friend. John bought Query. Fred being inspired by John decided to also get some pets: Morrel, Sasha and Sheeba.

Ontology

Let’s put this story in Turtle:

<test:Picca> a test:Parrot, test:Pet ;
	test:name "Picca" .

<test:Max> a test:Dog, test:Pet ;
	test:name "Max" .

<test:Morrel> a test:Dog, test:Pet ;
	test:name "Morrel" ;
	test:hasFriend <test:Max> .

<test:Sasha> a test:Dog, test:Pet ;
	test:name "Sasha" ;
	test:hasFriend <test:Morrel> ;
	test:hasFriend <test:Max> .

<test:Sheeba> a test:Cat, test:Pet ;
	test:name "Sheeba" ;
	test:hasFriend <test:Query> .

<test:Query> a test:Cat, test:Pet ;
	test:name "Query" .

<test:John> a test:Contact ;
	test:owns <test:Max> ;
	test:owns <test:Picca> ;
	test:owns <test:Query> ;
	test:name "John" .

<test:Fred> a test:Contact ;
	test:hasFriend <test:John> ;
	test:name "Fred" ;
	test:owns <test:Morrel> ;
	test:owns <test:Sasha> ;
	test:owns <test:Sheeba> .

Querytime!

Let’s first start with all friend relationships:

SELECT ?subject ?friend
WHERE { ?subject test:hasFriend ?friend }

  test:Morrel, test:Max
  test:Sasha, test:Morrel
  test:Sasha, test:Max
  test:Sheeba, test:Query
  test:Fred, test:John

Just counting these is pretty simple. In SPARQL all selectable fields must have a variable name, so we add the “as c” here.

SELECT COUNT (?friend) AS c
WHERE { ?subject test:hasFriend ?friend }

  5

We counted friend relationships, of course. Let’s say we want to count how many friends each subject has. This is a more interesting query than the previous one.

SELECT ?subject COUNT (?friend) AS c
WHERE { ?subject test:hasFriend ?friend }
GROUP BY ?subject

  test:Fred, 1
  test:Morrel, 1
  test:Sasha, 2
  test:Sheeba, 1

Actually, we’re only interested in the human friends:

SELECT ?subject COUNT (?friend) AS c
WHERE { ?subject test:hasFriend ?friend .
        ?friend a test:Contact
} GROUP BY ?subject

  test:Fred, 1

No no, we are only interested in friends that are either cats or dogs:

SELECT ?subject COUNT (?friend) AS c
WHERE { ?subject test:hasFriend ?friend .
       ?friend a ?type .
       FILTER ( ?type = test:Dog || ?type = test:Cat)
} GROUP BY ?subject"

  test:Morrel, 1
  test:Sasha, 2
  test:Sheeba, 1

Now we are only interested in friends that are either a cat or a dog, but whose name starts with a ‘S’.

SELECT ?subject COUNT (?friend) as c
WHERE { ?subject test:hasFriend ?friend ;
                 test:name ?n .
       ?friend a ?type .
       FILTER ( ?type = test:Dog || ?type = test:Cat) .
       FILTER REGEX (?n, '^S', 'i')
} GROUP BY ?subject

  test:Sasha, 2
  test:Sheeba, 1

Conclusions

Should we stop talking about ontologies and start talking about searchboxes and user interfaces instead? Although I certainly agree more UI-stuff is needed, I’m not sure yet. RDF and SPARQL are also about relationships and roles. Not just about matching stuff. Whenever we explain the new Tracker to people, most are stuck with ‘matching’ in their mind. They don’t think about a lot of other use-cases.

Such a search is just one use-case starting point: user entered a random search string and gives zero other meaning about what he needs. Many more situations can be starting points: When I select a contact in a user interface designed to show an archive of messages that he once sent to me, the searchbox becomes much more narrow, much more helpful.

As soon as you have RDF and SPARQL, and with Tracker you do, an application developer can start taking into account relationships between resources: The relationship between a contact in Instant Messaging and the attachments in an E-mail that he as a person has sent to you. Why not combine it with friendship relationships synced from online services?

With a populated store you can make the relationship between a friend who joined you on a trip, and photos of a friend of your friend who suggested the holiday location.

With GeoClue integration we could link his photos up with actual location markers. You’d find these photos that came from the friend of your friend, and we could immediately feed the location markers to the GPS software on your phone.

I really hope application developers have more imagination than just global searchboxes.

And this is just a use-case that is technically already possible with today’s high-end phones.

A ridiculous small shellscript

Now, we can finally replace Richard Stallman with a small shellscript

— Alp Toker, Gran Canaria at the Igalia party, 06 juli 2009

I’ll write it in C#

public void ActCrazy () {
   while (true) {
      be incorrect about Mono
   }
}

Finite resources, infinite growth

For some people this post can be controversial. I added a category “controversial” to my blog for people who prefer to filter it.

We start a imaginary experiment where we start with a bottle filled up with food and room left for exactly two worms. We assume worms replicate at a doubling time of one minute. We observed in a previous experiment that the bottle is filled up in exactly one hour. They eat the food as they double themselves, etc (use your imagination).

At 11’O clock in the morning we place two worms in the bottle. At what time will the bottle be full (easy)? At what time will the bottle be half full? At what time is the bottle only 3% filled up?

Humans have a global population growth of about 1.2% per year. It’s about 1% in wealthy countries and about 2-3% in poor countries. If you want to calculate a doubling time you take 70 and you divide it with the growth percentage. Which means that at our current growth rate, we’ll double our total population in 60 years.

In 1950 we were with about 2.7 thousand million people, in 1990 we were with 5 thousand million people. In 2050 we will be with 10 thousand million people. Infinite growth isn’t possible with finite resources. In 2400 years, at current growth rate, the earth’s mass will in theory be roughly equal to the total amount of human flesh.

The main question is, how big is our bottle? Let’s go back to the worms. For the worms the bottle is about 3% filled up at 11:55. It’s half full at 11:59. It’s overpopulated at 12:00. When three new bottles are found and pipes are connected with the first, the three new bottles will be filled up at 12:02. After that will four new bottles be filled up at 12:03. After that you need eight new bottles to survive minute 12:04. In minute 12:05 it starts getting crazy proportions.

Even if our bottle is only 3% filled up now, then still at our retirement age we will inevitably be at 50% capacity. During those retirement years we’ll see the population grow at an enormous speed to maximum capacity within a few years.

I’m among the people who believe that we’re already at 70% capacity of our planet. I think we have about 30 years of finite resources left: doubling the population to 10 thousand million people, is impossible (not unreasonable to think). Moving to another bottle will take us at least several more centuries of top notch space science (so this solution is not applicable). And that’s assuming we can leverage the resources of another planet. Moving to another star is simply out of the question unless we invent technology that allows us to let a huge mass travel at the speed of light (again, the solution isn’t applicable).

A solution that I have in mind? Genetically modifying newborn humans to have an annual fertility frequency and having their fertility enabled at a mature age. Instead of based on the phase of the moon would women be fertile only once per year. And instead of at the average age of 12 would women start becoming fertile at the average age of, for example, 25.

Is genetic modification immoral? Being an atheist I don’t have any believe system that forbids me to tamper with species. It’s indeed still immoral because we don’t know what we are doing, yet. No, morality is not divinely injected by a God. Atheists are born with morals, too.

But if we have to choose between living with each other under the condition of having insufficient resources, or making a change to our species, I know which of the two I will prefer.

Now, if you do believe in a God, then you must also acknowledge that your God’s intention was for us to become intelligent enough to genetically modify our species. If not, why ain’t it stopping us? We, for example, have successfully been genetically selecting dogs for centuries. And we have started genetically modifying them (active modification: interfering with the egg and sperm cells).

Mankind will have to open this difficult discussion sooner or later.