02.06.2013 Views

MAFCOG 1.0A Generic code generator for Mathematical Functions

MAFCOG 1.0A Generic code generator for Mathematical Functions

MAFCOG 1.0A Generic code generator for Mathematical Functions

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

-<br />

r-<br />

rr-<br />

r<br />

r<br />

r:<br />

r<br />

r:<br />

-r:<br />

r<br />

-<br />

"I"<br />

-<br />

-"<br />

r:<br />

r-<br />

•',/i-<br />

Proceedings oflhe National Conference on<br />

Compuling and Applications (NACCA '06)<br />

Nauonal Engineering College. Kovilpattt, Tamil-Nadu<br />

<strong>MAFCOG</strong> 1.0 A <strong>Generic</strong> <strong>code</strong> <strong>generator</strong> <strong>for</strong> <strong>Mathematical</strong><br />

<strong>Functions</strong><br />

Mrs. S. R.ldate 1 , Prof. S. H. Patil l , Mr. D. J. Male ,Mrs. M. M. Bewoor"<br />

'lecturer In Computer Department, B. V.D.U. CO.E. Pune, India.<br />

lprofessor In Computer Department, B. V.D. U. Co.E. Pune, India<br />

Jsoftware Engineer In IBM Global Services India Pvt. Ltd. Pune, India.<br />

4 "<br />

lecturer In Computer Department, B. V.D. U. Co.E. Pune, India<br />

Abstract<br />

<strong>MAFCOG</strong> 1.0 (MAthematical Formula Analyzer and COde Generator) is a flexible, generic<br />

<strong>code</strong> <strong>generator</strong> <strong>for</strong> java and other programming languages. A <strong>MAFCOG</strong> 1.0 user can define<br />

the mathematical functions by <strong>for</strong>mulating them using the inbuilt functions of <strong>MAFCOG</strong>.<br />

"Alternatively, the user can use the defined <strong>for</strong>mulas in the <strong>for</strong>m of images. Thus <strong>MAFCOG</strong><br />

1.0 will use the <strong>for</strong>mula, generate the <strong>code</strong> in the required language, and validates the same<br />

<strong>for</strong> user inputs. User can use this <strong>code</strong> as plug-in or as a snippet as per the requirement. Using<br />

JAVACC, Java Swing, and XML will develop the system.<br />

1.0 RELEVANCE TO GENERATIVE<br />

PROGRAMML~G<br />

The goal of generative and componentbased<br />

software engineering is to increase<br />

the productivity, quality, and time-tomarket<br />

in software development thanks to<br />

the deployment of both standard<br />

componentry and production automation.<br />

One important paradigm shift implied here<br />

is to build software systems from standard<br />

componentry rather than "reinventing the<br />

wheel" each time. This requires thinking in<br />

terms of system families rather than single<br />

systems. Another important paradigm shift<br />

is to replace manual search, adaptation, and<br />

assembly of components with the automatic<br />

generation of needed components on<br />

demand. Generative and component-based<br />

software engineering seeks to integrate<br />

domain engineering approaches,<br />

"<br />

produces a non-atomic, extended<br />

computation (i.e., iteration or recursion)<br />

361<br />

component-based approaches, and<br />

generative approaches. [REF 5]<br />

1.1 Problem<br />

The fundamental contribution of high-level<br />

languages to programming productivity was<br />

the ability of their compilers to trans<strong>for</strong>m a<br />

compact representation of a mathematical<br />

computation (i.e., an arithmetic expression<br />

expressed in a mature notation borrowed<br />

from mathematics) into an equivalent but<br />

expansive and complexly interrelated series<br />

of assembly language instructions. This<br />

saves the programmer a significant amount<br />

of work because the mathematical<br />

expression is that are already inherently<br />

composite. Thus, every operation on such<br />

abstractions<br />

because the operands are extended,<br />

composite structures. And to compound the<br />

,\<br />

I'<br />

I<br />

\


..<br />

,,-<br />

r-<br />

r-<br />

r:<br />

r:<br />

"...<br />

i<br />

MA FCOG 1.0 A <strong>Generic</strong> CCNk Generator For <strong>Mathematical</strong> <strong>Functions</strong><br />

problem; the ideal use of such abstract,<br />

domain specific operators and operands in<br />

expressions that compose many such<br />

operations together. [2]<br />

Even w-nen developers have recurring<br />

computer-readable metadata to process and<br />

a clear idea of how <strong>code</strong> should by<br />

structured, they can still find themselves in<br />

need of a technique to automatically<br />

generate me <strong>code</strong> <strong>for</strong> the mathematical<br />

<strong>for</strong>mula :.J avoid the drudge work of<br />

repeatedly writing and tweaking it.<br />

Generative programming is a technique that<br />

addresses this problem. Using generative<br />

programming techniques, any developer can<br />

solve matcernatical problems in families,<br />

rather than individually, saving time and<br />

coding ef<strong>for</strong>t. This article describes these<br />

techniques using <strong>MAFCOG</strong> 1.0, and builds<br />

a sample template-driven <strong>code</strong> <strong>generator</strong> <strong>for</strong><br />

mathernaticz l expression. [ 4]<br />

Applying generative programming produces<br />

implementation <strong>code</strong> from three things:<br />

• A Deans of specifying family<br />

members,<br />

• The implementation components<br />

from which members can be<br />

assern r-led, the configuration<br />

knowledge mapping between a<br />

specification of a member and a<br />

finished member. In other words,<br />

generative programming builds<br />

implerr. entations from the <strong>code</strong><br />

patterns and lower-level<br />

components, and the metadata that<br />

brings r::e two together. [1,3]<br />

•<br />

MAFCt)G 1.0 is a tool provided to<br />

