<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.expertiza.ncsu.edu/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Cyang14</id>
	<title>Expertiza_Wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.expertiza.ncsu.edu/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Cyang14"/>
	<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Special:Contributions/Cyang14"/>
	<updated>2026-06-03T03:22:42Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.41.0</generator>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E734&amp;diff=75288</id>
		<title>CSC/ECE 517 Spring 2013/OSS E734</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E734&amp;diff=75288"/>
		<updated>2013-04-29T03:07:38Z</updated>

		<summary type="html">&lt;p&gt;Cyang14: /* Introduction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Developers:&lt;br /&gt;
*Hao Liu (hliu11@ncsu.edu)&lt;br /&gt;
*Zhuowei Wang (zwang18@ncsu.edu)&lt;br /&gt;
*Chunxue Yang (cyang14@ncsu.edu)&lt;br /&gt;
&lt;br /&gt;
[https://docs.google.com/a/ncsu.edu/document/d/11YTjxFXFR13vJ769yFBbqn9qOueK2Ktz0Pd-gyS2W5U/edit# Write-up of This Topic.]&lt;br /&gt;
&lt;br /&gt;
This page is the design documentation for Expertiza project E734 conducted in the CSC/ECE 517 Spring 2003 class at North Carolina State University.  Learn more about Expertiza by visiting its main &lt;br /&gt;
page:  http://wikis.lib.ncsu.edu/index.php/Expertiza&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
The Expertiza project is system for using peer review to create reusable learning objects. Students do different assignments; then peer review selects the best work in each category, and assembles it to create a single unit.&lt;br /&gt;
&lt;br /&gt;
The process of reviewing includes reviews, re-reviews and meta reviews of assignments submitted by students for a course. This method of evaluation also inherently suggests that there will be vagaries in the way reviews have been performed. &lt;br /&gt;
&lt;br /&gt;
This project aim to provide a way for the users to examine various type of statistic about the courses, assignments and reviews. &lt;br /&gt;
&lt;br /&gt;
== Objective ==&lt;br /&gt;
The original objective of the project is to extend the Analytic project E732 from previous semester to incorporate new features. &lt;br /&gt;
Upon examination the implementation of E732 we decided to start from scratch and create a developer friendly and easily extendable framework for providing analytic data and displaying analytic data.&lt;br /&gt;
&lt;br /&gt;
To fulfill our purpose of providing a better experience for Expertiza users, we have several different aspects of to focus on. &lt;br /&gt;
&lt;br /&gt;
The primary focus of this project is on reviews and the related aspects that go hand in hand with reviews like meta reviews, re-reviews etc.  To be more specific, we will be working on an advanced search interface in which different criteria can be selected more flexibly to make a comparison for each review or meta review.&lt;br /&gt;
&lt;br /&gt;
Moreover, we will dedicate in developing a more dynamic and clear chart graph.  In our project, JavaScript libraries such as jQuery.js (GPL), highchart.js(CC by 3.0) are included to support the dynamic behaviors of the chart graph.  Multi chart areas are supported by Dom Tree.&lt;br /&gt;
&lt;br /&gt;
= Graphical User Interface (GUI) Design =&lt;br /&gt;
== Original Design ==&lt;br /&gt;
[[File:Original_analytic_gui.png]]&lt;br /&gt;
&lt;br /&gt;
In the original GUI design of analytic the user can select from a list of cryptically worded data comparison:&lt;br /&gt;
*Review: Question Text And Review Comment vs Assignment&lt;br /&gt;
*Review: Question Count And Review Comment vs Assignment&lt;br /&gt;
*Review: Sub Question Count And Review Comment vs Assignment&lt;br /&gt;
*Average Review Parameters And Student Experience vs Assignment&lt;br /&gt;
*Average Metareview Parameters And Student Experience vs Assignment&lt;br /&gt;
*Assignment Strategy And Review Parameter&lt;br /&gt;
&lt;br /&gt;
Besides the unfathomable meaning options, the design does not provide much room for the user of the page to explore and discover interesting correlations between data. Since the objective is to enable user to discover correlations between data, this implementation does not really meet the requirement.&lt;br /&gt;
&lt;br /&gt;
After selecting a graph type the user can add courses to the graph. Upon selection the all of the assignments from the course are added to the graph than the user can un-select the assignments from a drop down menu. Once a course is selected there's no way to remove a course from the selection.&lt;br /&gt;
&lt;br /&gt;
There are no option for selecting how the data are presented, data are displayed as a bar graph which restrict the type of data that can be displayed.&lt;br /&gt;
&lt;br /&gt;
The specific numeric value for the graph are displayed below the graph as a chart.&lt;br /&gt;
&lt;br /&gt;
== New Design ==&lt;br /&gt;
[[File:New_analytic_gui.png]]&lt;br /&gt;
=== Graph Type and Comparison Scope Selection ===&lt;br /&gt;
When the user arrive at the analytic page they are prevented with two options: Graph Type and Comparison Scope.&lt;br /&gt;
&lt;br /&gt;
[[File:New_analytic_gui_1.png]]&lt;br /&gt;
&lt;br /&gt;
First, in the new design we would like to offer the user and developer more options for displaying the data, instead of the restriction of only using bar graph.&lt;br /&gt;
&lt;br /&gt;
Graph type let the user select what kind of graph do they want the data to be displayed currently there are four different options:&lt;br /&gt;
*Bar Graph - useful for displaying simple numeric comparisons (i.e: average scores, number of participants)&lt;br /&gt;
[[File:New_analytic_gui_bar.png]]&lt;br /&gt;
*Line Graph - useful for displaying comparison of distributions (i.e: grade distribution for assignments)&lt;br /&gt;
[[File:New_analytic_gui_line.png]]&lt;br /&gt;
*Scatter Plot - useful for displaying raw data points (i.e: grades for all of the student for assignments)&lt;br /&gt;
[[File:New_analytic_gui_scatter.png]]&lt;br /&gt;
*Pie Chart - useful for displaying percentage related data (i.e: percentage of grade distribution for an single assignment)&lt;br /&gt;
[[File:New_analytic_gui_pie.png]]&lt;br /&gt;
&lt;br /&gt;
Second, we would like to offer the user more scope of comparison instead restricting to only assignment wise comparison &lt;br /&gt;
&lt;br /&gt;
Comparison scope give user options to select what do they want to compare:Course, Assignment, Team&lt;br /&gt;
&lt;br /&gt;
=== Comparison Object Selection===&lt;br /&gt;
After the user select the graph type and comparison scope the user are presented with options to select comparison object and comparison data. These options changes depending on the graph type and comparison type.&lt;br /&gt;
 &lt;br /&gt;
Comparison object depends on what comparison scope the user selected. For example:&lt;br /&gt;
 &lt;br /&gt;
If the user select course wise comparison scope than the user will be presented with a check-box list of courses that they are allow to view. &lt;br /&gt;
&lt;br /&gt;
[[File:New_analytic_gui_2_1.png‎]]&lt;br /&gt;
&lt;br /&gt;
However if the user select assignment wise comparison scope the user will be presented with drop down menu with list of courses that they are allow to view and once the user select the course that they are interested in the are presented with a check-box list of assignments that are within the courses.&lt;br /&gt;
&lt;br /&gt;
[[File:New_analytic_gui_2_2.png‎]]&lt;br /&gt;
&lt;br /&gt;
User can select multiple object that they want to compare and the graph will update dynamically.&lt;br /&gt;
&lt;br /&gt;
=== Comparison Data Selection ===&lt;br /&gt;
Options for comparison data depends on both comparison scope and graph type. A check-box list will be displayed containing the appropriate data type that are available for the selected graph type and selected comparison scope.&lt;br /&gt;
&lt;br /&gt;
'''Development Note:''' Currently we only finish the options related to for bar graph, however the framework for adding options related to other type of graph has been completed.&lt;br /&gt;
&lt;br /&gt;
[[File:New_analytic_gui_3.png]]&lt;br /&gt;
&lt;br /&gt;
The user can select multiple type of graph data and the graph will be updated dynamiclly&lt;br /&gt;
&lt;br /&gt;
=== Graph Display ===&lt;br /&gt;
Once all of the selections are complete a graph containing the data will be rendered. We would like to retain the ability to display the numeric value for the data, however table containing all of the data in the graph is not the most efficient way to  to percent the data instead in the new implementation the numeric value for the data will be display when the user hover the curer over the graph.&lt;br /&gt;
&lt;br /&gt;
[[File:New_analytic_gui_4.png]]&lt;br /&gt;
&lt;br /&gt;
= Framework Design =&lt;br /&gt;
Architecture of the Expertiza Analytic Framework is based on the model-view-controller design pattern used in Rails application.&lt;br /&gt;
[[File:Analytic_framwork_archtecture.png]]&lt;br /&gt;
&lt;br /&gt;
== Model ==&lt;br /&gt;
There are two tasks that analytic require the model to perform: data mining and data formating.&lt;br /&gt;
&lt;br /&gt;
=== Data Mining ===&lt;br /&gt;
The data mining operations performed by model classes gathers the data requested by the controller.&lt;br /&gt;
&lt;br /&gt;
==== Original Implementation ====&lt;br /&gt;
In the original implementation of analytic, data mining methods use to extract the data are scattered between the assignment model class and controller. There are several problems with this method of implementation.&lt;br /&gt;
&lt;br /&gt;
First, assignment model is crucial class for the correct operation of the Expertiza and the data mining methods are not related to the normal operation that the model performs this reduces the readability and maintainability of the model.&lt;br /&gt;
&lt;br /&gt;
Second, the data mining methods seem to recursively traverse through the data structure hierarchy level by level (for example: to obtain average grades for assignment, the original implementation do assignment-&amp;gt;for each teams-&amp;gt; for each responses-&amp;gt;score-&amp;gt;grade) in-order to obtain the data that they need. This design violates basic object oriented programming principle, instead the data mining methods should be layered and methods should be added to the corresponding models to avoid this behavior.&lt;br /&gt;
&lt;br /&gt;
==== New Implementation ==== &lt;br /&gt;
The new implementation of analytic the data mining methods are implemented as mixin modules for the corresponding models. The modules are gathered into the /models/analytic folder. The modules contains the analytic methods for the model that they relates to. The analytic modules only calls the methods that are defined in model that they corresponds to and methods defined in other analytic module. &lt;br /&gt;
&lt;br /&gt;
This implementation creates a clear separation between the normal model operation and data mining operation; abstract away the complexity of the structure of the underlying databases; improves the readability for the developer. &lt;br /&gt;
&lt;br /&gt;
==== Related Files ====&lt;br /&gt;
Analytic modules and their corresponding models:&lt;br /&gt;
*expertiza/app/models/analytic/course_analytic.rb             =&amp;gt; expertiza/app/models/course.rb&lt;br /&gt;
*expertiza/app/models/analytic/assignment_analytic.rb         =&amp;gt; expertiza/app/models/assignment.rb&lt;br /&gt;
*expertiza/app/models/analytic/assignment_team_analytic.rb    =&amp;gt; expertiza/app/models/assignment_team.rb&lt;br /&gt;
*expertiza/app/models/analytic/questionnaire_analytic.rb      =&amp;gt; expertiza/app/models/questionnaire.rb&lt;br /&gt;
*expertiza/app/models/analytic/question_analytic.rb           =&amp;gt; expertiza/app/models/question.rb&lt;br /&gt;
*expertiza/app/models/analytic/response_analytic.rb           =&amp;gt; expertiza/app/models/response.rb&lt;br /&gt;
*expertiza/app/models/analytic/question_analytic.rb           =&amp;gt; expertiza/app/models/question.rb&lt;br /&gt;
*expertiza/app/models/analytic/score_analytic.rb              =&amp;gt; expertiza/app/models/score.rb&lt;br /&gt;
&lt;br /&gt;
=== Data Formatting ===&lt;br /&gt;
The data formatting operations take the gathered data and formatted turn it into the format that are require by the methods that draws the charts that display the data.&lt;br /&gt;
 &lt;br /&gt;
==== Original Implementation ====&lt;br /&gt;
The original implementation of analytic have the controller handles data formatting function. In the MVC structure used in rails application, formatting the data is not a responsibility for the controller.&lt;br /&gt;
&lt;br /&gt;
==== New Implementation ====&lt;br /&gt;
The new implementation of analytic a Chart class was created to handle the formatting of data. The Chart class serve as a interface between the java script based front-end in view and the ruby model back-end. The controller passes the graph type and data into the Chart class and the Chart class will handle all formatting and return the data that controller can pass into view. &lt;br /&gt;
&lt;br /&gt;
The example for the input data format and how to use the Chart class will be provided in later section.&lt;br /&gt;
&lt;br /&gt;
==== Related files ====&lt;br /&gt;
*expertiza/app/helpers/chart.rb&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Development Note:''' currently the Chart class located in the helper folder, this is not the proper location for the file. The file should be move to the models folder and rename to analytic.rb.&lt;br /&gt;
&lt;br /&gt;
== View ==&lt;br /&gt;
In analytic view displays options for the user, send user input to the controller and presents graph in a particular format, triggered by a controller's decision based on the user input.&lt;br /&gt;
&lt;br /&gt;
=== Original Implementation ===&lt;br /&gt;
The original implementation of analytic leverages the google chart ruby gem to generate the graph in a static fashion. The graph data type that the user can request are hard coded into the view with lots of switch statements and if else statements to handle different cases. This method of implementation which limits what the user can see and dramatically increases the difficult for adding new data options that user can see. According to the documentation for the MVC architecture used in Rails, the decision for what to display should be handle by the controller.&lt;br /&gt;
&lt;br /&gt;
Using google chart ruby gem is an acceptable implementation however the google chart gem have limited capabilities and a somewhat outdated look. Further more, the gem it might have the risk running into compatibly issues with future Rails version if the gem is not kept up to date.&lt;br /&gt;
&lt;br /&gt;
=== New Implementation ===&lt;br /&gt;
For our new implementation of analytic we decided to move to jQuery. jQuery is a powerful, popular, and well maintained webpage UI library for java script.&lt;br /&gt;
(Steven... add more)&lt;br /&gt;
&lt;br /&gt;
== Controller ==&lt;br /&gt;
&lt;br /&gt;
=== Method Overview === &lt;br /&gt;
methods with post fix _list (graph_data_type_list, course_list, assignment_list, team_list) are use to populate the different selection in view.&lt;br /&gt;
&lt;br /&gt;
get_graph_data_bundle method is use to generate the data for the graph in view&lt;br /&gt;
&lt;br /&gt;
bar_chart_data, line_chart_data, scatter_chart_data, pie_chart_data located in analytic helper (expertiza/app/helpers/analytic_helper.rb) are the interface function between the analytic modules and chart class&lt;br /&gt;
&lt;br /&gt;
=== Access Control ===&lt;br /&gt;
Goldberg menu system provided control for who can access the analytic page however it does not control what courses are displayed once they are in the analytic page. By assigning permission of administer assignment to the controller in Goldberg menu system we restricted the access of analytic to user with role of super-admin, admin, instructor and teaching assistant. However we would like the user to only be able to see the courses that they are associated with. To achieve that we the method associated_courses was added to the course helper (expertiza/app/helpers/course_helper.rb)&lt;br /&gt;
&lt;br /&gt;
The associated_courses method returns a list of courses depending on the user's role. For super-admin and admin it will return all of the courses that are in the database. For instructor it will return the courses that belongs to the instructor. For teaching assistant it will return all the courses that they are TA-ing.&lt;br /&gt;
&lt;br /&gt;
The course_list method in the analytic controller uses this method to determine which list should be listed on the course selection section of the view.&lt;br /&gt;
&lt;br /&gt;
=== Adding New Option for the Data Type ===&lt;br /&gt;
The method graph_data_type_list is use to return a list of appropriate options for the data type selection in view. &lt;br /&gt;
&lt;br /&gt;
@available_data_type is a hash of comparison_scope and graph_type with method names in the analytic modules initialized in the init method.&lt;br /&gt;
 &lt;br /&gt;
all of the methods within the analytic modules will be automatically pull into the @available_data_type[comparison_scope] (i.e: @available_data_type[:course] will contain all of the methods defined in the CourseAanlytic module) @available_data_type[graph_type] contain the subset of methods that in the analytic modules that are suited for the particular graph_type.&lt;br /&gt;
&lt;br /&gt;
The method uses the cross section of @available_data_type[comparison_scope] and @available_data_type[graph_type] to determine the appropriated data type options in view&lt;br /&gt;
&lt;br /&gt;
To add new data type simply add a the data mining method in the corresponding analytic module than add the name of the method as a string into @available_data_type for the appropriate graph_type&lt;br /&gt;
&lt;br /&gt;
= Developer Resources = &lt;br /&gt;
[https://github.com/q115288339/expertiza github repository for the project] &lt;br /&gt;
the content of this repository might change suggest using [https://github.com/expertiza/expertiza the main repository for expertiza] if the branch has already been merged &lt;br /&gt;
&lt;br /&gt;
[http://www.youtube.com/watch?v=nGRxQtqaqOo&amp;amp;list=PLqv3LmpyB8wB76ZZjKnHBGfYqSBBnVt81 Youtube playlist about E734] &lt;br /&gt;
&lt;br /&gt;
== Development Environment Setup ==&lt;br /&gt;
*follow the instruction on expertiza wiki page to setup the development environment http://wikis.lib.ncsu.edu/index.php/Creating_a_Linux_Development_Environment_for_the_Expertiza_Application&lt;br /&gt;
''note:'' you might need to delete the automated_metareview table from the database before you can successfully run db:migrate&lt;br /&gt;
*launch expertiza server (ruby script/server)&lt;br /&gt;
*visit the page from your browser (0.0.0.0:3000)&lt;br /&gt;
*login as admin (username: admin, password: password)&lt;br /&gt;
*setup access permission for the controller&lt;br /&gt;
**on the menu bar navigates to administration-&amp;gt;setup-&amp;gt;controller/action &lt;br /&gt;
**under the missing tab you should see analytic, click on it and select administer_assignment for the permission than click add&lt;br /&gt;
**under the application tab click on analytic and add index and render_sample to the action list&lt;br /&gt;
*add analytic to menu bar for easy access (alternatively just enter 0.0.0.0:3000/analytic in your browser)&lt;br /&gt;
**on the menu bar navigates to administration-&amp;gt;setup-&amp;gt;menu editor&lt;br /&gt;
**add an menu option for analytic/index&lt;br /&gt;
''note:'' not all of the courses and assignments in scrubbed database are real (with actual data), in order to get courses and assignments that have real data you might want to consider impersonate a real instructor in the scrubbed data base (i.e: Dr.Geringer, user id: efg) this can be done by navigating to manage...-&amp;gt;impersonate_user from the menu bar.&lt;br /&gt;
&lt;br /&gt;
== Rendering Example ==&lt;br /&gt;
For the benefit of future developers we provided examples on how to use the Chart class (chart.rb) to format data for the chart and how to use the jQuery stubs (_chart.html.erb) to render the chart&lt;br /&gt;
&lt;br /&gt;
The example is located at expertiza/app/views/analytic/render_sample&lt;br /&gt;
&lt;br /&gt;
To access the webpage created by this example visit 0.0.0.0:3000/analytic/render_sample (assuming you follow the setup instruction above)&lt;br /&gt;
&lt;br /&gt;
= Future Works =&lt;br /&gt;
Sorted by task difficulty&lt;br /&gt;
*Move expertiza/app/helpers/chart.rb to expertiza/app/model and rename to analytic.rb&lt;br /&gt;
*Rename Chart class to Analytic&lt;br /&gt;
*Unit test&lt;br /&gt;
*Add data mining methods for meta-review&lt;br /&gt;
*Add options for displaying raw data point related data using scatter plot(for example: raw grades)&lt;br /&gt;
*Come up with other interesting data for the comparison scope&lt;br /&gt;
*Improve the performance of data mining methods especially score related methods. &lt;br /&gt;
*Add user comparisons (comparing student with student)&lt;br /&gt;
*Give control of options in the of graph type selection and comparison scope selection drop down to controller (require java script experience)&lt;br /&gt;
*Implement a static version of the index page using ruby (require java script experience)&lt;br /&gt;
*Strengthen access control by checking if the current user have permission to access to the data that they requested&lt;br /&gt;
&lt;br /&gt;
'''IMPORTANT NOTE:''' There is a bug in the get_average_score function in the response module class WATCH OUT!&lt;/div&gt;</summary>
		<author><name>Cyang14</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E629&amp;diff=74396</id>
		<title>CSC/ECE 517 Spring 2013/OSS E629</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E629&amp;diff=74396"/>
		<updated>2013-03-24T14:20:38Z</updated>

		<summary type="html">&lt;p&gt;Cyang14: /* Our Work */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=E629: Integrating game frontend for Expertiza=&lt;br /&gt;
&lt;br /&gt;
==Project Introduction==&lt;br /&gt;
This is a game made for the [http://wikis.lib.ncsu.edu/index.php/Expertiza Expertiza] system (Ed Gehringer, NCSU). It transforms an assignment-based course into a &amp;quot;game&amp;quot; in which the player levels up and completes areas. The player doesn't receive grades; rather, he gains experience, and the professor would convert the experience values of the players at the end of the course into grades. The map is split up into distinct &amp;quot;rooms&amp;quot; unlocked by completing assignments; these are arrayed around a &amp;quot;center&amp;quot; in the middle of the map which contains portals to each room (portals which unlock as the player progresses).&lt;br /&gt;
&lt;br /&gt;
==Project Requirement==&lt;br /&gt;
Students will need to develop a page that runs the Expertiza game in JavaScript when a user is logged in and calls the code from E407 to get the JSON string to be parsed by the frontend.&lt;br /&gt;
&lt;br /&gt;
The page should basically load the frontend [http://en.wikipedia.org/wiki/JavaScript JavaScript] and point it to the [http://en.wikipedia.org/wiki/JSON JSON] string (a URL that will check to make sure that the current user is logged in correctly).&lt;br /&gt;
&lt;br /&gt;
[https://docs.google.com/a/ncsu.edu/document/d/1SVNx7Eh6dUdrz2a9rGSgBxLkxAmuBUm8eu08nHEdjIg/edit# Project Description]&lt;br /&gt;
&lt;br /&gt;
==Our Work==&lt;br /&gt;
&lt;br /&gt;
===Experience Point Report===&lt;br /&gt;
In this game, the player doesn't receive grades; rather, he gains experience(XP), the most important thing about the XP points is that they can accumulate instead of just getting average as student’s final score. As an example, if a student scored 80 in the first several assignments may eventually lower his/her grade if he decides to do some extra work on extra assignment and got 70 in the extra assignment. The grade can either automatically generated by multiplying percentage score with the weight of assignment, it can also be set by the instructor manually. The professor would convert the experience values of the players at the end of the course into grades.&lt;br /&gt;
&lt;br /&gt;
The previous XP logic in JSON string generating module is a piece of static code which always just sends a manually set percentage score as the XP point. A part of our work is to rewrite this XP point system.&lt;br /&gt;
&lt;br /&gt;
The new XP point system is a weighted score system, where instructor can assign or automatically generate the XP point.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Assignment Group Modules===&lt;br /&gt;
Assignment group module is used to group similar assignments in to one assignment group, such that as long as the student accumulates enough XP points in one assignment group, he can continue to the next group regardless whether there is any assignment in this group left untouched.&lt;br /&gt;
&lt;br /&gt;
Previous assignment group from the previous project E407 is a fake module. We believe the fake module is there for testing proposes. So we rewrite the MVC code for the assignment group module and assign access privilege to it.&lt;br /&gt;
&lt;br /&gt;
In addition, in previous version of backend code, the code to decide whether an assignment group is “available” to a student appears in both frontend and backend, but neither of them works. So we also rewrite that part and have it implemented at backend.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===JavaScript Code Reconstruction===&lt;br /&gt;
When the game is running on the Expertiza system, Expertiza should send the game the info it needs to generate the level and game state at load-time. The page should basically load the frontend JavaScript and point it to the JSON string.&lt;br /&gt;
&lt;br /&gt;
Previous system fetches JSON string by sending the request and by [http://en.wikipedia.org/wiki/Polling_(computer_science) polling] the socket for response. In this strategy,  the computer does nothing other than check the status of the socket until it get hit, at which point the device is accessed.  In order to improve the utilization of the CPU time, we introduced [http://en.wikipedia.org/wiki/Event-driven_programming Event Driven Programming (EDP)] into our design.   In EDP, the flow of the program is now driven by events. In our case, a callback closure is called when the JSON data hit the frontend.&lt;br /&gt;
&lt;br /&gt;
We also Change the ig module a little bit to support visiting module variable in closure.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Fixing Mega Bugs===&lt;br /&gt;
====Uncaught Exception====&lt;br /&gt;
Previous JSON backend throws an uncaught exception when the user has an unsubmitted assignment and he is participating in that assignment.&lt;br /&gt;
&lt;br /&gt;
'''Cause''': previous code checks the submitting status prior to the privilege checking.&lt;br /&gt;
&lt;br /&gt;
'''Reproduce''': imitate a new assignment, add a student as participant. Then login to the game interface as the student directly.&lt;br /&gt;
&lt;br /&gt;
'''Status''': Fixed&lt;br /&gt;
&lt;br /&gt;
====Gate Name Error====&lt;br /&gt;
Previous JSON backend didn’t set gate name correctly. Thus when having multiple assignment group (&amp;gt;3) and at least one hub threshold is higher than current user’s XP point, JavaScript frontend falls into infinite loop.&lt;br /&gt;
&lt;br /&gt;
'''Cause''': frontend gate control code not working, we moive that part of code into backend.&lt;br /&gt;
&lt;br /&gt;
'''Reproduce''': Just create multiple assignment groups, each group with a XP threshold. Depend on the browsers and browser versions, the front end may stop responding, show black screen or some portal gates not functioning.&lt;br /&gt;
&lt;br /&gt;
'''Status''': Fixed&lt;br /&gt;
&lt;br /&gt;
====Assignment Group Error====&lt;br /&gt;
In previous JSON backend, if an assignment group has X assignment in it, it will return X copy of that an assignment group, thus there’ll be X*X assignment in total. X times greater than it should be.&lt;br /&gt;
&lt;br /&gt;
'''Cause''': the backend uses inner join to query the database where it should be left join.&lt;br /&gt;
&lt;br /&gt;
'''Reproduce''': Create 3 assignment groups, each contains 3 assignments. Make all assignments available to student. Then log into game interface as student. One should see much more assignments there than it should be.&lt;br /&gt;
&lt;br /&gt;
'''Status''': Fixed.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Fixing Minor Bugs===&lt;br /&gt;
* Previous backend send all assignment data to frontend even if the student is not participate in the assignment. &lt;br /&gt;
&lt;br /&gt;
* Previous JSON backend didn’t set MIME type correctly. That result in frontend code can only use low layer API to capture JSON data, when using high layer API, a silent error will occur.&lt;br /&gt;
&lt;br /&gt;
* Previous MIME type result in IE and Chrome has different behavior.&lt;br /&gt;
&lt;br /&gt;
* Sending a JSON string “null” to frontend will make it stop responding.&lt;br /&gt;
&lt;br /&gt;
==General Introduction to the System Setup==&lt;br /&gt;
Please follow these steps to setup the program:&lt;br /&gt;
&lt;br /&gt;
a)	Download and deploy the code from &lt;br /&gt;
http://commondatastorage.googleapis.com/silverwzw/expertiza-json.zip&lt;br /&gt;
&lt;br /&gt;
b)	Drop the database “pg_development”&lt;br /&gt;
&lt;br /&gt;
c)	Create an empty database “pg_development”&lt;br /&gt;
&lt;br /&gt;
d)	Run “db:migrate”&lt;br /&gt;
&lt;br /&gt;
e)	Start the server&lt;br /&gt;
&lt;br /&gt;
f)	Login as admin&lt;br /&gt;
&lt;br /&gt;
g)	Go to Navigation bar -&amp;gt; Administration -&amp;gt; Setup -&amp;gt; Menu Editor&lt;br /&gt;
&lt;br /&gt;
h)	In Menu Editor page, find “Assignment Group” row (it should be the last but six row)&lt;br /&gt;
&lt;br /&gt;
i)	You should see an up arrow at the left column of that row, click it.&lt;br /&gt;
&lt;br /&gt;
j)	Then the “Assignment Group” row goes up, click the 2nd column of that “Assignment Group” row, in which it should say “assignment_groups:list”&lt;br /&gt;
&lt;br /&gt;
k)	In the pop up page named “Editing controller_action” there’s a drop down box with label ”Permission”, choose “administer assignment” in that drop down box, then click button “Edit”&lt;br /&gt;
&lt;br /&gt;
l)	Create a student account, let’s say its name is “st1”.&lt;br /&gt;
&lt;br /&gt;
m)	Create 2 additional assignment groups (then we will have 3 assignment groups). Set the XP value of the “Default Group” to -1. Set the XP value of the “2nd Group” to 200. Set the XP value of the “3rd Group” to 3000. (XP value of an assignment group is the threshold to open that assignment room).&lt;br /&gt;
&lt;br /&gt;
n)	Create 5 assignment:&lt;br /&gt;
Please do it carefully and ONE BY ONE in order, set all deadlines to anytime LATER than May, 2013, select the “available to student” check button.&lt;br /&gt;
&lt;br /&gt;
	a1 : Group = Default Group | XP = 120 | directory =   /submitted_content/edit/1&lt;br /&gt;
&lt;br /&gt;
	a2 : Group = Default Group | XP = 200 | directory = /submitted_content/edit/2&lt;br /&gt;
&lt;br /&gt;
	a3 : Group = 2nd Group | XP = 2000 | directory = /submitted_content/edit/3&lt;br /&gt;
&lt;br /&gt;
	a4 : Group = 3rd Group | XP = 2000 | directory = /submitted_content/edit/4&lt;br /&gt;
&lt;br /&gt;
	a5 : Group = Default Group | XP = 2000 | directory = /submitted_content/edit/5&lt;br /&gt;
&lt;br /&gt;
add participant “st1” to  a1, a2, a3, a4 ( NOT for a5 )&lt;/div&gt;</summary>
		<author><name>Cyang14</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E629&amp;diff=74395</id>
		<title>CSC/ECE 517 Spring 2013/OSS E629</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E629&amp;diff=74395"/>
		<updated>2013-03-24T14:20:04Z</updated>

		<summary type="html">&lt;p&gt;Cyang14: /* Assignment Group Error */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=E629: Integrating game frontend for Expertiza=&lt;br /&gt;
&lt;br /&gt;
==Project Introduction==&lt;br /&gt;
This is a game made for the [http://wikis.lib.ncsu.edu/index.php/Expertiza Expertiza] system (Ed Gehringer, NCSU). It transforms an assignment-based course into a &amp;quot;game&amp;quot; in which the player levels up and completes areas. The player doesn't receive grades; rather, he gains experience, and the professor would convert the experience values of the players at the end of the course into grades. The map is split up into distinct &amp;quot;rooms&amp;quot; unlocked by completing assignments; these are arrayed around a &amp;quot;center&amp;quot; in the middle of the map which contains portals to each room (portals which unlock as the player progresses).&lt;br /&gt;
&lt;br /&gt;
==Project Requirement==&lt;br /&gt;
Students will need to develop a page that runs the Expertiza game in JavaScript when a user is logged in and calls the code from E407 to get the JSON string to be parsed by the frontend.&lt;br /&gt;
&lt;br /&gt;
The page should basically load the frontend [http://en.wikipedia.org/wiki/JavaScript JavaScript] and point it to the [http://en.wikipedia.org/wiki/JSON JSON] string (a URL that will check to make sure that the current user is logged in correctly).&lt;br /&gt;
&lt;br /&gt;
[https://docs.google.com/a/ncsu.edu/document/d/1SVNx7Eh6dUdrz2a9rGSgBxLkxAmuBUm8eu08nHEdjIg/edit# Project Description]&lt;br /&gt;
&lt;br /&gt;
==Our Work==&lt;br /&gt;
&lt;br /&gt;
===Experience Point Report===&lt;br /&gt;
In this game, the player doesn't receive grades; rather, he gains experience(XP), the most important thing about the XP points is that they can accumulate instead of just getting average as student’s final score. As an example, if a student scored 80 in the first several assignments may eventually lower his/her grade if he decides to do some extra work on extra assignment and got 70 in the extra assignment. The grade can either automatically generated by multiplying percentage score with the weight of assignment, it can also be set by the instructor manually. The professor would convert the experience values of the players at the end of the course into grades.&lt;br /&gt;
&lt;br /&gt;
The previous XP logic in JSON string generating module is a piece of static code which always just sends a manually set percentage score as the XP point. A part of our work is to rewrite this XP point system.&lt;br /&gt;
&lt;br /&gt;
The new XP point system is a weighted score system, where instructor can assign or automatically generate the XP point.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Assignment Group Modules===&lt;br /&gt;
Assignment group module is used to group similar assignments in to one assignment group, such that as long as the student accumulates enough XP points in one assignment group, he can continue to the next group regardless whether there is any assignment in this group left untouched.&lt;br /&gt;
&lt;br /&gt;
Previous assignment group from the previous project E407 is a fake module. We believe the fake module is there for testing proposes. So we rewrite the MVC code for the assignment group module and assign access privilege to it.&lt;br /&gt;
&lt;br /&gt;
In addition, in previous version of backend code, the code to decide whether an assignment group is “available” to a student appears in both frontend and backend, but neither of them works. So we also rewrite that part and have it implemented at backend.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===JavaScript Code Reconstruction===&lt;br /&gt;
When the game is running on the Expertiza system, Expertiza should send the game the info it needs to generate the level and game state at load-time. The page should basically load the frontend JavaScript and point it to the JSON string.&lt;br /&gt;
&lt;br /&gt;
Previous system fetches JSON string by sending the request and by [http://en.wikipedia.org/wiki/Polling_(computer_science) polling] the socket for response. In this strategy,  the computer does nothing other than check the status of the socket until it get hit, at which point the device is accessed.  In order to improve the utilization of the CPU time, we introduced [http://en.wikipedia.org/wiki/Event-driven_programming Event Driven Programming (EDP)] into our design.   In EDP, the flow of the program is now driven by events. In our case, a callback closure is called when the JSON data hit the frontend.&lt;br /&gt;
&lt;br /&gt;
We also Change the ig module a little bit to support visiting module variable in closure.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Fixing Mega Bugs===&lt;br /&gt;
====Uncaught Exception====&lt;br /&gt;
Previous JSON backend throws an uncaught exception when the user has an unsubmitted assignment and he is participating in that assignment.&lt;br /&gt;
&lt;br /&gt;
'''Cause''': previous code checks the submitting status prior to the privilege checking.&lt;br /&gt;
&lt;br /&gt;
'''Reproduce''': imitate a new assignment, add a student as participant. Then login to the game interface as the student directly.&lt;br /&gt;
&lt;br /&gt;
'''Status''': Fixed&lt;br /&gt;
&lt;br /&gt;
====Gate Name Error====&lt;br /&gt;
Previous JSON backend didn’t set gate name correctly. Thus when having multiple assignment group (&amp;gt;3) and at least one hub threshold is higher than current user’s XP point, JavaScript frontend falls into infinite loop.&lt;br /&gt;
&lt;br /&gt;
'''Cause''': frontend gate control code not working, we moive that part of code into backend.&lt;br /&gt;
&lt;br /&gt;
'''Reproduce''': Just create multiple assignment groups, each group with a XP threshold. Depend on the browsers and browser versions, the front end may stop responding, show black screen or some portal gates not functioning.&lt;br /&gt;
&lt;br /&gt;
'''Status''': Fixed&lt;br /&gt;
&lt;br /&gt;
====Assignment Group Error====&lt;br /&gt;
In previous JSON backend, if an assignment group has X assignment in it, it will return X copy of that an assignment group, thus there’ll be X*X assignment in total. X times greater than it should be.&lt;br /&gt;
&lt;br /&gt;
'''Cause''': the backend uses inner join to query the database where it should be left join.&lt;br /&gt;
&lt;br /&gt;
'''Reproduce''': Create 3 assignment groups, each contains 3 assignments. Make all assignments available to student. Then log into game interface as student. One should see much more assignments there than it should be.&lt;br /&gt;
&lt;br /&gt;
'''Status''': Fixed.&lt;br /&gt;
&lt;br /&gt;
===Fixing Minor Bugs===&lt;br /&gt;
* Previous backend send all assignment data to frontend even if the student is not participate in the assignment. &lt;br /&gt;
&lt;br /&gt;
* Previous JSON backend didn’t set MIME type correctly. That result in frontend code can only use low layer API to capture JSON data, when using high layer API, a silent error will occur.&lt;br /&gt;
&lt;br /&gt;
* Previous MIME type result in IE and Chrome has different behavior.&lt;br /&gt;
&lt;br /&gt;
* Sending a JSON string “null” to frontend will make it stop responding.&lt;br /&gt;
&lt;br /&gt;
==General Introduction to the System Setup==&lt;br /&gt;
Please follow these steps to setup the program:&lt;br /&gt;
&lt;br /&gt;
a)	Download and deploy the code from &lt;br /&gt;
http://commondatastorage.googleapis.com/silverwzw/expertiza-json.zip&lt;br /&gt;
&lt;br /&gt;
b)	Drop the database “pg_development”&lt;br /&gt;
&lt;br /&gt;
c)	Create an empty database “pg_development”&lt;br /&gt;
&lt;br /&gt;
d)	Run “db:migrate”&lt;br /&gt;
&lt;br /&gt;
e)	Start the server&lt;br /&gt;
&lt;br /&gt;
f)	Login as admin&lt;br /&gt;
&lt;br /&gt;
g)	Go to Navigation bar -&amp;gt; Administration -&amp;gt; Setup -&amp;gt; Menu Editor&lt;br /&gt;
&lt;br /&gt;
h)	In Menu Editor page, find “Assignment Group” row (it should be the last but six row)&lt;br /&gt;
&lt;br /&gt;
i)	You should see an up arrow at the left column of that row, click it.&lt;br /&gt;
&lt;br /&gt;
j)	Then the “Assignment Group” row goes up, click the 2nd column of that “Assignment Group” row, in which it should say “assignment_groups:list”&lt;br /&gt;
&lt;br /&gt;
k)	In the pop up page named “Editing controller_action” there’s a drop down box with label ”Permission”, choose “administer assignment” in that drop down box, then click button “Edit”&lt;br /&gt;
&lt;br /&gt;
l)	Create a student account, let’s say its name is “st1”.&lt;br /&gt;
&lt;br /&gt;
m)	Create 2 additional assignment groups (then we will have 3 assignment groups). Set the XP value of the “Default Group” to -1. Set the XP value of the “2nd Group” to 200. Set the XP value of the “3rd Group” to 3000. (XP value of an assignment group is the threshold to open that assignment room).&lt;br /&gt;
&lt;br /&gt;
n)	Create 5 assignment:&lt;br /&gt;
Please do it carefully and ONE BY ONE in order, set all deadlines to anytime LATER than May, 2013, select the “available to student” check button.&lt;br /&gt;
&lt;br /&gt;
	a1 : Group = Default Group | XP = 120 | directory =   /submitted_content/edit/1&lt;br /&gt;
&lt;br /&gt;
	a2 : Group = Default Group | XP = 200 | directory = /submitted_content/edit/2&lt;br /&gt;
&lt;br /&gt;
	a3 : Group = 2nd Group | XP = 2000 | directory = /submitted_content/edit/3&lt;br /&gt;
&lt;br /&gt;
	a4 : Group = 3rd Group | XP = 2000 | directory = /submitted_content/edit/4&lt;br /&gt;
&lt;br /&gt;
	a5 : Group = Default Group | XP = 2000 | directory = /submitted_content/edit/5&lt;br /&gt;
&lt;br /&gt;
add participant “st1” to  a1, a2, a3, a4 ( NOT for a5 )&lt;/div&gt;</summary>
		<author><name>Cyang14</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E629&amp;diff=74394</id>
		<title>CSC/ECE 517 Spring 2013/OSS E629</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E629&amp;diff=74394"/>
		<updated>2013-03-24T14:19:30Z</updated>

		<summary type="html">&lt;p&gt;Cyang14: /* Gate Name Error */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=E629: Integrating game frontend for Expertiza=&lt;br /&gt;
&lt;br /&gt;
==Project Introduction==&lt;br /&gt;
This is a game made for the [http://wikis.lib.ncsu.edu/index.php/Expertiza Expertiza] system (Ed Gehringer, NCSU). It transforms an assignment-based course into a &amp;quot;game&amp;quot; in which the player levels up and completes areas. The player doesn't receive grades; rather, he gains experience, and the professor would convert the experience values of the players at the end of the course into grades. The map is split up into distinct &amp;quot;rooms&amp;quot; unlocked by completing assignments; these are arrayed around a &amp;quot;center&amp;quot; in the middle of the map which contains portals to each room (portals which unlock as the player progresses).&lt;br /&gt;
&lt;br /&gt;
==Project Requirement==&lt;br /&gt;
Students will need to develop a page that runs the Expertiza game in JavaScript when a user is logged in and calls the code from E407 to get the JSON string to be parsed by the frontend.&lt;br /&gt;
&lt;br /&gt;
The page should basically load the frontend [http://en.wikipedia.org/wiki/JavaScript JavaScript] and point it to the [http://en.wikipedia.org/wiki/JSON JSON] string (a URL that will check to make sure that the current user is logged in correctly).&lt;br /&gt;
&lt;br /&gt;
[https://docs.google.com/a/ncsu.edu/document/d/1SVNx7Eh6dUdrz2a9rGSgBxLkxAmuBUm8eu08nHEdjIg/edit# Project Description]&lt;br /&gt;
&lt;br /&gt;
==Our Work==&lt;br /&gt;
&lt;br /&gt;
===Experience Point Report===&lt;br /&gt;
In this game, the player doesn't receive grades; rather, he gains experience(XP), the most important thing about the XP points is that they can accumulate instead of just getting average as student’s final score. As an example, if a student scored 80 in the first several assignments may eventually lower his/her grade if he decides to do some extra work on extra assignment and got 70 in the extra assignment. The grade can either automatically generated by multiplying percentage score with the weight of assignment, it can also be set by the instructor manually. The professor would convert the experience values of the players at the end of the course into grades.&lt;br /&gt;
&lt;br /&gt;
The previous XP logic in JSON string generating module is a piece of static code which always just sends a manually set percentage score as the XP point. A part of our work is to rewrite this XP point system.&lt;br /&gt;
&lt;br /&gt;
The new XP point system is a weighted score system, where instructor can assign or automatically generate the XP point.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Assignment Group Modules===&lt;br /&gt;
Assignment group module is used to group similar assignments in to one assignment group, such that as long as the student accumulates enough XP points in one assignment group, he can continue to the next group regardless whether there is any assignment in this group left untouched.&lt;br /&gt;
&lt;br /&gt;
Previous assignment group from the previous project E407 is a fake module. We believe the fake module is there for testing proposes. So we rewrite the MVC code for the assignment group module and assign access privilege to it.&lt;br /&gt;
&lt;br /&gt;
In addition, in previous version of backend code, the code to decide whether an assignment group is “available” to a student appears in both frontend and backend, but neither of them works. So we also rewrite that part and have it implemented at backend.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===JavaScript Code Reconstruction===&lt;br /&gt;
When the game is running on the Expertiza system, Expertiza should send the game the info it needs to generate the level and game state at load-time. The page should basically load the frontend JavaScript and point it to the JSON string.&lt;br /&gt;
&lt;br /&gt;
Previous system fetches JSON string by sending the request and by [http://en.wikipedia.org/wiki/Polling_(computer_science) polling] the socket for response. In this strategy,  the computer does nothing other than check the status of the socket until it get hit, at which point the device is accessed.  In order to improve the utilization of the CPU time, we introduced [http://en.wikipedia.org/wiki/Event-driven_programming Event Driven Programming (EDP)] into our design.   In EDP, the flow of the program is now driven by events. In our case, a callback closure is called when the JSON data hit the frontend.&lt;br /&gt;
&lt;br /&gt;
We also Change the ig module a little bit to support visiting module variable in closure.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Fixing Mega Bugs===&lt;br /&gt;
====Uncaught Exception====&lt;br /&gt;
Previous JSON backend throws an uncaught exception when the user has an unsubmitted assignment and he is participating in that assignment.&lt;br /&gt;
&lt;br /&gt;
'''Cause''': previous code checks the submitting status prior to the privilege checking.&lt;br /&gt;
&lt;br /&gt;
'''Reproduce''': imitate a new assignment, add a student as participant. Then login to the game interface as the student directly.&lt;br /&gt;
&lt;br /&gt;
'''Status''': Fixed&lt;br /&gt;
&lt;br /&gt;
====Gate Name Error====&lt;br /&gt;
Previous JSON backend didn’t set gate name correctly. Thus when having multiple assignment group (&amp;gt;3) and at least one hub threshold is higher than current user’s XP point, JavaScript frontend falls into infinite loop.&lt;br /&gt;
&lt;br /&gt;
'''Cause''': frontend gate control code not working, we moive that part of code into backend.&lt;br /&gt;
&lt;br /&gt;
'''Reproduce''': Just create multiple assignment groups, each group with a XP threshold. Depend on the browsers and browser versions, the front end may stop responding, show black screen or some portal gates not functioning.&lt;br /&gt;
&lt;br /&gt;
'''Status''': Fixed&lt;br /&gt;
&lt;br /&gt;
====Assignment Group Error====&lt;br /&gt;
In previous JSON backend, if an assignment group has X assignment in it, it will return X copy of that an assignment group, thus there’ll be X*X assignment in total. X times greater than it should be.&lt;br /&gt;
&lt;br /&gt;
We fix this bug by&lt;br /&gt;
&lt;br /&gt;
The following is the related code:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Fixing Minor Bugs===&lt;br /&gt;
* Previous backend send all assignment data to frontend even if the student is not participate in the assignment. &lt;br /&gt;
&lt;br /&gt;
* Previous JSON backend didn’t set MIME type correctly. That result in frontend code can only use low layer API to capture JSON data, when using high layer API, a silent error will occur.&lt;br /&gt;
&lt;br /&gt;
* Previous MIME type result in IE and Chrome has different behavior.&lt;br /&gt;
&lt;br /&gt;
* Sending a JSON string “null” to frontend will make it stop responding.&lt;br /&gt;
&lt;br /&gt;
==General Introduction to the System Setup==&lt;br /&gt;
Please follow these steps to setup the program:&lt;br /&gt;
&lt;br /&gt;
a)	Download and deploy the code from &lt;br /&gt;
http://commondatastorage.googleapis.com/silverwzw/expertiza-json.zip&lt;br /&gt;
&lt;br /&gt;
b)	Drop the database “pg_development”&lt;br /&gt;
&lt;br /&gt;
c)	Create an empty database “pg_development”&lt;br /&gt;
&lt;br /&gt;
d)	Run “db:migrate”&lt;br /&gt;
&lt;br /&gt;
e)	Start the server&lt;br /&gt;
&lt;br /&gt;
f)	Login as admin&lt;br /&gt;
&lt;br /&gt;
g)	Go to Navigation bar -&amp;gt; Administration -&amp;gt; Setup -&amp;gt; Menu Editor&lt;br /&gt;
&lt;br /&gt;
h)	In Menu Editor page, find “Assignment Group” row (it should be the last but six row)&lt;br /&gt;
&lt;br /&gt;
i)	You should see an up arrow at the left column of that row, click it.&lt;br /&gt;
&lt;br /&gt;
j)	Then the “Assignment Group” row goes up, click the 2nd column of that “Assignment Group” row, in which it should say “assignment_groups:list”&lt;br /&gt;
&lt;br /&gt;
k)	In the pop up page named “Editing controller_action” there’s a drop down box with label ”Permission”, choose “administer assignment” in that drop down box, then click button “Edit”&lt;br /&gt;
&lt;br /&gt;
l)	Create a student account, let’s say its name is “st1”.&lt;br /&gt;
&lt;br /&gt;
m)	Create 2 additional assignment groups (then we will have 3 assignment groups). Set the XP value of the “Default Group” to -1. Set the XP value of the “2nd Group” to 200. Set the XP value of the “3rd Group” to 3000. (XP value of an assignment group is the threshold to open that assignment room).&lt;br /&gt;
&lt;br /&gt;
n)	Create 5 assignment:&lt;br /&gt;
Please do it carefully and ONE BY ONE in order, set all deadlines to anytime LATER than May, 2013, select the “available to student” check button.&lt;br /&gt;
&lt;br /&gt;
	a1 : Group = Default Group | XP = 120 | directory =   /submitted_content/edit/1&lt;br /&gt;
&lt;br /&gt;
	a2 : Group = Default Group | XP = 200 | directory = /submitted_content/edit/2&lt;br /&gt;
&lt;br /&gt;
	a3 : Group = 2nd Group | XP = 2000 | directory = /submitted_content/edit/3&lt;br /&gt;
&lt;br /&gt;
	a4 : Group = 3rd Group | XP = 2000 | directory = /submitted_content/edit/4&lt;br /&gt;
&lt;br /&gt;
	a5 : Group = Default Group | XP = 2000 | directory = /submitted_content/edit/5&lt;br /&gt;
&lt;br /&gt;
add participant “st1” to  a1, a2, a3, a4 ( NOT for a5 )&lt;/div&gt;</summary>
		<author><name>Cyang14</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E629&amp;diff=74393</id>
		<title>CSC/ECE 517 Spring 2013/OSS E629</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E629&amp;diff=74393"/>
		<updated>2013-03-24T14:19:01Z</updated>

		<summary type="html">&lt;p&gt;Cyang14: /* Uncaught Exception */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=E629: Integrating game frontend for Expertiza=&lt;br /&gt;
&lt;br /&gt;
==Project Introduction==&lt;br /&gt;
This is a game made for the [http://wikis.lib.ncsu.edu/index.php/Expertiza Expertiza] system (Ed Gehringer, NCSU). It transforms an assignment-based course into a &amp;quot;game&amp;quot; in which the player levels up and completes areas. The player doesn't receive grades; rather, he gains experience, and the professor would convert the experience values of the players at the end of the course into grades. The map is split up into distinct &amp;quot;rooms&amp;quot; unlocked by completing assignments; these are arrayed around a &amp;quot;center&amp;quot; in the middle of the map which contains portals to each room (portals which unlock as the player progresses).&lt;br /&gt;
&lt;br /&gt;
==Project Requirement==&lt;br /&gt;
Students will need to develop a page that runs the Expertiza game in JavaScript when a user is logged in and calls the code from E407 to get the JSON string to be parsed by the frontend.&lt;br /&gt;
&lt;br /&gt;
The page should basically load the frontend [http://en.wikipedia.org/wiki/JavaScript JavaScript] and point it to the [http://en.wikipedia.org/wiki/JSON JSON] string (a URL that will check to make sure that the current user is logged in correctly).&lt;br /&gt;
&lt;br /&gt;
[https://docs.google.com/a/ncsu.edu/document/d/1SVNx7Eh6dUdrz2a9rGSgBxLkxAmuBUm8eu08nHEdjIg/edit# Project Description]&lt;br /&gt;
&lt;br /&gt;
==Our Work==&lt;br /&gt;
&lt;br /&gt;
===Experience Point Report===&lt;br /&gt;
In this game, the player doesn't receive grades; rather, he gains experience(XP), the most important thing about the XP points is that they can accumulate instead of just getting average as student’s final score. As an example, if a student scored 80 in the first several assignments may eventually lower his/her grade if he decides to do some extra work on extra assignment and got 70 in the extra assignment. The grade can either automatically generated by multiplying percentage score with the weight of assignment, it can also be set by the instructor manually. The professor would convert the experience values of the players at the end of the course into grades.&lt;br /&gt;
&lt;br /&gt;
The previous XP logic in JSON string generating module is a piece of static code which always just sends a manually set percentage score as the XP point. A part of our work is to rewrite this XP point system.&lt;br /&gt;
&lt;br /&gt;
The new XP point system is a weighted score system, where instructor can assign or automatically generate the XP point.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Assignment Group Modules===&lt;br /&gt;
Assignment group module is used to group similar assignments in to one assignment group, such that as long as the student accumulates enough XP points in one assignment group, he can continue to the next group regardless whether there is any assignment in this group left untouched.&lt;br /&gt;
&lt;br /&gt;
Previous assignment group from the previous project E407 is a fake module. We believe the fake module is there for testing proposes. So we rewrite the MVC code for the assignment group module and assign access privilege to it.&lt;br /&gt;
&lt;br /&gt;
In addition, in previous version of backend code, the code to decide whether an assignment group is “available” to a student appears in both frontend and backend, but neither of them works. So we also rewrite that part and have it implemented at backend.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===JavaScript Code Reconstruction===&lt;br /&gt;
When the game is running on the Expertiza system, Expertiza should send the game the info it needs to generate the level and game state at load-time. The page should basically load the frontend JavaScript and point it to the JSON string.&lt;br /&gt;
&lt;br /&gt;
Previous system fetches JSON string by sending the request and by [http://en.wikipedia.org/wiki/Polling_(computer_science) polling] the socket for response. In this strategy,  the computer does nothing other than check the status of the socket until it get hit, at which point the device is accessed.  In order to improve the utilization of the CPU time, we introduced [http://en.wikipedia.org/wiki/Event-driven_programming Event Driven Programming (EDP)] into our design.   In EDP, the flow of the program is now driven by events. In our case, a callback closure is called when the JSON data hit the frontend.&lt;br /&gt;
&lt;br /&gt;
We also Change the ig module a little bit to support visiting module variable in closure.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Fixing Mega Bugs===&lt;br /&gt;
====Uncaught Exception====&lt;br /&gt;
Previous JSON backend throws an uncaught exception when the user has an unsubmitted assignment and he is participating in that assignment.&lt;br /&gt;
&lt;br /&gt;
'''Cause''': previous code checks the submitting status prior to the privilege checking.&lt;br /&gt;
&lt;br /&gt;
'''Reproduce''': imitate a new assignment, add a student as participant. Then login to the game interface as the student directly.&lt;br /&gt;
&lt;br /&gt;
'''Status''': Fixed&lt;br /&gt;
&lt;br /&gt;
====Gate Name Error====&lt;br /&gt;
Previous JSON backend didn’t set gate name correctly. Thus when having multiple assignment group (&amp;gt;3) and at least one hub threshold is higher than current user’s XP point, JavaScript frontend falls into infinite loop.&lt;br /&gt;
&lt;br /&gt;
We fix this bug by&lt;br /&gt;
&lt;br /&gt;
The following is the related code:&lt;br /&gt;
&lt;br /&gt;
====Assignment Group Error====&lt;br /&gt;
In previous JSON backend, if an assignment group has X assignment in it, it will return X copy of that an assignment group, thus there’ll be X*X assignment in total. X times greater than it should be.&lt;br /&gt;
&lt;br /&gt;
We fix this bug by&lt;br /&gt;
&lt;br /&gt;
The following is the related code:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Fixing Minor Bugs===&lt;br /&gt;
* Previous backend send all assignment data to frontend even if the student is not participate in the assignment. &lt;br /&gt;
&lt;br /&gt;
* Previous JSON backend didn’t set MIME type correctly. That result in frontend code can only use low layer API to capture JSON data, when using high layer API, a silent error will occur.&lt;br /&gt;
&lt;br /&gt;
* Previous MIME type result in IE and Chrome has different behavior.&lt;br /&gt;
&lt;br /&gt;
* Sending a JSON string “null” to frontend will make it stop responding.&lt;br /&gt;
&lt;br /&gt;
==General Introduction to the System Setup==&lt;br /&gt;
Please follow these steps to setup the program:&lt;br /&gt;
&lt;br /&gt;
a)	Download and deploy the code from &lt;br /&gt;
http://commondatastorage.googleapis.com/silverwzw/expertiza-json.zip&lt;br /&gt;
&lt;br /&gt;
b)	Drop the database “pg_development”&lt;br /&gt;
&lt;br /&gt;
c)	Create an empty database “pg_development”&lt;br /&gt;
&lt;br /&gt;
d)	Run “db:migrate”&lt;br /&gt;
&lt;br /&gt;
e)	Start the server&lt;br /&gt;
&lt;br /&gt;
f)	Login as admin&lt;br /&gt;
&lt;br /&gt;
g)	Go to Navigation bar -&amp;gt; Administration -&amp;gt; Setup -&amp;gt; Menu Editor&lt;br /&gt;
&lt;br /&gt;
h)	In Menu Editor page, find “Assignment Group” row (it should be the last but six row)&lt;br /&gt;
&lt;br /&gt;
i)	You should see an up arrow at the left column of that row, click it.&lt;br /&gt;
&lt;br /&gt;
j)	Then the “Assignment Group” row goes up, click the 2nd column of that “Assignment Group” row, in which it should say “assignment_groups:list”&lt;br /&gt;
&lt;br /&gt;
k)	In the pop up page named “Editing controller_action” there’s a drop down box with label ”Permission”, choose “administer assignment” in that drop down box, then click button “Edit”&lt;br /&gt;
&lt;br /&gt;
l)	Create a student account, let’s say its name is “st1”.&lt;br /&gt;
&lt;br /&gt;
m)	Create 2 additional assignment groups (then we will have 3 assignment groups). Set the XP value of the “Default Group” to -1. Set the XP value of the “2nd Group” to 200. Set the XP value of the “3rd Group” to 3000. (XP value of an assignment group is the threshold to open that assignment room).&lt;br /&gt;
&lt;br /&gt;
n)	Create 5 assignment:&lt;br /&gt;
Please do it carefully and ONE BY ONE in order, set all deadlines to anytime LATER than May, 2013, select the “available to student” check button.&lt;br /&gt;
&lt;br /&gt;
	a1 : Group = Default Group | XP = 120 | directory =   /submitted_content/edit/1&lt;br /&gt;
&lt;br /&gt;
	a2 : Group = Default Group | XP = 200 | directory = /submitted_content/edit/2&lt;br /&gt;
&lt;br /&gt;
	a3 : Group = 2nd Group | XP = 2000 | directory = /submitted_content/edit/3&lt;br /&gt;
&lt;br /&gt;
	a4 : Group = 3rd Group | XP = 2000 | directory = /submitted_content/edit/4&lt;br /&gt;
&lt;br /&gt;
	a5 : Group = Default Group | XP = 2000 | directory = /submitted_content/edit/5&lt;br /&gt;
&lt;br /&gt;
add participant “st1” to  a1, a2, a3, a4 ( NOT for a5 )&lt;/div&gt;</summary>
		<author><name>Cyang14</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E629&amp;diff=74392</id>
		<title>CSC/ECE 517 Spring 2013/OSS E629</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E629&amp;diff=74392"/>
		<updated>2013-03-24T14:18:39Z</updated>

		<summary type="html">&lt;p&gt;Cyang14: /* Uncaught Exception */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=E629: Integrating game frontend for Expertiza=&lt;br /&gt;
&lt;br /&gt;
==Project Introduction==&lt;br /&gt;
This is a game made for the [http://wikis.lib.ncsu.edu/index.php/Expertiza Expertiza] system (Ed Gehringer, NCSU). It transforms an assignment-based course into a &amp;quot;game&amp;quot; in which the player levels up and completes areas. The player doesn't receive grades; rather, he gains experience, and the professor would convert the experience values of the players at the end of the course into grades. The map is split up into distinct &amp;quot;rooms&amp;quot; unlocked by completing assignments; these are arrayed around a &amp;quot;center&amp;quot; in the middle of the map which contains portals to each room (portals which unlock as the player progresses).&lt;br /&gt;
&lt;br /&gt;
==Project Requirement==&lt;br /&gt;
Students will need to develop a page that runs the Expertiza game in JavaScript when a user is logged in and calls the code from E407 to get the JSON string to be parsed by the frontend.&lt;br /&gt;
&lt;br /&gt;
The page should basically load the frontend [http://en.wikipedia.org/wiki/JavaScript JavaScript] and point it to the [http://en.wikipedia.org/wiki/JSON JSON] string (a URL that will check to make sure that the current user is logged in correctly).&lt;br /&gt;
&lt;br /&gt;
[https://docs.google.com/a/ncsu.edu/document/d/1SVNx7Eh6dUdrz2a9rGSgBxLkxAmuBUm8eu08nHEdjIg/edit# Project Description]&lt;br /&gt;
&lt;br /&gt;
==Our Work==&lt;br /&gt;
&lt;br /&gt;
===Experience Point Report===&lt;br /&gt;
In this game, the player doesn't receive grades; rather, he gains experience(XP), the most important thing about the XP points is that they can accumulate instead of just getting average as student’s final score. As an example, if a student scored 80 in the first several assignments may eventually lower his/her grade if he decides to do some extra work on extra assignment and got 70 in the extra assignment. The grade can either automatically generated by multiplying percentage score with the weight of assignment, it can also be set by the instructor manually. The professor would convert the experience values of the players at the end of the course into grades.&lt;br /&gt;
&lt;br /&gt;
The previous XP logic in JSON string generating module is a piece of static code which always just sends a manually set percentage score as the XP point. A part of our work is to rewrite this XP point system.&lt;br /&gt;
&lt;br /&gt;
The new XP point system is a weighted score system, where instructor can assign or automatically generate the XP point.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Assignment Group Modules===&lt;br /&gt;
Assignment group module is used to group similar assignments in to one assignment group, such that as long as the student accumulates enough XP points in one assignment group, he can continue to the next group regardless whether there is any assignment in this group left untouched.&lt;br /&gt;
&lt;br /&gt;
Previous assignment group from the previous project E407 is a fake module. We believe the fake module is there for testing proposes. So we rewrite the MVC code for the assignment group module and assign access privilege to it.&lt;br /&gt;
&lt;br /&gt;
In addition, in previous version of backend code, the code to decide whether an assignment group is “available” to a student appears in both frontend and backend, but neither of them works. So we also rewrite that part and have it implemented at backend.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===JavaScript Code Reconstruction===&lt;br /&gt;
When the game is running on the Expertiza system, Expertiza should send the game the info it needs to generate the level and game state at load-time. The page should basically load the frontend JavaScript and point it to the JSON string.&lt;br /&gt;
&lt;br /&gt;
Previous system fetches JSON string by sending the request and by [http://en.wikipedia.org/wiki/Polling_(computer_science) polling] the socket for response. In this strategy,  the computer does nothing other than check the status of the socket until it get hit, at which point the device is accessed.  In order to improve the utilization of the CPU time, we introduced [http://en.wikipedia.org/wiki/Event-driven_programming Event Driven Programming (EDP)] into our design.   In EDP, the flow of the program is now driven by events. In our case, a callback closure is called when the JSON data hit the frontend.&lt;br /&gt;
&lt;br /&gt;
We also Change the ig module a little bit to support visiting module variable in closure.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Fixing Mega Bugs===&lt;br /&gt;
====Uncaught Exception====&lt;br /&gt;
Previous JSON backend throws an uncaught exception when the user has an unsubmitted assignment and he is participating in that assignment.&lt;br /&gt;
&lt;br /&gt;
Cause: previous code checks the submitting status prior to the privilege checking.&lt;br /&gt;
&lt;br /&gt;
Reproduce: imitate a new assignment, add a student as participant. Then login to the game interface as the student directly.&lt;br /&gt;
&lt;br /&gt;
Status: Fixed&lt;br /&gt;
&lt;br /&gt;
====Gate Name Error====&lt;br /&gt;
Previous JSON backend didn’t set gate name correctly. Thus when having multiple assignment group (&amp;gt;3) and at least one hub threshold is higher than current user’s XP point, JavaScript frontend falls into infinite loop.&lt;br /&gt;
&lt;br /&gt;
We fix this bug by&lt;br /&gt;
&lt;br /&gt;
The following is the related code:&lt;br /&gt;
&lt;br /&gt;
====Assignment Group Error====&lt;br /&gt;
In previous JSON backend, if an assignment group has X assignment in it, it will return X copy of that an assignment group, thus there’ll be X*X assignment in total. X times greater than it should be.&lt;br /&gt;
&lt;br /&gt;
We fix this bug by&lt;br /&gt;
&lt;br /&gt;
The following is the related code:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Fixing Minor Bugs===&lt;br /&gt;
* Previous backend send all assignment data to frontend even if the student is not participate in the assignment. &lt;br /&gt;
&lt;br /&gt;
* Previous JSON backend didn’t set MIME type correctly. That result in frontend code can only use low layer API to capture JSON data, when using high layer API, a silent error will occur.&lt;br /&gt;
&lt;br /&gt;
* Previous MIME type result in IE and Chrome has different behavior.&lt;br /&gt;
&lt;br /&gt;
* Sending a JSON string “null” to frontend will make it stop responding.&lt;br /&gt;
&lt;br /&gt;
==General Introduction to the System Setup==&lt;br /&gt;
Please follow these steps to setup the program:&lt;br /&gt;
&lt;br /&gt;
a)	Download and deploy the code from &lt;br /&gt;
http://commondatastorage.googleapis.com/silverwzw/expertiza-json.zip&lt;br /&gt;
&lt;br /&gt;
b)	Drop the database “pg_development”&lt;br /&gt;
&lt;br /&gt;
c)	Create an empty database “pg_development”&lt;br /&gt;
&lt;br /&gt;
d)	Run “db:migrate”&lt;br /&gt;
&lt;br /&gt;
e)	Start the server&lt;br /&gt;
&lt;br /&gt;
f)	Login as admin&lt;br /&gt;
&lt;br /&gt;
g)	Go to Navigation bar -&amp;gt; Administration -&amp;gt; Setup -&amp;gt; Menu Editor&lt;br /&gt;
&lt;br /&gt;
h)	In Menu Editor page, find “Assignment Group” row (it should be the last but six row)&lt;br /&gt;
&lt;br /&gt;
i)	You should see an up arrow at the left column of that row, click it.&lt;br /&gt;
&lt;br /&gt;
j)	Then the “Assignment Group” row goes up, click the 2nd column of that “Assignment Group” row, in which it should say “assignment_groups:list”&lt;br /&gt;
&lt;br /&gt;
k)	In the pop up page named “Editing controller_action” there’s a drop down box with label ”Permission”, choose “administer assignment” in that drop down box, then click button “Edit”&lt;br /&gt;
&lt;br /&gt;
l)	Create a student account, let’s say its name is “st1”.&lt;br /&gt;
&lt;br /&gt;
m)	Create 2 additional assignment groups (then we will have 3 assignment groups). Set the XP value of the “Default Group” to -1. Set the XP value of the “2nd Group” to 200. Set the XP value of the “3rd Group” to 3000. (XP value of an assignment group is the threshold to open that assignment room).&lt;br /&gt;
&lt;br /&gt;
n)	Create 5 assignment:&lt;br /&gt;
Please do it carefully and ONE BY ONE in order, set all deadlines to anytime LATER than May, 2013, select the “available to student” check button.&lt;br /&gt;
&lt;br /&gt;
	a1 : Group = Default Group | XP = 120 | directory =   /submitted_content/edit/1&lt;br /&gt;
&lt;br /&gt;
	a2 : Group = Default Group | XP = 200 | directory = /submitted_content/edit/2&lt;br /&gt;
&lt;br /&gt;
	a3 : Group = 2nd Group | XP = 2000 | directory = /submitted_content/edit/3&lt;br /&gt;
&lt;br /&gt;
	a4 : Group = 3rd Group | XP = 2000 | directory = /submitted_content/edit/4&lt;br /&gt;
&lt;br /&gt;
	a5 : Group = Default Group | XP = 2000 | directory = /submitted_content/edit/5&lt;br /&gt;
&lt;br /&gt;
add participant “st1” to  a1, a2, a3, a4 ( NOT for a5 )&lt;/div&gt;</summary>
		<author><name>Cyang14</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E629&amp;diff=74391</id>
		<title>CSC/ECE 517 Spring 2013/OSS E629</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E629&amp;diff=74391"/>
		<updated>2013-03-24T14:17:30Z</updated>

		<summary type="html">&lt;p&gt;Cyang14: /* Our Work */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=E629: Integrating game frontend for Expertiza=&lt;br /&gt;
&lt;br /&gt;
==Project Introduction==&lt;br /&gt;
This is a game made for the [http://wikis.lib.ncsu.edu/index.php/Expertiza Expertiza] system (Ed Gehringer, NCSU). It transforms an assignment-based course into a &amp;quot;game&amp;quot; in which the player levels up and completes areas. The player doesn't receive grades; rather, he gains experience, and the professor would convert the experience values of the players at the end of the course into grades. The map is split up into distinct &amp;quot;rooms&amp;quot; unlocked by completing assignments; these are arrayed around a &amp;quot;center&amp;quot; in the middle of the map which contains portals to each room (portals which unlock as the player progresses).&lt;br /&gt;
&lt;br /&gt;
==Project Requirement==&lt;br /&gt;
Students will need to develop a page that runs the Expertiza game in JavaScript when a user is logged in and calls the code from E407 to get the JSON string to be parsed by the frontend.&lt;br /&gt;
&lt;br /&gt;
The page should basically load the frontend [http://en.wikipedia.org/wiki/JavaScript JavaScript] and point it to the [http://en.wikipedia.org/wiki/JSON JSON] string (a URL that will check to make sure that the current user is logged in correctly).&lt;br /&gt;
&lt;br /&gt;
[https://docs.google.com/a/ncsu.edu/document/d/1SVNx7Eh6dUdrz2a9rGSgBxLkxAmuBUm8eu08nHEdjIg/edit# Project Description]&lt;br /&gt;
&lt;br /&gt;
==Our Work==&lt;br /&gt;
&lt;br /&gt;
===Experience Point Report===&lt;br /&gt;
In this game, the player doesn't receive grades; rather, he gains experience(XP), the most important thing about the XP points is that they can accumulate instead of just getting average as student’s final score. As an example, if a student scored 80 in the first several assignments may eventually lower his/her grade if he decides to do some extra work on extra assignment and got 70 in the extra assignment. The grade can either automatically generated by multiplying percentage score with the weight of assignment, it can also be set by the instructor manually. The professor would convert the experience values of the players at the end of the course into grades.&lt;br /&gt;
&lt;br /&gt;
The previous XP logic in JSON string generating module is a piece of static code which always just sends a manually set percentage score as the XP point. A part of our work is to rewrite this XP point system.&lt;br /&gt;
&lt;br /&gt;
The new XP point system is a weighted score system, where instructor can assign or automatically generate the XP point.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Assignment Group Modules===&lt;br /&gt;
Assignment group module is used to group similar assignments in to one assignment group, such that as long as the student accumulates enough XP points in one assignment group, he can continue to the next group regardless whether there is any assignment in this group left untouched.&lt;br /&gt;
&lt;br /&gt;
Previous assignment group from the previous project E407 is a fake module. We believe the fake module is there for testing proposes. So we rewrite the MVC code for the assignment group module and assign access privilege to it.&lt;br /&gt;
&lt;br /&gt;
In addition, in previous version of backend code, the code to decide whether an assignment group is “available” to a student appears in both frontend and backend, but neither of them works. So we also rewrite that part and have it implemented at backend.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===JavaScript Code Reconstruction===&lt;br /&gt;
When the game is running on the Expertiza system, Expertiza should send the game the info it needs to generate the level and game state at load-time. The page should basically load the frontend JavaScript and point it to the JSON string.&lt;br /&gt;
&lt;br /&gt;
Previous system fetches JSON string by sending the request and by [http://en.wikipedia.org/wiki/Polling_(computer_science) polling] the socket for response. In this strategy,  the computer does nothing other than check the status of the socket until it get hit, at which point the device is accessed.  In order to improve the utilization of the CPU time, we introduced [http://en.wikipedia.org/wiki/Event-driven_programming Event Driven Programming (EDP)] into our design.   In EDP, the flow of the program is now driven by events. In our case, a callback closure is called when the JSON data hit the frontend.&lt;br /&gt;
&lt;br /&gt;
We also Change the ig module a little bit to support visiting module variable in closure.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Fixing Mega Bugs===&lt;br /&gt;
====Uncaught Exception====&lt;br /&gt;
Previous JSON backend throws an uncaught exception when the user has an unsubmitted assignment and he is participating in that assignment.&lt;br /&gt;
&lt;br /&gt;
We fix this bug by&lt;br /&gt;
&lt;br /&gt;
The following is the related code:&lt;br /&gt;
&lt;br /&gt;
====Gate Name Error====&lt;br /&gt;
Previous JSON backend didn’t set gate name correctly. Thus when having multiple assignment group (&amp;gt;3) and at least one hub threshold is higher than current user’s XP point, JavaScript frontend falls into infinite loop.&lt;br /&gt;
&lt;br /&gt;
We fix this bug by&lt;br /&gt;
&lt;br /&gt;
The following is the related code:&lt;br /&gt;
&lt;br /&gt;
====Assignment Group Error====&lt;br /&gt;
In previous JSON backend, if an assignment group has X assignment in it, it will return X copy of that an assignment group, thus there’ll be X*X assignment in total. X times greater than it should be.&lt;br /&gt;
&lt;br /&gt;
We fix this bug by&lt;br /&gt;
&lt;br /&gt;
The following is the related code:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Fixing Minor Bugs===&lt;br /&gt;
* Previous backend send all assignment data to frontend even if the student is not participate in the assignment. &lt;br /&gt;
&lt;br /&gt;
* Previous JSON backend didn’t set MIME type correctly. That result in frontend code can only use low layer API to capture JSON data, when using high layer API, a silent error will occur.&lt;br /&gt;
&lt;br /&gt;
* Previous MIME type result in IE and Chrome has different behavior.&lt;br /&gt;
&lt;br /&gt;
* Sending a JSON string “null” to frontend will make it stop responding.&lt;br /&gt;
&lt;br /&gt;
==General Introduction to the System Setup==&lt;br /&gt;
Please follow these steps to setup the program:&lt;br /&gt;
&lt;br /&gt;
a)	Download and deploy the code from &lt;br /&gt;
http://commondatastorage.googleapis.com/silverwzw/expertiza-json.zip&lt;br /&gt;
&lt;br /&gt;
b)	Drop the database “pg_development”&lt;br /&gt;
&lt;br /&gt;
c)	Create an empty database “pg_development”&lt;br /&gt;
&lt;br /&gt;
d)	Run “db:migrate”&lt;br /&gt;
&lt;br /&gt;
e)	Start the server&lt;br /&gt;
&lt;br /&gt;
f)	Login as admin&lt;br /&gt;
&lt;br /&gt;
g)	Go to Navigation bar -&amp;gt; Administration -&amp;gt; Setup -&amp;gt; Menu Editor&lt;br /&gt;
&lt;br /&gt;
h)	In Menu Editor page, find “Assignment Group” row (it should be the last but six row)&lt;br /&gt;
&lt;br /&gt;
i)	You should see an up arrow at the left column of that row, click it.&lt;br /&gt;
&lt;br /&gt;
j)	Then the “Assignment Group” row goes up, click the 2nd column of that “Assignment Group” row, in which it should say “assignment_groups:list”&lt;br /&gt;
&lt;br /&gt;
k)	In the pop up page named “Editing controller_action” there’s a drop down box with label ”Permission”, choose “administer assignment” in that drop down box, then click button “Edit”&lt;br /&gt;
&lt;br /&gt;
l)	Create a student account, let’s say its name is “st1”.&lt;br /&gt;
&lt;br /&gt;
m)	Create 2 additional assignment groups (then we will have 3 assignment groups). Set the XP value of the “Default Group” to -1. Set the XP value of the “2nd Group” to 200. Set the XP value of the “3rd Group” to 3000. (XP value of an assignment group is the threshold to open that assignment room).&lt;br /&gt;
&lt;br /&gt;
n)	Create 5 assignment:&lt;br /&gt;
Please do it carefully and ONE BY ONE in order, set all deadlines to anytime LATER than May, 2013, select the “available to student” check button.&lt;br /&gt;
&lt;br /&gt;
	a1 : Group = Default Group | XP = 120 | directory =   /submitted_content/edit/1&lt;br /&gt;
&lt;br /&gt;
	a2 : Group = Default Group | XP = 200 | directory = /submitted_content/edit/2&lt;br /&gt;
&lt;br /&gt;
	a3 : Group = 2nd Group | XP = 2000 | directory = /submitted_content/edit/3&lt;br /&gt;
&lt;br /&gt;
	a4 : Group = 3rd Group | XP = 2000 | directory = /submitted_content/edit/4&lt;br /&gt;
&lt;br /&gt;
	a5 : Group = Default Group | XP = 2000 | directory = /submitted_content/edit/5&lt;br /&gt;
&lt;br /&gt;
add participant “st1” to  a1, a2, a3, a4 ( NOT for a5 )&lt;/div&gt;</summary>
		<author><name>Cyang14</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E629&amp;diff=74390</id>
		<title>CSC/ECE 517 Spring 2013/OSS E629</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E629&amp;diff=74390"/>
		<updated>2013-03-24T14:16:36Z</updated>

		<summary type="html">&lt;p&gt;Cyang14: /* JavaScript Code Reconstruction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=E629: Integrating game frontend for Expertiza=&lt;br /&gt;
&lt;br /&gt;
==Project Introduction==&lt;br /&gt;
This is a game made for the [http://wikis.lib.ncsu.edu/index.php/Expertiza Expertiza] system (Ed Gehringer, NCSU). It transforms an assignment-based course into a &amp;quot;game&amp;quot; in which the player levels up and completes areas. The player doesn't receive grades; rather, he gains experience, and the professor would convert the experience values of the players at the end of the course into grades. The map is split up into distinct &amp;quot;rooms&amp;quot; unlocked by completing assignments; these are arrayed around a &amp;quot;center&amp;quot; in the middle of the map which contains portals to each room (portals which unlock as the player progresses).&lt;br /&gt;
&lt;br /&gt;
==Project Requirement==&lt;br /&gt;
Students will need to develop a page that runs the Expertiza game in JavaScript when a user is logged in and calls the code from E407 to get the JSON string to be parsed by the frontend.&lt;br /&gt;
&lt;br /&gt;
The page should basically load the frontend [http://en.wikipedia.org/wiki/JavaScript JavaScript] and point it to the [http://en.wikipedia.org/wiki/JSON JSON] string (a URL that will check to make sure that the current user is logged in correctly).&lt;br /&gt;
&lt;br /&gt;
[https://docs.google.com/a/ncsu.edu/document/d/1SVNx7Eh6dUdrz2a9rGSgBxLkxAmuBUm8eu08nHEdjIg/edit# Project Description]&lt;br /&gt;
&lt;br /&gt;
==Our Work==&lt;br /&gt;
&lt;br /&gt;
===Experience Point Report===&lt;br /&gt;
In this game, the player doesn't receive grades; rather, he gains experience(XP), the most important thing about the XP points is that they can accumulate instead of just getting average as student’s final score. As an example, if a student scored 80 in the first several assignments may eventually lower his/her grade if he decides to do some extra work on extra assignment and got 70 in the extra assignment. The grade can either automatically generated by multiplying percentage score with the weight of assignment, it can also be set by the instructor manually. The professor would convert the experience values of the players at the end of the course into grades.&lt;br /&gt;
&lt;br /&gt;
The previous XP logic in JSON string generating module is a piece of static code which always just sends a manually set percentage score as the XP point. A part of our work is to rewrite this XP point system.&lt;br /&gt;
&lt;br /&gt;
The new XP point system is a weighted score system, where instructor can assign or automatically generate the XP point.&lt;br /&gt;
&lt;br /&gt;
===Assignment Group Modules===&lt;br /&gt;
Assignment group module is used to group similar assignments in to one assignment group, such that as long as the student accumulates enough XP points in one assignment group, he can continue to the next group regardless whether there is any assignment in this group left untouched.&lt;br /&gt;
&lt;br /&gt;
Previous assignment group from the previous project E407 is a fake module. We believe the fake module is there for testing proposes. So we rewrite the MVC code for the assignment group module and assign access privilege to it.&lt;br /&gt;
&lt;br /&gt;
In addition, in previous version of backend code, the code to decide whether an assignment group is “available” to a student appears in both frontend and backend, but neither of them works. So we also rewrite that part and have it implemented at backend.&lt;br /&gt;
&lt;br /&gt;
===JavaScript Code Reconstruction===&lt;br /&gt;
When the game is running on the Expertiza system, Expertiza should send the game the info it needs to generate the level and game state at load-time. The page should basically load the frontend JavaScript and point it to the JSON string.&lt;br /&gt;
&lt;br /&gt;
Previous system fetches JSON string by sending the request and by [http://en.wikipedia.org/wiki/Polling_(computer_science) polling] the socket for response. In this strategy,  the computer does nothing other than check the status of the socket until it get hit, at which point the device is accessed.  In order to improve the utilization of the CPU time, we introduced [http://en.wikipedia.org/wiki/Event-driven_programming Event Driven Programming (EDP)] into our design.   In EDP, the flow of the program is now driven by events. In our case, a callback closure is called when the JSON data hit the frontend.&lt;br /&gt;
&lt;br /&gt;
We also Change the ig module a little bit to support visiting module variable in closure.&lt;br /&gt;
&lt;br /&gt;
===Fixing Mega Bugs===&lt;br /&gt;
====Uncaught Exception====&lt;br /&gt;
Previous JSON backend throws an uncaught exception when the user has an unsubmitted assignment and he is participating in that assignment.&lt;br /&gt;
&lt;br /&gt;
We fix this bug by&lt;br /&gt;
&lt;br /&gt;
The following is the related code:&lt;br /&gt;
&lt;br /&gt;
====Gate Name Error====&lt;br /&gt;
Previous JSON backend didn’t set gate name correctly. Thus when having multiple assignment group (&amp;gt;3) and at least one hub threshold is higher than current user’s XP point, JavaScript frontend falls into infinite loop.&lt;br /&gt;
&lt;br /&gt;
We fix this bug by&lt;br /&gt;
&lt;br /&gt;
The following is the related code:&lt;br /&gt;
&lt;br /&gt;
====Assignment Group Error====&lt;br /&gt;
In previous JSON backend, if an assignment group has X assignment in it, it will return X copy of that an assignment group, thus there’ll be X*X assignment in total. X times greater than it should be.&lt;br /&gt;
&lt;br /&gt;
We fix this bug by&lt;br /&gt;
&lt;br /&gt;
The following is the related code:&lt;br /&gt;
&lt;br /&gt;
===Fixing Minor Bugs===&lt;br /&gt;
* Previous backend send all assignment data to frontend even if the student is not participate in the assignment. &lt;br /&gt;
&lt;br /&gt;
* Previous JSON backend didn’t set MIME type correctly. That result in frontend code can only use low layer API to capture JSON data, when using high layer API, a silent error will occur.&lt;br /&gt;
&lt;br /&gt;
* Previous MIME type result in IE and Chrome has different behavior.&lt;br /&gt;
&lt;br /&gt;
* Sending a JSON string “null” to frontend will make it stop responding.&lt;br /&gt;
&lt;br /&gt;
==General Introduction to the System Setup==&lt;br /&gt;
Please follow these steps to setup the program:&lt;br /&gt;
&lt;br /&gt;
a)	Download and deploy the code from &lt;br /&gt;
http://commondatastorage.googleapis.com/silverwzw/expertiza-json.zip&lt;br /&gt;
&lt;br /&gt;
b)	Drop the database “pg_development”&lt;br /&gt;
&lt;br /&gt;
c)	Create an empty database “pg_development”&lt;br /&gt;
&lt;br /&gt;
d)	Run “db:migrate”&lt;br /&gt;
&lt;br /&gt;
e)	Start the server&lt;br /&gt;
&lt;br /&gt;
f)	Login as admin&lt;br /&gt;
&lt;br /&gt;
g)	Go to Navigation bar -&amp;gt; Administration -&amp;gt; Setup -&amp;gt; Menu Editor&lt;br /&gt;
&lt;br /&gt;
h)	In Menu Editor page, find “Assignment Group” row (it should be the last but six row)&lt;br /&gt;
&lt;br /&gt;
i)	You should see an up arrow at the left column of that row, click it.&lt;br /&gt;
&lt;br /&gt;
j)	Then the “Assignment Group” row goes up, click the 2nd column of that “Assignment Group” row, in which it should say “assignment_groups:list”&lt;br /&gt;
&lt;br /&gt;
k)	In the pop up page named “Editing controller_action” there’s a drop down box with label ”Permission”, choose “administer assignment” in that drop down box, then click button “Edit”&lt;br /&gt;
&lt;br /&gt;
l)	Create a student account, let’s say its name is “st1”.&lt;br /&gt;
&lt;br /&gt;
m)	Create 2 additional assignment groups (then we will have 3 assignment groups). Set the XP value of the “Default Group” to -1. Set the XP value of the “2nd Group” to 200. Set the XP value of the “3rd Group” to 3000. (XP value of an assignment group is the threshold to open that assignment room).&lt;br /&gt;
&lt;br /&gt;
n)	Create 5 assignment:&lt;br /&gt;
Please do it carefully and ONE BY ONE in order, set all deadlines to anytime LATER than May, 2013, select the “available to student” check button.&lt;br /&gt;
&lt;br /&gt;
	a1 : Group = Default Group | XP = 120 | directory =   /submitted_content/edit/1&lt;br /&gt;
&lt;br /&gt;
	a2 : Group = Default Group | XP = 200 | directory = /submitted_content/edit/2&lt;br /&gt;
&lt;br /&gt;
	a3 : Group = 2nd Group | XP = 2000 | directory = /submitted_content/edit/3&lt;br /&gt;
&lt;br /&gt;
	a4 : Group = 3rd Group | XP = 2000 | directory = /submitted_content/edit/4&lt;br /&gt;
&lt;br /&gt;
	a5 : Group = Default Group | XP = 2000 | directory = /submitted_content/edit/5&lt;br /&gt;
&lt;br /&gt;
add participant “st1” to  a1, a2, a3, a4 ( NOT for a5 )&lt;/div&gt;</summary>
		<author><name>Cyang14</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E629&amp;diff=74389</id>
		<title>CSC/ECE 517 Spring 2013/OSS E629</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E629&amp;diff=74389"/>
		<updated>2013-03-24T14:12:13Z</updated>

		<summary type="html">&lt;p&gt;Cyang14: /* Assignment Group Modules */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=E629: Integrating game frontend for Expertiza=&lt;br /&gt;
&lt;br /&gt;
==Project Introduction==&lt;br /&gt;
This is a game made for the [http://wikis.lib.ncsu.edu/index.php/Expertiza Expertiza] system (Ed Gehringer, NCSU). It transforms an assignment-based course into a &amp;quot;game&amp;quot; in which the player levels up and completes areas. The player doesn't receive grades; rather, he gains experience, and the professor would convert the experience values of the players at the end of the course into grades. The map is split up into distinct &amp;quot;rooms&amp;quot; unlocked by completing assignments; these are arrayed around a &amp;quot;center&amp;quot; in the middle of the map which contains portals to each room (portals which unlock as the player progresses).&lt;br /&gt;
&lt;br /&gt;
==Project Requirement==&lt;br /&gt;
Students will need to develop a page that runs the Expertiza game in JavaScript when a user is logged in and calls the code from E407 to get the JSON string to be parsed by the frontend.&lt;br /&gt;
&lt;br /&gt;
The page should basically load the frontend [http://en.wikipedia.org/wiki/JavaScript JavaScript] and point it to the [http://en.wikipedia.org/wiki/JSON JSON] string (a URL that will check to make sure that the current user is logged in correctly).&lt;br /&gt;
&lt;br /&gt;
[https://docs.google.com/a/ncsu.edu/document/d/1SVNx7Eh6dUdrz2a9rGSgBxLkxAmuBUm8eu08nHEdjIg/edit# Project Description]&lt;br /&gt;
&lt;br /&gt;
==Our Work==&lt;br /&gt;
&lt;br /&gt;
===Experience Point Report===&lt;br /&gt;
In this game, the player doesn't receive grades; rather, he gains experience(XP), the most important thing about the XP points is that they can accumulate instead of just getting average as student’s final score. As an example, if a student scored 80 in the first several assignments may eventually lower his/her grade if he decides to do some extra work on extra assignment and got 70 in the extra assignment. The grade can either automatically generated by multiplying percentage score with the weight of assignment, it can also be set by the instructor manually. The professor would convert the experience values of the players at the end of the course into grades.&lt;br /&gt;
&lt;br /&gt;
The previous XP logic in JSON string generating module is a piece of static code which always just sends a manually set percentage score as the XP point. A part of our work is to rewrite this XP point system.&lt;br /&gt;
&lt;br /&gt;
The new XP point system is a weighted score system, where instructor can assign or automatically generate the XP point.&lt;br /&gt;
&lt;br /&gt;
===Assignment Group Modules===&lt;br /&gt;
Assignment group module is used to group similar assignments in to one assignment group, such that as long as the student accumulates enough XP points in one assignment group, he can continue to the next group regardless whether there is any assignment in this group left untouched.&lt;br /&gt;
&lt;br /&gt;
Previous assignment group from the previous project E407 is a fake module. We believe the fake module is there for testing proposes. So we rewrite the MVC code for the assignment group module and assign access privilege to it.&lt;br /&gt;
&lt;br /&gt;
In addition, in previous version of backend code, the code to decide whether an assignment group is “available” to a student appears in both frontend and backend, but neither of them works. So we also rewrite that part and have it implemented at backend.&lt;br /&gt;
&lt;br /&gt;
===JavaScript Code Reconstruction===&lt;br /&gt;
When the game is running on the Expertiza system, Expertiza should send the game the info it needs to generate the level and game state at load-time. The page should basically load the frontend JavaScript and point it to the JSON string.&lt;br /&gt;
&lt;br /&gt;
Previous system get JSON by [http://en.wikipedia.org/wiki/Polling_(computer_science) polling] the server. In this strategy,  the computer does nothing other than check the status of the backend until it is ready, at which point the device is accessed.  In order to improve the efficiency of the CPU, we introduced [http://en.wikipedia.org/wiki/Event-driven_programming Event Driven Programming (EDP)] into our design.   In EDP, the flow of the program is determined by events. In our case, a callback closure is called when the JSON data hit the frontend.&lt;br /&gt;
&lt;br /&gt;
We also Change the ig module a little bit to support visiting module variable in closure.&lt;br /&gt;
&lt;br /&gt;
The related code is as following:&lt;br /&gt;
&lt;br /&gt;
===Fixing Mega Bugs===&lt;br /&gt;
====Uncaught Exception====&lt;br /&gt;
Previous JSON backend throws an uncaught exception when the user has an unsubmitted assignment and he is participating in that assignment.&lt;br /&gt;
&lt;br /&gt;
We fix this bug by&lt;br /&gt;
&lt;br /&gt;
The following is the related code:&lt;br /&gt;
&lt;br /&gt;
====Gate Name Error====&lt;br /&gt;
Previous JSON backend didn’t set gate name correctly. Thus when having multiple assignment group (&amp;gt;3) and at least one hub threshold is higher than current user’s XP point, JavaScript frontend falls into infinite loop.&lt;br /&gt;
&lt;br /&gt;
We fix this bug by&lt;br /&gt;
&lt;br /&gt;
The following is the related code:&lt;br /&gt;
&lt;br /&gt;
====Assignment Group Error====&lt;br /&gt;
In previous JSON backend, if an assignment group has X assignment in it, it will return X copy of that an assignment group, thus there’ll be X*X assignment in total. X times greater than it should be.&lt;br /&gt;
&lt;br /&gt;
We fix this bug by&lt;br /&gt;
&lt;br /&gt;
The following is the related code:&lt;br /&gt;
&lt;br /&gt;
===Fixing Minor Bugs===&lt;br /&gt;
* Previous backend send all assignment data to frontend even if the student is not participate in the assignment. &lt;br /&gt;
&lt;br /&gt;
* Previous JSON backend didn’t set MIME type correctly. That result in frontend code can only use low layer API to capture JSON data, when using high layer API, a silent error will occur.&lt;br /&gt;
&lt;br /&gt;
* Previous MIME type result in IE and Chrome has different behavior.&lt;br /&gt;
&lt;br /&gt;
* Sending a JSON string “null” to frontend will make it stop responding.&lt;br /&gt;
&lt;br /&gt;
==General Introduction to the System Setup==&lt;br /&gt;
Please follow these steps to setup the program:&lt;br /&gt;
&lt;br /&gt;
a)	Download and deploy the code from &lt;br /&gt;
http://commondatastorage.googleapis.com/silverwzw/expertiza-json.zip&lt;br /&gt;
&lt;br /&gt;
b)	Drop the database “pg_development”&lt;br /&gt;
&lt;br /&gt;
c)	Create an empty database “pg_development”&lt;br /&gt;
&lt;br /&gt;
d)	Run “db:migrate”&lt;br /&gt;
&lt;br /&gt;
e)	Start the server&lt;br /&gt;
&lt;br /&gt;
f)	Login as admin&lt;br /&gt;
&lt;br /&gt;
g)	Go to Navigation bar -&amp;gt; Administration -&amp;gt; Setup -&amp;gt; Menu Editor&lt;br /&gt;
&lt;br /&gt;
h)	In Menu Editor page, find “Assignment Group” row (it should be the last but six row)&lt;br /&gt;
&lt;br /&gt;
i)	You should see an up arrow at the left column of that row, click it.&lt;br /&gt;
&lt;br /&gt;
j)	Then the “Assignment Group” row goes up, click the 2nd column of that “Assignment Group” row, in which it should say “assignment_groups:list”&lt;br /&gt;
&lt;br /&gt;
k)	In the pop up page named “Editing controller_action” there’s a drop down box with label ”Permission”, choose “administer assignment” in that drop down box, then click button “Edit”&lt;br /&gt;
&lt;br /&gt;
l)	Create a student account, let’s say its name is “st1”.&lt;br /&gt;
&lt;br /&gt;
m)	Create 2 additional assignment groups (then we will have 3 assignment groups). Set the XP value of the “Default Group” to -1. Set the XP value of the “2nd Group” to 200. Set the XP value of the “3rd Group” to 3000. (XP value of an assignment group is the threshold to open that assignment room).&lt;br /&gt;
&lt;br /&gt;
n)	Create 5 assignment:&lt;br /&gt;
Please do it carefully and ONE BY ONE in order, set all deadlines to anytime LATER than May, 2013, select the “available to student” check button.&lt;br /&gt;
&lt;br /&gt;
	a1 : Group = Default Group | XP = 120 | directory =   /submitted_content/edit/1&lt;br /&gt;
&lt;br /&gt;
	a2 : Group = Default Group | XP = 200 | directory = /submitted_content/edit/2&lt;br /&gt;
&lt;br /&gt;
	a3 : Group = 2nd Group | XP = 2000 | directory = /submitted_content/edit/3&lt;br /&gt;
&lt;br /&gt;
	a4 : Group = 3rd Group | XP = 2000 | directory = /submitted_content/edit/4&lt;br /&gt;
&lt;br /&gt;
	a5 : Group = Default Group | XP = 2000 | directory = /submitted_content/edit/5&lt;br /&gt;
&lt;br /&gt;
add participant “st1” to  a1, a2, a3, a4 ( NOT for a5 )&lt;/div&gt;</summary>
		<author><name>Cyang14</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E629&amp;diff=74388</id>
		<title>CSC/ECE 517 Spring 2013/OSS E629</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E629&amp;diff=74388"/>
		<updated>2013-03-24T14:10:41Z</updated>

		<summary type="html">&lt;p&gt;Cyang14: /* Experience Point Report */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=E629: Integrating game frontend for Expertiza=&lt;br /&gt;
&lt;br /&gt;
==Project Introduction==&lt;br /&gt;
This is a game made for the [http://wikis.lib.ncsu.edu/index.php/Expertiza Expertiza] system (Ed Gehringer, NCSU). It transforms an assignment-based course into a &amp;quot;game&amp;quot; in which the player levels up and completes areas. The player doesn't receive grades; rather, he gains experience, and the professor would convert the experience values of the players at the end of the course into grades. The map is split up into distinct &amp;quot;rooms&amp;quot; unlocked by completing assignments; these are arrayed around a &amp;quot;center&amp;quot; in the middle of the map which contains portals to each room (portals which unlock as the player progresses).&lt;br /&gt;
&lt;br /&gt;
==Project Requirement==&lt;br /&gt;
Students will need to develop a page that runs the Expertiza game in JavaScript when a user is logged in and calls the code from E407 to get the JSON string to be parsed by the frontend.&lt;br /&gt;
&lt;br /&gt;
The page should basically load the frontend [http://en.wikipedia.org/wiki/JavaScript JavaScript] and point it to the [http://en.wikipedia.org/wiki/JSON JSON] string (a URL that will check to make sure that the current user is logged in correctly).&lt;br /&gt;
&lt;br /&gt;
[https://docs.google.com/a/ncsu.edu/document/d/1SVNx7Eh6dUdrz2a9rGSgBxLkxAmuBUm8eu08nHEdjIg/edit# Project Description]&lt;br /&gt;
&lt;br /&gt;
==Our Work==&lt;br /&gt;
&lt;br /&gt;
===Experience Point Report===&lt;br /&gt;
In this game, the player doesn't receive grades; rather, he gains experience(XP), the most important thing about the XP points is that they can accumulate instead of just getting average as student’s final score. As an example, if a student scored 80 in the first several assignments may eventually lower his/her grade if he decides to do some extra work on extra assignment and got 70 in the extra assignment. The grade can either automatically generated by multiplying percentage score with the weight of assignment, it can also be set by the instructor manually. The professor would convert the experience values of the players at the end of the course into grades.&lt;br /&gt;
&lt;br /&gt;
The previous XP logic in JSON string generating module is a piece of static code which always just sends a manually set percentage score as the XP point. A part of our work is to rewrite this XP point system.&lt;br /&gt;
&lt;br /&gt;
The new XP point system is a weighted score system, where instructor can assign or automatically generate the XP point.&lt;br /&gt;
&lt;br /&gt;
===Assignment Group Modules===&lt;br /&gt;
Assignment group module is used to&lt;br /&gt;
&lt;br /&gt;
Previous assignment group from the previous project E407 is a fake module. In this version, the code to decide whether an assignment group is “available” to a student should appear in both frontend and backend, but neither of them works. So we rewrite it and assign privileges to it.&lt;br /&gt;
&lt;br /&gt;
The following is the code for the assignment group module:&lt;br /&gt;
&lt;br /&gt;
===JavaScript Code Reconstruction===&lt;br /&gt;
When the game is running on the Expertiza system, Expertiza should send the game the info it needs to generate the level and game state at load-time. The page should basically load the frontend JavaScript and point it to the JSON string.&lt;br /&gt;
&lt;br /&gt;
Previous system get JSON by [http://en.wikipedia.org/wiki/Polling_(computer_science) polling] the server. In this strategy,  the computer does nothing other than check the status of the backend until it is ready, at which point the device is accessed.  In order to improve the efficiency of the CPU, we introduced [http://en.wikipedia.org/wiki/Event-driven_programming Event Driven Programming (EDP)] into our design.   In EDP, the flow of the program is determined by events. In our case, a callback closure is called when the JSON data hit the frontend.&lt;br /&gt;
&lt;br /&gt;
We also Change the ig module a little bit to support visiting module variable in closure.&lt;br /&gt;
&lt;br /&gt;
The related code is as following:&lt;br /&gt;
&lt;br /&gt;
===Fixing Mega Bugs===&lt;br /&gt;
====Uncaught Exception====&lt;br /&gt;
Previous JSON backend throws an uncaught exception when the user has an unsubmitted assignment and he is participating in that assignment.&lt;br /&gt;
&lt;br /&gt;
We fix this bug by&lt;br /&gt;
&lt;br /&gt;
The following is the related code:&lt;br /&gt;
&lt;br /&gt;
====Gate Name Error====&lt;br /&gt;
Previous JSON backend didn’t set gate name correctly. Thus when having multiple assignment group (&amp;gt;3) and at least one hub threshold is higher than current user’s XP point, JavaScript frontend falls into infinite loop.&lt;br /&gt;
&lt;br /&gt;
We fix this bug by&lt;br /&gt;
&lt;br /&gt;
The following is the related code:&lt;br /&gt;
&lt;br /&gt;
====Assignment Group Error====&lt;br /&gt;
In previous JSON backend, if an assignment group has X assignment in it, it will return X copy of that an assignment group, thus there’ll be X*X assignment in total. X times greater than it should be.&lt;br /&gt;
&lt;br /&gt;
We fix this bug by&lt;br /&gt;
&lt;br /&gt;
The following is the related code:&lt;br /&gt;
&lt;br /&gt;
===Fixing Minor Bugs===&lt;br /&gt;
* Previous backend send all assignment data to frontend even if the student is not participate in the assignment. &lt;br /&gt;
&lt;br /&gt;
* Previous JSON backend didn’t set MIME type correctly. That result in frontend code can only use low layer API to capture JSON data, when using high layer API, a silent error will occur.&lt;br /&gt;
&lt;br /&gt;
* Previous MIME type result in IE and Chrome has different behavior.&lt;br /&gt;
&lt;br /&gt;
* Sending a JSON string “null” to frontend will make it stop responding.&lt;br /&gt;
&lt;br /&gt;
==General Introduction to the System Setup==&lt;br /&gt;
Please follow these steps to setup the program:&lt;br /&gt;
&lt;br /&gt;
a)	Download and deploy the code from &lt;br /&gt;
http://commondatastorage.googleapis.com/silverwzw/expertiza-json.zip&lt;br /&gt;
&lt;br /&gt;
b)	Drop the database “pg_development”&lt;br /&gt;
&lt;br /&gt;
c)	Create an empty database “pg_development”&lt;br /&gt;
&lt;br /&gt;
d)	Run “db:migrate”&lt;br /&gt;
&lt;br /&gt;
e)	Start the server&lt;br /&gt;
&lt;br /&gt;
f)	Login as admin&lt;br /&gt;
&lt;br /&gt;
g)	Go to Navigation bar -&amp;gt; Administration -&amp;gt; Setup -&amp;gt; Menu Editor&lt;br /&gt;
&lt;br /&gt;
h)	In Menu Editor page, find “Assignment Group” row (it should be the last but six row)&lt;br /&gt;
&lt;br /&gt;
i)	You should see an up arrow at the left column of that row, click it.&lt;br /&gt;
&lt;br /&gt;
j)	Then the “Assignment Group” row goes up, click the 2nd column of that “Assignment Group” row, in which it should say “assignment_groups:list”&lt;br /&gt;
&lt;br /&gt;
k)	In the pop up page named “Editing controller_action” there’s a drop down box with label ”Permission”, choose “administer assignment” in that drop down box, then click button “Edit”&lt;br /&gt;
&lt;br /&gt;
l)	Create a student account, let’s say its name is “st1”.&lt;br /&gt;
&lt;br /&gt;
m)	Create 2 additional assignment groups (then we will have 3 assignment groups). Set the XP value of the “Default Group” to -1. Set the XP value of the “2nd Group” to 200. Set the XP value of the “3rd Group” to 3000. (XP value of an assignment group is the threshold to open that assignment room).&lt;br /&gt;
&lt;br /&gt;
n)	Create 5 assignment:&lt;br /&gt;
Please do it carefully and ONE BY ONE in order, set all deadlines to anytime LATER than May, 2013, select the “available to student” check button.&lt;br /&gt;
&lt;br /&gt;
	a1 : Group = Default Group | XP = 120 | directory =   /submitted_content/edit/1&lt;br /&gt;
&lt;br /&gt;
	a2 : Group = Default Group | XP = 200 | directory = /submitted_content/edit/2&lt;br /&gt;
&lt;br /&gt;
	a3 : Group = 2nd Group | XP = 2000 | directory = /submitted_content/edit/3&lt;br /&gt;
&lt;br /&gt;
	a4 : Group = 3rd Group | XP = 2000 | directory = /submitted_content/edit/4&lt;br /&gt;
&lt;br /&gt;
	a5 : Group = Default Group | XP = 2000 | directory = /submitted_content/edit/5&lt;br /&gt;
&lt;br /&gt;
add participant “st1” to  a1, a2, a3, a4 ( NOT for a5 )&lt;/div&gt;</summary>
		<author><name>Cyang14</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E629&amp;diff=74387</id>
		<title>CSC/ECE 517 Spring 2013/OSS E629</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E629&amp;diff=74387"/>
		<updated>2013-03-24T14:10:09Z</updated>

		<summary type="html">&lt;p&gt;Cyang14: /* Experience Point Report */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=E629: Integrating game frontend for Expertiza=&lt;br /&gt;
&lt;br /&gt;
==Project Introduction==&lt;br /&gt;
This is a game made for the [http://wikis.lib.ncsu.edu/index.php/Expertiza Expertiza] system (Ed Gehringer, NCSU). It transforms an assignment-based course into a &amp;quot;game&amp;quot; in which the player levels up and completes areas. The player doesn't receive grades; rather, he gains experience, and the professor would convert the experience values of the players at the end of the course into grades. The map is split up into distinct &amp;quot;rooms&amp;quot; unlocked by completing assignments; these are arrayed around a &amp;quot;center&amp;quot; in the middle of the map which contains portals to each room (portals which unlock as the player progresses).&lt;br /&gt;
&lt;br /&gt;
==Project Requirement==&lt;br /&gt;
Students will need to develop a page that runs the Expertiza game in JavaScript when a user is logged in and calls the code from E407 to get the JSON string to be parsed by the frontend.&lt;br /&gt;
&lt;br /&gt;
The page should basically load the frontend [http://en.wikipedia.org/wiki/JavaScript JavaScript] and point it to the [http://en.wikipedia.org/wiki/JSON JSON] string (a URL that will check to make sure that the current user is logged in correctly).&lt;br /&gt;
&lt;br /&gt;
[https://docs.google.com/a/ncsu.edu/document/d/1SVNx7Eh6dUdrz2a9rGSgBxLkxAmuBUm8eu08nHEdjIg/edit# Project Description]&lt;br /&gt;
&lt;br /&gt;
==Our Work==&lt;br /&gt;
&lt;br /&gt;
===Experience Point Report===&lt;br /&gt;
In this game, the player doesn't receive grades; rather, he gains experience(XP), ), the most important thing about the XP points is that they can accumulate instead of just getting average as student’s final score. As an example, if a student scored 80 in the first several assignments may eventually lower his/her grade if he decides to do some extra work on extra assignment and got 70 in the extra assignment. The grade can either automatically generated by multiplying percentage score with the weight of assignment, it can also be set by the instructor manually. The professor would convert the experience values of the players at the end of the course into grades.&lt;br /&gt;
&lt;br /&gt;
The previous XP logic in JSON string generating module is a piece of static code which always just sends a manually set percentage score as the XP point. A part of our work is to rewrite this XP point system.&lt;br /&gt;
&lt;br /&gt;
The new XP point system is a weighted score system, where instructor can assign or automatically generate the XP point.&lt;br /&gt;
&lt;br /&gt;
===Assignment Group Modules===&lt;br /&gt;
Assignment group module is used to&lt;br /&gt;
&lt;br /&gt;
Previous assignment group from the previous project E407 is a fake module. In this version, the code to decide whether an assignment group is “available” to a student should appear in both frontend and backend, but neither of them works. So we rewrite it and assign privileges to it.&lt;br /&gt;
&lt;br /&gt;
The following is the code for the assignment group module:&lt;br /&gt;
&lt;br /&gt;
===JavaScript Code Reconstruction===&lt;br /&gt;
When the game is running on the Expertiza system, Expertiza should send the game the info it needs to generate the level and game state at load-time. The page should basically load the frontend JavaScript and point it to the JSON string.&lt;br /&gt;
&lt;br /&gt;
Previous system get JSON by [http://en.wikipedia.org/wiki/Polling_(computer_science) polling] the server. In this strategy,  the computer does nothing other than check the status of the backend until it is ready, at which point the device is accessed.  In order to improve the efficiency of the CPU, we introduced [http://en.wikipedia.org/wiki/Event-driven_programming Event Driven Programming (EDP)] into our design.   In EDP, the flow of the program is determined by events. In our case, a callback closure is called when the JSON data hit the frontend.&lt;br /&gt;
&lt;br /&gt;
We also Change the ig module a little bit to support visiting module variable in closure.&lt;br /&gt;
&lt;br /&gt;
The related code is as following:&lt;br /&gt;
&lt;br /&gt;
===Fixing Mega Bugs===&lt;br /&gt;
====Uncaught Exception====&lt;br /&gt;
Previous JSON backend throws an uncaught exception when the user has an unsubmitted assignment and he is participating in that assignment.&lt;br /&gt;
&lt;br /&gt;
We fix this bug by&lt;br /&gt;
&lt;br /&gt;
The following is the related code:&lt;br /&gt;
&lt;br /&gt;
====Gate Name Error====&lt;br /&gt;
Previous JSON backend didn’t set gate name correctly. Thus when having multiple assignment group (&amp;gt;3) and at least one hub threshold is higher than current user’s XP point, JavaScript frontend falls into infinite loop.&lt;br /&gt;
&lt;br /&gt;
We fix this bug by&lt;br /&gt;
&lt;br /&gt;
The following is the related code:&lt;br /&gt;
&lt;br /&gt;
====Assignment Group Error====&lt;br /&gt;
In previous JSON backend, if an assignment group has X assignment in it, it will return X copy of that an assignment group, thus there’ll be X*X assignment in total. X times greater than it should be.&lt;br /&gt;
&lt;br /&gt;
We fix this bug by&lt;br /&gt;
&lt;br /&gt;
The following is the related code:&lt;br /&gt;
&lt;br /&gt;
===Fixing Minor Bugs===&lt;br /&gt;
* Previous backend send all assignment data to frontend even if the student is not participate in the assignment. &lt;br /&gt;
&lt;br /&gt;
* Previous JSON backend didn’t set MIME type correctly. That result in frontend code can only use low layer API to capture JSON data, when using high layer API, a silent error will occur.&lt;br /&gt;
&lt;br /&gt;
* Previous MIME type result in IE and Chrome has different behavior.&lt;br /&gt;
&lt;br /&gt;
* Sending a JSON string “null” to frontend will make it stop responding.&lt;br /&gt;
&lt;br /&gt;
==General Introduction to the System Setup==&lt;br /&gt;
Please follow these steps to setup the program:&lt;br /&gt;
&lt;br /&gt;
a)	Download and deploy the code from &lt;br /&gt;
http://commondatastorage.googleapis.com/silverwzw/expertiza-json.zip&lt;br /&gt;
&lt;br /&gt;
b)	Drop the database “pg_development”&lt;br /&gt;
&lt;br /&gt;
c)	Create an empty database “pg_development”&lt;br /&gt;
&lt;br /&gt;
d)	Run “db:migrate”&lt;br /&gt;
&lt;br /&gt;
e)	Start the server&lt;br /&gt;
&lt;br /&gt;
f)	Login as admin&lt;br /&gt;
&lt;br /&gt;
g)	Go to Navigation bar -&amp;gt; Administration -&amp;gt; Setup -&amp;gt; Menu Editor&lt;br /&gt;
&lt;br /&gt;
h)	In Menu Editor page, find “Assignment Group” row (it should be the last but six row)&lt;br /&gt;
&lt;br /&gt;
i)	You should see an up arrow at the left column of that row, click it.&lt;br /&gt;
&lt;br /&gt;
j)	Then the “Assignment Group” row goes up, click the 2nd column of that “Assignment Group” row, in which it should say “assignment_groups:list”&lt;br /&gt;
&lt;br /&gt;
k)	In the pop up page named “Editing controller_action” there’s a drop down box with label ”Permission”, choose “administer assignment” in that drop down box, then click button “Edit”&lt;br /&gt;
&lt;br /&gt;
l)	Create a student account, let’s say its name is “st1”.&lt;br /&gt;
&lt;br /&gt;
m)	Create 2 additional assignment groups (then we will have 3 assignment groups). Set the XP value of the “Default Group” to -1. Set the XP value of the “2nd Group” to 200. Set the XP value of the “3rd Group” to 3000. (XP value of an assignment group is the threshold to open that assignment room).&lt;br /&gt;
&lt;br /&gt;
n)	Create 5 assignment:&lt;br /&gt;
Please do it carefully and ONE BY ONE in order, set all deadlines to anytime LATER than May, 2013, select the “available to student” check button.&lt;br /&gt;
&lt;br /&gt;
	a1 : Group = Default Group | XP = 120 | directory =   /submitted_content/edit/1&lt;br /&gt;
&lt;br /&gt;
	a2 : Group = Default Group | XP = 200 | directory = /submitted_content/edit/2&lt;br /&gt;
&lt;br /&gt;
	a3 : Group = 2nd Group | XP = 2000 | directory = /submitted_content/edit/3&lt;br /&gt;
&lt;br /&gt;
	a4 : Group = 3rd Group | XP = 2000 | directory = /submitted_content/edit/4&lt;br /&gt;
&lt;br /&gt;
	a5 : Group = Default Group | XP = 2000 | directory = /submitted_content/edit/5&lt;br /&gt;
&lt;br /&gt;
add participant “st1” to  a1, a2, a3, a4 ( NOT for a5 )&lt;/div&gt;</summary>
		<author><name>Cyang14</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E629&amp;diff=74386</id>
		<title>CSC/ECE 517 Spring 2013/OSS E629</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E629&amp;diff=74386"/>
		<updated>2013-03-23T18:16:07Z</updated>

		<summary type="html">&lt;p&gt;Cyang14: /* JavaScript Code Reconstruction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=E629: Integrating game frontend for Expertiza=&lt;br /&gt;
&lt;br /&gt;
==Project Introduction==&lt;br /&gt;
This is a game made for the [http://wikis.lib.ncsu.edu/index.php/Expertiza Expertiza] system (Ed Gehringer, NCSU). It transforms an assignment-based course into a &amp;quot;game&amp;quot; in which the player levels up and completes areas. The player doesn't receive grades; rather, he gains experience, and the professor would convert the experience values of the players at the end of the course into grades. The map is split up into distinct &amp;quot;rooms&amp;quot; unlocked by completing assignments; these are arrayed around a &amp;quot;center&amp;quot; in the middle of the map which contains portals to each room (portals which unlock as the player progresses).&lt;br /&gt;
&lt;br /&gt;
==Project Requirement==&lt;br /&gt;
Students will need to develop a page that runs the Expertiza game in JavaScript when a user is logged in and calls the code from E407 to get the JSON string to be parsed by the frontend.&lt;br /&gt;
&lt;br /&gt;
The page should basically load the frontend [http://en.wikipedia.org/wiki/JavaScript JavaScript] and point it to the [http://en.wikipedia.org/wiki/JSON JSON] string (a URL that will check to make sure that the current user is logged in correctly).&lt;br /&gt;
&lt;br /&gt;
[https://docs.google.com/a/ncsu.edu/document/d/1SVNx7Eh6dUdrz2a9rGSgBxLkxAmuBUm8eu08nHEdjIg/edit# Project Description]&lt;br /&gt;
&lt;br /&gt;
==Our Work==&lt;br /&gt;
&lt;br /&gt;
===Experience Point Report===&lt;br /&gt;
In this game, the player doesn't receive grades; rather, he gains experience(XP), and the professor would convert the experience values of the players at the end of the course into grades.&lt;br /&gt;
&lt;br /&gt;
The Previous XP is a fake one, they just send the &amp;quot;grade&amp;quot; score as the XP point. So one part of work is to rewrite this part.&lt;br /&gt;
&lt;br /&gt;
The new XP point system is a weighted score system, where instructor can assign or automatically generate the XP point.&lt;br /&gt;
&lt;br /&gt;
The following is the code for XP point report system:&lt;br /&gt;
&lt;br /&gt;
===Assignment Group Modules===&lt;br /&gt;
Assignment group module is used to&lt;br /&gt;
&lt;br /&gt;
Previous assignment group from the previous project E407 is a fake module. In this version, the code to decide whether an assignment group is “available” to a student should appear in both frontend and backend, but neither of them works. So we rewrite it and assign privileges to it.&lt;br /&gt;
&lt;br /&gt;
The following is the code for the assignment group module:&lt;br /&gt;
&lt;br /&gt;
===JavaScript Code Reconstruction===&lt;br /&gt;
When the game is running on the Expertiza system, Expertiza should send the game the info it needs to generate the level and game state at load-time. The page should basically load the frontend JavaScript and point it to the JSON string.&lt;br /&gt;
&lt;br /&gt;
Previous system get JSON by [http://en.wikipedia.org/wiki/Polling_(computer_science) polling] the server. In this strategy,  the computer does nothing other than check the status of the backend until it is ready, at which point the device is accessed.  In order to improve the efficiency of the CPU, we introduced [http://en.wikipedia.org/wiki/Event-driven_programming Event Driven Programming (EDP)] into our design.   In EDP, the flow of the program is determined by events. In our case, a callback closure is called when the JSON data hit the frontend.&lt;br /&gt;
&lt;br /&gt;
We also Change the ig module a little bit to support visiting module variable in closure.&lt;br /&gt;
&lt;br /&gt;
The related code is as following:&lt;br /&gt;
&lt;br /&gt;
===Fixing Mega Bugs===&lt;br /&gt;
====Uncaught Exception====&lt;br /&gt;
Previous JSON backend throws an uncaught exception when the user has an unsubmitted assignment and he is participating in that assignment.&lt;br /&gt;
&lt;br /&gt;
We fix this bug by&lt;br /&gt;
&lt;br /&gt;
The following is the related code:&lt;br /&gt;
&lt;br /&gt;
====Gate Name Error====&lt;br /&gt;
Previous JSON backend didn’t set gate name correctly. Thus when having multiple assignment group (&amp;gt;3) and at least one hub threshold is higher than current user’s XP point, JavaScript frontend falls into infinite loop.&lt;br /&gt;
&lt;br /&gt;
We fix this bug by&lt;br /&gt;
&lt;br /&gt;
The following is the related code:&lt;br /&gt;
&lt;br /&gt;
====Assignment Group Error====&lt;br /&gt;
In previous JSON backend, if an assignment group has X assignment in it, it will return X copy of that an assignment group, thus there’ll be X*X assignment in total. X times greater than it should be.&lt;br /&gt;
&lt;br /&gt;
We fix this bug by&lt;br /&gt;
&lt;br /&gt;
The following is the related code:&lt;br /&gt;
&lt;br /&gt;
===Fixing Minor Bugs===&lt;br /&gt;
* Previous backend send all assignment data to frontend even if the student is not participate in the assignment. &lt;br /&gt;
&lt;br /&gt;
* Previous JSON backend didn’t set MIME type correctly. That result in frontend code can only use low layer API to capture JSON data, when using high layer API, a silent error will occur.&lt;br /&gt;
&lt;br /&gt;
* Previous MIME type result in IE and Chrome has different behavior.&lt;br /&gt;
&lt;br /&gt;
* Sending a JSON string “null” to frontend will make it stop responding.&lt;br /&gt;
&lt;br /&gt;
==General Introduction to the System Setup==&lt;br /&gt;
Please follow these steps to setup the program:&lt;br /&gt;
&lt;br /&gt;
a)	Download and deploy the code from &lt;br /&gt;
http://commondatastorage.googleapis.com/silverwzw/expertiza-json.zip&lt;br /&gt;
&lt;br /&gt;
b)	Drop the database “pg_development”&lt;br /&gt;
&lt;br /&gt;
c)	Create an empty database “pg_development”&lt;br /&gt;
&lt;br /&gt;
d)	Run “db:migrate”&lt;br /&gt;
&lt;br /&gt;
e)	Start the server&lt;br /&gt;
&lt;br /&gt;
f)	Login as admin&lt;br /&gt;
&lt;br /&gt;
g)	Go to Navigation bar -&amp;gt; Administration -&amp;gt; Setup -&amp;gt; Menu Editor&lt;br /&gt;
&lt;br /&gt;
h)	In Menu Editor page, find “Assignment Group” row (it should be the last but six row)&lt;br /&gt;
&lt;br /&gt;
i)	You should see an up arrow at the left column of that row, click it.&lt;br /&gt;
&lt;br /&gt;
j)	Then the “Assignment Group” row goes up, click the 2nd column of that “Assignment Group” row, in which it should say “assignment_groups:list”&lt;br /&gt;
&lt;br /&gt;
k)	In the pop up page named “Editing controller_action” there’s a drop down box with label ”Permission”, choose “administer assignment” in that drop down box, then click button “Edit”&lt;br /&gt;
&lt;br /&gt;
l)	Create a student account, let’s say its name is “st1”.&lt;br /&gt;
&lt;br /&gt;
m)	Create 2 additional assignment groups (then we will have 3 assignment groups). Set the XP value of the “Default Group” to -1. Set the XP value of the “2nd Group” to 200. Set the XP value of the “3rd Group” to 3000. (XP value of an assignment group is the threshold to open that assignment room).&lt;br /&gt;
&lt;br /&gt;
n)	Create 5 assignment:&lt;br /&gt;
Please do it carefully and ONE BY ONE in order, set all deadlines to anytime LATER than May, 2013, select the “available to student” check button.&lt;br /&gt;
&lt;br /&gt;
	a1 : Group = Default Group | XP = 120 | directory =   /submitted_content/edit/1&lt;br /&gt;
&lt;br /&gt;
	a2 : Group = Default Group | XP = 200 | directory = /submitted_content/edit/2&lt;br /&gt;
&lt;br /&gt;
	a3 : Group = 2nd Group | XP = 2000 | directory = /submitted_content/edit/3&lt;br /&gt;
&lt;br /&gt;
	a4 : Group = 3rd Group | XP = 2000 | directory = /submitted_content/edit/4&lt;br /&gt;
&lt;br /&gt;
	a5 : Group = Default Group | XP = 2000 | directory = /submitted_content/edit/5&lt;br /&gt;
&lt;br /&gt;
add participant “st1” to  a1, a2, a3, a4 ( NOT for a5 )&lt;/div&gt;</summary>
		<author><name>Cyang14</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E629&amp;diff=74385</id>
		<title>CSC/ECE 517 Spring 2013/OSS E629</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E629&amp;diff=74385"/>
		<updated>2013-03-23T18:13:48Z</updated>

		<summary type="html">&lt;p&gt;Cyang14: /* Project Requirement */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=E629: Integrating game frontend for Expertiza=&lt;br /&gt;
&lt;br /&gt;
==Project Introduction==&lt;br /&gt;
This is a game made for the [http://wikis.lib.ncsu.edu/index.php/Expertiza Expertiza] system (Ed Gehringer, NCSU). It transforms an assignment-based course into a &amp;quot;game&amp;quot; in which the player levels up and completes areas. The player doesn't receive grades; rather, he gains experience, and the professor would convert the experience values of the players at the end of the course into grades. The map is split up into distinct &amp;quot;rooms&amp;quot; unlocked by completing assignments; these are arrayed around a &amp;quot;center&amp;quot; in the middle of the map which contains portals to each room (portals which unlock as the player progresses).&lt;br /&gt;
&lt;br /&gt;
==Project Requirement==&lt;br /&gt;
Students will need to develop a page that runs the Expertiza game in JavaScript when a user is logged in and calls the code from E407 to get the JSON string to be parsed by the frontend.&lt;br /&gt;
&lt;br /&gt;
The page should basically load the frontend [http://en.wikipedia.org/wiki/JavaScript JavaScript] and point it to the [http://en.wikipedia.org/wiki/JSON JSON] string (a URL that will check to make sure that the current user is logged in correctly).&lt;br /&gt;
&lt;br /&gt;
[https://docs.google.com/a/ncsu.edu/document/d/1SVNx7Eh6dUdrz2a9rGSgBxLkxAmuBUm8eu08nHEdjIg/edit# Project Description]&lt;br /&gt;
&lt;br /&gt;
==Our Work==&lt;br /&gt;
&lt;br /&gt;
===Experience Point Report===&lt;br /&gt;
In this game, the player doesn't receive grades; rather, he gains experience(XP), and the professor would convert the experience values of the players at the end of the course into grades.&lt;br /&gt;
&lt;br /&gt;
The Previous XP is a fake one, they just send the &amp;quot;grade&amp;quot; score as the XP point. So one part of work is to rewrite this part.&lt;br /&gt;
&lt;br /&gt;
The new XP point system is a weighted score system, where instructor can assign or automatically generate the XP point.&lt;br /&gt;
&lt;br /&gt;
The following is the code for XP point report system:&lt;br /&gt;
&lt;br /&gt;
===Assignment Group Modules===&lt;br /&gt;
Assignment group module is used to&lt;br /&gt;
&lt;br /&gt;
Previous assignment group from the previous project E407 is a fake module. In this version, the code to decide whether an assignment group is “available” to a student should appear in both frontend and backend, but neither of them works. So we rewrite it and assign privileges to it.&lt;br /&gt;
&lt;br /&gt;
The following is the code for the assignment group module:&lt;br /&gt;
&lt;br /&gt;
===JavaScript Code Reconstruction===&lt;br /&gt;
When the game is running on the Expertiza system, Expertiza should send the game the info it needs to generate the level and game state at load-time. The page should basically load the frontend JavaScript and point it to the JSON string.&lt;br /&gt;
&lt;br /&gt;
Previous system get JSON by polling the server. In this strategy,  the computer does nothing other than check the status of the backend until it is ready, at which point the device is accessed.  In order to improve the efficiency of the CPU, we introduced Event Driven Programming (EDP) into our design.   In EDP, the flow of the program is determined by events. In our case, a callback closure is called when the JSON data hit the frontend.&lt;br /&gt;
&lt;br /&gt;
We also Change the ig module a little bit to support visiting module variable in closure.&lt;br /&gt;
&lt;br /&gt;
The related code is as following:&lt;br /&gt;
&lt;br /&gt;
===Fixing Mega Bugs===&lt;br /&gt;
====Uncaught Exception====&lt;br /&gt;
Previous JSON backend throws an uncaught exception when the user has an unsubmitted assignment and he is participating in that assignment.&lt;br /&gt;
&lt;br /&gt;
We fix this bug by&lt;br /&gt;
&lt;br /&gt;
The following is the related code:&lt;br /&gt;
&lt;br /&gt;
====Gate Name Error====&lt;br /&gt;
Previous JSON backend didn’t set gate name correctly. Thus when having multiple assignment group (&amp;gt;3) and at least one hub threshold is higher than current user’s XP point, JavaScript frontend falls into infinite loop.&lt;br /&gt;
&lt;br /&gt;
We fix this bug by&lt;br /&gt;
&lt;br /&gt;
The following is the related code:&lt;br /&gt;
&lt;br /&gt;
====Assignment Group Error====&lt;br /&gt;
In previous JSON backend, if an assignment group has X assignment in it, it will return X copy of that an assignment group, thus there’ll be X*X assignment in total. X times greater than it should be.&lt;br /&gt;
&lt;br /&gt;
We fix this bug by&lt;br /&gt;
&lt;br /&gt;
The following is the related code:&lt;br /&gt;
&lt;br /&gt;
===Fixing Minor Bugs===&lt;br /&gt;
* Previous backend send all assignment data to frontend even if the student is not participate in the assignment. &lt;br /&gt;
&lt;br /&gt;
* Previous JSON backend didn’t set MIME type correctly. That result in frontend code can only use low layer API to capture JSON data, when using high layer API, a silent error will occur.&lt;br /&gt;
&lt;br /&gt;
* Previous MIME type result in IE and Chrome has different behavior.&lt;br /&gt;
&lt;br /&gt;
* Sending a JSON string “null” to frontend will make it stop responding.&lt;br /&gt;
&lt;br /&gt;
==General Introduction to the System Setup==&lt;br /&gt;
Please follow these steps to setup the program:&lt;br /&gt;
&lt;br /&gt;
a)	Download and deploy the code from &lt;br /&gt;
http://commondatastorage.googleapis.com/silverwzw/expertiza-json.zip&lt;br /&gt;
&lt;br /&gt;
b)	Drop the database “pg_development”&lt;br /&gt;
&lt;br /&gt;
c)	Create an empty database “pg_development”&lt;br /&gt;
&lt;br /&gt;
d)	Run “db:migrate”&lt;br /&gt;
&lt;br /&gt;
e)	Start the server&lt;br /&gt;
&lt;br /&gt;
f)	Login as admin&lt;br /&gt;
&lt;br /&gt;
g)	Go to Navigation bar -&amp;gt; Administration -&amp;gt; Setup -&amp;gt; Menu Editor&lt;br /&gt;
&lt;br /&gt;
h)	In Menu Editor page, find “Assignment Group” row (it should be the last but six row)&lt;br /&gt;
&lt;br /&gt;
i)	You should see an up arrow at the left column of that row, click it.&lt;br /&gt;
&lt;br /&gt;
j)	Then the “Assignment Group” row goes up, click the 2nd column of that “Assignment Group” row, in which it should say “assignment_groups:list”&lt;br /&gt;
&lt;br /&gt;
k)	In the pop up page named “Editing controller_action” there’s a drop down box with label ”Permission”, choose “administer assignment” in that drop down box, then click button “Edit”&lt;br /&gt;
&lt;br /&gt;
l)	Create a student account, let’s say its name is “st1”.&lt;br /&gt;
&lt;br /&gt;
m)	Create 2 additional assignment groups (then we will have 3 assignment groups). Set the XP value of the “Default Group” to -1. Set the XP value of the “2nd Group” to 200. Set the XP value of the “3rd Group” to 3000. (XP value of an assignment group is the threshold to open that assignment room).&lt;br /&gt;
&lt;br /&gt;
n)	Create 5 assignment:&lt;br /&gt;
Please do it carefully and ONE BY ONE in order, set all deadlines to anytime LATER than May, 2013, select the “available to student” check button.&lt;br /&gt;
&lt;br /&gt;
	a1 : Group = Default Group | XP = 120 | directory =   /submitted_content/edit/1&lt;br /&gt;
&lt;br /&gt;
	a2 : Group = Default Group | XP = 200 | directory = /submitted_content/edit/2&lt;br /&gt;
&lt;br /&gt;
	a3 : Group = 2nd Group | XP = 2000 | directory = /submitted_content/edit/3&lt;br /&gt;
&lt;br /&gt;
	a4 : Group = 3rd Group | XP = 2000 | directory = /submitted_content/edit/4&lt;br /&gt;
&lt;br /&gt;
	a5 : Group = Default Group | XP = 2000 | directory = /submitted_content/edit/5&lt;br /&gt;
&lt;br /&gt;
add participant “st1” to  a1, a2, a3, a4 ( NOT for a5 )&lt;/div&gt;</summary>
		<author><name>Cyang14</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E629&amp;diff=74384</id>
		<title>CSC/ECE 517 Spring 2013/OSS E629</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E629&amp;diff=74384"/>
		<updated>2013-03-23T18:10:39Z</updated>

		<summary type="html">&lt;p&gt;Cyang14: /* Project Introduction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=E629: Integrating game frontend for Expertiza=&lt;br /&gt;
&lt;br /&gt;
==Project Introduction==&lt;br /&gt;
This is a game made for the [http://wikis.lib.ncsu.edu/index.php/Expertiza Expertiza] system (Ed Gehringer, NCSU). It transforms an assignment-based course into a &amp;quot;game&amp;quot; in which the player levels up and completes areas. The player doesn't receive grades; rather, he gains experience, and the professor would convert the experience values of the players at the end of the course into grades. The map is split up into distinct &amp;quot;rooms&amp;quot; unlocked by completing assignments; these are arrayed around a &amp;quot;center&amp;quot; in the middle of the map which contains portals to each room (portals which unlock as the player progresses).&lt;br /&gt;
&lt;br /&gt;
==Project Requirement==&lt;br /&gt;
Students will need to develop a page that runs the Expertiza game in JavaScript when a user is logged in and calls the code from E407 to get the JSON string to be parsed by the frontend.&lt;br /&gt;
&lt;br /&gt;
The page should basically load the frontend JavaScript and point it to the JSON string (a URL that will check to make sure that the current user is logged in correctly).&lt;br /&gt;
&lt;br /&gt;
[https://docs.google.com/a/ncsu.edu/document/d/1SVNx7Eh6dUdrz2a9rGSgBxLkxAmuBUm8eu08nHEdjIg/edit# Project Description]&lt;br /&gt;
&lt;br /&gt;
==Our Work==&lt;br /&gt;
&lt;br /&gt;
===Experience Point Report===&lt;br /&gt;
In this game, the player doesn't receive grades; rather, he gains experience(XP), and the professor would convert the experience values of the players at the end of the course into grades.&lt;br /&gt;
&lt;br /&gt;
The Previous XP is a fake one, they just send the &amp;quot;grade&amp;quot; score as the XP point. So one part of work is to rewrite this part.&lt;br /&gt;
&lt;br /&gt;
The new XP point system is a weighted score system, where instructor can assign or automatically generate the XP point.&lt;br /&gt;
&lt;br /&gt;
The following is the code for XP point report system:&lt;br /&gt;
&lt;br /&gt;
===Assignment Group Modules===&lt;br /&gt;
Assignment group module is used to&lt;br /&gt;
&lt;br /&gt;
Previous assignment group from the previous project E407 is a fake module. In this version, the code to decide whether an assignment group is “available” to a student should appear in both frontend and backend, but neither of them works. So we rewrite it and assign privileges to it.&lt;br /&gt;
&lt;br /&gt;
The following is the code for the assignment group module:&lt;br /&gt;
&lt;br /&gt;
===JavaScript Code Reconstruction===&lt;br /&gt;
When the game is running on the Expertiza system, Expertiza should send the game the info it needs to generate the level and game state at load-time. The page should basically load the frontend JavaScript and point it to the JSON string.&lt;br /&gt;
&lt;br /&gt;
Previous system get JSON by polling the server. In this strategy,  the computer does nothing other than check the status of the backend until it is ready, at which point the device is accessed.  In order to improve the efficiency of the CPU, we introduced Event Driven Programming (EDP) into our design.   In EDP, the flow of the program is determined by events. In our case, a callback closure is called when the JSON data hit the frontend.&lt;br /&gt;
&lt;br /&gt;
We also Change the ig module a little bit to support visiting module variable in closure.&lt;br /&gt;
&lt;br /&gt;
The related code is as following:&lt;br /&gt;
&lt;br /&gt;
===Fixing Mega Bugs===&lt;br /&gt;
====Uncaught Exception====&lt;br /&gt;
Previous JSON backend throws an uncaught exception when the user has an unsubmitted assignment and he is participating in that assignment.&lt;br /&gt;
&lt;br /&gt;
We fix this bug by&lt;br /&gt;
&lt;br /&gt;
The following is the related code:&lt;br /&gt;
&lt;br /&gt;
====Gate Name Error====&lt;br /&gt;
Previous JSON backend didn’t set gate name correctly. Thus when having multiple assignment group (&amp;gt;3) and at least one hub threshold is higher than current user’s XP point, JavaScript frontend falls into infinite loop.&lt;br /&gt;
&lt;br /&gt;
We fix this bug by&lt;br /&gt;
&lt;br /&gt;
The following is the related code:&lt;br /&gt;
&lt;br /&gt;
====Assignment Group Error====&lt;br /&gt;
In previous JSON backend, if an assignment group has X assignment in it, it will return X copy of that an assignment group, thus there’ll be X*X assignment in total. X times greater than it should be.&lt;br /&gt;
&lt;br /&gt;
We fix this bug by&lt;br /&gt;
&lt;br /&gt;
The following is the related code:&lt;br /&gt;
&lt;br /&gt;
===Fixing Minor Bugs===&lt;br /&gt;
* Previous backend send all assignment data to frontend even if the student is not participate in the assignment. &lt;br /&gt;
&lt;br /&gt;
* Previous JSON backend didn’t set MIME type correctly. That result in frontend code can only use low layer API to capture JSON data, when using high layer API, a silent error will occur.&lt;br /&gt;
&lt;br /&gt;
* Previous MIME type result in IE and Chrome has different behavior.&lt;br /&gt;
&lt;br /&gt;
* Sending a JSON string “null” to frontend will make it stop responding.&lt;br /&gt;
&lt;br /&gt;
==General Introduction to the System Setup==&lt;br /&gt;
Please follow these steps to setup the program:&lt;br /&gt;
&lt;br /&gt;
a)	Download and deploy the code from &lt;br /&gt;
http://commondatastorage.googleapis.com/silverwzw/expertiza-json.zip&lt;br /&gt;
&lt;br /&gt;
b)	Drop the database “pg_development”&lt;br /&gt;
&lt;br /&gt;
c)	Create an empty database “pg_development”&lt;br /&gt;
&lt;br /&gt;
d)	Run “db:migrate”&lt;br /&gt;
&lt;br /&gt;
e)	Start the server&lt;br /&gt;
&lt;br /&gt;
f)	Login as admin&lt;br /&gt;
&lt;br /&gt;
g)	Go to Navigation bar -&amp;gt; Administration -&amp;gt; Setup -&amp;gt; Menu Editor&lt;br /&gt;
&lt;br /&gt;
h)	In Menu Editor page, find “Assignment Group” row (it should be the last but six row)&lt;br /&gt;
&lt;br /&gt;
i)	You should see an up arrow at the left column of that row, click it.&lt;br /&gt;
&lt;br /&gt;
j)	Then the “Assignment Group” row goes up, click the 2nd column of that “Assignment Group” row, in which it should say “assignment_groups:list”&lt;br /&gt;
&lt;br /&gt;
k)	In the pop up page named “Editing controller_action” there’s a drop down box with label ”Permission”, choose “administer assignment” in that drop down box, then click button “Edit”&lt;br /&gt;
&lt;br /&gt;
l)	Create a student account, let’s say its name is “st1”.&lt;br /&gt;
&lt;br /&gt;
m)	Create 2 additional assignment groups (then we will have 3 assignment groups). Set the XP value of the “Default Group” to -1. Set the XP value of the “2nd Group” to 200. Set the XP value of the “3rd Group” to 3000. (XP value of an assignment group is the threshold to open that assignment room).&lt;br /&gt;
&lt;br /&gt;
n)	Create 5 assignment:&lt;br /&gt;
Please do it carefully and ONE BY ONE in order, set all deadlines to anytime LATER than May, 2013, select the “available to student” check button.&lt;br /&gt;
&lt;br /&gt;
	a1 : Group = Default Group | XP = 120 | directory =   /submitted_content/edit/1&lt;br /&gt;
&lt;br /&gt;
	a2 : Group = Default Group | XP = 200 | directory = /submitted_content/edit/2&lt;br /&gt;
&lt;br /&gt;
	a3 : Group = 2nd Group | XP = 2000 | directory = /submitted_content/edit/3&lt;br /&gt;
&lt;br /&gt;
	a4 : Group = 3rd Group | XP = 2000 | directory = /submitted_content/edit/4&lt;br /&gt;
&lt;br /&gt;
	a5 : Group = Default Group | XP = 2000 | directory = /submitted_content/edit/5&lt;br /&gt;
&lt;br /&gt;
add participant “st1” to  a1, a2, a3, a4 ( NOT for a5 )&lt;/div&gt;</summary>
		<author><name>Cyang14</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E629&amp;diff=74383</id>
		<title>CSC/ECE 517 Spring 2013/OSS E629</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E629&amp;diff=74383"/>
		<updated>2013-03-23T18:05:11Z</updated>

		<summary type="html">&lt;p&gt;Cyang14: /* General Introduction to the System Setup */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=E629: Integrating game frontend for Expertiza=&lt;br /&gt;
&lt;br /&gt;
==Project Introduction==&lt;br /&gt;
This is a game made for the Expertiza system (Ed Gehringer, NCSU). It transforms an assignment-based course into a &amp;quot;game&amp;quot; in which the player levels up and completes areas. The player doesn't receive grades; rather, he gains experience, and the professor would convert the experience values of the players at the end of the course into grades. The map is split up into distinct &amp;quot;rooms&amp;quot; unlocked by completing assignments; these are arrayed around a &amp;quot;center&amp;quot; in the middle of the map which contains portals to each room (portals which unlock as the player progresses).&lt;br /&gt;
&lt;br /&gt;
==Project Requirement==&lt;br /&gt;
Students will need to develop a page that runs the Expertiza game in JavaScript when a user is logged in and calls the code from E407 to get the JSON string to be parsed by the frontend.&lt;br /&gt;
&lt;br /&gt;
The page should basically load the frontend JavaScript and point it to the JSON string (a URL that will check to make sure that the current user is logged in correctly).&lt;br /&gt;
&lt;br /&gt;
[https://docs.google.com/a/ncsu.edu/document/d/1SVNx7Eh6dUdrz2a9rGSgBxLkxAmuBUm8eu08nHEdjIg/edit# Project Description]&lt;br /&gt;
&lt;br /&gt;
==Our Work==&lt;br /&gt;
&lt;br /&gt;
===Experience Point Report===&lt;br /&gt;
In this game, the player doesn't receive grades; rather, he gains experience(XP), and the professor would convert the experience values of the players at the end of the course into grades.&lt;br /&gt;
&lt;br /&gt;
The Previous XP is a fake one, they just send the &amp;quot;grade&amp;quot; score as the XP point. So one part of work is to rewrite this part.&lt;br /&gt;
&lt;br /&gt;
The new XP point system is a weighted score system, where instructor can assign or automatically generate the XP point.&lt;br /&gt;
&lt;br /&gt;
The following is the code for XP point report system:&lt;br /&gt;
&lt;br /&gt;
===Assignment Group Modules===&lt;br /&gt;
Assignment group module is used to&lt;br /&gt;
&lt;br /&gt;
Previous assignment group from the previous project E407 is a fake module. In this version, the code to decide whether an assignment group is “available” to a student should appear in both frontend and backend, but neither of them works. So we rewrite it and assign privileges to it.&lt;br /&gt;
&lt;br /&gt;
The following is the code for the assignment group module:&lt;br /&gt;
&lt;br /&gt;
===JavaScript Code Reconstruction===&lt;br /&gt;
When the game is running on the Expertiza system, Expertiza should send the game the info it needs to generate the level and game state at load-time. The page should basically load the frontend JavaScript and point it to the JSON string.&lt;br /&gt;
&lt;br /&gt;
Previous system get JSON by polling the server. In this strategy,  the computer does nothing other than check the status of the backend until it is ready, at which point the device is accessed.  In order to improve the efficiency of the CPU, we introduced Event Driven Programming (EDP) into our design.   In EDP, the flow of the program is determined by events. In our case, a callback closure is called when the JSON data hit the frontend.&lt;br /&gt;
&lt;br /&gt;
We also Change the ig module a little bit to support visiting module variable in closure.&lt;br /&gt;
&lt;br /&gt;
The related code is as following:&lt;br /&gt;
&lt;br /&gt;
===Fixing Mega Bugs===&lt;br /&gt;
====Uncaught Exception====&lt;br /&gt;
Previous JSON backend throws an uncaught exception when the user has an unsubmitted assignment and he is participating in that assignment.&lt;br /&gt;
&lt;br /&gt;
We fix this bug by&lt;br /&gt;
&lt;br /&gt;
The following is the related code:&lt;br /&gt;
&lt;br /&gt;
====Gate Name Error====&lt;br /&gt;
Previous JSON backend didn’t set gate name correctly. Thus when having multiple assignment group (&amp;gt;3) and at least one hub threshold is higher than current user’s XP point, JavaScript frontend falls into infinite loop.&lt;br /&gt;
&lt;br /&gt;
We fix this bug by&lt;br /&gt;
&lt;br /&gt;
The following is the related code:&lt;br /&gt;
&lt;br /&gt;
====Assignment Group Error====&lt;br /&gt;
In previous JSON backend, if an assignment group has X assignment in it, it will return X copy of that an assignment group, thus there’ll be X*X assignment in total. X times greater than it should be.&lt;br /&gt;
&lt;br /&gt;
We fix this bug by&lt;br /&gt;
&lt;br /&gt;
The following is the related code:&lt;br /&gt;
&lt;br /&gt;
===Fixing Minor Bugs===&lt;br /&gt;
* Previous backend send all assignment data to frontend even if the student is not participate in the assignment. &lt;br /&gt;
&lt;br /&gt;
* Previous JSON backend didn’t set MIME type correctly. That result in frontend code can only use low layer API to capture JSON data, when using high layer API, a silent error will occur.&lt;br /&gt;
&lt;br /&gt;
* Previous MIME type result in IE and Chrome has different behavior.&lt;br /&gt;
&lt;br /&gt;
* Sending a JSON string “null” to frontend will make it stop responding.&lt;br /&gt;
&lt;br /&gt;
==General Introduction to the System Setup==&lt;br /&gt;
Please follow these steps to setup the program:&lt;br /&gt;
&lt;br /&gt;
a)	Download and deploy the code from &lt;br /&gt;
http://commondatastorage.googleapis.com/silverwzw/expertiza-json.zip&lt;br /&gt;
&lt;br /&gt;
b)	Drop the database “pg_development”&lt;br /&gt;
&lt;br /&gt;
c)	Create an empty database “pg_development”&lt;br /&gt;
&lt;br /&gt;
d)	Run “db:migrate”&lt;br /&gt;
&lt;br /&gt;
e)	Start the server&lt;br /&gt;
&lt;br /&gt;
f)	Login as admin&lt;br /&gt;
&lt;br /&gt;
g)	Go to Navigation bar -&amp;gt; Administration -&amp;gt; Setup -&amp;gt; Menu Editor&lt;br /&gt;
&lt;br /&gt;
h)	In Menu Editor page, find “Assignment Group” row (it should be the last but six row)&lt;br /&gt;
&lt;br /&gt;
i)	You should see an up arrow at the left column of that row, click it.&lt;br /&gt;
&lt;br /&gt;
j)	Then the “Assignment Group” row goes up, click the 2nd column of that “Assignment Group” row, in which it should say “assignment_groups:list”&lt;br /&gt;
&lt;br /&gt;
k)	In the pop up page named “Editing controller_action” there’s a drop down box with label ”Permission”, choose “administer assignment” in that drop down box, then click button “Edit”&lt;br /&gt;
&lt;br /&gt;
l)	Create a student account, let’s say its name is “st1”.&lt;br /&gt;
&lt;br /&gt;
m)	Create 2 additional assignment groups (then we will have 3 assignment groups). Set the XP value of the “Default Group” to -1. Set the XP value of the “2nd Group” to 200. Set the XP value of the “3rd Group” to 3000. (XP value of an assignment group is the threshold to open that assignment room).&lt;br /&gt;
&lt;br /&gt;
n)	Create 5 assignment:&lt;br /&gt;
Please do it carefully and ONE BY ONE in order, set all deadlines to anytime LATER than May, 2013, select the “available to student” check button.&lt;br /&gt;
&lt;br /&gt;
	a1 : Group = Default Group | XP = 120 | directory =   /submitted_content/edit/1&lt;br /&gt;
&lt;br /&gt;
	a2 : Group = Default Group | XP = 200 | directory = /submitted_content/edit/2&lt;br /&gt;
&lt;br /&gt;
	a3 : Group = 2nd Group | XP = 2000 | directory = /submitted_content/edit/3&lt;br /&gt;
&lt;br /&gt;
	a4 : Group = 3rd Group | XP = 2000 | directory = /submitted_content/edit/4&lt;br /&gt;
&lt;br /&gt;
	a5 : Group = Default Group | XP = 2000 | directory = /submitted_content/edit/5&lt;br /&gt;
&lt;br /&gt;
add participant “st1” to  a1, a2, a3, a4 ( NOT for a5 )&lt;/div&gt;</summary>
		<author><name>Cyang14</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E629&amp;diff=74382</id>
		<title>CSC/ECE 517 Spring 2013/OSS E629</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E629&amp;diff=74382"/>
		<updated>2013-03-23T18:04:39Z</updated>

		<summary type="html">&lt;p&gt;Cyang14: /* General Introduction to the System Setup */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=E629: Integrating game frontend for Expertiza=&lt;br /&gt;
&lt;br /&gt;
==Project Introduction==&lt;br /&gt;
This is a game made for the Expertiza system (Ed Gehringer, NCSU). It transforms an assignment-based course into a &amp;quot;game&amp;quot; in which the player levels up and completes areas. The player doesn't receive grades; rather, he gains experience, and the professor would convert the experience values of the players at the end of the course into grades. The map is split up into distinct &amp;quot;rooms&amp;quot; unlocked by completing assignments; these are arrayed around a &amp;quot;center&amp;quot; in the middle of the map which contains portals to each room (portals which unlock as the player progresses).&lt;br /&gt;
&lt;br /&gt;
==Project Requirement==&lt;br /&gt;
Students will need to develop a page that runs the Expertiza game in JavaScript when a user is logged in and calls the code from E407 to get the JSON string to be parsed by the frontend.&lt;br /&gt;
&lt;br /&gt;
The page should basically load the frontend JavaScript and point it to the JSON string (a URL that will check to make sure that the current user is logged in correctly).&lt;br /&gt;
&lt;br /&gt;
[https://docs.google.com/a/ncsu.edu/document/d/1SVNx7Eh6dUdrz2a9rGSgBxLkxAmuBUm8eu08nHEdjIg/edit# Project Description]&lt;br /&gt;
&lt;br /&gt;
==Our Work==&lt;br /&gt;
&lt;br /&gt;
===Experience Point Report===&lt;br /&gt;
In this game, the player doesn't receive grades; rather, he gains experience(XP), and the professor would convert the experience values of the players at the end of the course into grades.&lt;br /&gt;
&lt;br /&gt;
The Previous XP is a fake one, they just send the &amp;quot;grade&amp;quot; score as the XP point. So one part of work is to rewrite this part.&lt;br /&gt;
&lt;br /&gt;
The new XP point system is a weighted score system, where instructor can assign or automatically generate the XP point.&lt;br /&gt;
&lt;br /&gt;
The following is the code for XP point report system:&lt;br /&gt;
&lt;br /&gt;
===Assignment Group Modules===&lt;br /&gt;
Assignment group module is used to&lt;br /&gt;
&lt;br /&gt;
Previous assignment group from the previous project E407 is a fake module. In this version, the code to decide whether an assignment group is “available” to a student should appear in both frontend and backend, but neither of them works. So we rewrite it and assign privileges to it.&lt;br /&gt;
&lt;br /&gt;
The following is the code for the assignment group module:&lt;br /&gt;
&lt;br /&gt;
===JavaScript Code Reconstruction===&lt;br /&gt;
When the game is running on the Expertiza system, Expertiza should send the game the info it needs to generate the level and game state at load-time. The page should basically load the frontend JavaScript and point it to the JSON string.&lt;br /&gt;
&lt;br /&gt;
Previous system get JSON by polling the server. In this strategy,  the computer does nothing other than check the status of the backend until it is ready, at which point the device is accessed.  In order to improve the efficiency of the CPU, we introduced Event Driven Programming (EDP) into our design.   In EDP, the flow of the program is determined by events. In our case, a callback closure is called when the JSON data hit the frontend.&lt;br /&gt;
&lt;br /&gt;
We also Change the ig module a little bit to support visiting module variable in closure.&lt;br /&gt;
&lt;br /&gt;
The related code is as following:&lt;br /&gt;
&lt;br /&gt;
===Fixing Mega Bugs===&lt;br /&gt;
====Uncaught Exception====&lt;br /&gt;
Previous JSON backend throws an uncaught exception when the user has an unsubmitted assignment and he is participating in that assignment.&lt;br /&gt;
&lt;br /&gt;
We fix this bug by&lt;br /&gt;
&lt;br /&gt;
The following is the related code:&lt;br /&gt;
&lt;br /&gt;
====Gate Name Error====&lt;br /&gt;
Previous JSON backend didn’t set gate name correctly. Thus when having multiple assignment group (&amp;gt;3) and at least one hub threshold is higher than current user’s XP point, JavaScript frontend falls into infinite loop.&lt;br /&gt;
&lt;br /&gt;
We fix this bug by&lt;br /&gt;
&lt;br /&gt;
The following is the related code:&lt;br /&gt;
&lt;br /&gt;
====Assignment Group Error====&lt;br /&gt;
In previous JSON backend, if an assignment group has X assignment in it, it will return X copy of that an assignment group, thus there’ll be X*X assignment in total. X times greater than it should be.&lt;br /&gt;
&lt;br /&gt;
We fix this bug by&lt;br /&gt;
&lt;br /&gt;
The following is the related code:&lt;br /&gt;
&lt;br /&gt;
===Fixing Minor Bugs===&lt;br /&gt;
* Previous backend send all assignment data to frontend even if the student is not participate in the assignment. &lt;br /&gt;
&lt;br /&gt;
* Previous JSON backend didn’t set MIME type correctly. That result in frontend code can only use low layer API to capture JSON data, when using high layer API, a silent error will occur.&lt;br /&gt;
&lt;br /&gt;
* Previous MIME type result in IE and Chrome has different behavior.&lt;br /&gt;
&lt;br /&gt;
* Sending a JSON string “null” to frontend will make it stop responding.&lt;br /&gt;
&lt;br /&gt;
==General Introduction to the System Setup==&lt;br /&gt;
Please follow these steps to setup the program:&lt;br /&gt;
&lt;br /&gt;
a)	Download and deploy the code from &lt;br /&gt;
http://commondatastorage.googleapis.com/silverwzw/expertiza-json.zip&lt;br /&gt;
&lt;br /&gt;
b)	Drop the database “pg_development”&lt;br /&gt;
&lt;br /&gt;
c)	Create an empty database “pg_development”&lt;br /&gt;
&lt;br /&gt;
d)	Run “db:migrate”&lt;br /&gt;
&lt;br /&gt;
e)	Start the server&lt;br /&gt;
&lt;br /&gt;
f)	Login as admin&lt;br /&gt;
&lt;br /&gt;
g)	Go to Navigation bar -&amp;gt; Administration -&amp;gt; Setup -&amp;gt; Menu Editor&lt;br /&gt;
&lt;br /&gt;
h)	In Menu Editor page, find “Assignment Group” row (it should be the last but six row)&lt;br /&gt;
&lt;br /&gt;
i)	You should see an up arrow at the left column of that row, click it.&lt;br /&gt;
&lt;br /&gt;
j)	Then the “Assignment Group” row goes up, click the 2nd column of that “Assignment Group” row, in which it should say “assignment_groups:list”&lt;br /&gt;
&lt;br /&gt;
k)	In the pop up page named “Editing controller_action” there’s a drop down box with label ”Permission”, choose “administer assignment” in that drop down box, then click button “Edit”&lt;br /&gt;
&lt;br /&gt;
l)	Create a student account, let’s say its name is “st1”.&lt;br /&gt;
&lt;br /&gt;
m)	Create 2 additional assignment groups (then we will have 3 assignment groups). Set the XP value of the “Default Group” to -1. Set the XP value of the “2nd Group” to 200. Set the XP value of the “3rd Group” to 3000. (XP value of an assignment group is the threshold to open that assignment room).&lt;br /&gt;
&lt;br /&gt;
n)	Create 5 assignment:&lt;br /&gt;
Please do it carefully and ONE BY ONE in order, set all deadlines to anytime LATER than May, 2013, select the “available to student” check button.&lt;br /&gt;
&lt;br /&gt;
	a1 : Group = Default Group | XP = 120 | directory =   /submitted_content/edit/1&lt;br /&gt;
	a2 : Group = Default Group | XP = 200 | directory = /submitted_content/edit/2&lt;br /&gt;
	a3 : Group = 2nd Group | XP = 2000 | directory = /submitted_content/edit/3&lt;br /&gt;
	a4 : Group = 3rd Group | XP = 2000 | directory = /submitted_content/edit/4&lt;br /&gt;
	a5 : Group = Default Group | XP = 2000 | directory = /submitted_content/edit/5&lt;br /&gt;
&lt;br /&gt;
add participant “st1” to  a1, a2, a3, a4 ( NOT for a5 )&lt;/div&gt;</summary>
		<author><name>Cyang14</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E629&amp;diff=74381</id>
		<title>CSC/ECE 517 Spring 2013/OSS E629</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E629&amp;diff=74381"/>
		<updated>2013-03-23T18:03:35Z</updated>

		<summary type="html">&lt;p&gt;Cyang14: /* General Introduction to the System Setup */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=E629: Integrating game frontend for Expertiza=&lt;br /&gt;
&lt;br /&gt;
==Project Introduction==&lt;br /&gt;
This is a game made for the Expertiza system (Ed Gehringer, NCSU). It transforms an assignment-based course into a &amp;quot;game&amp;quot; in which the player levels up and completes areas. The player doesn't receive grades; rather, he gains experience, and the professor would convert the experience values of the players at the end of the course into grades. The map is split up into distinct &amp;quot;rooms&amp;quot; unlocked by completing assignments; these are arrayed around a &amp;quot;center&amp;quot; in the middle of the map which contains portals to each room (portals which unlock as the player progresses).&lt;br /&gt;
&lt;br /&gt;
==Project Requirement==&lt;br /&gt;
Students will need to develop a page that runs the Expertiza game in JavaScript when a user is logged in and calls the code from E407 to get the JSON string to be parsed by the frontend.&lt;br /&gt;
&lt;br /&gt;
The page should basically load the frontend JavaScript and point it to the JSON string (a URL that will check to make sure that the current user is logged in correctly).&lt;br /&gt;
&lt;br /&gt;
[https://docs.google.com/a/ncsu.edu/document/d/1SVNx7Eh6dUdrz2a9rGSgBxLkxAmuBUm8eu08nHEdjIg/edit# Project Description]&lt;br /&gt;
&lt;br /&gt;
==Our Work==&lt;br /&gt;
&lt;br /&gt;
===Experience Point Report===&lt;br /&gt;
In this game, the player doesn't receive grades; rather, he gains experience(XP), and the professor would convert the experience values of the players at the end of the course into grades.&lt;br /&gt;
&lt;br /&gt;
The Previous XP is a fake one, they just send the &amp;quot;grade&amp;quot; score as the XP point. So one part of work is to rewrite this part.&lt;br /&gt;
&lt;br /&gt;
The new XP point system is a weighted score system, where instructor can assign or automatically generate the XP point.&lt;br /&gt;
&lt;br /&gt;
The following is the code for XP point report system:&lt;br /&gt;
&lt;br /&gt;
===Assignment Group Modules===&lt;br /&gt;
Assignment group module is used to&lt;br /&gt;
&lt;br /&gt;
Previous assignment group from the previous project E407 is a fake module. In this version, the code to decide whether an assignment group is “available” to a student should appear in both frontend and backend, but neither of them works. So we rewrite it and assign privileges to it.&lt;br /&gt;
&lt;br /&gt;
The following is the code for the assignment group module:&lt;br /&gt;
&lt;br /&gt;
===JavaScript Code Reconstruction===&lt;br /&gt;
When the game is running on the Expertiza system, Expertiza should send the game the info it needs to generate the level and game state at load-time. The page should basically load the frontend JavaScript and point it to the JSON string.&lt;br /&gt;
&lt;br /&gt;
Previous system get JSON by polling the server. In this strategy,  the computer does nothing other than check the status of the backend until it is ready, at which point the device is accessed.  In order to improve the efficiency of the CPU, we introduced Event Driven Programming (EDP) into our design.   In EDP, the flow of the program is determined by events. In our case, a callback closure is called when the JSON data hit the frontend.&lt;br /&gt;
&lt;br /&gt;
We also Change the ig module a little bit to support visiting module variable in closure.&lt;br /&gt;
&lt;br /&gt;
The related code is as following:&lt;br /&gt;
&lt;br /&gt;
===Fixing Mega Bugs===&lt;br /&gt;
====Uncaught Exception====&lt;br /&gt;
Previous JSON backend throws an uncaught exception when the user has an unsubmitted assignment and he is participating in that assignment.&lt;br /&gt;
&lt;br /&gt;
We fix this bug by&lt;br /&gt;
&lt;br /&gt;
The following is the related code:&lt;br /&gt;
&lt;br /&gt;
====Gate Name Error====&lt;br /&gt;
Previous JSON backend didn’t set gate name correctly. Thus when having multiple assignment group (&amp;gt;3) and at least one hub threshold is higher than current user’s XP point, JavaScript frontend falls into infinite loop.&lt;br /&gt;
&lt;br /&gt;
We fix this bug by&lt;br /&gt;
&lt;br /&gt;
The following is the related code:&lt;br /&gt;
&lt;br /&gt;
====Assignment Group Error====&lt;br /&gt;
In previous JSON backend, if an assignment group has X assignment in it, it will return X copy of that an assignment group, thus there’ll be X*X assignment in total. X times greater than it should be.&lt;br /&gt;
&lt;br /&gt;
We fix this bug by&lt;br /&gt;
&lt;br /&gt;
The following is the related code:&lt;br /&gt;
&lt;br /&gt;
===Fixing Minor Bugs===&lt;br /&gt;
* Previous backend send all assignment data to frontend even if the student is not participate in the assignment. &lt;br /&gt;
&lt;br /&gt;
* Previous JSON backend didn’t set MIME type correctly. That result in frontend code can only use low layer API to capture JSON data, when using high layer API, a silent error will occur.&lt;br /&gt;
&lt;br /&gt;
* Previous MIME type result in IE and Chrome has different behavior.&lt;br /&gt;
&lt;br /&gt;
* Sending a JSON string “null” to frontend will make it stop responding.&lt;br /&gt;
&lt;br /&gt;
==General Introduction to the System Setup==&lt;br /&gt;
Please follow these steps to setup the program:&lt;br /&gt;
&lt;br /&gt;
a)	Download and deploy the code from &lt;br /&gt;
http://commondatastorage.googleapis.com/silverwzw/expertiza-json.zip&lt;br /&gt;
&lt;br /&gt;
b)	Drop the database “pg_development”&lt;br /&gt;
&lt;br /&gt;
c)	Create an empty database “pg_development”&lt;br /&gt;
&lt;br /&gt;
d)	Run “db:migrate”&lt;br /&gt;
&lt;br /&gt;
e)	Start the server&lt;br /&gt;
&lt;br /&gt;
f)	Login as admin&lt;br /&gt;
&lt;br /&gt;
g)	Go to Navigation bar -&amp;gt; Administration -&amp;gt; Setup -&amp;gt; Menu Editor&lt;br /&gt;
&lt;br /&gt;
h)	In Menu Editor page, find “Assignment Group” row (it should be the last but six row)&lt;br /&gt;
&lt;br /&gt;
i)	You should see an up arrow at the left column of that row, click it.&lt;br /&gt;
&lt;br /&gt;
j)	Then the “Assignment Group” row goes up, click the 2nd column of that “Assignment Group” row, in which it should say “assignment_groups:list”&lt;br /&gt;
&lt;br /&gt;
k)	In the pop up page named “Editing controller_action” there’s a drop down box with label ”Permission”, choose “administer assignment” in that drop down box, then click button “Edit”&lt;br /&gt;
&lt;br /&gt;
l)	Create a student account, let’s say its name is “st1”.&lt;br /&gt;
&lt;br /&gt;
m)	Create 2 additional assignment groups (then we will have 3 assignment groups). Set the XP value of the “Default Group” to -1. Set the XP value of the “2nd Group” to 200. Set the XP value of the “3rd Group” to 3000. (XP value of an assignment group is the threshold to open that assignment room).&lt;br /&gt;
&lt;br /&gt;
n)	Create 5 assignment:&lt;br /&gt;
 Please do it carefully and ONE BY ONE in order, set all deadlines to anytime LATER than May, 2013, select the “available to student” check button.&lt;br /&gt;
i.	a1 : Group = Default Group | XP = 120 | directory = /submitted_content/edit/1&lt;br /&gt;
ii.	a2 : Group = Default Group | XP = 200 | directory = /submitted_content/edit/2&lt;br /&gt;
iii.	a3 : Group = 2nd Group | XP = 2000 | directory = /submitted_content/edit/3&lt;br /&gt;
iv.	a4 : Group = 3rd Group | XP = 2000 | directory = /submitted_content/edit/4&lt;br /&gt;
v.	a5 : Group = Default Group | XP = 2000 | directory = /submitted_content/edit/5&lt;br /&gt;
add participant “st1” to  a1, a2, a3, a4 ( NOT for a5 )&lt;/div&gt;</summary>
		<author><name>Cyang14</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E629&amp;diff=74380</id>
		<title>CSC/ECE 517 Spring 2013/OSS E629</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E629&amp;diff=74380"/>
		<updated>2013-03-23T18:03:04Z</updated>

		<summary type="html">&lt;p&gt;Cyang14: /* General Introduction to the System Setup */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=E629: Integrating game frontend for Expertiza=&lt;br /&gt;
&lt;br /&gt;
==Project Introduction==&lt;br /&gt;
This is a game made for the Expertiza system (Ed Gehringer, NCSU). It transforms an assignment-based course into a &amp;quot;game&amp;quot; in which the player levels up and completes areas. The player doesn't receive grades; rather, he gains experience, and the professor would convert the experience values of the players at the end of the course into grades. The map is split up into distinct &amp;quot;rooms&amp;quot; unlocked by completing assignments; these are arrayed around a &amp;quot;center&amp;quot; in the middle of the map which contains portals to each room (portals which unlock as the player progresses).&lt;br /&gt;
&lt;br /&gt;
==Project Requirement==&lt;br /&gt;
Students will need to develop a page that runs the Expertiza game in JavaScript when a user is logged in and calls the code from E407 to get the JSON string to be parsed by the frontend.&lt;br /&gt;
&lt;br /&gt;
The page should basically load the frontend JavaScript and point it to the JSON string (a URL that will check to make sure that the current user is logged in correctly).&lt;br /&gt;
&lt;br /&gt;
[https://docs.google.com/a/ncsu.edu/document/d/1SVNx7Eh6dUdrz2a9rGSgBxLkxAmuBUm8eu08nHEdjIg/edit# Project Description]&lt;br /&gt;
&lt;br /&gt;
==Our Work==&lt;br /&gt;
&lt;br /&gt;
===Experience Point Report===&lt;br /&gt;
In this game, the player doesn't receive grades; rather, he gains experience(XP), and the professor would convert the experience values of the players at the end of the course into grades.&lt;br /&gt;
&lt;br /&gt;
The Previous XP is a fake one, they just send the &amp;quot;grade&amp;quot; score as the XP point. So one part of work is to rewrite this part.&lt;br /&gt;
&lt;br /&gt;
The new XP point system is a weighted score system, where instructor can assign or automatically generate the XP point.&lt;br /&gt;
&lt;br /&gt;
The following is the code for XP point report system:&lt;br /&gt;
&lt;br /&gt;
===Assignment Group Modules===&lt;br /&gt;
Assignment group module is used to&lt;br /&gt;
&lt;br /&gt;
Previous assignment group from the previous project E407 is a fake module. In this version, the code to decide whether an assignment group is “available” to a student should appear in both frontend and backend, but neither of them works. So we rewrite it and assign privileges to it.&lt;br /&gt;
&lt;br /&gt;
The following is the code for the assignment group module:&lt;br /&gt;
&lt;br /&gt;
===JavaScript Code Reconstruction===&lt;br /&gt;
When the game is running on the Expertiza system, Expertiza should send the game the info it needs to generate the level and game state at load-time. The page should basically load the frontend JavaScript and point it to the JSON string.&lt;br /&gt;
&lt;br /&gt;
Previous system get JSON by polling the server. In this strategy,  the computer does nothing other than check the status of the backend until it is ready, at which point the device is accessed.  In order to improve the efficiency of the CPU, we introduced Event Driven Programming (EDP) into our design.   In EDP, the flow of the program is determined by events. In our case, a callback closure is called when the JSON data hit the frontend.&lt;br /&gt;
&lt;br /&gt;
We also Change the ig module a little bit to support visiting module variable in closure.&lt;br /&gt;
&lt;br /&gt;
The related code is as following:&lt;br /&gt;
&lt;br /&gt;
===Fixing Mega Bugs===&lt;br /&gt;
====Uncaught Exception====&lt;br /&gt;
Previous JSON backend throws an uncaught exception when the user has an unsubmitted assignment and he is participating in that assignment.&lt;br /&gt;
&lt;br /&gt;
We fix this bug by&lt;br /&gt;
&lt;br /&gt;
The following is the related code:&lt;br /&gt;
&lt;br /&gt;
====Gate Name Error====&lt;br /&gt;
Previous JSON backend didn’t set gate name correctly. Thus when having multiple assignment group (&amp;gt;3) and at least one hub threshold is higher than current user’s XP point, JavaScript frontend falls into infinite loop.&lt;br /&gt;
&lt;br /&gt;
We fix this bug by&lt;br /&gt;
&lt;br /&gt;
The following is the related code:&lt;br /&gt;
&lt;br /&gt;
====Assignment Group Error====&lt;br /&gt;
In previous JSON backend, if an assignment group has X assignment in it, it will return X copy of that an assignment group, thus there’ll be X*X assignment in total. X times greater than it should be.&lt;br /&gt;
&lt;br /&gt;
We fix this bug by&lt;br /&gt;
&lt;br /&gt;
The following is the related code:&lt;br /&gt;
&lt;br /&gt;
===Fixing Minor Bugs===&lt;br /&gt;
* Previous backend send all assignment data to frontend even if the student is not participate in the assignment. &lt;br /&gt;
&lt;br /&gt;
* Previous JSON backend didn’t set MIME type correctly. That result in frontend code can only use low layer API to capture JSON data, when using high layer API, a silent error will occur.&lt;br /&gt;
&lt;br /&gt;
* Previous MIME type result in IE and Chrome has different behavior.&lt;br /&gt;
&lt;br /&gt;
* Sending a JSON string “null” to frontend will make it stop responding.&lt;br /&gt;
&lt;br /&gt;
==General Introduction to the System Setup==&lt;br /&gt;
Please follow these steps to setup the program:&lt;br /&gt;
&lt;br /&gt;
a)	Download and deploy the code from &lt;br /&gt;
http://commondatastorage.googleapis.com/silverwzw/expertiza-json.zip&lt;br /&gt;
&lt;br /&gt;
b)	Drop the database “pg_development”&lt;br /&gt;
&lt;br /&gt;
c)	Create an empty database “pg_development”&lt;br /&gt;
&lt;br /&gt;
d)	Run “db:migrate”&lt;br /&gt;
&lt;br /&gt;
e)	Start the server&lt;br /&gt;
&lt;br /&gt;
f)	Login as admin&lt;br /&gt;
&lt;br /&gt;
g)	Go to Navigation bar -&amp;gt; Administration -&amp;gt; Setup -&amp;gt; Menu Editor&lt;br /&gt;
&lt;br /&gt;
h)	In Menu Editor page, find “Assignment Group” row (it should be the last but six row)&lt;br /&gt;
&lt;br /&gt;
i)	You should see an up arrow at the left column of that row, click it.&lt;br /&gt;
j)	Then the “Assignment Group” row goes up, click the 2nd column of that “Assignment Group” row, in which it should say “assignment_groups:list”&lt;br /&gt;
k)	In the pop up page named “Editing controller_action” there’s a drop down box with label ”Permission”, choose “administer assignment” in that drop down box, then click button “Edit”&lt;br /&gt;
l)	Create a student account, let’s say its name is “st1”.&lt;br /&gt;
m)	Create 2 additional assignment groups (then we will have 3 assignment groups). Set the XP value of the “Default Group” to -1. Set the XP value of the “2nd Group” to 200. Set the XP value of the “3rd Group” to 3000. (XP value of an assignment group is the threshold to open that assignment room).&lt;br /&gt;
n)	Create 5 assignment:&lt;br /&gt;
 Please do it carefully and ONE BY ONE in order, set all deadlines to anytime LATER than May, 2013, select the “available to student” check button.&lt;br /&gt;
i.	a1 : Group = Default Group | XP = 120 | directory = /submitted_content/edit/1&lt;br /&gt;
ii.	a2 : Group = Default Group | XP = 200 | directory = /submitted_content/edit/2&lt;br /&gt;
iii.	a3 : Group = 2nd Group | XP = 2000 | directory = /submitted_content/edit/3&lt;br /&gt;
iv.	a4 : Group = 3rd Group | XP = 2000 | directory = /submitted_content/edit/4&lt;br /&gt;
v.	a5 : Group = Default Group | XP = 2000 | directory = /submitted_content/edit/5&lt;br /&gt;
add participant “st1” to  a1, a2, a3, a4 ( NOT for a5 )&lt;/div&gt;</summary>
		<author><name>Cyang14</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E629&amp;diff=74379</id>
		<title>CSC/ECE 517 Spring 2013/OSS E629</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E629&amp;diff=74379"/>
		<updated>2013-03-23T18:02:39Z</updated>

		<summary type="html">&lt;p&gt;Cyang14: /* General Introduction to the System Setup */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=E629: Integrating game frontend for Expertiza=&lt;br /&gt;
&lt;br /&gt;
==Project Introduction==&lt;br /&gt;
This is a game made for the Expertiza system (Ed Gehringer, NCSU). It transforms an assignment-based course into a &amp;quot;game&amp;quot; in which the player levels up and completes areas. The player doesn't receive grades; rather, he gains experience, and the professor would convert the experience values of the players at the end of the course into grades. The map is split up into distinct &amp;quot;rooms&amp;quot; unlocked by completing assignments; these are arrayed around a &amp;quot;center&amp;quot; in the middle of the map which contains portals to each room (portals which unlock as the player progresses).&lt;br /&gt;
&lt;br /&gt;
==Project Requirement==&lt;br /&gt;
Students will need to develop a page that runs the Expertiza game in JavaScript when a user is logged in and calls the code from E407 to get the JSON string to be parsed by the frontend.&lt;br /&gt;
&lt;br /&gt;
The page should basically load the frontend JavaScript and point it to the JSON string (a URL that will check to make sure that the current user is logged in correctly).&lt;br /&gt;
&lt;br /&gt;
[https://docs.google.com/a/ncsu.edu/document/d/1SVNx7Eh6dUdrz2a9rGSgBxLkxAmuBUm8eu08nHEdjIg/edit# Project Description]&lt;br /&gt;
&lt;br /&gt;
==Our Work==&lt;br /&gt;
&lt;br /&gt;
===Experience Point Report===&lt;br /&gt;
In this game, the player doesn't receive grades; rather, he gains experience(XP), and the professor would convert the experience values of the players at the end of the course into grades.&lt;br /&gt;
&lt;br /&gt;
The Previous XP is a fake one, they just send the &amp;quot;grade&amp;quot; score as the XP point. So one part of work is to rewrite this part.&lt;br /&gt;
&lt;br /&gt;
The new XP point system is a weighted score system, where instructor can assign or automatically generate the XP point.&lt;br /&gt;
&lt;br /&gt;
The following is the code for XP point report system:&lt;br /&gt;
&lt;br /&gt;
===Assignment Group Modules===&lt;br /&gt;
Assignment group module is used to&lt;br /&gt;
&lt;br /&gt;
Previous assignment group from the previous project E407 is a fake module. In this version, the code to decide whether an assignment group is “available” to a student should appear in both frontend and backend, but neither of them works. So we rewrite it and assign privileges to it.&lt;br /&gt;
&lt;br /&gt;
The following is the code for the assignment group module:&lt;br /&gt;
&lt;br /&gt;
===JavaScript Code Reconstruction===&lt;br /&gt;
When the game is running on the Expertiza system, Expertiza should send the game the info it needs to generate the level and game state at load-time. The page should basically load the frontend JavaScript and point it to the JSON string.&lt;br /&gt;
&lt;br /&gt;
Previous system get JSON by polling the server. In this strategy,  the computer does nothing other than check the status of the backend until it is ready, at which point the device is accessed.  In order to improve the efficiency of the CPU, we introduced Event Driven Programming (EDP) into our design.   In EDP, the flow of the program is determined by events. In our case, a callback closure is called when the JSON data hit the frontend.&lt;br /&gt;
&lt;br /&gt;
We also Change the ig module a little bit to support visiting module variable in closure.&lt;br /&gt;
&lt;br /&gt;
The related code is as following:&lt;br /&gt;
&lt;br /&gt;
===Fixing Mega Bugs===&lt;br /&gt;
====Uncaught Exception====&lt;br /&gt;
Previous JSON backend throws an uncaught exception when the user has an unsubmitted assignment and he is participating in that assignment.&lt;br /&gt;
&lt;br /&gt;
We fix this bug by&lt;br /&gt;
&lt;br /&gt;
The following is the related code:&lt;br /&gt;
&lt;br /&gt;
====Gate Name Error====&lt;br /&gt;
Previous JSON backend didn’t set gate name correctly. Thus when having multiple assignment group (&amp;gt;3) and at least one hub threshold is higher than current user’s XP point, JavaScript frontend falls into infinite loop.&lt;br /&gt;
&lt;br /&gt;
We fix this bug by&lt;br /&gt;
&lt;br /&gt;
The following is the related code:&lt;br /&gt;
&lt;br /&gt;
====Assignment Group Error====&lt;br /&gt;
In previous JSON backend, if an assignment group has X assignment in it, it will return X copy of that an assignment group, thus there’ll be X*X assignment in total. X times greater than it should be.&lt;br /&gt;
&lt;br /&gt;
We fix this bug by&lt;br /&gt;
&lt;br /&gt;
The following is the related code:&lt;br /&gt;
&lt;br /&gt;
===Fixing Minor Bugs===&lt;br /&gt;
* Previous backend send all assignment data to frontend even if the student is not participate in the assignment. &lt;br /&gt;
&lt;br /&gt;
* Previous JSON backend didn’t set MIME type correctly. That result in frontend code can only use low layer API to capture JSON data, when using high layer API, a silent error will occur.&lt;br /&gt;
&lt;br /&gt;
* Previous MIME type result in IE and Chrome has different behavior.&lt;br /&gt;
&lt;br /&gt;
* Sending a JSON string “null” to frontend will make it stop responding.&lt;br /&gt;
&lt;br /&gt;
==General Introduction to the System Setup==&lt;br /&gt;
Please follow these steps to setup the program:&lt;br /&gt;
&lt;br /&gt;
a)	Download and deploy the code from &lt;br /&gt;
http://commondatastorage.googleapis.com/silverwzw/expertiza-json.zip&lt;br /&gt;
&lt;br /&gt;
b)	Drop the database “pg_development”&lt;br /&gt;
c)	Create an empty database “pg_development”&lt;br /&gt;
d)	Run “db:migrate”&lt;br /&gt;
e)	Start the server&lt;br /&gt;
f)	Login as admin&lt;br /&gt;
g)	Go to Navigation bar -&amp;gt; Administration -&amp;gt; Setup -&amp;gt; Menu Editor&lt;br /&gt;
h)	In Menu Editor page, find “Assignment Group” row (it should be the last but six row)&lt;br /&gt;
i)	You should see an up arrow at the left column of that row, click it.&lt;br /&gt;
j)	Then the “Assignment Group” row goes up, click the 2nd column of that “Assignment Group” row, in which it should say “assignment_groups:list”&lt;br /&gt;
k)	In the pop up page named “Editing controller_action” there’s a drop down box with label ”Permission”, choose “administer assignment” in that drop down box, then click button “Edit”&lt;br /&gt;
l)	Create a student account, let’s say its name is “st1”.&lt;br /&gt;
m)	Create 2 additional assignment groups (then we will have 3 assignment groups). Set the XP value of the “Default Group” to -1. Set the XP value of the “2nd Group” to 200. Set the XP value of the “3rd Group” to 3000. (XP value of an assignment group is the threshold to open that assignment room).&lt;br /&gt;
n)	Create 5 assignment:&lt;br /&gt;
 Please do it carefully and ONE BY ONE in order, set all deadlines to anytime LATER than May, 2013, select the “available to student” check button.&lt;br /&gt;
i.	a1 : Group = Default Group | XP = 120 | directory = /submitted_content/edit/1&lt;br /&gt;
ii.	a2 : Group = Default Group | XP = 200 | directory = /submitted_content/edit/2&lt;br /&gt;
iii.	a3 : Group = 2nd Group | XP = 2000 | directory = /submitted_content/edit/3&lt;br /&gt;
iv.	a4 : Group = 3rd Group | XP = 2000 | directory = /submitted_content/edit/4&lt;br /&gt;
v.	a5 : Group = Default Group | XP = 2000 | directory = /submitted_content/edit/5&lt;br /&gt;
add participant “st1” to  a1, a2, a3, a4 ( NOT for a5 )&lt;/div&gt;</summary>
		<author><name>Cyang14</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E629&amp;diff=74378</id>
		<title>CSC/ECE 517 Spring 2013/OSS E629</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E629&amp;diff=74378"/>
		<updated>2013-03-23T17:59:15Z</updated>

		<summary type="html">&lt;p&gt;Cyang14: /* General Introduction to the System Setup */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=E629: Integrating game frontend for Expertiza=&lt;br /&gt;
&lt;br /&gt;
==Project Introduction==&lt;br /&gt;
This is a game made for the Expertiza system (Ed Gehringer, NCSU). It transforms an assignment-based course into a &amp;quot;game&amp;quot; in which the player levels up and completes areas. The player doesn't receive grades; rather, he gains experience, and the professor would convert the experience values of the players at the end of the course into grades. The map is split up into distinct &amp;quot;rooms&amp;quot; unlocked by completing assignments; these are arrayed around a &amp;quot;center&amp;quot; in the middle of the map which contains portals to each room (portals which unlock as the player progresses).&lt;br /&gt;
&lt;br /&gt;
==Project Requirement==&lt;br /&gt;
Students will need to develop a page that runs the Expertiza game in JavaScript when a user is logged in and calls the code from E407 to get the JSON string to be parsed by the frontend.&lt;br /&gt;
&lt;br /&gt;
The page should basically load the frontend JavaScript and point it to the JSON string (a URL that will check to make sure that the current user is logged in correctly).&lt;br /&gt;
&lt;br /&gt;
[https://docs.google.com/a/ncsu.edu/document/d/1SVNx7Eh6dUdrz2a9rGSgBxLkxAmuBUm8eu08nHEdjIg/edit# Project Description]&lt;br /&gt;
&lt;br /&gt;
==Our Work==&lt;br /&gt;
&lt;br /&gt;
===Experience Point Report===&lt;br /&gt;
In this game, the player doesn't receive grades; rather, he gains experience(XP), and the professor would convert the experience values of the players at the end of the course into grades.&lt;br /&gt;
&lt;br /&gt;
The Previous XP is a fake one, they just send the &amp;quot;grade&amp;quot; score as the XP point. So one part of work is to rewrite this part.&lt;br /&gt;
&lt;br /&gt;
The new XP point system is a weighted score system, where instructor can assign or automatically generate the XP point.&lt;br /&gt;
&lt;br /&gt;
The following is the code for XP point report system:&lt;br /&gt;
&lt;br /&gt;
===Assignment Group Modules===&lt;br /&gt;
Assignment group module is used to&lt;br /&gt;
&lt;br /&gt;
Previous assignment group from the previous project E407 is a fake module. In this version, the code to decide whether an assignment group is “available” to a student should appear in both frontend and backend, but neither of them works. So we rewrite it and assign privileges to it.&lt;br /&gt;
&lt;br /&gt;
The following is the code for the assignment group module:&lt;br /&gt;
&lt;br /&gt;
===JavaScript Code Reconstruction===&lt;br /&gt;
When the game is running on the Expertiza system, Expertiza should send the game the info it needs to generate the level and game state at load-time. The page should basically load the frontend JavaScript and point it to the JSON string.&lt;br /&gt;
&lt;br /&gt;
Previous system get JSON by polling the server. In this strategy,  the computer does nothing other than check the status of the backend until it is ready, at which point the device is accessed.  In order to improve the efficiency of the CPU, we introduced Event Driven Programming (EDP) into our design.   In EDP, the flow of the program is determined by events. In our case, a callback closure is called when the JSON data hit the frontend.&lt;br /&gt;
&lt;br /&gt;
We also Change the ig module a little bit to support visiting module variable in closure.&lt;br /&gt;
&lt;br /&gt;
The related code is as following:&lt;br /&gt;
&lt;br /&gt;
===Fixing Mega Bugs===&lt;br /&gt;
====Uncaught Exception====&lt;br /&gt;
Previous JSON backend throws an uncaught exception when the user has an unsubmitted assignment and he is participating in that assignment.&lt;br /&gt;
&lt;br /&gt;
We fix this bug by&lt;br /&gt;
&lt;br /&gt;
The following is the related code:&lt;br /&gt;
&lt;br /&gt;
====Gate Name Error====&lt;br /&gt;
Previous JSON backend didn’t set gate name correctly. Thus when having multiple assignment group (&amp;gt;3) and at least one hub threshold is higher than current user’s XP point, JavaScript frontend falls into infinite loop.&lt;br /&gt;
&lt;br /&gt;
We fix this bug by&lt;br /&gt;
&lt;br /&gt;
The following is the related code:&lt;br /&gt;
&lt;br /&gt;
====Assignment Group Error====&lt;br /&gt;
In previous JSON backend, if an assignment group has X assignment in it, it will return X copy of that an assignment group, thus there’ll be X*X assignment in total. X times greater than it should be.&lt;br /&gt;
&lt;br /&gt;
We fix this bug by&lt;br /&gt;
&lt;br /&gt;
The following is the related code:&lt;br /&gt;
&lt;br /&gt;
===Fixing Minor Bugs===&lt;br /&gt;
* Previous backend send all assignment data to frontend even if the student is not participate in the assignment. &lt;br /&gt;
&lt;br /&gt;
* Previous JSON backend didn’t set MIME type correctly. That result in frontend code can only use low layer API to capture JSON data, when using high layer API, a silent error will occur.&lt;br /&gt;
&lt;br /&gt;
* Previous MIME type result in IE and Chrome has different behavior.&lt;br /&gt;
&lt;br /&gt;
* Sending a JSON string “null” to frontend will make it stop responding.&lt;br /&gt;
&lt;br /&gt;
==General Introduction to the System Setup==&lt;br /&gt;
Please follow these steps to setup the program:&lt;br /&gt;
&lt;br /&gt;
a)	Download and deploy the code from &lt;br /&gt;
http://commondatastorage.googleapis.com/silverwzw/expertiza-json.zip&lt;br /&gt;
b)	Drop the database “pg_development”&lt;br /&gt;
c)	Create an empty database “pg_development”&lt;br /&gt;
d)	Run “db:migrate”&lt;br /&gt;
e)	Start the server&lt;br /&gt;
f)	Login as admin&lt;br /&gt;
g)	Go to Navigation bar -&amp;gt; Administration -&amp;gt; Setup -&amp;gt; Menu Editor&lt;br /&gt;
h)	In Menu Editor page, find “Assignment Group” row (it should be the last but six row)&lt;br /&gt;
i)	You should see an up arrow at the left column of that row, click it.&lt;br /&gt;
j)	Then the “Assignment Group” row goes up, click the 2nd column of that “Assignment Group” row, in which it should say “assignment_groups:list”&lt;br /&gt;
k)	In the pop up page named “Editing controller_action” there’s a drop down box with label ”Permission”, choose “administer assignment” in that drop down box, then click button “Edit”&lt;br /&gt;
l)	Create a student account, let’s say its name is “st1”.&lt;br /&gt;
m)	Create 2 additional assignment groups (then we will have 3 assignment groups). Set the XP value of the “Default Group” to -1. Set the XP value of the “2nd Group” to 200. Set the XP value of the “3rd Group” to 3000. (XP value of an assignment group is the threshold to open that assignment room).&lt;br /&gt;
n)	Create 5 assignment:&lt;br /&gt;
 Please do it carefully and ONE BY ONE in order, set all deadlines to anytime LATER than May, 2013, select the “available to student” check button.&lt;br /&gt;
i.	a1 : Group = Default Group | XP = 120 | directory = /submitted_content/edit/1&lt;br /&gt;
ii.	a2 : Group = Default Group | XP = 200 | directory = /submitted_content/edit/2&lt;br /&gt;
iii.	a3 : Group = 2nd Group | XP = 2000 | directory = /submitted_content/edit/3&lt;br /&gt;
iv.	a4 : Group = 3rd Group | XP = 2000 | directory = /submitted_content/edit/4&lt;br /&gt;
v.	a5 : Group = Default Group | XP = 2000 | directory = /submitted_content/edit/5&lt;br /&gt;
add participant “st1” to  a1, a2, a3, a4 ( NOT for a5 )&lt;/div&gt;</summary>
		<author><name>Cyang14</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E629&amp;diff=74377</id>
		<title>CSC/ECE 517 Spring 2013/OSS E629</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E629&amp;diff=74377"/>
		<updated>2013-03-23T17:58:42Z</updated>

		<summary type="html">&lt;p&gt;Cyang14: /* Fixing Minor Bugs */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=E629: Integrating game frontend for Expertiza=&lt;br /&gt;
&lt;br /&gt;
==Project Introduction==&lt;br /&gt;
This is a game made for the Expertiza system (Ed Gehringer, NCSU). It transforms an assignment-based course into a &amp;quot;game&amp;quot; in which the player levels up and completes areas. The player doesn't receive grades; rather, he gains experience, and the professor would convert the experience values of the players at the end of the course into grades. The map is split up into distinct &amp;quot;rooms&amp;quot; unlocked by completing assignments; these are arrayed around a &amp;quot;center&amp;quot; in the middle of the map which contains portals to each room (portals which unlock as the player progresses).&lt;br /&gt;
&lt;br /&gt;
==Project Requirement==&lt;br /&gt;
Students will need to develop a page that runs the Expertiza game in JavaScript when a user is logged in and calls the code from E407 to get the JSON string to be parsed by the frontend.&lt;br /&gt;
&lt;br /&gt;
The page should basically load the frontend JavaScript and point it to the JSON string (a URL that will check to make sure that the current user is logged in correctly).&lt;br /&gt;
&lt;br /&gt;
[https://docs.google.com/a/ncsu.edu/document/d/1SVNx7Eh6dUdrz2a9rGSgBxLkxAmuBUm8eu08nHEdjIg/edit# Project Description]&lt;br /&gt;
&lt;br /&gt;
==Our Work==&lt;br /&gt;
&lt;br /&gt;
===Experience Point Report===&lt;br /&gt;
In this game, the player doesn't receive grades; rather, he gains experience(XP), and the professor would convert the experience values of the players at the end of the course into grades.&lt;br /&gt;
&lt;br /&gt;
The Previous XP is a fake one, they just send the &amp;quot;grade&amp;quot; score as the XP point. So one part of work is to rewrite this part.&lt;br /&gt;
&lt;br /&gt;
The new XP point system is a weighted score system, where instructor can assign or automatically generate the XP point.&lt;br /&gt;
&lt;br /&gt;
The following is the code for XP point report system:&lt;br /&gt;
&lt;br /&gt;
===Assignment Group Modules===&lt;br /&gt;
Assignment group module is used to&lt;br /&gt;
&lt;br /&gt;
Previous assignment group from the previous project E407 is a fake module. In this version, the code to decide whether an assignment group is “available” to a student should appear in both frontend and backend, but neither of them works. So we rewrite it and assign privileges to it.&lt;br /&gt;
&lt;br /&gt;
The following is the code for the assignment group module:&lt;br /&gt;
&lt;br /&gt;
===JavaScript Code Reconstruction===&lt;br /&gt;
When the game is running on the Expertiza system, Expertiza should send the game the info it needs to generate the level and game state at load-time. The page should basically load the frontend JavaScript and point it to the JSON string.&lt;br /&gt;
&lt;br /&gt;
Previous system get JSON by polling the server. In this strategy,  the computer does nothing other than check the status of the backend until it is ready, at which point the device is accessed.  In order to improve the efficiency of the CPU, we introduced Event Driven Programming (EDP) into our design.   In EDP, the flow of the program is determined by events. In our case, a callback closure is called when the JSON data hit the frontend.&lt;br /&gt;
&lt;br /&gt;
We also Change the ig module a little bit to support visiting module variable in closure.&lt;br /&gt;
&lt;br /&gt;
The related code is as following:&lt;br /&gt;
&lt;br /&gt;
===Fixing Mega Bugs===&lt;br /&gt;
====Uncaught Exception====&lt;br /&gt;
Previous JSON backend throws an uncaught exception when the user has an unsubmitted assignment and he is participating in that assignment.&lt;br /&gt;
&lt;br /&gt;
We fix this bug by&lt;br /&gt;
&lt;br /&gt;
The following is the related code:&lt;br /&gt;
&lt;br /&gt;
====Gate Name Error====&lt;br /&gt;
Previous JSON backend didn’t set gate name correctly. Thus when having multiple assignment group (&amp;gt;3) and at least one hub threshold is higher than current user’s XP point, JavaScript frontend falls into infinite loop.&lt;br /&gt;
&lt;br /&gt;
We fix this bug by&lt;br /&gt;
&lt;br /&gt;
The following is the related code:&lt;br /&gt;
&lt;br /&gt;
====Assignment Group Error====&lt;br /&gt;
In previous JSON backend, if an assignment group has X assignment in it, it will return X copy of that an assignment group, thus there’ll be X*X assignment in total. X times greater than it should be.&lt;br /&gt;
&lt;br /&gt;
We fix this bug by&lt;br /&gt;
&lt;br /&gt;
The following is the related code:&lt;br /&gt;
&lt;br /&gt;
===Fixing Minor Bugs===&lt;br /&gt;
* Previous backend send all assignment data to frontend even if the student is not participate in the assignment. &lt;br /&gt;
&lt;br /&gt;
* Previous JSON backend didn’t set MIME type correctly. That result in frontend code can only use low layer API to capture JSON data, when using high layer API, a silent error will occur.&lt;br /&gt;
&lt;br /&gt;
* Previous MIME type result in IE and Chrome has different behavior.&lt;br /&gt;
&lt;br /&gt;
* Sending a JSON string “null” to frontend will make it stop responding.&lt;br /&gt;
&lt;br /&gt;
==General Introduction to the System Setup==&lt;/div&gt;</summary>
		<author><name>Cyang14</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E629&amp;diff=74376</id>
		<title>CSC/ECE 517 Spring 2013/OSS E629</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E629&amp;diff=74376"/>
		<updated>2013-03-23T17:57:08Z</updated>

		<summary type="html">&lt;p&gt;Cyang14: /* Assignment Group Error */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=E629: Integrating game frontend for Expertiza=&lt;br /&gt;
&lt;br /&gt;
==Project Introduction==&lt;br /&gt;
This is a game made for the Expertiza system (Ed Gehringer, NCSU). It transforms an assignment-based course into a &amp;quot;game&amp;quot; in which the player levels up and completes areas. The player doesn't receive grades; rather, he gains experience, and the professor would convert the experience values of the players at the end of the course into grades. The map is split up into distinct &amp;quot;rooms&amp;quot; unlocked by completing assignments; these are arrayed around a &amp;quot;center&amp;quot; in the middle of the map which contains portals to each room (portals which unlock as the player progresses).&lt;br /&gt;
&lt;br /&gt;
==Project Requirement==&lt;br /&gt;
Students will need to develop a page that runs the Expertiza game in JavaScript when a user is logged in and calls the code from E407 to get the JSON string to be parsed by the frontend.&lt;br /&gt;
&lt;br /&gt;
The page should basically load the frontend JavaScript and point it to the JSON string (a URL that will check to make sure that the current user is logged in correctly).&lt;br /&gt;
&lt;br /&gt;
[https://docs.google.com/a/ncsu.edu/document/d/1SVNx7Eh6dUdrz2a9rGSgBxLkxAmuBUm8eu08nHEdjIg/edit# Project Description]&lt;br /&gt;
&lt;br /&gt;
==Our Work==&lt;br /&gt;
&lt;br /&gt;
===Experience Point Report===&lt;br /&gt;
In this game, the player doesn't receive grades; rather, he gains experience(XP), and the professor would convert the experience values of the players at the end of the course into grades.&lt;br /&gt;
&lt;br /&gt;
The Previous XP is a fake one, they just send the &amp;quot;grade&amp;quot; score as the XP point. So one part of work is to rewrite this part.&lt;br /&gt;
&lt;br /&gt;
The new XP point system is a weighted score system, where instructor can assign or automatically generate the XP point.&lt;br /&gt;
&lt;br /&gt;
The following is the code for XP point report system:&lt;br /&gt;
&lt;br /&gt;
===Assignment Group Modules===&lt;br /&gt;
Assignment group module is used to&lt;br /&gt;
&lt;br /&gt;
Previous assignment group from the previous project E407 is a fake module. In this version, the code to decide whether an assignment group is “available” to a student should appear in both frontend and backend, but neither of them works. So we rewrite it and assign privileges to it.&lt;br /&gt;
&lt;br /&gt;
The following is the code for the assignment group module:&lt;br /&gt;
&lt;br /&gt;
===JavaScript Code Reconstruction===&lt;br /&gt;
When the game is running on the Expertiza system, Expertiza should send the game the info it needs to generate the level and game state at load-time. The page should basically load the frontend JavaScript and point it to the JSON string.&lt;br /&gt;
&lt;br /&gt;
Previous system get JSON by polling the server. In this strategy,  the computer does nothing other than check the status of the backend until it is ready, at which point the device is accessed.  In order to improve the efficiency of the CPU, we introduced Event Driven Programming (EDP) into our design.   In EDP, the flow of the program is determined by events. In our case, a callback closure is called when the JSON data hit the frontend.&lt;br /&gt;
&lt;br /&gt;
We also Change the ig module a little bit to support visiting module variable in closure.&lt;br /&gt;
&lt;br /&gt;
The related code is as following:&lt;br /&gt;
&lt;br /&gt;
===Fixing Mega Bugs===&lt;br /&gt;
====Uncaught Exception====&lt;br /&gt;
Previous JSON backend throws an uncaught exception when the user has an unsubmitted assignment and he is participating in that assignment.&lt;br /&gt;
&lt;br /&gt;
We fix this bug by&lt;br /&gt;
&lt;br /&gt;
The following is the related code:&lt;br /&gt;
&lt;br /&gt;
====Gate Name Error====&lt;br /&gt;
Previous JSON backend didn’t set gate name correctly. Thus when having multiple assignment group (&amp;gt;3) and at least one hub threshold is higher than current user’s XP point, JavaScript frontend falls into infinite loop.&lt;br /&gt;
&lt;br /&gt;
We fix this bug by&lt;br /&gt;
&lt;br /&gt;
The following is the related code:&lt;br /&gt;
&lt;br /&gt;
====Assignment Group Error====&lt;br /&gt;
In previous JSON backend, if an assignment group has X assignment in it, it will return X copy of that an assignment group, thus there’ll be X*X assignment in total. X times greater than it should be.&lt;br /&gt;
&lt;br /&gt;
We fix this bug by&lt;br /&gt;
&lt;br /&gt;
The following is the related code:&lt;br /&gt;
&lt;br /&gt;
===Fixing Minor Bugs===&lt;br /&gt;
&lt;br /&gt;
==General Introduction to the System Setup==&lt;/div&gt;</summary>
		<author><name>Cyang14</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E629&amp;diff=74375</id>
		<title>CSC/ECE 517 Spring 2013/OSS E629</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E629&amp;diff=74375"/>
		<updated>2013-03-23T17:56:19Z</updated>

		<summary type="html">&lt;p&gt;Cyang14: /* Gate Name Error */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=E629: Integrating game frontend for Expertiza=&lt;br /&gt;
&lt;br /&gt;
==Project Introduction==&lt;br /&gt;
This is a game made for the Expertiza system (Ed Gehringer, NCSU). It transforms an assignment-based course into a &amp;quot;game&amp;quot; in which the player levels up and completes areas. The player doesn't receive grades; rather, he gains experience, and the professor would convert the experience values of the players at the end of the course into grades. The map is split up into distinct &amp;quot;rooms&amp;quot; unlocked by completing assignments; these are arrayed around a &amp;quot;center&amp;quot; in the middle of the map which contains portals to each room (portals which unlock as the player progresses).&lt;br /&gt;
&lt;br /&gt;
==Project Requirement==&lt;br /&gt;
Students will need to develop a page that runs the Expertiza game in JavaScript when a user is logged in and calls the code from E407 to get the JSON string to be parsed by the frontend.&lt;br /&gt;
&lt;br /&gt;
The page should basically load the frontend JavaScript and point it to the JSON string (a URL that will check to make sure that the current user is logged in correctly).&lt;br /&gt;
&lt;br /&gt;
[https://docs.google.com/a/ncsu.edu/document/d/1SVNx7Eh6dUdrz2a9rGSgBxLkxAmuBUm8eu08nHEdjIg/edit# Project Description]&lt;br /&gt;
&lt;br /&gt;
==Our Work==&lt;br /&gt;
&lt;br /&gt;
===Experience Point Report===&lt;br /&gt;
In this game, the player doesn't receive grades; rather, he gains experience(XP), and the professor would convert the experience values of the players at the end of the course into grades.&lt;br /&gt;
&lt;br /&gt;
The Previous XP is a fake one, they just send the &amp;quot;grade&amp;quot; score as the XP point. So one part of work is to rewrite this part.&lt;br /&gt;
&lt;br /&gt;
The new XP point system is a weighted score system, where instructor can assign or automatically generate the XP point.&lt;br /&gt;
&lt;br /&gt;
The following is the code for XP point report system:&lt;br /&gt;
&lt;br /&gt;
===Assignment Group Modules===&lt;br /&gt;
Assignment group module is used to&lt;br /&gt;
&lt;br /&gt;
Previous assignment group from the previous project E407 is a fake module. In this version, the code to decide whether an assignment group is “available” to a student should appear in both frontend and backend, but neither of them works. So we rewrite it and assign privileges to it.&lt;br /&gt;
&lt;br /&gt;
The following is the code for the assignment group module:&lt;br /&gt;
&lt;br /&gt;
===JavaScript Code Reconstruction===&lt;br /&gt;
When the game is running on the Expertiza system, Expertiza should send the game the info it needs to generate the level and game state at load-time. The page should basically load the frontend JavaScript and point it to the JSON string.&lt;br /&gt;
&lt;br /&gt;
Previous system get JSON by polling the server. In this strategy,  the computer does nothing other than check the status of the backend until it is ready, at which point the device is accessed.  In order to improve the efficiency of the CPU, we introduced Event Driven Programming (EDP) into our design.   In EDP, the flow of the program is determined by events. In our case, a callback closure is called when the JSON data hit the frontend.&lt;br /&gt;
&lt;br /&gt;
We also Change the ig module a little bit to support visiting module variable in closure.&lt;br /&gt;
&lt;br /&gt;
The related code is as following:&lt;br /&gt;
&lt;br /&gt;
===Fixing Mega Bugs===&lt;br /&gt;
====Uncaught Exception====&lt;br /&gt;
Previous JSON backend throws an uncaught exception when the user has an unsubmitted assignment and he is participating in that assignment.&lt;br /&gt;
&lt;br /&gt;
We fix this bug by&lt;br /&gt;
&lt;br /&gt;
The following is the related code:&lt;br /&gt;
&lt;br /&gt;
====Gate Name Error====&lt;br /&gt;
Previous JSON backend didn’t set gate name correctly. Thus when having multiple assignment group (&amp;gt;3) and at least one hub threshold is higher than current user’s XP point, JavaScript frontend falls into infinite loop.&lt;br /&gt;
&lt;br /&gt;
We fix this bug by&lt;br /&gt;
&lt;br /&gt;
The following is the related code:&lt;br /&gt;
&lt;br /&gt;
====Assignment Group Error====&lt;br /&gt;
&lt;br /&gt;
===Fixing Minor Bugs===&lt;br /&gt;
&lt;br /&gt;
==General Introduction to the System Setup==&lt;/div&gt;</summary>
		<author><name>Cyang14</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E629&amp;diff=74374</id>
		<title>CSC/ECE 517 Spring 2013/OSS E629</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E629&amp;diff=74374"/>
		<updated>2013-03-23T17:55:26Z</updated>

		<summary type="html">&lt;p&gt;Cyang14: /* Uncaught Exception */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=E629: Integrating game frontend for Expertiza=&lt;br /&gt;
&lt;br /&gt;
==Project Introduction==&lt;br /&gt;
This is a game made for the Expertiza system (Ed Gehringer, NCSU). It transforms an assignment-based course into a &amp;quot;game&amp;quot; in which the player levels up and completes areas. The player doesn't receive grades; rather, he gains experience, and the professor would convert the experience values of the players at the end of the course into grades. The map is split up into distinct &amp;quot;rooms&amp;quot; unlocked by completing assignments; these are arrayed around a &amp;quot;center&amp;quot; in the middle of the map which contains portals to each room (portals which unlock as the player progresses).&lt;br /&gt;
&lt;br /&gt;
==Project Requirement==&lt;br /&gt;
Students will need to develop a page that runs the Expertiza game in JavaScript when a user is logged in and calls the code from E407 to get the JSON string to be parsed by the frontend.&lt;br /&gt;
&lt;br /&gt;
The page should basically load the frontend JavaScript and point it to the JSON string (a URL that will check to make sure that the current user is logged in correctly).&lt;br /&gt;
&lt;br /&gt;
[https://docs.google.com/a/ncsu.edu/document/d/1SVNx7Eh6dUdrz2a9rGSgBxLkxAmuBUm8eu08nHEdjIg/edit# Project Description]&lt;br /&gt;
&lt;br /&gt;
==Our Work==&lt;br /&gt;
&lt;br /&gt;
===Experience Point Report===&lt;br /&gt;
In this game, the player doesn't receive grades; rather, he gains experience(XP), and the professor would convert the experience values of the players at the end of the course into grades.&lt;br /&gt;
&lt;br /&gt;
The Previous XP is a fake one, they just send the &amp;quot;grade&amp;quot; score as the XP point. So one part of work is to rewrite this part.&lt;br /&gt;
&lt;br /&gt;
The new XP point system is a weighted score system, where instructor can assign or automatically generate the XP point.&lt;br /&gt;
&lt;br /&gt;
The following is the code for XP point report system:&lt;br /&gt;
&lt;br /&gt;
===Assignment Group Modules===&lt;br /&gt;
Assignment group module is used to&lt;br /&gt;
&lt;br /&gt;
Previous assignment group from the previous project E407 is a fake module. In this version, the code to decide whether an assignment group is “available” to a student should appear in both frontend and backend, but neither of them works. So we rewrite it and assign privileges to it.&lt;br /&gt;
&lt;br /&gt;
The following is the code for the assignment group module:&lt;br /&gt;
&lt;br /&gt;
===JavaScript Code Reconstruction===&lt;br /&gt;
When the game is running on the Expertiza system, Expertiza should send the game the info it needs to generate the level and game state at load-time. The page should basically load the frontend JavaScript and point it to the JSON string.&lt;br /&gt;
&lt;br /&gt;
Previous system get JSON by polling the server. In this strategy,  the computer does nothing other than check the status of the backend until it is ready, at which point the device is accessed.  In order to improve the efficiency of the CPU, we introduced Event Driven Programming (EDP) into our design.   In EDP, the flow of the program is determined by events. In our case, a callback closure is called when the JSON data hit the frontend.&lt;br /&gt;
&lt;br /&gt;
We also Change the ig module a little bit to support visiting module variable in closure.&lt;br /&gt;
&lt;br /&gt;
The related code is as following:&lt;br /&gt;
&lt;br /&gt;
===Fixing Mega Bugs===&lt;br /&gt;
====Uncaught Exception====&lt;br /&gt;
Previous JSON backend throws an uncaught exception when the user has an unsubmitted assignment and he is participating in that assignment.&lt;br /&gt;
&lt;br /&gt;
We fix this bug by&lt;br /&gt;
&lt;br /&gt;
The following is the related code:&lt;br /&gt;
&lt;br /&gt;
====Gate Name Error==== &lt;br /&gt;
&lt;br /&gt;
====Assignment Group Error====&lt;br /&gt;
&lt;br /&gt;
===Fixing Minor Bugs===&lt;br /&gt;
&lt;br /&gt;
==General Introduction to the System Setup==&lt;/div&gt;</summary>
		<author><name>Cyang14</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E629&amp;diff=74373</id>
		<title>CSC/ECE 517 Spring 2013/OSS E629</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E629&amp;diff=74373"/>
		<updated>2013-03-23T17:53:20Z</updated>

		<summary type="html">&lt;p&gt;Cyang14: /* Fixing Mega Bugs */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=E629: Integrating game frontend for Expertiza=&lt;br /&gt;
&lt;br /&gt;
==Project Introduction==&lt;br /&gt;
This is a game made for the Expertiza system (Ed Gehringer, NCSU). It transforms an assignment-based course into a &amp;quot;game&amp;quot; in which the player levels up and completes areas. The player doesn't receive grades; rather, he gains experience, and the professor would convert the experience values of the players at the end of the course into grades. The map is split up into distinct &amp;quot;rooms&amp;quot; unlocked by completing assignments; these are arrayed around a &amp;quot;center&amp;quot; in the middle of the map which contains portals to each room (portals which unlock as the player progresses).&lt;br /&gt;
&lt;br /&gt;
==Project Requirement==&lt;br /&gt;
Students will need to develop a page that runs the Expertiza game in JavaScript when a user is logged in and calls the code from E407 to get the JSON string to be parsed by the frontend.&lt;br /&gt;
&lt;br /&gt;
The page should basically load the frontend JavaScript and point it to the JSON string (a URL that will check to make sure that the current user is logged in correctly).&lt;br /&gt;
&lt;br /&gt;
[https://docs.google.com/a/ncsu.edu/document/d/1SVNx7Eh6dUdrz2a9rGSgBxLkxAmuBUm8eu08nHEdjIg/edit# Project Description]&lt;br /&gt;
&lt;br /&gt;
==Our Work==&lt;br /&gt;
&lt;br /&gt;
===Experience Point Report===&lt;br /&gt;
In this game, the player doesn't receive grades; rather, he gains experience(XP), and the professor would convert the experience values of the players at the end of the course into grades.&lt;br /&gt;
&lt;br /&gt;
The Previous XP is a fake one, they just send the &amp;quot;grade&amp;quot; score as the XP point. So one part of work is to rewrite this part.&lt;br /&gt;
&lt;br /&gt;
The new XP point system is a weighted score system, where instructor can assign or automatically generate the XP point.&lt;br /&gt;
&lt;br /&gt;
The following is the code for XP point report system:&lt;br /&gt;
&lt;br /&gt;
===Assignment Group Modules===&lt;br /&gt;
Assignment group module is used to&lt;br /&gt;
&lt;br /&gt;
Previous assignment group from the previous project E407 is a fake module. In this version, the code to decide whether an assignment group is “available” to a student should appear in both frontend and backend, but neither of them works. So we rewrite it and assign privileges to it.&lt;br /&gt;
&lt;br /&gt;
The following is the code for the assignment group module:&lt;br /&gt;
&lt;br /&gt;
===JavaScript Code Reconstruction===&lt;br /&gt;
When the game is running on the Expertiza system, Expertiza should send the game the info it needs to generate the level and game state at load-time. The page should basically load the frontend JavaScript and point it to the JSON string.&lt;br /&gt;
&lt;br /&gt;
Previous system get JSON by polling the server. In this strategy,  the computer does nothing other than check the status of the backend until it is ready, at which point the device is accessed.  In order to improve the efficiency of the CPU, we introduced Event Driven Programming (EDP) into our design.   In EDP, the flow of the program is determined by events. In our case, a callback closure is called when the JSON data hit the frontend.&lt;br /&gt;
&lt;br /&gt;
We also Change the ig module a little bit to support visiting module variable in closure.&lt;br /&gt;
&lt;br /&gt;
The related code is as following:&lt;br /&gt;
&lt;br /&gt;
===Fixing Mega Bugs===&lt;br /&gt;
====Uncaught Exception====&lt;br /&gt;
&lt;br /&gt;
====Gate Name Error==== &lt;br /&gt;
&lt;br /&gt;
====Assignment Group Error====&lt;br /&gt;
&lt;br /&gt;
===Fixing Minor Bugs===&lt;br /&gt;
&lt;br /&gt;
==General Introduction to the System Setup==&lt;/div&gt;</summary>
		<author><name>Cyang14</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E629&amp;diff=74372</id>
		<title>CSC/ECE 517 Spring 2013/OSS E629</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E629&amp;diff=74372"/>
		<updated>2013-03-23T17:50:48Z</updated>

		<summary type="html">&lt;p&gt;Cyang14: /* JavaScript Code Reconstruction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=E629: Integrating game frontend for Expertiza=&lt;br /&gt;
&lt;br /&gt;
==Project Introduction==&lt;br /&gt;
This is a game made for the Expertiza system (Ed Gehringer, NCSU). It transforms an assignment-based course into a &amp;quot;game&amp;quot; in which the player levels up and completes areas. The player doesn't receive grades; rather, he gains experience, and the professor would convert the experience values of the players at the end of the course into grades. The map is split up into distinct &amp;quot;rooms&amp;quot; unlocked by completing assignments; these are arrayed around a &amp;quot;center&amp;quot; in the middle of the map which contains portals to each room (portals which unlock as the player progresses).&lt;br /&gt;
&lt;br /&gt;
==Project Requirement==&lt;br /&gt;
Students will need to develop a page that runs the Expertiza game in JavaScript when a user is logged in and calls the code from E407 to get the JSON string to be parsed by the frontend.&lt;br /&gt;
&lt;br /&gt;
The page should basically load the frontend JavaScript and point it to the JSON string (a URL that will check to make sure that the current user is logged in correctly).&lt;br /&gt;
&lt;br /&gt;
[https://docs.google.com/a/ncsu.edu/document/d/1SVNx7Eh6dUdrz2a9rGSgBxLkxAmuBUm8eu08nHEdjIg/edit# Project Description]&lt;br /&gt;
&lt;br /&gt;
==Our Work==&lt;br /&gt;
&lt;br /&gt;
===Experience Point Report===&lt;br /&gt;
In this game, the player doesn't receive grades; rather, he gains experience(XP), and the professor would convert the experience values of the players at the end of the course into grades.&lt;br /&gt;
&lt;br /&gt;
The Previous XP is a fake one, they just send the &amp;quot;grade&amp;quot; score as the XP point. So one part of work is to rewrite this part.&lt;br /&gt;
&lt;br /&gt;
The new XP point system is a weighted score system, where instructor can assign or automatically generate the XP point.&lt;br /&gt;
&lt;br /&gt;
The following is the code for XP point report system:&lt;br /&gt;
&lt;br /&gt;
===Assignment Group Modules===&lt;br /&gt;
Assignment group module is used to&lt;br /&gt;
&lt;br /&gt;
Previous assignment group from the previous project E407 is a fake module. In this version, the code to decide whether an assignment group is “available” to a student should appear in both frontend and backend, but neither of them works. So we rewrite it and assign privileges to it.&lt;br /&gt;
&lt;br /&gt;
The following is the code for the assignment group module:&lt;br /&gt;
&lt;br /&gt;
===JavaScript Code Reconstruction===&lt;br /&gt;
When the game is running on the Expertiza system, Expertiza should send the game the info it needs to generate the level and game state at load-time. The page should basically load the frontend JavaScript and point it to the JSON string.&lt;br /&gt;
&lt;br /&gt;
Previous system get JSON by polling the server. In this strategy,  the computer does nothing other than check the status of the backend until it is ready, at which point the device is accessed.  In order to improve the efficiency of the CPU, we introduced Event Driven Programming (EDP) into our design.   In EDP, the flow of the program is determined by events. In our case, a callback closure is called when the JSON data hit the frontend.&lt;br /&gt;
&lt;br /&gt;
We also Change the ig module a little bit to support visiting module variable in closure.&lt;br /&gt;
&lt;br /&gt;
The related code is as following:&lt;br /&gt;
&lt;br /&gt;
===Fixing Mega Bugs===&lt;br /&gt;
&lt;br /&gt;
===Fixing Minor Bugs===&lt;br /&gt;
&lt;br /&gt;
==General Introduction to the System Setup==&lt;/div&gt;</summary>
		<author><name>Cyang14</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E629&amp;diff=74371</id>
		<title>CSC/ECE 517 Spring 2013/OSS E629</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E629&amp;diff=74371"/>
		<updated>2013-03-23T17:50:27Z</updated>

		<summary type="html">&lt;p&gt;Cyang14: /* Assignment Group Modules */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=E629: Integrating game frontend for Expertiza=&lt;br /&gt;
&lt;br /&gt;
==Project Introduction==&lt;br /&gt;
This is a game made for the Expertiza system (Ed Gehringer, NCSU). It transforms an assignment-based course into a &amp;quot;game&amp;quot; in which the player levels up and completes areas. The player doesn't receive grades; rather, he gains experience, and the professor would convert the experience values of the players at the end of the course into grades. The map is split up into distinct &amp;quot;rooms&amp;quot; unlocked by completing assignments; these are arrayed around a &amp;quot;center&amp;quot; in the middle of the map which contains portals to each room (portals which unlock as the player progresses).&lt;br /&gt;
&lt;br /&gt;
==Project Requirement==&lt;br /&gt;
Students will need to develop a page that runs the Expertiza game in JavaScript when a user is logged in and calls the code from E407 to get the JSON string to be parsed by the frontend.&lt;br /&gt;
&lt;br /&gt;
The page should basically load the frontend JavaScript and point it to the JSON string (a URL that will check to make sure that the current user is logged in correctly).&lt;br /&gt;
&lt;br /&gt;
[https://docs.google.com/a/ncsu.edu/document/d/1SVNx7Eh6dUdrz2a9rGSgBxLkxAmuBUm8eu08nHEdjIg/edit# Project Description]&lt;br /&gt;
&lt;br /&gt;
==Our Work==&lt;br /&gt;
&lt;br /&gt;
===Experience Point Report===&lt;br /&gt;
In this game, the player doesn't receive grades; rather, he gains experience(XP), and the professor would convert the experience values of the players at the end of the course into grades.&lt;br /&gt;
&lt;br /&gt;
The Previous XP is a fake one, they just send the &amp;quot;grade&amp;quot; score as the XP point. So one part of work is to rewrite this part.&lt;br /&gt;
&lt;br /&gt;
The new XP point system is a weighted score system, where instructor can assign or automatically generate the XP point.&lt;br /&gt;
&lt;br /&gt;
The following is the code for XP point report system:&lt;br /&gt;
&lt;br /&gt;
===Assignment Group Modules===&lt;br /&gt;
Assignment group module is used to&lt;br /&gt;
&lt;br /&gt;
Previous assignment group from the previous project E407 is a fake module. In this version, the code to decide whether an assignment group is “available” to a student should appear in both frontend and backend, but neither of them works. So we rewrite it and assign privileges to it.&lt;br /&gt;
&lt;br /&gt;
The following is the code for the assignment group module:&lt;br /&gt;
&lt;br /&gt;
===JavaScript Code Reconstruction===&lt;br /&gt;
&lt;br /&gt;
===Fixing Mega Bugs===&lt;br /&gt;
&lt;br /&gt;
===Fixing Minor Bugs===&lt;br /&gt;
&lt;br /&gt;
==General Introduction to the System Setup==&lt;/div&gt;</summary>
		<author><name>Cyang14</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E629&amp;diff=74370</id>
		<title>CSC/ECE 517 Spring 2013/OSS E629</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E629&amp;diff=74370"/>
		<updated>2013-03-23T17:49:46Z</updated>

		<summary type="html">&lt;p&gt;Cyang14: /* Experience Point Report */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=E629: Integrating game frontend for Expertiza=&lt;br /&gt;
&lt;br /&gt;
==Project Introduction==&lt;br /&gt;
This is a game made for the Expertiza system (Ed Gehringer, NCSU). It transforms an assignment-based course into a &amp;quot;game&amp;quot; in which the player levels up and completes areas. The player doesn't receive grades; rather, he gains experience, and the professor would convert the experience values of the players at the end of the course into grades. The map is split up into distinct &amp;quot;rooms&amp;quot; unlocked by completing assignments; these are arrayed around a &amp;quot;center&amp;quot; in the middle of the map which contains portals to each room (portals which unlock as the player progresses).&lt;br /&gt;
&lt;br /&gt;
==Project Requirement==&lt;br /&gt;
Students will need to develop a page that runs the Expertiza game in JavaScript when a user is logged in and calls the code from E407 to get the JSON string to be parsed by the frontend.&lt;br /&gt;
&lt;br /&gt;
The page should basically load the frontend JavaScript and point it to the JSON string (a URL that will check to make sure that the current user is logged in correctly).&lt;br /&gt;
&lt;br /&gt;
[https://docs.google.com/a/ncsu.edu/document/d/1SVNx7Eh6dUdrz2a9rGSgBxLkxAmuBUm8eu08nHEdjIg/edit# Project Description]&lt;br /&gt;
&lt;br /&gt;
==Our Work==&lt;br /&gt;
&lt;br /&gt;
===Experience Point Report===&lt;br /&gt;
In this game, the player doesn't receive grades; rather, he gains experience(XP), and the professor would convert the experience values of the players at the end of the course into grades.&lt;br /&gt;
&lt;br /&gt;
The Previous XP is a fake one, they just send the &amp;quot;grade&amp;quot; score as the XP point. So one part of work is to rewrite this part.&lt;br /&gt;
&lt;br /&gt;
The new XP point system is a weighted score system, where instructor can assign or automatically generate the XP point.&lt;br /&gt;
&lt;br /&gt;
The following is the code for XP point report system:&lt;br /&gt;
&lt;br /&gt;
===Assignment Group Modules===&lt;br /&gt;
&lt;br /&gt;
===JavaScript Code Reconstruction===&lt;br /&gt;
&lt;br /&gt;
===Fixing Mega Bugs===&lt;br /&gt;
&lt;br /&gt;
===Fixing Minor Bugs===&lt;br /&gt;
&lt;br /&gt;
==General Introduction to the System Setup==&lt;/div&gt;</summary>
		<author><name>Cyang14</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E629&amp;diff=74369</id>
		<title>CSC/ECE 517 Spring 2013/OSS E629</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E629&amp;diff=74369"/>
		<updated>2013-03-23T17:48:54Z</updated>

		<summary type="html">&lt;p&gt;Cyang14: /* Project Requirement */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=E629: Integrating game frontend for Expertiza=&lt;br /&gt;
&lt;br /&gt;
==Project Introduction==&lt;br /&gt;
This is a game made for the Expertiza system (Ed Gehringer, NCSU). It transforms an assignment-based course into a &amp;quot;game&amp;quot; in which the player levels up and completes areas. The player doesn't receive grades; rather, he gains experience, and the professor would convert the experience values of the players at the end of the course into grades. The map is split up into distinct &amp;quot;rooms&amp;quot; unlocked by completing assignments; these are arrayed around a &amp;quot;center&amp;quot; in the middle of the map which contains portals to each room (portals which unlock as the player progresses).&lt;br /&gt;
&lt;br /&gt;
==Project Requirement==&lt;br /&gt;
Students will need to develop a page that runs the Expertiza game in JavaScript when a user is logged in and calls the code from E407 to get the JSON string to be parsed by the frontend.&lt;br /&gt;
&lt;br /&gt;
The page should basically load the frontend JavaScript and point it to the JSON string (a URL that will check to make sure that the current user is logged in correctly).&lt;br /&gt;
&lt;br /&gt;
[https://docs.google.com/a/ncsu.edu/document/d/1SVNx7Eh6dUdrz2a9rGSgBxLkxAmuBUm8eu08nHEdjIg/edit# Project Description]&lt;br /&gt;
&lt;br /&gt;
==Our Work==&lt;br /&gt;
&lt;br /&gt;
===Experience Point Report===&lt;br /&gt;
&lt;br /&gt;
===Assignment Group Modules===&lt;br /&gt;
&lt;br /&gt;
===JavaScript Code Reconstruction===&lt;br /&gt;
&lt;br /&gt;
===Fixing Mega Bugs===&lt;br /&gt;
&lt;br /&gt;
===Fixing Minor Bugs===&lt;br /&gt;
&lt;br /&gt;
==General Introduction to the System Setup==&lt;/div&gt;</summary>
		<author><name>Cyang14</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E629&amp;diff=74368</id>
		<title>CSC/ECE 517 Spring 2013/OSS E629</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E629&amp;diff=74368"/>
		<updated>2013-03-23T17:48:16Z</updated>

		<summary type="html">&lt;p&gt;Cyang14: /* Project Requirement */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=E629: Integrating game frontend for Expertiza=&lt;br /&gt;
&lt;br /&gt;
==Project Introduction==&lt;br /&gt;
This is a game made for the Expertiza system (Ed Gehringer, NCSU). It transforms an assignment-based course into a &amp;quot;game&amp;quot; in which the player levels up and completes areas. The player doesn't receive grades; rather, he gains experience, and the professor would convert the experience values of the players at the end of the course into grades. The map is split up into distinct &amp;quot;rooms&amp;quot; unlocked by completing assignments; these are arrayed around a &amp;quot;center&amp;quot; in the middle of the map which contains portals to each room (portals which unlock as the player progresses).&lt;br /&gt;
&lt;br /&gt;
==Project Requirement==&lt;br /&gt;
Students will need to develop a page that runs the Expertiza game in JavaScript when a user is logged in and calls the code from E407 to get the JSON string to be parsed by the frontend.&lt;br /&gt;
&lt;br /&gt;
The page should basically load the frontend JavaScript and point it to the JSON string (a URL that will check to make sure that the current user is logged in correctly).&lt;br /&gt;
&lt;br /&gt;
==Our Work==&lt;br /&gt;
&lt;br /&gt;
===Experience Point Report===&lt;br /&gt;
&lt;br /&gt;
===Assignment Group Modules===&lt;br /&gt;
&lt;br /&gt;
===JavaScript Code Reconstruction===&lt;br /&gt;
&lt;br /&gt;
===Fixing Mega Bugs===&lt;br /&gt;
&lt;br /&gt;
===Fixing Minor Bugs===&lt;br /&gt;
&lt;br /&gt;
==General Introduction to the System Setup==&lt;/div&gt;</summary>
		<author><name>Cyang14</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E629&amp;diff=74367</id>
		<title>CSC/ECE 517 Spring 2013/OSS E629</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E629&amp;diff=74367"/>
		<updated>2013-03-23T17:47:42Z</updated>

		<summary type="html">&lt;p&gt;Cyang14: /* Project Introduction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=E629: Integrating game frontend for Expertiza=&lt;br /&gt;
&lt;br /&gt;
==Project Introduction==&lt;br /&gt;
This is a game made for the Expertiza system (Ed Gehringer, NCSU). It transforms an assignment-based course into a &amp;quot;game&amp;quot; in which the player levels up and completes areas. The player doesn't receive grades; rather, he gains experience, and the professor would convert the experience values of the players at the end of the course into grades. The map is split up into distinct &amp;quot;rooms&amp;quot; unlocked by completing assignments; these are arrayed around a &amp;quot;center&amp;quot; in the middle of the map which contains portals to each room (portals which unlock as the player progresses).&lt;br /&gt;
&lt;br /&gt;
==Project Requirement==&lt;br /&gt;
&lt;br /&gt;
==Our Work==&lt;br /&gt;
&lt;br /&gt;
===Experience Point Report===&lt;br /&gt;
&lt;br /&gt;
===Assignment Group Modules===&lt;br /&gt;
&lt;br /&gt;
===JavaScript Code Reconstruction===&lt;br /&gt;
&lt;br /&gt;
===Fixing Mega Bugs===&lt;br /&gt;
&lt;br /&gt;
===Fixing Minor Bugs===&lt;br /&gt;
&lt;br /&gt;
==General Introduction to the System Setup==&lt;/div&gt;</summary>
		<author><name>Cyang14</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E629&amp;diff=74366</id>
		<title>CSC/ECE 517 Spring 2013/OSS E629</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E629&amp;diff=74366"/>
		<updated>2013-03-23T17:47:02Z</updated>

		<summary type="html">&lt;p&gt;Cyang14: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=E629: Integrating game frontend for Expertiza=&lt;br /&gt;
&lt;br /&gt;
==Project Introduction==&lt;br /&gt;
&lt;br /&gt;
==Project Requirement==&lt;br /&gt;
&lt;br /&gt;
==Our Work==&lt;br /&gt;
&lt;br /&gt;
===Experience Point Report===&lt;br /&gt;
&lt;br /&gt;
===Assignment Group Modules===&lt;br /&gt;
&lt;br /&gt;
===JavaScript Code Reconstruction===&lt;br /&gt;
&lt;br /&gt;
===Fixing Mega Bugs===&lt;br /&gt;
&lt;br /&gt;
===Fixing Minor Bugs===&lt;br /&gt;
&lt;br /&gt;
==General Introduction to the System Setup==&lt;/div&gt;</summary>
		<author><name>Cyang14</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E629&amp;diff=74365</id>
		<title>CSC/ECE 517 Spring 2013/OSS E629</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E629&amp;diff=74365"/>
		<updated>2013-03-23T16:52:51Z</updated>

		<summary type="html">&lt;p&gt;Cyang14: Created page with &amp;quot;=E629: Integrating game frontend for Expertiza=&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=E629: Integrating game frontend for Expertiza=&lt;/div&gt;</summary>
		<author><name>Cyang14</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1_1h_jc&amp;diff=73628</id>
		<title>CSC/ECE 517 Spring 2013/ch1 1h jc</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1_1h_jc&amp;diff=73628"/>
		<updated>2013-02-24T16:55:33Z</updated>

		<summary type="html">&lt;p&gt;Cyang14: /* Mini-language for boiler-plate */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Metaprogramming in statically typed languages&lt;br /&gt;
=Metaprogramming in statically typed languages=&lt;br /&gt;
==Introduction==&lt;br /&gt;
===What is metaprogramming===&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming] refers to writing programs that can manipulate other programs or themselves. The program that is doing the manipulating is called the ''metaprogram'', and the program that is being manipulated is called the ''object program''. The language of the ''metaprogram'' is referred to as the ''metalanguage'' and the language of the object program is called the ''object language''. A language has the ability of [http://en.wikipedia.org/wiki/Reflection_(computer_programming) reflection] if it can be used to write a metaprogram that can manipulate a program written in the same language.&lt;br /&gt;
&lt;br /&gt;
===Metaprogramming in statically typed languages===&lt;br /&gt;
&lt;br /&gt;
Metaprogramming can be accomplished in both statically and dynamically typed languages, however [http://en.wikipedia.org/wiki/Dynamically_typed_language#Dynamic_typing dynamically typed languages] have some advantages over [http://en.wikipedia.org/wiki/Dynamically_typed_language#Dynamic_typing statically typed languages] when it comes to metaprogramming.&lt;br /&gt;
&lt;br /&gt;
One example of a metaprogramming task is to add a method to a particular class at runtime. Sample code in Ruby to perform this task is outlined below. First is a definition of a Ruby class called TestAddMethodAtRuntime:&lt;br /&gt;
&lt;br /&gt;
  class TestAddMethodAtRuntime&lt;br /&gt;
    def originalMethod&lt;br /&gt;
        puts &amp;quot;Original Method&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The code below created a new instance of the class. After that, it defines a new method called newlyAddedMethod. Then the original method is called, follow by the newly added method at runtime:&lt;br /&gt;
&lt;br /&gt;
  t = TestAddMethodAtRuntime.new&lt;br /&gt;
  def t.newlyAddedMethod&lt;br /&gt;
    puts &amp;quot;Newly Added Method&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  t.originalMethod&lt;br /&gt;
  puts &amp;quot;\n&amp;quot;&lt;br /&gt;
  t.newlyAddedMethod&lt;br /&gt;
&lt;br /&gt;
Below is the output of the code above:&lt;br /&gt;
&lt;br /&gt;
  Original Method&lt;br /&gt;
  Newly Added Method&lt;br /&gt;
&lt;br /&gt;
Accomplishing something like this in a statically typed language would be much more difficult or impossible depending on the language. It is for this reason that dynamically typed languages are usually viewed as being better equipped to handle metaprogramming overall.&lt;br /&gt;
&lt;br /&gt;
==Implementation==&lt;br /&gt;
===Programming Language API===&lt;br /&gt;
&lt;br /&gt;
One way that metaprogramming can be possible in a language is through an [http://en.wikipedia.org/wiki/API API] that helps programmers achieve a metaprogramming task. An example of this is a language that provides an API for reflection. One type of reflection is ''introspection'' in which the program can access the source code of the program itself.&lt;br /&gt;
&lt;br /&gt;
Below is an example from the Java API to illustrate how an API can be used to achieve some metaprogramming tasks:&lt;br /&gt;
&lt;br /&gt;
   import java.lang.reflect.*; &lt;br /&gt;
   public class DumpMethods {&lt;br /&gt;
   public static void main(String args[]) {&lt;br /&gt;
     	try {&lt;br /&gt;
           Class c = Class.forName(args[0]);&lt;br /&gt;
           Method m[] = c.getDeclaredMethods();&lt;br /&gt;
           for (int i = 0; i &amp;lt; m.length; i++) {&lt;br /&gt;
              System.out.println(m[i].toString());&lt;br /&gt;
           }&lt;br /&gt;
     	}&lt;br /&gt;
     	catch (Throwable e) {&lt;br /&gt;
        	System.err.println(e);&lt;br /&gt;
     	}&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
The above example prints out all of the methods that are declared for a particular class. This is an example of metaprogramming since the program is accessing the source code of itself.&lt;br /&gt;
&lt;br /&gt;
===Program transformation system===&lt;br /&gt;
&lt;br /&gt;
A program transformation system is something that takes a program as the input, and outputs a different program. One example of a program transformation system is the Java compiler. The Java compiler takes a Java program as its input, and outputs a .class file with platform independent bytecode in it. Another example of a program transformation system is a Java decompiler, which takes bytecode as an input and outputs a Java program.&lt;br /&gt;
&lt;br /&gt;
In the case of the Java compiler, the program input is being output into another program in a different &amp;quot;language&amp;quot;. There are other types of transformation systems that take a program as an input and output a different program in the same language. One example of this would be ''program migration'' which migrates source code to a newer or older version of the same language. This can be useful if upgrading a software system to run on a newer version of a framework.&lt;br /&gt;
&lt;br /&gt;
==Common Uses==&lt;br /&gt;
===Pre-generate static data at compile time===&lt;br /&gt;
One common use of metaprogramming in statically typed languages is to write programs that will pre-generate tables of data for use at runtime.&amp;lt;ref name=&amp;quot;use1&amp;quot;&amp;gt;Jonathan Bartlett, &amp;quot;The art of metaprogramming, Part 1: Introduction to metaprogramming&amp;quot;, 20 Oct 2005   http://www.ibm.com/developerworks/library/l-metaprog1/index.html#N10052&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One simple but useful code generator is to build static lookup tables. Often, in order to build fast functions in C programming, we simply create a lookup table of all of the answers. This means that we either need to pre-compute them by hand (which is wasteful of your time) or build them at runtime (which is wasteful of the user's time).&lt;br /&gt;
&lt;br /&gt;
In following example we will build a generator that will take a function or set of functions on an integer and build lookup tables for the answer.&lt;br /&gt;
&lt;br /&gt;
To think of how to make such a program, we can start from the end and work backward. Firstly we need a lookup table that will return square roots of numbers between 5 and 20. A simple program can be written to generate such a table like this:&lt;br /&gt;
&lt;br /&gt;
Generate and use a lookup table of square roots&lt;br /&gt;
&lt;br /&gt;
        /* our lookup table */&lt;br /&gt;
        double square_roots[21];&lt;br /&gt;
        /* function to load the table at runtime */&lt;br /&gt;
        void init_square_roots()&lt;br /&gt;
        {&lt;br /&gt;
           int i;&lt;br /&gt;
           for(i = 5; i &amp;lt; 21; i++)&lt;br /&gt;
             {&lt;br /&gt;
               square_roots[i] = sqrt((double)i);&lt;br /&gt;
             }&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
With that single macro, we can take away a lot of work for any program that has to generate mathematical tables indexed by integer. A little extra work would also allow tables containing full struct definitions; a little more would ensure that space isn't wasted at the front of the array with useless empty entries.&lt;br /&gt;
        /* program that uses the table */&lt;br /&gt;
        int main ()&lt;br /&gt;
        {&lt;br /&gt;
           init_square_roots();&lt;br /&gt;
           printf(&amp;quot;The square root of 5 is %f\n&amp;quot;, square_roots[5]);&lt;br /&gt;
           return 0;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
Now, to convert this to a statically initialized array, you would remove the first part of the program and replace it with something like this, calculated by hand:&lt;br /&gt;
&lt;br /&gt;
Square root program with a static lookup table&lt;br /&gt;
&lt;br /&gt;
        double square_roots[] = {&lt;br /&gt;
           /* these are the ones we skipped */ 0.0, 0.0, 0.0, 0.0, 0.0&lt;br /&gt;
           2.236068, /* Square root of 5 */&lt;br /&gt;
           2.449490, /* Square root of 6 */&lt;br /&gt;
           2.645751, /* Square root of 7 */&lt;br /&gt;
           2.828427, /* Square root of 8 */&lt;br /&gt;
           3.0, /* Square root of 9 */&lt;br /&gt;
           ...&lt;br /&gt;
           4.472136 /* Square root of 20 */&lt;br /&gt;
           };&lt;br /&gt;
&lt;br /&gt;
What is needed is a program that will produce these values and print them out in a table like the previous one so they are loaded in at compile-time.&lt;br /&gt;
&lt;br /&gt;
Code generator for the table macro&lt;br /&gt;
&lt;br /&gt;
           #!/usr/bin/perl  &lt;br /&gt;
           #&lt;br /&gt;
           #tablegen.pl &lt;br /&gt;
           #&lt;br /&gt;
           ##Puts each program line into $line&lt;br /&gt;
           while(my $line = &amp;lt;&amp;gt;)&lt;br /&gt;
           {&lt;br /&gt;
             #Is this a macro invocation?&lt;br /&gt;
             if($line =~ m/TABLE:/)&lt;br /&gt;
             {&lt;br /&gt;
                #If so, split it apart into its component pieces&lt;br /&gt;
                my ($dummy, $table_name, $type, $start_idx, $end_idx, $default, $procedure) = split(m/:/, $line, 7);&lt;br /&gt;
                #The main difference between C and Perl for mathematical expressions is that&lt;br /&gt;
                #Perl prefixes its variables with a dollar sign, so we will add that here&lt;br /&gt;
                $procedure =~ s/VAL/\$VAL/g;&lt;br /&gt;
                #Print out the array declaration&lt;br /&gt;
                print &amp;quot;${type} ${table_name} [] = {\n&amp;quot;;&lt;br /&gt;
                #Go through each array element&lt;br /&gt;
                foreach my $VAL (0 .. $end_idx)&lt;br /&gt;
                {&lt;br /&gt;
                   #Only process an answer if we have reached our starting index&lt;br /&gt;
                   if($VAL &amp;gt;= $start_idx)&lt;br /&gt;
                   {&lt;br /&gt;
                      #evaluate the procedure specified (this sets $@ if there are any errors)&lt;br /&gt;
                      $result = eval $procedure;&lt;br /&gt;
                      die(&amp;quot;Error processing: $@&amp;quot;) if $@;&lt;br /&gt;
                   }&lt;br /&gt;
                   else&lt;br /&gt;
                   {&lt;br /&gt;
                      #if we haven't reached the starting index, just use the default&lt;br /&gt;
                      $result = $default;&lt;br /&gt;
                   }&lt;br /&gt;
                   #Print out the value&lt;br /&gt;
                   print &amp;quot;\t${result}&amp;quot;;&lt;br /&gt;
                   #If there are more to be processed, add a comma after the value&lt;br /&gt;
                   if($VAL != $end_idx)&lt;br /&gt;
                   {&lt;br /&gt;
                      print &amp;quot;,&amp;quot;;&lt;br /&gt;
                   }&lt;br /&gt;
                   print &amp;quot;\n&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
                #Finish the declaration&lt;br /&gt;
                print &amp;quot;};\n&amp;quot;;&lt;br /&gt;
             }&lt;br /&gt;
             else&lt;br /&gt;
             {&lt;br /&gt;
                #If this is not a macro invocation, just copy the line directly to the output&lt;br /&gt;
                print $line;&lt;br /&gt;
             }&lt;br /&gt;
           }&lt;br /&gt;
&lt;br /&gt;
To run this program, do this:&lt;br /&gt;
&lt;br /&gt;
Running the code generator&lt;br /&gt;
&lt;br /&gt;
           ./tablegen.pl &amp;lt; sqrt.in &amp;gt; sqrt.c&lt;br /&gt;
           gcc sqrt.c -o sqrt&lt;br /&gt;
           ./a.out&lt;br /&gt;
&lt;br /&gt;
===Mini-language for boiler-plate===&lt;br /&gt;
If you have a large application where many of the functions include a lot of boilerplate code, it is often a good idea to create a mini-language that allows you to work with your boilerplate code in an easier fashion.&amp;lt;ref name=&amp;quot;boiler&amp;quot;&amp;gt;K. Czarnecki, &amp;quot;Generative Programming&amp;quot; chapter 8, Static Metaprogramming in C++&amp;lt;/ref&amp;gt; This mini-language will then be converted into your regular source code language before compiling.&amp;lt;ref name=&amp;quot;use2&amp;quot;&amp;gt;Jonathan Bartlett, &amp;quot;The art of metaprogramming, Part 2: Metaprogramming using Scheme&amp;quot;, 02 May 2006  http://www.ibm.com/developerworks/linux/library/l-metaprog2/index.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following is an example:&lt;br /&gt;
&lt;br /&gt;
Let's say that we are building a CGI application consisting of many independent CGI scripts. In most CGI applications, much of the state is stored in a database, but only a session ID is passed to each script via a cookie.&lt;br /&gt;
&lt;br /&gt;
However, in nearly every page we need to know the other standard information (such as the username, group number, the current job being worked on, whatever else information is pertinent). In addition, we need to redirect the user if they do not have an appropriate cookie.&lt;br /&gt;
&lt;br /&gt;
            (define (handle-cgi-request req)&lt;br /&gt;
              (let (&lt;br /&gt;
                     (session-id (webserver:cookie req &amp;quot;sessionid&amp;quot;)))&lt;br /&gt;
                (if (not (webserver:valid-session-id session-id))&lt;br /&gt;
                    (webserver:redirect-to-login-page)&lt;br /&gt;
                    (let (&lt;br /&gt;
                          (username (webserver:username-for-session session-id))&lt;br /&gt;
                          (group (webserver:group-for-user username))&lt;br /&gt;
                          (current-job (webserver:current-job-for-user username)))&lt;br /&gt;
                       ;;Code for processing goes here&lt;br /&gt;
                       ))))&lt;br /&gt;
&lt;br /&gt;
While some of that can be handled by a procedure, the bindings certainly cannot. However, we can turn most of it into a macro. The macro can be implemented like this:&lt;br /&gt;
&lt;br /&gt;
            (define-syntax cgi-boilerplate&lt;br /&gt;
              (lambda (x)&lt;br /&gt;
                (syntax-case x ()&lt;br /&gt;
                 (&lt;br /&gt;
                   (cgi-boilerplate expr)&lt;br /&gt;
                   (datum-&amp;gt;syntax-object&lt;br /&gt;
                     (syntax k)&lt;br /&gt;
                      (let (&lt;br /&gt;
                            (session-id (webserver:cookie req &amp;quot;sessionid&amp;quot;)))&lt;br /&gt;
                           (if (not (webserver:valid-session-id session-id))&lt;br /&gt;
                               (webserver:redirect-to-login-page)&lt;br /&gt;
                               (let (&lt;br /&gt;
                                      (username (webserver:username-for-session session-id))&lt;br /&gt;
                                      (group (webserver:group-for-user username))&lt;br /&gt;
                                      (current-job (webserver:current-job-for-user username)))&lt;br /&gt;
                                      (syntax-object-&amp;gt;datum (syntax expr))))))&lt;br /&gt;
                  )&lt;br /&gt;
             )))&lt;br /&gt;
&lt;br /&gt;
We can now create new forms based on our boilerplate code by doing the following:&lt;br /&gt;
&lt;br /&gt;
            (define (handle-cgi-request req)&lt;br /&gt;
              (cgi-boilerplate&lt;br /&gt;
               (begin&lt;br /&gt;
                 ;;Do whatever I want here&lt;br /&gt;
                 )))&lt;br /&gt;
&lt;br /&gt;
In addition, since we are not defining our variables explicitly, adding new variable definitions to our boilerplate won't affect its calling conventions, so new features can be added without having to create a whole new function.&lt;br /&gt;
&lt;br /&gt;
In any large project, there are inevitably templates to follow which cannot be reduced to functions, usually because of the bindings being created. Using boilerplate macros can make maintenance of such templated code much easier.&lt;br /&gt;
&lt;br /&gt;
Likewise, other standard macros can be created which make use of variables defined in the boilerplate. Using macros like this significantly reduces typing because you do not have to constantly be writing and rewriting variable bindings, derivations, and parameter passing. This also reduces the potential for errors in such code.&lt;br /&gt;
&lt;br /&gt;
Realize though that boilerplate macros are not a panacea. There are many significant problems that can occur, including:&lt;br /&gt;
*  Accidentally overwriting bindings by introducing a variable name that was previously defined in a macro.&lt;br /&gt;
*  Difficulty tracing problems because the inputs and the outputs of the macros are implicit, not explicit.&lt;br /&gt;
&lt;br /&gt;
These can be largely avoided by doing a few things in conjunction with your boilerplate macros:&lt;br /&gt;
*  Have a naming convention which clearly labels macros as such, as well as indicate that a variable came from boilerplate code. This could be done by affixing -m to macros and -b to variables defined within a boilerplate.&lt;br /&gt;
*  Carefully document all boilerplate macros, especially the introduced variable bindings and all changes between versions.&lt;br /&gt;
*  Only use boilerplate macros when the savings in repetitiveness clearly outweigh the negatives of implicit functionality.&lt;br /&gt;
&lt;br /&gt;
===Abbreviate statements and prevent mistakes===&lt;br /&gt;
A lot of programming languages make you write really verbose statements to do really simple things. Code-generating programs allow you to abbreviate such statements and save a lot of typing, which also prevents a lot of mistakes because there is less chance of mistyping.&lt;br /&gt;
&lt;br /&gt;
=Metaprogramming Framework in Java=&lt;br /&gt;
==Reflection==&lt;br /&gt;
===Introduction to reflection===&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_(computer_programming) Reflection] is the ability to introspect metalevel information about the program structure itself at runtime. Usually this metalevel information is modeled using the general abstraction mechanisms available in the language. In Java, reflection enables to discover information about the loaded classes:&lt;br /&gt;
*  Fields,&lt;br /&gt;
*  Methods, and&lt;br /&gt;
*  Constructors&lt;br /&gt;
*  Generics information&lt;br /&gt;
*  Metadata annotations&lt;br /&gt;
It also enables to use these metaobjects to their instances in runtime environment.&amp;lt;ref name=&amp;quot;meta_java&amp;quot;&amp;gt;Abdelmonaim Remani, &amp;quot;The Art of Metaprogramming in Java&amp;quot;, Jul 19, 2012  http://www.slideshare.net/PolymathicCoder/the-art-of-metaprogramming-in-java&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following is the Java metalevel architecture&amp;lt;ref name=&amp;quot;reflection&amp;quot;&amp;gt;Mika Haapakorpi, &amp;quot;Meta Programming In Java&amp;quot;&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Metalevel architecture.PNG]]&lt;br /&gt;
&lt;br /&gt;
===Dynamic proxy classes===&lt;br /&gt;
A dynamic proxy class implements a list of interfaces specified at runtime when the class is created.&lt;br /&gt;
*  A proxy interface is an interface that is implemented by a proxy class.&lt;br /&gt;
*  A proxy instance is an instance of a proxy class which has an associated invocation handler object.&lt;br /&gt;
The following is an example of dynamic proxy classes&lt;br /&gt;
&lt;br /&gt;
[[File:Dynamic_proxy_class.PNG]]&lt;br /&gt;
&lt;br /&gt;
==Generics==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Generics_in_Java Generics] are a facility of generic programming that was added to the Java programming language in 2004 as part of J2SE 5.0. They allow &amp;quot;a type or method to operate on objects of various types while providing compile-time type safety.&amp;quot;  The generics can be used in classes, interfaces, methods and constructors.&amp;lt;ref name=&amp;quot;reflection&amp;quot;&amp;gt;Mika Haapakorpi, &amp;quot;Meta Programming In Java&amp;quot;&amp;lt;/ref&amp;gt;&lt;br /&gt;
===Two new types in generics===&lt;br /&gt;
Two new types in generics:&lt;br /&gt;
*  Parametrized types&lt;br /&gt;
*  Type variables&lt;br /&gt;
&lt;br /&gt;
A type variable is an unqualified identifier. Class and interface declarations can have type arguments (type variables). Method and constructors definitions can have type arguments (type variables). &lt;br /&gt;
&lt;br /&gt;
The following is an example for type variables&lt;br /&gt;
&lt;br /&gt;
       List&amp;lt;String&amp;gt; anExample = new ArrayList&amp;lt;String&amp;gt;()&lt;br /&gt;
&lt;br /&gt;
*  List : interface&lt;br /&gt;
*  ArrayList : class&lt;br /&gt;
*  String : class (the actual type argument)&lt;br /&gt;
*  List&amp;lt;String&amp;gt; and ArrayList&amp;lt;String&amp;gt; : parametrized type&lt;br /&gt;
&lt;br /&gt;
===Class/Interface Declarations===&lt;br /&gt;
The following is an example of class declaration:&lt;br /&gt;
&lt;br /&gt;
       public interface List&amp;lt;E&amp;gt; {&lt;br /&gt;
         void add(Ex);&lt;br /&gt;
         Iterator&amp;lt;E&amp;gt; iterator();&lt;br /&gt;
       }&lt;br /&gt;
       public interface Iterator&amp;lt;E&amp;gt; {&lt;br /&gt;
         E next();&lt;br /&gt;
         booleanhasNext();&lt;br /&gt;
       }&lt;br /&gt;
       List&amp;lt;String&amp;gt; anExample;&lt;br /&gt;
       anExample.add(”sdfdfss”);&lt;br /&gt;
       anExample.add(new Object()); // compile time error&lt;br /&gt;
       String aTest = anExample.iterator().next();&lt;br /&gt;
&lt;br /&gt;
==Metadata annotation==&lt;br /&gt;
An annotation, in the Java computer programming language, is a form of syntactic metadata that can be added to Java source code. Classes, methods, variables, parameters and packages may be annotated.&amp;lt;ref name=&amp;quot;reflection&amp;quot;&amp;gt;Mika Haapakorpi, &amp;quot;Meta Programming In Java&amp;quot;&amp;lt;/ref&amp;gt; &lt;br /&gt;
&lt;br /&gt;
The following is an example of annotation:&lt;br /&gt;
&lt;br /&gt;
        /**&lt;br /&gt;
        * Designates a formatter to pretty-print the annotated class.&lt;br /&gt;
        */&lt;br /&gt;
        public @interface PrettyPrinter{&lt;br /&gt;
        Class&amp;lt;? extends Formatter&amp;gt; value();&lt;br /&gt;
        }&lt;br /&gt;
        // Single-member annotation with Class&lt;br /&gt;
        // member restricted by bounded wildcard&lt;br /&gt;
        // The annotation presumes the existence of this class.&lt;br /&gt;
        class GorgeousFormatterimplements Formatter { ... }&lt;br /&gt;
        @PrettyPrinter(GorgeousFormatter.class)&lt;br /&gt;
        public class Petunia { ... }&lt;br /&gt;
&lt;br /&gt;
‘@’ indicates the start of an annotation definition.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Cyang14</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1_1h_jc&amp;diff=73627</id>
		<title>CSC/ECE 517 Spring 2013/ch1 1h jc</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1_1h_jc&amp;diff=73627"/>
		<updated>2013-02-24T16:54:12Z</updated>

		<summary type="html">&lt;p&gt;Cyang14: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Metaprogramming in statically typed languages&lt;br /&gt;
=Metaprogramming in statically typed languages=&lt;br /&gt;
==Introduction==&lt;br /&gt;
===What is metaprogramming===&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming] refers to writing programs that can manipulate other programs or themselves. The program that is doing the manipulating is called the ''metaprogram'', and the program that is being manipulated is called the ''object program''. The language of the ''metaprogram'' is referred to as the ''metalanguage'' and the language of the object program is called the ''object language''. A language has the ability of [http://en.wikipedia.org/wiki/Reflection_(computer_programming) reflection] if it can be used to write a metaprogram that can manipulate a program written in the same language.&lt;br /&gt;
&lt;br /&gt;
===Metaprogramming in statically typed languages===&lt;br /&gt;
&lt;br /&gt;
Metaprogramming can be accomplished in both statically and dynamically typed languages, however [http://en.wikipedia.org/wiki/Dynamically_typed_language#Dynamic_typing dynamically typed languages] have some advantages over [http://en.wikipedia.org/wiki/Dynamically_typed_language#Dynamic_typing statically typed languages] when it comes to metaprogramming.&lt;br /&gt;
&lt;br /&gt;
One example of a metaprogramming task is to add a method to a particular class at runtime. Sample code in Ruby to perform this task is outlined below. First is a definition of a Ruby class called TestAddMethodAtRuntime:&lt;br /&gt;
&lt;br /&gt;
  class TestAddMethodAtRuntime&lt;br /&gt;
    def originalMethod&lt;br /&gt;
        puts &amp;quot;Original Method&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The code below created a new instance of the class. After that, it defines a new method called newlyAddedMethod. Then the original method is called, follow by the newly added method at runtime:&lt;br /&gt;
&lt;br /&gt;
  t = TestAddMethodAtRuntime.new&lt;br /&gt;
  def t.newlyAddedMethod&lt;br /&gt;
    puts &amp;quot;Newly Added Method&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  t.originalMethod&lt;br /&gt;
  puts &amp;quot;\n&amp;quot;&lt;br /&gt;
  t.newlyAddedMethod&lt;br /&gt;
&lt;br /&gt;
Below is the output of the code above:&lt;br /&gt;
&lt;br /&gt;
  Original Method&lt;br /&gt;
  Newly Added Method&lt;br /&gt;
&lt;br /&gt;
Accomplishing something like this in a statically typed language would be much more difficult or impossible depending on the language. It is for this reason that dynamically typed languages are usually viewed as being better equipped to handle metaprogramming overall.&lt;br /&gt;
&lt;br /&gt;
==Implementation==&lt;br /&gt;
===Programming Language API===&lt;br /&gt;
&lt;br /&gt;
One way that metaprogramming can be possible in a language is through an [http://en.wikipedia.org/wiki/API API] that helps programmers achieve a metaprogramming task. An example of this is a language that provides an API for reflection. One type of reflection is ''introspection'' in which the program can access the source code of the program itself.&lt;br /&gt;
&lt;br /&gt;
Below is an example from the Java API to illustrate how an API can be used to achieve some metaprogramming tasks:&lt;br /&gt;
&lt;br /&gt;
   import java.lang.reflect.*; &lt;br /&gt;
   public class DumpMethods {&lt;br /&gt;
   public static void main(String args[]) {&lt;br /&gt;
     	try {&lt;br /&gt;
           Class c = Class.forName(args[0]);&lt;br /&gt;
           Method m[] = c.getDeclaredMethods();&lt;br /&gt;
           for (int i = 0; i &amp;lt; m.length; i++) {&lt;br /&gt;
              System.out.println(m[i].toString());&lt;br /&gt;
           }&lt;br /&gt;
     	}&lt;br /&gt;
     	catch (Throwable e) {&lt;br /&gt;
        	System.err.println(e);&lt;br /&gt;
     	}&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
The above example prints out all of the methods that are declared for a particular class. This is an example of metaprogramming since the program is accessing the source code of itself.&lt;br /&gt;
&lt;br /&gt;
===Program transformation system===&lt;br /&gt;
&lt;br /&gt;
A program transformation system is something that takes a program as the input, and outputs a different program. One example of a program transformation system is the Java compiler. The Java compiler takes a Java program as its input, and outputs a .class file with platform independent bytecode in it. Another example of a program transformation system is a Java decompiler, which takes bytecode as an input and outputs a Java program.&lt;br /&gt;
&lt;br /&gt;
In the case of the Java compiler, the program input is being output into another program in a different &amp;quot;language&amp;quot;. There are other types of transformation systems that take a program as an input and output a different program in the same language. One example of this would be ''program migration'' which migrates source code to a newer or older version of the same language. This can be useful if upgrading a software system to run on a newer version of a framework.&lt;br /&gt;
&lt;br /&gt;
==Common Uses==&lt;br /&gt;
===Pre-generate static data at compile time===&lt;br /&gt;
One common use of metaprogramming in statically typed languages is to write programs that will pre-generate tables of data for use at runtime.&amp;lt;ref name=&amp;quot;use1&amp;quot;&amp;gt;Jonathan Bartlett, &amp;quot;The art of metaprogramming, Part 1: Introduction to metaprogramming&amp;quot;, 20 Oct 2005   http://www.ibm.com/developerworks/library/l-metaprog1/index.html#N10052&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One simple but useful code generator is to build static lookup tables. Often, in order to build fast functions in C programming, we simply create a lookup table of all of the answers. This means that we either need to pre-compute them by hand (which is wasteful of your time) or build them at runtime (which is wasteful of the user's time).&lt;br /&gt;
&lt;br /&gt;
In following example we will build a generator that will take a function or set of functions on an integer and build lookup tables for the answer.&lt;br /&gt;
&lt;br /&gt;
To think of how to make such a program, we can start from the end and work backward. Firstly we need a lookup table that will return square roots of numbers between 5 and 20. A simple program can be written to generate such a table like this:&lt;br /&gt;
&lt;br /&gt;
Generate and use a lookup table of square roots&lt;br /&gt;
&lt;br /&gt;
        /* our lookup table */&lt;br /&gt;
        double square_roots[21];&lt;br /&gt;
        /* function to load the table at runtime */&lt;br /&gt;
        void init_square_roots()&lt;br /&gt;
        {&lt;br /&gt;
           int i;&lt;br /&gt;
           for(i = 5; i &amp;lt; 21; i++)&lt;br /&gt;
             {&lt;br /&gt;
               square_roots[i] = sqrt((double)i);&lt;br /&gt;
             }&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
With that single macro, we can take away a lot of work for any program that has to generate mathematical tables indexed by integer. A little extra work would also allow tables containing full struct definitions; a little more would ensure that space isn't wasted at the front of the array with useless empty entries.&lt;br /&gt;
        /* program that uses the table */&lt;br /&gt;
        int main ()&lt;br /&gt;
        {&lt;br /&gt;
           init_square_roots();&lt;br /&gt;
           printf(&amp;quot;The square root of 5 is %f\n&amp;quot;, square_roots[5]);&lt;br /&gt;
           return 0;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
Now, to convert this to a statically initialized array, you would remove the first part of the program and replace it with something like this, calculated by hand:&lt;br /&gt;
&lt;br /&gt;
Square root program with a static lookup table&lt;br /&gt;
&lt;br /&gt;
        double square_roots[] = {&lt;br /&gt;
           /* these are the ones we skipped */ 0.0, 0.0, 0.0, 0.0, 0.0&lt;br /&gt;
           2.236068, /* Square root of 5 */&lt;br /&gt;
           2.449490, /* Square root of 6 */&lt;br /&gt;
           2.645751, /* Square root of 7 */&lt;br /&gt;
           2.828427, /* Square root of 8 */&lt;br /&gt;
           3.0, /* Square root of 9 */&lt;br /&gt;
           ...&lt;br /&gt;
           4.472136 /* Square root of 20 */&lt;br /&gt;
           };&lt;br /&gt;
&lt;br /&gt;
What is needed is a program that will produce these values and print them out in a table like the previous one so they are loaded in at compile-time.&lt;br /&gt;
&lt;br /&gt;
Code generator for the table macro&lt;br /&gt;
&lt;br /&gt;
           #!/usr/bin/perl  &lt;br /&gt;
           #&lt;br /&gt;
           #tablegen.pl &lt;br /&gt;
           #&lt;br /&gt;
           ##Puts each program line into $line&lt;br /&gt;
           while(my $line = &amp;lt;&amp;gt;)&lt;br /&gt;
           {&lt;br /&gt;
             #Is this a macro invocation?&lt;br /&gt;
             if($line =~ m/TABLE:/)&lt;br /&gt;
             {&lt;br /&gt;
                #If so, split it apart into its component pieces&lt;br /&gt;
                my ($dummy, $table_name, $type, $start_idx, $end_idx, $default, $procedure) = split(m/:/, $line, 7);&lt;br /&gt;
                #The main difference between C and Perl for mathematical expressions is that&lt;br /&gt;
                #Perl prefixes its variables with a dollar sign, so we will add that here&lt;br /&gt;
                $procedure =~ s/VAL/\$VAL/g;&lt;br /&gt;
                #Print out the array declaration&lt;br /&gt;
                print &amp;quot;${type} ${table_name} [] = {\n&amp;quot;;&lt;br /&gt;
                #Go through each array element&lt;br /&gt;
                foreach my $VAL (0 .. $end_idx)&lt;br /&gt;
                {&lt;br /&gt;
                   #Only process an answer if we have reached our starting index&lt;br /&gt;
                   if($VAL &amp;gt;= $start_idx)&lt;br /&gt;
                   {&lt;br /&gt;
                      #evaluate the procedure specified (this sets $@ if there are any errors)&lt;br /&gt;
                      $result = eval $procedure;&lt;br /&gt;
                      die(&amp;quot;Error processing: $@&amp;quot;) if $@;&lt;br /&gt;
                   }&lt;br /&gt;
                   else&lt;br /&gt;
                   {&lt;br /&gt;
                      #if we haven't reached the starting index, just use the default&lt;br /&gt;
                      $result = $default;&lt;br /&gt;
                   }&lt;br /&gt;
                   #Print out the value&lt;br /&gt;
                   print &amp;quot;\t${result}&amp;quot;;&lt;br /&gt;
                   #If there are more to be processed, add a comma after the value&lt;br /&gt;
                   if($VAL != $end_idx)&lt;br /&gt;
                   {&lt;br /&gt;
                      print &amp;quot;,&amp;quot;;&lt;br /&gt;
                   }&lt;br /&gt;
                   print &amp;quot;\n&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
                #Finish the declaration&lt;br /&gt;
                print &amp;quot;};\n&amp;quot;;&lt;br /&gt;
             }&lt;br /&gt;
             else&lt;br /&gt;
             {&lt;br /&gt;
                #If this is not a macro invocation, just copy the line directly to the output&lt;br /&gt;
                print $line;&lt;br /&gt;
             }&lt;br /&gt;
           }&lt;br /&gt;
&lt;br /&gt;
To run this program, do this:&lt;br /&gt;
&lt;br /&gt;
Running the code generator&lt;br /&gt;
&lt;br /&gt;
           ./tablegen.pl &amp;lt; sqrt.in &amp;gt; sqrt.c&lt;br /&gt;
           gcc sqrt.c -o sqrt&lt;br /&gt;
           ./a.out&lt;br /&gt;
&lt;br /&gt;
===Mini-language for boiler-plate===&lt;br /&gt;
If you have a large application where many of the functions include a lot of boilerplate code, it is often a good idea to create a mini-language that allows you to work with your boilerplate code in an easier fashion. This mini-language will then be converted into your regular source code language before compiling.&amp;lt;ref name=&amp;quot;use2&amp;quot;&amp;gt;Jonathan Bartlett, &amp;quot;The art of metaprogramming, Part 2: Metaprogramming using Scheme&amp;quot;, 02 May 2006  http://www.ibm.com/developerworks/linux/library/l-metaprog2/index.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following is an example:&lt;br /&gt;
&lt;br /&gt;
Let's say that we are building a CGI application consisting of many independent CGI scripts. In most CGI applications, much of the state is stored in a database, but only a session ID is passed to each script via a cookie.&lt;br /&gt;
&lt;br /&gt;
However, in nearly every page we need to know the other standard information (such as the username, group number, the current job being worked on, whatever else information is pertinent). In addition, we need to redirect the user if they do not have an appropriate cookie.&lt;br /&gt;
&lt;br /&gt;
            (define (handle-cgi-request req)&lt;br /&gt;
              (let (&lt;br /&gt;
                     (session-id (webserver:cookie req &amp;quot;sessionid&amp;quot;)))&lt;br /&gt;
                (if (not (webserver:valid-session-id session-id))&lt;br /&gt;
                    (webserver:redirect-to-login-page)&lt;br /&gt;
                    (let (&lt;br /&gt;
                          (username (webserver:username-for-session session-id))&lt;br /&gt;
                          (group (webserver:group-for-user username))&lt;br /&gt;
                          (current-job (webserver:current-job-for-user username)))&lt;br /&gt;
                       ;;Code for processing goes here&lt;br /&gt;
                       ))))&lt;br /&gt;
&lt;br /&gt;
While some of that can be handled by a procedure, the bindings certainly cannot. However, we can turn most of it into a macro. The macro can be implemented like this:&lt;br /&gt;
&lt;br /&gt;
            (define-syntax cgi-boilerplate&lt;br /&gt;
              (lambda (x)&lt;br /&gt;
                (syntax-case x ()&lt;br /&gt;
                 (&lt;br /&gt;
                   (cgi-boilerplate expr)&lt;br /&gt;
                   (datum-&amp;gt;syntax-object&lt;br /&gt;
                     (syntax k)&lt;br /&gt;
                      (let (&lt;br /&gt;
                            (session-id (webserver:cookie req &amp;quot;sessionid&amp;quot;)))&lt;br /&gt;
                           (if (not (webserver:valid-session-id session-id))&lt;br /&gt;
                               (webserver:redirect-to-login-page)&lt;br /&gt;
                               (let (&lt;br /&gt;
                                      (username (webserver:username-for-session session-id))&lt;br /&gt;
                                      (group (webserver:group-for-user username))&lt;br /&gt;
                                      (current-job (webserver:current-job-for-user username)))&lt;br /&gt;
                                      (syntax-object-&amp;gt;datum (syntax expr))))))&lt;br /&gt;
                  )&lt;br /&gt;
             )))&lt;br /&gt;
&lt;br /&gt;
We can now create new forms based on our boilerplate code by doing the following:&lt;br /&gt;
&lt;br /&gt;
            (define (handle-cgi-request req)&lt;br /&gt;
              (cgi-boilerplate&lt;br /&gt;
               (begin&lt;br /&gt;
                 ;;Do whatever I want here&lt;br /&gt;
                 )))&lt;br /&gt;
&lt;br /&gt;
In addition, since we are not defining our variables explicitly, adding new variable definitions to our boilerplate won't affect its calling conventions, so new features can be added without having to create a whole new function.&lt;br /&gt;
&lt;br /&gt;
In any large project, there are inevitably templates to follow which cannot be reduced to functions, usually because of the bindings being created. Using boilerplate macros can make maintenance of such templated code much easier.&lt;br /&gt;
&lt;br /&gt;
Likewise, other standard macros can be created which make use of variables defined in the boilerplate. Using macros like this significantly reduces typing because you do not have to constantly be writing and rewriting variable bindings, derivations, and parameter passing. This also reduces the potential for errors in such code.&lt;br /&gt;
&lt;br /&gt;
Realize though that boilerplate macros are not a panacea. There are many significant problems that can occur, including:&lt;br /&gt;
*  Accidentally overwriting bindings by introducing a variable name that was previously defined in a macro.&lt;br /&gt;
*  Difficulty tracing problems because the inputs and the outputs of the macros are implicit, not explicit.&lt;br /&gt;
&lt;br /&gt;
These can be largely avoided by doing a few things in conjunction with your boilerplate macros:&lt;br /&gt;
*  Have a naming convention which clearly labels macros as such, as well as indicate that a variable came from boilerplate code. This could be done by affixing -m to macros and -b to variables defined within a boilerplate.&lt;br /&gt;
*  Carefully document all boilerplate macros, especially the introduced variable bindings and all changes between versions.&lt;br /&gt;
*  Only use boilerplate macros when the savings in repetitiveness clearly outweigh the negatives of implicit functionality.&lt;br /&gt;
&lt;br /&gt;
===Abbreviate statements and prevent mistakes===&lt;br /&gt;
A lot of programming languages make you write really verbose statements to do really simple things. Code-generating programs allow you to abbreviate such statements and save a lot of typing, which also prevents a lot of mistakes because there is less chance of mistyping.&lt;br /&gt;
&lt;br /&gt;
=Metaprogramming Framework in Java=&lt;br /&gt;
==Reflection==&lt;br /&gt;
===Introduction to reflection===&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_(computer_programming) Reflection] is the ability to introspect metalevel information about the program structure itself at runtime. Usually this metalevel information is modeled using the general abstraction mechanisms available in the language. In Java, reflection enables to discover information about the loaded classes:&lt;br /&gt;
*  Fields,&lt;br /&gt;
*  Methods, and&lt;br /&gt;
*  Constructors&lt;br /&gt;
*  Generics information&lt;br /&gt;
*  Metadata annotations&lt;br /&gt;
It also enables to use these metaobjects to their instances in runtime environment.&amp;lt;ref name=&amp;quot;meta_java&amp;quot;&amp;gt;Abdelmonaim Remani, &amp;quot;The Art of Metaprogramming in Java&amp;quot;, Jul 19, 2012  http://www.slideshare.net/PolymathicCoder/the-art-of-metaprogramming-in-java&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following is the Java metalevel architecture&amp;lt;ref name=&amp;quot;reflection&amp;quot;&amp;gt;Mika Haapakorpi, &amp;quot;Meta Programming In Java&amp;quot;&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Metalevel architecture.PNG]]&lt;br /&gt;
&lt;br /&gt;
===Dynamic proxy classes===&lt;br /&gt;
A dynamic proxy class implements a list of interfaces specified at runtime when the class is created.&lt;br /&gt;
*  A proxy interface is an interface that is implemented by a proxy class.&lt;br /&gt;
*  A proxy instance is an instance of a proxy class which has an associated invocation handler object.&lt;br /&gt;
The following is an example of dynamic proxy classes&lt;br /&gt;
&lt;br /&gt;
[[File:Dynamic_proxy_class.PNG]]&lt;br /&gt;
&lt;br /&gt;
==Generics==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Generics_in_Java Generics] are a facility of generic programming that was added to the Java programming language in 2004 as part of J2SE 5.0. They allow &amp;quot;a type or method to operate on objects of various types while providing compile-time type safety.&amp;quot;  The generics can be used in classes, interfaces, methods and constructors.&amp;lt;ref name=&amp;quot;reflection&amp;quot;&amp;gt;Mika Haapakorpi, &amp;quot;Meta Programming In Java&amp;quot;&amp;lt;/ref&amp;gt;&lt;br /&gt;
===Two new types in generics===&lt;br /&gt;
Two new types in generics:&lt;br /&gt;
*  Parametrized types&lt;br /&gt;
*  Type variables&lt;br /&gt;
&lt;br /&gt;
A type variable is an unqualified identifier. Class and interface declarations can have type arguments (type variables). Method and constructors definitions can have type arguments (type variables). &lt;br /&gt;
&lt;br /&gt;
The following is an example for type variables&lt;br /&gt;
&lt;br /&gt;
       List&amp;lt;String&amp;gt; anExample = new ArrayList&amp;lt;String&amp;gt;()&lt;br /&gt;
&lt;br /&gt;
*  List : interface&lt;br /&gt;
*  ArrayList : class&lt;br /&gt;
*  String : class (the actual type argument)&lt;br /&gt;
*  List&amp;lt;String&amp;gt; and ArrayList&amp;lt;String&amp;gt; : parametrized type&lt;br /&gt;
&lt;br /&gt;
===Class/Interface Declarations===&lt;br /&gt;
The following is an example of class declaration:&lt;br /&gt;
&lt;br /&gt;
       public interface List&amp;lt;E&amp;gt; {&lt;br /&gt;
         void add(Ex);&lt;br /&gt;
         Iterator&amp;lt;E&amp;gt; iterator();&lt;br /&gt;
       }&lt;br /&gt;
       public interface Iterator&amp;lt;E&amp;gt; {&lt;br /&gt;
         E next();&lt;br /&gt;
         booleanhasNext();&lt;br /&gt;
       }&lt;br /&gt;
       List&amp;lt;String&amp;gt; anExample;&lt;br /&gt;
       anExample.add(”sdfdfss”);&lt;br /&gt;
       anExample.add(new Object()); // compile time error&lt;br /&gt;
       String aTest = anExample.iterator().next();&lt;br /&gt;
&lt;br /&gt;
==Metadata annotation==&lt;br /&gt;
An annotation, in the Java computer programming language, is a form of syntactic metadata that can be added to Java source code. Classes, methods, variables, parameters and packages may be annotated.&amp;lt;ref name=&amp;quot;reflection&amp;quot;&amp;gt;Mika Haapakorpi, &amp;quot;Meta Programming In Java&amp;quot;&amp;lt;/ref&amp;gt; &lt;br /&gt;
&lt;br /&gt;
The following is an example of annotation:&lt;br /&gt;
&lt;br /&gt;
        /**&lt;br /&gt;
        * Designates a formatter to pretty-print the annotated class.&lt;br /&gt;
        */&lt;br /&gt;
        public @interface PrettyPrinter{&lt;br /&gt;
        Class&amp;lt;? extends Formatter&amp;gt; value();&lt;br /&gt;
        }&lt;br /&gt;
        // Single-member annotation with Class&lt;br /&gt;
        // member restricted by bounded wildcard&lt;br /&gt;
        // The annotation presumes the existence of this class.&lt;br /&gt;
        class GorgeousFormatterimplements Formatter { ... }&lt;br /&gt;
        @PrettyPrinter(GorgeousFormatter.class)&lt;br /&gt;
        public class Petunia { ... }&lt;br /&gt;
&lt;br /&gt;
‘@’ indicates the start of an annotation definition.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Cyang14</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1_1h_jc&amp;diff=73626</id>
		<title>CSC/ECE 517 Spring 2013/ch1 1h jc</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1_1h_jc&amp;diff=73626"/>
		<updated>2013-02-24T16:53:54Z</updated>

		<summary type="html">&lt;p&gt;Cyang14: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Metaprogramming in statically typed languages&lt;br /&gt;
=Metaprogramming in statically typed languages=&lt;br /&gt;
==Introduction==&lt;br /&gt;
===What is metaprogramming===&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming] refers to writing programs that can manipulate other programs or themselves. The program that is doing the manipulating is called the ''metaprogram'', and the program that is being manipulated is called the ''object program''. The language of the ''metaprogram'' is referred to as the ''metalanguage'' and the language of the object program is called the ''object language''. A language has the ability of [http://en.wikipedia.org/wiki/Reflection_(computer_programming) reflection] if it can be used to write a metaprogram that can manipulate a program written in the same language.&lt;br /&gt;
&lt;br /&gt;
===Metaprogramming in statically typed languages===&lt;br /&gt;
&lt;br /&gt;
Metaprogramming can be accomplished in both statically and dynamically typed languages, however [http://en.wikipedia.org/wiki/Dynamically_typed_language#Dynamic_typing dynamically typed languages] have some advantages over [http://en.wikipedia.org/wiki/Dynamically_typed_language#Dynamic_typing statically typed languages] when it comes to metaprogramming.&lt;br /&gt;
&lt;br /&gt;
One example of a metaprogramming task is to add a method to a particular class at runtime. Sample code in Ruby to perform this task is outlined below. First is a definition of a Ruby class called TestAddMethodAtRuntime:&lt;br /&gt;
&lt;br /&gt;
  class TestAddMethodAtRuntime&lt;br /&gt;
    def originalMethod&lt;br /&gt;
        puts &amp;quot;Original Method&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The code below created a new instance of the class. After that, it defines a new method called newlyAddedMethod. Then the original method is called, follow by the newly added method at runtime:&lt;br /&gt;
&lt;br /&gt;
  t = TestAddMethodAtRuntime.new&lt;br /&gt;
  def t.newlyAddedMethod&lt;br /&gt;
    puts &amp;quot;Newly Added Method&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  t.originalMethod&lt;br /&gt;
  puts &amp;quot;\n&amp;quot;&lt;br /&gt;
  t.newlyAddedMethod&lt;br /&gt;
&lt;br /&gt;
Below is the output of the code above:&lt;br /&gt;
&lt;br /&gt;
  Original Method&lt;br /&gt;
  Newly Added Method&lt;br /&gt;
&lt;br /&gt;
Accomplishing something like this in a statically typed language would be much more difficult or impossible depending on the language. It is for this reason that dynamically typed languages are usually viewed as being better equipped to handle metaprogramming overall.&lt;br /&gt;
&lt;br /&gt;
==Implementation==&lt;br /&gt;
===Programming Language API===&lt;br /&gt;
&lt;br /&gt;
One way that metaprogramming can be possible in a language is through an [http://en.wikipedia.org/wiki/API API] that helps programmers achieve a metaprogramming task. An example of this is a language that provides an API for reflection. One type of reflection is ''introspection'' in which the program can access the source code of the program itself.&lt;br /&gt;
&lt;br /&gt;
Below is an example from the Java API to illustrate how an API can be used to achieve some metaprogramming tasks:&lt;br /&gt;
&lt;br /&gt;
   import java.lang.reflect.*; &lt;br /&gt;
   public class DumpMethods {&lt;br /&gt;
   public static void main(String args[]) {&lt;br /&gt;
     	try {&lt;br /&gt;
           Class c = Class.forName(args[0]);&lt;br /&gt;
           Method m[] = c.getDeclaredMethods();&lt;br /&gt;
           for (int i = 0; i &amp;lt; m.length; i++) {&lt;br /&gt;
              System.out.println(m[i].toString());&lt;br /&gt;
           }&lt;br /&gt;
     	}&lt;br /&gt;
     	catch (Throwable e) {&lt;br /&gt;
        	System.err.println(e);&lt;br /&gt;
     	}&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
The above example prints out all of the methods that are declared for a particular class. This is an example of metaprogramming since the program is accessing the source code of itself.&lt;br /&gt;
&lt;br /&gt;
===Program transformation system===&lt;br /&gt;
&lt;br /&gt;
A program transformation system is something that takes a program as the input, and outputs a different program. One example of a program transformation system is the Java compiler. The Java compiler takes a Java program as its input, and outputs a .class file with platform independent bytecode in it. Another example of a program transformation system is a Java decompiler, which takes bytecode as an input and outputs a Java program.&lt;br /&gt;
&lt;br /&gt;
In the case of the Java compiler, the program input is being output into another program in a different &amp;quot;language&amp;quot;. There are other types of transformation systems that take a program as an input and output a different program in the same language. One example of this would be ''program migration'' which migrates source code to a newer or older version of the same language. This can be useful if upgrading a software system to run on a newer version of a framework.&lt;br /&gt;
&lt;br /&gt;
==Common Uses==&lt;br /&gt;
===Pre-generate static data at compile time===&lt;br /&gt;
One common use of metaprogramming in statically typed languages is to write programs that will pre-generate tables of data for use at runtime.&amp;lt;ref name=&amp;quot;use1&amp;quot;&amp;gt;Jonathan Bartlett, &amp;quot;The art of metaprogramming, Part 1: Introduction to metaprogramming&amp;quot;, 20 Oct 2005   http://www.ibm.com/developerworks/library/l-metaprog1/index.html#N10052&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One simple but useful code generator is to build static lookup tables. Often, in order to build fast functions in C programming, we simply create a lookup table of all of the answers. This means that we either need to pre-compute them by hand (which is wasteful of your time) or build them at runtime (which is wasteful of the user's time).&lt;br /&gt;
&lt;br /&gt;
In following example we will build a generator that will take a function or set of functions on an integer and build lookup tables for the answer.&lt;br /&gt;
&lt;br /&gt;
To think of how to make such a program, we can start from the end and work backward. Firstly we need a lookup table that will return square roots of numbers between 5 and 20. A simple program can be written to generate such a table like this:&lt;br /&gt;
&lt;br /&gt;
Generate and use a lookup table of square roots&lt;br /&gt;
&lt;br /&gt;
        /* our lookup table */&lt;br /&gt;
        double square_roots[21];&lt;br /&gt;
        /* function to load the table at runtime */&lt;br /&gt;
        void init_square_roots()&lt;br /&gt;
        {&lt;br /&gt;
           int i;&lt;br /&gt;
           for(i = 5; i &amp;lt; 21; i++)&lt;br /&gt;
             {&lt;br /&gt;
               square_roots[i] = sqrt((double)i);&lt;br /&gt;
             }&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
With that single macro, we can take away a lot of work for any program that has to generate mathematical tables indexed by integer. A little extra work would also allow tables containing full struct definitions; a little more would ensure that space isn't wasted at the front of the array with useless empty entries.&lt;br /&gt;
        /* program that uses the table */&lt;br /&gt;
        int main ()&lt;br /&gt;
        {&lt;br /&gt;
           init_square_roots();&lt;br /&gt;
           printf(&amp;quot;The square root of 5 is %f\n&amp;quot;, square_roots[5]);&lt;br /&gt;
           return 0;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
Now, to convert this to a statically initialized array, you would remove the first part of the program and replace it with something like this, calculated by hand:&lt;br /&gt;
&lt;br /&gt;
Square root program with a static lookup table&lt;br /&gt;
&lt;br /&gt;
        double square_roots[] = {&lt;br /&gt;
           /* these are the ones we skipped */ 0.0, 0.0, 0.0, 0.0, 0.0&lt;br /&gt;
           2.236068, /* Square root of 5 */&lt;br /&gt;
           2.449490, /* Square root of 6 */&lt;br /&gt;
           2.645751, /* Square root of 7 */&lt;br /&gt;
           2.828427, /* Square root of 8 */&lt;br /&gt;
           3.0, /* Square root of 9 */&lt;br /&gt;
           ...&lt;br /&gt;
           4.472136 /* Square root of 20 */&lt;br /&gt;
           };&lt;br /&gt;
&lt;br /&gt;
What is needed is a program that will produce these values and print them out in a table like the previous one so they are loaded in at compile-time.&lt;br /&gt;
&lt;br /&gt;
Code generator for the table macro&lt;br /&gt;
&lt;br /&gt;
           #!/usr/bin/perl  &lt;br /&gt;
           #&lt;br /&gt;
           #tablegen.pl &lt;br /&gt;
           #&lt;br /&gt;
           ##Puts each program line into $line&lt;br /&gt;
           while(my $line = &amp;lt;&amp;gt;)&lt;br /&gt;
           {&lt;br /&gt;
             #Is this a macro invocation?&lt;br /&gt;
             if($line =~ m/TABLE:/)&lt;br /&gt;
             {&lt;br /&gt;
                #If so, split it apart into its component pieces&lt;br /&gt;
                my ($dummy, $table_name, $type, $start_idx, $end_idx, $default, $procedure) = split(m/:/, $line, 7);&lt;br /&gt;
                #The main difference between C and Perl for mathematical expressions is that&lt;br /&gt;
                #Perl prefixes its variables with a dollar sign, so we will add that here&lt;br /&gt;
                $procedure =~ s/VAL/\$VAL/g;&lt;br /&gt;
                #Print out the array declaration&lt;br /&gt;
                print &amp;quot;${type} ${table_name} [] = {\n&amp;quot;;&lt;br /&gt;
                #Go through each array element&lt;br /&gt;
                foreach my $VAL (0 .. $end_idx)&lt;br /&gt;
                {&lt;br /&gt;
                   #Only process an answer if we have reached our starting index&lt;br /&gt;
                   if($VAL &amp;gt;= $start_idx)&lt;br /&gt;
                   {&lt;br /&gt;
                      #evaluate the procedure specified (this sets $@ if there are any errors)&lt;br /&gt;
                      $result = eval $procedure;&lt;br /&gt;
                      die(&amp;quot;Error processing: $@&amp;quot;) if $@;&lt;br /&gt;
                   }&lt;br /&gt;
                   else&lt;br /&gt;
                   {&lt;br /&gt;
                      #if we haven't reached the starting index, just use the default&lt;br /&gt;
                      $result = $default;&lt;br /&gt;
                   }&lt;br /&gt;
                   #Print out the value&lt;br /&gt;
                   print &amp;quot;\t${result}&amp;quot;;&lt;br /&gt;
                   #If there are more to be processed, add a comma after the value&lt;br /&gt;
                   if($VAL != $end_idx)&lt;br /&gt;
                   {&lt;br /&gt;
                      print &amp;quot;,&amp;quot;;&lt;br /&gt;
                   }&lt;br /&gt;
                   print &amp;quot;\n&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
                #Finish the declaration&lt;br /&gt;
                print &amp;quot;};\n&amp;quot;;&lt;br /&gt;
             }&lt;br /&gt;
             else&lt;br /&gt;
             {&lt;br /&gt;
                #If this is not a macro invocation, just copy the line directly to the output&lt;br /&gt;
                print $line;&lt;br /&gt;
             }&lt;br /&gt;
           }&lt;br /&gt;
&lt;br /&gt;
To run this program, do this:&lt;br /&gt;
&lt;br /&gt;
Running the code generator&lt;br /&gt;
&lt;br /&gt;
           ./tablegen.pl &amp;lt; sqrt.in &amp;gt; sqrt.c&lt;br /&gt;
           gcc sqrt.c -o sqrt&lt;br /&gt;
           ./a.out&lt;br /&gt;
&lt;br /&gt;
===Mini-language for boiler-plate===&lt;br /&gt;
If you have a large application where many of the functions include a lot of boilerplate code, it is often a good idea to create a mini-language that allows you to work with your boilerplate code in an easier fashion. This mini-language will then be converted into your regular source code language before compiling.&amp;lt;ref name=&amp;quot;use2&amp;quot;&amp;gt;Jonathan Bartlett, &amp;quot;The art of metaprogramming, Part 2: Metaprogramming using Scheme&amp;quot;, 02 May 2006  http://www.ibm.com/developerworks/linux/library/l-metaprog2/index.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following is an example:&lt;br /&gt;
&lt;br /&gt;
Let's say that we are building a CGI application consisting of many independent CGI scripts. In most CGI applications, much of the state is stored in a database, but only a session ID is passed to each script via a cookie.&lt;br /&gt;
&lt;br /&gt;
However, in nearly every page we need to know the other standard information (such as the username, group number, the current job being worked on, whatever else information is pertinent). In addition, we need to redirect the user if they do not have an appropriate cookie.&lt;br /&gt;
&lt;br /&gt;
            (define (handle-cgi-request req)&lt;br /&gt;
              (let (&lt;br /&gt;
                     (session-id (webserver:cookie req &amp;quot;sessionid&amp;quot;)))&lt;br /&gt;
                (if (not (webserver:valid-session-id session-id))&lt;br /&gt;
                    (webserver:redirect-to-login-page)&lt;br /&gt;
                    (let (&lt;br /&gt;
                          (username (webserver:username-for-session session-id))&lt;br /&gt;
                          (group (webserver:group-for-user username))&lt;br /&gt;
                          (current-job (webserver:current-job-for-user username)))&lt;br /&gt;
                       ;;Code for processing goes here&lt;br /&gt;
                       ))))&lt;br /&gt;
&lt;br /&gt;
While some of that can be handled by a procedure, the bindings certainly cannot. However, we can turn most of it into a macro. The macro can be implemented like this:&lt;br /&gt;
&lt;br /&gt;
            (define-syntax cgi-boilerplate&lt;br /&gt;
              (lambda (x)&lt;br /&gt;
                (syntax-case x ()&lt;br /&gt;
                 (&lt;br /&gt;
                   (cgi-boilerplate expr)&lt;br /&gt;
                   (datum-&amp;gt;syntax-object&lt;br /&gt;
                     (syntax k)&lt;br /&gt;
                      (let (&lt;br /&gt;
                            (session-id (webserver:cookie req &amp;quot;sessionid&amp;quot;)))&lt;br /&gt;
                           (if (not (webserver:valid-session-id session-id))&lt;br /&gt;
                               (webserver:redirect-to-login-page)&lt;br /&gt;
                               (let (&lt;br /&gt;
                                      (username (webserver:username-for-session session-id))&lt;br /&gt;
                                      (group (webserver:group-for-user username))&lt;br /&gt;
                                      (current-job (webserver:current-job-for-user username)))&lt;br /&gt;
                                      (syntax-object-&amp;gt;datum (syntax expr))))))&lt;br /&gt;
                  )&lt;br /&gt;
             )))&lt;br /&gt;
&lt;br /&gt;
We can now create new forms based on our boilerplate code by doing the following:&lt;br /&gt;
&lt;br /&gt;
            (define (handle-cgi-request req)&lt;br /&gt;
              (cgi-boilerplate&lt;br /&gt;
               (begin&lt;br /&gt;
                 ;;Do whatever I want here&lt;br /&gt;
                 )))&lt;br /&gt;
&lt;br /&gt;
In addition, since we are not defining our variables explicitly, adding new variable definitions to our boilerplate won't affect its calling conventions, so new features can be added without having to create a whole new function.&lt;br /&gt;
&lt;br /&gt;
In any large project, there are inevitably templates to follow which cannot be reduced to functions, usually because of the bindings being created. Using boilerplate macros can make maintenance of such templated code much easier.&lt;br /&gt;
&lt;br /&gt;
Likewise, other standard macros can be created which make use of variables defined in the boilerplate. Using macros like this significantly reduces typing because you do not have to constantly be writing and rewriting variable bindings, derivations, and parameter passing. This also reduces the potential for errors in such code.&lt;br /&gt;
&lt;br /&gt;
Realize though that boilerplate macros are not a panacea. There are many significant problems that can occur, including:&lt;br /&gt;
*  Accidentally overwriting bindings by introducing a variable name that was previously defined in a macro.&lt;br /&gt;
*  Difficulty tracing problems because the inputs and the outputs of the macros are implicit, not explicit.&lt;br /&gt;
&lt;br /&gt;
These can be largely avoided by doing a few things in conjunction with your boilerplate macros:&lt;br /&gt;
*  Have a naming convention which clearly labels macros as such, as well as indicate that a variable came from boilerplate code. This could be done by affixing -m to macros and -b to variables defined within a boilerplate.&lt;br /&gt;
*  Carefully document all boilerplate macros, especially the introduced variable bindings and all changes between versions.&lt;br /&gt;
*  Only use boilerplate macros when the savings in repetitiveness clearly outweigh the negatives of implicit functionality.&lt;br /&gt;
&lt;br /&gt;
===Abbreviate statements and prevent mistakes===&lt;br /&gt;
A lot of programming languages make you write really verbose statements to do really simple things. Code-generating programs allow you to abbreviate such statements and save a lot of typing, which also prevents a lot of mistakes because there is less chance of mistyping.&lt;br /&gt;
&lt;br /&gt;
=Metaprogramming Framework in Java=&lt;br /&gt;
==Reflection==&lt;br /&gt;
===Introduction to reflection===&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_(computer_programming) Reflection] is the ability to introspect metalevel information about the program structure itself at runtime. Usually this metalevel information is modeled using the general abstraction mechanisms available in the language. In Java, reflection enables to discover information about the loaded classes:&lt;br /&gt;
*  Fields,&lt;br /&gt;
*  Methods, and&lt;br /&gt;
*  Constructors&lt;br /&gt;
*  Generics information&lt;br /&gt;
*  Metadata annotations&lt;br /&gt;
It also enables to use these metaobjects to their instances in runtime environment.&amp;lt;ref name=&amp;quot;meta_java&amp;quot;&amp;gt;Abdelmonaim Remani, &amp;quot;The Art of Metaprogramming in Java&amp;quot;, Jul 19, 2012  http://www.slideshare.net/PolymathicCoder/the-art-of-metaprogramming-in-java&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following is the Java metalevel architecture&amp;lt;ref name=&amp;quot;reflection&amp;quot;&amp;gt;Mika Haapakorpi, &amp;quot;Meta Programming In Java&amp;quot;&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Metalevel architecture.PNG]]&lt;br /&gt;
&lt;br /&gt;
===Dynamic proxy classes===&lt;br /&gt;
A dynamic proxy class implements a list of interfaces specified at runtime when the class is created.&lt;br /&gt;
*  A proxy interface is an interface that is implemented by a proxy class.&lt;br /&gt;
*  A proxy instance is an instance of a proxy class which has an associated invocation handler object.&lt;br /&gt;
The following is an example of dynamic proxy classes&lt;br /&gt;
&lt;br /&gt;
[[File:Dynamic_proxy_class.PNG]]&lt;br /&gt;
&lt;br /&gt;
==Generics==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Generics_in_Java Generics] are a facility of generic programming that was added to the Java programming language in 2004 as part of J2SE 5.0. They allow &amp;quot;a type or method to operate on objects of various types while providing compile-time type safety.&amp;quot;  The generics can be used in classes, interfaces, methods and constructors.&amp;lt;ref name=&amp;quot;reflection&amp;quot;&amp;gt;Mika Haapakorpi, &amp;quot;Meta Programming In Java&amp;quot;&amp;lt;/ref&amp;gt;&lt;br /&gt;
===Two new types in generics===&lt;br /&gt;
Two new types in generics:&lt;br /&gt;
*  Parametrized types&lt;br /&gt;
*  Type variables&lt;br /&gt;
&lt;br /&gt;
A type variable is an unqualified identifier. Class and interface declarations can have type arguments (type variables). Method and constructors definitions can have type arguments (type variables). &lt;br /&gt;
&lt;br /&gt;
The following is an example for type variables&lt;br /&gt;
&lt;br /&gt;
       List&amp;lt;String&amp;gt; anExample = new ArrayList&amp;lt;String&amp;gt;()&lt;br /&gt;
&lt;br /&gt;
*  List : interface&lt;br /&gt;
*  ArrayList : class&lt;br /&gt;
*  String : class (the actual type argument)&lt;br /&gt;
*  List&amp;lt;String&amp;gt; and ArrayList&amp;lt;String&amp;gt; : parametrized type&lt;br /&gt;
&lt;br /&gt;
===Class/Interface Declarations===&lt;br /&gt;
The following is an example of class declaration:&lt;br /&gt;
&lt;br /&gt;
       public interface List&amp;lt;E&amp;gt; {&lt;br /&gt;
         void add(Ex);&lt;br /&gt;
         Iterator&amp;lt;E&amp;gt; iterator();&lt;br /&gt;
       }&lt;br /&gt;
       public interface Iterator&amp;lt;E&amp;gt; {&lt;br /&gt;
         E next();&lt;br /&gt;
         booleanhasNext();&lt;br /&gt;
       }&lt;br /&gt;
       List&amp;lt;String&amp;gt; anExample;&lt;br /&gt;
       anExample.add(”sdfdfss”);&lt;br /&gt;
       anExample.add(new Object()); // compile time error&lt;br /&gt;
       String aTest = anExample.iterator().next();&lt;br /&gt;
&lt;br /&gt;
==Metadata annotation==&lt;br /&gt;
An annotation, in the Java computer programming language, is a form of syntactic metadata that can be added to Java source code. Classes, methods, variables, parameters and packages may be annotated.&amp;lt;ref name=&amp;quot;reflection&amp;quot;&amp;gt;Mika Haapakorpi, &amp;quot;Meta Programming In Java&amp;quot;&amp;lt;/ref&amp;gt; &lt;br /&gt;
&lt;br /&gt;
The following is an example of annotation:&lt;br /&gt;
&lt;br /&gt;
        /**&lt;br /&gt;
        * Designates a formatter to pretty-print the annotated class.&lt;br /&gt;
        */&lt;br /&gt;
        public @interface PrettyPrinter{&lt;br /&gt;
        Class&amp;lt;? extends Formatter&amp;gt; value();&lt;br /&gt;
        }&lt;br /&gt;
        // Single-member annotation with Class&lt;br /&gt;
        // member restricted by bounded wildcard&lt;br /&gt;
        // The annotation presumes the existence of this class.&lt;br /&gt;
        class GorgeousFormatterimplements Formatter { ... }&lt;br /&gt;
        @PrettyPrinter(GorgeousFormatter.class)&lt;br /&gt;
        public class Petunia { ... }&lt;br /&gt;
&lt;br /&gt;
‘@’ indicates the start of an annotation definition.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:K. Czarnecki, &amp;quot;Generative Programming&amp;quot; chapter 8, Static Metaprogramming in C++&lt;br /&gt;
&lt;br /&gt;
:http://www.program-transformation.org&lt;br /&gt;
&lt;br /&gt;
:http://www.codeproject.com/Articles/15429/Ruby-Add-class-methods-at-runtime&lt;br /&gt;
&lt;br /&gt;
:http://docs.oracle.com/javase/tutorial/reflect/index.html&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Cyang14</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1_1h_jc&amp;diff=73625</id>
		<title>CSC/ECE 517 Spring 2013/ch1 1h jc</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1_1h_jc&amp;diff=73625"/>
		<updated>2013-02-24T16:53:34Z</updated>

		<summary type="html">&lt;p&gt;Cyang14: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Metaprogramming in statically typed languages&lt;br /&gt;
=Metaprogramming in statically typed languages=&lt;br /&gt;
==Introduction==&lt;br /&gt;
===What is metaprogramming===&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming] refers to writing programs that can manipulate other programs or themselves. The program that is doing the manipulating is called the ''metaprogram'', and the program that is being manipulated is called the ''object program''. The language of the ''metaprogram'' is referred to as the ''metalanguage'' and the language of the object program is called the ''object language''. A language has the ability of [http://en.wikipedia.org/wiki/Reflection_(computer_programming) reflection] if it can be used to write a metaprogram that can manipulate a program written in the same language.&lt;br /&gt;
&lt;br /&gt;
===Metaprogramming in statically typed languages===&lt;br /&gt;
&lt;br /&gt;
Metaprogramming can be accomplished in both statically and dynamically typed languages, however [http://en.wikipedia.org/wiki/Dynamically_typed_language#Dynamic_typing dynamically typed languages] have some advantages over [http://en.wikipedia.org/wiki/Dynamically_typed_language#Dynamic_typing statically typed languages] when it comes to metaprogramming.&lt;br /&gt;
&lt;br /&gt;
One example of a metaprogramming task is to add a method to a particular class at runtime. Sample code in Ruby to perform this task is outlined below. First is a definition of a Ruby class called TestAddMethodAtRuntime:&lt;br /&gt;
&lt;br /&gt;
  class TestAddMethodAtRuntime&lt;br /&gt;
    def originalMethod&lt;br /&gt;
        puts &amp;quot;Original Method&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The code below created a new instance of the class. After that, it defines a new method called newlyAddedMethod. Then the original method is called, follow by the newly added method at runtime:&lt;br /&gt;
&lt;br /&gt;
  t = TestAddMethodAtRuntime.new&lt;br /&gt;
  def t.newlyAddedMethod&lt;br /&gt;
    puts &amp;quot;Newly Added Method&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  t.originalMethod&lt;br /&gt;
  puts &amp;quot;\n&amp;quot;&lt;br /&gt;
  t.newlyAddedMethod&lt;br /&gt;
&lt;br /&gt;
Below is the output of the code above:&lt;br /&gt;
&lt;br /&gt;
  Original Method&lt;br /&gt;
  Newly Added Method&lt;br /&gt;
&lt;br /&gt;
Accomplishing something like this in a statically typed language would be much more difficult or impossible depending on the language. It is for this reason that dynamically typed languages are usually viewed as being better equipped to handle metaprogramming overall.&lt;br /&gt;
&lt;br /&gt;
==Implementation==&lt;br /&gt;
===Programming Language API===&lt;br /&gt;
&lt;br /&gt;
One way that metaprogramming can be possible in a language is through an [http://en.wikipedia.org/wiki/API API] that helps programmers achieve a metaprogramming task. An example of this is a language that provides an API for reflection. One type of reflection is ''introspection'' in which the program can access the source code of the program itself.&lt;br /&gt;
&lt;br /&gt;
Below is an example from the Java API to illustrate how an API can be used to achieve some metaprogramming tasks:&lt;br /&gt;
&lt;br /&gt;
   import java.lang.reflect.*; &lt;br /&gt;
   public class DumpMethods {&lt;br /&gt;
   public static void main(String args[]) {&lt;br /&gt;
     	try {&lt;br /&gt;
           Class c = Class.forName(args[0]);&lt;br /&gt;
           Method m[] = c.getDeclaredMethods();&lt;br /&gt;
           for (int i = 0; i &amp;lt; m.length; i++) {&lt;br /&gt;
              System.out.println(m[i].toString());&lt;br /&gt;
           }&lt;br /&gt;
     	}&lt;br /&gt;
     	catch (Throwable e) {&lt;br /&gt;
        	System.err.println(e);&lt;br /&gt;
     	}&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
The above example prints out all of the methods that are declared for a particular class. This is an example of metaprogramming since the program is accessing the source code of itself.&lt;br /&gt;
&lt;br /&gt;
===Program transformation system===&lt;br /&gt;
&lt;br /&gt;
A program transformation system is something that takes a program as the input, and outputs a different program. One example of a program transformation system is the Java compiler. The Java compiler takes a Java program as its input, and outputs a .class file with platform independent bytecode in it. Another example of a program transformation system is a Java decompiler, which takes bytecode as an input and outputs a Java program.&lt;br /&gt;
&lt;br /&gt;
In the case of the Java compiler, the program input is being output into another program in a different &amp;quot;language&amp;quot;. There are other types of transformation systems that take a program as an input and output a different program in the same language. One example of this would be ''program migration'' which migrates source code to a newer or older version of the same language. This can be useful if upgrading a software system to run on a newer version of a framework.&lt;br /&gt;
&lt;br /&gt;
==Common Uses==&lt;br /&gt;
===Pre-generate static data at compile time===&lt;br /&gt;
One common use of metaprogramming in statically typed languages is to write programs that will pre-generate tables of data for use at runtime.&amp;lt;ref name=&amp;quot;use1&amp;quot;&amp;gt;Jonathan Bartlett, &amp;quot;The art of metaprogramming, Part 1: Introduction to metaprogramming&amp;quot;, 20 Oct 2005   http://www.ibm.com/developerworks/library/l-metaprog1/index.html#N10052&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One simple but useful code generator is to build static lookup tables. Often, in order to build fast functions in C programming, we simply create a lookup table of all of the answers. This means that we either need to pre-compute them by hand (which is wasteful of your time) or build them at runtime (which is wasteful of the user's time).&lt;br /&gt;
&lt;br /&gt;
In following example we will build a generator that will take a function or set of functions on an integer and build lookup tables for the answer.&lt;br /&gt;
&lt;br /&gt;
To think of how to make such a program, we can start from the end and work backward. Firstly we need a lookup table that will return square roots of numbers between 5 and 20. A simple program can be written to generate such a table like this:&lt;br /&gt;
&lt;br /&gt;
Generate and use a lookup table of square roots&lt;br /&gt;
&lt;br /&gt;
        /* our lookup table */&lt;br /&gt;
        double square_roots[21];&lt;br /&gt;
        /* function to load the table at runtime */&lt;br /&gt;
        void init_square_roots()&lt;br /&gt;
        {&lt;br /&gt;
           int i;&lt;br /&gt;
           for(i = 5; i &amp;lt; 21; i++)&lt;br /&gt;
             {&lt;br /&gt;
               square_roots[i] = sqrt((double)i);&lt;br /&gt;
             }&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
With that single macro, we can take away a lot of work for any program that has to generate mathematical tables indexed by integer. A little extra work would also allow tables containing full struct definitions; a little more would ensure that space isn't wasted at the front of the array with useless empty entries.&lt;br /&gt;
        /* program that uses the table */&lt;br /&gt;
        int main ()&lt;br /&gt;
        {&lt;br /&gt;
           init_square_roots();&lt;br /&gt;
           printf(&amp;quot;The square root of 5 is %f\n&amp;quot;, square_roots[5]);&lt;br /&gt;
           return 0;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
Now, to convert this to a statically initialized array, you would remove the first part of the program and replace it with something like this, calculated by hand:&lt;br /&gt;
&lt;br /&gt;
Square root program with a static lookup table&lt;br /&gt;
&lt;br /&gt;
        double square_roots[] = {&lt;br /&gt;
           /* these are the ones we skipped */ 0.0, 0.0, 0.0, 0.0, 0.0&lt;br /&gt;
           2.236068, /* Square root of 5 */&lt;br /&gt;
           2.449490, /* Square root of 6 */&lt;br /&gt;
           2.645751, /* Square root of 7 */&lt;br /&gt;
           2.828427, /* Square root of 8 */&lt;br /&gt;
           3.0, /* Square root of 9 */&lt;br /&gt;
           ...&lt;br /&gt;
           4.472136 /* Square root of 20 */&lt;br /&gt;
           };&lt;br /&gt;
&lt;br /&gt;
What is needed is a program that will produce these values and print them out in a table like the previous one so they are loaded in at compile-time.&lt;br /&gt;
&lt;br /&gt;
Code generator for the table macro&lt;br /&gt;
&lt;br /&gt;
           #!/usr/bin/perl  &lt;br /&gt;
           #&lt;br /&gt;
           #tablegen.pl &lt;br /&gt;
           #&lt;br /&gt;
           ##Puts each program line into $line&lt;br /&gt;
           while(my $line = &amp;lt;&amp;gt;)&lt;br /&gt;
           {&lt;br /&gt;
             #Is this a macro invocation?&lt;br /&gt;
             if($line =~ m/TABLE:/)&lt;br /&gt;
             {&lt;br /&gt;
                #If so, split it apart into its component pieces&lt;br /&gt;
                my ($dummy, $table_name, $type, $start_idx, $end_idx, $default, $procedure) = split(m/:/, $line, 7);&lt;br /&gt;
                #The main difference between C and Perl for mathematical expressions is that&lt;br /&gt;
                #Perl prefixes its variables with a dollar sign, so we will add that here&lt;br /&gt;
                $procedure =~ s/VAL/\$VAL/g;&lt;br /&gt;
                #Print out the array declaration&lt;br /&gt;
                print &amp;quot;${type} ${table_name} [] = {\n&amp;quot;;&lt;br /&gt;
                #Go through each array element&lt;br /&gt;
                foreach my $VAL (0 .. $end_idx)&lt;br /&gt;
                {&lt;br /&gt;
                   #Only process an answer if we have reached our starting index&lt;br /&gt;
                   if($VAL &amp;gt;= $start_idx)&lt;br /&gt;
                   {&lt;br /&gt;
                      #evaluate the procedure specified (this sets $@ if there are any errors)&lt;br /&gt;
                      $result = eval $procedure;&lt;br /&gt;
                      die(&amp;quot;Error processing: $@&amp;quot;) if $@;&lt;br /&gt;
                   }&lt;br /&gt;
                   else&lt;br /&gt;
                   {&lt;br /&gt;
                      #if we haven't reached the starting index, just use the default&lt;br /&gt;
                      $result = $default;&lt;br /&gt;
                   }&lt;br /&gt;
                   #Print out the value&lt;br /&gt;
                   print &amp;quot;\t${result}&amp;quot;;&lt;br /&gt;
                   #If there are more to be processed, add a comma after the value&lt;br /&gt;
                   if($VAL != $end_idx)&lt;br /&gt;
                   {&lt;br /&gt;
                      print &amp;quot;,&amp;quot;;&lt;br /&gt;
                   }&lt;br /&gt;
                   print &amp;quot;\n&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
                #Finish the declaration&lt;br /&gt;
                print &amp;quot;};\n&amp;quot;;&lt;br /&gt;
             }&lt;br /&gt;
             else&lt;br /&gt;
             {&lt;br /&gt;
                #If this is not a macro invocation, just copy the line directly to the output&lt;br /&gt;
                print $line;&lt;br /&gt;
             }&lt;br /&gt;
           }&lt;br /&gt;
&lt;br /&gt;
To run this program, do this:&lt;br /&gt;
&lt;br /&gt;
Running the code generator&lt;br /&gt;
&lt;br /&gt;
           ./tablegen.pl &amp;lt; sqrt.in &amp;gt; sqrt.c&lt;br /&gt;
           gcc sqrt.c -o sqrt&lt;br /&gt;
           ./a.out&lt;br /&gt;
&lt;br /&gt;
===Mini-language for boiler-plate===&lt;br /&gt;
If you have a large application where many of the functions include a lot of boilerplate code, it is often a good idea to create a mini-language that allows you to work with your boilerplate code in an easier fashion. This mini-language will then be converted into your regular source code language before compiling.&amp;lt;ref name=&amp;quot;use2&amp;quot;&amp;gt;Jonathan Bartlett, &amp;quot;The art of metaprogramming, Part 2: Metaprogramming using Scheme&amp;quot;, 02 May 2006  http://www.ibm.com/developerworks/linux/library/l-metaprog2/index.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following is an example:&lt;br /&gt;
&lt;br /&gt;
Let's say that we are building a CGI application consisting of many independent CGI scripts. In most CGI applications, much of the state is stored in a database, but only a session ID is passed to each script via a cookie.&lt;br /&gt;
&lt;br /&gt;
However, in nearly every page we need to know the other standard information (such as the username, group number, the current job being worked on, whatever else information is pertinent). In addition, we need to redirect the user if they do not have an appropriate cookie.&lt;br /&gt;
&lt;br /&gt;
            (define (handle-cgi-request req)&lt;br /&gt;
              (let (&lt;br /&gt;
                     (session-id (webserver:cookie req &amp;quot;sessionid&amp;quot;)))&lt;br /&gt;
                (if (not (webserver:valid-session-id session-id))&lt;br /&gt;
                    (webserver:redirect-to-login-page)&lt;br /&gt;
                    (let (&lt;br /&gt;
                          (username (webserver:username-for-session session-id))&lt;br /&gt;
                          (group (webserver:group-for-user username))&lt;br /&gt;
                          (current-job (webserver:current-job-for-user username)))&lt;br /&gt;
                       ;;Code for processing goes here&lt;br /&gt;
                       ))))&lt;br /&gt;
&lt;br /&gt;
While some of that can be handled by a procedure, the bindings certainly cannot. However, we can turn most of it into a macro. The macro can be implemented like this:&lt;br /&gt;
&lt;br /&gt;
            (define-syntax cgi-boilerplate&lt;br /&gt;
              (lambda (x)&lt;br /&gt;
                (syntax-case x ()&lt;br /&gt;
                 (&lt;br /&gt;
                   (cgi-boilerplate expr)&lt;br /&gt;
                   (datum-&amp;gt;syntax-object&lt;br /&gt;
                     (syntax k)&lt;br /&gt;
                      (let (&lt;br /&gt;
                            (session-id (webserver:cookie req &amp;quot;sessionid&amp;quot;)))&lt;br /&gt;
                           (if (not (webserver:valid-session-id session-id))&lt;br /&gt;
                               (webserver:redirect-to-login-page)&lt;br /&gt;
                               (let (&lt;br /&gt;
                                      (username (webserver:username-for-session session-id))&lt;br /&gt;
                                      (group (webserver:group-for-user username))&lt;br /&gt;
                                      (current-job (webserver:current-job-for-user username)))&lt;br /&gt;
                                      (syntax-object-&amp;gt;datum (syntax expr))))))&lt;br /&gt;
                  )&lt;br /&gt;
             )))&lt;br /&gt;
&lt;br /&gt;
We can now create new forms based on our boilerplate code by doing the following:&lt;br /&gt;
&lt;br /&gt;
            (define (handle-cgi-request req)&lt;br /&gt;
              (cgi-boilerplate&lt;br /&gt;
               (begin&lt;br /&gt;
                 ;;Do whatever I want here&lt;br /&gt;
                 )))&lt;br /&gt;
&lt;br /&gt;
In addition, since we are not defining our variables explicitly, adding new variable definitions to our boilerplate won't affect its calling conventions, so new features can be added without having to create a whole new function.&lt;br /&gt;
&lt;br /&gt;
In any large project, there are inevitably templates to follow which cannot be reduced to functions, usually because of the bindings being created. Using boilerplate macros can make maintenance of such templated code much easier.&lt;br /&gt;
&lt;br /&gt;
Likewise, other standard macros can be created which make use of variables defined in the boilerplate. Using macros like this significantly reduces typing because you do not have to constantly be writing and rewriting variable bindings, derivations, and parameter passing. This also reduces the potential for errors in such code.&lt;br /&gt;
&lt;br /&gt;
Realize though that boilerplate macros are not a panacea. There are many significant problems that can occur, including:&lt;br /&gt;
*  Accidentally overwriting bindings by introducing a variable name that was previously defined in a macro.&lt;br /&gt;
*  Difficulty tracing problems because the inputs and the outputs of the macros are implicit, not explicit.&lt;br /&gt;
&lt;br /&gt;
These can be largely avoided by doing a few things in conjunction with your boilerplate macros:&lt;br /&gt;
*  Have a naming convention which clearly labels macros as such, as well as indicate that a variable came from boilerplate code. This could be done by affixing -m to macros and -b to variables defined within a boilerplate.&lt;br /&gt;
*  Carefully document all boilerplate macros, especially the introduced variable bindings and all changes between versions.&lt;br /&gt;
*  Only use boilerplate macros when the savings in repetitiveness clearly outweigh the negatives of implicit functionality.&lt;br /&gt;
&lt;br /&gt;
===Abbreviate statements and prevent mistakes===&lt;br /&gt;
A lot of programming languages make you write really verbose statements to do really simple things. Code-generating programs allow you to abbreviate such statements and save a lot of typing, which also prevents a lot of mistakes because there is less chance of mistyping.&lt;br /&gt;
&lt;br /&gt;
=Metaprogramming Framework in Java=&lt;br /&gt;
==Reflection==&lt;br /&gt;
===Introduction to reflection===&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_(computer_programming) Reflection] is the ability to introspect metalevel information about the program structure itself at runtime. Usually this metalevel information is modeled using the general abstraction mechanisms available in the language. In Java, reflection enables to discover information about the loaded classes:&lt;br /&gt;
*  Fields,&lt;br /&gt;
*  Methods, and&lt;br /&gt;
*  Constructors&lt;br /&gt;
*  Generics information&lt;br /&gt;
*  Metadata annotations&lt;br /&gt;
It also enables to use these metaobjects to their instances in runtime environment.&amp;lt;ref name=&amp;quot;meta_java&amp;quot;&amp;gt;Abdelmonaim Remani, &amp;quot;The Art of Metaprogramming in Java&amp;quot;, Jul 19, 2012  http://www.slideshare.net/PolymathicCoder/the-art-of-metaprogramming-in-java&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following is the Java metalevel architecture&amp;lt;ref name=&amp;quot;reflection&amp;quot;&amp;gt;Mika Haapakorpi, &amp;quot;Meta Programming In Java&amp;quot;&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Metalevel architecture.PNG]]&lt;br /&gt;
&lt;br /&gt;
===Dynamic proxy classes===&lt;br /&gt;
A dynamic proxy class implements a list of interfaces specified at runtime when the class is created.&lt;br /&gt;
*  A proxy interface is an interface that is implemented by a proxy class.&lt;br /&gt;
*  A proxy instance is an instance of a proxy class which has an associated invocation handler object.&lt;br /&gt;
The following is an example of dynamic proxy classes&lt;br /&gt;
&lt;br /&gt;
[[File:Dynamic_proxy_class.PNG]]&lt;br /&gt;
&lt;br /&gt;
==Generics==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Generics_in_Java Generics] are a facility of generic programming that was added to the Java programming language in 2004 as part of J2SE 5.0. They allow &amp;quot;a type or method to operate on objects of various types while providing compile-time type safety.&amp;quot;  The generics can be used in classes, interfaces, methods and constructors.&amp;lt;ref name=&amp;quot;reflection&amp;quot;&amp;gt;Mika Haapakorpi, &amp;quot;Meta Programming In Java&amp;quot;&amp;lt;/ref&amp;gt;&lt;br /&gt;
===Two new types in generics===&lt;br /&gt;
Two new types in generics:&lt;br /&gt;
*  Parametrized types&lt;br /&gt;
*  Type variables&lt;br /&gt;
&lt;br /&gt;
A type variable is an unqualified identifier. Class and interface declarations can have type arguments (type variables). Method and constructors definitions can have type arguments (type variables). &lt;br /&gt;
&lt;br /&gt;
The following is an example for type variables&lt;br /&gt;
&lt;br /&gt;
       List&amp;lt;String&amp;gt; anExample = new ArrayList&amp;lt;String&amp;gt;()&lt;br /&gt;
&lt;br /&gt;
*  List : interface&lt;br /&gt;
*  ArrayList : class&lt;br /&gt;
*  String : class (the actual type argument)&lt;br /&gt;
*  List&amp;lt;String&amp;gt; and ArrayList&amp;lt;String&amp;gt; : parametrized type&lt;br /&gt;
&lt;br /&gt;
===Class/Interface Declarations===&lt;br /&gt;
The following is an example of class declaration:&lt;br /&gt;
&lt;br /&gt;
       public interface List&amp;lt;E&amp;gt; {&lt;br /&gt;
         void add(Ex);&lt;br /&gt;
         Iterator&amp;lt;E&amp;gt; iterator();&lt;br /&gt;
       }&lt;br /&gt;
       public interface Iterator&amp;lt;E&amp;gt; {&lt;br /&gt;
         E next();&lt;br /&gt;
         booleanhasNext();&lt;br /&gt;
       }&lt;br /&gt;
       List&amp;lt;String&amp;gt; anExample;&lt;br /&gt;
       anExample.add(”sdfdfss”);&lt;br /&gt;
       anExample.add(new Object()); // compile time error&lt;br /&gt;
       String aTest = anExample.iterator().next();&lt;br /&gt;
&lt;br /&gt;
==Metadata annotation==&lt;br /&gt;
An annotation, in the Java computer programming language, is a form of syntactic metadata that can be added to Java source code. Classes, methods, variables, parameters and packages may be annotated.&amp;lt;ref name=&amp;quot;reflection&amp;quot;&amp;gt;Mika Haapakorpi, &amp;quot;Meta Programming In Java&amp;quot;&amp;lt;/ref&amp;gt; &lt;br /&gt;
&lt;br /&gt;
The following is an example of annotation:&lt;br /&gt;
&lt;br /&gt;
        /**&lt;br /&gt;
        * Designates a formatter to pretty-print the annotated class.&lt;br /&gt;
        */&lt;br /&gt;
        public @interface PrettyPrinter{&lt;br /&gt;
        Class&amp;lt;? extends Formatter&amp;gt; value();&lt;br /&gt;
        }&lt;br /&gt;
        // Single-member annotation with Class&lt;br /&gt;
        // member restricted by bounded wildcard&lt;br /&gt;
        // The annotation presumes the existence of this class.&lt;br /&gt;
        class GorgeousFormatterimplements Formatter { ... }&lt;br /&gt;
        @PrettyPrinter(GorgeousFormatter.class)&lt;br /&gt;
        public class Petunia { ... }&lt;br /&gt;
&lt;br /&gt;
‘@’ indicates the start of an annotation definition.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:5.  K. Czarnecki, &amp;quot;Generative Programming&amp;quot; chapter 8, Static Metaprogramming in C++&lt;br /&gt;
&lt;br /&gt;
:6.  http://www.program-transformation.org&lt;br /&gt;
&lt;br /&gt;
:7.  http://www.codeproject.com/Articles/15429/Ruby-Add-class-methods-at-runtime&lt;br /&gt;
&lt;br /&gt;
:8.  http://docs.oracle.com/javase/tutorial/reflect/index.html&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Cyang14</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1_1h_jc&amp;diff=73624</id>
		<title>CSC/ECE 517 Spring 2013/ch1 1h jc</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1_1h_jc&amp;diff=73624"/>
		<updated>2013-02-24T16:53:16Z</updated>

		<summary type="html">&lt;p&gt;Cyang14: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Metaprogramming in statically typed languages&lt;br /&gt;
=Metaprogramming in statically typed languages=&lt;br /&gt;
==Introduction==&lt;br /&gt;
===What is metaprogramming===&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming] refers to writing programs that can manipulate other programs or themselves. The program that is doing the manipulating is called the ''metaprogram'', and the program that is being manipulated is called the ''object program''. The language of the ''metaprogram'' is referred to as the ''metalanguage'' and the language of the object program is called the ''object language''. A language has the ability of [http://en.wikipedia.org/wiki/Reflection_(computer_programming) reflection] if it can be used to write a metaprogram that can manipulate a program written in the same language.&lt;br /&gt;
&lt;br /&gt;
===Metaprogramming in statically typed languages===&lt;br /&gt;
&lt;br /&gt;
Metaprogramming can be accomplished in both statically and dynamically typed languages, however [http://en.wikipedia.org/wiki/Dynamically_typed_language#Dynamic_typing dynamically typed languages] have some advantages over [http://en.wikipedia.org/wiki/Dynamically_typed_language#Dynamic_typing statically typed languages] when it comes to metaprogramming.&lt;br /&gt;
&lt;br /&gt;
One example of a metaprogramming task is to add a method to a particular class at runtime. Sample code in Ruby to perform this task is outlined below. First is a definition of a Ruby class called TestAddMethodAtRuntime:&lt;br /&gt;
&lt;br /&gt;
  class TestAddMethodAtRuntime&lt;br /&gt;
    def originalMethod&lt;br /&gt;
        puts &amp;quot;Original Method&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The code below created a new instance of the class. After that, it defines a new method called newlyAddedMethod. Then the original method is called, follow by the newly added method at runtime:&lt;br /&gt;
&lt;br /&gt;
  t = TestAddMethodAtRuntime.new&lt;br /&gt;
  def t.newlyAddedMethod&lt;br /&gt;
    puts &amp;quot;Newly Added Method&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  t.originalMethod&lt;br /&gt;
  puts &amp;quot;\n&amp;quot;&lt;br /&gt;
  t.newlyAddedMethod&lt;br /&gt;
&lt;br /&gt;
Below is the output of the code above:&lt;br /&gt;
&lt;br /&gt;
  Original Method&lt;br /&gt;
  Newly Added Method&lt;br /&gt;
&lt;br /&gt;
Accomplishing something like this in a statically typed language would be much more difficult or impossible depending on the language. It is for this reason that dynamically typed languages are usually viewed as being better equipped to handle metaprogramming overall.&lt;br /&gt;
&lt;br /&gt;
==Implementation==&lt;br /&gt;
===Programming Language API===&lt;br /&gt;
&lt;br /&gt;
One way that metaprogramming can be possible in a language is through an [http://en.wikipedia.org/wiki/API API] that helps programmers achieve a metaprogramming task. An example of this is a language that provides an API for reflection. One type of reflection is ''introspection'' in which the program can access the source code of the program itself.&lt;br /&gt;
&lt;br /&gt;
Below is an example from the Java API to illustrate how an API can be used to achieve some metaprogramming tasks:&lt;br /&gt;
&lt;br /&gt;
   import java.lang.reflect.*; &lt;br /&gt;
   public class DumpMethods {&lt;br /&gt;
   public static void main(String args[]) {&lt;br /&gt;
     	try {&lt;br /&gt;
           Class c = Class.forName(args[0]);&lt;br /&gt;
           Method m[] = c.getDeclaredMethods();&lt;br /&gt;
           for (int i = 0; i &amp;lt; m.length; i++) {&lt;br /&gt;
              System.out.println(m[i].toString());&lt;br /&gt;
           }&lt;br /&gt;
     	}&lt;br /&gt;
     	catch (Throwable e) {&lt;br /&gt;
        	System.err.println(e);&lt;br /&gt;
     	}&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
The above example prints out all of the methods that are declared for a particular class. This is an example of metaprogramming since the program is accessing the source code of itself.&lt;br /&gt;
&lt;br /&gt;
===Program transformation system===&lt;br /&gt;
&lt;br /&gt;
A program transformation system is something that takes a program as the input, and outputs a different program. One example of a program transformation system is the Java compiler. The Java compiler takes a Java program as its input, and outputs a .class file with platform independent bytecode in it. Another example of a program transformation system is a Java decompiler, which takes bytecode as an input and outputs a Java program.&lt;br /&gt;
&lt;br /&gt;
In the case of the Java compiler, the program input is being output into another program in a different &amp;quot;language&amp;quot;. There are other types of transformation systems that take a program as an input and output a different program in the same language. One example of this would be ''program migration'' which migrates source code to a newer or older version of the same language. This can be useful if upgrading a software system to run on a newer version of a framework.&lt;br /&gt;
&lt;br /&gt;
==Common Uses==&lt;br /&gt;
===Pre-generate static data at compile time===&lt;br /&gt;
One common use of metaprogramming in statically typed languages is to write programs that will pre-generate tables of data for use at runtime.&amp;lt;ref name=&amp;quot;use1&amp;quot;&amp;gt;Jonathan Bartlett, &amp;quot;The art of metaprogramming, Part 1: Introduction to metaprogramming&amp;quot;, 20 Oct 2005   http://www.ibm.com/developerworks/library/l-metaprog1/index.html#N10052&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One simple but useful code generator is to build static lookup tables. Often, in order to build fast functions in C programming, we simply create a lookup table of all of the answers. This means that we either need to pre-compute them by hand (which is wasteful of your time) or build them at runtime (which is wasteful of the user's time).&lt;br /&gt;
&lt;br /&gt;
In following example we will build a generator that will take a function or set of functions on an integer and build lookup tables for the answer.&lt;br /&gt;
&lt;br /&gt;
To think of how to make such a program, we can start from the end and work backward. Firstly we need a lookup table that will return square roots of numbers between 5 and 20. A simple program can be written to generate such a table like this:&lt;br /&gt;
&lt;br /&gt;
Generate and use a lookup table of square roots&lt;br /&gt;
&lt;br /&gt;
        /* our lookup table */&lt;br /&gt;
        double square_roots[21];&lt;br /&gt;
        /* function to load the table at runtime */&lt;br /&gt;
        void init_square_roots()&lt;br /&gt;
        {&lt;br /&gt;
           int i;&lt;br /&gt;
           for(i = 5; i &amp;lt; 21; i++)&lt;br /&gt;
             {&lt;br /&gt;
               square_roots[i] = sqrt((double)i);&lt;br /&gt;
             }&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
With that single macro, we can take away a lot of work for any program that has to generate mathematical tables indexed by integer. A little extra work would also allow tables containing full struct definitions; a little more would ensure that space isn't wasted at the front of the array with useless empty entries.&lt;br /&gt;
        /* program that uses the table */&lt;br /&gt;
        int main ()&lt;br /&gt;
        {&lt;br /&gt;
           init_square_roots();&lt;br /&gt;
           printf(&amp;quot;The square root of 5 is %f\n&amp;quot;, square_roots[5]);&lt;br /&gt;
           return 0;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
Now, to convert this to a statically initialized array, you would remove the first part of the program and replace it with something like this, calculated by hand:&lt;br /&gt;
&lt;br /&gt;
Square root program with a static lookup table&lt;br /&gt;
&lt;br /&gt;
        double square_roots[] = {&lt;br /&gt;
           /* these are the ones we skipped */ 0.0, 0.0, 0.0, 0.0, 0.0&lt;br /&gt;
           2.236068, /* Square root of 5 */&lt;br /&gt;
           2.449490, /* Square root of 6 */&lt;br /&gt;
           2.645751, /* Square root of 7 */&lt;br /&gt;
           2.828427, /* Square root of 8 */&lt;br /&gt;
           3.0, /* Square root of 9 */&lt;br /&gt;
           ...&lt;br /&gt;
           4.472136 /* Square root of 20 */&lt;br /&gt;
           };&lt;br /&gt;
&lt;br /&gt;
What is needed is a program that will produce these values and print them out in a table like the previous one so they are loaded in at compile-time.&lt;br /&gt;
&lt;br /&gt;
Code generator for the table macro&lt;br /&gt;
&lt;br /&gt;
           #!/usr/bin/perl  &lt;br /&gt;
           #&lt;br /&gt;
           #tablegen.pl &lt;br /&gt;
           #&lt;br /&gt;
           ##Puts each program line into $line&lt;br /&gt;
           while(my $line = &amp;lt;&amp;gt;)&lt;br /&gt;
           {&lt;br /&gt;
             #Is this a macro invocation?&lt;br /&gt;
             if($line =~ m/TABLE:/)&lt;br /&gt;
             {&lt;br /&gt;
                #If so, split it apart into its component pieces&lt;br /&gt;
                my ($dummy, $table_name, $type, $start_idx, $end_idx, $default, $procedure) = split(m/:/, $line, 7);&lt;br /&gt;
                #The main difference between C and Perl for mathematical expressions is that&lt;br /&gt;
                #Perl prefixes its variables with a dollar sign, so we will add that here&lt;br /&gt;
                $procedure =~ s/VAL/\$VAL/g;&lt;br /&gt;
                #Print out the array declaration&lt;br /&gt;
                print &amp;quot;${type} ${table_name} [] = {\n&amp;quot;;&lt;br /&gt;
                #Go through each array element&lt;br /&gt;
                foreach my $VAL (0 .. $end_idx)&lt;br /&gt;
                {&lt;br /&gt;
                   #Only process an answer if we have reached our starting index&lt;br /&gt;
                   if($VAL &amp;gt;= $start_idx)&lt;br /&gt;
                   {&lt;br /&gt;
                      #evaluate the procedure specified (this sets $@ if there are any errors)&lt;br /&gt;
                      $result = eval $procedure;&lt;br /&gt;
                      die(&amp;quot;Error processing: $@&amp;quot;) if $@;&lt;br /&gt;
                   }&lt;br /&gt;
                   else&lt;br /&gt;
                   {&lt;br /&gt;
                      #if we haven't reached the starting index, just use the default&lt;br /&gt;
                      $result = $default;&lt;br /&gt;
                   }&lt;br /&gt;
                   #Print out the value&lt;br /&gt;
                   print &amp;quot;\t${result}&amp;quot;;&lt;br /&gt;
                   #If there are more to be processed, add a comma after the value&lt;br /&gt;
                   if($VAL != $end_idx)&lt;br /&gt;
                   {&lt;br /&gt;
                      print &amp;quot;,&amp;quot;;&lt;br /&gt;
                   }&lt;br /&gt;
                   print &amp;quot;\n&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
                #Finish the declaration&lt;br /&gt;
                print &amp;quot;};\n&amp;quot;;&lt;br /&gt;
             }&lt;br /&gt;
             else&lt;br /&gt;
             {&lt;br /&gt;
                #If this is not a macro invocation, just copy the line directly to the output&lt;br /&gt;
                print $line;&lt;br /&gt;
             }&lt;br /&gt;
           }&lt;br /&gt;
&lt;br /&gt;
To run this program, do this:&lt;br /&gt;
&lt;br /&gt;
Running the code generator&lt;br /&gt;
&lt;br /&gt;
           ./tablegen.pl &amp;lt; sqrt.in &amp;gt; sqrt.c&lt;br /&gt;
           gcc sqrt.c -o sqrt&lt;br /&gt;
           ./a.out&lt;br /&gt;
&lt;br /&gt;
===Mini-language for boiler-plate===&lt;br /&gt;
If you have a large application where many of the functions include a lot of boilerplate code, it is often a good idea to create a mini-language that allows you to work with your boilerplate code in an easier fashion. This mini-language will then be converted into your regular source code language before compiling.&amp;lt;ref name=&amp;quot;use2&amp;quot;&amp;gt;Jonathan Bartlett, &amp;quot;The art of metaprogramming, Part 2: Metaprogramming using Scheme&amp;quot;, 02 May 2006  http://www.ibm.com/developerworks/linux/library/l-metaprog2/index.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following is an example:&lt;br /&gt;
&lt;br /&gt;
Let's say that we are building a CGI application consisting of many independent CGI scripts. In most CGI applications, much of the state is stored in a database, but only a session ID is passed to each script via a cookie.&lt;br /&gt;
&lt;br /&gt;
However, in nearly every page we need to know the other standard information (such as the username, group number, the current job being worked on, whatever else information is pertinent). In addition, we need to redirect the user if they do not have an appropriate cookie.&lt;br /&gt;
&lt;br /&gt;
            (define (handle-cgi-request req)&lt;br /&gt;
              (let (&lt;br /&gt;
                     (session-id (webserver:cookie req &amp;quot;sessionid&amp;quot;)))&lt;br /&gt;
                (if (not (webserver:valid-session-id session-id))&lt;br /&gt;
                    (webserver:redirect-to-login-page)&lt;br /&gt;
                    (let (&lt;br /&gt;
                          (username (webserver:username-for-session session-id))&lt;br /&gt;
                          (group (webserver:group-for-user username))&lt;br /&gt;
                          (current-job (webserver:current-job-for-user username)))&lt;br /&gt;
                       ;;Code for processing goes here&lt;br /&gt;
                       ))))&lt;br /&gt;
&lt;br /&gt;
While some of that can be handled by a procedure, the bindings certainly cannot. However, we can turn most of it into a macro. The macro can be implemented like this:&lt;br /&gt;
&lt;br /&gt;
            (define-syntax cgi-boilerplate&lt;br /&gt;
              (lambda (x)&lt;br /&gt;
                (syntax-case x ()&lt;br /&gt;
                 (&lt;br /&gt;
                   (cgi-boilerplate expr)&lt;br /&gt;
                   (datum-&amp;gt;syntax-object&lt;br /&gt;
                     (syntax k)&lt;br /&gt;
                      (let (&lt;br /&gt;
                            (session-id (webserver:cookie req &amp;quot;sessionid&amp;quot;)))&lt;br /&gt;
                           (if (not (webserver:valid-session-id session-id))&lt;br /&gt;
                               (webserver:redirect-to-login-page)&lt;br /&gt;
                               (let (&lt;br /&gt;
                                      (username (webserver:username-for-session session-id))&lt;br /&gt;
                                      (group (webserver:group-for-user username))&lt;br /&gt;
                                      (current-job (webserver:current-job-for-user username)))&lt;br /&gt;
                                      (syntax-object-&amp;gt;datum (syntax expr))))))&lt;br /&gt;
                  )&lt;br /&gt;
             )))&lt;br /&gt;
&lt;br /&gt;
We can now create new forms based on our boilerplate code by doing the following:&lt;br /&gt;
&lt;br /&gt;
            (define (handle-cgi-request req)&lt;br /&gt;
              (cgi-boilerplate&lt;br /&gt;
               (begin&lt;br /&gt;
                 ;;Do whatever I want here&lt;br /&gt;
                 )))&lt;br /&gt;
&lt;br /&gt;
In addition, since we are not defining our variables explicitly, adding new variable definitions to our boilerplate won't affect its calling conventions, so new features can be added without having to create a whole new function.&lt;br /&gt;
&lt;br /&gt;
In any large project, there are inevitably templates to follow which cannot be reduced to functions, usually because of the bindings being created. Using boilerplate macros can make maintenance of such templated code much easier.&lt;br /&gt;
&lt;br /&gt;
Likewise, other standard macros can be created which make use of variables defined in the boilerplate. Using macros like this significantly reduces typing because you do not have to constantly be writing and rewriting variable bindings, derivations, and parameter passing. This also reduces the potential for errors in such code.&lt;br /&gt;
&lt;br /&gt;
Realize though that boilerplate macros are not a panacea. There are many significant problems that can occur, including:&lt;br /&gt;
*  Accidentally overwriting bindings by introducing a variable name that was previously defined in a macro.&lt;br /&gt;
*  Difficulty tracing problems because the inputs and the outputs of the macros are implicit, not explicit.&lt;br /&gt;
&lt;br /&gt;
These can be largely avoided by doing a few things in conjunction with your boilerplate macros:&lt;br /&gt;
*  Have a naming convention which clearly labels macros as such, as well as indicate that a variable came from boilerplate code. This could be done by affixing -m to macros and -b to variables defined within a boilerplate.&lt;br /&gt;
*  Carefully document all boilerplate macros, especially the introduced variable bindings and all changes between versions.&lt;br /&gt;
*  Only use boilerplate macros when the savings in repetitiveness clearly outweigh the negatives of implicit functionality.&lt;br /&gt;
&lt;br /&gt;
===Abbreviate statements and prevent mistakes===&lt;br /&gt;
A lot of programming languages make you write really verbose statements to do really simple things. Code-generating programs allow you to abbreviate such statements and save a lot of typing, which also prevents a lot of mistakes because there is less chance of mistyping.&lt;br /&gt;
&lt;br /&gt;
=Metaprogramming Framework in Java=&lt;br /&gt;
==Reflection==&lt;br /&gt;
===Introduction to reflection===&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_(computer_programming) Reflection] is the ability to introspect metalevel information about the program structure itself at runtime. Usually this metalevel information is modeled using the general abstraction mechanisms available in the language. In Java, reflection enables to discover information about the loaded classes:&lt;br /&gt;
*  Fields,&lt;br /&gt;
*  Methods, and&lt;br /&gt;
*  Constructors&lt;br /&gt;
*  Generics information&lt;br /&gt;
*  Metadata annotations&lt;br /&gt;
It also enables to use these metaobjects to their instances in runtime environment.&amp;lt;ref name=&amp;quot;meta_java&amp;quot;&amp;gt;Abdelmonaim Remani, &amp;quot;The Art of Metaprogramming in Java&amp;quot;, Jul 19, 2012  http://www.slideshare.net/PolymathicCoder/the-art-of-metaprogramming-in-java&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following is the Java metalevel architecture&amp;lt;ref name=&amp;quot;reflection&amp;quot;&amp;gt;Mika Haapakorpi, &amp;quot;Meta Programming In Java&amp;quot;&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Metalevel architecture.PNG]]&lt;br /&gt;
&lt;br /&gt;
===Dynamic proxy classes===&lt;br /&gt;
A dynamic proxy class implements a list of interfaces specified at runtime when the class is created.&lt;br /&gt;
*  A proxy interface is an interface that is implemented by a proxy class.&lt;br /&gt;
*  A proxy instance is an instance of a proxy class which has an associated invocation handler object.&lt;br /&gt;
The following is an example of dynamic proxy classes&lt;br /&gt;
&lt;br /&gt;
[[File:Dynamic_proxy_class.PNG]]&lt;br /&gt;
&lt;br /&gt;
==Generics==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Generics_in_Java Generics] are a facility of generic programming that was added to the Java programming language in 2004 as part of J2SE 5.0. They allow &amp;quot;a type or method to operate on objects of various types while providing compile-time type safety.&amp;quot;  The generics can be used in classes, interfaces, methods and constructors.&amp;lt;ref name=&amp;quot;reflection&amp;quot;&amp;gt;Mika Haapakorpi, &amp;quot;Meta Programming In Java&amp;quot;&amp;lt;/ref&amp;gt;&lt;br /&gt;
===Two new types in generics===&lt;br /&gt;
Two new types in generics:&lt;br /&gt;
*  Parametrized types&lt;br /&gt;
*  Type variables&lt;br /&gt;
&lt;br /&gt;
A type variable is an unqualified identifier. Class and interface declarations can have type arguments (type variables). Method and constructors definitions can have type arguments (type variables). &lt;br /&gt;
&lt;br /&gt;
The following is an example for type variables&lt;br /&gt;
&lt;br /&gt;
       List&amp;lt;String&amp;gt; anExample = new ArrayList&amp;lt;String&amp;gt;()&lt;br /&gt;
&lt;br /&gt;
*  List : interface&lt;br /&gt;
*  ArrayList : class&lt;br /&gt;
*  String : class (the actual type argument)&lt;br /&gt;
*  List&amp;lt;String&amp;gt; and ArrayList&amp;lt;String&amp;gt; : parametrized type&lt;br /&gt;
&lt;br /&gt;
===Class/Interface Declarations===&lt;br /&gt;
The following is an example of class declaration:&lt;br /&gt;
&lt;br /&gt;
       public interface List&amp;lt;E&amp;gt; {&lt;br /&gt;
         void add(Ex);&lt;br /&gt;
         Iterator&amp;lt;E&amp;gt; iterator();&lt;br /&gt;
       }&lt;br /&gt;
       public interface Iterator&amp;lt;E&amp;gt; {&lt;br /&gt;
         E next();&lt;br /&gt;
         booleanhasNext();&lt;br /&gt;
       }&lt;br /&gt;
       List&amp;lt;String&amp;gt; anExample;&lt;br /&gt;
       anExample.add(”sdfdfss”);&lt;br /&gt;
       anExample.add(new Object()); // compile time error&lt;br /&gt;
       String aTest = anExample.iterator().next();&lt;br /&gt;
&lt;br /&gt;
==Metadata annotation==&lt;br /&gt;
An annotation, in the Java computer programming language, is a form of syntactic metadata that can be added to Java source code. Classes, methods, variables, parameters and packages may be annotated.&amp;lt;ref name=&amp;quot;reflection&amp;quot;&amp;gt;Mika Haapakorpi, &amp;quot;Meta Programming In Java&amp;quot;&amp;lt;/ref&amp;gt; &lt;br /&gt;
&lt;br /&gt;
The following is an example of annotation:&lt;br /&gt;
&lt;br /&gt;
        /**&lt;br /&gt;
        * Designates a formatter to pretty-print the annotated class.&lt;br /&gt;
        */&lt;br /&gt;
        public @interface PrettyPrinter{&lt;br /&gt;
        Class&amp;lt;? extends Formatter&amp;gt; value();&lt;br /&gt;
        }&lt;br /&gt;
        // Single-member annotation with Class&lt;br /&gt;
        // member restricted by bounded wildcard&lt;br /&gt;
        // The annotation presumes the existence of this class.&lt;br /&gt;
        class GorgeousFormatterimplements Formatter { ... }&lt;br /&gt;
        @PrettyPrinter(GorgeousFormatter.class)&lt;br /&gt;
        public class Petunia { ... }&lt;br /&gt;
&lt;br /&gt;
‘@’ indicates the start of an annotation definition.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:5. K. Czarnecki, &amp;quot;Generative Programming&amp;quot; chapter 8, Static Metaprogramming in C++&lt;br /&gt;
&lt;br /&gt;
:6. http://www.program-transformation.org&lt;br /&gt;
&lt;br /&gt;
:7. http://www.codeproject.com/Articles/15429/Ruby-Add-class-methods-at-runtime&lt;br /&gt;
&lt;br /&gt;
:8. http://docs.oracle.com/javase/tutorial/reflect/index.html&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Cyang14</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1_1h_jc&amp;diff=73623</id>
		<title>CSC/ECE 517 Spring 2013/ch1 1h jc</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1_1h_jc&amp;diff=73623"/>
		<updated>2013-02-24T16:52:49Z</updated>

		<summary type="html">&lt;p&gt;Cyang14: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Metaprogramming in statically typed languages&lt;br /&gt;
=Metaprogramming in statically typed languages=&lt;br /&gt;
==Introduction==&lt;br /&gt;
===What is metaprogramming===&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming] refers to writing programs that can manipulate other programs or themselves. The program that is doing the manipulating is called the ''metaprogram'', and the program that is being manipulated is called the ''object program''. The language of the ''metaprogram'' is referred to as the ''metalanguage'' and the language of the object program is called the ''object language''. A language has the ability of [http://en.wikipedia.org/wiki/Reflection_(computer_programming) reflection] if it can be used to write a metaprogram that can manipulate a program written in the same language.&lt;br /&gt;
&lt;br /&gt;
===Metaprogramming in statically typed languages===&lt;br /&gt;
&lt;br /&gt;
Metaprogramming can be accomplished in both statically and dynamically typed languages, however [http://en.wikipedia.org/wiki/Dynamically_typed_language#Dynamic_typing dynamically typed languages] have some advantages over [http://en.wikipedia.org/wiki/Dynamically_typed_language#Dynamic_typing statically typed languages] when it comes to metaprogramming.&lt;br /&gt;
&lt;br /&gt;
One example of a metaprogramming task is to add a method to a particular class at runtime. Sample code in Ruby to perform this task is outlined below. First is a definition of a Ruby class called TestAddMethodAtRuntime:&lt;br /&gt;
&lt;br /&gt;
  class TestAddMethodAtRuntime&lt;br /&gt;
    def originalMethod&lt;br /&gt;
        puts &amp;quot;Original Method&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The code below created a new instance of the class. After that, it defines a new method called newlyAddedMethod. Then the original method is called, follow by the newly added method at runtime:&lt;br /&gt;
&lt;br /&gt;
  t = TestAddMethodAtRuntime.new&lt;br /&gt;
  def t.newlyAddedMethod&lt;br /&gt;
    puts &amp;quot;Newly Added Method&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  t.originalMethod&lt;br /&gt;
  puts &amp;quot;\n&amp;quot;&lt;br /&gt;
  t.newlyAddedMethod&lt;br /&gt;
&lt;br /&gt;
Below is the output of the code above:&lt;br /&gt;
&lt;br /&gt;
  Original Method&lt;br /&gt;
  Newly Added Method&lt;br /&gt;
&lt;br /&gt;
Accomplishing something like this in a statically typed language would be much more difficult or impossible depending on the language. It is for this reason that dynamically typed languages are usually viewed as being better equipped to handle metaprogramming overall.&lt;br /&gt;
&lt;br /&gt;
==Implementation==&lt;br /&gt;
===Programming Language API===&lt;br /&gt;
&lt;br /&gt;
One way that metaprogramming can be possible in a language is through an [http://en.wikipedia.org/wiki/API API] that helps programmers achieve a metaprogramming task. An example of this is a language that provides an API for reflection. One type of reflection is ''introspection'' in which the program can access the source code of the program itself.&lt;br /&gt;
&lt;br /&gt;
Below is an example from the Java API to illustrate how an API can be used to achieve some metaprogramming tasks:&lt;br /&gt;
&lt;br /&gt;
   import java.lang.reflect.*; &lt;br /&gt;
   public class DumpMethods {&lt;br /&gt;
   public static void main(String args[]) {&lt;br /&gt;
     	try {&lt;br /&gt;
           Class c = Class.forName(args[0]);&lt;br /&gt;
           Method m[] = c.getDeclaredMethods();&lt;br /&gt;
           for (int i = 0; i &amp;lt; m.length; i++) {&lt;br /&gt;
              System.out.println(m[i].toString());&lt;br /&gt;
           }&lt;br /&gt;
     	}&lt;br /&gt;
     	catch (Throwable e) {&lt;br /&gt;
        	System.err.println(e);&lt;br /&gt;
     	}&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
The above example prints out all of the methods that are declared for a particular class. This is an example of metaprogramming since the program is accessing the source code of itself.&lt;br /&gt;
&lt;br /&gt;
===Program transformation system===&lt;br /&gt;
&lt;br /&gt;
A program transformation system is something that takes a program as the input, and outputs a different program. One example of a program transformation system is the Java compiler. The Java compiler takes a Java program as its input, and outputs a .class file with platform independent bytecode in it. Another example of a program transformation system is a Java decompiler, which takes bytecode as an input and outputs a Java program.&lt;br /&gt;
&lt;br /&gt;
In the case of the Java compiler, the program input is being output into another program in a different &amp;quot;language&amp;quot;. There are other types of transformation systems that take a program as an input and output a different program in the same language. One example of this would be ''program migration'' which migrates source code to a newer or older version of the same language. This can be useful if upgrading a software system to run on a newer version of a framework.&lt;br /&gt;
&lt;br /&gt;
==Common Uses==&lt;br /&gt;
===Pre-generate static data at compile time===&lt;br /&gt;
One common use of metaprogramming in statically typed languages is to write programs that will pre-generate tables of data for use at runtime.&amp;lt;ref name=&amp;quot;use1&amp;quot;&amp;gt;Jonathan Bartlett, &amp;quot;The art of metaprogramming, Part 1: Introduction to metaprogramming&amp;quot;, 20 Oct 2005   http://www.ibm.com/developerworks/library/l-metaprog1/index.html#N10052&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One simple but useful code generator is to build static lookup tables. Often, in order to build fast functions in C programming, we simply create a lookup table of all of the answers. This means that we either need to pre-compute them by hand (which is wasteful of your time) or build them at runtime (which is wasteful of the user's time).&lt;br /&gt;
&lt;br /&gt;
In following example we will build a generator that will take a function or set of functions on an integer and build lookup tables for the answer.&lt;br /&gt;
&lt;br /&gt;
To think of how to make such a program, we can start from the end and work backward. Firstly we need a lookup table that will return square roots of numbers between 5 and 20. A simple program can be written to generate such a table like this:&lt;br /&gt;
&lt;br /&gt;
Generate and use a lookup table of square roots&lt;br /&gt;
&lt;br /&gt;
        /* our lookup table */&lt;br /&gt;
        double square_roots[21];&lt;br /&gt;
        /* function to load the table at runtime */&lt;br /&gt;
        void init_square_roots()&lt;br /&gt;
        {&lt;br /&gt;
           int i;&lt;br /&gt;
           for(i = 5; i &amp;lt; 21; i++)&lt;br /&gt;
             {&lt;br /&gt;
               square_roots[i] = sqrt((double)i);&lt;br /&gt;
             }&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
With that single macro, we can take away a lot of work for any program that has to generate mathematical tables indexed by integer. A little extra work would also allow tables containing full struct definitions; a little more would ensure that space isn't wasted at the front of the array with useless empty entries.&lt;br /&gt;
        /* program that uses the table */&lt;br /&gt;
        int main ()&lt;br /&gt;
        {&lt;br /&gt;
           init_square_roots();&lt;br /&gt;
           printf(&amp;quot;The square root of 5 is %f\n&amp;quot;, square_roots[5]);&lt;br /&gt;
           return 0;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
Now, to convert this to a statically initialized array, you would remove the first part of the program and replace it with something like this, calculated by hand:&lt;br /&gt;
&lt;br /&gt;
Square root program with a static lookup table&lt;br /&gt;
&lt;br /&gt;
        double square_roots[] = {&lt;br /&gt;
           /* these are the ones we skipped */ 0.0, 0.0, 0.0, 0.0, 0.0&lt;br /&gt;
           2.236068, /* Square root of 5 */&lt;br /&gt;
           2.449490, /* Square root of 6 */&lt;br /&gt;
           2.645751, /* Square root of 7 */&lt;br /&gt;
           2.828427, /* Square root of 8 */&lt;br /&gt;
           3.0, /* Square root of 9 */&lt;br /&gt;
           ...&lt;br /&gt;
           4.472136 /* Square root of 20 */&lt;br /&gt;
           };&lt;br /&gt;
&lt;br /&gt;
What is needed is a program that will produce these values and print them out in a table like the previous one so they are loaded in at compile-time.&lt;br /&gt;
&lt;br /&gt;
Code generator for the table macro&lt;br /&gt;
&lt;br /&gt;
           #!/usr/bin/perl  &lt;br /&gt;
           #&lt;br /&gt;
           #tablegen.pl &lt;br /&gt;
           #&lt;br /&gt;
           ##Puts each program line into $line&lt;br /&gt;
           while(my $line = &amp;lt;&amp;gt;)&lt;br /&gt;
           {&lt;br /&gt;
             #Is this a macro invocation?&lt;br /&gt;
             if($line =~ m/TABLE:/)&lt;br /&gt;
             {&lt;br /&gt;
                #If so, split it apart into its component pieces&lt;br /&gt;
                my ($dummy, $table_name, $type, $start_idx, $end_idx, $default, $procedure) = split(m/:/, $line, 7);&lt;br /&gt;
                #The main difference between C and Perl for mathematical expressions is that&lt;br /&gt;
                #Perl prefixes its variables with a dollar sign, so we will add that here&lt;br /&gt;
                $procedure =~ s/VAL/\$VAL/g;&lt;br /&gt;
                #Print out the array declaration&lt;br /&gt;
                print &amp;quot;${type} ${table_name} [] = {\n&amp;quot;;&lt;br /&gt;
                #Go through each array element&lt;br /&gt;
                foreach my $VAL (0 .. $end_idx)&lt;br /&gt;
                {&lt;br /&gt;
                   #Only process an answer if we have reached our starting index&lt;br /&gt;
                   if($VAL &amp;gt;= $start_idx)&lt;br /&gt;
                   {&lt;br /&gt;
                      #evaluate the procedure specified (this sets $@ if there are any errors)&lt;br /&gt;
                      $result = eval $procedure;&lt;br /&gt;
                      die(&amp;quot;Error processing: $@&amp;quot;) if $@;&lt;br /&gt;
                   }&lt;br /&gt;
                   else&lt;br /&gt;
                   {&lt;br /&gt;
                      #if we haven't reached the starting index, just use the default&lt;br /&gt;
                      $result = $default;&lt;br /&gt;
                   }&lt;br /&gt;
                   #Print out the value&lt;br /&gt;
                   print &amp;quot;\t${result}&amp;quot;;&lt;br /&gt;
                   #If there are more to be processed, add a comma after the value&lt;br /&gt;
                   if($VAL != $end_idx)&lt;br /&gt;
                   {&lt;br /&gt;
                      print &amp;quot;,&amp;quot;;&lt;br /&gt;
                   }&lt;br /&gt;
                   print &amp;quot;\n&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
                #Finish the declaration&lt;br /&gt;
                print &amp;quot;};\n&amp;quot;;&lt;br /&gt;
             }&lt;br /&gt;
             else&lt;br /&gt;
             {&lt;br /&gt;
                #If this is not a macro invocation, just copy the line directly to the output&lt;br /&gt;
                print $line;&lt;br /&gt;
             }&lt;br /&gt;
           }&lt;br /&gt;
&lt;br /&gt;
To run this program, do this:&lt;br /&gt;
&lt;br /&gt;
Running the code generator&lt;br /&gt;
&lt;br /&gt;
           ./tablegen.pl &amp;lt; sqrt.in &amp;gt; sqrt.c&lt;br /&gt;
           gcc sqrt.c -o sqrt&lt;br /&gt;
           ./a.out&lt;br /&gt;
&lt;br /&gt;
===Mini-language for boiler-plate===&lt;br /&gt;
If you have a large application where many of the functions include a lot of boilerplate code, it is often a good idea to create a mini-language that allows you to work with your boilerplate code in an easier fashion. This mini-language will then be converted into your regular source code language before compiling.&amp;lt;ref name=&amp;quot;use2&amp;quot;&amp;gt;Jonathan Bartlett, &amp;quot;The art of metaprogramming, Part 2: Metaprogramming using Scheme&amp;quot;, 02 May 2006  http://www.ibm.com/developerworks/linux/library/l-metaprog2/index.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following is an example:&lt;br /&gt;
&lt;br /&gt;
Let's say that we are building a CGI application consisting of many independent CGI scripts. In most CGI applications, much of the state is stored in a database, but only a session ID is passed to each script via a cookie.&lt;br /&gt;
&lt;br /&gt;
However, in nearly every page we need to know the other standard information (such as the username, group number, the current job being worked on, whatever else information is pertinent). In addition, we need to redirect the user if they do not have an appropriate cookie.&lt;br /&gt;
&lt;br /&gt;
            (define (handle-cgi-request req)&lt;br /&gt;
              (let (&lt;br /&gt;
                     (session-id (webserver:cookie req &amp;quot;sessionid&amp;quot;)))&lt;br /&gt;
                (if (not (webserver:valid-session-id session-id))&lt;br /&gt;
                    (webserver:redirect-to-login-page)&lt;br /&gt;
                    (let (&lt;br /&gt;
                          (username (webserver:username-for-session session-id))&lt;br /&gt;
                          (group (webserver:group-for-user username))&lt;br /&gt;
                          (current-job (webserver:current-job-for-user username)))&lt;br /&gt;
                       ;;Code for processing goes here&lt;br /&gt;
                       ))))&lt;br /&gt;
&lt;br /&gt;
While some of that can be handled by a procedure, the bindings certainly cannot. However, we can turn most of it into a macro. The macro can be implemented like this:&lt;br /&gt;
&lt;br /&gt;
            (define-syntax cgi-boilerplate&lt;br /&gt;
              (lambda (x)&lt;br /&gt;
                (syntax-case x ()&lt;br /&gt;
                 (&lt;br /&gt;
                   (cgi-boilerplate expr)&lt;br /&gt;
                   (datum-&amp;gt;syntax-object&lt;br /&gt;
                     (syntax k)&lt;br /&gt;
                      (let (&lt;br /&gt;
                            (session-id (webserver:cookie req &amp;quot;sessionid&amp;quot;)))&lt;br /&gt;
                           (if (not (webserver:valid-session-id session-id))&lt;br /&gt;
                               (webserver:redirect-to-login-page)&lt;br /&gt;
                               (let (&lt;br /&gt;
                                      (username (webserver:username-for-session session-id))&lt;br /&gt;
                                      (group (webserver:group-for-user username))&lt;br /&gt;
                                      (current-job (webserver:current-job-for-user username)))&lt;br /&gt;
                                      (syntax-object-&amp;gt;datum (syntax expr))))))&lt;br /&gt;
                  )&lt;br /&gt;
             )))&lt;br /&gt;
&lt;br /&gt;
We can now create new forms based on our boilerplate code by doing the following:&lt;br /&gt;
&lt;br /&gt;
            (define (handle-cgi-request req)&lt;br /&gt;
              (cgi-boilerplate&lt;br /&gt;
               (begin&lt;br /&gt;
                 ;;Do whatever I want here&lt;br /&gt;
                 )))&lt;br /&gt;
&lt;br /&gt;
In addition, since we are not defining our variables explicitly, adding new variable definitions to our boilerplate won't affect its calling conventions, so new features can be added without having to create a whole new function.&lt;br /&gt;
&lt;br /&gt;
In any large project, there are inevitably templates to follow which cannot be reduced to functions, usually because of the bindings being created. Using boilerplate macros can make maintenance of such templated code much easier.&lt;br /&gt;
&lt;br /&gt;
Likewise, other standard macros can be created which make use of variables defined in the boilerplate. Using macros like this significantly reduces typing because you do not have to constantly be writing and rewriting variable bindings, derivations, and parameter passing. This also reduces the potential for errors in such code.&lt;br /&gt;
&lt;br /&gt;
Realize though that boilerplate macros are not a panacea. There are many significant problems that can occur, including:&lt;br /&gt;
*  Accidentally overwriting bindings by introducing a variable name that was previously defined in a macro.&lt;br /&gt;
*  Difficulty tracing problems because the inputs and the outputs of the macros are implicit, not explicit.&lt;br /&gt;
&lt;br /&gt;
These can be largely avoided by doing a few things in conjunction with your boilerplate macros:&lt;br /&gt;
*  Have a naming convention which clearly labels macros as such, as well as indicate that a variable came from boilerplate code. This could be done by affixing -m to macros and -b to variables defined within a boilerplate.&lt;br /&gt;
*  Carefully document all boilerplate macros, especially the introduced variable bindings and all changes between versions.&lt;br /&gt;
*  Only use boilerplate macros when the savings in repetitiveness clearly outweigh the negatives of implicit functionality.&lt;br /&gt;
&lt;br /&gt;
===Abbreviate statements and prevent mistakes===&lt;br /&gt;
A lot of programming languages make you write really verbose statements to do really simple things. Code-generating programs allow you to abbreviate such statements and save a lot of typing, which also prevents a lot of mistakes because there is less chance of mistyping.&lt;br /&gt;
&lt;br /&gt;
=Metaprogramming Framework in Java=&lt;br /&gt;
==Reflection==&lt;br /&gt;
===Introduction to reflection===&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_(computer_programming) Reflection] is the ability to introspect metalevel information about the program structure itself at runtime. Usually this metalevel information is modeled using the general abstraction mechanisms available in the language. In Java, reflection enables to discover information about the loaded classes:&lt;br /&gt;
*  Fields,&lt;br /&gt;
*  Methods, and&lt;br /&gt;
*  Constructors&lt;br /&gt;
*  Generics information&lt;br /&gt;
*  Metadata annotations&lt;br /&gt;
It also enables to use these metaobjects to their instances in runtime environment.&amp;lt;ref name=&amp;quot;meta_java&amp;quot;&amp;gt;Abdelmonaim Remani, &amp;quot;The Art of Metaprogramming in Java&amp;quot;, Jul 19, 2012  http://www.slideshare.net/PolymathicCoder/the-art-of-metaprogramming-in-java&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following is the Java metalevel architecture&amp;lt;ref name=&amp;quot;reflection&amp;quot;&amp;gt;Mika Haapakorpi, &amp;quot;Meta Programming In Java&amp;quot;&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Metalevel architecture.PNG]]&lt;br /&gt;
&lt;br /&gt;
===Dynamic proxy classes===&lt;br /&gt;
A dynamic proxy class implements a list of interfaces specified at runtime when the class is created.&lt;br /&gt;
*  A proxy interface is an interface that is implemented by a proxy class.&lt;br /&gt;
*  A proxy instance is an instance of a proxy class which has an associated invocation handler object.&lt;br /&gt;
The following is an example of dynamic proxy classes&lt;br /&gt;
&lt;br /&gt;
[[File:Dynamic_proxy_class.PNG]]&lt;br /&gt;
&lt;br /&gt;
==Generics==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Generics_in_Java Generics] are a facility of generic programming that was added to the Java programming language in 2004 as part of J2SE 5.0. They allow &amp;quot;a type or method to operate on objects of various types while providing compile-time type safety.&amp;quot;  The generics can be used in classes, interfaces, methods and constructors.&amp;lt;ref name=&amp;quot;reflection&amp;quot;&amp;gt;Mika Haapakorpi, &amp;quot;Meta Programming In Java&amp;quot;&amp;lt;/ref&amp;gt;&lt;br /&gt;
===Two new types in generics===&lt;br /&gt;
Two new types in generics:&lt;br /&gt;
*  Parametrized types&lt;br /&gt;
*  Type variables&lt;br /&gt;
&lt;br /&gt;
A type variable is an unqualified identifier. Class and interface declarations can have type arguments (type variables). Method and constructors definitions can have type arguments (type variables). &lt;br /&gt;
&lt;br /&gt;
The following is an example for type variables&lt;br /&gt;
&lt;br /&gt;
       List&amp;lt;String&amp;gt; anExample = new ArrayList&amp;lt;String&amp;gt;()&lt;br /&gt;
&lt;br /&gt;
*  List : interface&lt;br /&gt;
*  ArrayList : class&lt;br /&gt;
*  String : class (the actual type argument)&lt;br /&gt;
*  List&amp;lt;String&amp;gt; and ArrayList&amp;lt;String&amp;gt; : parametrized type&lt;br /&gt;
&lt;br /&gt;
===Class/Interface Declarations===&lt;br /&gt;
The following is an example of class declaration:&lt;br /&gt;
&lt;br /&gt;
       public interface List&amp;lt;E&amp;gt; {&lt;br /&gt;
         void add(Ex);&lt;br /&gt;
         Iterator&amp;lt;E&amp;gt; iterator();&lt;br /&gt;
       }&lt;br /&gt;
       public interface Iterator&amp;lt;E&amp;gt; {&lt;br /&gt;
         E next();&lt;br /&gt;
         booleanhasNext();&lt;br /&gt;
       }&lt;br /&gt;
       List&amp;lt;String&amp;gt; anExample;&lt;br /&gt;
       anExample.add(”sdfdfss”);&lt;br /&gt;
       anExample.add(new Object()); // compile time error&lt;br /&gt;
       String aTest = anExample.iterator().next();&lt;br /&gt;
&lt;br /&gt;
==Metadata annotation==&lt;br /&gt;
An annotation, in the Java computer programming language, is a form of syntactic metadata that can be added to Java source code. Classes, methods, variables, parameters and packages may be annotated.&amp;lt;ref name=&amp;quot;reflection&amp;quot;&amp;gt;Mika Haapakorpi, &amp;quot;Meta Programming In Java&amp;quot;&amp;lt;/ref&amp;gt; &lt;br /&gt;
&lt;br /&gt;
The following is an example of annotation:&lt;br /&gt;
&lt;br /&gt;
        /**&lt;br /&gt;
        * Designates a formatter to pretty-print the annotated class.&lt;br /&gt;
        */&lt;br /&gt;
        public @interface PrettyPrinter{&lt;br /&gt;
        Class&amp;lt;? extends Formatter&amp;gt; value();&lt;br /&gt;
        }&lt;br /&gt;
        // Single-member annotation with Class&lt;br /&gt;
        // member restricted by bounded wildcard&lt;br /&gt;
        // The annotation presumes the existence of this class.&lt;br /&gt;
        class GorgeousFormatterimplements Formatter { ... }&lt;br /&gt;
        @PrettyPrinter(GorgeousFormatter.class)&lt;br /&gt;
        public class Petunia { ... }&lt;br /&gt;
&lt;br /&gt;
‘@’ indicates the start of an annotation definition.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:5. K. Czarnecki, &amp;quot;Generative Programming&amp;quot; chapter 8, Static Metaprogramming in C++&lt;br /&gt;
&lt;br /&gt;
6. http://www.program-transformation.org&lt;br /&gt;
&lt;br /&gt;
7. http://www.codeproject.com/Articles/15429/Ruby-Add-class-methods-at-runtime&lt;br /&gt;
&lt;br /&gt;
8. http://docs.oracle.com/javase/tutorial/reflect/index.html&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Cyang14</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1_1h_jc&amp;diff=73622</id>
		<title>CSC/ECE 517 Spring 2013/ch1 1h jc</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1_1h_jc&amp;diff=73622"/>
		<updated>2013-02-24T16:52:37Z</updated>

		<summary type="html">&lt;p&gt;Cyang14: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Metaprogramming in statically typed languages&lt;br /&gt;
=Metaprogramming in statically typed languages=&lt;br /&gt;
==Introduction==&lt;br /&gt;
===What is metaprogramming===&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming] refers to writing programs that can manipulate other programs or themselves. The program that is doing the manipulating is called the ''metaprogram'', and the program that is being manipulated is called the ''object program''. The language of the ''metaprogram'' is referred to as the ''metalanguage'' and the language of the object program is called the ''object language''. A language has the ability of [http://en.wikipedia.org/wiki/Reflection_(computer_programming) reflection] if it can be used to write a metaprogram that can manipulate a program written in the same language.&lt;br /&gt;
&lt;br /&gt;
===Metaprogramming in statically typed languages===&lt;br /&gt;
&lt;br /&gt;
Metaprogramming can be accomplished in both statically and dynamically typed languages, however [http://en.wikipedia.org/wiki/Dynamically_typed_language#Dynamic_typing dynamically typed languages] have some advantages over [http://en.wikipedia.org/wiki/Dynamically_typed_language#Dynamic_typing statically typed languages] when it comes to metaprogramming.&lt;br /&gt;
&lt;br /&gt;
One example of a metaprogramming task is to add a method to a particular class at runtime. Sample code in Ruby to perform this task is outlined below. First is a definition of a Ruby class called TestAddMethodAtRuntime:&lt;br /&gt;
&lt;br /&gt;
  class TestAddMethodAtRuntime&lt;br /&gt;
    def originalMethod&lt;br /&gt;
        puts &amp;quot;Original Method&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The code below created a new instance of the class. After that, it defines a new method called newlyAddedMethod. Then the original method is called, follow by the newly added method at runtime:&lt;br /&gt;
&lt;br /&gt;
  t = TestAddMethodAtRuntime.new&lt;br /&gt;
  def t.newlyAddedMethod&lt;br /&gt;
    puts &amp;quot;Newly Added Method&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  t.originalMethod&lt;br /&gt;
  puts &amp;quot;\n&amp;quot;&lt;br /&gt;
  t.newlyAddedMethod&lt;br /&gt;
&lt;br /&gt;
Below is the output of the code above:&lt;br /&gt;
&lt;br /&gt;
  Original Method&lt;br /&gt;
  Newly Added Method&lt;br /&gt;
&lt;br /&gt;
Accomplishing something like this in a statically typed language would be much more difficult or impossible depending on the language. It is for this reason that dynamically typed languages are usually viewed as being better equipped to handle metaprogramming overall.&lt;br /&gt;
&lt;br /&gt;
==Implementation==&lt;br /&gt;
===Programming Language API===&lt;br /&gt;
&lt;br /&gt;
One way that metaprogramming can be possible in a language is through an [http://en.wikipedia.org/wiki/API API] that helps programmers achieve a metaprogramming task. An example of this is a language that provides an API for reflection. One type of reflection is ''introspection'' in which the program can access the source code of the program itself.&lt;br /&gt;
&lt;br /&gt;
Below is an example from the Java API to illustrate how an API can be used to achieve some metaprogramming tasks:&lt;br /&gt;
&lt;br /&gt;
   import java.lang.reflect.*; &lt;br /&gt;
   public class DumpMethods {&lt;br /&gt;
   public static void main(String args[]) {&lt;br /&gt;
     	try {&lt;br /&gt;
           Class c = Class.forName(args[0]);&lt;br /&gt;
           Method m[] = c.getDeclaredMethods();&lt;br /&gt;
           for (int i = 0; i &amp;lt; m.length; i++) {&lt;br /&gt;
              System.out.println(m[i].toString());&lt;br /&gt;
           }&lt;br /&gt;
     	}&lt;br /&gt;
     	catch (Throwable e) {&lt;br /&gt;
        	System.err.println(e);&lt;br /&gt;
     	}&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
The above example prints out all of the methods that are declared for a particular class. This is an example of metaprogramming since the program is accessing the source code of itself.&lt;br /&gt;
&lt;br /&gt;
===Program transformation system===&lt;br /&gt;
&lt;br /&gt;
A program transformation system is something that takes a program as the input, and outputs a different program. One example of a program transformation system is the Java compiler. The Java compiler takes a Java program as its input, and outputs a .class file with platform independent bytecode in it. Another example of a program transformation system is a Java decompiler, which takes bytecode as an input and outputs a Java program.&lt;br /&gt;
&lt;br /&gt;
In the case of the Java compiler, the program input is being output into another program in a different &amp;quot;language&amp;quot;. There are other types of transformation systems that take a program as an input and output a different program in the same language. One example of this would be ''program migration'' which migrates source code to a newer or older version of the same language. This can be useful if upgrading a software system to run on a newer version of a framework.&lt;br /&gt;
&lt;br /&gt;
==Common Uses==&lt;br /&gt;
===Pre-generate static data at compile time===&lt;br /&gt;
One common use of metaprogramming in statically typed languages is to write programs that will pre-generate tables of data for use at runtime.&amp;lt;ref name=&amp;quot;use1&amp;quot;&amp;gt;Jonathan Bartlett, &amp;quot;The art of metaprogramming, Part 1: Introduction to metaprogramming&amp;quot;, 20 Oct 2005   http://www.ibm.com/developerworks/library/l-metaprog1/index.html#N10052&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One simple but useful code generator is to build static lookup tables. Often, in order to build fast functions in C programming, we simply create a lookup table of all of the answers. This means that we either need to pre-compute them by hand (which is wasteful of your time) or build them at runtime (which is wasteful of the user's time).&lt;br /&gt;
&lt;br /&gt;
In following example we will build a generator that will take a function or set of functions on an integer and build lookup tables for the answer.&lt;br /&gt;
&lt;br /&gt;
To think of how to make such a program, we can start from the end and work backward. Firstly we need a lookup table that will return square roots of numbers between 5 and 20. A simple program can be written to generate such a table like this:&lt;br /&gt;
&lt;br /&gt;
Generate and use a lookup table of square roots&lt;br /&gt;
&lt;br /&gt;
        /* our lookup table */&lt;br /&gt;
        double square_roots[21];&lt;br /&gt;
        /* function to load the table at runtime */&lt;br /&gt;
        void init_square_roots()&lt;br /&gt;
        {&lt;br /&gt;
           int i;&lt;br /&gt;
           for(i = 5; i &amp;lt; 21; i++)&lt;br /&gt;
             {&lt;br /&gt;
               square_roots[i] = sqrt((double)i);&lt;br /&gt;
             }&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
With that single macro, we can take away a lot of work for any program that has to generate mathematical tables indexed by integer. A little extra work would also allow tables containing full struct definitions; a little more would ensure that space isn't wasted at the front of the array with useless empty entries.&lt;br /&gt;
        /* program that uses the table */&lt;br /&gt;
        int main ()&lt;br /&gt;
        {&lt;br /&gt;
           init_square_roots();&lt;br /&gt;
           printf(&amp;quot;The square root of 5 is %f\n&amp;quot;, square_roots[5]);&lt;br /&gt;
           return 0;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
Now, to convert this to a statically initialized array, you would remove the first part of the program and replace it with something like this, calculated by hand:&lt;br /&gt;
&lt;br /&gt;
Square root program with a static lookup table&lt;br /&gt;
&lt;br /&gt;
        double square_roots[] = {&lt;br /&gt;
           /* these are the ones we skipped */ 0.0, 0.0, 0.0, 0.0, 0.0&lt;br /&gt;
           2.236068, /* Square root of 5 */&lt;br /&gt;
           2.449490, /* Square root of 6 */&lt;br /&gt;
           2.645751, /* Square root of 7 */&lt;br /&gt;
           2.828427, /* Square root of 8 */&lt;br /&gt;
           3.0, /* Square root of 9 */&lt;br /&gt;
           ...&lt;br /&gt;
           4.472136 /* Square root of 20 */&lt;br /&gt;
           };&lt;br /&gt;
&lt;br /&gt;
What is needed is a program that will produce these values and print them out in a table like the previous one so they are loaded in at compile-time.&lt;br /&gt;
&lt;br /&gt;
Code generator for the table macro&lt;br /&gt;
&lt;br /&gt;
           #!/usr/bin/perl  &lt;br /&gt;
           #&lt;br /&gt;
           #tablegen.pl &lt;br /&gt;
           #&lt;br /&gt;
           ##Puts each program line into $line&lt;br /&gt;
           while(my $line = &amp;lt;&amp;gt;)&lt;br /&gt;
           {&lt;br /&gt;
             #Is this a macro invocation?&lt;br /&gt;
             if($line =~ m/TABLE:/)&lt;br /&gt;
             {&lt;br /&gt;
                #If so, split it apart into its component pieces&lt;br /&gt;
                my ($dummy, $table_name, $type, $start_idx, $end_idx, $default, $procedure) = split(m/:/, $line, 7);&lt;br /&gt;
                #The main difference between C and Perl for mathematical expressions is that&lt;br /&gt;
                #Perl prefixes its variables with a dollar sign, so we will add that here&lt;br /&gt;
                $procedure =~ s/VAL/\$VAL/g;&lt;br /&gt;
                #Print out the array declaration&lt;br /&gt;
                print &amp;quot;${type} ${table_name} [] = {\n&amp;quot;;&lt;br /&gt;
                #Go through each array element&lt;br /&gt;
                foreach my $VAL (0 .. $end_idx)&lt;br /&gt;
                {&lt;br /&gt;
                   #Only process an answer if we have reached our starting index&lt;br /&gt;
                   if($VAL &amp;gt;= $start_idx)&lt;br /&gt;
                   {&lt;br /&gt;
                      #evaluate the procedure specified (this sets $@ if there are any errors)&lt;br /&gt;
                      $result = eval $procedure;&lt;br /&gt;
                      die(&amp;quot;Error processing: $@&amp;quot;) if $@;&lt;br /&gt;
                   }&lt;br /&gt;
                   else&lt;br /&gt;
                   {&lt;br /&gt;
                      #if we haven't reached the starting index, just use the default&lt;br /&gt;
                      $result = $default;&lt;br /&gt;
                   }&lt;br /&gt;
                   #Print out the value&lt;br /&gt;
                   print &amp;quot;\t${result}&amp;quot;;&lt;br /&gt;
                   #If there are more to be processed, add a comma after the value&lt;br /&gt;
                   if($VAL != $end_idx)&lt;br /&gt;
                   {&lt;br /&gt;
                      print &amp;quot;,&amp;quot;;&lt;br /&gt;
                   }&lt;br /&gt;
                   print &amp;quot;\n&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
                #Finish the declaration&lt;br /&gt;
                print &amp;quot;};\n&amp;quot;;&lt;br /&gt;
             }&lt;br /&gt;
             else&lt;br /&gt;
             {&lt;br /&gt;
                #If this is not a macro invocation, just copy the line directly to the output&lt;br /&gt;
                print $line;&lt;br /&gt;
             }&lt;br /&gt;
           }&lt;br /&gt;
&lt;br /&gt;
To run this program, do this:&lt;br /&gt;
&lt;br /&gt;
Running the code generator&lt;br /&gt;
&lt;br /&gt;
           ./tablegen.pl &amp;lt; sqrt.in &amp;gt; sqrt.c&lt;br /&gt;
           gcc sqrt.c -o sqrt&lt;br /&gt;
           ./a.out&lt;br /&gt;
&lt;br /&gt;
===Mini-language for boiler-plate===&lt;br /&gt;
If you have a large application where many of the functions include a lot of boilerplate code, it is often a good idea to create a mini-language that allows you to work with your boilerplate code in an easier fashion. This mini-language will then be converted into your regular source code language before compiling.&amp;lt;ref name=&amp;quot;use2&amp;quot;&amp;gt;Jonathan Bartlett, &amp;quot;The art of metaprogramming, Part 2: Metaprogramming using Scheme&amp;quot;, 02 May 2006  http://www.ibm.com/developerworks/linux/library/l-metaprog2/index.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following is an example:&lt;br /&gt;
&lt;br /&gt;
Let's say that we are building a CGI application consisting of many independent CGI scripts. In most CGI applications, much of the state is stored in a database, but only a session ID is passed to each script via a cookie.&lt;br /&gt;
&lt;br /&gt;
However, in nearly every page we need to know the other standard information (such as the username, group number, the current job being worked on, whatever else information is pertinent). In addition, we need to redirect the user if they do not have an appropriate cookie.&lt;br /&gt;
&lt;br /&gt;
            (define (handle-cgi-request req)&lt;br /&gt;
              (let (&lt;br /&gt;
                     (session-id (webserver:cookie req &amp;quot;sessionid&amp;quot;)))&lt;br /&gt;
                (if (not (webserver:valid-session-id session-id))&lt;br /&gt;
                    (webserver:redirect-to-login-page)&lt;br /&gt;
                    (let (&lt;br /&gt;
                          (username (webserver:username-for-session session-id))&lt;br /&gt;
                          (group (webserver:group-for-user username))&lt;br /&gt;
                          (current-job (webserver:current-job-for-user username)))&lt;br /&gt;
                       ;;Code for processing goes here&lt;br /&gt;
                       ))))&lt;br /&gt;
&lt;br /&gt;
While some of that can be handled by a procedure, the bindings certainly cannot. However, we can turn most of it into a macro. The macro can be implemented like this:&lt;br /&gt;
&lt;br /&gt;
            (define-syntax cgi-boilerplate&lt;br /&gt;
              (lambda (x)&lt;br /&gt;
                (syntax-case x ()&lt;br /&gt;
                 (&lt;br /&gt;
                   (cgi-boilerplate expr)&lt;br /&gt;
                   (datum-&amp;gt;syntax-object&lt;br /&gt;
                     (syntax k)&lt;br /&gt;
                      (let (&lt;br /&gt;
                            (session-id (webserver:cookie req &amp;quot;sessionid&amp;quot;)))&lt;br /&gt;
                           (if (not (webserver:valid-session-id session-id))&lt;br /&gt;
                               (webserver:redirect-to-login-page)&lt;br /&gt;
                               (let (&lt;br /&gt;
                                      (username (webserver:username-for-session session-id))&lt;br /&gt;
                                      (group (webserver:group-for-user username))&lt;br /&gt;
                                      (current-job (webserver:current-job-for-user username)))&lt;br /&gt;
                                      (syntax-object-&amp;gt;datum (syntax expr))))))&lt;br /&gt;
                  )&lt;br /&gt;
             )))&lt;br /&gt;
&lt;br /&gt;
We can now create new forms based on our boilerplate code by doing the following:&lt;br /&gt;
&lt;br /&gt;
            (define (handle-cgi-request req)&lt;br /&gt;
              (cgi-boilerplate&lt;br /&gt;
               (begin&lt;br /&gt;
                 ;;Do whatever I want here&lt;br /&gt;
                 )))&lt;br /&gt;
&lt;br /&gt;
In addition, since we are not defining our variables explicitly, adding new variable definitions to our boilerplate won't affect its calling conventions, so new features can be added without having to create a whole new function.&lt;br /&gt;
&lt;br /&gt;
In any large project, there are inevitably templates to follow which cannot be reduced to functions, usually because of the bindings being created. Using boilerplate macros can make maintenance of such templated code much easier.&lt;br /&gt;
&lt;br /&gt;
Likewise, other standard macros can be created which make use of variables defined in the boilerplate. Using macros like this significantly reduces typing because you do not have to constantly be writing and rewriting variable bindings, derivations, and parameter passing. This also reduces the potential for errors in such code.&lt;br /&gt;
&lt;br /&gt;
Realize though that boilerplate macros are not a panacea. There are many significant problems that can occur, including:&lt;br /&gt;
*  Accidentally overwriting bindings by introducing a variable name that was previously defined in a macro.&lt;br /&gt;
*  Difficulty tracing problems because the inputs and the outputs of the macros are implicit, not explicit.&lt;br /&gt;
&lt;br /&gt;
These can be largely avoided by doing a few things in conjunction with your boilerplate macros:&lt;br /&gt;
*  Have a naming convention which clearly labels macros as such, as well as indicate that a variable came from boilerplate code. This could be done by affixing -m to macros and -b to variables defined within a boilerplate.&lt;br /&gt;
*  Carefully document all boilerplate macros, especially the introduced variable bindings and all changes between versions.&lt;br /&gt;
*  Only use boilerplate macros when the savings in repetitiveness clearly outweigh the negatives of implicit functionality.&lt;br /&gt;
&lt;br /&gt;
===Abbreviate statements and prevent mistakes===&lt;br /&gt;
A lot of programming languages make you write really verbose statements to do really simple things. Code-generating programs allow you to abbreviate such statements and save a lot of typing, which also prevents a lot of mistakes because there is less chance of mistyping.&lt;br /&gt;
&lt;br /&gt;
=Metaprogramming Framework in Java=&lt;br /&gt;
==Reflection==&lt;br /&gt;
===Introduction to reflection===&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_(computer_programming) Reflection] is the ability to introspect metalevel information about the program structure itself at runtime. Usually this metalevel information is modeled using the general abstraction mechanisms available in the language. In Java, reflection enables to discover information about the loaded classes:&lt;br /&gt;
*  Fields,&lt;br /&gt;
*  Methods, and&lt;br /&gt;
*  Constructors&lt;br /&gt;
*  Generics information&lt;br /&gt;
*  Metadata annotations&lt;br /&gt;
It also enables to use these metaobjects to their instances in runtime environment.&amp;lt;ref name=&amp;quot;meta_java&amp;quot;&amp;gt;Abdelmonaim Remani, &amp;quot;The Art of Metaprogramming in Java&amp;quot;, Jul 19, 2012  http://www.slideshare.net/PolymathicCoder/the-art-of-metaprogramming-in-java&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following is the Java metalevel architecture&amp;lt;ref name=&amp;quot;reflection&amp;quot;&amp;gt;Mika Haapakorpi, &amp;quot;Meta Programming In Java&amp;quot;&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Metalevel architecture.PNG]]&lt;br /&gt;
&lt;br /&gt;
===Dynamic proxy classes===&lt;br /&gt;
A dynamic proxy class implements a list of interfaces specified at runtime when the class is created.&lt;br /&gt;
*  A proxy interface is an interface that is implemented by a proxy class.&lt;br /&gt;
*  A proxy instance is an instance of a proxy class which has an associated invocation handler object.&lt;br /&gt;
The following is an example of dynamic proxy classes&lt;br /&gt;
&lt;br /&gt;
[[File:Dynamic_proxy_class.PNG]]&lt;br /&gt;
&lt;br /&gt;
==Generics==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Generics_in_Java Generics] are a facility of generic programming that was added to the Java programming language in 2004 as part of J2SE 5.0. They allow &amp;quot;a type or method to operate on objects of various types while providing compile-time type safety.&amp;quot;  The generics can be used in classes, interfaces, methods and constructors.&amp;lt;ref name=&amp;quot;reflection&amp;quot;&amp;gt;Mika Haapakorpi, &amp;quot;Meta Programming In Java&amp;quot;&amp;lt;/ref&amp;gt;&lt;br /&gt;
===Two new types in generics===&lt;br /&gt;
Two new types in generics:&lt;br /&gt;
*  Parametrized types&lt;br /&gt;
*  Type variables&lt;br /&gt;
&lt;br /&gt;
A type variable is an unqualified identifier. Class and interface declarations can have type arguments (type variables). Method and constructors definitions can have type arguments (type variables). &lt;br /&gt;
&lt;br /&gt;
The following is an example for type variables&lt;br /&gt;
&lt;br /&gt;
       List&amp;lt;String&amp;gt; anExample = new ArrayList&amp;lt;String&amp;gt;()&lt;br /&gt;
&lt;br /&gt;
*  List : interface&lt;br /&gt;
*  ArrayList : class&lt;br /&gt;
*  String : class (the actual type argument)&lt;br /&gt;
*  List&amp;lt;String&amp;gt; and ArrayList&amp;lt;String&amp;gt; : parametrized type&lt;br /&gt;
&lt;br /&gt;
===Class/Interface Declarations===&lt;br /&gt;
The following is an example of class declaration:&lt;br /&gt;
&lt;br /&gt;
       public interface List&amp;lt;E&amp;gt; {&lt;br /&gt;
         void add(Ex);&lt;br /&gt;
         Iterator&amp;lt;E&amp;gt; iterator();&lt;br /&gt;
       }&lt;br /&gt;
       public interface Iterator&amp;lt;E&amp;gt; {&lt;br /&gt;
         E next();&lt;br /&gt;
         booleanhasNext();&lt;br /&gt;
       }&lt;br /&gt;
       List&amp;lt;String&amp;gt; anExample;&lt;br /&gt;
       anExample.add(”sdfdfss”);&lt;br /&gt;
       anExample.add(new Object()); // compile time error&lt;br /&gt;
       String aTest = anExample.iterator().next();&lt;br /&gt;
&lt;br /&gt;
==Metadata annotation==&lt;br /&gt;
An annotation, in the Java computer programming language, is a form of syntactic metadata that can be added to Java source code. Classes, methods, variables, parameters and packages may be annotated.&amp;lt;ref name=&amp;quot;reflection&amp;quot;&amp;gt;Mika Haapakorpi, &amp;quot;Meta Programming In Java&amp;quot;&amp;lt;/ref&amp;gt; &lt;br /&gt;
&lt;br /&gt;
The following is an example of annotation:&lt;br /&gt;
&lt;br /&gt;
        /**&lt;br /&gt;
        * Designates a formatter to pretty-print the annotated class.&lt;br /&gt;
        */&lt;br /&gt;
        public @interface PrettyPrinter{&lt;br /&gt;
        Class&amp;lt;? extends Formatter&amp;gt; value();&lt;br /&gt;
        }&lt;br /&gt;
        // Single-member annotation with Class&lt;br /&gt;
        // member restricted by bounded wildcard&lt;br /&gt;
        // The annotation presumes the existence of this class.&lt;br /&gt;
        class GorgeousFormatterimplements Formatter { ... }&lt;br /&gt;
        @PrettyPrinter(GorgeousFormatter.class)&lt;br /&gt;
        public class Petunia { ... }&lt;br /&gt;
&lt;br /&gt;
‘@’ indicates the start of an annotation definition.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
5. K. Czarnecki, &amp;quot;Generative Programming&amp;quot; chapter 8, Static Metaprogramming in C++&lt;br /&gt;
&lt;br /&gt;
6. http://www.program-transformation.org&lt;br /&gt;
&lt;br /&gt;
7. http://www.codeproject.com/Articles/15429/Ruby-Add-class-methods-at-runtime&lt;br /&gt;
&lt;br /&gt;
8. http://docs.oracle.com/javase/tutorial/reflect/index.html&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Cyang14</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1_1h_jc&amp;diff=73621</id>
		<title>CSC/ECE 517 Spring 2013/ch1 1h jc</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1_1h_jc&amp;diff=73621"/>
		<updated>2013-02-24T16:52:10Z</updated>

		<summary type="html">&lt;p&gt;Cyang14: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Metaprogramming in statically typed languages&lt;br /&gt;
=Metaprogramming in statically typed languages=&lt;br /&gt;
==Introduction==&lt;br /&gt;
===What is metaprogramming===&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming] refers to writing programs that can manipulate other programs or themselves. The program that is doing the manipulating is called the ''metaprogram'', and the program that is being manipulated is called the ''object program''. The language of the ''metaprogram'' is referred to as the ''metalanguage'' and the language of the object program is called the ''object language''. A language has the ability of [http://en.wikipedia.org/wiki/Reflection_(computer_programming) reflection] if it can be used to write a metaprogram that can manipulate a program written in the same language.&lt;br /&gt;
&lt;br /&gt;
===Metaprogramming in statically typed languages===&lt;br /&gt;
&lt;br /&gt;
Metaprogramming can be accomplished in both statically and dynamically typed languages, however [http://en.wikipedia.org/wiki/Dynamically_typed_language#Dynamic_typing dynamically typed languages] have some advantages over [http://en.wikipedia.org/wiki/Dynamically_typed_language#Dynamic_typing statically typed languages] when it comes to metaprogramming.&lt;br /&gt;
&lt;br /&gt;
One example of a metaprogramming task is to add a method to a particular class at runtime. Sample code in Ruby to perform this task is outlined below. First is a definition of a Ruby class called TestAddMethodAtRuntime:&lt;br /&gt;
&lt;br /&gt;
  class TestAddMethodAtRuntime&lt;br /&gt;
    def originalMethod&lt;br /&gt;
        puts &amp;quot;Original Method&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The code below created a new instance of the class. After that, it defines a new method called newlyAddedMethod. Then the original method is called, follow by the newly added method at runtime:&lt;br /&gt;
&lt;br /&gt;
  t = TestAddMethodAtRuntime.new&lt;br /&gt;
  def t.newlyAddedMethod&lt;br /&gt;
    puts &amp;quot;Newly Added Method&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  t.originalMethod&lt;br /&gt;
  puts &amp;quot;\n&amp;quot;&lt;br /&gt;
  t.newlyAddedMethod&lt;br /&gt;
&lt;br /&gt;
Below is the output of the code above:&lt;br /&gt;
&lt;br /&gt;
  Original Method&lt;br /&gt;
  Newly Added Method&lt;br /&gt;
&lt;br /&gt;
Accomplishing something like this in a statically typed language would be much more difficult or impossible depending on the language. It is for this reason that dynamically typed languages are usually viewed as being better equipped to handle metaprogramming overall.&lt;br /&gt;
&lt;br /&gt;
==Implementation==&lt;br /&gt;
===Programming Language API===&lt;br /&gt;
&lt;br /&gt;
One way that metaprogramming can be possible in a language is through an [http://en.wikipedia.org/wiki/API API] that helps programmers achieve a metaprogramming task. An example of this is a language that provides an API for reflection. One type of reflection is ''introspection'' in which the program can access the source code of the program itself.&lt;br /&gt;
&lt;br /&gt;
Below is an example from the Java API to illustrate how an API can be used to achieve some metaprogramming tasks:&lt;br /&gt;
&lt;br /&gt;
   import java.lang.reflect.*; &lt;br /&gt;
   public class DumpMethods {&lt;br /&gt;
   public static void main(String args[]) {&lt;br /&gt;
     	try {&lt;br /&gt;
           Class c = Class.forName(args[0]);&lt;br /&gt;
           Method m[] = c.getDeclaredMethods();&lt;br /&gt;
           for (int i = 0; i &amp;lt; m.length; i++) {&lt;br /&gt;
              System.out.println(m[i].toString());&lt;br /&gt;
           }&lt;br /&gt;
     	}&lt;br /&gt;
     	catch (Throwable e) {&lt;br /&gt;
        	System.err.println(e);&lt;br /&gt;
     	}&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
The above example prints out all of the methods that are declared for a particular class. This is an example of metaprogramming since the program is accessing the source code of itself.&lt;br /&gt;
&lt;br /&gt;
===Program transformation system===&lt;br /&gt;
&lt;br /&gt;
A program transformation system is something that takes a program as the input, and outputs a different program. One example of a program transformation system is the Java compiler. The Java compiler takes a Java program as its input, and outputs a .class file with platform independent bytecode in it. Another example of a program transformation system is a Java decompiler, which takes bytecode as an input and outputs a Java program.&lt;br /&gt;
&lt;br /&gt;
In the case of the Java compiler, the program input is being output into another program in a different &amp;quot;language&amp;quot;. There are other types of transformation systems that take a program as an input and output a different program in the same language. One example of this would be ''program migration'' which migrates source code to a newer or older version of the same language. This can be useful if upgrading a software system to run on a newer version of a framework.&lt;br /&gt;
&lt;br /&gt;
==Common Uses==&lt;br /&gt;
===Pre-generate static data at compile time===&lt;br /&gt;
One common use of metaprogramming in statically typed languages is to write programs that will pre-generate tables of data for use at runtime.&amp;lt;ref name=&amp;quot;use1&amp;quot;&amp;gt;Jonathan Bartlett, &amp;quot;The art of metaprogramming, Part 1: Introduction to metaprogramming&amp;quot;, 20 Oct 2005   http://www.ibm.com/developerworks/library/l-metaprog1/index.html#N10052&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One simple but useful code generator is to build static lookup tables. Often, in order to build fast functions in C programming, we simply create a lookup table of all of the answers. This means that we either need to pre-compute them by hand (which is wasteful of your time) or build them at runtime (which is wasteful of the user's time).&lt;br /&gt;
&lt;br /&gt;
In following example we will build a generator that will take a function or set of functions on an integer and build lookup tables for the answer.&lt;br /&gt;
&lt;br /&gt;
To think of how to make such a program, we can start from the end and work backward. Firstly we need a lookup table that will return square roots of numbers between 5 and 20. A simple program can be written to generate such a table like this:&lt;br /&gt;
&lt;br /&gt;
Generate and use a lookup table of square roots&lt;br /&gt;
&lt;br /&gt;
        /* our lookup table */&lt;br /&gt;
        double square_roots[21];&lt;br /&gt;
        /* function to load the table at runtime */&lt;br /&gt;
        void init_square_roots()&lt;br /&gt;
        {&lt;br /&gt;
           int i;&lt;br /&gt;
           for(i = 5; i &amp;lt; 21; i++)&lt;br /&gt;
             {&lt;br /&gt;
               square_roots[i] = sqrt((double)i);&lt;br /&gt;
             }&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
With that single macro, we can take away a lot of work for any program that has to generate mathematical tables indexed by integer. A little extra work would also allow tables containing full struct definitions; a little more would ensure that space isn't wasted at the front of the array with useless empty entries.&lt;br /&gt;
        /* program that uses the table */&lt;br /&gt;
        int main ()&lt;br /&gt;
        {&lt;br /&gt;
           init_square_roots();&lt;br /&gt;
           printf(&amp;quot;The square root of 5 is %f\n&amp;quot;, square_roots[5]);&lt;br /&gt;
           return 0;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
Now, to convert this to a statically initialized array, you would remove the first part of the program and replace it with something like this, calculated by hand:&lt;br /&gt;
&lt;br /&gt;
Square root program with a static lookup table&lt;br /&gt;
&lt;br /&gt;
        double square_roots[] = {&lt;br /&gt;
           /* these are the ones we skipped */ 0.0, 0.0, 0.0, 0.0, 0.0&lt;br /&gt;
           2.236068, /* Square root of 5 */&lt;br /&gt;
           2.449490, /* Square root of 6 */&lt;br /&gt;
           2.645751, /* Square root of 7 */&lt;br /&gt;
           2.828427, /* Square root of 8 */&lt;br /&gt;
           3.0, /* Square root of 9 */&lt;br /&gt;
           ...&lt;br /&gt;
           4.472136 /* Square root of 20 */&lt;br /&gt;
           };&lt;br /&gt;
&lt;br /&gt;
What is needed is a program that will produce these values and print them out in a table like the previous one so they are loaded in at compile-time.&lt;br /&gt;
&lt;br /&gt;
Code generator for the table macro&lt;br /&gt;
&lt;br /&gt;
           #!/usr/bin/perl  &lt;br /&gt;
           #&lt;br /&gt;
           #tablegen.pl &lt;br /&gt;
           #&lt;br /&gt;
           ##Puts each program line into $line&lt;br /&gt;
           while(my $line = &amp;lt;&amp;gt;)&lt;br /&gt;
           {&lt;br /&gt;
             #Is this a macro invocation?&lt;br /&gt;
             if($line =~ m/TABLE:/)&lt;br /&gt;
             {&lt;br /&gt;
                #If so, split it apart into its component pieces&lt;br /&gt;
                my ($dummy, $table_name, $type, $start_idx, $end_idx, $default, $procedure) = split(m/:/, $line, 7);&lt;br /&gt;
                #The main difference between C and Perl for mathematical expressions is that&lt;br /&gt;
                #Perl prefixes its variables with a dollar sign, so we will add that here&lt;br /&gt;
                $procedure =~ s/VAL/\$VAL/g;&lt;br /&gt;
                #Print out the array declaration&lt;br /&gt;
                print &amp;quot;${type} ${table_name} [] = {\n&amp;quot;;&lt;br /&gt;
                #Go through each array element&lt;br /&gt;
                foreach my $VAL (0 .. $end_idx)&lt;br /&gt;
                {&lt;br /&gt;
                   #Only process an answer if we have reached our starting index&lt;br /&gt;
                   if($VAL &amp;gt;= $start_idx)&lt;br /&gt;
                   {&lt;br /&gt;
                      #evaluate the procedure specified (this sets $@ if there are any errors)&lt;br /&gt;
                      $result = eval $procedure;&lt;br /&gt;
                      die(&amp;quot;Error processing: $@&amp;quot;) if $@;&lt;br /&gt;
                   }&lt;br /&gt;
                   else&lt;br /&gt;
                   {&lt;br /&gt;
                      #if we haven't reached the starting index, just use the default&lt;br /&gt;
                      $result = $default;&lt;br /&gt;
                   }&lt;br /&gt;
                   #Print out the value&lt;br /&gt;
                   print &amp;quot;\t${result}&amp;quot;;&lt;br /&gt;
                   #If there are more to be processed, add a comma after the value&lt;br /&gt;
                   if($VAL != $end_idx)&lt;br /&gt;
                   {&lt;br /&gt;
                      print &amp;quot;,&amp;quot;;&lt;br /&gt;
                   }&lt;br /&gt;
                   print &amp;quot;\n&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
                #Finish the declaration&lt;br /&gt;
                print &amp;quot;};\n&amp;quot;;&lt;br /&gt;
             }&lt;br /&gt;
             else&lt;br /&gt;
             {&lt;br /&gt;
                #If this is not a macro invocation, just copy the line directly to the output&lt;br /&gt;
                print $line;&lt;br /&gt;
             }&lt;br /&gt;
           }&lt;br /&gt;
&lt;br /&gt;
To run this program, do this:&lt;br /&gt;
&lt;br /&gt;
Running the code generator&lt;br /&gt;
&lt;br /&gt;
           ./tablegen.pl &amp;lt; sqrt.in &amp;gt; sqrt.c&lt;br /&gt;
           gcc sqrt.c -o sqrt&lt;br /&gt;
           ./a.out&lt;br /&gt;
&lt;br /&gt;
===Mini-language for boiler-plate===&lt;br /&gt;
If you have a large application where many of the functions include a lot of boilerplate code, it is often a good idea to create a mini-language that allows you to work with your boilerplate code in an easier fashion. This mini-language will then be converted into your regular source code language before compiling.&amp;lt;ref name=&amp;quot;use2&amp;quot;&amp;gt;Jonathan Bartlett, &amp;quot;The art of metaprogramming, Part 2: Metaprogramming using Scheme&amp;quot;, 02 May 2006  http://www.ibm.com/developerworks/linux/library/l-metaprog2/index.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following is an example:&lt;br /&gt;
&lt;br /&gt;
Let's say that we are building a CGI application consisting of many independent CGI scripts. In most CGI applications, much of the state is stored in a database, but only a session ID is passed to each script via a cookie.&lt;br /&gt;
&lt;br /&gt;
However, in nearly every page we need to know the other standard information (such as the username, group number, the current job being worked on, whatever else information is pertinent). In addition, we need to redirect the user if they do not have an appropriate cookie.&lt;br /&gt;
&lt;br /&gt;
            (define (handle-cgi-request req)&lt;br /&gt;
              (let (&lt;br /&gt;
                     (session-id (webserver:cookie req &amp;quot;sessionid&amp;quot;)))&lt;br /&gt;
                (if (not (webserver:valid-session-id session-id))&lt;br /&gt;
                    (webserver:redirect-to-login-page)&lt;br /&gt;
                    (let (&lt;br /&gt;
                          (username (webserver:username-for-session session-id))&lt;br /&gt;
                          (group (webserver:group-for-user username))&lt;br /&gt;
                          (current-job (webserver:current-job-for-user username)))&lt;br /&gt;
                       ;;Code for processing goes here&lt;br /&gt;
                       ))))&lt;br /&gt;
&lt;br /&gt;
While some of that can be handled by a procedure, the bindings certainly cannot. However, we can turn most of it into a macro. The macro can be implemented like this:&lt;br /&gt;
&lt;br /&gt;
            (define-syntax cgi-boilerplate&lt;br /&gt;
              (lambda (x)&lt;br /&gt;
                (syntax-case x ()&lt;br /&gt;
                 (&lt;br /&gt;
                   (cgi-boilerplate expr)&lt;br /&gt;
                   (datum-&amp;gt;syntax-object&lt;br /&gt;
                     (syntax k)&lt;br /&gt;
                      (let (&lt;br /&gt;
                            (session-id (webserver:cookie req &amp;quot;sessionid&amp;quot;)))&lt;br /&gt;
                           (if (not (webserver:valid-session-id session-id))&lt;br /&gt;
                               (webserver:redirect-to-login-page)&lt;br /&gt;
                               (let (&lt;br /&gt;
                                      (username (webserver:username-for-session session-id))&lt;br /&gt;
                                      (group (webserver:group-for-user username))&lt;br /&gt;
                                      (current-job (webserver:current-job-for-user username)))&lt;br /&gt;
                                      (syntax-object-&amp;gt;datum (syntax expr))))))&lt;br /&gt;
                  )&lt;br /&gt;
             )))&lt;br /&gt;
&lt;br /&gt;
We can now create new forms based on our boilerplate code by doing the following:&lt;br /&gt;
&lt;br /&gt;
            (define (handle-cgi-request req)&lt;br /&gt;
              (cgi-boilerplate&lt;br /&gt;
               (begin&lt;br /&gt;
                 ;;Do whatever I want here&lt;br /&gt;
                 )))&lt;br /&gt;
&lt;br /&gt;
In addition, since we are not defining our variables explicitly, adding new variable definitions to our boilerplate won't affect its calling conventions, so new features can be added without having to create a whole new function.&lt;br /&gt;
&lt;br /&gt;
In any large project, there are inevitably templates to follow which cannot be reduced to functions, usually because of the bindings being created. Using boilerplate macros can make maintenance of such templated code much easier.&lt;br /&gt;
&lt;br /&gt;
Likewise, other standard macros can be created which make use of variables defined in the boilerplate. Using macros like this significantly reduces typing because you do not have to constantly be writing and rewriting variable bindings, derivations, and parameter passing. This also reduces the potential for errors in such code.&lt;br /&gt;
&lt;br /&gt;
Realize though that boilerplate macros are not a panacea. There are many significant problems that can occur, including:&lt;br /&gt;
*  Accidentally overwriting bindings by introducing a variable name that was previously defined in a macro.&lt;br /&gt;
*  Difficulty tracing problems because the inputs and the outputs of the macros are implicit, not explicit.&lt;br /&gt;
&lt;br /&gt;
These can be largely avoided by doing a few things in conjunction with your boilerplate macros:&lt;br /&gt;
*  Have a naming convention which clearly labels macros as such, as well as indicate that a variable came from boilerplate code. This could be done by affixing -m to macros and -b to variables defined within a boilerplate.&lt;br /&gt;
*  Carefully document all boilerplate macros, especially the introduced variable bindings and all changes between versions.&lt;br /&gt;
*  Only use boilerplate macros when the savings in repetitiveness clearly outweigh the negatives of implicit functionality.&lt;br /&gt;
&lt;br /&gt;
===Abbreviate statements and prevent mistakes===&lt;br /&gt;
A lot of programming languages make you write really verbose statements to do really simple things. Code-generating programs allow you to abbreviate such statements and save a lot of typing, which also prevents a lot of mistakes because there is less chance of mistyping.&lt;br /&gt;
&lt;br /&gt;
=Metaprogramming Framework in Java=&lt;br /&gt;
==Reflection==&lt;br /&gt;
===Introduction to reflection===&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_(computer_programming) Reflection] is the ability to introspect metalevel information about the program structure itself at runtime. Usually this metalevel information is modeled using the general abstraction mechanisms available in the language. In Java, reflection enables to discover information about the loaded classes:&lt;br /&gt;
*  Fields,&lt;br /&gt;
*  Methods, and&lt;br /&gt;
*  Constructors&lt;br /&gt;
*  Generics information&lt;br /&gt;
*  Metadata annotations&lt;br /&gt;
It also enables to use these metaobjects to their instances in runtime environment.&amp;lt;ref name=&amp;quot;meta_java&amp;quot;&amp;gt;Abdelmonaim Remani, &amp;quot;The Art of Metaprogramming in Java&amp;quot;, Jul 19, 2012  http://www.slideshare.net/PolymathicCoder/the-art-of-metaprogramming-in-java&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following is the Java metalevel architecture&amp;lt;ref name=&amp;quot;reflection&amp;quot;&amp;gt;Mika Haapakorpi, &amp;quot;Meta Programming In Java&amp;quot;&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Metalevel architecture.PNG]]&lt;br /&gt;
&lt;br /&gt;
===Dynamic proxy classes===&lt;br /&gt;
A dynamic proxy class implements a list of interfaces specified at runtime when the class is created.&lt;br /&gt;
*  A proxy interface is an interface that is implemented by a proxy class.&lt;br /&gt;
*  A proxy instance is an instance of a proxy class which has an associated invocation handler object.&lt;br /&gt;
The following is an example of dynamic proxy classes&lt;br /&gt;
&lt;br /&gt;
[[File:Dynamic_proxy_class.PNG]]&lt;br /&gt;
&lt;br /&gt;
==Generics==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Generics_in_Java Generics] are a facility of generic programming that was added to the Java programming language in 2004 as part of J2SE 5.0. They allow &amp;quot;a type or method to operate on objects of various types while providing compile-time type safety.&amp;quot;  The generics can be used in classes, interfaces, methods and constructors.&amp;lt;ref name=&amp;quot;reflection&amp;quot;&amp;gt;Mika Haapakorpi, &amp;quot;Meta Programming In Java&amp;quot;&amp;lt;/ref&amp;gt;&lt;br /&gt;
===Two new types in generics===&lt;br /&gt;
Two new types in generics:&lt;br /&gt;
*  Parametrized types&lt;br /&gt;
*  Type variables&lt;br /&gt;
&lt;br /&gt;
A type variable is an unqualified identifier. Class and interface declarations can have type arguments (type variables). Method and constructors definitions can have type arguments (type variables). &lt;br /&gt;
&lt;br /&gt;
The following is an example for type variables&lt;br /&gt;
&lt;br /&gt;
       List&amp;lt;String&amp;gt; anExample = new ArrayList&amp;lt;String&amp;gt;()&lt;br /&gt;
&lt;br /&gt;
*  List : interface&lt;br /&gt;
*  ArrayList : class&lt;br /&gt;
*  String : class (the actual type argument)&lt;br /&gt;
*  List&amp;lt;String&amp;gt; and ArrayList&amp;lt;String&amp;gt; : parametrized type&lt;br /&gt;
&lt;br /&gt;
===Class/Interface Declarations===&lt;br /&gt;
The following is an example of class declaration:&lt;br /&gt;
&lt;br /&gt;
       public interface List&amp;lt;E&amp;gt; {&lt;br /&gt;
         void add(Ex);&lt;br /&gt;
         Iterator&amp;lt;E&amp;gt; iterator();&lt;br /&gt;
       }&lt;br /&gt;
       public interface Iterator&amp;lt;E&amp;gt; {&lt;br /&gt;
         E next();&lt;br /&gt;
         booleanhasNext();&lt;br /&gt;
       }&lt;br /&gt;
       List&amp;lt;String&amp;gt; anExample;&lt;br /&gt;
       anExample.add(”sdfdfss”);&lt;br /&gt;
       anExample.add(new Object()); // compile time error&lt;br /&gt;
       String aTest = anExample.iterator().next();&lt;br /&gt;
&lt;br /&gt;
==Metadata annotation==&lt;br /&gt;
An annotation, in the Java computer programming language, is a form of syntactic metadata that can be added to Java source code. Classes, methods, variables, parameters and packages may be annotated.&amp;lt;ref name=&amp;quot;reflection&amp;quot;&amp;gt;Mika Haapakorpi, &amp;quot;Meta Programming In Java&amp;quot;&amp;lt;/ref&amp;gt; &lt;br /&gt;
&lt;br /&gt;
The following is an example of annotation:&lt;br /&gt;
&lt;br /&gt;
        /**&lt;br /&gt;
        * Designates a formatter to pretty-print the annotated class.&lt;br /&gt;
        */&lt;br /&gt;
        public @interface PrettyPrinter{&lt;br /&gt;
        Class&amp;lt;? extends Formatter&amp;gt; value();&lt;br /&gt;
        }&lt;br /&gt;
        // Single-member annotation with Class&lt;br /&gt;
        // member restricted by bounded wildcard&lt;br /&gt;
        // The annotation presumes the existence of this class.&lt;br /&gt;
        class GorgeousFormatterimplements Formatter { ... }&lt;br /&gt;
        @PrettyPrinter(GorgeousFormatter.class)&lt;br /&gt;
        public class Petunia { ... }&lt;br /&gt;
&lt;br /&gt;
‘@’ indicates the start of an annotation definition.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    5. K. Czarnecki, &amp;quot;Generative Programming&amp;quot; chapter 8, Static Metaprogramming in C++&lt;br /&gt;
&lt;br /&gt;
    6. http://www.program-transformation.org&lt;br /&gt;
&lt;br /&gt;
    7. http://www.codeproject.com/Articles/15429/Ruby-Add-class-methods-at-runtime&lt;br /&gt;
&lt;br /&gt;
    8. http://docs.oracle.com/javase/tutorial/reflect/index.html&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Cyang14</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1_1h_jc&amp;diff=73620</id>
		<title>CSC/ECE 517 Spring 2013/ch1 1h jc</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1_1h_jc&amp;diff=73620"/>
		<updated>2013-02-24T16:51:18Z</updated>

		<summary type="html">&lt;p&gt;Cyang14: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Metaprogramming in statically typed languages&lt;br /&gt;
=Metaprogramming in statically typed languages=&lt;br /&gt;
==Introduction==&lt;br /&gt;
===What is metaprogramming===&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming] refers to writing programs that can manipulate other programs or themselves. The program that is doing the manipulating is called the ''metaprogram'', and the program that is being manipulated is called the ''object program''. The language of the ''metaprogram'' is referred to as the ''metalanguage'' and the language of the object program is called the ''object language''. A language has the ability of [http://en.wikipedia.org/wiki/Reflection_(computer_programming) reflection] if it can be used to write a metaprogram that can manipulate a program written in the same language.&lt;br /&gt;
&lt;br /&gt;
===Metaprogramming in statically typed languages===&lt;br /&gt;
&lt;br /&gt;
Metaprogramming can be accomplished in both statically and dynamically typed languages, however [http://en.wikipedia.org/wiki/Dynamically_typed_language#Dynamic_typing dynamically typed languages] have some advantages over [http://en.wikipedia.org/wiki/Dynamically_typed_language#Dynamic_typing statically typed languages] when it comes to metaprogramming.&lt;br /&gt;
&lt;br /&gt;
One example of a metaprogramming task is to add a method to a particular class at runtime. Sample code in Ruby to perform this task is outlined below. First is a definition of a Ruby class called TestAddMethodAtRuntime:&lt;br /&gt;
&lt;br /&gt;
  class TestAddMethodAtRuntime&lt;br /&gt;
    def originalMethod&lt;br /&gt;
        puts &amp;quot;Original Method&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The code below created a new instance of the class. After that, it defines a new method called newlyAddedMethod. Then the original method is called, follow by the newly added method at runtime:&lt;br /&gt;
&lt;br /&gt;
  t = TestAddMethodAtRuntime.new&lt;br /&gt;
  def t.newlyAddedMethod&lt;br /&gt;
    puts &amp;quot;Newly Added Method&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  t.originalMethod&lt;br /&gt;
  puts &amp;quot;\n&amp;quot;&lt;br /&gt;
  t.newlyAddedMethod&lt;br /&gt;
&lt;br /&gt;
Below is the output of the code above:&lt;br /&gt;
&lt;br /&gt;
  Original Method&lt;br /&gt;
  Newly Added Method&lt;br /&gt;
&lt;br /&gt;
Accomplishing something like this in a statically typed language would be much more difficult or impossible depending on the language. It is for this reason that dynamically typed languages are usually viewed as being better equipped to handle metaprogramming overall.&lt;br /&gt;
&lt;br /&gt;
==Implementation==&lt;br /&gt;
===Programming Language API===&lt;br /&gt;
&lt;br /&gt;
One way that metaprogramming can be possible in a language is through an [http://en.wikipedia.org/wiki/API API] that helps programmers achieve a metaprogramming task. An example of this is a language that provides an API for reflection. One type of reflection is ''introspection'' in which the program can access the source code of the program itself.&lt;br /&gt;
&lt;br /&gt;
Below is an example from the Java API to illustrate how an API can be used to achieve some metaprogramming tasks:&lt;br /&gt;
&lt;br /&gt;
   import java.lang.reflect.*; &lt;br /&gt;
   public class DumpMethods {&lt;br /&gt;
   public static void main(String args[]) {&lt;br /&gt;
     	try {&lt;br /&gt;
           Class c = Class.forName(args[0]);&lt;br /&gt;
           Method m[] = c.getDeclaredMethods();&lt;br /&gt;
           for (int i = 0; i &amp;lt; m.length; i++) {&lt;br /&gt;
              System.out.println(m[i].toString());&lt;br /&gt;
           }&lt;br /&gt;
     	}&lt;br /&gt;
     	catch (Throwable e) {&lt;br /&gt;
        	System.err.println(e);&lt;br /&gt;
     	}&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
The above example prints out all of the methods that are declared for a particular class. This is an example of metaprogramming since the program is accessing the source code of itself.&lt;br /&gt;
&lt;br /&gt;
===Program transformation system===&lt;br /&gt;
&lt;br /&gt;
A program transformation system is something that takes a program as the input, and outputs a different program. One example of a program transformation system is the Java compiler. The Java compiler takes a Java program as its input, and outputs a .class file with platform independent bytecode in it. Another example of a program transformation system is a Java decompiler, which takes bytecode as an input and outputs a Java program.&lt;br /&gt;
&lt;br /&gt;
In the case of the Java compiler, the program input is being output into another program in a different &amp;quot;language&amp;quot;. There are other types of transformation systems that take a program as an input and output a different program in the same language. One example of this would be ''program migration'' which migrates source code to a newer or older version of the same language. This can be useful if upgrading a software system to run on a newer version of a framework.&lt;br /&gt;
&lt;br /&gt;
==Common Uses==&lt;br /&gt;
===Pre-generate static data at compile time===&lt;br /&gt;
One common use of metaprogramming in statically typed languages is to write programs that will pre-generate tables of data for use at runtime.&amp;lt;ref name=&amp;quot;use1&amp;quot;&amp;gt;Jonathan Bartlett, &amp;quot;The art of metaprogramming, Part 1: Introduction to metaprogramming&amp;quot;, 20 Oct 2005   http://www.ibm.com/developerworks/library/l-metaprog1/index.html#N10052&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One simple but useful code generator is to build static lookup tables. Often, in order to build fast functions in C programming, we simply create a lookup table of all of the answers. This means that we either need to pre-compute them by hand (which is wasteful of your time) or build them at runtime (which is wasteful of the user's time).&lt;br /&gt;
&lt;br /&gt;
In following example we will build a generator that will take a function or set of functions on an integer and build lookup tables for the answer.&lt;br /&gt;
&lt;br /&gt;
To think of how to make such a program, we can start from the end and work backward. Firstly we need a lookup table that will return square roots of numbers between 5 and 20. A simple program can be written to generate such a table like this:&lt;br /&gt;
&lt;br /&gt;
Generate and use a lookup table of square roots&lt;br /&gt;
&lt;br /&gt;
        /* our lookup table */&lt;br /&gt;
        double square_roots[21];&lt;br /&gt;
        /* function to load the table at runtime */&lt;br /&gt;
        void init_square_roots()&lt;br /&gt;
        {&lt;br /&gt;
           int i;&lt;br /&gt;
           for(i = 5; i &amp;lt; 21; i++)&lt;br /&gt;
             {&lt;br /&gt;
               square_roots[i] = sqrt((double)i);&lt;br /&gt;
             }&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
With that single macro, we can take away a lot of work for any program that has to generate mathematical tables indexed by integer. A little extra work would also allow tables containing full struct definitions; a little more would ensure that space isn't wasted at the front of the array with useless empty entries.&lt;br /&gt;
        /* program that uses the table */&lt;br /&gt;
        int main ()&lt;br /&gt;
        {&lt;br /&gt;
           init_square_roots();&lt;br /&gt;
           printf(&amp;quot;The square root of 5 is %f\n&amp;quot;, square_roots[5]);&lt;br /&gt;
           return 0;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
Now, to convert this to a statically initialized array, you would remove the first part of the program and replace it with something like this, calculated by hand:&lt;br /&gt;
&lt;br /&gt;
Square root program with a static lookup table&lt;br /&gt;
&lt;br /&gt;
        double square_roots[] = {&lt;br /&gt;
           /* these are the ones we skipped */ 0.0, 0.0, 0.0, 0.0, 0.0&lt;br /&gt;
           2.236068, /* Square root of 5 */&lt;br /&gt;
           2.449490, /* Square root of 6 */&lt;br /&gt;
           2.645751, /* Square root of 7 */&lt;br /&gt;
           2.828427, /* Square root of 8 */&lt;br /&gt;
           3.0, /* Square root of 9 */&lt;br /&gt;
           ...&lt;br /&gt;
           4.472136 /* Square root of 20 */&lt;br /&gt;
           };&lt;br /&gt;
&lt;br /&gt;
What is needed is a program that will produce these values and print them out in a table like the previous one so they are loaded in at compile-time.&lt;br /&gt;
&lt;br /&gt;
Code generator for the table macro&lt;br /&gt;
&lt;br /&gt;
           #!/usr/bin/perl  &lt;br /&gt;
           #&lt;br /&gt;
           #tablegen.pl &lt;br /&gt;
           #&lt;br /&gt;
           ##Puts each program line into $line&lt;br /&gt;
           while(my $line = &amp;lt;&amp;gt;)&lt;br /&gt;
           {&lt;br /&gt;
             #Is this a macro invocation?&lt;br /&gt;
             if($line =~ m/TABLE:/)&lt;br /&gt;
             {&lt;br /&gt;
                #If so, split it apart into its component pieces&lt;br /&gt;
                my ($dummy, $table_name, $type, $start_idx, $end_idx, $default, $procedure) = split(m/:/, $line, 7);&lt;br /&gt;
                #The main difference between C and Perl for mathematical expressions is that&lt;br /&gt;
                #Perl prefixes its variables with a dollar sign, so we will add that here&lt;br /&gt;
                $procedure =~ s/VAL/\$VAL/g;&lt;br /&gt;
                #Print out the array declaration&lt;br /&gt;
                print &amp;quot;${type} ${table_name} [] = {\n&amp;quot;;&lt;br /&gt;
                #Go through each array element&lt;br /&gt;
                foreach my $VAL (0 .. $end_idx)&lt;br /&gt;
                {&lt;br /&gt;
                   #Only process an answer if we have reached our starting index&lt;br /&gt;
                   if($VAL &amp;gt;= $start_idx)&lt;br /&gt;
                   {&lt;br /&gt;
                      #evaluate the procedure specified (this sets $@ if there are any errors)&lt;br /&gt;
                      $result = eval $procedure;&lt;br /&gt;
                      die(&amp;quot;Error processing: $@&amp;quot;) if $@;&lt;br /&gt;
                   }&lt;br /&gt;
                   else&lt;br /&gt;
                   {&lt;br /&gt;
                      #if we haven't reached the starting index, just use the default&lt;br /&gt;
                      $result = $default;&lt;br /&gt;
                   }&lt;br /&gt;
                   #Print out the value&lt;br /&gt;
                   print &amp;quot;\t${result}&amp;quot;;&lt;br /&gt;
                   #If there are more to be processed, add a comma after the value&lt;br /&gt;
                   if($VAL != $end_idx)&lt;br /&gt;
                   {&lt;br /&gt;
                      print &amp;quot;,&amp;quot;;&lt;br /&gt;
                   }&lt;br /&gt;
                   print &amp;quot;\n&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
                #Finish the declaration&lt;br /&gt;
                print &amp;quot;};\n&amp;quot;;&lt;br /&gt;
             }&lt;br /&gt;
             else&lt;br /&gt;
             {&lt;br /&gt;
                #If this is not a macro invocation, just copy the line directly to the output&lt;br /&gt;
                print $line;&lt;br /&gt;
             }&lt;br /&gt;
           }&lt;br /&gt;
&lt;br /&gt;
To run this program, do this:&lt;br /&gt;
&lt;br /&gt;
Running the code generator&lt;br /&gt;
&lt;br /&gt;
           ./tablegen.pl &amp;lt; sqrt.in &amp;gt; sqrt.c&lt;br /&gt;
           gcc sqrt.c -o sqrt&lt;br /&gt;
           ./a.out&lt;br /&gt;
&lt;br /&gt;
===Mini-language for boiler-plate===&lt;br /&gt;
If you have a large application where many of the functions include a lot of boilerplate code, it is often a good idea to create a mini-language that allows you to work with your boilerplate code in an easier fashion. This mini-language will then be converted into your regular source code language before compiling.&amp;lt;ref name=&amp;quot;use2&amp;quot;&amp;gt;Jonathan Bartlett, &amp;quot;The art of metaprogramming, Part 2: Metaprogramming using Scheme&amp;quot;, 02 May 2006  http://www.ibm.com/developerworks/linux/library/l-metaprog2/index.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following is an example:&lt;br /&gt;
&lt;br /&gt;
Let's say that we are building a CGI application consisting of many independent CGI scripts. In most CGI applications, much of the state is stored in a database, but only a session ID is passed to each script via a cookie.&lt;br /&gt;
&lt;br /&gt;
However, in nearly every page we need to know the other standard information (such as the username, group number, the current job being worked on, whatever else information is pertinent). In addition, we need to redirect the user if they do not have an appropriate cookie.&lt;br /&gt;
&lt;br /&gt;
            (define (handle-cgi-request req)&lt;br /&gt;
              (let (&lt;br /&gt;
                     (session-id (webserver:cookie req &amp;quot;sessionid&amp;quot;)))&lt;br /&gt;
                (if (not (webserver:valid-session-id session-id))&lt;br /&gt;
                    (webserver:redirect-to-login-page)&lt;br /&gt;
                    (let (&lt;br /&gt;
                          (username (webserver:username-for-session session-id))&lt;br /&gt;
                          (group (webserver:group-for-user username))&lt;br /&gt;
                          (current-job (webserver:current-job-for-user username)))&lt;br /&gt;
                       ;;Code for processing goes here&lt;br /&gt;
                       ))))&lt;br /&gt;
&lt;br /&gt;
While some of that can be handled by a procedure, the bindings certainly cannot. However, we can turn most of it into a macro. The macro can be implemented like this:&lt;br /&gt;
&lt;br /&gt;
            (define-syntax cgi-boilerplate&lt;br /&gt;
              (lambda (x)&lt;br /&gt;
                (syntax-case x ()&lt;br /&gt;
                 (&lt;br /&gt;
                   (cgi-boilerplate expr)&lt;br /&gt;
                   (datum-&amp;gt;syntax-object&lt;br /&gt;
                     (syntax k)&lt;br /&gt;
                      (let (&lt;br /&gt;
                            (session-id (webserver:cookie req &amp;quot;sessionid&amp;quot;)))&lt;br /&gt;
                           (if (not (webserver:valid-session-id session-id))&lt;br /&gt;
                               (webserver:redirect-to-login-page)&lt;br /&gt;
                               (let (&lt;br /&gt;
                                      (username (webserver:username-for-session session-id))&lt;br /&gt;
                                      (group (webserver:group-for-user username))&lt;br /&gt;
                                      (current-job (webserver:current-job-for-user username)))&lt;br /&gt;
                                      (syntax-object-&amp;gt;datum (syntax expr))))))&lt;br /&gt;
                  )&lt;br /&gt;
             )))&lt;br /&gt;
&lt;br /&gt;
We can now create new forms based on our boilerplate code by doing the following:&lt;br /&gt;
&lt;br /&gt;
            (define (handle-cgi-request req)&lt;br /&gt;
              (cgi-boilerplate&lt;br /&gt;
               (begin&lt;br /&gt;
                 ;;Do whatever I want here&lt;br /&gt;
                 )))&lt;br /&gt;
&lt;br /&gt;
In addition, since we are not defining our variables explicitly, adding new variable definitions to our boilerplate won't affect its calling conventions, so new features can be added without having to create a whole new function.&lt;br /&gt;
&lt;br /&gt;
In any large project, there are inevitably templates to follow which cannot be reduced to functions, usually because of the bindings being created. Using boilerplate macros can make maintenance of such templated code much easier.&lt;br /&gt;
&lt;br /&gt;
Likewise, other standard macros can be created which make use of variables defined in the boilerplate. Using macros like this significantly reduces typing because you do not have to constantly be writing and rewriting variable bindings, derivations, and parameter passing. This also reduces the potential for errors in such code.&lt;br /&gt;
&lt;br /&gt;
Realize though that boilerplate macros are not a panacea. There are many significant problems that can occur, including:&lt;br /&gt;
*  Accidentally overwriting bindings by introducing a variable name that was previously defined in a macro.&lt;br /&gt;
*  Difficulty tracing problems because the inputs and the outputs of the macros are implicit, not explicit.&lt;br /&gt;
&lt;br /&gt;
These can be largely avoided by doing a few things in conjunction with your boilerplate macros:&lt;br /&gt;
*  Have a naming convention which clearly labels macros as such, as well as indicate that a variable came from boilerplate code. This could be done by affixing -m to macros and -b to variables defined within a boilerplate.&lt;br /&gt;
*  Carefully document all boilerplate macros, especially the introduced variable bindings and all changes between versions.&lt;br /&gt;
*  Only use boilerplate macros when the savings in repetitiveness clearly outweigh the negatives of implicit functionality.&lt;br /&gt;
&lt;br /&gt;
===Abbreviate statements and prevent mistakes===&lt;br /&gt;
A lot of programming languages make you write really verbose statements to do really simple things. Code-generating programs allow you to abbreviate such statements and save a lot of typing, which also prevents a lot of mistakes because there is less chance of mistyping.&lt;br /&gt;
&lt;br /&gt;
=Metaprogramming Framework in Java=&lt;br /&gt;
==Reflection==&lt;br /&gt;
===Introduction to reflection===&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_(computer_programming) Reflection] is the ability to introspect metalevel information about the program structure itself at runtime. Usually this metalevel information is modeled using the general abstraction mechanisms available in the language. In Java, reflection enables to discover information about the loaded classes:&lt;br /&gt;
*  Fields,&lt;br /&gt;
*  Methods, and&lt;br /&gt;
*  Constructors&lt;br /&gt;
*  Generics information&lt;br /&gt;
*  Metadata annotations&lt;br /&gt;
It also enables to use these metaobjects to their instances in runtime environment.&amp;lt;ref name=&amp;quot;meta_java&amp;quot;&amp;gt;Abdelmonaim Remani, &amp;quot;The Art of Metaprogramming in Java&amp;quot;, Jul 19, 2012  http://www.slideshare.net/PolymathicCoder/the-art-of-metaprogramming-in-java&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following is the Java metalevel architecture&amp;lt;ref name=&amp;quot;reflection&amp;quot;&amp;gt;Mika Haapakorpi, &amp;quot;Meta Programming In Java&amp;quot;&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Metalevel architecture.PNG]]&lt;br /&gt;
&lt;br /&gt;
===Dynamic proxy classes===&lt;br /&gt;
A dynamic proxy class implements a list of interfaces specified at runtime when the class is created.&lt;br /&gt;
*  A proxy interface is an interface that is implemented by a proxy class.&lt;br /&gt;
*  A proxy instance is an instance of a proxy class which has an associated invocation handler object.&lt;br /&gt;
The following is an example of dynamic proxy classes&lt;br /&gt;
&lt;br /&gt;
[[File:Dynamic_proxy_class.PNG]]&lt;br /&gt;
&lt;br /&gt;
==Generics==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Generics_in_Java Generics] are a facility of generic programming that was added to the Java programming language in 2004 as part of J2SE 5.0. They allow &amp;quot;a type or method to operate on objects of various types while providing compile-time type safety.&amp;quot;  The generics can be used in classes, interfaces, methods and constructors.&amp;lt;ref name=&amp;quot;reflection&amp;quot;&amp;gt;Mika Haapakorpi, &amp;quot;Meta Programming In Java&amp;quot;&amp;lt;/ref&amp;gt;&lt;br /&gt;
===Two new types in generics===&lt;br /&gt;
Two new types in generics:&lt;br /&gt;
*  Parametrized types&lt;br /&gt;
*  Type variables&lt;br /&gt;
&lt;br /&gt;
A type variable is an unqualified identifier. Class and interface declarations can have type arguments (type variables). Method and constructors definitions can have type arguments (type variables). &lt;br /&gt;
&lt;br /&gt;
The following is an example for type variables&lt;br /&gt;
&lt;br /&gt;
       List&amp;lt;String&amp;gt; anExample = new ArrayList&amp;lt;String&amp;gt;()&lt;br /&gt;
&lt;br /&gt;
*  List : interface&lt;br /&gt;
*  ArrayList : class&lt;br /&gt;
*  String : class (the actual type argument)&lt;br /&gt;
*  List&amp;lt;String&amp;gt; and ArrayList&amp;lt;String&amp;gt; : parametrized type&lt;br /&gt;
&lt;br /&gt;
===Class/Interface Declarations===&lt;br /&gt;
The following is an example of class declaration:&lt;br /&gt;
&lt;br /&gt;
       public interface List&amp;lt;E&amp;gt; {&lt;br /&gt;
         void add(Ex);&lt;br /&gt;
         Iterator&amp;lt;E&amp;gt; iterator();&lt;br /&gt;
       }&lt;br /&gt;
       public interface Iterator&amp;lt;E&amp;gt; {&lt;br /&gt;
         E next();&lt;br /&gt;
         booleanhasNext();&lt;br /&gt;
       }&lt;br /&gt;
       List&amp;lt;String&amp;gt; anExample;&lt;br /&gt;
       anExample.add(”sdfdfss”);&lt;br /&gt;
       anExample.add(new Object()); // compile time error&lt;br /&gt;
       String aTest = anExample.iterator().next();&lt;br /&gt;
&lt;br /&gt;
==Metadata annotation==&lt;br /&gt;
An annotation, in the Java computer programming language, is a form of syntactic metadata that can be added to Java source code. Classes, methods, variables, parameters and packages may be annotated.&amp;lt;ref name=&amp;quot;reflection&amp;quot;&amp;gt;Mika Haapakorpi, &amp;quot;Meta Programming In Java&amp;quot;&amp;lt;/ref&amp;gt; &lt;br /&gt;
&lt;br /&gt;
The following is an example of annotation:&lt;br /&gt;
&lt;br /&gt;
        /**&lt;br /&gt;
        * Designates a formatter to pretty-print the annotated class.&lt;br /&gt;
        */&lt;br /&gt;
        public @interface PrettyPrinter{&lt;br /&gt;
        Class&amp;lt;? extends Formatter&amp;gt; value();&lt;br /&gt;
        }&lt;br /&gt;
        // Single-member annotation with Class&lt;br /&gt;
        // member restricted by bounded wildcard&lt;br /&gt;
        // The annotation presumes the existence of this class.&lt;br /&gt;
        class GorgeousFormatterimplements Formatter { ... }&lt;br /&gt;
        @PrettyPrinter(GorgeousFormatter.class)&lt;br /&gt;
        public class Petunia { ... }&lt;br /&gt;
&lt;br /&gt;
‘@’ indicates the start of an annotation definition.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
K. Czarnecki, &amp;quot;Generative Programming&amp;quot; chapter 8, Static Metaprogramming in C++&lt;br /&gt;
&lt;br /&gt;
http://www.program-transformation.org&lt;br /&gt;
&lt;br /&gt;
http://www.codeproject.com/Articles/15429/Ruby-Add-class-methods-at-runtime&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Cyang14</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1_1h_jc&amp;diff=73619</id>
		<title>CSC/ECE 517 Spring 2013/ch1 1h jc</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1_1h_jc&amp;diff=73619"/>
		<updated>2013-02-24T16:49:30Z</updated>

		<summary type="html">&lt;p&gt;Cyang14: /* Introduction to reflection */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Metaprogramming in statically typed languages&lt;br /&gt;
=Metaprogramming in statically typed languages=&lt;br /&gt;
==Introduction==&lt;br /&gt;
===What is metaprogramming===&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming] refers to writing programs that can manipulate other programs or themselves. The program that is doing the manipulating is called the ''metaprogram'', and the program that is being manipulated is called the ''object program''. The language of the ''metaprogram'' is referred to as the ''metalanguage'' and the language of the object program is called the ''object language''. A language has the ability of [http://en.wikipedia.org/wiki/Reflection_(computer_programming) reflection] if it can be used to write a metaprogram that can manipulate a program written in the same language.&lt;br /&gt;
&lt;br /&gt;
===Metaprogramming in statically typed languages===&lt;br /&gt;
&lt;br /&gt;
Metaprogramming can be accomplished in both statically and dynamically typed languages, however [http://en.wikipedia.org/wiki/Dynamically_typed_language#Dynamic_typing dynamically typed languages] have some advantages over [http://en.wikipedia.org/wiki/Dynamically_typed_language#Dynamic_typing statically typed languages] when it comes to metaprogramming.&lt;br /&gt;
&lt;br /&gt;
One example of a metaprogramming task is to add a method to a particular class at runtime. Sample code in Ruby to perform this task is outlined below. First is a definition of a Ruby class called TestAddMethodAtRuntime:&lt;br /&gt;
&lt;br /&gt;
  class TestAddMethodAtRuntime&lt;br /&gt;
    def originalMethod&lt;br /&gt;
        puts &amp;quot;Original Method&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The code below created a new instance of the class. After that, it defines a new method called newlyAddedMethod. Then the original method is called, follow by the newly added method at runtime:&lt;br /&gt;
&lt;br /&gt;
  t = TestAddMethodAtRuntime.new&lt;br /&gt;
  def t.newlyAddedMethod&lt;br /&gt;
    puts &amp;quot;Newly Added Method&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  t.originalMethod&lt;br /&gt;
  puts &amp;quot;\n&amp;quot;&lt;br /&gt;
  t.newlyAddedMethod&lt;br /&gt;
&lt;br /&gt;
Below is the output of the code above:&lt;br /&gt;
&lt;br /&gt;
  Original Method&lt;br /&gt;
  Newly Added Method&lt;br /&gt;
&lt;br /&gt;
Accomplishing something like this in a statically typed language would be much more difficult or impossible depending on the language. It is for this reason that dynamically typed languages are usually viewed as being better equipped to handle metaprogramming overall.&lt;br /&gt;
&lt;br /&gt;
==Implementation==&lt;br /&gt;
===Programming Language API===&lt;br /&gt;
&lt;br /&gt;
One way that metaprogramming can be possible in a language is through an [http://en.wikipedia.org/wiki/API API] that helps programmers achieve a metaprogramming task. An example of this is a language that provides an API for reflection. One type of reflection is ''introspection'' in which the program can access the source code of the program itself.&lt;br /&gt;
&lt;br /&gt;
Below is an example from the Java API to illustrate how an API can be used to achieve some metaprogramming tasks:&lt;br /&gt;
&lt;br /&gt;
   import java.lang.reflect.*; &lt;br /&gt;
   public class DumpMethods {&lt;br /&gt;
   public static void main(String args[]) {&lt;br /&gt;
     	try {&lt;br /&gt;
           Class c = Class.forName(args[0]);&lt;br /&gt;
           Method m[] = c.getDeclaredMethods();&lt;br /&gt;
           for (int i = 0; i &amp;lt; m.length; i++) {&lt;br /&gt;
              System.out.println(m[i].toString());&lt;br /&gt;
           }&lt;br /&gt;
     	}&lt;br /&gt;
     	catch (Throwable e) {&lt;br /&gt;
        	System.err.println(e);&lt;br /&gt;
     	}&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
The above example prints out all of the methods that are declared for a particular class. This is an example of metaprogramming since the program is accessing the source code of itself.&lt;br /&gt;
&lt;br /&gt;
===Program transformation system===&lt;br /&gt;
&lt;br /&gt;
A program transformation system is something that takes a program as the input, and outputs a different program. One example of a program transformation system is the Java compiler. The Java compiler takes a Java program as its input, and outputs a .class file with platform independent bytecode in it. Another example of a program transformation system is a Java decompiler, which takes bytecode as an input and outputs a Java program.&lt;br /&gt;
&lt;br /&gt;
In the case of the Java compiler, the program input is being output into another program in a different &amp;quot;language&amp;quot;. There are other types of transformation systems that take a program as an input and output a different program in the same language. One example of this would be ''program migration'' which migrates source code to a newer or older version of the same language. This can be useful if upgrading a software system to run on a newer version of a framework.&lt;br /&gt;
&lt;br /&gt;
==Common Uses==&lt;br /&gt;
===Pre-generate static data at compile time===&lt;br /&gt;
One common use of metaprogramming in statically typed languages is to write programs that will pre-generate tables of data for use at runtime.&amp;lt;ref name=&amp;quot;use1&amp;quot;&amp;gt;Jonathan Bartlett, &amp;quot;The art of metaprogramming, Part 1: Introduction to metaprogramming&amp;quot;, 20 Oct 2005   http://www.ibm.com/developerworks/library/l-metaprog1/index.html#N10052&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One simple but useful code generator is to build static lookup tables. Often, in order to build fast functions in C programming, we simply create a lookup table of all of the answers. This means that we either need to pre-compute them by hand (which is wasteful of your time) or build them at runtime (which is wasteful of the user's time).&lt;br /&gt;
&lt;br /&gt;
In following example we will build a generator that will take a function or set of functions on an integer and build lookup tables for the answer.&lt;br /&gt;
&lt;br /&gt;
To think of how to make such a program, we can start from the end and work backward. Firstly we need a lookup table that will return square roots of numbers between 5 and 20. A simple program can be written to generate such a table like this:&lt;br /&gt;
&lt;br /&gt;
Generate and use a lookup table of square roots&lt;br /&gt;
&lt;br /&gt;
        /* our lookup table */&lt;br /&gt;
        double square_roots[21];&lt;br /&gt;
        /* function to load the table at runtime */&lt;br /&gt;
        void init_square_roots()&lt;br /&gt;
        {&lt;br /&gt;
           int i;&lt;br /&gt;
           for(i = 5; i &amp;lt; 21; i++)&lt;br /&gt;
             {&lt;br /&gt;
               square_roots[i] = sqrt((double)i);&lt;br /&gt;
             }&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
With that single macro, we can take away a lot of work for any program that has to generate mathematical tables indexed by integer. A little extra work would also allow tables containing full struct definitions; a little more would ensure that space isn't wasted at the front of the array with useless empty entries.&lt;br /&gt;
        /* program that uses the table */&lt;br /&gt;
        int main ()&lt;br /&gt;
        {&lt;br /&gt;
           init_square_roots();&lt;br /&gt;
           printf(&amp;quot;The square root of 5 is %f\n&amp;quot;, square_roots[5]);&lt;br /&gt;
           return 0;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
Now, to convert this to a statically initialized array, you would remove the first part of the program and replace it with something like this, calculated by hand:&lt;br /&gt;
&lt;br /&gt;
Square root program with a static lookup table&lt;br /&gt;
&lt;br /&gt;
        double square_roots[] = {&lt;br /&gt;
           /* these are the ones we skipped */ 0.0, 0.0, 0.0, 0.0, 0.0&lt;br /&gt;
           2.236068, /* Square root of 5 */&lt;br /&gt;
           2.449490, /* Square root of 6 */&lt;br /&gt;
           2.645751, /* Square root of 7 */&lt;br /&gt;
           2.828427, /* Square root of 8 */&lt;br /&gt;
           3.0, /* Square root of 9 */&lt;br /&gt;
           ...&lt;br /&gt;
           4.472136 /* Square root of 20 */&lt;br /&gt;
           };&lt;br /&gt;
&lt;br /&gt;
What is needed is a program that will produce these values and print them out in a table like the previous one so they are loaded in at compile-time.&lt;br /&gt;
&lt;br /&gt;
Code generator for the table macro&lt;br /&gt;
&lt;br /&gt;
           #!/usr/bin/perl  &lt;br /&gt;
           #&lt;br /&gt;
           #tablegen.pl &lt;br /&gt;
           #&lt;br /&gt;
           ##Puts each program line into $line&lt;br /&gt;
           while(my $line = &amp;lt;&amp;gt;)&lt;br /&gt;
           {&lt;br /&gt;
             #Is this a macro invocation?&lt;br /&gt;
             if($line =~ m/TABLE:/)&lt;br /&gt;
             {&lt;br /&gt;
                #If so, split it apart into its component pieces&lt;br /&gt;
                my ($dummy, $table_name, $type, $start_idx, $end_idx, $default, $procedure) = split(m/:/, $line, 7);&lt;br /&gt;
                #The main difference between C and Perl for mathematical expressions is that&lt;br /&gt;
                #Perl prefixes its variables with a dollar sign, so we will add that here&lt;br /&gt;
                $procedure =~ s/VAL/\$VAL/g;&lt;br /&gt;
                #Print out the array declaration&lt;br /&gt;
                print &amp;quot;${type} ${table_name} [] = {\n&amp;quot;;&lt;br /&gt;
                #Go through each array element&lt;br /&gt;
                foreach my $VAL (0 .. $end_idx)&lt;br /&gt;
                {&lt;br /&gt;
                   #Only process an answer if we have reached our starting index&lt;br /&gt;
                   if($VAL &amp;gt;= $start_idx)&lt;br /&gt;
                   {&lt;br /&gt;
                      #evaluate the procedure specified (this sets $@ if there are any errors)&lt;br /&gt;
                      $result = eval $procedure;&lt;br /&gt;
                      die(&amp;quot;Error processing: $@&amp;quot;) if $@;&lt;br /&gt;
                   }&lt;br /&gt;
                   else&lt;br /&gt;
                   {&lt;br /&gt;
                      #if we haven't reached the starting index, just use the default&lt;br /&gt;
                      $result = $default;&lt;br /&gt;
                   }&lt;br /&gt;
                   #Print out the value&lt;br /&gt;
                   print &amp;quot;\t${result}&amp;quot;;&lt;br /&gt;
                   #If there are more to be processed, add a comma after the value&lt;br /&gt;
                   if($VAL != $end_idx)&lt;br /&gt;
                   {&lt;br /&gt;
                      print &amp;quot;,&amp;quot;;&lt;br /&gt;
                   }&lt;br /&gt;
                   print &amp;quot;\n&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
                #Finish the declaration&lt;br /&gt;
                print &amp;quot;};\n&amp;quot;;&lt;br /&gt;
             }&lt;br /&gt;
             else&lt;br /&gt;
             {&lt;br /&gt;
                #If this is not a macro invocation, just copy the line directly to the output&lt;br /&gt;
                print $line;&lt;br /&gt;
             }&lt;br /&gt;
           }&lt;br /&gt;
&lt;br /&gt;
To run this program, do this:&lt;br /&gt;
&lt;br /&gt;
Running the code generator&lt;br /&gt;
&lt;br /&gt;
           ./tablegen.pl &amp;lt; sqrt.in &amp;gt; sqrt.c&lt;br /&gt;
           gcc sqrt.c -o sqrt&lt;br /&gt;
           ./a.out&lt;br /&gt;
&lt;br /&gt;
===Mini-language for boiler-plate===&lt;br /&gt;
If you have a large application where many of the functions include a lot of boilerplate code, it is often a good idea to create a mini-language that allows you to work with your boilerplate code in an easier fashion. This mini-language will then be converted into your regular source code language before compiling.&amp;lt;ref name=&amp;quot;use2&amp;quot;&amp;gt;Jonathan Bartlett, &amp;quot;The art of metaprogramming, Part 2: Metaprogramming using Scheme&amp;quot;, 02 May 2006  http://www.ibm.com/developerworks/linux/library/l-metaprog2/index.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following is an example:&lt;br /&gt;
&lt;br /&gt;
Let's say that we are building a CGI application consisting of many independent CGI scripts. In most CGI applications, much of the state is stored in a database, but only a session ID is passed to each script via a cookie.&lt;br /&gt;
&lt;br /&gt;
However, in nearly every page we need to know the other standard information (such as the username, group number, the current job being worked on, whatever else information is pertinent). In addition, we need to redirect the user if they do not have an appropriate cookie.&lt;br /&gt;
&lt;br /&gt;
            (define (handle-cgi-request req)&lt;br /&gt;
              (let (&lt;br /&gt;
                     (session-id (webserver:cookie req &amp;quot;sessionid&amp;quot;)))&lt;br /&gt;
                (if (not (webserver:valid-session-id session-id))&lt;br /&gt;
                    (webserver:redirect-to-login-page)&lt;br /&gt;
                    (let (&lt;br /&gt;
                          (username (webserver:username-for-session session-id))&lt;br /&gt;
                          (group (webserver:group-for-user username))&lt;br /&gt;
                          (current-job (webserver:current-job-for-user username)))&lt;br /&gt;
                       ;;Code for processing goes here&lt;br /&gt;
                       ))))&lt;br /&gt;
&lt;br /&gt;
While some of that can be handled by a procedure, the bindings certainly cannot. However, we can turn most of it into a macro. The macro can be implemented like this:&lt;br /&gt;
&lt;br /&gt;
            (define-syntax cgi-boilerplate&lt;br /&gt;
              (lambda (x)&lt;br /&gt;
                (syntax-case x ()&lt;br /&gt;
                 (&lt;br /&gt;
                   (cgi-boilerplate expr)&lt;br /&gt;
                   (datum-&amp;gt;syntax-object&lt;br /&gt;
                     (syntax k)&lt;br /&gt;
                      (let (&lt;br /&gt;
                            (session-id (webserver:cookie req &amp;quot;sessionid&amp;quot;)))&lt;br /&gt;
                           (if (not (webserver:valid-session-id session-id))&lt;br /&gt;
                               (webserver:redirect-to-login-page)&lt;br /&gt;
                               (let (&lt;br /&gt;
                                      (username (webserver:username-for-session session-id))&lt;br /&gt;
                                      (group (webserver:group-for-user username))&lt;br /&gt;
                                      (current-job (webserver:current-job-for-user username)))&lt;br /&gt;
                                      (syntax-object-&amp;gt;datum (syntax expr))))))&lt;br /&gt;
                  )&lt;br /&gt;
             )))&lt;br /&gt;
&lt;br /&gt;
We can now create new forms based on our boilerplate code by doing the following:&lt;br /&gt;
&lt;br /&gt;
            (define (handle-cgi-request req)&lt;br /&gt;
              (cgi-boilerplate&lt;br /&gt;
               (begin&lt;br /&gt;
                 ;;Do whatever I want here&lt;br /&gt;
                 )))&lt;br /&gt;
&lt;br /&gt;
In addition, since we are not defining our variables explicitly, adding new variable definitions to our boilerplate won't affect its calling conventions, so new features can be added without having to create a whole new function.&lt;br /&gt;
&lt;br /&gt;
In any large project, there are inevitably templates to follow which cannot be reduced to functions, usually because of the bindings being created. Using boilerplate macros can make maintenance of such templated code much easier.&lt;br /&gt;
&lt;br /&gt;
Likewise, other standard macros can be created which make use of variables defined in the boilerplate. Using macros like this significantly reduces typing because you do not have to constantly be writing and rewriting variable bindings, derivations, and parameter passing. This also reduces the potential for errors in such code.&lt;br /&gt;
&lt;br /&gt;
Realize though that boilerplate macros are not a panacea. There are many significant problems that can occur, including:&lt;br /&gt;
*  Accidentally overwriting bindings by introducing a variable name that was previously defined in a macro.&lt;br /&gt;
*  Difficulty tracing problems because the inputs and the outputs of the macros are implicit, not explicit.&lt;br /&gt;
&lt;br /&gt;
These can be largely avoided by doing a few things in conjunction with your boilerplate macros:&lt;br /&gt;
*  Have a naming convention which clearly labels macros as such, as well as indicate that a variable came from boilerplate code. This could be done by affixing -m to macros and -b to variables defined within a boilerplate.&lt;br /&gt;
*  Carefully document all boilerplate macros, especially the introduced variable bindings and all changes between versions.&lt;br /&gt;
*  Only use boilerplate macros when the savings in repetitiveness clearly outweigh the negatives of implicit functionality.&lt;br /&gt;
&lt;br /&gt;
===Abbreviate statements and prevent mistakes===&lt;br /&gt;
A lot of programming languages make you write really verbose statements to do really simple things. Code-generating programs allow you to abbreviate such statements and save a lot of typing, which also prevents a lot of mistakes because there is less chance of mistyping.&lt;br /&gt;
&lt;br /&gt;
=Metaprogramming Framework in Java=&lt;br /&gt;
==Reflection==&lt;br /&gt;
===Introduction to reflection===&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_(computer_programming) Reflection] is the ability to introspect metalevel information about the program structure itself at runtime. Usually this metalevel information is modeled using the general abstraction mechanisms available in the language. In Java, reflection enables to discover information about the loaded classes:&lt;br /&gt;
*  Fields,&lt;br /&gt;
*  Methods, and&lt;br /&gt;
*  Constructors&lt;br /&gt;
*  Generics information&lt;br /&gt;
*  Metadata annotations&lt;br /&gt;
It also enables to use these metaobjects to their instances in runtime environment.&amp;lt;ref name=&amp;quot;meta_java&amp;quot;&amp;gt;Abdelmonaim Remani, &amp;quot;The Art of Metaprogramming in Java&amp;quot;, Jul 19, 2012  http://www.slideshare.net/PolymathicCoder/the-art-of-metaprogramming-in-java&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following is the Java metalevel architecture&amp;lt;ref name=&amp;quot;reflection&amp;quot;&amp;gt;Mika Haapakorpi, &amp;quot;Meta Programming In Java&amp;quot;&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Metalevel architecture.PNG]]&lt;br /&gt;
&lt;br /&gt;
===Dynamic proxy classes===&lt;br /&gt;
A dynamic proxy class implements a list of interfaces specified at runtime when the class is created.&lt;br /&gt;
*  A proxy interface is an interface that is implemented by a proxy class.&lt;br /&gt;
*  A proxy instance is an instance of a proxy class which has an associated invocation handler object.&lt;br /&gt;
The following is an example of dynamic proxy classes&lt;br /&gt;
&lt;br /&gt;
[[File:Dynamic_proxy_class.PNG]]&lt;br /&gt;
&lt;br /&gt;
==Generics==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Generics_in_Java Generics] are a facility of generic programming that was added to the Java programming language in 2004 as part of J2SE 5.0. They allow &amp;quot;a type or method to operate on objects of various types while providing compile-time type safety.&amp;quot;  The generics can be used in classes, interfaces, methods and constructors.&amp;lt;ref name=&amp;quot;reflection&amp;quot;&amp;gt;Mika Haapakorpi, &amp;quot;Meta Programming In Java&amp;quot;&amp;lt;/ref&amp;gt;&lt;br /&gt;
===Two new types in generics===&lt;br /&gt;
Two new types in generics:&lt;br /&gt;
*  Parametrized types&lt;br /&gt;
*  Type variables&lt;br /&gt;
&lt;br /&gt;
A type variable is an unqualified identifier. Class and interface declarations can have type arguments (type variables). Method and constructors definitions can have type arguments (type variables). &lt;br /&gt;
&lt;br /&gt;
The following is an example for type variables&lt;br /&gt;
&lt;br /&gt;
       List&amp;lt;String&amp;gt; anExample = new ArrayList&amp;lt;String&amp;gt;()&lt;br /&gt;
&lt;br /&gt;
*  List : interface&lt;br /&gt;
*  ArrayList : class&lt;br /&gt;
*  String : class (the actual type argument)&lt;br /&gt;
*  List&amp;lt;String&amp;gt; and ArrayList&amp;lt;String&amp;gt; : parametrized type&lt;br /&gt;
&lt;br /&gt;
===Class/Interface Declarations===&lt;br /&gt;
The following is an example of class declaration:&lt;br /&gt;
&lt;br /&gt;
       public interface List&amp;lt;E&amp;gt; {&lt;br /&gt;
         void add(Ex);&lt;br /&gt;
         Iterator&amp;lt;E&amp;gt; iterator();&lt;br /&gt;
       }&lt;br /&gt;
       public interface Iterator&amp;lt;E&amp;gt; {&lt;br /&gt;
         E next();&lt;br /&gt;
         booleanhasNext();&lt;br /&gt;
       }&lt;br /&gt;
       List&amp;lt;String&amp;gt; anExample;&lt;br /&gt;
       anExample.add(”sdfdfss”);&lt;br /&gt;
       anExample.add(new Object()); // compile time error&lt;br /&gt;
       String aTest = anExample.iterator().next();&lt;br /&gt;
&lt;br /&gt;
==Metadata annotation==&lt;br /&gt;
An annotation, in the Java computer programming language, is a form of syntactic metadata that can be added to Java source code. Classes, methods, variables, parameters and packages may be annotated.&amp;lt;ref name=&amp;quot;reflection&amp;quot;&amp;gt;Mika Haapakorpi, &amp;quot;Meta Programming In Java&amp;quot;&amp;lt;/ref&amp;gt; &lt;br /&gt;
&lt;br /&gt;
The following is an example of annotation:&lt;br /&gt;
&lt;br /&gt;
        /**&lt;br /&gt;
        * Designates a formatter to pretty-print the annotated class.&lt;br /&gt;
        */&lt;br /&gt;
        public @interface PrettyPrinter{&lt;br /&gt;
        Class&amp;lt;? extends Formatter&amp;gt; value();&lt;br /&gt;
        }&lt;br /&gt;
        // Single-member annotation with Class&lt;br /&gt;
        // member restricted by bounded wildcard&lt;br /&gt;
        // The annotation presumes the existence of this class.&lt;br /&gt;
        class GorgeousFormatterimplements Formatter { ... }&lt;br /&gt;
        @PrettyPrinter(GorgeousFormatter.class)&lt;br /&gt;
        public class Petunia { ... }&lt;br /&gt;
&lt;br /&gt;
‘@’ indicates the start of an annotation definition.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Cyang14</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1_1h_jc&amp;diff=73618</id>
		<title>CSC/ECE 517 Spring 2013/ch1 1h jc</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1_1h_jc&amp;diff=73618"/>
		<updated>2013-02-24T16:48:59Z</updated>

		<summary type="html">&lt;p&gt;Cyang14: /* Introduction to reflection */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Metaprogramming in statically typed languages&lt;br /&gt;
=Metaprogramming in statically typed languages=&lt;br /&gt;
==Introduction==&lt;br /&gt;
===What is metaprogramming===&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming] refers to writing programs that can manipulate other programs or themselves. The program that is doing the manipulating is called the ''metaprogram'', and the program that is being manipulated is called the ''object program''. The language of the ''metaprogram'' is referred to as the ''metalanguage'' and the language of the object program is called the ''object language''. A language has the ability of [http://en.wikipedia.org/wiki/Reflection_(computer_programming) reflection] if it can be used to write a metaprogram that can manipulate a program written in the same language.&lt;br /&gt;
&lt;br /&gt;
===Metaprogramming in statically typed languages===&lt;br /&gt;
&lt;br /&gt;
Metaprogramming can be accomplished in both statically and dynamically typed languages, however [http://en.wikipedia.org/wiki/Dynamically_typed_language#Dynamic_typing dynamically typed languages] have some advantages over [http://en.wikipedia.org/wiki/Dynamically_typed_language#Dynamic_typing statically typed languages] when it comes to metaprogramming.&lt;br /&gt;
&lt;br /&gt;
One example of a metaprogramming task is to add a method to a particular class at runtime. Sample code in Ruby to perform this task is outlined below. First is a definition of a Ruby class called TestAddMethodAtRuntime:&lt;br /&gt;
&lt;br /&gt;
  class TestAddMethodAtRuntime&lt;br /&gt;
    def originalMethod&lt;br /&gt;
        puts &amp;quot;Original Method&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The code below created a new instance of the class. After that, it defines a new method called newlyAddedMethod. Then the original method is called, follow by the newly added method at runtime:&lt;br /&gt;
&lt;br /&gt;
  t = TestAddMethodAtRuntime.new&lt;br /&gt;
  def t.newlyAddedMethod&lt;br /&gt;
    puts &amp;quot;Newly Added Method&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  t.originalMethod&lt;br /&gt;
  puts &amp;quot;\n&amp;quot;&lt;br /&gt;
  t.newlyAddedMethod&lt;br /&gt;
&lt;br /&gt;
Below is the output of the code above:&lt;br /&gt;
&lt;br /&gt;
  Original Method&lt;br /&gt;
  Newly Added Method&lt;br /&gt;
&lt;br /&gt;
Accomplishing something like this in a statically typed language would be much more difficult or impossible depending on the language. It is for this reason that dynamically typed languages are usually viewed as being better equipped to handle metaprogramming overall.&lt;br /&gt;
&lt;br /&gt;
==Implementation==&lt;br /&gt;
===Programming Language API===&lt;br /&gt;
&lt;br /&gt;
One way that metaprogramming can be possible in a language is through an [http://en.wikipedia.org/wiki/API API] that helps programmers achieve a metaprogramming task. An example of this is a language that provides an API for reflection. One type of reflection is ''introspection'' in which the program can access the source code of the program itself.&lt;br /&gt;
&lt;br /&gt;
Below is an example from the Java API to illustrate how an API can be used to achieve some metaprogramming tasks:&lt;br /&gt;
&lt;br /&gt;
   import java.lang.reflect.*; &lt;br /&gt;
   public class DumpMethods {&lt;br /&gt;
   public static void main(String args[]) {&lt;br /&gt;
     	try {&lt;br /&gt;
           Class c = Class.forName(args[0]);&lt;br /&gt;
           Method m[] = c.getDeclaredMethods();&lt;br /&gt;
           for (int i = 0; i &amp;lt; m.length; i++) {&lt;br /&gt;
              System.out.println(m[i].toString());&lt;br /&gt;
           }&lt;br /&gt;
     	}&lt;br /&gt;
     	catch (Throwable e) {&lt;br /&gt;
        	System.err.println(e);&lt;br /&gt;
     	}&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
The above example prints out all of the methods that are declared for a particular class. This is an example of metaprogramming since the program is accessing the source code of itself.&lt;br /&gt;
&lt;br /&gt;
===Program transformation system===&lt;br /&gt;
&lt;br /&gt;
A program transformation system is something that takes a program as the input, and outputs a different program. One example of a program transformation system is the Java compiler. The Java compiler takes a Java program as its input, and outputs a .class file with platform independent bytecode in it. Another example of a program transformation system is a Java decompiler, which takes bytecode as an input and outputs a Java program.&lt;br /&gt;
&lt;br /&gt;
In the case of the Java compiler, the program input is being output into another program in a different &amp;quot;language&amp;quot;. There are other types of transformation systems that take a program as an input and output a different program in the same language. One example of this would be ''program migration'' which migrates source code to a newer or older version of the same language. This can be useful if upgrading a software system to run on a newer version of a framework.&lt;br /&gt;
&lt;br /&gt;
==Common Uses==&lt;br /&gt;
===Pre-generate static data at compile time===&lt;br /&gt;
One common use of metaprogramming in statically typed languages is to write programs that will pre-generate tables of data for use at runtime.&amp;lt;ref name=&amp;quot;use1&amp;quot;&amp;gt;Jonathan Bartlett, &amp;quot;The art of metaprogramming, Part 1: Introduction to metaprogramming&amp;quot;, 20 Oct 2005   http://www.ibm.com/developerworks/library/l-metaprog1/index.html#N10052&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One simple but useful code generator is to build static lookup tables. Often, in order to build fast functions in C programming, we simply create a lookup table of all of the answers. This means that we either need to pre-compute them by hand (which is wasteful of your time) or build them at runtime (which is wasteful of the user's time).&lt;br /&gt;
&lt;br /&gt;
In following example we will build a generator that will take a function or set of functions on an integer and build lookup tables for the answer.&lt;br /&gt;
&lt;br /&gt;
To think of how to make such a program, we can start from the end and work backward. Firstly we need a lookup table that will return square roots of numbers between 5 and 20. A simple program can be written to generate such a table like this:&lt;br /&gt;
&lt;br /&gt;
Generate and use a lookup table of square roots&lt;br /&gt;
&lt;br /&gt;
        /* our lookup table */&lt;br /&gt;
        double square_roots[21];&lt;br /&gt;
        /* function to load the table at runtime */&lt;br /&gt;
        void init_square_roots()&lt;br /&gt;
        {&lt;br /&gt;
           int i;&lt;br /&gt;
           for(i = 5; i &amp;lt; 21; i++)&lt;br /&gt;
             {&lt;br /&gt;
               square_roots[i] = sqrt((double)i);&lt;br /&gt;
             }&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
With that single macro, we can take away a lot of work for any program that has to generate mathematical tables indexed by integer. A little extra work would also allow tables containing full struct definitions; a little more would ensure that space isn't wasted at the front of the array with useless empty entries.&lt;br /&gt;
        /* program that uses the table */&lt;br /&gt;
        int main ()&lt;br /&gt;
        {&lt;br /&gt;
           init_square_roots();&lt;br /&gt;
           printf(&amp;quot;The square root of 5 is %f\n&amp;quot;, square_roots[5]);&lt;br /&gt;
           return 0;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
Now, to convert this to a statically initialized array, you would remove the first part of the program and replace it with something like this, calculated by hand:&lt;br /&gt;
&lt;br /&gt;
Square root program with a static lookup table&lt;br /&gt;
&lt;br /&gt;
        double square_roots[] = {&lt;br /&gt;
           /* these are the ones we skipped */ 0.0, 0.0, 0.0, 0.0, 0.0&lt;br /&gt;
           2.236068, /* Square root of 5 */&lt;br /&gt;
           2.449490, /* Square root of 6 */&lt;br /&gt;
           2.645751, /* Square root of 7 */&lt;br /&gt;
           2.828427, /* Square root of 8 */&lt;br /&gt;
           3.0, /* Square root of 9 */&lt;br /&gt;
           ...&lt;br /&gt;
           4.472136 /* Square root of 20 */&lt;br /&gt;
           };&lt;br /&gt;
&lt;br /&gt;
What is needed is a program that will produce these values and print them out in a table like the previous one so they are loaded in at compile-time.&lt;br /&gt;
&lt;br /&gt;
Code generator for the table macro&lt;br /&gt;
&lt;br /&gt;
           #!/usr/bin/perl  &lt;br /&gt;
           #&lt;br /&gt;
           #tablegen.pl &lt;br /&gt;
           #&lt;br /&gt;
           ##Puts each program line into $line&lt;br /&gt;
           while(my $line = &amp;lt;&amp;gt;)&lt;br /&gt;
           {&lt;br /&gt;
             #Is this a macro invocation?&lt;br /&gt;
             if($line =~ m/TABLE:/)&lt;br /&gt;
             {&lt;br /&gt;
                #If so, split it apart into its component pieces&lt;br /&gt;
                my ($dummy, $table_name, $type, $start_idx, $end_idx, $default, $procedure) = split(m/:/, $line, 7);&lt;br /&gt;
                #The main difference between C and Perl for mathematical expressions is that&lt;br /&gt;
                #Perl prefixes its variables with a dollar sign, so we will add that here&lt;br /&gt;
                $procedure =~ s/VAL/\$VAL/g;&lt;br /&gt;
                #Print out the array declaration&lt;br /&gt;
                print &amp;quot;${type} ${table_name} [] = {\n&amp;quot;;&lt;br /&gt;
                #Go through each array element&lt;br /&gt;
                foreach my $VAL (0 .. $end_idx)&lt;br /&gt;
                {&lt;br /&gt;
                   #Only process an answer if we have reached our starting index&lt;br /&gt;
                   if($VAL &amp;gt;= $start_idx)&lt;br /&gt;
                   {&lt;br /&gt;
                      #evaluate the procedure specified (this sets $@ if there are any errors)&lt;br /&gt;
                      $result = eval $procedure;&lt;br /&gt;
                      die(&amp;quot;Error processing: $@&amp;quot;) if $@;&lt;br /&gt;
                   }&lt;br /&gt;
                   else&lt;br /&gt;
                   {&lt;br /&gt;
                      #if we haven't reached the starting index, just use the default&lt;br /&gt;
                      $result = $default;&lt;br /&gt;
                   }&lt;br /&gt;
                   #Print out the value&lt;br /&gt;
                   print &amp;quot;\t${result}&amp;quot;;&lt;br /&gt;
                   #If there are more to be processed, add a comma after the value&lt;br /&gt;
                   if($VAL != $end_idx)&lt;br /&gt;
                   {&lt;br /&gt;
                      print &amp;quot;,&amp;quot;;&lt;br /&gt;
                   }&lt;br /&gt;
                   print &amp;quot;\n&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
                #Finish the declaration&lt;br /&gt;
                print &amp;quot;};\n&amp;quot;;&lt;br /&gt;
             }&lt;br /&gt;
             else&lt;br /&gt;
             {&lt;br /&gt;
                #If this is not a macro invocation, just copy the line directly to the output&lt;br /&gt;
                print $line;&lt;br /&gt;
             }&lt;br /&gt;
           }&lt;br /&gt;
&lt;br /&gt;
To run this program, do this:&lt;br /&gt;
&lt;br /&gt;
Running the code generator&lt;br /&gt;
&lt;br /&gt;
           ./tablegen.pl &amp;lt; sqrt.in &amp;gt; sqrt.c&lt;br /&gt;
           gcc sqrt.c -o sqrt&lt;br /&gt;
           ./a.out&lt;br /&gt;
&lt;br /&gt;
===Mini-language for boiler-plate===&lt;br /&gt;
If you have a large application where many of the functions include a lot of boilerplate code, it is often a good idea to create a mini-language that allows you to work with your boilerplate code in an easier fashion. This mini-language will then be converted into your regular source code language before compiling.&amp;lt;ref name=&amp;quot;use2&amp;quot;&amp;gt;Jonathan Bartlett, &amp;quot;The art of metaprogramming, Part 2: Metaprogramming using Scheme&amp;quot;, 02 May 2006  http://www.ibm.com/developerworks/linux/library/l-metaprog2/index.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following is an example:&lt;br /&gt;
&lt;br /&gt;
Let's say that we are building a CGI application consisting of many independent CGI scripts. In most CGI applications, much of the state is stored in a database, but only a session ID is passed to each script via a cookie.&lt;br /&gt;
&lt;br /&gt;
However, in nearly every page we need to know the other standard information (such as the username, group number, the current job being worked on, whatever else information is pertinent). In addition, we need to redirect the user if they do not have an appropriate cookie.&lt;br /&gt;
&lt;br /&gt;
            (define (handle-cgi-request req)&lt;br /&gt;
              (let (&lt;br /&gt;
                     (session-id (webserver:cookie req &amp;quot;sessionid&amp;quot;)))&lt;br /&gt;
                (if (not (webserver:valid-session-id session-id))&lt;br /&gt;
                    (webserver:redirect-to-login-page)&lt;br /&gt;
                    (let (&lt;br /&gt;
                          (username (webserver:username-for-session session-id))&lt;br /&gt;
                          (group (webserver:group-for-user username))&lt;br /&gt;
                          (current-job (webserver:current-job-for-user username)))&lt;br /&gt;
                       ;;Code for processing goes here&lt;br /&gt;
                       ))))&lt;br /&gt;
&lt;br /&gt;
While some of that can be handled by a procedure, the bindings certainly cannot. However, we can turn most of it into a macro. The macro can be implemented like this:&lt;br /&gt;
&lt;br /&gt;
            (define-syntax cgi-boilerplate&lt;br /&gt;
              (lambda (x)&lt;br /&gt;
                (syntax-case x ()&lt;br /&gt;
                 (&lt;br /&gt;
                   (cgi-boilerplate expr)&lt;br /&gt;
                   (datum-&amp;gt;syntax-object&lt;br /&gt;
                     (syntax k)&lt;br /&gt;
                      (let (&lt;br /&gt;
                            (session-id (webserver:cookie req &amp;quot;sessionid&amp;quot;)))&lt;br /&gt;
                           (if (not (webserver:valid-session-id session-id))&lt;br /&gt;
                               (webserver:redirect-to-login-page)&lt;br /&gt;
                               (let (&lt;br /&gt;
                                      (username (webserver:username-for-session session-id))&lt;br /&gt;
                                      (group (webserver:group-for-user username))&lt;br /&gt;
                                      (current-job (webserver:current-job-for-user username)))&lt;br /&gt;
                                      (syntax-object-&amp;gt;datum (syntax expr))))))&lt;br /&gt;
                  )&lt;br /&gt;
             )))&lt;br /&gt;
&lt;br /&gt;
We can now create new forms based on our boilerplate code by doing the following:&lt;br /&gt;
&lt;br /&gt;
            (define (handle-cgi-request req)&lt;br /&gt;
              (cgi-boilerplate&lt;br /&gt;
               (begin&lt;br /&gt;
                 ;;Do whatever I want here&lt;br /&gt;
                 )))&lt;br /&gt;
&lt;br /&gt;
In addition, since we are not defining our variables explicitly, adding new variable definitions to our boilerplate won't affect its calling conventions, so new features can be added without having to create a whole new function.&lt;br /&gt;
&lt;br /&gt;
In any large project, there are inevitably templates to follow which cannot be reduced to functions, usually because of the bindings being created. Using boilerplate macros can make maintenance of such templated code much easier.&lt;br /&gt;
&lt;br /&gt;
Likewise, other standard macros can be created which make use of variables defined in the boilerplate. Using macros like this significantly reduces typing because you do not have to constantly be writing and rewriting variable bindings, derivations, and parameter passing. This also reduces the potential for errors in such code.&lt;br /&gt;
&lt;br /&gt;
Realize though that boilerplate macros are not a panacea. There are many significant problems that can occur, including:&lt;br /&gt;
*  Accidentally overwriting bindings by introducing a variable name that was previously defined in a macro.&lt;br /&gt;
*  Difficulty tracing problems because the inputs and the outputs of the macros are implicit, not explicit.&lt;br /&gt;
&lt;br /&gt;
These can be largely avoided by doing a few things in conjunction with your boilerplate macros:&lt;br /&gt;
*  Have a naming convention which clearly labels macros as such, as well as indicate that a variable came from boilerplate code. This could be done by affixing -m to macros and -b to variables defined within a boilerplate.&lt;br /&gt;
*  Carefully document all boilerplate macros, especially the introduced variable bindings and all changes between versions.&lt;br /&gt;
*  Only use boilerplate macros when the savings in repetitiveness clearly outweigh the negatives of implicit functionality.&lt;br /&gt;
&lt;br /&gt;
===Abbreviate statements and prevent mistakes===&lt;br /&gt;
A lot of programming languages make you write really verbose statements to do really simple things. Code-generating programs allow you to abbreviate such statements and save a lot of typing, which also prevents a lot of mistakes because there is less chance of mistyping.&lt;br /&gt;
&lt;br /&gt;
=Metaprogramming Framework in Java=&lt;br /&gt;
==Reflection==&lt;br /&gt;
===Introduction to reflection===&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_(computer_programming) Reflection] is the ability to introspect metalevel information about the program structure itself at runtime. Usually this metalevel information is modeled using the general abstraction mechanisms available in the language. In Java, reflection enables to discover information about the loaded classes:&lt;br /&gt;
*  Fields,&lt;br /&gt;
*  Methods, and&lt;br /&gt;
*  Constructors&lt;br /&gt;
*  Generics information&lt;br /&gt;
*  Metadata annotations&lt;br /&gt;
It also enables to use these metaobjects to their instances in runtime environment.&amp;lt;ref name=&amp;quot;reflection&amp;quot;&amp;gt;Abdelmonaim Remani, &amp;quot;The Art of Metaprogramming in Java&amp;quot;, Jul 19, 2012  http://www.slideshare.net/PolymathicCoder/the-art-of-metaprogramming-in-java&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following is the Java metalevel architecture&amp;lt;ref name=&amp;quot;reflection&amp;quot;&amp;gt;Mika Haapakorpi, &amp;quot;Meta Programming In Java&amp;quot;&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Metalevel architecture.PNG]]&lt;br /&gt;
&lt;br /&gt;
===Dynamic proxy classes===&lt;br /&gt;
A dynamic proxy class implements a list of interfaces specified at runtime when the class is created.&lt;br /&gt;
*  A proxy interface is an interface that is implemented by a proxy class.&lt;br /&gt;
*  A proxy instance is an instance of a proxy class which has an associated invocation handler object.&lt;br /&gt;
The following is an example of dynamic proxy classes&lt;br /&gt;
&lt;br /&gt;
[[File:Dynamic_proxy_class.PNG]]&lt;br /&gt;
&lt;br /&gt;
==Generics==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Generics_in_Java Generics] are a facility of generic programming that was added to the Java programming language in 2004 as part of J2SE 5.0. They allow &amp;quot;a type or method to operate on objects of various types while providing compile-time type safety.&amp;quot;  The generics can be used in classes, interfaces, methods and constructors.&amp;lt;ref name=&amp;quot;reflection&amp;quot;&amp;gt;Mika Haapakorpi, &amp;quot;Meta Programming In Java&amp;quot;&amp;lt;/ref&amp;gt;&lt;br /&gt;
===Two new types in generics===&lt;br /&gt;
Two new types in generics:&lt;br /&gt;
*  Parametrized types&lt;br /&gt;
*  Type variables&lt;br /&gt;
&lt;br /&gt;
A type variable is an unqualified identifier. Class and interface declarations can have type arguments (type variables). Method and constructors definitions can have type arguments (type variables). &lt;br /&gt;
&lt;br /&gt;
The following is an example for type variables&lt;br /&gt;
&lt;br /&gt;
       List&amp;lt;String&amp;gt; anExample = new ArrayList&amp;lt;String&amp;gt;()&lt;br /&gt;
&lt;br /&gt;
*  List : interface&lt;br /&gt;
*  ArrayList : class&lt;br /&gt;
*  String : class (the actual type argument)&lt;br /&gt;
*  List&amp;lt;String&amp;gt; and ArrayList&amp;lt;String&amp;gt; : parametrized type&lt;br /&gt;
&lt;br /&gt;
===Class/Interface Declarations===&lt;br /&gt;
The following is an example of class declaration:&lt;br /&gt;
&lt;br /&gt;
       public interface List&amp;lt;E&amp;gt; {&lt;br /&gt;
         void add(Ex);&lt;br /&gt;
         Iterator&amp;lt;E&amp;gt; iterator();&lt;br /&gt;
       }&lt;br /&gt;
       public interface Iterator&amp;lt;E&amp;gt; {&lt;br /&gt;
         E next();&lt;br /&gt;
         booleanhasNext();&lt;br /&gt;
       }&lt;br /&gt;
       List&amp;lt;String&amp;gt; anExample;&lt;br /&gt;
       anExample.add(”sdfdfss”);&lt;br /&gt;
       anExample.add(new Object()); // compile time error&lt;br /&gt;
       String aTest = anExample.iterator().next();&lt;br /&gt;
&lt;br /&gt;
==Metadata annotation==&lt;br /&gt;
An annotation, in the Java computer programming language, is a form of syntactic metadata that can be added to Java source code. Classes, methods, variables, parameters and packages may be annotated.&amp;lt;ref name=&amp;quot;reflection&amp;quot;&amp;gt;Mika Haapakorpi, &amp;quot;Meta Programming In Java&amp;quot;&amp;lt;/ref&amp;gt; &lt;br /&gt;
&lt;br /&gt;
The following is an example of annotation:&lt;br /&gt;
&lt;br /&gt;
        /**&lt;br /&gt;
        * Designates a formatter to pretty-print the annotated class.&lt;br /&gt;
        */&lt;br /&gt;
        public @interface PrettyPrinter{&lt;br /&gt;
        Class&amp;lt;? extends Formatter&amp;gt; value();&lt;br /&gt;
        }&lt;br /&gt;
        // Single-member annotation with Class&lt;br /&gt;
        // member restricted by bounded wildcard&lt;br /&gt;
        // The annotation presumes the existence of this class.&lt;br /&gt;
        class GorgeousFormatterimplements Formatter { ... }&lt;br /&gt;
        @PrettyPrinter(GorgeousFormatter.class)&lt;br /&gt;
        public class Petunia { ... }&lt;br /&gt;
&lt;br /&gt;
‘@’ indicates the start of an annotation definition.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Cyang14</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1_1h_jc&amp;diff=73617</id>
		<title>CSC/ECE 517 Spring 2013/ch1 1h jc</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1_1h_jc&amp;diff=73617"/>
		<updated>2013-02-24T16:46:48Z</updated>

		<summary type="html">&lt;p&gt;Cyang14: /* Metadata annotation */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Metaprogramming in statically typed languages&lt;br /&gt;
=Metaprogramming in statically typed languages=&lt;br /&gt;
==Introduction==&lt;br /&gt;
===What is metaprogramming===&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming] refers to writing programs that can manipulate other programs or themselves. The program that is doing the manipulating is called the ''metaprogram'', and the program that is being manipulated is called the ''object program''. The language of the ''metaprogram'' is referred to as the ''metalanguage'' and the language of the object program is called the ''object language''. A language has the ability of [http://en.wikipedia.org/wiki/Reflection_(computer_programming) reflection] if it can be used to write a metaprogram that can manipulate a program written in the same language.&lt;br /&gt;
&lt;br /&gt;
===Metaprogramming in statically typed languages===&lt;br /&gt;
&lt;br /&gt;
Metaprogramming can be accomplished in both statically and dynamically typed languages, however [http://en.wikipedia.org/wiki/Dynamically_typed_language#Dynamic_typing dynamically typed languages] have some advantages over [http://en.wikipedia.org/wiki/Dynamically_typed_language#Dynamic_typing statically typed languages] when it comes to metaprogramming.&lt;br /&gt;
&lt;br /&gt;
One example of a metaprogramming task is to add a method to a particular class at runtime. Sample code in Ruby to perform this task is outlined below. First is a definition of a Ruby class called TestAddMethodAtRuntime:&lt;br /&gt;
&lt;br /&gt;
  class TestAddMethodAtRuntime&lt;br /&gt;
    def originalMethod&lt;br /&gt;
        puts &amp;quot;Original Method&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The code below created a new instance of the class. After that, it defines a new method called newlyAddedMethod. Then the original method is called, follow by the newly added method at runtime:&lt;br /&gt;
&lt;br /&gt;
  t = TestAddMethodAtRuntime.new&lt;br /&gt;
  def t.newlyAddedMethod&lt;br /&gt;
    puts &amp;quot;Newly Added Method&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  t.originalMethod&lt;br /&gt;
  puts &amp;quot;\n&amp;quot;&lt;br /&gt;
  t.newlyAddedMethod&lt;br /&gt;
&lt;br /&gt;
Below is the output of the code above:&lt;br /&gt;
&lt;br /&gt;
  Original Method&lt;br /&gt;
  Newly Added Method&lt;br /&gt;
&lt;br /&gt;
Accomplishing something like this in a statically typed language would be much more difficult or impossible depending on the language. It is for this reason that dynamically typed languages are usually viewed as being better equipped to handle metaprogramming overall.&lt;br /&gt;
&lt;br /&gt;
==Implementation==&lt;br /&gt;
===Programming Language API===&lt;br /&gt;
&lt;br /&gt;
One way that metaprogramming can be possible in a language is through an [http://en.wikipedia.org/wiki/API API] that helps programmers achieve a metaprogramming task. An example of this is a language that provides an API for reflection. One type of reflection is ''introspection'' in which the program can access the source code of the program itself.&lt;br /&gt;
&lt;br /&gt;
Below is an example from the Java API to illustrate how an API can be used to achieve some metaprogramming tasks:&lt;br /&gt;
&lt;br /&gt;
   import java.lang.reflect.*; &lt;br /&gt;
   public class DumpMethods {&lt;br /&gt;
   public static void main(String args[]) {&lt;br /&gt;
     	try {&lt;br /&gt;
           Class c = Class.forName(args[0]);&lt;br /&gt;
           Method m[] = c.getDeclaredMethods();&lt;br /&gt;
           for (int i = 0; i &amp;lt; m.length; i++) {&lt;br /&gt;
              System.out.println(m[i].toString());&lt;br /&gt;
           }&lt;br /&gt;
     	}&lt;br /&gt;
     	catch (Throwable e) {&lt;br /&gt;
        	System.err.println(e);&lt;br /&gt;
     	}&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
The above example prints out all of the methods that are declared for a particular class. This is an example of metaprogramming since the program is accessing the source code of itself.&lt;br /&gt;
&lt;br /&gt;
===Program transformation system===&lt;br /&gt;
&lt;br /&gt;
A program transformation system is something that takes a program as the input, and outputs a different program. One example of a program transformation system is the Java compiler. The Java compiler takes a Java program as its input, and outputs a .class file with platform independent bytecode in it. Another example of a program transformation system is a Java decompiler, which takes bytecode as an input and outputs a Java program.&lt;br /&gt;
&lt;br /&gt;
In the case of the Java compiler, the program input is being output into another program in a different &amp;quot;language&amp;quot;. There are other types of transformation systems that take a program as an input and output a different program in the same language. One example of this would be ''program migration'' which migrates source code to a newer or older version of the same language. This can be useful if upgrading a software system to run on a newer version of a framework.&lt;br /&gt;
&lt;br /&gt;
==Common Uses==&lt;br /&gt;
===Pre-generate static data at compile time===&lt;br /&gt;
One common use of metaprogramming in statically typed languages is to write programs that will pre-generate tables of data for use at runtime.&amp;lt;ref name=&amp;quot;use1&amp;quot;&amp;gt;Jonathan Bartlett, &amp;quot;The art of metaprogramming, Part 1: Introduction to metaprogramming&amp;quot;, 20 Oct 2005   http://www.ibm.com/developerworks/library/l-metaprog1/index.html#N10052&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One simple but useful code generator is to build static lookup tables. Often, in order to build fast functions in C programming, we simply create a lookup table of all of the answers. This means that we either need to pre-compute them by hand (which is wasteful of your time) or build them at runtime (which is wasteful of the user's time).&lt;br /&gt;
&lt;br /&gt;
In following example we will build a generator that will take a function or set of functions on an integer and build lookup tables for the answer.&lt;br /&gt;
&lt;br /&gt;
To think of how to make such a program, we can start from the end and work backward. Firstly we need a lookup table that will return square roots of numbers between 5 and 20. A simple program can be written to generate such a table like this:&lt;br /&gt;
&lt;br /&gt;
Generate and use a lookup table of square roots&lt;br /&gt;
&lt;br /&gt;
        /* our lookup table */&lt;br /&gt;
        double square_roots[21];&lt;br /&gt;
        /* function to load the table at runtime */&lt;br /&gt;
        void init_square_roots()&lt;br /&gt;
        {&lt;br /&gt;
           int i;&lt;br /&gt;
           for(i = 5; i &amp;lt; 21; i++)&lt;br /&gt;
             {&lt;br /&gt;
               square_roots[i] = sqrt((double)i);&lt;br /&gt;
             }&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
With that single macro, we can take away a lot of work for any program that has to generate mathematical tables indexed by integer. A little extra work would also allow tables containing full struct definitions; a little more would ensure that space isn't wasted at the front of the array with useless empty entries.&lt;br /&gt;
        /* program that uses the table */&lt;br /&gt;
        int main ()&lt;br /&gt;
        {&lt;br /&gt;
           init_square_roots();&lt;br /&gt;
           printf(&amp;quot;The square root of 5 is %f\n&amp;quot;, square_roots[5]);&lt;br /&gt;
           return 0;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
Now, to convert this to a statically initialized array, you would remove the first part of the program and replace it with something like this, calculated by hand:&lt;br /&gt;
&lt;br /&gt;
Square root program with a static lookup table&lt;br /&gt;
&lt;br /&gt;
        double square_roots[] = {&lt;br /&gt;
           /* these are the ones we skipped */ 0.0, 0.0, 0.0, 0.0, 0.0&lt;br /&gt;
           2.236068, /* Square root of 5 */&lt;br /&gt;
           2.449490, /* Square root of 6 */&lt;br /&gt;
           2.645751, /* Square root of 7 */&lt;br /&gt;
           2.828427, /* Square root of 8 */&lt;br /&gt;
           3.0, /* Square root of 9 */&lt;br /&gt;
           ...&lt;br /&gt;
           4.472136 /* Square root of 20 */&lt;br /&gt;
           };&lt;br /&gt;
&lt;br /&gt;
What is needed is a program that will produce these values and print them out in a table like the previous one so they are loaded in at compile-time.&lt;br /&gt;
&lt;br /&gt;
Code generator for the table macro&lt;br /&gt;
&lt;br /&gt;
           #!/usr/bin/perl  &lt;br /&gt;
           #&lt;br /&gt;
           #tablegen.pl &lt;br /&gt;
           #&lt;br /&gt;
           ##Puts each program line into $line&lt;br /&gt;
           while(my $line = &amp;lt;&amp;gt;)&lt;br /&gt;
           {&lt;br /&gt;
             #Is this a macro invocation?&lt;br /&gt;
             if($line =~ m/TABLE:/)&lt;br /&gt;
             {&lt;br /&gt;
                #If so, split it apart into its component pieces&lt;br /&gt;
                my ($dummy, $table_name, $type, $start_idx, $end_idx, $default, $procedure) = split(m/:/, $line, 7);&lt;br /&gt;
                #The main difference between C and Perl for mathematical expressions is that&lt;br /&gt;
                #Perl prefixes its variables with a dollar sign, so we will add that here&lt;br /&gt;
                $procedure =~ s/VAL/\$VAL/g;&lt;br /&gt;
                #Print out the array declaration&lt;br /&gt;
                print &amp;quot;${type} ${table_name} [] = {\n&amp;quot;;&lt;br /&gt;
                #Go through each array element&lt;br /&gt;
                foreach my $VAL (0 .. $end_idx)&lt;br /&gt;
                {&lt;br /&gt;
                   #Only process an answer if we have reached our starting index&lt;br /&gt;
                   if($VAL &amp;gt;= $start_idx)&lt;br /&gt;
                   {&lt;br /&gt;
                      #evaluate the procedure specified (this sets $@ if there are any errors)&lt;br /&gt;
                      $result = eval $procedure;&lt;br /&gt;
                      die(&amp;quot;Error processing: $@&amp;quot;) if $@;&lt;br /&gt;
                   }&lt;br /&gt;
                   else&lt;br /&gt;
                   {&lt;br /&gt;
                      #if we haven't reached the starting index, just use the default&lt;br /&gt;
                      $result = $default;&lt;br /&gt;
                   }&lt;br /&gt;
                   #Print out the value&lt;br /&gt;
                   print &amp;quot;\t${result}&amp;quot;;&lt;br /&gt;
                   #If there are more to be processed, add a comma after the value&lt;br /&gt;
                   if($VAL != $end_idx)&lt;br /&gt;
                   {&lt;br /&gt;
                      print &amp;quot;,&amp;quot;;&lt;br /&gt;
                   }&lt;br /&gt;
                   print &amp;quot;\n&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
                #Finish the declaration&lt;br /&gt;
                print &amp;quot;};\n&amp;quot;;&lt;br /&gt;
             }&lt;br /&gt;
             else&lt;br /&gt;
             {&lt;br /&gt;
                #If this is not a macro invocation, just copy the line directly to the output&lt;br /&gt;
                print $line;&lt;br /&gt;
             }&lt;br /&gt;
           }&lt;br /&gt;
&lt;br /&gt;
To run this program, do this:&lt;br /&gt;
&lt;br /&gt;
Running the code generator&lt;br /&gt;
&lt;br /&gt;
           ./tablegen.pl &amp;lt; sqrt.in &amp;gt; sqrt.c&lt;br /&gt;
           gcc sqrt.c -o sqrt&lt;br /&gt;
           ./a.out&lt;br /&gt;
&lt;br /&gt;
===Mini-language for boiler-plate===&lt;br /&gt;
If you have a large application where many of the functions include a lot of boilerplate code, it is often a good idea to create a mini-language that allows you to work with your boilerplate code in an easier fashion. This mini-language will then be converted into your regular source code language before compiling.&amp;lt;ref name=&amp;quot;use2&amp;quot;&amp;gt;Jonathan Bartlett, &amp;quot;The art of metaprogramming, Part 2: Metaprogramming using Scheme&amp;quot;, 02 May 2006  http://www.ibm.com/developerworks/linux/library/l-metaprog2/index.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following is an example:&lt;br /&gt;
&lt;br /&gt;
Let's say that we are building a CGI application consisting of many independent CGI scripts. In most CGI applications, much of the state is stored in a database, but only a session ID is passed to each script via a cookie.&lt;br /&gt;
&lt;br /&gt;
However, in nearly every page we need to know the other standard information (such as the username, group number, the current job being worked on, whatever else information is pertinent). In addition, we need to redirect the user if they do not have an appropriate cookie.&lt;br /&gt;
&lt;br /&gt;
            (define (handle-cgi-request req)&lt;br /&gt;
              (let (&lt;br /&gt;
                     (session-id (webserver:cookie req &amp;quot;sessionid&amp;quot;)))&lt;br /&gt;
                (if (not (webserver:valid-session-id session-id))&lt;br /&gt;
                    (webserver:redirect-to-login-page)&lt;br /&gt;
                    (let (&lt;br /&gt;
                          (username (webserver:username-for-session session-id))&lt;br /&gt;
                          (group (webserver:group-for-user username))&lt;br /&gt;
                          (current-job (webserver:current-job-for-user username)))&lt;br /&gt;
                       ;;Code for processing goes here&lt;br /&gt;
                       ))))&lt;br /&gt;
&lt;br /&gt;
While some of that can be handled by a procedure, the bindings certainly cannot. However, we can turn most of it into a macro. The macro can be implemented like this:&lt;br /&gt;
&lt;br /&gt;
            (define-syntax cgi-boilerplate&lt;br /&gt;
              (lambda (x)&lt;br /&gt;
                (syntax-case x ()&lt;br /&gt;
                 (&lt;br /&gt;
                   (cgi-boilerplate expr)&lt;br /&gt;
                   (datum-&amp;gt;syntax-object&lt;br /&gt;
                     (syntax k)&lt;br /&gt;
                      (let (&lt;br /&gt;
                            (session-id (webserver:cookie req &amp;quot;sessionid&amp;quot;)))&lt;br /&gt;
                           (if (not (webserver:valid-session-id session-id))&lt;br /&gt;
                               (webserver:redirect-to-login-page)&lt;br /&gt;
                               (let (&lt;br /&gt;
                                      (username (webserver:username-for-session session-id))&lt;br /&gt;
                                      (group (webserver:group-for-user username))&lt;br /&gt;
                                      (current-job (webserver:current-job-for-user username)))&lt;br /&gt;
                                      (syntax-object-&amp;gt;datum (syntax expr))))))&lt;br /&gt;
                  )&lt;br /&gt;
             )))&lt;br /&gt;
&lt;br /&gt;
We can now create new forms based on our boilerplate code by doing the following:&lt;br /&gt;
&lt;br /&gt;
            (define (handle-cgi-request req)&lt;br /&gt;
              (cgi-boilerplate&lt;br /&gt;
               (begin&lt;br /&gt;
                 ;;Do whatever I want here&lt;br /&gt;
                 )))&lt;br /&gt;
&lt;br /&gt;
In addition, since we are not defining our variables explicitly, adding new variable definitions to our boilerplate won't affect its calling conventions, so new features can be added without having to create a whole new function.&lt;br /&gt;
&lt;br /&gt;
In any large project, there are inevitably templates to follow which cannot be reduced to functions, usually because of the bindings being created. Using boilerplate macros can make maintenance of such templated code much easier.&lt;br /&gt;
&lt;br /&gt;
Likewise, other standard macros can be created which make use of variables defined in the boilerplate. Using macros like this significantly reduces typing because you do not have to constantly be writing and rewriting variable bindings, derivations, and parameter passing. This also reduces the potential for errors in such code.&lt;br /&gt;
&lt;br /&gt;
Realize though that boilerplate macros are not a panacea. There are many significant problems that can occur, including:&lt;br /&gt;
*  Accidentally overwriting bindings by introducing a variable name that was previously defined in a macro.&lt;br /&gt;
*  Difficulty tracing problems because the inputs and the outputs of the macros are implicit, not explicit.&lt;br /&gt;
&lt;br /&gt;
These can be largely avoided by doing a few things in conjunction with your boilerplate macros:&lt;br /&gt;
*  Have a naming convention which clearly labels macros as such, as well as indicate that a variable came from boilerplate code. This could be done by affixing -m to macros and -b to variables defined within a boilerplate.&lt;br /&gt;
*  Carefully document all boilerplate macros, especially the introduced variable bindings and all changes between versions.&lt;br /&gt;
*  Only use boilerplate macros when the savings in repetitiveness clearly outweigh the negatives of implicit functionality.&lt;br /&gt;
&lt;br /&gt;
===Abbreviate statements and prevent mistakes===&lt;br /&gt;
A lot of programming languages make you write really verbose statements to do really simple things. Code-generating programs allow you to abbreviate such statements and save a lot of typing, which also prevents a lot of mistakes because there is less chance of mistyping.&lt;br /&gt;
&lt;br /&gt;
=Metaprogramming Framework in Java=&lt;br /&gt;
==Reflection==&lt;br /&gt;
===Introduction to reflection===&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_(computer_programming) Reflection] is the ability to introspect metalevel information about the program structure itself at runtime. Usually this metalevel information is modeled using the general abstraction mechanisms available in the language. In Java, reflection enables to discover information about the loaded classes:&lt;br /&gt;
*  Fields,&lt;br /&gt;
*  Methods, and&lt;br /&gt;
*  Constructors&lt;br /&gt;
*  Generics information&lt;br /&gt;
*  Metadata annotations&lt;br /&gt;
It also enables to use these metaobjects to their instances in runtime environment.&lt;br /&gt;
&lt;br /&gt;
The following is the Java metalevel architecture&amp;lt;ref name=&amp;quot;reflection&amp;quot;&amp;gt;Mika Haapakorpi, &amp;quot;Meta Programming In Java&amp;quot;&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Metalevel architecture.PNG]]&lt;br /&gt;
&lt;br /&gt;
===Dynamic proxy classes===&lt;br /&gt;
A dynamic proxy class implements a list of interfaces specified at runtime when the class is created.&lt;br /&gt;
*  A proxy interface is an interface that is implemented by a proxy class.&lt;br /&gt;
*  A proxy instance is an instance of a proxy class which has an associated invocation handler object.&lt;br /&gt;
The following is an example of dynamic proxy classes&lt;br /&gt;
&lt;br /&gt;
[[File:Dynamic_proxy_class.PNG]]&lt;br /&gt;
&lt;br /&gt;
==Generics==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Generics_in_Java Generics] are a facility of generic programming that was added to the Java programming language in 2004 as part of J2SE 5.0. They allow &amp;quot;a type or method to operate on objects of various types while providing compile-time type safety.&amp;quot;  The generics can be used in classes, interfaces, methods and constructors.&amp;lt;ref name=&amp;quot;reflection&amp;quot;&amp;gt;Mika Haapakorpi, &amp;quot;Meta Programming In Java&amp;quot;&amp;lt;/ref&amp;gt;&lt;br /&gt;
===Two new types in generics===&lt;br /&gt;
Two new types in generics:&lt;br /&gt;
*  Parametrized types&lt;br /&gt;
*  Type variables&lt;br /&gt;
&lt;br /&gt;
A type variable is an unqualified identifier. Class and interface declarations can have type arguments (type variables). Method and constructors definitions can have type arguments (type variables). &lt;br /&gt;
&lt;br /&gt;
The following is an example for type variables&lt;br /&gt;
&lt;br /&gt;
       List&amp;lt;String&amp;gt; anExample = new ArrayList&amp;lt;String&amp;gt;()&lt;br /&gt;
&lt;br /&gt;
*  List : interface&lt;br /&gt;
*  ArrayList : class&lt;br /&gt;
*  String : class (the actual type argument)&lt;br /&gt;
*  List&amp;lt;String&amp;gt; and ArrayList&amp;lt;String&amp;gt; : parametrized type&lt;br /&gt;
&lt;br /&gt;
===Class/Interface Declarations===&lt;br /&gt;
The following is an example of class declaration:&lt;br /&gt;
&lt;br /&gt;
       public interface List&amp;lt;E&amp;gt; {&lt;br /&gt;
         void add(Ex);&lt;br /&gt;
         Iterator&amp;lt;E&amp;gt; iterator();&lt;br /&gt;
       }&lt;br /&gt;
       public interface Iterator&amp;lt;E&amp;gt; {&lt;br /&gt;
         E next();&lt;br /&gt;
         booleanhasNext();&lt;br /&gt;
       }&lt;br /&gt;
       List&amp;lt;String&amp;gt; anExample;&lt;br /&gt;
       anExample.add(”sdfdfss”);&lt;br /&gt;
       anExample.add(new Object()); // compile time error&lt;br /&gt;
       String aTest = anExample.iterator().next();&lt;br /&gt;
&lt;br /&gt;
==Metadata annotation==&lt;br /&gt;
An annotation, in the Java computer programming language, is a form of syntactic metadata that can be added to Java source code. Classes, methods, variables, parameters and packages may be annotated.&amp;lt;ref name=&amp;quot;reflection&amp;quot;&amp;gt;Mika Haapakorpi, &amp;quot;Meta Programming In Java&amp;quot;&amp;lt;/ref&amp;gt; &lt;br /&gt;
&lt;br /&gt;
The following is an example of annotation:&lt;br /&gt;
&lt;br /&gt;
        /**&lt;br /&gt;
        * Designates a formatter to pretty-print the annotated class.&lt;br /&gt;
        */&lt;br /&gt;
        public @interface PrettyPrinter{&lt;br /&gt;
        Class&amp;lt;? extends Formatter&amp;gt; value();&lt;br /&gt;
        }&lt;br /&gt;
        // Single-member annotation with Class&lt;br /&gt;
        // member restricted by bounded wildcard&lt;br /&gt;
        // The annotation presumes the existence of this class.&lt;br /&gt;
        class GorgeousFormatterimplements Formatter { ... }&lt;br /&gt;
        @PrettyPrinter(GorgeousFormatter.class)&lt;br /&gt;
        public class Petunia { ... }&lt;br /&gt;
&lt;br /&gt;
‘@’ indicates the start of an annotation definition.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Cyang14</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1_1h_jc&amp;diff=73616</id>
		<title>CSC/ECE 517 Spring 2013/ch1 1h jc</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1_1h_jc&amp;diff=73616"/>
		<updated>2013-02-24T16:46:26Z</updated>

		<summary type="html">&lt;p&gt;Cyang14: /* Generics */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Metaprogramming in statically typed languages&lt;br /&gt;
=Metaprogramming in statically typed languages=&lt;br /&gt;
==Introduction==&lt;br /&gt;
===What is metaprogramming===&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming] refers to writing programs that can manipulate other programs or themselves. The program that is doing the manipulating is called the ''metaprogram'', and the program that is being manipulated is called the ''object program''. The language of the ''metaprogram'' is referred to as the ''metalanguage'' and the language of the object program is called the ''object language''. A language has the ability of [http://en.wikipedia.org/wiki/Reflection_(computer_programming) reflection] if it can be used to write a metaprogram that can manipulate a program written in the same language.&lt;br /&gt;
&lt;br /&gt;
===Metaprogramming in statically typed languages===&lt;br /&gt;
&lt;br /&gt;
Metaprogramming can be accomplished in both statically and dynamically typed languages, however [http://en.wikipedia.org/wiki/Dynamically_typed_language#Dynamic_typing dynamically typed languages] have some advantages over [http://en.wikipedia.org/wiki/Dynamically_typed_language#Dynamic_typing statically typed languages] when it comes to metaprogramming.&lt;br /&gt;
&lt;br /&gt;
One example of a metaprogramming task is to add a method to a particular class at runtime. Sample code in Ruby to perform this task is outlined below. First is a definition of a Ruby class called TestAddMethodAtRuntime:&lt;br /&gt;
&lt;br /&gt;
  class TestAddMethodAtRuntime&lt;br /&gt;
    def originalMethod&lt;br /&gt;
        puts &amp;quot;Original Method&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The code below created a new instance of the class. After that, it defines a new method called newlyAddedMethod. Then the original method is called, follow by the newly added method at runtime:&lt;br /&gt;
&lt;br /&gt;
  t = TestAddMethodAtRuntime.new&lt;br /&gt;
  def t.newlyAddedMethod&lt;br /&gt;
    puts &amp;quot;Newly Added Method&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  t.originalMethod&lt;br /&gt;
  puts &amp;quot;\n&amp;quot;&lt;br /&gt;
  t.newlyAddedMethod&lt;br /&gt;
&lt;br /&gt;
Below is the output of the code above:&lt;br /&gt;
&lt;br /&gt;
  Original Method&lt;br /&gt;
  Newly Added Method&lt;br /&gt;
&lt;br /&gt;
Accomplishing something like this in a statically typed language would be much more difficult or impossible depending on the language. It is for this reason that dynamically typed languages are usually viewed as being better equipped to handle metaprogramming overall.&lt;br /&gt;
&lt;br /&gt;
==Implementation==&lt;br /&gt;
===Programming Language API===&lt;br /&gt;
&lt;br /&gt;
One way that metaprogramming can be possible in a language is through an [http://en.wikipedia.org/wiki/API API] that helps programmers achieve a metaprogramming task. An example of this is a language that provides an API for reflection. One type of reflection is ''introspection'' in which the program can access the source code of the program itself.&lt;br /&gt;
&lt;br /&gt;
Below is an example from the Java API to illustrate how an API can be used to achieve some metaprogramming tasks:&lt;br /&gt;
&lt;br /&gt;
   import java.lang.reflect.*; &lt;br /&gt;
   public class DumpMethods {&lt;br /&gt;
   public static void main(String args[]) {&lt;br /&gt;
     	try {&lt;br /&gt;
           Class c = Class.forName(args[0]);&lt;br /&gt;
           Method m[] = c.getDeclaredMethods();&lt;br /&gt;
           for (int i = 0; i &amp;lt; m.length; i++) {&lt;br /&gt;
              System.out.println(m[i].toString());&lt;br /&gt;
           }&lt;br /&gt;
     	}&lt;br /&gt;
     	catch (Throwable e) {&lt;br /&gt;
        	System.err.println(e);&lt;br /&gt;
     	}&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
The above example prints out all of the methods that are declared for a particular class. This is an example of metaprogramming since the program is accessing the source code of itself.&lt;br /&gt;
&lt;br /&gt;
===Program transformation system===&lt;br /&gt;
&lt;br /&gt;
A program transformation system is something that takes a program as the input, and outputs a different program. One example of a program transformation system is the Java compiler. The Java compiler takes a Java program as its input, and outputs a .class file with platform independent bytecode in it. Another example of a program transformation system is a Java decompiler, which takes bytecode as an input and outputs a Java program.&lt;br /&gt;
&lt;br /&gt;
In the case of the Java compiler, the program input is being output into another program in a different &amp;quot;language&amp;quot;. There are other types of transformation systems that take a program as an input and output a different program in the same language. One example of this would be ''program migration'' which migrates source code to a newer or older version of the same language. This can be useful if upgrading a software system to run on a newer version of a framework.&lt;br /&gt;
&lt;br /&gt;
==Common Uses==&lt;br /&gt;
===Pre-generate static data at compile time===&lt;br /&gt;
One common use of metaprogramming in statically typed languages is to write programs that will pre-generate tables of data for use at runtime.&amp;lt;ref name=&amp;quot;use1&amp;quot;&amp;gt;Jonathan Bartlett, &amp;quot;The art of metaprogramming, Part 1: Introduction to metaprogramming&amp;quot;, 20 Oct 2005   http://www.ibm.com/developerworks/library/l-metaprog1/index.html#N10052&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One simple but useful code generator is to build static lookup tables. Often, in order to build fast functions in C programming, we simply create a lookup table of all of the answers. This means that we either need to pre-compute them by hand (which is wasteful of your time) or build them at runtime (which is wasteful of the user's time).&lt;br /&gt;
&lt;br /&gt;
In following example we will build a generator that will take a function or set of functions on an integer and build lookup tables for the answer.&lt;br /&gt;
&lt;br /&gt;
To think of how to make such a program, we can start from the end and work backward. Firstly we need a lookup table that will return square roots of numbers between 5 and 20. A simple program can be written to generate such a table like this:&lt;br /&gt;
&lt;br /&gt;
Generate and use a lookup table of square roots&lt;br /&gt;
&lt;br /&gt;
        /* our lookup table */&lt;br /&gt;
        double square_roots[21];&lt;br /&gt;
        /* function to load the table at runtime */&lt;br /&gt;
        void init_square_roots()&lt;br /&gt;
        {&lt;br /&gt;
           int i;&lt;br /&gt;
           for(i = 5; i &amp;lt; 21; i++)&lt;br /&gt;
             {&lt;br /&gt;
               square_roots[i] = sqrt((double)i);&lt;br /&gt;
             }&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
With that single macro, we can take away a lot of work for any program that has to generate mathematical tables indexed by integer. A little extra work would also allow tables containing full struct definitions; a little more would ensure that space isn't wasted at the front of the array with useless empty entries.&lt;br /&gt;
        /* program that uses the table */&lt;br /&gt;
        int main ()&lt;br /&gt;
        {&lt;br /&gt;
           init_square_roots();&lt;br /&gt;
           printf(&amp;quot;The square root of 5 is %f\n&amp;quot;, square_roots[5]);&lt;br /&gt;
           return 0;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
Now, to convert this to a statically initialized array, you would remove the first part of the program and replace it with something like this, calculated by hand:&lt;br /&gt;
&lt;br /&gt;
Square root program with a static lookup table&lt;br /&gt;
&lt;br /&gt;
        double square_roots[] = {&lt;br /&gt;
           /* these are the ones we skipped */ 0.0, 0.0, 0.0, 0.0, 0.0&lt;br /&gt;
           2.236068, /* Square root of 5 */&lt;br /&gt;
           2.449490, /* Square root of 6 */&lt;br /&gt;
           2.645751, /* Square root of 7 */&lt;br /&gt;
           2.828427, /* Square root of 8 */&lt;br /&gt;
           3.0, /* Square root of 9 */&lt;br /&gt;
           ...&lt;br /&gt;
           4.472136 /* Square root of 20 */&lt;br /&gt;
           };&lt;br /&gt;
&lt;br /&gt;
What is needed is a program that will produce these values and print them out in a table like the previous one so they are loaded in at compile-time.&lt;br /&gt;
&lt;br /&gt;
Code generator for the table macro&lt;br /&gt;
&lt;br /&gt;
           #!/usr/bin/perl  &lt;br /&gt;
           #&lt;br /&gt;
           #tablegen.pl &lt;br /&gt;
           #&lt;br /&gt;
           ##Puts each program line into $line&lt;br /&gt;
           while(my $line = &amp;lt;&amp;gt;)&lt;br /&gt;
           {&lt;br /&gt;
             #Is this a macro invocation?&lt;br /&gt;
             if($line =~ m/TABLE:/)&lt;br /&gt;
             {&lt;br /&gt;
                #If so, split it apart into its component pieces&lt;br /&gt;
                my ($dummy, $table_name, $type, $start_idx, $end_idx, $default, $procedure) = split(m/:/, $line, 7);&lt;br /&gt;
                #The main difference between C and Perl for mathematical expressions is that&lt;br /&gt;
                #Perl prefixes its variables with a dollar sign, so we will add that here&lt;br /&gt;
                $procedure =~ s/VAL/\$VAL/g;&lt;br /&gt;
                #Print out the array declaration&lt;br /&gt;
                print &amp;quot;${type} ${table_name} [] = {\n&amp;quot;;&lt;br /&gt;
                #Go through each array element&lt;br /&gt;
                foreach my $VAL (0 .. $end_idx)&lt;br /&gt;
                {&lt;br /&gt;
                   #Only process an answer if we have reached our starting index&lt;br /&gt;
                   if($VAL &amp;gt;= $start_idx)&lt;br /&gt;
                   {&lt;br /&gt;
                      #evaluate the procedure specified (this sets $@ if there are any errors)&lt;br /&gt;
                      $result = eval $procedure;&lt;br /&gt;
                      die(&amp;quot;Error processing: $@&amp;quot;) if $@;&lt;br /&gt;
                   }&lt;br /&gt;
                   else&lt;br /&gt;
                   {&lt;br /&gt;
                      #if we haven't reached the starting index, just use the default&lt;br /&gt;
                      $result = $default;&lt;br /&gt;
                   }&lt;br /&gt;
                   #Print out the value&lt;br /&gt;
                   print &amp;quot;\t${result}&amp;quot;;&lt;br /&gt;
                   #If there are more to be processed, add a comma after the value&lt;br /&gt;
                   if($VAL != $end_idx)&lt;br /&gt;
                   {&lt;br /&gt;
                      print &amp;quot;,&amp;quot;;&lt;br /&gt;
                   }&lt;br /&gt;
                   print &amp;quot;\n&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
                #Finish the declaration&lt;br /&gt;
                print &amp;quot;};\n&amp;quot;;&lt;br /&gt;
             }&lt;br /&gt;
             else&lt;br /&gt;
             {&lt;br /&gt;
                #If this is not a macro invocation, just copy the line directly to the output&lt;br /&gt;
                print $line;&lt;br /&gt;
             }&lt;br /&gt;
           }&lt;br /&gt;
&lt;br /&gt;
To run this program, do this:&lt;br /&gt;
&lt;br /&gt;
Running the code generator&lt;br /&gt;
&lt;br /&gt;
           ./tablegen.pl &amp;lt; sqrt.in &amp;gt; sqrt.c&lt;br /&gt;
           gcc sqrt.c -o sqrt&lt;br /&gt;
           ./a.out&lt;br /&gt;
&lt;br /&gt;
===Mini-language for boiler-plate===&lt;br /&gt;
If you have a large application where many of the functions include a lot of boilerplate code, it is often a good idea to create a mini-language that allows you to work with your boilerplate code in an easier fashion. This mini-language will then be converted into your regular source code language before compiling.&amp;lt;ref name=&amp;quot;use2&amp;quot;&amp;gt;Jonathan Bartlett, &amp;quot;The art of metaprogramming, Part 2: Metaprogramming using Scheme&amp;quot;, 02 May 2006  http://www.ibm.com/developerworks/linux/library/l-metaprog2/index.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following is an example:&lt;br /&gt;
&lt;br /&gt;
Let's say that we are building a CGI application consisting of many independent CGI scripts. In most CGI applications, much of the state is stored in a database, but only a session ID is passed to each script via a cookie.&lt;br /&gt;
&lt;br /&gt;
However, in nearly every page we need to know the other standard information (such as the username, group number, the current job being worked on, whatever else information is pertinent). In addition, we need to redirect the user if they do not have an appropriate cookie.&lt;br /&gt;
&lt;br /&gt;
            (define (handle-cgi-request req)&lt;br /&gt;
              (let (&lt;br /&gt;
                     (session-id (webserver:cookie req &amp;quot;sessionid&amp;quot;)))&lt;br /&gt;
                (if (not (webserver:valid-session-id session-id))&lt;br /&gt;
                    (webserver:redirect-to-login-page)&lt;br /&gt;
                    (let (&lt;br /&gt;
                          (username (webserver:username-for-session session-id))&lt;br /&gt;
                          (group (webserver:group-for-user username))&lt;br /&gt;
                          (current-job (webserver:current-job-for-user username)))&lt;br /&gt;
                       ;;Code for processing goes here&lt;br /&gt;
                       ))))&lt;br /&gt;
&lt;br /&gt;
While some of that can be handled by a procedure, the bindings certainly cannot. However, we can turn most of it into a macro. The macro can be implemented like this:&lt;br /&gt;
&lt;br /&gt;
            (define-syntax cgi-boilerplate&lt;br /&gt;
              (lambda (x)&lt;br /&gt;
                (syntax-case x ()&lt;br /&gt;
                 (&lt;br /&gt;
                   (cgi-boilerplate expr)&lt;br /&gt;
                   (datum-&amp;gt;syntax-object&lt;br /&gt;
                     (syntax k)&lt;br /&gt;
                      (let (&lt;br /&gt;
                            (session-id (webserver:cookie req &amp;quot;sessionid&amp;quot;)))&lt;br /&gt;
                           (if (not (webserver:valid-session-id session-id))&lt;br /&gt;
                               (webserver:redirect-to-login-page)&lt;br /&gt;
                               (let (&lt;br /&gt;
                                      (username (webserver:username-for-session session-id))&lt;br /&gt;
                                      (group (webserver:group-for-user username))&lt;br /&gt;
                                      (current-job (webserver:current-job-for-user username)))&lt;br /&gt;
                                      (syntax-object-&amp;gt;datum (syntax expr))))))&lt;br /&gt;
                  )&lt;br /&gt;
             )))&lt;br /&gt;
&lt;br /&gt;
We can now create new forms based on our boilerplate code by doing the following:&lt;br /&gt;
&lt;br /&gt;
            (define (handle-cgi-request req)&lt;br /&gt;
              (cgi-boilerplate&lt;br /&gt;
               (begin&lt;br /&gt;
                 ;;Do whatever I want here&lt;br /&gt;
                 )))&lt;br /&gt;
&lt;br /&gt;
In addition, since we are not defining our variables explicitly, adding new variable definitions to our boilerplate won't affect its calling conventions, so new features can be added without having to create a whole new function.&lt;br /&gt;
&lt;br /&gt;
In any large project, there are inevitably templates to follow which cannot be reduced to functions, usually because of the bindings being created. Using boilerplate macros can make maintenance of such templated code much easier.&lt;br /&gt;
&lt;br /&gt;
Likewise, other standard macros can be created which make use of variables defined in the boilerplate. Using macros like this significantly reduces typing because you do not have to constantly be writing and rewriting variable bindings, derivations, and parameter passing. This also reduces the potential for errors in such code.&lt;br /&gt;
&lt;br /&gt;
Realize though that boilerplate macros are not a panacea. There are many significant problems that can occur, including:&lt;br /&gt;
*  Accidentally overwriting bindings by introducing a variable name that was previously defined in a macro.&lt;br /&gt;
*  Difficulty tracing problems because the inputs and the outputs of the macros are implicit, not explicit.&lt;br /&gt;
&lt;br /&gt;
These can be largely avoided by doing a few things in conjunction with your boilerplate macros:&lt;br /&gt;
*  Have a naming convention which clearly labels macros as such, as well as indicate that a variable came from boilerplate code. This could be done by affixing -m to macros and -b to variables defined within a boilerplate.&lt;br /&gt;
*  Carefully document all boilerplate macros, especially the introduced variable bindings and all changes between versions.&lt;br /&gt;
*  Only use boilerplate macros when the savings in repetitiveness clearly outweigh the negatives of implicit functionality.&lt;br /&gt;
&lt;br /&gt;
===Abbreviate statements and prevent mistakes===&lt;br /&gt;
A lot of programming languages make you write really verbose statements to do really simple things. Code-generating programs allow you to abbreviate such statements and save a lot of typing, which also prevents a lot of mistakes because there is less chance of mistyping.&lt;br /&gt;
&lt;br /&gt;
=Metaprogramming Framework in Java=&lt;br /&gt;
==Reflection==&lt;br /&gt;
===Introduction to reflection===&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_(computer_programming) Reflection] is the ability to introspect metalevel information about the program structure itself at runtime. Usually this metalevel information is modeled using the general abstraction mechanisms available in the language. In Java, reflection enables to discover information about the loaded classes:&lt;br /&gt;
*  Fields,&lt;br /&gt;
*  Methods, and&lt;br /&gt;
*  Constructors&lt;br /&gt;
*  Generics information&lt;br /&gt;
*  Metadata annotations&lt;br /&gt;
It also enables to use these metaobjects to their instances in runtime environment.&lt;br /&gt;
&lt;br /&gt;
The following is the Java metalevel architecture&amp;lt;ref name=&amp;quot;reflection&amp;quot;&amp;gt;Mika Haapakorpi, &amp;quot;Meta Programming In Java&amp;quot;&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Metalevel architecture.PNG]]&lt;br /&gt;
&lt;br /&gt;
===Dynamic proxy classes===&lt;br /&gt;
A dynamic proxy class implements a list of interfaces specified at runtime when the class is created.&lt;br /&gt;
*  A proxy interface is an interface that is implemented by a proxy class.&lt;br /&gt;
*  A proxy instance is an instance of a proxy class which has an associated invocation handler object.&lt;br /&gt;
The following is an example of dynamic proxy classes&lt;br /&gt;
&lt;br /&gt;
[[File:Dynamic_proxy_class.PNG]]&lt;br /&gt;
&lt;br /&gt;
==Generics==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Generics_in_Java Generics] are a facility of generic programming that was added to the Java programming language in 2004 as part of J2SE 5.0. They allow &amp;quot;a type or method to operate on objects of various types while providing compile-time type safety.&amp;quot;  The generics can be used in classes, interfaces, methods and constructors.&amp;lt;ref name=&amp;quot;reflection&amp;quot;&amp;gt;Mika Haapakorpi, &amp;quot;Meta Programming In Java&amp;quot;&amp;lt;/ref&amp;gt;&lt;br /&gt;
===Two new types in generics===&lt;br /&gt;
Two new types in generics:&lt;br /&gt;
*  Parametrized types&lt;br /&gt;
*  Type variables&lt;br /&gt;
&lt;br /&gt;
A type variable is an unqualified identifier. Class and interface declarations can have type arguments (type variables). Method and constructors definitions can have type arguments (type variables). &lt;br /&gt;
&lt;br /&gt;
The following is an example for type variables&lt;br /&gt;
&lt;br /&gt;
       List&amp;lt;String&amp;gt; anExample = new ArrayList&amp;lt;String&amp;gt;()&lt;br /&gt;
&lt;br /&gt;
*  List : interface&lt;br /&gt;
*  ArrayList : class&lt;br /&gt;
*  String : class (the actual type argument)&lt;br /&gt;
*  List&amp;lt;String&amp;gt; and ArrayList&amp;lt;String&amp;gt; : parametrized type&lt;br /&gt;
&lt;br /&gt;
===Class/Interface Declarations===&lt;br /&gt;
The following is an example of class declaration:&lt;br /&gt;
&lt;br /&gt;
       public interface List&amp;lt;E&amp;gt; {&lt;br /&gt;
         void add(Ex);&lt;br /&gt;
         Iterator&amp;lt;E&amp;gt; iterator();&lt;br /&gt;
       }&lt;br /&gt;
       public interface Iterator&amp;lt;E&amp;gt; {&lt;br /&gt;
         E next();&lt;br /&gt;
         booleanhasNext();&lt;br /&gt;
       }&lt;br /&gt;
       List&amp;lt;String&amp;gt; anExample;&lt;br /&gt;
       anExample.add(”sdfdfss”);&lt;br /&gt;
       anExample.add(new Object()); // compile time error&lt;br /&gt;
       String aTest = anExample.iterator().next();&lt;br /&gt;
&lt;br /&gt;
==Metadata annotation==&lt;br /&gt;
An annotation, in the Java computer programming language, is a form of syntactic metadata that can be added to Java source code. Classes, methods, variables, parameters and packages may be annotated. &lt;br /&gt;
&lt;br /&gt;
The following is an example of annotation:&lt;br /&gt;
&lt;br /&gt;
        /**&lt;br /&gt;
        * Designates a formatter to pretty-print the annotated class.&lt;br /&gt;
        */&lt;br /&gt;
        public @interface PrettyPrinter{&lt;br /&gt;
        Class&amp;lt;? extends Formatter&amp;gt; value();&lt;br /&gt;
        }&lt;br /&gt;
        // Single-member annotation with Class&lt;br /&gt;
        // member restricted by bounded wildcard&lt;br /&gt;
        // The annotation presumes the existence of this class.&lt;br /&gt;
        class GorgeousFormatterimplements Formatter { ... }&lt;br /&gt;
        @PrettyPrinter(GorgeousFormatter.class)&lt;br /&gt;
        public class Petunia { ... }&lt;br /&gt;
&lt;br /&gt;
‘@’ indicates the start of an annotation definition.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Cyang14</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1_1h_jc&amp;diff=73615</id>
		<title>CSC/ECE 517 Spring 2013/ch1 1h jc</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1_1h_jc&amp;diff=73615"/>
		<updated>2013-02-24T16:45:32Z</updated>

		<summary type="html">&lt;p&gt;Cyang14: /* Generics */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Metaprogramming in statically typed languages&lt;br /&gt;
=Metaprogramming in statically typed languages=&lt;br /&gt;
==Introduction==&lt;br /&gt;
===What is metaprogramming===&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming] refers to writing programs that can manipulate other programs or themselves. The program that is doing the manipulating is called the ''metaprogram'', and the program that is being manipulated is called the ''object program''. The language of the ''metaprogram'' is referred to as the ''metalanguage'' and the language of the object program is called the ''object language''. A language has the ability of [http://en.wikipedia.org/wiki/Reflection_(computer_programming) reflection] if it can be used to write a metaprogram that can manipulate a program written in the same language.&lt;br /&gt;
&lt;br /&gt;
===Metaprogramming in statically typed languages===&lt;br /&gt;
&lt;br /&gt;
Metaprogramming can be accomplished in both statically and dynamically typed languages, however [http://en.wikipedia.org/wiki/Dynamically_typed_language#Dynamic_typing dynamically typed languages] have some advantages over [http://en.wikipedia.org/wiki/Dynamically_typed_language#Dynamic_typing statically typed languages] when it comes to metaprogramming.&lt;br /&gt;
&lt;br /&gt;
One example of a metaprogramming task is to add a method to a particular class at runtime. Sample code in Ruby to perform this task is outlined below. First is a definition of a Ruby class called TestAddMethodAtRuntime:&lt;br /&gt;
&lt;br /&gt;
  class TestAddMethodAtRuntime&lt;br /&gt;
    def originalMethod&lt;br /&gt;
        puts &amp;quot;Original Method&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The code below created a new instance of the class. After that, it defines a new method called newlyAddedMethod. Then the original method is called, follow by the newly added method at runtime:&lt;br /&gt;
&lt;br /&gt;
  t = TestAddMethodAtRuntime.new&lt;br /&gt;
  def t.newlyAddedMethod&lt;br /&gt;
    puts &amp;quot;Newly Added Method&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  t.originalMethod&lt;br /&gt;
  puts &amp;quot;\n&amp;quot;&lt;br /&gt;
  t.newlyAddedMethod&lt;br /&gt;
&lt;br /&gt;
Below is the output of the code above:&lt;br /&gt;
&lt;br /&gt;
  Original Method&lt;br /&gt;
  Newly Added Method&lt;br /&gt;
&lt;br /&gt;
Accomplishing something like this in a statically typed language would be much more difficult or impossible depending on the language. It is for this reason that dynamically typed languages are usually viewed as being better equipped to handle metaprogramming overall.&lt;br /&gt;
&lt;br /&gt;
==Implementation==&lt;br /&gt;
===Programming Language API===&lt;br /&gt;
&lt;br /&gt;
One way that metaprogramming can be possible in a language is through an [http://en.wikipedia.org/wiki/API API] that helps programmers achieve a metaprogramming task. An example of this is a language that provides an API for reflection. One type of reflection is ''introspection'' in which the program can access the source code of the program itself.&lt;br /&gt;
&lt;br /&gt;
Below is an example from the Java API to illustrate how an API can be used to achieve some metaprogramming tasks:&lt;br /&gt;
&lt;br /&gt;
   import java.lang.reflect.*; &lt;br /&gt;
   public class DumpMethods {&lt;br /&gt;
   public static void main(String args[]) {&lt;br /&gt;
     	try {&lt;br /&gt;
           Class c = Class.forName(args[0]);&lt;br /&gt;
           Method m[] = c.getDeclaredMethods();&lt;br /&gt;
           for (int i = 0; i &amp;lt; m.length; i++) {&lt;br /&gt;
              System.out.println(m[i].toString());&lt;br /&gt;
           }&lt;br /&gt;
     	}&lt;br /&gt;
     	catch (Throwable e) {&lt;br /&gt;
        	System.err.println(e);&lt;br /&gt;
     	}&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
The above example prints out all of the methods that are declared for a particular class. This is an example of metaprogramming since the program is accessing the source code of itself.&lt;br /&gt;
&lt;br /&gt;
===Program transformation system===&lt;br /&gt;
&lt;br /&gt;
A program transformation system is something that takes a program as the input, and outputs a different program. One example of a program transformation system is the Java compiler. The Java compiler takes a Java program as its input, and outputs a .class file with platform independent bytecode in it. Another example of a program transformation system is a Java decompiler, which takes bytecode as an input and outputs a Java program.&lt;br /&gt;
&lt;br /&gt;
In the case of the Java compiler, the program input is being output into another program in a different &amp;quot;language&amp;quot;. There are other types of transformation systems that take a program as an input and output a different program in the same language. One example of this would be ''program migration'' which migrates source code to a newer or older version of the same language. This can be useful if upgrading a software system to run on a newer version of a framework.&lt;br /&gt;
&lt;br /&gt;
==Common Uses==&lt;br /&gt;
===Pre-generate static data at compile time===&lt;br /&gt;
One common use of metaprogramming in statically typed languages is to write programs that will pre-generate tables of data for use at runtime.&amp;lt;ref name=&amp;quot;use1&amp;quot;&amp;gt;Jonathan Bartlett, &amp;quot;The art of metaprogramming, Part 1: Introduction to metaprogramming&amp;quot;, 20 Oct 2005   http://www.ibm.com/developerworks/library/l-metaprog1/index.html#N10052&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One simple but useful code generator is to build static lookup tables. Often, in order to build fast functions in C programming, we simply create a lookup table of all of the answers. This means that we either need to pre-compute them by hand (which is wasteful of your time) or build them at runtime (which is wasteful of the user's time).&lt;br /&gt;
&lt;br /&gt;
In following example we will build a generator that will take a function or set of functions on an integer and build lookup tables for the answer.&lt;br /&gt;
&lt;br /&gt;
To think of how to make such a program, we can start from the end and work backward. Firstly we need a lookup table that will return square roots of numbers between 5 and 20. A simple program can be written to generate such a table like this:&lt;br /&gt;
&lt;br /&gt;
Generate and use a lookup table of square roots&lt;br /&gt;
&lt;br /&gt;
        /* our lookup table */&lt;br /&gt;
        double square_roots[21];&lt;br /&gt;
        /* function to load the table at runtime */&lt;br /&gt;
        void init_square_roots()&lt;br /&gt;
        {&lt;br /&gt;
           int i;&lt;br /&gt;
           for(i = 5; i &amp;lt; 21; i++)&lt;br /&gt;
             {&lt;br /&gt;
               square_roots[i] = sqrt((double)i);&lt;br /&gt;
             }&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
With that single macro, we can take away a lot of work for any program that has to generate mathematical tables indexed by integer. A little extra work would also allow tables containing full struct definitions; a little more would ensure that space isn't wasted at the front of the array with useless empty entries.&lt;br /&gt;
        /* program that uses the table */&lt;br /&gt;
        int main ()&lt;br /&gt;
        {&lt;br /&gt;
           init_square_roots();&lt;br /&gt;
           printf(&amp;quot;The square root of 5 is %f\n&amp;quot;, square_roots[5]);&lt;br /&gt;
           return 0;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
Now, to convert this to a statically initialized array, you would remove the first part of the program and replace it with something like this, calculated by hand:&lt;br /&gt;
&lt;br /&gt;
Square root program with a static lookup table&lt;br /&gt;
&lt;br /&gt;
        double square_roots[] = {&lt;br /&gt;
           /* these are the ones we skipped */ 0.0, 0.0, 0.0, 0.0, 0.0&lt;br /&gt;
           2.236068, /* Square root of 5 */&lt;br /&gt;
           2.449490, /* Square root of 6 */&lt;br /&gt;
           2.645751, /* Square root of 7 */&lt;br /&gt;
           2.828427, /* Square root of 8 */&lt;br /&gt;
           3.0, /* Square root of 9 */&lt;br /&gt;
           ...&lt;br /&gt;
           4.472136 /* Square root of 20 */&lt;br /&gt;
           };&lt;br /&gt;
&lt;br /&gt;
What is needed is a program that will produce these values and print them out in a table like the previous one so they are loaded in at compile-time.&lt;br /&gt;
&lt;br /&gt;
Code generator for the table macro&lt;br /&gt;
&lt;br /&gt;
           #!/usr/bin/perl  &lt;br /&gt;
           #&lt;br /&gt;
           #tablegen.pl &lt;br /&gt;
           #&lt;br /&gt;
           ##Puts each program line into $line&lt;br /&gt;
           while(my $line = &amp;lt;&amp;gt;)&lt;br /&gt;
           {&lt;br /&gt;
             #Is this a macro invocation?&lt;br /&gt;
             if($line =~ m/TABLE:/)&lt;br /&gt;
             {&lt;br /&gt;
                #If so, split it apart into its component pieces&lt;br /&gt;
                my ($dummy, $table_name, $type, $start_idx, $end_idx, $default, $procedure) = split(m/:/, $line, 7);&lt;br /&gt;
                #The main difference between C and Perl for mathematical expressions is that&lt;br /&gt;
                #Perl prefixes its variables with a dollar sign, so we will add that here&lt;br /&gt;
                $procedure =~ s/VAL/\$VAL/g;&lt;br /&gt;
                #Print out the array declaration&lt;br /&gt;
                print &amp;quot;${type} ${table_name} [] = {\n&amp;quot;;&lt;br /&gt;
                #Go through each array element&lt;br /&gt;
                foreach my $VAL (0 .. $end_idx)&lt;br /&gt;
                {&lt;br /&gt;
                   #Only process an answer if we have reached our starting index&lt;br /&gt;
                   if($VAL &amp;gt;= $start_idx)&lt;br /&gt;
                   {&lt;br /&gt;
                      #evaluate the procedure specified (this sets $@ if there are any errors)&lt;br /&gt;
                      $result = eval $procedure;&lt;br /&gt;
                      die(&amp;quot;Error processing: $@&amp;quot;) if $@;&lt;br /&gt;
                   }&lt;br /&gt;
                   else&lt;br /&gt;
                   {&lt;br /&gt;
                      #if we haven't reached the starting index, just use the default&lt;br /&gt;
                      $result = $default;&lt;br /&gt;
                   }&lt;br /&gt;
                   #Print out the value&lt;br /&gt;
                   print &amp;quot;\t${result}&amp;quot;;&lt;br /&gt;
                   #If there are more to be processed, add a comma after the value&lt;br /&gt;
                   if($VAL != $end_idx)&lt;br /&gt;
                   {&lt;br /&gt;
                      print &amp;quot;,&amp;quot;;&lt;br /&gt;
                   }&lt;br /&gt;
                   print &amp;quot;\n&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
                #Finish the declaration&lt;br /&gt;
                print &amp;quot;};\n&amp;quot;;&lt;br /&gt;
             }&lt;br /&gt;
             else&lt;br /&gt;
             {&lt;br /&gt;
                #If this is not a macro invocation, just copy the line directly to the output&lt;br /&gt;
                print $line;&lt;br /&gt;
             }&lt;br /&gt;
           }&lt;br /&gt;
&lt;br /&gt;
To run this program, do this:&lt;br /&gt;
&lt;br /&gt;
Running the code generator&lt;br /&gt;
&lt;br /&gt;
           ./tablegen.pl &amp;lt; sqrt.in &amp;gt; sqrt.c&lt;br /&gt;
           gcc sqrt.c -o sqrt&lt;br /&gt;
           ./a.out&lt;br /&gt;
&lt;br /&gt;
===Mini-language for boiler-plate===&lt;br /&gt;
If you have a large application where many of the functions include a lot of boilerplate code, it is often a good idea to create a mini-language that allows you to work with your boilerplate code in an easier fashion. This mini-language will then be converted into your regular source code language before compiling.&amp;lt;ref name=&amp;quot;use2&amp;quot;&amp;gt;Jonathan Bartlett, &amp;quot;The art of metaprogramming, Part 2: Metaprogramming using Scheme&amp;quot;, 02 May 2006  http://www.ibm.com/developerworks/linux/library/l-metaprog2/index.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following is an example:&lt;br /&gt;
&lt;br /&gt;
Let's say that we are building a CGI application consisting of many independent CGI scripts. In most CGI applications, much of the state is stored in a database, but only a session ID is passed to each script via a cookie.&lt;br /&gt;
&lt;br /&gt;
However, in nearly every page we need to know the other standard information (such as the username, group number, the current job being worked on, whatever else information is pertinent). In addition, we need to redirect the user if they do not have an appropriate cookie.&lt;br /&gt;
&lt;br /&gt;
            (define (handle-cgi-request req)&lt;br /&gt;
              (let (&lt;br /&gt;
                     (session-id (webserver:cookie req &amp;quot;sessionid&amp;quot;)))&lt;br /&gt;
                (if (not (webserver:valid-session-id session-id))&lt;br /&gt;
                    (webserver:redirect-to-login-page)&lt;br /&gt;
                    (let (&lt;br /&gt;
                          (username (webserver:username-for-session session-id))&lt;br /&gt;
                          (group (webserver:group-for-user username))&lt;br /&gt;
                          (current-job (webserver:current-job-for-user username)))&lt;br /&gt;
                       ;;Code for processing goes here&lt;br /&gt;
                       ))))&lt;br /&gt;
&lt;br /&gt;
While some of that can be handled by a procedure, the bindings certainly cannot. However, we can turn most of it into a macro. The macro can be implemented like this:&lt;br /&gt;
&lt;br /&gt;
            (define-syntax cgi-boilerplate&lt;br /&gt;
              (lambda (x)&lt;br /&gt;
                (syntax-case x ()&lt;br /&gt;
                 (&lt;br /&gt;
                   (cgi-boilerplate expr)&lt;br /&gt;
                   (datum-&amp;gt;syntax-object&lt;br /&gt;
                     (syntax k)&lt;br /&gt;
                      (let (&lt;br /&gt;
                            (session-id (webserver:cookie req &amp;quot;sessionid&amp;quot;)))&lt;br /&gt;
                           (if (not (webserver:valid-session-id session-id))&lt;br /&gt;
                               (webserver:redirect-to-login-page)&lt;br /&gt;
                               (let (&lt;br /&gt;
                                      (username (webserver:username-for-session session-id))&lt;br /&gt;
                                      (group (webserver:group-for-user username))&lt;br /&gt;
                                      (current-job (webserver:current-job-for-user username)))&lt;br /&gt;
                                      (syntax-object-&amp;gt;datum (syntax expr))))))&lt;br /&gt;
                  )&lt;br /&gt;
             )))&lt;br /&gt;
&lt;br /&gt;
We can now create new forms based on our boilerplate code by doing the following:&lt;br /&gt;
&lt;br /&gt;
            (define (handle-cgi-request req)&lt;br /&gt;
              (cgi-boilerplate&lt;br /&gt;
               (begin&lt;br /&gt;
                 ;;Do whatever I want here&lt;br /&gt;
                 )))&lt;br /&gt;
&lt;br /&gt;
In addition, since we are not defining our variables explicitly, adding new variable definitions to our boilerplate won't affect its calling conventions, so new features can be added without having to create a whole new function.&lt;br /&gt;
&lt;br /&gt;
In any large project, there are inevitably templates to follow which cannot be reduced to functions, usually because of the bindings being created. Using boilerplate macros can make maintenance of such templated code much easier.&lt;br /&gt;
&lt;br /&gt;
Likewise, other standard macros can be created which make use of variables defined in the boilerplate. Using macros like this significantly reduces typing because you do not have to constantly be writing and rewriting variable bindings, derivations, and parameter passing. This also reduces the potential for errors in such code.&lt;br /&gt;
&lt;br /&gt;
Realize though that boilerplate macros are not a panacea. There are many significant problems that can occur, including:&lt;br /&gt;
*  Accidentally overwriting bindings by introducing a variable name that was previously defined in a macro.&lt;br /&gt;
*  Difficulty tracing problems because the inputs and the outputs of the macros are implicit, not explicit.&lt;br /&gt;
&lt;br /&gt;
These can be largely avoided by doing a few things in conjunction with your boilerplate macros:&lt;br /&gt;
*  Have a naming convention which clearly labels macros as such, as well as indicate that a variable came from boilerplate code. This could be done by affixing -m to macros and -b to variables defined within a boilerplate.&lt;br /&gt;
*  Carefully document all boilerplate macros, especially the introduced variable bindings and all changes between versions.&lt;br /&gt;
*  Only use boilerplate macros when the savings in repetitiveness clearly outweigh the negatives of implicit functionality.&lt;br /&gt;
&lt;br /&gt;
===Abbreviate statements and prevent mistakes===&lt;br /&gt;
A lot of programming languages make you write really verbose statements to do really simple things. Code-generating programs allow you to abbreviate such statements and save a lot of typing, which also prevents a lot of mistakes because there is less chance of mistyping.&lt;br /&gt;
&lt;br /&gt;
=Metaprogramming Framework in Java=&lt;br /&gt;
==Reflection==&lt;br /&gt;
===Introduction to reflection===&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_(computer_programming) Reflection] is the ability to introspect metalevel information about the program structure itself at runtime. Usually this metalevel information is modeled using the general abstraction mechanisms available in the language. In Java, reflection enables to discover information about the loaded classes:&lt;br /&gt;
*  Fields,&lt;br /&gt;
*  Methods, and&lt;br /&gt;
*  Constructors&lt;br /&gt;
*  Generics information&lt;br /&gt;
*  Metadata annotations&lt;br /&gt;
It also enables to use these metaobjects to their instances in runtime environment.&lt;br /&gt;
&lt;br /&gt;
The following is the Java metalevel architecture&amp;lt;ref name=&amp;quot;reflection&amp;quot;&amp;gt;Mika Haapakorpi, &amp;quot;Meta Programming In Java&amp;quot;&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Metalevel architecture.PNG]]&lt;br /&gt;
&lt;br /&gt;
===Dynamic proxy classes===&lt;br /&gt;
A dynamic proxy class implements a list of interfaces specified at runtime when the class is created.&lt;br /&gt;
*  A proxy interface is an interface that is implemented by a proxy class.&lt;br /&gt;
*  A proxy instance is an instance of a proxy class which has an associated invocation handler object.&lt;br /&gt;
The following is an example of dynamic proxy classes&lt;br /&gt;
&lt;br /&gt;
[[File:Dynamic_proxy_class.PNG]]&lt;br /&gt;
&lt;br /&gt;
==Generics==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Generics_in_Java Generics] are a facility of generic programming that was added to the Java programming language in 2004 as part of J2SE 5.0. They allow &amp;quot;a type or method to operate on objects of various types while providing compile-time type safety.&amp;quot;  The generics can be used in classes, interfaces, methods and constructors.&lt;br /&gt;
===Two new types in generics===&lt;br /&gt;
Two new types in generics:&lt;br /&gt;
*  Parametrized types&lt;br /&gt;
*  Type variables&lt;br /&gt;
&lt;br /&gt;
A type variable is an unqualified identifier. Class and interface declarations can have type arguments (type variables). Method and constructors definitions can have type arguments (type variables). &lt;br /&gt;
&lt;br /&gt;
The following is an example for type variables&lt;br /&gt;
&lt;br /&gt;
       List&amp;lt;String&amp;gt; anExample = new ArrayList&amp;lt;String&amp;gt;()&lt;br /&gt;
&lt;br /&gt;
*  List : interface&lt;br /&gt;
*  ArrayList : class&lt;br /&gt;
*  String : class (the actual type argument)&lt;br /&gt;
*  List&amp;lt;String&amp;gt; and ArrayList&amp;lt;String&amp;gt; : parametrized type&lt;br /&gt;
&lt;br /&gt;
===Class/Interface Declarations===&lt;br /&gt;
The following is an example of class declaration:&lt;br /&gt;
&lt;br /&gt;
       public interface List&amp;lt;E&amp;gt; {&lt;br /&gt;
         void add(Ex);&lt;br /&gt;
         Iterator&amp;lt;E&amp;gt; iterator();&lt;br /&gt;
       }&lt;br /&gt;
       public interface Iterator&amp;lt;E&amp;gt; {&lt;br /&gt;
         E next();&lt;br /&gt;
         booleanhasNext();&lt;br /&gt;
       }&lt;br /&gt;
       List&amp;lt;String&amp;gt; anExample;&lt;br /&gt;
       anExample.add(”sdfdfss”);&lt;br /&gt;
       anExample.add(new Object()); // compile time error&lt;br /&gt;
       String aTest = anExample.iterator().next();&lt;br /&gt;
&lt;br /&gt;
==Metadata annotation==&lt;br /&gt;
An annotation, in the Java computer programming language, is a form of syntactic metadata that can be added to Java source code. Classes, methods, variables, parameters and packages may be annotated. &lt;br /&gt;
&lt;br /&gt;
The following is an example of annotation:&lt;br /&gt;
&lt;br /&gt;
        /**&lt;br /&gt;
        * Designates a formatter to pretty-print the annotated class.&lt;br /&gt;
        */&lt;br /&gt;
        public @interface PrettyPrinter{&lt;br /&gt;
        Class&amp;lt;? extends Formatter&amp;gt; value();&lt;br /&gt;
        }&lt;br /&gt;
        // Single-member annotation with Class&lt;br /&gt;
        // member restricted by bounded wildcard&lt;br /&gt;
        // The annotation presumes the existence of this class.&lt;br /&gt;
        class GorgeousFormatterimplements Formatter { ... }&lt;br /&gt;
        @PrettyPrinter(GorgeousFormatter.class)&lt;br /&gt;
        public class Petunia { ... }&lt;br /&gt;
&lt;br /&gt;
‘@’ indicates the start of an annotation definition.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Cyang14</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1_1h_jc&amp;diff=73614</id>
		<title>CSC/ECE 517 Spring 2013/ch1 1h jc</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1_1h_jc&amp;diff=73614"/>
		<updated>2013-02-24T16:44:14Z</updated>

		<summary type="html">&lt;p&gt;Cyang14: /* Introduction to reflection */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Metaprogramming in statically typed languages&lt;br /&gt;
=Metaprogramming in statically typed languages=&lt;br /&gt;
==Introduction==&lt;br /&gt;
===What is metaprogramming===&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming] refers to writing programs that can manipulate other programs or themselves. The program that is doing the manipulating is called the ''metaprogram'', and the program that is being manipulated is called the ''object program''. The language of the ''metaprogram'' is referred to as the ''metalanguage'' and the language of the object program is called the ''object language''. A language has the ability of [http://en.wikipedia.org/wiki/Reflection_(computer_programming) reflection] if it can be used to write a metaprogram that can manipulate a program written in the same language.&lt;br /&gt;
&lt;br /&gt;
===Metaprogramming in statically typed languages===&lt;br /&gt;
&lt;br /&gt;
Metaprogramming can be accomplished in both statically and dynamically typed languages, however [http://en.wikipedia.org/wiki/Dynamically_typed_language#Dynamic_typing dynamically typed languages] have some advantages over [http://en.wikipedia.org/wiki/Dynamically_typed_language#Dynamic_typing statically typed languages] when it comes to metaprogramming.&lt;br /&gt;
&lt;br /&gt;
One example of a metaprogramming task is to add a method to a particular class at runtime. Sample code in Ruby to perform this task is outlined below. First is a definition of a Ruby class called TestAddMethodAtRuntime:&lt;br /&gt;
&lt;br /&gt;
  class TestAddMethodAtRuntime&lt;br /&gt;
    def originalMethod&lt;br /&gt;
        puts &amp;quot;Original Method&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The code below created a new instance of the class. After that, it defines a new method called newlyAddedMethod. Then the original method is called, follow by the newly added method at runtime:&lt;br /&gt;
&lt;br /&gt;
  t = TestAddMethodAtRuntime.new&lt;br /&gt;
  def t.newlyAddedMethod&lt;br /&gt;
    puts &amp;quot;Newly Added Method&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  t.originalMethod&lt;br /&gt;
  puts &amp;quot;\n&amp;quot;&lt;br /&gt;
  t.newlyAddedMethod&lt;br /&gt;
&lt;br /&gt;
Below is the output of the code above:&lt;br /&gt;
&lt;br /&gt;
  Original Method&lt;br /&gt;
  Newly Added Method&lt;br /&gt;
&lt;br /&gt;
Accomplishing something like this in a statically typed language would be much more difficult or impossible depending on the language. It is for this reason that dynamically typed languages are usually viewed as being better equipped to handle metaprogramming overall.&lt;br /&gt;
&lt;br /&gt;
==Implementation==&lt;br /&gt;
===Programming Language API===&lt;br /&gt;
&lt;br /&gt;
One way that metaprogramming can be possible in a language is through an [http://en.wikipedia.org/wiki/API API] that helps programmers achieve a metaprogramming task. An example of this is a language that provides an API for reflection. One type of reflection is ''introspection'' in which the program can access the source code of the program itself.&lt;br /&gt;
&lt;br /&gt;
Below is an example from the Java API to illustrate how an API can be used to achieve some metaprogramming tasks:&lt;br /&gt;
&lt;br /&gt;
   import java.lang.reflect.*; &lt;br /&gt;
   public class DumpMethods {&lt;br /&gt;
   public static void main(String args[]) {&lt;br /&gt;
     	try {&lt;br /&gt;
           Class c = Class.forName(args[0]);&lt;br /&gt;
           Method m[] = c.getDeclaredMethods();&lt;br /&gt;
           for (int i = 0; i &amp;lt; m.length; i++) {&lt;br /&gt;
              System.out.println(m[i].toString());&lt;br /&gt;
           }&lt;br /&gt;
     	}&lt;br /&gt;
     	catch (Throwable e) {&lt;br /&gt;
        	System.err.println(e);&lt;br /&gt;
     	}&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
The above example prints out all of the methods that are declared for a particular class. This is an example of metaprogramming since the program is accessing the source code of itself.&lt;br /&gt;
&lt;br /&gt;
===Program transformation system===&lt;br /&gt;
&lt;br /&gt;
A program transformation system is something that takes a program as the input, and outputs a different program. One example of a program transformation system is the Java compiler. The Java compiler takes a Java program as its input, and outputs a .class file with platform independent bytecode in it. Another example of a program transformation system is a Java decompiler, which takes bytecode as an input and outputs a Java program.&lt;br /&gt;
&lt;br /&gt;
In the case of the Java compiler, the program input is being output into another program in a different &amp;quot;language&amp;quot;. There are other types of transformation systems that take a program as an input and output a different program in the same language. One example of this would be ''program migration'' which migrates source code to a newer or older version of the same language. This can be useful if upgrading a software system to run on a newer version of a framework.&lt;br /&gt;
&lt;br /&gt;
==Common Uses==&lt;br /&gt;
===Pre-generate static data at compile time===&lt;br /&gt;
One common use of metaprogramming in statically typed languages is to write programs that will pre-generate tables of data for use at runtime.&amp;lt;ref name=&amp;quot;use1&amp;quot;&amp;gt;Jonathan Bartlett, &amp;quot;The art of metaprogramming, Part 1: Introduction to metaprogramming&amp;quot;, 20 Oct 2005   http://www.ibm.com/developerworks/library/l-metaprog1/index.html#N10052&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One simple but useful code generator is to build static lookup tables. Often, in order to build fast functions in C programming, we simply create a lookup table of all of the answers. This means that we either need to pre-compute them by hand (which is wasteful of your time) or build them at runtime (which is wasteful of the user's time).&lt;br /&gt;
&lt;br /&gt;
In following example we will build a generator that will take a function or set of functions on an integer and build lookup tables for the answer.&lt;br /&gt;
&lt;br /&gt;
To think of how to make such a program, we can start from the end and work backward. Firstly we need a lookup table that will return square roots of numbers between 5 and 20. A simple program can be written to generate such a table like this:&lt;br /&gt;
&lt;br /&gt;
Generate and use a lookup table of square roots&lt;br /&gt;
&lt;br /&gt;
        /* our lookup table */&lt;br /&gt;
        double square_roots[21];&lt;br /&gt;
        /* function to load the table at runtime */&lt;br /&gt;
        void init_square_roots()&lt;br /&gt;
        {&lt;br /&gt;
           int i;&lt;br /&gt;
           for(i = 5; i &amp;lt; 21; i++)&lt;br /&gt;
             {&lt;br /&gt;
               square_roots[i] = sqrt((double)i);&lt;br /&gt;
             }&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
With that single macro, we can take away a lot of work for any program that has to generate mathematical tables indexed by integer. A little extra work would also allow tables containing full struct definitions; a little more would ensure that space isn't wasted at the front of the array with useless empty entries.&lt;br /&gt;
        /* program that uses the table */&lt;br /&gt;
        int main ()&lt;br /&gt;
        {&lt;br /&gt;
           init_square_roots();&lt;br /&gt;
           printf(&amp;quot;The square root of 5 is %f\n&amp;quot;, square_roots[5]);&lt;br /&gt;
           return 0;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
Now, to convert this to a statically initialized array, you would remove the first part of the program and replace it with something like this, calculated by hand:&lt;br /&gt;
&lt;br /&gt;
Square root program with a static lookup table&lt;br /&gt;
&lt;br /&gt;
        double square_roots[] = {&lt;br /&gt;
           /* these are the ones we skipped */ 0.0, 0.0, 0.0, 0.0, 0.0&lt;br /&gt;
           2.236068, /* Square root of 5 */&lt;br /&gt;
           2.449490, /* Square root of 6 */&lt;br /&gt;
           2.645751, /* Square root of 7 */&lt;br /&gt;
           2.828427, /* Square root of 8 */&lt;br /&gt;
           3.0, /* Square root of 9 */&lt;br /&gt;
           ...&lt;br /&gt;
           4.472136 /* Square root of 20 */&lt;br /&gt;
           };&lt;br /&gt;
&lt;br /&gt;
What is needed is a program that will produce these values and print them out in a table like the previous one so they are loaded in at compile-time.&lt;br /&gt;
&lt;br /&gt;
Code generator for the table macro&lt;br /&gt;
&lt;br /&gt;
           #!/usr/bin/perl  &lt;br /&gt;
           #&lt;br /&gt;
           #tablegen.pl &lt;br /&gt;
           #&lt;br /&gt;
           ##Puts each program line into $line&lt;br /&gt;
           while(my $line = &amp;lt;&amp;gt;)&lt;br /&gt;
           {&lt;br /&gt;
             #Is this a macro invocation?&lt;br /&gt;
             if($line =~ m/TABLE:/)&lt;br /&gt;
             {&lt;br /&gt;
                #If so, split it apart into its component pieces&lt;br /&gt;
                my ($dummy, $table_name, $type, $start_idx, $end_idx, $default, $procedure) = split(m/:/, $line, 7);&lt;br /&gt;
                #The main difference between C and Perl for mathematical expressions is that&lt;br /&gt;
                #Perl prefixes its variables with a dollar sign, so we will add that here&lt;br /&gt;
                $procedure =~ s/VAL/\$VAL/g;&lt;br /&gt;
                #Print out the array declaration&lt;br /&gt;
                print &amp;quot;${type} ${table_name} [] = {\n&amp;quot;;&lt;br /&gt;
                #Go through each array element&lt;br /&gt;
                foreach my $VAL (0 .. $end_idx)&lt;br /&gt;
                {&lt;br /&gt;
                   #Only process an answer if we have reached our starting index&lt;br /&gt;
                   if($VAL &amp;gt;= $start_idx)&lt;br /&gt;
                   {&lt;br /&gt;
                      #evaluate the procedure specified (this sets $@ if there are any errors)&lt;br /&gt;
                      $result = eval $procedure;&lt;br /&gt;
                      die(&amp;quot;Error processing: $@&amp;quot;) if $@;&lt;br /&gt;
                   }&lt;br /&gt;
                   else&lt;br /&gt;
                   {&lt;br /&gt;
                      #if we haven't reached the starting index, just use the default&lt;br /&gt;
                      $result = $default;&lt;br /&gt;
                   }&lt;br /&gt;
                   #Print out the value&lt;br /&gt;
                   print &amp;quot;\t${result}&amp;quot;;&lt;br /&gt;
                   #If there are more to be processed, add a comma after the value&lt;br /&gt;
                   if($VAL != $end_idx)&lt;br /&gt;
                   {&lt;br /&gt;
                      print &amp;quot;,&amp;quot;;&lt;br /&gt;
                   }&lt;br /&gt;
                   print &amp;quot;\n&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
                #Finish the declaration&lt;br /&gt;
                print &amp;quot;};\n&amp;quot;;&lt;br /&gt;
             }&lt;br /&gt;
             else&lt;br /&gt;
             {&lt;br /&gt;
                #If this is not a macro invocation, just copy the line directly to the output&lt;br /&gt;
                print $line;&lt;br /&gt;
             }&lt;br /&gt;
           }&lt;br /&gt;
&lt;br /&gt;
To run this program, do this:&lt;br /&gt;
&lt;br /&gt;
Running the code generator&lt;br /&gt;
&lt;br /&gt;
           ./tablegen.pl &amp;lt; sqrt.in &amp;gt; sqrt.c&lt;br /&gt;
           gcc sqrt.c -o sqrt&lt;br /&gt;
           ./a.out&lt;br /&gt;
&lt;br /&gt;
===Mini-language for boiler-plate===&lt;br /&gt;
If you have a large application where many of the functions include a lot of boilerplate code, it is often a good idea to create a mini-language that allows you to work with your boilerplate code in an easier fashion. This mini-language will then be converted into your regular source code language before compiling.&amp;lt;ref name=&amp;quot;use2&amp;quot;&amp;gt;Jonathan Bartlett, &amp;quot;The art of metaprogramming, Part 2: Metaprogramming using Scheme&amp;quot;, 02 May 2006  http://www.ibm.com/developerworks/linux/library/l-metaprog2/index.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following is an example:&lt;br /&gt;
&lt;br /&gt;
Let's say that we are building a CGI application consisting of many independent CGI scripts. In most CGI applications, much of the state is stored in a database, but only a session ID is passed to each script via a cookie.&lt;br /&gt;
&lt;br /&gt;
However, in nearly every page we need to know the other standard information (such as the username, group number, the current job being worked on, whatever else information is pertinent). In addition, we need to redirect the user if they do not have an appropriate cookie.&lt;br /&gt;
&lt;br /&gt;
            (define (handle-cgi-request req)&lt;br /&gt;
              (let (&lt;br /&gt;
                     (session-id (webserver:cookie req &amp;quot;sessionid&amp;quot;)))&lt;br /&gt;
                (if (not (webserver:valid-session-id session-id))&lt;br /&gt;
                    (webserver:redirect-to-login-page)&lt;br /&gt;
                    (let (&lt;br /&gt;
                          (username (webserver:username-for-session session-id))&lt;br /&gt;
                          (group (webserver:group-for-user username))&lt;br /&gt;
                          (current-job (webserver:current-job-for-user username)))&lt;br /&gt;
                       ;;Code for processing goes here&lt;br /&gt;
                       ))))&lt;br /&gt;
&lt;br /&gt;
While some of that can be handled by a procedure, the bindings certainly cannot. However, we can turn most of it into a macro. The macro can be implemented like this:&lt;br /&gt;
&lt;br /&gt;
            (define-syntax cgi-boilerplate&lt;br /&gt;
              (lambda (x)&lt;br /&gt;
                (syntax-case x ()&lt;br /&gt;
                 (&lt;br /&gt;
                   (cgi-boilerplate expr)&lt;br /&gt;
                   (datum-&amp;gt;syntax-object&lt;br /&gt;
                     (syntax k)&lt;br /&gt;
                      (let (&lt;br /&gt;
                            (session-id (webserver:cookie req &amp;quot;sessionid&amp;quot;)))&lt;br /&gt;
                           (if (not (webserver:valid-session-id session-id))&lt;br /&gt;
                               (webserver:redirect-to-login-page)&lt;br /&gt;
                               (let (&lt;br /&gt;
                                      (username (webserver:username-for-session session-id))&lt;br /&gt;
                                      (group (webserver:group-for-user username))&lt;br /&gt;
                                      (current-job (webserver:current-job-for-user username)))&lt;br /&gt;
                                      (syntax-object-&amp;gt;datum (syntax expr))))))&lt;br /&gt;
                  )&lt;br /&gt;
             )))&lt;br /&gt;
&lt;br /&gt;
We can now create new forms based on our boilerplate code by doing the following:&lt;br /&gt;
&lt;br /&gt;
            (define (handle-cgi-request req)&lt;br /&gt;
              (cgi-boilerplate&lt;br /&gt;
               (begin&lt;br /&gt;
                 ;;Do whatever I want here&lt;br /&gt;
                 )))&lt;br /&gt;
&lt;br /&gt;
In addition, since we are not defining our variables explicitly, adding new variable definitions to our boilerplate won't affect its calling conventions, so new features can be added without having to create a whole new function.&lt;br /&gt;
&lt;br /&gt;
In any large project, there are inevitably templates to follow which cannot be reduced to functions, usually because of the bindings being created. Using boilerplate macros can make maintenance of such templated code much easier.&lt;br /&gt;
&lt;br /&gt;
Likewise, other standard macros can be created which make use of variables defined in the boilerplate. Using macros like this significantly reduces typing because you do not have to constantly be writing and rewriting variable bindings, derivations, and parameter passing. This also reduces the potential for errors in such code.&lt;br /&gt;
&lt;br /&gt;
Realize though that boilerplate macros are not a panacea. There are many significant problems that can occur, including:&lt;br /&gt;
*  Accidentally overwriting bindings by introducing a variable name that was previously defined in a macro.&lt;br /&gt;
*  Difficulty tracing problems because the inputs and the outputs of the macros are implicit, not explicit.&lt;br /&gt;
&lt;br /&gt;
These can be largely avoided by doing a few things in conjunction with your boilerplate macros:&lt;br /&gt;
*  Have a naming convention which clearly labels macros as such, as well as indicate that a variable came from boilerplate code. This could be done by affixing -m to macros and -b to variables defined within a boilerplate.&lt;br /&gt;
*  Carefully document all boilerplate macros, especially the introduced variable bindings and all changes between versions.&lt;br /&gt;
*  Only use boilerplate macros when the savings in repetitiveness clearly outweigh the negatives of implicit functionality.&lt;br /&gt;
&lt;br /&gt;
===Abbreviate statements and prevent mistakes===&lt;br /&gt;
A lot of programming languages make you write really verbose statements to do really simple things. Code-generating programs allow you to abbreviate such statements and save a lot of typing, which also prevents a lot of mistakes because there is less chance of mistyping.&lt;br /&gt;
&lt;br /&gt;
=Metaprogramming Framework in Java=&lt;br /&gt;
==Reflection==&lt;br /&gt;
===Introduction to reflection===&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_(computer_programming) Reflection] is the ability to introspect metalevel information about the program structure itself at runtime. Usually this metalevel information is modeled using the general abstraction mechanisms available in the language. In Java, reflection enables to discover information about the loaded classes:&lt;br /&gt;
*  Fields,&lt;br /&gt;
*  Methods, and&lt;br /&gt;
*  Constructors&lt;br /&gt;
*  Generics information&lt;br /&gt;
*  Metadata annotations&lt;br /&gt;
It also enables to use these metaobjects to their instances in runtime environment.&lt;br /&gt;
&lt;br /&gt;
The following is the Java metalevel architecture&amp;lt;ref name=&amp;quot;reflection&amp;quot;&amp;gt;Mika Haapakorpi, &amp;quot;Meta Programming In Java&amp;quot;&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Metalevel architecture.PNG]]&lt;br /&gt;
&lt;br /&gt;
===Dynamic proxy classes===&lt;br /&gt;
A dynamic proxy class implements a list of interfaces specified at runtime when the class is created.&lt;br /&gt;
*  A proxy interface is an interface that is implemented by a proxy class.&lt;br /&gt;
*  A proxy instance is an instance of a proxy class which has an associated invocation handler object.&lt;br /&gt;
The following is an example of dynamic proxy classes&lt;br /&gt;
&lt;br /&gt;
[[File:Dynamic_proxy_class.PNG]]&lt;br /&gt;
&lt;br /&gt;
==Generics==&lt;br /&gt;
Generics are a facility of generic programming that was added to the Java programming language in 2004 as part of J2SE 5.0. They allow &amp;quot;a type or method to operate on objects of various types while providing compile-time type safety.&amp;quot;  The generics can be used in classes, interfaces, methods and constructors.&lt;br /&gt;
===Two new types in generics===&lt;br /&gt;
Two new types in generics:&lt;br /&gt;
*  Parametrized types&lt;br /&gt;
*  Type variables&lt;br /&gt;
&lt;br /&gt;
A type variable is an unqualified identifier. Class and interface declarations can have type arguments (type variables). Method and constructors definitions can have type arguments (type variables). &lt;br /&gt;
&lt;br /&gt;
The following is an example for type variables&lt;br /&gt;
&lt;br /&gt;
       List&amp;lt;String&amp;gt; anExample = new ArrayList&amp;lt;String&amp;gt;()&lt;br /&gt;
&lt;br /&gt;
*  List : interface&lt;br /&gt;
*  ArrayList : class&lt;br /&gt;
*  String : class (the actual type argument)&lt;br /&gt;
*  List&amp;lt;String&amp;gt; and ArrayList&amp;lt;String&amp;gt; : parametrized type&lt;br /&gt;
&lt;br /&gt;
===Class/Interface Declarations===&lt;br /&gt;
The following is an example of class declaration:&lt;br /&gt;
&lt;br /&gt;
       public interface List&amp;lt;E&amp;gt; {&lt;br /&gt;
         void add(Ex);&lt;br /&gt;
         Iterator&amp;lt;E&amp;gt; iterator();&lt;br /&gt;
       }&lt;br /&gt;
       public interface Iterator&amp;lt;E&amp;gt; {&lt;br /&gt;
         E next();&lt;br /&gt;
         booleanhasNext();&lt;br /&gt;
       }&lt;br /&gt;
       List&amp;lt;String&amp;gt; anExample;&lt;br /&gt;
       anExample.add(”sdfdfss”);&lt;br /&gt;
       anExample.add(new Object()); // compile time error&lt;br /&gt;
       String aTest = anExample.iterator().next();&lt;br /&gt;
&lt;br /&gt;
==Metadata annotation==&lt;br /&gt;
An annotation, in the Java computer programming language, is a form of syntactic metadata that can be added to Java source code. Classes, methods, variables, parameters and packages may be annotated. &lt;br /&gt;
&lt;br /&gt;
The following is an example of annotation:&lt;br /&gt;
&lt;br /&gt;
        /**&lt;br /&gt;
        * Designates a formatter to pretty-print the annotated class.&lt;br /&gt;
        */&lt;br /&gt;
        public @interface PrettyPrinter{&lt;br /&gt;
        Class&amp;lt;? extends Formatter&amp;gt; value();&lt;br /&gt;
        }&lt;br /&gt;
        // Single-member annotation with Class&lt;br /&gt;
        // member restricted by bounded wildcard&lt;br /&gt;
        // The annotation presumes the existence of this class.&lt;br /&gt;
        class GorgeousFormatterimplements Formatter { ... }&lt;br /&gt;
        @PrettyPrinter(GorgeousFormatter.class)&lt;br /&gt;
        public class Petunia { ... }&lt;br /&gt;
&lt;br /&gt;
‘@’ indicates the start of an annotation definition.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Cyang14</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1_1h_jc&amp;diff=73613</id>
		<title>CSC/ECE 517 Spring 2013/ch1 1h jc</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1_1h_jc&amp;diff=73613"/>
		<updated>2013-02-24T16:42:55Z</updated>

		<summary type="html">&lt;p&gt;Cyang14: /* Introduction to reflection */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Metaprogramming in statically typed languages&lt;br /&gt;
=Metaprogramming in statically typed languages=&lt;br /&gt;
==Introduction==&lt;br /&gt;
===What is metaprogramming===&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming] refers to writing programs that can manipulate other programs or themselves. The program that is doing the manipulating is called the ''metaprogram'', and the program that is being manipulated is called the ''object program''. The language of the ''metaprogram'' is referred to as the ''metalanguage'' and the language of the object program is called the ''object language''. A language has the ability of [http://en.wikipedia.org/wiki/Reflection_(computer_programming) reflection] if it can be used to write a metaprogram that can manipulate a program written in the same language.&lt;br /&gt;
&lt;br /&gt;
===Metaprogramming in statically typed languages===&lt;br /&gt;
&lt;br /&gt;
Metaprogramming can be accomplished in both statically and dynamically typed languages, however [http://en.wikipedia.org/wiki/Dynamically_typed_language#Dynamic_typing dynamically typed languages] have some advantages over [http://en.wikipedia.org/wiki/Dynamically_typed_language#Dynamic_typing statically typed languages] when it comes to metaprogramming.&lt;br /&gt;
&lt;br /&gt;
One example of a metaprogramming task is to add a method to a particular class at runtime. Sample code in Ruby to perform this task is outlined below. First is a definition of a Ruby class called TestAddMethodAtRuntime:&lt;br /&gt;
&lt;br /&gt;
  class TestAddMethodAtRuntime&lt;br /&gt;
    def originalMethod&lt;br /&gt;
        puts &amp;quot;Original Method&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The code below created a new instance of the class. After that, it defines a new method called newlyAddedMethod. Then the original method is called, follow by the newly added method at runtime:&lt;br /&gt;
&lt;br /&gt;
  t = TestAddMethodAtRuntime.new&lt;br /&gt;
  def t.newlyAddedMethod&lt;br /&gt;
    puts &amp;quot;Newly Added Method&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  t.originalMethod&lt;br /&gt;
  puts &amp;quot;\n&amp;quot;&lt;br /&gt;
  t.newlyAddedMethod&lt;br /&gt;
&lt;br /&gt;
Below is the output of the code above:&lt;br /&gt;
&lt;br /&gt;
  Original Method&lt;br /&gt;
  Newly Added Method&lt;br /&gt;
&lt;br /&gt;
Accomplishing something like this in a statically typed language would be much more difficult or impossible depending on the language. It is for this reason that dynamically typed languages are usually viewed as being better equipped to handle metaprogramming overall.&lt;br /&gt;
&lt;br /&gt;
==Implementation==&lt;br /&gt;
===Programming Language API===&lt;br /&gt;
&lt;br /&gt;
One way that metaprogramming can be possible in a language is through an [http://en.wikipedia.org/wiki/API API] that helps programmers achieve a metaprogramming task. An example of this is a language that provides an API for reflection. One type of reflection is ''introspection'' in which the program can access the source code of the program itself.&lt;br /&gt;
&lt;br /&gt;
Below is an example from the Java API to illustrate how an API can be used to achieve some metaprogramming tasks:&lt;br /&gt;
&lt;br /&gt;
   import java.lang.reflect.*; &lt;br /&gt;
   public class DumpMethods {&lt;br /&gt;
   public static void main(String args[]) {&lt;br /&gt;
     	try {&lt;br /&gt;
           Class c = Class.forName(args[0]);&lt;br /&gt;
           Method m[] = c.getDeclaredMethods();&lt;br /&gt;
           for (int i = 0; i &amp;lt; m.length; i++) {&lt;br /&gt;
              System.out.println(m[i].toString());&lt;br /&gt;
           }&lt;br /&gt;
     	}&lt;br /&gt;
     	catch (Throwable e) {&lt;br /&gt;
        	System.err.println(e);&lt;br /&gt;
     	}&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
The above example prints out all of the methods that are declared for a particular class. This is an example of metaprogramming since the program is accessing the source code of itself.&lt;br /&gt;
&lt;br /&gt;
===Program transformation system===&lt;br /&gt;
&lt;br /&gt;
A program transformation system is something that takes a program as the input, and outputs a different program. One example of a program transformation system is the Java compiler. The Java compiler takes a Java program as its input, and outputs a .class file with platform independent bytecode in it. Another example of a program transformation system is a Java decompiler, which takes bytecode as an input and outputs a Java program.&lt;br /&gt;
&lt;br /&gt;
In the case of the Java compiler, the program input is being output into another program in a different &amp;quot;language&amp;quot;. There are other types of transformation systems that take a program as an input and output a different program in the same language. One example of this would be ''program migration'' which migrates source code to a newer or older version of the same language. This can be useful if upgrading a software system to run on a newer version of a framework.&lt;br /&gt;
&lt;br /&gt;
==Common Uses==&lt;br /&gt;
===Pre-generate static data at compile time===&lt;br /&gt;
One common use of metaprogramming in statically typed languages is to write programs that will pre-generate tables of data for use at runtime.&amp;lt;ref name=&amp;quot;use1&amp;quot;&amp;gt;Jonathan Bartlett, &amp;quot;The art of metaprogramming, Part 1: Introduction to metaprogramming&amp;quot;, 20 Oct 2005   http://www.ibm.com/developerworks/library/l-metaprog1/index.html#N10052&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One simple but useful code generator is to build static lookup tables. Often, in order to build fast functions in C programming, we simply create a lookup table of all of the answers. This means that we either need to pre-compute them by hand (which is wasteful of your time) or build them at runtime (which is wasteful of the user's time).&lt;br /&gt;
&lt;br /&gt;
In following example we will build a generator that will take a function or set of functions on an integer and build lookup tables for the answer.&lt;br /&gt;
&lt;br /&gt;
To think of how to make such a program, we can start from the end and work backward. Firstly we need a lookup table that will return square roots of numbers between 5 and 20. A simple program can be written to generate such a table like this:&lt;br /&gt;
&lt;br /&gt;
Generate and use a lookup table of square roots&lt;br /&gt;
&lt;br /&gt;
        /* our lookup table */&lt;br /&gt;
        double square_roots[21];&lt;br /&gt;
        /* function to load the table at runtime */&lt;br /&gt;
        void init_square_roots()&lt;br /&gt;
        {&lt;br /&gt;
           int i;&lt;br /&gt;
           for(i = 5; i &amp;lt; 21; i++)&lt;br /&gt;
             {&lt;br /&gt;
               square_roots[i] = sqrt((double)i);&lt;br /&gt;
             }&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
With that single macro, we can take away a lot of work for any program that has to generate mathematical tables indexed by integer. A little extra work would also allow tables containing full struct definitions; a little more would ensure that space isn't wasted at the front of the array with useless empty entries.&lt;br /&gt;
        /* program that uses the table */&lt;br /&gt;
        int main ()&lt;br /&gt;
        {&lt;br /&gt;
           init_square_roots();&lt;br /&gt;
           printf(&amp;quot;The square root of 5 is %f\n&amp;quot;, square_roots[5]);&lt;br /&gt;
           return 0;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
Now, to convert this to a statically initialized array, you would remove the first part of the program and replace it with something like this, calculated by hand:&lt;br /&gt;
&lt;br /&gt;
Square root program with a static lookup table&lt;br /&gt;
&lt;br /&gt;
        double square_roots[] = {&lt;br /&gt;
           /* these are the ones we skipped */ 0.0, 0.0, 0.0, 0.0, 0.0&lt;br /&gt;
           2.236068, /* Square root of 5 */&lt;br /&gt;
           2.449490, /* Square root of 6 */&lt;br /&gt;
           2.645751, /* Square root of 7 */&lt;br /&gt;
           2.828427, /* Square root of 8 */&lt;br /&gt;
           3.0, /* Square root of 9 */&lt;br /&gt;
           ...&lt;br /&gt;
           4.472136 /* Square root of 20 */&lt;br /&gt;
           };&lt;br /&gt;
&lt;br /&gt;
What is needed is a program that will produce these values and print them out in a table like the previous one so they are loaded in at compile-time.&lt;br /&gt;
&lt;br /&gt;
Code generator for the table macro&lt;br /&gt;
&lt;br /&gt;
           #!/usr/bin/perl  &lt;br /&gt;
           #&lt;br /&gt;
           #tablegen.pl &lt;br /&gt;
           #&lt;br /&gt;
           ##Puts each program line into $line&lt;br /&gt;
           while(my $line = &amp;lt;&amp;gt;)&lt;br /&gt;
           {&lt;br /&gt;
             #Is this a macro invocation?&lt;br /&gt;
             if($line =~ m/TABLE:/)&lt;br /&gt;
             {&lt;br /&gt;
                #If so, split it apart into its component pieces&lt;br /&gt;
                my ($dummy, $table_name, $type, $start_idx, $end_idx, $default, $procedure) = split(m/:/, $line, 7);&lt;br /&gt;
                #The main difference between C and Perl for mathematical expressions is that&lt;br /&gt;
                #Perl prefixes its variables with a dollar sign, so we will add that here&lt;br /&gt;
                $procedure =~ s/VAL/\$VAL/g;&lt;br /&gt;
                #Print out the array declaration&lt;br /&gt;
                print &amp;quot;${type} ${table_name} [] = {\n&amp;quot;;&lt;br /&gt;
                #Go through each array element&lt;br /&gt;
                foreach my $VAL (0 .. $end_idx)&lt;br /&gt;
                {&lt;br /&gt;
                   #Only process an answer if we have reached our starting index&lt;br /&gt;
                   if($VAL &amp;gt;= $start_idx)&lt;br /&gt;
                   {&lt;br /&gt;
                      #evaluate the procedure specified (this sets $@ if there are any errors)&lt;br /&gt;
                      $result = eval $procedure;&lt;br /&gt;
                      die(&amp;quot;Error processing: $@&amp;quot;) if $@;&lt;br /&gt;
                   }&lt;br /&gt;
                   else&lt;br /&gt;
                   {&lt;br /&gt;
                      #if we haven't reached the starting index, just use the default&lt;br /&gt;
                      $result = $default;&lt;br /&gt;
                   }&lt;br /&gt;
                   #Print out the value&lt;br /&gt;
                   print &amp;quot;\t${result}&amp;quot;;&lt;br /&gt;
                   #If there are more to be processed, add a comma after the value&lt;br /&gt;
                   if($VAL != $end_idx)&lt;br /&gt;
                   {&lt;br /&gt;
                      print &amp;quot;,&amp;quot;;&lt;br /&gt;
                   }&lt;br /&gt;
                   print &amp;quot;\n&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
                #Finish the declaration&lt;br /&gt;
                print &amp;quot;};\n&amp;quot;;&lt;br /&gt;
             }&lt;br /&gt;
             else&lt;br /&gt;
             {&lt;br /&gt;
                #If this is not a macro invocation, just copy the line directly to the output&lt;br /&gt;
                print $line;&lt;br /&gt;
             }&lt;br /&gt;
           }&lt;br /&gt;
&lt;br /&gt;
To run this program, do this:&lt;br /&gt;
&lt;br /&gt;
Running the code generator&lt;br /&gt;
&lt;br /&gt;
           ./tablegen.pl &amp;lt; sqrt.in &amp;gt; sqrt.c&lt;br /&gt;
           gcc sqrt.c -o sqrt&lt;br /&gt;
           ./a.out&lt;br /&gt;
&lt;br /&gt;
===Mini-language for boiler-plate===&lt;br /&gt;
If you have a large application where many of the functions include a lot of boilerplate code, it is often a good idea to create a mini-language that allows you to work with your boilerplate code in an easier fashion. This mini-language will then be converted into your regular source code language before compiling.&amp;lt;ref name=&amp;quot;use2&amp;quot;&amp;gt;Jonathan Bartlett, &amp;quot;The art of metaprogramming, Part 2: Metaprogramming using Scheme&amp;quot;, 02 May 2006  http://www.ibm.com/developerworks/linux/library/l-metaprog2/index.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following is an example:&lt;br /&gt;
&lt;br /&gt;
Let's say that we are building a CGI application consisting of many independent CGI scripts. In most CGI applications, much of the state is stored in a database, but only a session ID is passed to each script via a cookie.&lt;br /&gt;
&lt;br /&gt;
However, in nearly every page we need to know the other standard information (such as the username, group number, the current job being worked on, whatever else information is pertinent). In addition, we need to redirect the user if they do not have an appropriate cookie.&lt;br /&gt;
&lt;br /&gt;
            (define (handle-cgi-request req)&lt;br /&gt;
              (let (&lt;br /&gt;
                     (session-id (webserver:cookie req &amp;quot;sessionid&amp;quot;)))&lt;br /&gt;
                (if (not (webserver:valid-session-id session-id))&lt;br /&gt;
                    (webserver:redirect-to-login-page)&lt;br /&gt;
                    (let (&lt;br /&gt;
                          (username (webserver:username-for-session session-id))&lt;br /&gt;
                          (group (webserver:group-for-user username))&lt;br /&gt;
                          (current-job (webserver:current-job-for-user username)))&lt;br /&gt;
                       ;;Code for processing goes here&lt;br /&gt;
                       ))))&lt;br /&gt;
&lt;br /&gt;
While some of that can be handled by a procedure, the bindings certainly cannot. However, we can turn most of it into a macro. The macro can be implemented like this:&lt;br /&gt;
&lt;br /&gt;
            (define-syntax cgi-boilerplate&lt;br /&gt;
              (lambda (x)&lt;br /&gt;
                (syntax-case x ()&lt;br /&gt;
                 (&lt;br /&gt;
                   (cgi-boilerplate expr)&lt;br /&gt;
                   (datum-&amp;gt;syntax-object&lt;br /&gt;
                     (syntax k)&lt;br /&gt;
                      (let (&lt;br /&gt;
                            (session-id (webserver:cookie req &amp;quot;sessionid&amp;quot;)))&lt;br /&gt;
                           (if (not (webserver:valid-session-id session-id))&lt;br /&gt;
                               (webserver:redirect-to-login-page)&lt;br /&gt;
                               (let (&lt;br /&gt;
                                      (username (webserver:username-for-session session-id))&lt;br /&gt;
                                      (group (webserver:group-for-user username))&lt;br /&gt;
                                      (current-job (webserver:current-job-for-user username)))&lt;br /&gt;
                                      (syntax-object-&amp;gt;datum (syntax expr))))))&lt;br /&gt;
                  )&lt;br /&gt;
             )))&lt;br /&gt;
&lt;br /&gt;
We can now create new forms based on our boilerplate code by doing the following:&lt;br /&gt;
&lt;br /&gt;
            (define (handle-cgi-request req)&lt;br /&gt;
              (cgi-boilerplate&lt;br /&gt;
               (begin&lt;br /&gt;
                 ;;Do whatever I want here&lt;br /&gt;
                 )))&lt;br /&gt;
&lt;br /&gt;
In addition, since we are not defining our variables explicitly, adding new variable definitions to our boilerplate won't affect its calling conventions, so new features can be added without having to create a whole new function.&lt;br /&gt;
&lt;br /&gt;
In any large project, there are inevitably templates to follow which cannot be reduced to functions, usually because of the bindings being created. Using boilerplate macros can make maintenance of such templated code much easier.&lt;br /&gt;
&lt;br /&gt;
Likewise, other standard macros can be created which make use of variables defined in the boilerplate. Using macros like this significantly reduces typing because you do not have to constantly be writing and rewriting variable bindings, derivations, and parameter passing. This also reduces the potential for errors in such code.&lt;br /&gt;
&lt;br /&gt;
Realize though that boilerplate macros are not a panacea. There are many significant problems that can occur, including:&lt;br /&gt;
*  Accidentally overwriting bindings by introducing a variable name that was previously defined in a macro.&lt;br /&gt;
*  Difficulty tracing problems because the inputs and the outputs of the macros are implicit, not explicit.&lt;br /&gt;
&lt;br /&gt;
These can be largely avoided by doing a few things in conjunction with your boilerplate macros:&lt;br /&gt;
*  Have a naming convention which clearly labels macros as such, as well as indicate that a variable came from boilerplate code. This could be done by affixing -m to macros and -b to variables defined within a boilerplate.&lt;br /&gt;
*  Carefully document all boilerplate macros, especially the introduced variable bindings and all changes between versions.&lt;br /&gt;
*  Only use boilerplate macros when the savings in repetitiveness clearly outweigh the negatives of implicit functionality.&lt;br /&gt;
&lt;br /&gt;
===Abbreviate statements and prevent mistakes===&lt;br /&gt;
A lot of programming languages make you write really verbose statements to do really simple things. Code-generating programs allow you to abbreviate such statements and save a lot of typing, which also prevents a lot of mistakes because there is less chance of mistyping.&lt;br /&gt;
&lt;br /&gt;
=Metaprogramming Framework in Java=&lt;br /&gt;
==Reflection==&lt;br /&gt;
===Introduction to reflection===&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_(computer_programming) Reflection] is the ability to introspect metalevel information about the program structure itself at runtime. Usually this metalevel information is modeled using the general abstraction mechanisms available in the language. In Java, reflection enables to discover information about the loaded classes:&lt;br /&gt;
*  Fields,&lt;br /&gt;
*  Methods, and&lt;br /&gt;
*  Constructors&lt;br /&gt;
*  Generics information&lt;br /&gt;
*  Metadata annotations&lt;br /&gt;
It also enables to use these metaobjects to their instances in runtime environment.&lt;br /&gt;
&lt;br /&gt;
The following is the Java metalevel architecture&lt;br /&gt;
&lt;br /&gt;
[[File:Metalevel architecture.PNG]]&lt;br /&gt;
&lt;br /&gt;
===Dynamic proxy classes===&lt;br /&gt;
A dynamic proxy class implements a list of interfaces specified at runtime when the class is created.&lt;br /&gt;
*  A proxy interface is an interface that is implemented by a proxy class.&lt;br /&gt;
*  A proxy instance is an instance of a proxy class which has an associated invocation handler object.&lt;br /&gt;
The following is an example of dynamic proxy classes&lt;br /&gt;
&lt;br /&gt;
[[File:Dynamic_proxy_class.PNG]]&lt;br /&gt;
&lt;br /&gt;
==Generics==&lt;br /&gt;
Generics are a facility of generic programming that was added to the Java programming language in 2004 as part of J2SE 5.0. They allow &amp;quot;a type or method to operate on objects of various types while providing compile-time type safety.&amp;quot;  The generics can be used in classes, interfaces, methods and constructors.&lt;br /&gt;
===Two new types in generics===&lt;br /&gt;
Two new types in generics:&lt;br /&gt;
*  Parametrized types&lt;br /&gt;
*  Type variables&lt;br /&gt;
&lt;br /&gt;
A type variable is an unqualified identifier. Class and interface declarations can have type arguments (type variables). Method and constructors definitions can have type arguments (type variables). &lt;br /&gt;
&lt;br /&gt;
The following is an example for type variables&lt;br /&gt;
&lt;br /&gt;
       List&amp;lt;String&amp;gt; anExample = new ArrayList&amp;lt;String&amp;gt;()&lt;br /&gt;
&lt;br /&gt;
*  List : interface&lt;br /&gt;
*  ArrayList : class&lt;br /&gt;
*  String : class (the actual type argument)&lt;br /&gt;
*  List&amp;lt;String&amp;gt; and ArrayList&amp;lt;String&amp;gt; : parametrized type&lt;br /&gt;
&lt;br /&gt;
===Class/Interface Declarations===&lt;br /&gt;
The following is an example of class declaration:&lt;br /&gt;
&lt;br /&gt;
       public interface List&amp;lt;E&amp;gt; {&lt;br /&gt;
         void add(Ex);&lt;br /&gt;
         Iterator&amp;lt;E&amp;gt; iterator();&lt;br /&gt;
       }&lt;br /&gt;
       public interface Iterator&amp;lt;E&amp;gt; {&lt;br /&gt;
         E next();&lt;br /&gt;
         booleanhasNext();&lt;br /&gt;
       }&lt;br /&gt;
       List&amp;lt;String&amp;gt; anExample;&lt;br /&gt;
       anExample.add(”sdfdfss”);&lt;br /&gt;
       anExample.add(new Object()); // compile time error&lt;br /&gt;
       String aTest = anExample.iterator().next();&lt;br /&gt;
&lt;br /&gt;
==Metadata annotation==&lt;br /&gt;
An annotation, in the Java computer programming language, is a form of syntactic metadata that can be added to Java source code. Classes, methods, variables, parameters and packages may be annotated. &lt;br /&gt;
&lt;br /&gt;
The following is an example of annotation:&lt;br /&gt;
&lt;br /&gt;
        /**&lt;br /&gt;
        * Designates a formatter to pretty-print the annotated class.&lt;br /&gt;
        */&lt;br /&gt;
        public @interface PrettyPrinter{&lt;br /&gt;
        Class&amp;lt;? extends Formatter&amp;gt; value();&lt;br /&gt;
        }&lt;br /&gt;
        // Single-member annotation with Class&lt;br /&gt;
        // member restricted by bounded wildcard&lt;br /&gt;
        // The annotation presumes the existence of this class.&lt;br /&gt;
        class GorgeousFormatterimplements Formatter { ... }&lt;br /&gt;
        @PrettyPrinter(GorgeousFormatter.class)&lt;br /&gt;
        public class Petunia { ... }&lt;br /&gt;
&lt;br /&gt;
‘@’ indicates the start of an annotation definition.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Cyang14</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1_1h_jc&amp;diff=73611</id>
		<title>CSC/ECE 517 Spring 2013/ch1 1h jc</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1_1h_jc&amp;diff=73611"/>
		<updated>2013-02-24T16:41:19Z</updated>

		<summary type="html">&lt;p&gt;Cyang14: /* Mini-language for boiler-plate */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Metaprogramming in statically typed languages&lt;br /&gt;
=Metaprogramming in statically typed languages=&lt;br /&gt;
==Introduction==&lt;br /&gt;
===What is metaprogramming===&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming] refers to writing programs that can manipulate other programs or themselves. The program that is doing the manipulating is called the ''metaprogram'', and the program that is being manipulated is called the ''object program''. The language of the ''metaprogram'' is referred to as the ''metalanguage'' and the language of the object program is called the ''object language''. A language has the ability of [http://en.wikipedia.org/wiki/Reflection_(computer_programming) reflection] if it can be used to write a metaprogram that can manipulate a program written in the same language.&lt;br /&gt;
&lt;br /&gt;
===Metaprogramming in statically typed languages===&lt;br /&gt;
&lt;br /&gt;
Metaprogramming can be accomplished in both statically and dynamically typed languages, however [http://en.wikipedia.org/wiki/Dynamically_typed_language#Dynamic_typing dynamically typed languages] have some advantages over [http://en.wikipedia.org/wiki/Dynamically_typed_language#Dynamic_typing statically typed languages] when it comes to metaprogramming.&lt;br /&gt;
&lt;br /&gt;
One example of a metaprogramming task is to add a method to a particular class at runtime. Sample code in Ruby to perform this task is outlined below. First is a definition of a Ruby class called TestAddMethodAtRuntime:&lt;br /&gt;
&lt;br /&gt;
  class TestAddMethodAtRuntime&lt;br /&gt;
    def originalMethod&lt;br /&gt;
        puts &amp;quot;Original Method&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The code below created a new instance of the class. After that, it defines a new method called newlyAddedMethod. Then the original method is called, follow by the newly added method at runtime:&lt;br /&gt;
&lt;br /&gt;
  t = TestAddMethodAtRuntime.new&lt;br /&gt;
  def t.newlyAddedMethod&lt;br /&gt;
    puts &amp;quot;Newly Added Method&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  t.originalMethod&lt;br /&gt;
  puts &amp;quot;\n&amp;quot;&lt;br /&gt;
  t.newlyAddedMethod&lt;br /&gt;
&lt;br /&gt;
Below is the output of the code above:&lt;br /&gt;
&lt;br /&gt;
  Original Method&lt;br /&gt;
  Newly Added Method&lt;br /&gt;
&lt;br /&gt;
Accomplishing something like this in a statically typed language would be much more difficult or impossible depending on the language. It is for this reason that dynamically typed languages are usually viewed as being better equipped to handle metaprogramming overall.&lt;br /&gt;
&lt;br /&gt;
==Implementation==&lt;br /&gt;
===Programming Language API===&lt;br /&gt;
&lt;br /&gt;
One way that metaprogramming can be possible in a language is through an [http://en.wikipedia.org/wiki/API API] that helps programmers achieve a metaprogramming task. An example of this is a language that provides an API for reflection. One type of reflection is ''introspection'' in which the program can access the source code of the program itself.&lt;br /&gt;
&lt;br /&gt;
Below is an example from the Java API to illustrate how an API can be used to achieve some metaprogramming tasks:&lt;br /&gt;
&lt;br /&gt;
   import java.lang.reflect.*; &lt;br /&gt;
   public class DumpMethods {&lt;br /&gt;
   public static void main(String args[]) {&lt;br /&gt;
     	try {&lt;br /&gt;
           Class c = Class.forName(args[0]);&lt;br /&gt;
           Method m[] = c.getDeclaredMethods();&lt;br /&gt;
           for (int i = 0; i &amp;lt; m.length; i++) {&lt;br /&gt;
              System.out.println(m[i].toString());&lt;br /&gt;
           }&lt;br /&gt;
     	}&lt;br /&gt;
     	catch (Throwable e) {&lt;br /&gt;
        	System.err.println(e);&lt;br /&gt;
     	}&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
The above example prints out all of the methods that are declared for a particular class. This is an example of metaprogramming since the program is accessing the source code of itself.&lt;br /&gt;
&lt;br /&gt;
===Program transformation system===&lt;br /&gt;
&lt;br /&gt;
A program transformation system is something that takes a program as the input, and outputs a different program. One example of a program transformation system is the Java compiler. The Java compiler takes a Java program as its input, and outputs a .class file with platform independent bytecode in it. Another example of a program transformation system is a Java decompiler, which takes bytecode as an input and outputs a Java program.&lt;br /&gt;
&lt;br /&gt;
In the case of the Java compiler, the program input is being output into another program in a different &amp;quot;language&amp;quot;. There are other types of transformation systems that take a program as an input and output a different program in the same language. One example of this would be ''program migration'' which migrates source code to a newer or older version of the same language. This can be useful if upgrading a software system to run on a newer version of a framework.&lt;br /&gt;
&lt;br /&gt;
==Common Uses==&lt;br /&gt;
===Pre-generate static data at compile time===&lt;br /&gt;
One common use of metaprogramming in statically typed languages is to write programs that will pre-generate tables of data for use at runtime.&amp;lt;ref name=&amp;quot;use1&amp;quot;&amp;gt;Jonathan Bartlett, &amp;quot;The art of metaprogramming, Part 1: Introduction to metaprogramming&amp;quot;, 20 Oct 2005   http://www.ibm.com/developerworks/library/l-metaprog1/index.html#N10052&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One simple but useful code generator is to build static lookup tables. Often, in order to build fast functions in C programming, we simply create a lookup table of all of the answers. This means that we either need to pre-compute them by hand (which is wasteful of your time) or build them at runtime (which is wasteful of the user's time).&lt;br /&gt;
&lt;br /&gt;
In following example we will build a generator that will take a function or set of functions on an integer and build lookup tables for the answer.&lt;br /&gt;
&lt;br /&gt;
To think of how to make such a program, we can start from the end and work backward. Firstly we need a lookup table that will return square roots of numbers between 5 and 20. A simple program can be written to generate such a table like this:&lt;br /&gt;
&lt;br /&gt;
Generate and use a lookup table of square roots&lt;br /&gt;
&lt;br /&gt;
        /* our lookup table */&lt;br /&gt;
        double square_roots[21];&lt;br /&gt;
        /* function to load the table at runtime */&lt;br /&gt;
        void init_square_roots()&lt;br /&gt;
        {&lt;br /&gt;
           int i;&lt;br /&gt;
           for(i = 5; i &amp;lt; 21; i++)&lt;br /&gt;
             {&lt;br /&gt;
               square_roots[i] = sqrt((double)i);&lt;br /&gt;
             }&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
With that single macro, we can take away a lot of work for any program that has to generate mathematical tables indexed by integer. A little extra work would also allow tables containing full struct definitions; a little more would ensure that space isn't wasted at the front of the array with useless empty entries.&lt;br /&gt;
        /* program that uses the table */&lt;br /&gt;
        int main ()&lt;br /&gt;
        {&lt;br /&gt;
           init_square_roots();&lt;br /&gt;
           printf(&amp;quot;The square root of 5 is %f\n&amp;quot;, square_roots[5]);&lt;br /&gt;
           return 0;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
Now, to convert this to a statically initialized array, you would remove the first part of the program and replace it with something like this, calculated by hand:&lt;br /&gt;
&lt;br /&gt;
Square root program with a static lookup table&lt;br /&gt;
&lt;br /&gt;
        double square_roots[] = {&lt;br /&gt;
           /* these are the ones we skipped */ 0.0, 0.0, 0.0, 0.0, 0.0&lt;br /&gt;
           2.236068, /* Square root of 5 */&lt;br /&gt;
           2.449490, /* Square root of 6 */&lt;br /&gt;
           2.645751, /* Square root of 7 */&lt;br /&gt;
           2.828427, /* Square root of 8 */&lt;br /&gt;
           3.0, /* Square root of 9 */&lt;br /&gt;
           ...&lt;br /&gt;
           4.472136 /* Square root of 20 */&lt;br /&gt;
           };&lt;br /&gt;
&lt;br /&gt;
What is needed is a program that will produce these values and print them out in a table like the previous one so they are loaded in at compile-time.&lt;br /&gt;
&lt;br /&gt;
Code generator for the table macro&lt;br /&gt;
&lt;br /&gt;
           #!/usr/bin/perl  &lt;br /&gt;
           #&lt;br /&gt;
           #tablegen.pl &lt;br /&gt;
           #&lt;br /&gt;
           ##Puts each program line into $line&lt;br /&gt;
           while(my $line = &amp;lt;&amp;gt;)&lt;br /&gt;
           {&lt;br /&gt;
             #Is this a macro invocation?&lt;br /&gt;
             if($line =~ m/TABLE:/)&lt;br /&gt;
             {&lt;br /&gt;
                #If so, split it apart into its component pieces&lt;br /&gt;
                my ($dummy, $table_name, $type, $start_idx, $end_idx, $default, $procedure) = split(m/:/, $line, 7);&lt;br /&gt;
                #The main difference between C and Perl for mathematical expressions is that&lt;br /&gt;
                #Perl prefixes its variables with a dollar sign, so we will add that here&lt;br /&gt;
                $procedure =~ s/VAL/\$VAL/g;&lt;br /&gt;
                #Print out the array declaration&lt;br /&gt;
                print &amp;quot;${type} ${table_name} [] = {\n&amp;quot;;&lt;br /&gt;
                #Go through each array element&lt;br /&gt;
                foreach my $VAL (0 .. $end_idx)&lt;br /&gt;
                {&lt;br /&gt;
                   #Only process an answer if we have reached our starting index&lt;br /&gt;
                   if($VAL &amp;gt;= $start_idx)&lt;br /&gt;
                   {&lt;br /&gt;
                      #evaluate the procedure specified (this sets $@ if there are any errors)&lt;br /&gt;
                      $result = eval $procedure;&lt;br /&gt;
                      die(&amp;quot;Error processing: $@&amp;quot;) if $@;&lt;br /&gt;
                   }&lt;br /&gt;
                   else&lt;br /&gt;
                   {&lt;br /&gt;
                      #if we haven't reached the starting index, just use the default&lt;br /&gt;
                      $result = $default;&lt;br /&gt;
                   }&lt;br /&gt;
                   #Print out the value&lt;br /&gt;
                   print &amp;quot;\t${result}&amp;quot;;&lt;br /&gt;
                   #If there are more to be processed, add a comma after the value&lt;br /&gt;
                   if($VAL != $end_idx)&lt;br /&gt;
                   {&lt;br /&gt;
                      print &amp;quot;,&amp;quot;;&lt;br /&gt;
                   }&lt;br /&gt;
                   print &amp;quot;\n&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
                #Finish the declaration&lt;br /&gt;
                print &amp;quot;};\n&amp;quot;;&lt;br /&gt;
             }&lt;br /&gt;
             else&lt;br /&gt;
             {&lt;br /&gt;
                #If this is not a macro invocation, just copy the line directly to the output&lt;br /&gt;
                print $line;&lt;br /&gt;
             }&lt;br /&gt;
           }&lt;br /&gt;
&lt;br /&gt;
To run this program, do this:&lt;br /&gt;
&lt;br /&gt;
Running the code generator&lt;br /&gt;
&lt;br /&gt;
           ./tablegen.pl &amp;lt; sqrt.in &amp;gt; sqrt.c&lt;br /&gt;
           gcc sqrt.c -o sqrt&lt;br /&gt;
           ./a.out&lt;br /&gt;
&lt;br /&gt;
===Mini-language for boiler-plate===&lt;br /&gt;
If you have a large application where many of the functions include a lot of boilerplate code, it is often a good idea to create a mini-language that allows you to work with your boilerplate code in an easier fashion. This mini-language will then be converted into your regular source code language before compiling.&amp;lt;ref name=&amp;quot;use2&amp;quot;&amp;gt;Jonathan Bartlett, &amp;quot;The art of metaprogramming, Part 2: Metaprogramming using Scheme&amp;quot;, 02 May 2006  http://www.ibm.com/developerworks/linux/library/l-metaprog2/index.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following is an example:&lt;br /&gt;
&lt;br /&gt;
Let's say that we are building a CGI application consisting of many independent CGI scripts. In most CGI applications, much of the state is stored in a database, but only a session ID is passed to each script via a cookie.&lt;br /&gt;
&lt;br /&gt;
However, in nearly every page we need to know the other standard information (such as the username, group number, the current job being worked on, whatever else information is pertinent). In addition, we need to redirect the user if they do not have an appropriate cookie.&lt;br /&gt;
&lt;br /&gt;
            (define (handle-cgi-request req)&lt;br /&gt;
              (let (&lt;br /&gt;
                     (session-id (webserver:cookie req &amp;quot;sessionid&amp;quot;)))&lt;br /&gt;
                (if (not (webserver:valid-session-id session-id))&lt;br /&gt;
                    (webserver:redirect-to-login-page)&lt;br /&gt;
                    (let (&lt;br /&gt;
                          (username (webserver:username-for-session session-id))&lt;br /&gt;
                          (group (webserver:group-for-user username))&lt;br /&gt;
                          (current-job (webserver:current-job-for-user username)))&lt;br /&gt;
                       ;;Code for processing goes here&lt;br /&gt;
                       ))))&lt;br /&gt;
&lt;br /&gt;
While some of that can be handled by a procedure, the bindings certainly cannot. However, we can turn most of it into a macro. The macro can be implemented like this:&lt;br /&gt;
&lt;br /&gt;
            (define-syntax cgi-boilerplate&lt;br /&gt;
              (lambda (x)&lt;br /&gt;
                (syntax-case x ()&lt;br /&gt;
                 (&lt;br /&gt;
                   (cgi-boilerplate expr)&lt;br /&gt;
                   (datum-&amp;gt;syntax-object&lt;br /&gt;
                     (syntax k)&lt;br /&gt;
                      (let (&lt;br /&gt;
                            (session-id (webserver:cookie req &amp;quot;sessionid&amp;quot;)))&lt;br /&gt;
                           (if (not (webserver:valid-session-id session-id))&lt;br /&gt;
                               (webserver:redirect-to-login-page)&lt;br /&gt;
                               (let (&lt;br /&gt;
                                      (username (webserver:username-for-session session-id))&lt;br /&gt;
                                      (group (webserver:group-for-user username))&lt;br /&gt;
                                      (current-job (webserver:current-job-for-user username)))&lt;br /&gt;
                                      (syntax-object-&amp;gt;datum (syntax expr))))))&lt;br /&gt;
                  )&lt;br /&gt;
             )))&lt;br /&gt;
&lt;br /&gt;
We can now create new forms based on our boilerplate code by doing the following:&lt;br /&gt;
&lt;br /&gt;
            (define (handle-cgi-request req)&lt;br /&gt;
              (cgi-boilerplate&lt;br /&gt;
               (begin&lt;br /&gt;
                 ;;Do whatever I want here&lt;br /&gt;
                 )))&lt;br /&gt;
&lt;br /&gt;
In addition, since we are not defining our variables explicitly, adding new variable definitions to our boilerplate won't affect its calling conventions, so new features can be added without having to create a whole new function.&lt;br /&gt;
&lt;br /&gt;
In any large project, there are inevitably templates to follow which cannot be reduced to functions, usually because of the bindings being created. Using boilerplate macros can make maintenance of such templated code much easier.&lt;br /&gt;
&lt;br /&gt;
Likewise, other standard macros can be created which make use of variables defined in the boilerplate. Using macros like this significantly reduces typing because you do not have to constantly be writing and rewriting variable bindings, derivations, and parameter passing. This also reduces the potential for errors in such code.&lt;br /&gt;
&lt;br /&gt;
Realize though that boilerplate macros are not a panacea. There are many significant problems that can occur, including:&lt;br /&gt;
*  Accidentally overwriting bindings by introducing a variable name that was previously defined in a macro.&lt;br /&gt;
*  Difficulty tracing problems because the inputs and the outputs of the macros are implicit, not explicit.&lt;br /&gt;
&lt;br /&gt;
These can be largely avoided by doing a few things in conjunction with your boilerplate macros:&lt;br /&gt;
*  Have a naming convention which clearly labels macros as such, as well as indicate that a variable came from boilerplate code. This could be done by affixing -m to macros and -b to variables defined within a boilerplate.&lt;br /&gt;
*  Carefully document all boilerplate macros, especially the introduced variable bindings and all changes between versions.&lt;br /&gt;
*  Only use boilerplate macros when the savings in repetitiveness clearly outweigh the negatives of implicit functionality.&lt;br /&gt;
&lt;br /&gt;
===Abbreviate statements and prevent mistakes===&lt;br /&gt;
A lot of programming languages make you write really verbose statements to do really simple things. Code-generating programs allow you to abbreviate such statements and save a lot of typing, which also prevents a lot of mistakes because there is less chance of mistyping.&lt;br /&gt;
&lt;br /&gt;
=Metaprogramming Framework in Java=&lt;br /&gt;
==Reflection==&lt;br /&gt;
===Introduction to reflection===&lt;br /&gt;
Reflection is the ability to introspect metalevel information about the program structure itself at runtime. Usually this metalevel information is modeled using the general abstraction mechanisms available in the language. In Java, reflection enables to discover information about the loaded classes:&lt;br /&gt;
*  Fields,&lt;br /&gt;
*  Methods, and&lt;br /&gt;
*  Constructors&lt;br /&gt;
*  Generics information&lt;br /&gt;
*  Metadata annotations&lt;br /&gt;
It also enables to use these metaobjects to their instances in runtime environment.&lt;br /&gt;
&lt;br /&gt;
The following is the Java metalevel architecture&lt;br /&gt;
&lt;br /&gt;
[[File:Metalevel architecture.PNG]]&lt;br /&gt;
&lt;br /&gt;
===Dynamic proxy classes===&lt;br /&gt;
A dynamic proxy class implements a list of interfaces specified at runtime when the class is created.&lt;br /&gt;
*  A proxy interface is an interface that is implemented by a proxy class.&lt;br /&gt;
*  A proxy instance is an instance of a proxy class which has an associated invocation handler object.&lt;br /&gt;
The following is an example of dynamic proxy classes&lt;br /&gt;
&lt;br /&gt;
[[File:Dynamic_proxy_class.PNG]]&lt;br /&gt;
&lt;br /&gt;
==Generics==&lt;br /&gt;
Generics are a facility of generic programming that was added to the Java programming language in 2004 as part of J2SE 5.0. They allow &amp;quot;a type or method to operate on objects of various types while providing compile-time type safety.&amp;quot;  The generics can be used in classes, interfaces, methods and constructors.&lt;br /&gt;
===Two new types in generics===&lt;br /&gt;
Two new types in generics:&lt;br /&gt;
*  Parametrized types&lt;br /&gt;
*  Type variables&lt;br /&gt;
&lt;br /&gt;
A type variable is an unqualified identifier. Class and interface declarations can have type arguments (type variables). Method and constructors definitions can have type arguments (type variables). &lt;br /&gt;
&lt;br /&gt;
The following is an example for type variables&lt;br /&gt;
&lt;br /&gt;
       List&amp;lt;String&amp;gt; anExample = new ArrayList&amp;lt;String&amp;gt;()&lt;br /&gt;
&lt;br /&gt;
*  List : interface&lt;br /&gt;
*  ArrayList : class&lt;br /&gt;
*  String : class (the actual type argument)&lt;br /&gt;
*  List&amp;lt;String&amp;gt; and ArrayList&amp;lt;String&amp;gt; : parametrized type&lt;br /&gt;
&lt;br /&gt;
===Class/Interface Declarations===&lt;br /&gt;
The following is an example of class declaration:&lt;br /&gt;
&lt;br /&gt;
       public interface List&amp;lt;E&amp;gt; {&lt;br /&gt;
         void add(Ex);&lt;br /&gt;
         Iterator&amp;lt;E&amp;gt; iterator();&lt;br /&gt;
       }&lt;br /&gt;
       public interface Iterator&amp;lt;E&amp;gt; {&lt;br /&gt;
         E next();&lt;br /&gt;
         booleanhasNext();&lt;br /&gt;
       }&lt;br /&gt;
       List&amp;lt;String&amp;gt; anExample;&lt;br /&gt;
       anExample.add(”sdfdfss”);&lt;br /&gt;
       anExample.add(new Object()); // compile time error&lt;br /&gt;
       String aTest = anExample.iterator().next();&lt;br /&gt;
&lt;br /&gt;
==Metadata annotation==&lt;br /&gt;
An annotation, in the Java computer programming language, is a form of syntactic metadata that can be added to Java source code. Classes, methods, variables, parameters and packages may be annotated. &lt;br /&gt;
&lt;br /&gt;
The following is an example of annotation:&lt;br /&gt;
&lt;br /&gt;
        /**&lt;br /&gt;
        * Designates a formatter to pretty-print the annotated class.&lt;br /&gt;
        */&lt;br /&gt;
        public @interface PrettyPrinter{&lt;br /&gt;
        Class&amp;lt;? extends Formatter&amp;gt; value();&lt;br /&gt;
        }&lt;br /&gt;
        // Single-member annotation with Class&lt;br /&gt;
        // member restricted by bounded wildcard&lt;br /&gt;
        // The annotation presumes the existence of this class.&lt;br /&gt;
        class GorgeousFormatterimplements Formatter { ... }&lt;br /&gt;
        @PrettyPrinter(GorgeousFormatter.class)&lt;br /&gt;
        public class Petunia { ... }&lt;br /&gt;
&lt;br /&gt;
‘@’ indicates the start of an annotation definition.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Cyang14</name></author>
	</entry>
</feed>