Friday, January 16, 2015

MAFCOG 1.0 : A Generic code generator for Mathematical Functions


MAFCOG 1.0 : A Generic code generator for Mathematical Functions

Abstract

MAFCOG 1.0 (MAthematical Formula Analyzer and COde Generator) is a flexible, generic code generator for java and other programming languages. A MAFCOG 1.0 user can define the mathematical functions by formulating them using the inbuilt functions of MAFCOG. Alternatively, the user can use the defined formulas in the form of images. Thus MAFCOG 1.0 will use the formula, generate the code in the required language, and validates the same for user inputs. User can use this code as plug-in or as a snippet as per the requirement. Using JAVACC, Java Swing, and XML will develop the system.


1.0  RELEVANCE TO GENERATIVE   PROGRAMMING
The goal of generative and component-based software engineering is to increase the productivity, quality, and time-to-market in software development thanks to the deployment of both standard componentry and production automation.
One important paradigm shift implied here is to build software systems from standard componentry rather than "reinventing the wheel" each time. This requires thinking in terms of system families rather than single systems. Another important paradigm shift is to replace manual search, adaptation, and assembly of components with the automatic generation of needed components on demand. Generative and component-based software engineering seeks to integrate domain engineering approaches, component-based approaches, and generative approaches. [REF 5]
1.1  Problem
The fundamental contribution of high-level languages to programming productivity was the ability of their compilers to transform a compact representation of a mathematical computation (i.e., an arithmetic expression expressed in a mature notation borrowed from mathematics) into an equivalent but expansive and complexly interrelated series of assembly language instructions. This saves the programmer a significant amount of work because the mathematical expression is that are already inherently composite. Thus, every operation on such abstractions produces a non-atomic, extended computation (i.e., iteration or recursion) because the operands are extended, composite structures. And to compound the problem; the ideal use of such abstract, domain specific operators and operands in expressions that compose many such operations together. [2]
Even when developers have recurring computer-readable metadata to process and a clear idea of how code should be structured, they can still find themselves in need of a technique to automatically generate the code for the mathematical formula to avoid the drudge work of repeatedly writing and tweaking it. Generative programming is a technique that addresses this problem. Using generative programming techniques, any developer can solve mathematical problems in families, rather than individually, saving time and coding effort. This article describes these techniques using MAFCOG 1.0, and builds a sample template-driven code generator for mathematical expression. [ 4]
Applying generative programming produces implementation code from three things:
§  A means of specifying family members,
The implementation components from which members can be assembled, the configuration knowledge mapping between a specification of a member and a finished member. In other words, generative programming builds implementations from the code patterns and lower-level components, and the metadata that §  MAFCOG 1.0 is a tool provided to the user to generate the code for the specified language.
1.2. Tool Description
Code Generators are programs that automatically generate high-level code (Java, C, C++ etc). These tools range in size and complexity. There are two types of code generators: active and passive. Active model maintain the code generated by the generator long term. Passive models create the initial code, and then it is the responsibility of the engineer to maintain it. A typical example of a passive code generator is a wizard.
MAFCOG 1.0 will be the ideal tool for professionals who need quick solution to technical problems. If user knows the mathematical formula and the platform in which the formula solution required to be developed (Java, C++ etc) MAFCOG 1.0 lets him to add evaluation of mathematical expression to his/her application easily. It should support 7 operators (including Parenthesis), 10 mathematical functions, 10-15 users defined variables and provides complete error handling support.
MAFCOG 1.0 is a template driven code generation product that streamlines the application development process and slashes development time. Using breakthrough, template-driven technology.
2. 0 OVERVIEW
MAFCOG 1.0 will help Java developers create sophisticated code for mathematical functions faster, more efficiently and more economically. Code templates provided are Developer modifiable.
The benefits provided by MAFCOG 1.0 approach include higher developer productivity, better software quality, and lower maintenance costs.