the user to generate the <strong>code</strong> <strong>for</strong> the<br />

specifiec language.<br />

1.2. Tool Description<br />

Code Generators are programs that<br />

automatically generate high-level<br />

<strong>code</strong>(Java, C, C-+ etc). These tools range<br />

.~\ l' . ., .<br />

362<br />

in size and complexity. There are two types<br />

of <strong>code</strong> <strong>generator</strong>s: active and passive.<br />

Active model maintain the <strong>code</strong> generated<br />

by the <strong>generator</strong> long term. Passive models<br />

create the initial <strong>code</strong>, and then it is the<br />

responsibility of the engineer to maintain it.<br />

A typical example of a passive <strong>code</strong><br />

<strong>generator</strong> is a wizard.<br />

<strong>MAFCOG</strong> 1.0 will be the ideal tool<br />

<strong>for</strong> professionals who need quick solution to<br />

. technical problems. If user knows the<br />

mathematical <strong>for</strong>mula and the plat<strong>for</strong>m in<br />

which the <strong>for</strong>mula solution required to be<br />

developed (Java, C++ etc) <strong>MAFCOG</strong> 1.0<br />

lets him to add evaluation of mathematical<br />

expression to his/her application easily. It<br />

should support 7 operators (including<br />

Parenthesis), 10 mathematical functions,<br />

10-15 users defined variables and provides<br />

complete error handling support.<br />

<strong>MAFCOG</strong> 1.0 is a template driven<br />

<strong>code</strong> generation product that streamlines the<br />

application development process and.<br />

slashes development time.· Using<br />

breakthrough, template-driven technology<br />

2. 0 OVERVIEW<br />

<strong>MAFCOG</strong> 1.0 will help Java developers<br />

create sophisticated <strong>code</strong> <strong>for</strong> mathematical<br />

functions faster, more efficiently and more<br />

economically. Code templates provided are<br />

Developer modifiable.<br />

The benefits provided by <strong>MAFCOG</strong> 1.0<br />

approach include higher developer<br />

productivity, better software quality, and<br />

lower maintenance costs.<br />

2.1 Measurable Productivity Gains<br />

Generating <strong>code</strong> by definition is<br />

always going to be much faster than<br />

hand coding. The productivity gain<br />

when writing the mathematical <strong>code</strong> is<br />

at least 70%, with the coding part of the<br />

design, <strong>code</strong>, and test cycle eliminated.


2.~ Higher Quality:<br />

v ,<br />

'""<br />

Generated <strong>code</strong> is error free and well<br />

designed." Development teams are more<br />

consistent. Automatic <strong>code</strong> generation<br />

ensures that software developers are more<br />

likelv to be focused on the business<br />

requirements than the more mundane task<br />

of coding.<br />

2.3 Lower Maintenance Costs:<br />

.,'<br />

The <strong>code</strong> generated by <strong>MAFCOG</strong> 1.0 will<br />

be very easy to understand and there<strong>for</strong>e<br />

easier to maintain. The true long-term value<br />

