Intercalate Logo
Intercalate is a foundation, a framework, a future.
Intercalate is based on principle, designed to embrace, built to sustain.

Example 2 Intercalate as maintenance tool

As for the previous examples of mail-merging, Intercalate generates textual realizations of a model by looking up and replacing keywords in a Template with values found in a Property List.  These realizations are not restricted to a single output file or passive text substitutions.  For example, suppose we wished to be able to access with Java classes personal and vehicle data stored in a relational database.  The final structure of the database schema has not quite settled down yet, but we have to proceed with the project.  We want a means to automatically generate the source code for our Java classes whenever the schema is changed so that the coupling remains correct and current.

Property Lists

The Property List shown below contains a representation of people and their vehicles, as derived from a database schema having tables and columns. A person has a name and identifying number, each with its data type and size. A vehicle is similarly characterized by a vehicle ID, make, and the identifier of its owner.

Property List

{
 javatype = "$sql2java.type$";
 sql2java = {
  char = "String";
  number = "Integer";
 };
 tables = (
  {
   table = "Person";
   columns = (
    { column = "Name"; type = "char"; size = "100"; },
    { column = "ID"; type = "number"; primary_key = ""; }
   );
  },
  {
   table = "Vehicle";
   columns = (
    { column = "VIN"; type = "number"; primary_key = ""; },
    { column = "make"; type = "char"; size = "100"; },
    { column = "owner_ID"; type = "number"; }
   );
  }
 );
}

A Property List may also contain tables which represent regular conversions of values from one domain to another. In this example, the sql2java lookup table is used to convert the database SQL data types into corresponding Java data types.

More advanced Property Lists may additionally contain keys representing default values and relationships between objects in the model.

The values in a Property List are most often text to be substituted into a template, but may also themselves be templates which reference other keys, to any desired degree of nesting. The javatype key is an example of this.

Templates

An Intercalate Template describes how the model, represented by the Property List, should be converted to a textual realization. A Template is composed of text interleaved with keywords. Text is copied to the output verbatim, while keywords direct Intercalate to look up values in the Property List or perform other processing actions. The keywords and control directives are delimited by dollar signs, and, in addition to value substitution, support conditional processing, repeated processing, input and output control, and case adjustments to values.

Java template

$for tables$$write table.java$
public class $table$
{
$for columns$
 private $javatype$ $column$;
$end$
$for columns$
 public $javatype$ get$column(capitalize)$() {
  return $column$;
 }
$end$
$include table.extras$
}
$end$

The Template shown here, when applied to the model above, generates a  Java class for each of the tables listed, with instance variables and accessor methods for the columns. Of course a complete Java class that includes business logic and a persistence mechanism would contain many more methods and is beyond the scope of this example.

Not shown here is a second template, one that generates the SQL that creates the tables that correspond to the structure specified in the property list.

Realizations

Below are the Java classes generated by processing the model through the Template. Some white space has been removed to condense the display.

Person.java

public class Person
{
 private String Name;
 private Integer ID;

 public String getName() {
  return Name;
 }
 public Integer getID() {
  return ID;
 }
}

Vehicle.java

public class Vehicle
{
 private Integer VIN;
 private String make;
 private Integer owner_ID;

 public Integer getVIN() {
  return VIN;
 }
 public String getMake() {
  return make;
 }
 public Integer getOwner_ID() {
  return owner_ID;
 }
}

Keeping on top of changes

So now suppose that it is decided that the database schema is going to change. What happens then?  Look at the next page to find out.
 
Alodar Systems