2.1 Measurable Productivity Gains:
Generating code by definition is always going to be much faster than hand coding. The productivity gain when writing the mathematical code is at least 70%, with the coding part of the design, code, and test cycle eliminated.
2.2 Higher Quality:
Generated code is error free and well designed. Development teams are more consistent. Automatic code generation ensures that software developers are more likely to be focused on the business requirements than the more mundane task of coding.
2.3 Lower Maintenance Costs:
The code generated by MAFCOG 1.0 will be very easy to understand and therefore easier to maintain. The true long-term value of code generation emerges when there are new business requirements. Ongoing maintenance costs are reduced because it is easier to regenerate code than to make manual code changes. There are two primary components used in MAFCOG 1.0
  • Formula Parsing engine
  • Code generation engine
The Formula Parsing engine is a platform that hosts grammar files (called JJ files) with the help of which, actual formula parsing can be done. There are a variety of grammar files that you can plug in to the engine to generate code a particular way.  Code generation engine implements a template-based approach that allows to specify the architectural aspects of the code under construction. To do this, it provides a specific Meta Model.
It comprises a set of rules for formula code generation on how to read formula, splitting them into tokens, interpret them, get the rules from defined templates and extract information from them to generate code. The meta model planner then matches parts and also provides the paths and directions needed when creating a new template.
3.0 HOW MAFCOG 1.0 GENERATES CODE?
The following diagram describes the process used by MAFCOG 1.0 engine to generate code.  Details of each step follow this section.
Fig1.1 MAFCOG1.0 Processes

3.1 Formula Parsing And Validation
 The formula provided by the user will be parsed and validated against the defined grammar. The tokens from the formula are split up into tokens and the information is extracted to generate the code.
 3.2 XML Template Parsing And Rules Generation
Formula Parsing engine will provide the input to XML parser, which is a rule-expanding planner, implemented in Java. Rules have associated constraints that can refer to knowledge bases (templates) that are constructed from the input to the generator. The planner matches parts of this input to select appropriate templates (i.e., partial derivation trees) and then parse the templates, generate rules and fills them with data from the input.
3.3 Code Generation
This is where the heavy lifting happens. This involves reading the XML rules, selecting the appropriate data; bind it into a proper format. More than one template can be used to generate code for any given class; the default templates may be used, or a customized version may be used, depending on configuration.
4. 0 MAFCOG 1.0 ARCHITECTURE:
As shown in Figure 1.2, MAFCOG 1.0 typically contains three layers Formula Parsing Layer, XML Parsing Layer, Code Generation Layer as described below.
Figure 1.2 MAFCOG 1.0 Architecture
4.1 Formula Parsing Layer
The formula for which the code is required to be generated is parsed and validated against the defined grammar rules. For parsing the formula and populate the data structures with the formula tokens, the Grammar files and associated template files will be used and data is populated.
The formula parsing includes following analysis:
4.1.1 Lexical Analysis:
Lexical analysis takes a cursory look at the input data and divides into proper tokens. Tokens examples in mathematical expression include variable, symbols such as P numbers etc.
4.1.2 Syntactic analysis (Parsing):
During Syntactic analysis, a parser extracts meaning from the program source code by ensuring the syntactical correctness of input data. During syntactic analysis a compiler examines the input with respect to the rules defined in the language’s grammar. If any rule is violated, the compiler displays an error message. A grammar rule should be specified unambiguously and in entirety with the EBNF (Extended Backus-Naur-Form).
Following grammar rules for small language that describes basic arithmetic expression:
Following grammar rules for small language that describes basic arithmetic expression:
Expr: = number
      |expr ‘+’ expr
        |expr ‘-’ expr
      |expr ‘*’ expr
       |expr‘/’expr
            |number:= digit+(‘.’ Digit+)?
Digit:=’0’|’1’|’2’|’3’|’4’|’5’|’6’|’7’|’8’|’9’  [7]
JavaCC: JavaCC available for free, is a parser generator It provides a Java Language extension for specifying a programming language’s grammar. JavaCC allows us to define grammar in a fashion similar to EBNF, making it easy to translate EBNF grammar into the JavaCC format. [6]

4.2. XML Parsing Layer

Tasks for parsing specifications and generating code are executed in a straightforward process presented in figure1.3 below. The source code generator that uses the rules to select the appropriate data populated from templates and assembles them into source code and writes out the source code
The XML parsing layer is broken out into following components
4.2.1 Formula Parser Engine Out Put:
The Data Structures containing the details of the formula are extracted and provided as input to the XML reader and parser. The XML reader and parser will read templates accordingly for generating the code. The Out put from Formula Parser engine includes the details such as “Target Language”, Tokens, Operands used, Mathematical operators (Power, Square, Square Root etc.)
4.2.2 The XML Document Reader And Parser:
This part generates two things:
  • A programming language that is capable of complex logic;
  • An XML utility for the language.
For MAFCOG 1.0 the language to be used as “Java” and XML utility as “JDOM”. The XML document reader and parser will read out the Out put from Formula Parser engine and parse the XML templates accordingly
4.2.3 The template library:
The XML Templates, which will be read, are as  
§ MathRules
§ JavaDOC 
§ Control Structures.
Temporary templates will be dynamically created to store the code blocks for further use as explained above.
4.2.4 The logic module that generates rules:
After reading the XML templates in the XML documents and navigating through it, MAFCOG 1.0 generator will pick up information in different parts of the templates to select and assemble the templates into proper code thus generating the pseudo-code after reading the APPLICATION node’s target-language attribute .The code blocks generated will be stored in temporary templates in template library for further use.

Fig1.3 XML Parsing Layer

Code Generation Layer
Source Code Generator will read the XML rules, selecting the appropriate data structures populated from XML templates, inserting the proper variables into the templates and nesting the templates inside each other as needed.
5.0 USER INTERFACE DESIGN
The user interface shows command hierarchy that defines following major system menu categories. The menu bar, the tool palette and all sub functions. The GUI contains the button hierarchy as: to enter the mathematical formula, update, save and undo the changes. The tool bar contains the icons for code generation, edition, save, and test the developed code. The menu bar contains the operation as code generation, edition, save, and test the developed code. Middle window is reserved to display the JAVA code generated for specified mathematical expression. Lower window is used to display messages, exceptions, and evaluation of execution results.

Fig1.4  MAFCOG 1.0 GUI


















6. USING MAFCOG 1.0 
The design of MAFCOG 1.0 is founded on flexibility. It can serve small organization intent on having quality coding practices consistently implemented  throughout the organization. Improved coding practices will be quickly propagated and will stay with the organization even if star programmers have left. Knowledge stays and accumulates with the organization, neatly encapsulated in the template library.
7.0 CONCLUSION
The premise of a code generator is that it pushes code re-use much further without contravening the OOP approach; the templates must follow proper OOP structure. We are trying to create better code, not more bad code faster. While having a class should allow for code that is more useful we still have to build the class that will do this. MAFCOG 1.0 will give us the ability to build an OOP class that has less error and follows coding standards that have been build over the years.

8. REFERENCES
[2] The xJen Architecture by Michel Katz (http://www.codegeneration.net/tiki-read_article.php? articleId=32)
[3] Generative and Component-Based Software Engineering (http://www-ia.tu-ilmenau.de/~czarn/generate/engl.html)

[4] Generative Programming: Modern Techniques to Automate Repetitive Programming Tasks: MSDN Magazine, December 2001

[5] Generative Programming - Methods, Tools, and Applications by Krzysztof Czarnecki and Ulrich W. Eisenecker  (http://www.generative-programming.org)
[6] https://javacc.dev.java.net
[7] Aho Ulman Sethi “Principals of compiler construction”.

No comments:

Post a Comment