of <strong>code</strong> generation emerges when there are<br />

new" business requirements. Ongoing<br />

maintenance costs are reduced because it is<br />

easier to regenerate <strong>code</strong> than to make<br />

manual <strong>code</strong> changes. There are two<br />

";:primary components used in <strong>MAFCOG</strong> 1.0<br />

• Formula Parsing engine<br />

• Code generation engine<br />

The Formula Parsing engine is a plat<strong>for</strong>m<br />

that hosts grammar files (called JJ files)<br />

with the help of which, actual <strong>for</strong>mula<br />

parsing can be done. There are a variety of<br />

grammar files that you can plug in to the<br />

engine to generate <strong>code</strong> a particular way.<br />

Code generation engine implements a<br />

template-based" approach that allows to<br />

specify the architectural aspects of the <strong>code</strong><br />

under construction. To do this, it provides a<br />

specific Meta Model.<br />

it comprises a set of rules <strong>for</strong> <strong>for</strong>mula <strong>code</strong><br />

generation on how to read <strong>for</strong>mula, splitting<br />

them into tokens, interpret them, get the<br />

rules from defined templates and extract<br />

in<strong>for</strong>mation from them to generate <strong>code</strong>.<br />

The meta model planner then matches parts<br />

and also provides the paths and directions<br />

needed when creating a new template.<br />

3.0 HO'V <strong>MAFCOG</strong> 1.0 GENERATES<br />

CODE?<br />

National Conference on Computing and Applications<br />

363<br />

The following describes the process used by<br />

<strong>MAFCOG</strong> 1,0 engine to generate <strong>code</strong>.<br />

Details of each step follow this section.<br />

Figl.1 :\1AFCOGl.O Processes<br />

3.1 Formula Parsing And Validation<br />

The <strong>for</strong>mula provided by the user wi 11 be<br />

parsed and validated against the defined<br />

grammar. The tokens from the <strong>for</strong>mula are<br />

split up into tokens and the in<strong>for</strong>mation is<br />

extracted to generate the <strong>code</strong>.<br />

3.2 Xt\fL Template Parsing And Rules<br />

Generation<br />

Formula Parsing engine will provide the<br />

input to XML parser, which is a ruleexpanding<br />

planner, implemented in Java.<br />

Rules have associated constraints that can<br />

refer to knowledge bases (templates) that<br />

are constructed from the input to the<br />

<strong>generator</strong>. the planner matches parts of this<br />

input to select appropriate templates (i.e.,<br />

partial derivation trees) and then parse the<br />

templates, generate rules and fills them with<br />

data from the input.<br />

. 3.3 Code Generation<br />

This is where the heavy lifting happens.<br />

This involves reading the XML rules,<br />

selecting the appropriate data; bind it into a<br />

proper <strong>for</strong>mat. More than one template can<br />

be used to generate <strong>code</strong> <strong>for</strong> any given<br />

class; the default templates may be used, or<br />

a customized version may be used,<br />

depending on configuration.<br />

4. 0 <strong>MAFCOG</strong> 1.0 ARCHITECTURE:<br />

As shown in Figure 1.2, <strong>MAFCOG</strong> 1.0<br />

typically contains three layers Formula<br />

j


:<br />

I<br />

1 "tOR~ ~.¥Y I' "S 't',:. .•••.•' " " " .. ',ni"' C~1"t''Yi:t.. xU ")"3 .<br />

}.fA FCOG <strong>1.0A</strong> <strong>Generic</strong> Code Generator For .~!:;!het1latical <strong>Functions</strong><br />

Parsing Layer, XML Parsing Layer, Code Expr: = Dumber<br />

Generation Layer as described below.<br />

4.1 Formula Parsing Layer<br />

expr '+' expr<br />

'expr '-' expr<br />

expr ,*, expr<br />

expr'Z'expr<br />

The <strong>for</strong>mula <strong>for</strong> which the <strong>code</strong> is<br />

required to be generated is parsed and<br />

validated against the defined grammar rules.<br />

number:= digit+('.' Digit+)?<br />

Digit.=tu'" 1'1'2'1'3';'4'1'5'1'6'1'7'1'8'1'9'<br />

[7]<br />

For parsing the <strong>for</strong>mula and populate the JavaCC: JavaCC available <strong>for</strong> free, is a<br />

data structures with the <strong>for</strong>mula tokens, the parser <strong>generator</strong> It provides a Java<br />

