Angel \”Java\” Lopez on Blog

February 27, 2010

Models for Code Generation in AjGenesis

Filed under: AjGenesis, Code Generation, Software Development — ajlopez @ 12:45 pm

Today, I want to enumerate and short review the options you have to write a model in AjGenesis, my open source code generation project.

One of the basic ideas of AjGenesis is to have a free model as an starting point. It’s not a fixed one. What it means? It means that you can create and use your own model. A concrete example, if you want to model a "Hello, World”, you can start writing a simple model like this:

<Project Company="ajlopez.com">
   <Message>Hello, World</Message>
</Project>

If you want more info, extends it as in:

<Project>
	<Messages>
		<Message>Hello, World One</Message>
		<Message>Hello, World Two</Message>
		<Message>Hello, World Three</Message>
	</Messages>
</Project>

More info about this particular example in my post A Hello World Application in AjGenesis.

But XML is only the serialized version of the model. You can write it as text:

Project
	Messages
		Message = "Hello, World One"
		Message = "Hello, World Two"
		Message = "Hello, World Three"
	End Messages
End Project

More info about textual models at

Textual Model for Code Generation in AjGenesis

If you want to describe an Entity in your domain model, you can write something like this in free XML:

<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>
<Entity>
	<Name>Employee</Name>
	<Description>Employee</Description>
	<SetName>Employees</SetName>
	<Descriptor>Employee</Descriptor>
	<SetDescriptor>Employees</SetDescriptor>
	<SqlTable>employees</SqlTable>
	<Properties>
		<Property>
			<Name>Id</Name>
			<Description>Id</Description>
			<Type>Id</Type>
			<SqlType>int</SqlType>
		</Property>
		<Property>
			<Name>EmployeeCode</Name>
			<Description>Code</Description>
			<Type>Text</Type>
			<SqlType>varchar(20)</SqlType>
		</Property>
		<Property>
			<Name>LastName</Name>
			<Description>Last Name</Description>
			<Type>Text</Type>
			<SqlType>varchar(100)</SqlType>
		</Property>
		<Property>
			<Name>FirstName</Name>
			<Description>First Name</Description>
			<Type>Text</Type>
			<SqlType>varchar(100)</SqlType>
		</Property>
		<Property>
			<Name>IdDepartment</Name>
			<Description>Department</Description>
			<Type>IdRef</Type>
			<SqlType>int</SqlType>
			<Reference>Department</Reference>
		</Property>
	</Properties>
</Entity>

A more detailed description of use of such entity model at Application Generation using AjGenesis.

But if read XML hurts your eyes, you can use a in free text form (same as above example, but abbreviated)

Entity Employee
    Description = "Employee"
    SetName = "Employees"
    ...
    Properties
        Property Id
            ...
        End Property
            ...
        Property Name
        End Property
    End Properties
End Entity

 

Not yet implemented, I’m going to add spreedsheets as models:

I wrote a short post about this idea: Another Model for AjGenesis

But sometimes, you want to use an existing source for model metadata. And with AjGenesis, you can! One of the most “popular” starting pseudo-models is the database. In my opinion, it’s not the ideal model, but, depending on the project and your needs, it could be a pragmatic solution (remember, AjGenesis and code generation is a pragmactic solution to some problems in software development, so, I don’t hesitate to adopt pragmatic sources, but be aware of their limitations).

I wrote about some examples at: Generating a Model From the Database Using AjGenesis and AjGenesis: Generating the Model from the Database

AjGenesis was born and designed to survive “fashion development”: it’s not oriented to a particular technology, platform, framework or language. It can generate everything that is a text artifact: Java, C#, VB.NET, PHP, config files, xml, database scripts, even COBOL. You can write tasks and templates to do what you want to generate, at any moment. You can start small, and grow up, adding more abstractions and tricks. But tasks and templates are topics for another blog. Said that, you can use as a model the compiled domain. There is a description of an example written by Dario Quintana using .NET at:

AjGenesis: Generating the model from assemblies

In Java world, it was used to generate code from annotations in code (remember XDocLet). Or from configuration files: hbm2java tools in Hibernate. NHibernate, the most popular .NET open source Object Relational Mapping library, lacks such tools. As in Java, AjGenesis can use meta tags embedded in .hbm mapping files, to generate code. See:

Generating Code with AjGenesis using NHibernate Hbm Mapping Files

(I have a more complete example in trunk, that generates a complete solution using ASP.NET and NHibernate).

You can download the project from the source code trunk at:

http://www.codeplex.com/ajlopez

Next topics: tasks, templates, abstractions vs technicalities, in-memory models, model to model transformations, completing the model, decisions, expert systems and artificial intelligence applied to code generation and AjGenesis.

Stay tuned!

Angel "Java" Lopez

http://www.ajlopez.com

http://twitter.com/ajlopez

6 Comments »

  1. [...] AjGenesis – Generating the model from the database Generating a Model from the Database using AjGenesis Models for Code Generation in AjGenesis [...]

    Pingback by Code generation with AjGenesis in a Real Project « Angel “Java” Lopez on Blog — May 27, 2010 @ 12:50 pm

  2. [...] wrote a simple Project.txt (note that I’m using the textual model instead the XML one, see Models for AjGenesis and Textual model for Code Generation in AjGenesis [...]

    Pingback by AjGenesis and Language Workbench Competition 2011 « Angel “Java” Lopez on Blog — March 8, 2011 @ 5:34 pm

  3. [...] AjGenesis posts). The models can be written using XML or text. Or you can use your own models (see Models for Code Generation in AjGenesis). The text artifacts could be Java, C#, VB.NET, Ruby, whatever you want. Notably: my AjGenesis [...]

    Pingback by Code Generation and Artificial Intelligence « Angel “Java” Lopez on Blog — May 25, 2011 @ 4:24 pm

  4. [...] Los modelos se pueden escribir en XML o en texto. O podemos usar nuestros propios modelos (ver Models for Code Generation in AjGenesis, Modelos para Generación de Código en AjGenesis). Los artefactos de texto pueden ser Java, C#, [...]

    Pingback by Generación de Código e Inteligencia Artificial - Angel "Java" Lopez — May 31, 2011 @ 10:28 am

  5. [...] López already defended the idea that the format of input models should be flexible and textual models are becoming more and more popular every day but models in excel?? It´s [...]

    Pingback by Models in Excel | Modeling Languages — November 9, 2011 @ 12:40 pm

  6. [...] Building an Application with AjGenesis Code Generation with AjGenesis in a Real Project Models for Code Generation in AjGenesis AjBasic: An Open Source Basic-alike Interpreter Generating Code Hello World with [...]

    Pingback by AjGenesis: Basis of its Implementation « Angel “Java” Lopez on Blog — November 18, 2011 @ 12:07 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. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

Join 65 other followers

%d bloggers like this: