Angel \”Java\” Lopez on Blog

July 9, 2010

Building an Application using AjGenesis (Part 2)

Filed under: AjGenesis, Code Generation, Open Source Projects — ajlopez @ 10:06 pm

This is the second post in the serie, explaining how to build an application using AjGenesis, my open source code generation project. The previous post in this serie:

Building An Application Using AjGenesis (Part 1)

In this second part, I will generate two simple files, from the model defined in the last post.

The files for this second steps can be downloaded from AppExampleStep02.zip.

You need to download the AjGenesis binaries (trunk version) from AjGenesisTrunkBinaries.zip, expand them, AND add the bin folder to your path.

There is the command PrintEntities.cmd:

AjGenesis.Console Project.xml PrintEntities.ajg

AjGenesis.Console is the console program that launch the AjGenesis process. It receives many arguments. If the argument is an .xml file, its content is loaded in memory as an AjGenesis model (explained below). If the argument is an .ajg, the content of the file is a task, a list of commands to execute in AjGenesis. The commands are written in an interpreted language, named AjBasic.

This is the content of Project.xml (read previous post for more details):

<Project>
  <Name>AjApp</Name>
  <Description>Building an Application using AjGenesis</Description>
  <Model>
    <Entities>
      <Entity Source="Customer.xml"/>
      <Entity Source="Supplier.xml"/>
    </Entities>
  </Model>
</Project>

Once the model is loaded in memory, there is a public variable Project with dynamic properties loaded for each attribute and element. From AjBasic, we can access Project, Project.Model, and Project.Model.Entities.

The content of PrintEntities.ajg:

for each Entity in Project.Model.Entities
  Message "Entity " & Entity.Name
  
  for each Property in Entity.Properties
    Message "   Property ${Property.Name}"
  end for
end for

Note the use of for each, an AjBasic command. Message is a command to print something in the output console. The above code shows one of the features of AjBasic: string expansion. That is, the string:

“ Property ${Property.Name}”

is expanded in runtime to the content of the expression between ${ and }. If you run

PrintEntities.cmd

the output is:

Entity Customer

   Property Name

   Property Address

Entity Supplier

   Property Name

   Property Address

Now, let generate one class file in C# for each entity in model. This is the GenerateClasses.cmd:

AjGenesis.Console Project.xml GenerateClasses.ajg

This command loads Project.xml in memory, and execute the AjGenesis task:

for each Entity in Project.Model.Entities
  TransformerManager.Transform("EntityClass.tpl", "${Entity.Name}.cs", Environment)
end for

There are few public helper objects, predefined in AjGenesis environment. One of them is TransformerManager, that takes three arguments:

- The template file name

- The name of the file to generate

- The current Environment (name/values of global variables and functions, usually pointed by Environment global variable)

This is the first template in this tutorial series, EntityClass.tpl:

// Entity Class, generated with AjGenesis (http://ajgenesis.codeplex.com)
public class ${Entity.Name}
{
<#
  for each Property in Entity.Properties
#>
  public string ${Property.Name} { get; set; }
<#
  end for
#>
}

A template is a text file with the content to generate, and with fragments between <# and #> that are filled with AjBasic commands. In the above template, the for each command processes each of the properties in the current entity. As in the string expansion, expressions between ${ and } are evaluated and its results are inserted in the output of template process.

This is the output generated for Customer.cs using EntityClass.tpl template:

// Entity Class, generated with AjGenesis (http://ajgenesis.codeplex.com)
public class Customer
{
  public string Name { get; set; }
  public string Address { get; set; }
}

There are still some simplifications: no namespace, all properties are strings, no id for entities, etc… I will address these issues, step by step, in future posts. Next steps: generate, from the same model, C#, VB.NET or Java class files.

Keep tuned!

Angel “Java” Lopez

http://www.ajlopez.com

http://twitter.com/ajlopez

3 Comments »

  1. [...] Building An Application Using AjGenesis (Part 1) Building An Application Using AjGenesis (Part 2) [...]

    Pingback by Building an Application Using AjGenesis (Part 3) « Angel “Java” Lopez on Blog — July 12, 2010 @ 10:36 am

  2. [...] An Application Using AjGenesis (Part 1) Armando una Aplicación usando AjGenesis (Parte 1) Building An Application Using AjGenesis (Part 2) Armando una Aplicación usando AjGenesis (Parte [...]

    Pingback by Armando una aplicación usando AjGenesis (Parte 3) - Angel "Java" Lopez — July 12, 2010 @ 10:51 am

  3. [...] An Application Using AjGenesis (Part 1) Building An Application Using AjGenesis (Part 2) Building an Application Using AjGenesis (Part [...]

    Pingback by Building an Application Using AjGenesis (Part 4) « Angel “Java” Lopez on Blog — July 14, 2010 @ 7:28 pm


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

The Shocking Blue Green Theme. Create a free website or blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

Join 66 other followers

%d bloggers like this: