The Pragmatic Programmer: From Journeyman to Master
Blackboards
The writing is on the wall
You may not usually associate elegance with police detectives , picturing instead some sort of doughnut and coffee clich . But consider how detectives might use a blackboard to coordinate and solve a murder investigation.
Suppose the chief inspector starts off by setting up a large blackboard in the conference room. On it, he writes a single question:
H. D UMPTY (M ALE , E GG ): A CCIDENT OR MURDER ?
Did Humpty really fall, or was he pushed ? Each detective may make contributions to this potential murder mystery by adding facts, statements from witnesses, any forensic evidence that might arise, and so on. As the data accumulates, a detective might notice a connection and post that observation or speculation as well. This process continues, across all shifts, with many different people and agents , until the case is closed. A sample blackboard is shown in Figure 5.6 on the next page.
Figure 5.6. Someone found a connection between Humpty's gambling debts and the phone logs. Perhaps he was getting threatening phone calls.
Some key features of the blackboard approach are:
-
None of the detectives needs to know of the existence of any other detective ”they watch the board for new information, and add their findings.
-
The detectives may be trained in different disciplines, may have different levels of education and expertise, and may not even work in the same precinct. They share a desire to solve the case, but that's all.
-
Different detectives may come and go during the course of the process, and may work different shifts.
-
There are no restrictions on what may be placed on the blackboard. It may be pictures, sentences, physical evidence, and so on.
We've worked on a number of projects that involved a workflow or distributed data gathering process. With each, designing a solution around a simple blackboard model gave us a solid metaphor to work with: all of the features listed above using detectives are just as applicable to objects and code modules.
A blackboard system lets us decouple our objects from each other completely, providing a forum where knowledge consumers and producers can exchange data anonymously and asynchronously. As you might guess, it also cuts down on the amount of code we have to write.
Blackboard Implementations
Computer-based blackboard systems were originally invented for use in artificial intelligence applications where the problems to be solved were large and complex ”speech recognition, knowledge-based reasoning systems, and so on.
Modern distributed blackboard-like systems such as JavaSpaces and T Spaces [URL 50, URL 25] are based on a model of key/value pairs first popularized in Linda [CG90], where the concept was known as tuple space.
With these systems, you can store active Java objects ”not just data ”on the blackboard, and retrieve them by partial matching of fields (via templates and wildcards) or by subtypes . For example, suppose you had a type Author, which is a subtype of Person. You could search a blackboard containing Person objects by using an Author template with a lastName value of "Shakespeare." You'd get Bill Shakespeare the author, but not Fred Shakespeare the gardener.
The main operations in JavaSpaces are:
Name | Function |
---|---|
read | Search for and retrieve data from the space. |
write | Put an item into the space. |
take | Similar to read, but removes the item from the space as well. |
notify | Set up a notification to occur whenever an object is written that matches the template. |
T Spaces supports a similar set of operations, but with different names and slightly different semantics. Both systems are built like a database product; they provide atomic operations and distributed transactions to ensure data integrity.
Since we can store objects, we can use a blackboard to design algorithms based on a flow of objects, not just data. It's as if our detectives could pin people to the blackboard ”witnesses themselves , not just their statements. Anyone can ask a witness questions in the pursuit of the case, post the transcript, and move that witness to another area of the blackboard, where he might respond differently (if you allow the witness to read the blackboard too).
A big advantage of systems such as these is that you have a single, consistent interface to the blackboard. When building a conventional distributed application, you can spend a great deal of time crafting unique API calls for every distributed transaction and interaction in the system. With the combinatorial explosion of interfaces and interactions, the project can quickly become a nightmare.
Organizing Your Blackboard
When the detectives work on large cases, the blackboard may be- come cluttered, and it may become difficult to locate data on the board. The solution is to partition the blackboard and start to organize the data on the blackboard somehow. Different software systems handle this partitioning in different ways; some use fairly flat zones or interests groups, while others adopt a more hierarchical treelike structure. |
The blackboard style of programming removes the need for so many interfaces, making for a more elegant and consistent system.
Application Example
Suppose we are writing a program to accept and process mortgage or loan applications. The laws that govern this area are odiously complex, with federal, state, and local governments all having their say. The lender must prove they have disclosed certain things, and must ask for certain information ”but must not ask certain other questions, and so on, and so on.
Beyond the miasma of applicable law, we also have the following problems to contend with.
-
There is no guarantee on the order in which data arrives. For instance, queries for a credit check or title search may take a substantial amount of time, while items such as name and address may be available immediately.
-
Data gathering may be done by different people, distributed across different offices, in different time zones.
-
Some data gathering may be done automatically by other systems. This data may arrive asynchronously as well.
-
Nonetheless, certain data may still be dependent on other data. For instance, you may not be able to start the title search for a car until you get proof of ownership or insurance.
-
Arrival of new data may raise new questions and policies. Suppose the credit check comes back with a less than glowing report; now you need these five extra forms and perhaps a blood sample.
You can try to handle every possible combination and circumstance using a workflow system. Many such systems exist, but they can be complex and programmer intensive . As regulations change, the work-flow must be reorganized: people may have to change their procedures and hard-wired code may have to be rewritten.
A blackboard, in combination with a rules engine that encapsulates the legal requirements, is an elegant solution to the difficulties found here. Order of data arrival is irrelevant: when a fact is posted it can trigger the appropriate rules. Feedback is easily handled as well: the output of any set of rules can post to the blackboard and cause the triggering of yet more applicable rules.
Tip 43
Use Blackboards to Coordinate Workflow
We can use the blackboard to coordinate disparate facts and agents, while still maintaining independence and even isolation among participants .
You can accomplish the same results with more brute-force methods , of course, but you'll have a more brittle system. When it breaks, all the king's horses and all the king's men might not get your program working again.
Related sections include:
-
The Power of Plain Text
-
It's Just a View
Challenges
-
Do you use blackboard systems in the real world ”the message board by the refrigerator, or the big whiteboard at work? What makes them effective? Are messages ever posted with a consistent format? Does it matter?
Exercises
30. | For each of the following applications, would a blackboard system be appropriate or not? Why?
|