Angel \”Java\” Lopez on Blog

May 25, 2011

Code Generation and Artificial Intelligence

In late seventies and early eighties (past century ;-)) I was learning a lot about programming, programming languages and computer science in general. They were the days of COBOL, BCPL, IBM/360 assembler languages, JCL, punched cards, Pick Operating Systems, Pick Basic, ALGOL/W, Fortran, PL/I, and C (in CP/M, DOS, Unix/Xenix world). One of my preferred topics (then and now) was Artificial Intelligence, a broad term. Those days were the golden age of expert systems. I was writing a lot of C code, then, as a lazy programmer, I started to write my own C utilities to generate more C programs ;-). Since then, I have an idea:

Code Generation should be an Artificial Intelligence application

In the late eighties, early nineties, the amount of code to write to have a professional application diminished (new IDEs, wizards). But that was a transitory state: the complexity imposed by market, new customer requirements, online application, concurrency, new technologies and languages (Windows, GUIs…) all conspired to the increase of effort that should be employed to create any non-trivial application. I embraced Java at mid-90s, and .NET at 2001. Large class libraries, new tips and tricks, architectural styles, patterns, JSP, JSF, MVC, ASP.NET, Javascript, some PHP, distributed computing, physical tiers… The software development arena became more complex. And now, we have mobile, Android, Windows Phone 7, ASP.NET MVC, new JDKs, JVMs, new dynamic languages… I could continue this enumeration for ever, no break here ;-)

So, at the begin of the century, I decided to start my open source project for code generation, AjGenesis. First, this is the current process I use in my code generation projects:

(Ok, I’m a developer, not a designer ;-))

The staring models (left) are free: you can use any model you want, it’s not fixed. This is a key decision: I want YOU have the freedom to start whit whatever you want as a base for your code generation. But it should be a MODEL. It’s not only code generation: it’s CODE GENERATION FROM A MODEL(S). Usually, I have an abstract model, like:

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

and one or more technology model, like:

<Technology>
	<Programming>
		<Dialect>CSharp4NhMvc</Dialect>
	</Programming>
	<Database>
		<Dialect>MsSql</Dialect>
		<Name>AjTestNh</Name>
		<Host>.\SQLEXPRESS</Host>
	</Database>
	<NHibernate>
		<Dialect>NHibernate.Dialect.MsSql2000Dialect</Dialect>
	</NHibernate>
</Technology>

(more info in my 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 examples started generating PHP, and JSP 1.x, and they evolved WITHOUT CHANGING THE TOOL to generate PHP (new versions), Java, JSP 2.x, Hibernate, NHibernate, ASP.NET 1.x, ASP.NET 2.x, ASP.NET MVC… It can be adapted to any text-based technology. To have such flexibility, it has no GUI IDE association. It's has Spartan interface, but I liked it.

Since inception, I adopted a general dynamic language, AjBasic, to support tasks and templates: I didn't want a transformation engine, from model to code via templates. I want to have the power of a GENERAL language. Why? TO SUPPORT EXTENSIBILITY, beyond template expansion. And, surprise! What kind of extensibility? Anyone, but you know…. yes, right! Artificial Intelligence!

My assertion:

Code Generation from a Model can be implemented as an Expert System, and then, it's a clear target for Artificial Intelligence

Graphically:

You can view software developers as experts. We can distill their expertise in expert systems and add them to code generation process. Some examples:

- An expert system can use a rule engine to make decisions: macro or mini ones. Mini-one: “This web report can have lot of rows, I need pagination, filtering, and ordering….” And then, some part of the expert systems recommends: “Yes! We could use JQuery and JQuery.UI, client-side filtering”… and other part (yes, my expert systems is multi-mind ;-)) says “No! We need server-side filtering, then we need such queries and services, and Ajax endpoints and … “ Got the picture?

A micro-decision I employed last year in a real project: one kind of entity, in an abstract model, was marked as “massive” meaning: “we have lot of instances of Entity X”. Then, when in the UI we needed to select one Entity X, instead of a dropdown, the code generation engine put a search box, with auto-complete, with Ajax service at server.

I could use, for the abstract model, some base ontology and make assumptions and decisions. Example:

- Every entity could be: a person, a company, a place, a physical good, an event, a document. Then, the expert system could infer: a place has an address. A physical good should be located in a place. A document X should be needed to represent the move of a physical good from place A to place B. A person can be an employee. Every employee has social security… etc…. etc.. Then, we need a report of the X events, ordered by date, grouped by location, etc… etc… This document has a money total, it should be interesting to have a total grouped by any party (customer? company? person?) involved in the document. And by month? Yes, we need a pie chart … and so on… ;-)

I could add a rule engine, to make some of these kind of decisions, with rules like “if entity X is massive and the UI is web and JQuery is an available option then … “. Or rules like: “if entity X inherits from entity Y and we need persistence, we adopt table-per-hierarchy (or table-per-concrete class) ORM strategy, and then we generate…. database tables and …. ORM mapping code or configuration”. Or: “this entity X is a document having a date, we add an index on that date/time column in Y table”. So many decisions can be made in this way. Actually, in AjGenesis, most of these decisions are in the tasks and technology models.

In the long term, code generation from a model involves:

- This is the starting point: abstract models, technical models (restrictions: “we need all this in ASP.NET MVC 3”)

- This is the expected deliverable (a partial working system)

- And these are the distilled expert knowledge we got from our/others experience.  And with all these, we can go from start to end. using the accumulated programming skills, ORM configuration, dependency injection libraries, UI tips and tricks, recurrent patterns, database creation knowledge, etc..

Software development and architecture is related to:

- Know what you need

- Know what are the problems to resolve (persistence, concurrency, security, traceability, UI access, etc…)

- Know what are the available solutions (database, NoSql, language, class libraries, frameworks, UI technology, patterns…)

Code generation from a model has a (great, IMO) benefit: it put clear the existence, separation and relations of the above points.

Other way to explore: the use of expert agents. What does “expert agent” mean in my jargon? My vision: you can have modules, agents that can be added, removed without much effort, that can resolves problems. Then, someone can develop a JSF expert, that knows what JavaBeans to define for implement a given interface in a use case. Then, if in the future the rule engine or the initial restrictions say: “Now, we need JSF 17.2, or ASP.NET MVC 6.5 or the new SuperDuperOpenSourceUIThatIsTheNextBigThingHere”, we can adopt new solutions for the similar problems, distilling the new knowledge and putting it in a new expert agent.

Ok, all these are the reasons for an (upcoming?) AjRules ;-). And the justification for AjLisp (see here) and some AjProlog to be updated (see it inside AjCodeKatas).

Related posts (in Spanish):

Models, decisions, Artificial Intelligence and AjGenesis

About Code Generation

Keep tuned!

Angel “Java” Lopez

http://www.ajlopez.com

http://twitter.com/ajlopez

3 Comments »

  1. very cool and interesting post man!

    Comment by machadogj — May 25, 2011 @ 5:23 pm

  2. Si el primer adelantado fue Don Pedro de Mendoza, usté es el primer iluminado, Maestro!

    Comment by Martin Salias — May 26, 2011 @ 11:46 pm

  3. I’m gone to tell my little brother, that he should also visit this webpage on regular
    basis to obtain updated from most up-to-date reports.

    Comment by EInvite Coupon Code — May 27, 2014 @ 9: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. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

Join 66 other followers

%d bloggers like this: