Angel \”Java\” Lopez on Blog

March 14, 2011

More Language Workbench Competition 2011 with AjGenesis

Filed under: AjGenesis, Code Generation, Lwc2011 — ajlopez @ 9:03 am

In my previous post I described my initial work for Language Workbench Competition 2011, using AjGenesis. Now, it’s time to write about some new tasks I wrote. You can read the tasks of the competition at:

http://www.languageworkbenches.net/LWCTask-1.0.pdf

First, I skipped Phase 0.3 (constraint checks such as name-uniqueness). I plan to complete it, but not now. I wrote Phase 0.4:

0.4 Show how to break down a (large) model into several parts, while still cross-referencing between the parts. For example, put the Car and Person entities into different files, while still having the Person -> Car reference work.

The code was committed to AjGenesis repository:

http://ajgenesis.codeplex.com/SourceControl/list/changesets

under examples/Lcw2011

This step was an easy one. The Project.txt is:

Project Lwc2011
	Entities
		Entity Person Source="Entities\Person.txt"	
		Entity Car Source="Entities\Car.txt"
	End Entities
End Project

 

The Source attribute is special in AjGenesis model loading: it indicates a separate file to load in order to complete the object. I added this feature in the early times of AjGenesis: I want to have model files that are manageable, that don’t hurt the eyes ;-)

This is Person.txt:

Entity Person
	Properties
		Property Name Type="Text"
		Property FirstName Type="Text"
		Property BirthDate Type="Date"
		Property OwnedCar Type="Car"
	End Properties
End Entity

I began to write Phase 1.0 first step:

1.1 Show the integration of several languages. Define a second language to define instances of the entities, including assignment of values to the properties. This should ideally really be a second language that integrates with the first one, not just "more syntax" in the same

grammar. We want to showcase language modularity and composition here.

This is the new model:

Project Lwc2011
	Description = "Lwc"
	Entities
		Entity Person Source="Entities\Person.txt"	
		Entity Car Source="Entities\Car.txt"
	End Entities
	
	Instances
		Instance p 
			Type = "Person"
			Properties
				Property Name Value = "Voelter"
				Property FirstName Value = "Markus"
				Property BirthDay Value = "14/02/1927"
				Property OwnedCar Value = "c"
			End Properties
		End Instance
		
		Instance c 
			Type = "Car"
			Properties
				Property Make Value = "VW"
				Property Model Value = "Touran"
			End Properties
		End Instance
	End Instances
End Project

But it's still work in progress. I could put the instances in another independent model file (Instances.txt) or I could put the each instance in a separate file, referenced by the above one using Source attribute. I should complete the tasks checking the existence of each property in the entity type that correspond to each instance. AjGenesis doesn't support Domain-Specific Language definition: the above example is still a free model. You can add any property to any instance, and the current implementation doesn't check the proper use of the model. So, this kind of check is pending work: I will add the checking logic in the tasks to execute.

Apparently, this task doesn't involve code generation. In the next task, I should check the values of the properties, too.

Keep tuned!

Angel “Java” Lopez

http://www.ajlopez.com

http://twitter.com/ajlopez

March 8, 2011

AjGenesis and Language Workbench Competition 2011

Filed under: AjGenesis, Code Generation, Lwc2011 — ajlopez @ 5:33 pm

Last week, thanks to a @pmolinam, I met

Language Workbench Competition 2011

Molina had sent me the info last year, but I forget the challenge. It is mentioned in InfoQ:

Language Workbench Competition 2011 Submissions

You can read the task list at:

http://www.languageworkbenches.net/LWCTask-1.0.pdf

I don’t sure if AjGenesis could qualify as a Language Workbench: it’s more oriented to be a free-model code generation tool. It has no IDE integration, but it is flexible and language-independent. It doesn’t produce any DSL: instead, it has a free model that can be use in many ways, to describe the concepts you have to model, without attached domain specific language. @pmolinam asked if I could complete the tasks using AjGenesis, my open source codegeneration tool. My answer: Yes!

Then, some messages appeared at twitter:

Today, I started to write Phase 0, points 0.1-0.2. The example is at AjGenesis trunk, at:

http://ajgenesis.codeplex.com/SourceControl/list/changesets

under examples/Lcw2011

The first point 0.1 is:

0.1 Simple (structural) DSL without any fancy expression language or such. Build a simple data definition language to define entities with properties. Properties have a name and a type. It should be possible to use primitive types for properties, as well as other Entities.

entity Person { 
    string name
    string firstname
    date bithdate
    Car ownedCar
}


entity Car { 
    string make
    string model
}

I 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 ).

Project Lwc2011
    Entities
	Entity Person
	    Properties
		Property Name Type="Text"
		Property FirstName Type="Text"
		Property BirthDate Type="Date"
		Property OwnedCar Type="Car"
	    End Properties
	End Entity
	
	Entity Car
	    Properties
		Property Make Type="Text"
		Property Model Type="Text"
	    End Properties
	End Entity
    End Entities
End Project

The Phase 0-0.2:

0.2 Code generation to GPL such as Java, C#, C++ or XML Generate Java Beans (or some equivalent data structure in C#, Scala, etc.) with setters, getters and fields for the properties.

Using assets from my post Building An Application using AjGenesis (Part 4), I wrote the commands:

GenerateCSharp.cmd

GenerateJava.cmd

GenerateVbNet.cmd

I’m using additional model files for describing technology. This is my VbNet technology model:

<Technology>
	<Name>VbNet</Name>
</Technology>

Simple! This time, I'm using the XML model (in memory, textual and XML have the same features; it's only a serialization option).

Look at GenerateCSharp.cmd:

AjGenesis.Console Project\Project.txt Project\Technologies\CSharp.xml Tasks\Complete.ajg Tasks\Generate.ajg

It loads the Project.txt, the corresponding technology model, loads and executes the Complete.ajg task, and then loads and executes the Generate.ajg task. The Complete.task (see the mentioned post Building An Application using AjGenesis (Part 4)) it's used for complete the model: one thing it dows is to translate abstract types like “Text”, to the corresponding final language type, like “string” in C#, or “String” in Java.

The Generate.ajg takes the enriched model and generate the code for each entity, using templates. This is the template for C#:

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

The generated code for Person entity:

// Entity Class, generated with AjGenesis (http://ajgenesis.codeplex.com)
namespace Lwc2011 {
	public class Person
	{
		public string Name { get; set; }
		public string FirstName { get; set; }
		public DateTime BirthDate { get; set; }
		public Car OwnedCar { get; set; }
	}
}

Next steps: complete 0.3, 0.4 tasks, and then, solve Phase 1. I could complete the assignment in two weeks. I will publish the advance in upcoming posts.

Keep tuned!

Angel “Java” Lopez

http://www.ajlopez.com

http://twitter.com/ajlopez

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

Follow

Get every new post delivered to your Inbox.

Join 57 other followers