Grammar files and associated template<br />

files will be used and data is populated.<br />

The <strong>for</strong>mula parsing includes following<br />

analysis:<br />

4.1.1 Lexical Analysis:<br />

Language extension <strong>for</strong> specifying a<br />

programming language's grammar. JavaCC<br />

allows us to define grammar in a fashion<br />

similar tc EBNF, making it easy to translate<br />

EBNF grammar into the JavaCC <strong>for</strong>mat. [6]<br />

Lexical analysis takes a cursory look at the<br />

input data and divides .into" proper tokens.;<br />

Tokens examples in mathematical<br />

expression include. variable, symbols such<br />

as IT numbers etc.<br />

4.1.2 Syntactic analysis (Parsing):<br />

During Syntactic analysis, a parser extracts<br />

meaning from the program source <strong>code</strong> by<br />

ensuring the syntactical correctness of input<br />

data. During syntactic analysis a compiler<br />

examines the input with respect to the rules<br />

defined in the language's grammar. If any<br />

rule is violated, the compiler. displays an<br />

error message. A grammar rule should be<br />

specified unambiguously and in entirety<br />

with the EBNF (Extended Backus-Naur-<br />

Form). Following grammar rules <strong>for</strong> small<br />

language that describes basic. arithmetic<br />

expression:<br />

4.2. XlVILParsing Layer<br />

Tasks fer parsing specifications and<br />

generating <strong>code</strong> are executed In a<br />

straight<strong>for</strong>ward process presented in<br />

figure 1.3 :-elow. The source <strong>code</strong> <strong>generator</strong><br />

that uses :::e rules to select the appropriate<br />

data populated from templates and<br />

assembles :~em into source <strong>code</strong> and writes<br />

out the soi.-ce <strong>code</strong><br />

The XML parsing layer is broken out into<br />

following c ompcnents<br />

"," ,=' ." fit ",'


.<br />

r<br />

r -<br />

;;-<br />

"<br />

.r-<br />

r<br />

r<br />

r:<br />

,<br />

;.<br />

!'r<br />

~r<br />

• r<br />

j'<br />

I I<br />

I<br />

!<br />

~ I"'"<br />

,<br />

I"'"<br />

r:<br />

r:<br />

r<br />

----<br />

Formula<br />

D..r.nlUon<br />

XMI..Pars.ng I..ayer .<br />

XMt.. Templates<br />

, Parsing<br />

.S:<br />

Ruks<br />

.<br />

r<br />

- j~ ~.<br />

•••<br />

4.2.1 Formula PI1T.\'I!TEngine Out Put:<br />

The Data Structures containing the details<br />

of the <strong>for</strong>mula are extracted and provided as<br />

input to the X~tL reader and parser. The<br />

XML reader and parser will read templates<br />

accordingly <strong>for</strong> generating the <strong>code</strong>. The<br />

Out put from Formula Parser engine<br />

includes the detnil s such as "Target<br />

Language", Tokcns, Operands used,<br />

<strong>Mathematical</strong> operators (Power, Square,<br />

Square Root etc.)<br />

