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
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!
Code Generation from a Model can be implemented as an Expert System, and then, it's a clear target for Artificial Intelligence
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.
Related posts (in Spanish):
Angel “Java” Lopez