Enlightenment: Narrative Programming by Tom Radcliffe Find Enlightenment

Narrative Programming
by Tom Radcliffe

Date: 2001-03-06
Forum: Enlightenment
Copyright: Tom Radcliffe

This is a partial draft of a book on the Narrative Programming Framework (NPF). I invented the NPF because I hate writing code, especially boilerplate code like serialization, where all the information is available to generate it automatically. I was at SGML96, where the incomparable Jon Bosak announced the creation of XML, and had worked with structured text (SGML/XML) processing for long enough to understand its generality. Thinking about the application as a document, rather than the document as an application, seemed like a very natural idea, and it still does.

For those outside the structured text (SGML/XML) community, the idea of structured text is to treat documents as nested tree structures--HTML is an example of this, where the <HTML> element contains the <BODY> which contains paragraphs and lists and so on. Structured text processors typically build a tree of objects as the applciation parses the document, to create a programmatic representation of the text. The the application then typically lets the user manipulate this representation of the document, so "the document is the application."

Narrative programming turns this idea around: rather than thinking about building an application to work on documents, think instead of creating a document to build applications. The "document" in this case is the XML Document Type Definition (DTD), which specifies the range of allowed forms a conforming document can have. Once we know the allowed structure of the documents it is supposed to work on, we should be able to automatically generate an application that will only represent conforming documents. The Narrative Programming Framework, which is partially described in the book, is an early attempt to realize this possibility.

The book is incomplete, and the framework code is not publicly available, because the framework is now used inhouse commercially for application development, particularly for rapid prototyping. As I no longer own the code, I'm not free to distribute it. The book is partly about framework design, and partly about the specific design choices I made. As such, it may be of interest to people who are trying to do something similar--the NPF falls well within the compass of the growing trend in "Generative Programming", and I'm pretty pleased to have been involved in GP almost three years before I'd even heard of it.

Automatically generated code is an idea whose time has come, and the overlap between document structure and application structures is ripe for exploitation by code generators because we have a standard language for describing document structure: XML. The NPF is one early attempt to exploit that overlap, and while I don't expect anyone will agree with all my design decisions--I don't agree with all my design choices--I hope this document will be of value to those who are exploring this route.

Table of Contents

NP: Saving OO from Itself
  1. Objects, Languages and Applications
  2. Method and Process
  3. Barriers to Methodology Adoption
  4. An Ideal Methodology
  5. Reuse and Other Myths
  6. Automation of Routine Tasks
  7. Ease of Use
  8. Testing
  9. Summary of An Ideal Methodology

The NP Idea
  1. Introduction
  2. HowtoThinkAboutApplications
  3. Documents and Trees
  4. What This Has To Do With Applications
  5. Advanced Warning About State
  6. Summary

The Narrative Programming Framework
  1. Introduction
  2. What the NPF Is
  3. A Simple Example
  4. GeneratedCode: Declaration
  5. GeneratedCode: Implementation
  6. Summary of Example
  7. XML Serialization
  8. NPFElement Basics
  9. Container Classes
  10. Modified DTD
  11. Summary

Advanced Features That Are Simple To Use
  1. Introduction
  2. Ownership of Children
  3. ID and IDREF Attributes
  4. NMTOKEN Declarations for Type-Safe Child Access
  5. Forwarding Attributes with NMTOKENS
  6. REQUIRED, IMPLIED and FIXED Attributes
  7. Default and Allowed Values
  8. Private, Protected and Virtual Bases
  9. Modified DTD
  10. DTD Structure
  11. Summary

Entity Attributes and Binary Data
  1. Introduction
  2. Internal Entities
  3. External Binary Entities
  4. Binary Entity Declarations
  5. Code Generated for Binary Entities
  6. Binary Entity Serialization
  7. External Text Entities
  8. Summary

Serialization In Detail
  1. Introduction
  2. XML Serialization
  3. Text Entity Serialization
  4. Binary Entity Serialization
  5. Writing In Detail
  6. Reading In Detail
  7. Summary

The NPF State Engine
  1. Introduction
  2. Overview
  3. Syntax
  4. GeneratedCode