4.2.2 TII(~XML<br />

Parser:<br />

This' part generates two things:<br />

• A programm i:1g language<br />

.capable of complex logic;<br />

··1encra~~. c- .. -<br />

'... . . ':r,,!!\ m;.:1~~;I' .<br />

Proceedings of the National Conference on<br />

Computing and Applications (NACCA '06)<br />

National Engineering College. Kovtlpatti, Tamil Nadu<br />

.~rc.... I<br />

. .. , E'~~'~"':-i' ;<br />

-u: ....,.,<br />

C4de Genera ••on Layer<br />

Temporary templates will be dynamically<br />

created to store the <strong>code</strong> blocks <strong>for</strong> further<br />

use as explained above.<br />

',4.2.4 The logic module that generates<br />

rules: .<br />

After reading the XML templates in the<br />

XML documents and navigating through it,<br />

<strong>MAFCOG</strong> LO <strong>generator</strong> will pick up<br />

in<strong>for</strong>mation in different parts of the<br />

templates to select and assemble the<br />

templates into proper <strong>code</strong> thus generating<br />

the pseudo-<strong>code</strong> after reading the<br />

Document Reader And .. APPLICATION nude's target-language<br />

• An XML utiliry <strong>for</strong> the language ..<br />

For <strong>MAFCOG</strong> 1,0 the language to be used<br />

as "Java" and X~IL utility as "JDOM"_ The<br />

XML document reader-and parser will read<br />

out the Out put from Formula Parser engine.<br />

and parse the Xl\IL templates accordingly<br />

4.2.3 The template lihrary:<br />

The XML Templates. which will be read, are as<br />

• Mathkules<br />

• JavaDOC<br />

• Control Structures.<br />

CeneriJre :<br />

- :. attribute .The <strong>code</strong> blocks generated will be<br />

. stored .in temporary templates in template<br />

that is library <strong>for</strong> further use.<br />

TtmpIL~!..~<br />

XMLParSing L!'rer<br />

Fig 1.3 XML Parsing Layer<br />

4.3. Code Generation Layer<br />

Code Generation Layer<br />

Source Code Generator will read the XML<br />

rules, selecting the appropriate data<br />

structures populated from XML templates,<br />

inserting the proper variables into the<br />

365 ~I<br />

\<br />


I \<br />

MA FCOG 1.0 A <strong>Generic</strong> Code Generator For <strong>Mathematical</strong> <strong>Functions</strong><br />

templates and nesting the templates inside<br />

each other as needed.<br />

S.O USER INTERFACE DESIGN<br />

The user interface shows command<br />

hierarchy that defines following major<br />

system menu categories. The menu bar, the<br />

tool palette and all sub functions. The GUI<br />

contains the button hierarchy as: to enter the<br />

mathematical <strong>for</strong>mula, update; save and<br />

undo the changes. The tool bar contains the.<br />

, !Ii<br />

Figl.4 <strong>MAFCOG</strong> 1.0 GUI<br />

The rightmost window is used to initialize<br />

the variables in order to test the generated<br />

JAVA <strong>code</strong>. .<br />

6. USING <strong>MAFCOG</strong> 1.0 ,.<br />

~ The design of <strong>MAFCOG</strong> 1.0 is founded on<br />

,..,--.xibility. It can serve small organization<br />

-- intent on having quality. coding<br />

"~ctices consistently. implemented<br />

r- ·,••roughout the organization." Improved<br />

coding practices will be quickly propagated.<br />

,.... and will stay with the organization even<br />

if star programmers have left. Knowledge<br />

-- stays and accumulates with the<br />

,.. organization, neatly encapsulated in the<br />

template library.<br />

r- 7.0 CONCLUSION<br />

r- The premise of a <strong>code</strong> <strong>generator</strong> is that it<br />

pushes <strong>code</strong> re-use much further without<br />

~\366<br />

icons <strong>for</strong> <strong>code</strong> generation, edition, save, and<br />

test the developed <strong>code</strong>. The menu bar<br />

contains the operation as <strong>code</strong> generation,<br />

edition, save and test the developed <strong>code</strong>.<br />

Middle window is reserved to display the<br />

JAVA <strong>code</strong> 'generated <strong>for</strong> specified<br />

mathematical expression. Lower window is<br />

used to display messages, exceptions and<br />

evallHition of execution results<br />

contravening the OOP approach; the<br />

templates must follow proper OOP<br />

structure. We are trying to create better<br />

<strong>code</strong>, not more bad <strong>code</strong> faster. While<br />

having a class should allow <strong>for</strong> <strong>code</strong> that is<br />

more useful we still have to build the class<br />

that will do this. <strong>MAFCOG</strong> 1.0 will give us<br />

the ability to build an OOP class that has<br />

less error and follows coding standards that<br />

have been building over the years.<br />

8. REFERENCES<br />

[I] Generative Programming (http://wwwia.tu-ilmenau.de/-czam'generate/engl.html)<br />

[2] The xJen Architecture by Michel Katz<br />

(http://www.<strong>code</strong>generation.netltikiread_article.php?<br />

artic1eId=32)<br />

[3] Generative and Component-Based<br />

Software Engineering (http://www-ia.tuilmenau.de/-czarn/generate/engl.html)<br />

[4] Generative Programming: Modem<br />

Techniques to Automate Repetitive<br />

Programming Tasks: MSDN Magazine,<br />

December 200 I<br />

[5] Generative Programming - Methods,<br />

Tools, and Applications by Krzysztof<br />

Czarnecki and Ulrich W. Eisenecker.<br />

(http://www.generative-programming.org)<br />

[6] https://javacc.dev.java.net

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!