<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Dimuthu's Blog &#187; algorithm</title>
	<atom:link href="http://www.dimuthu.org/catagory/algorithm/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dimuthu.org</link>
	<description>Waiting for your comments</description>
	<lastBuildDate>Wed, 07 Jul 2010 12:42:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
<image>
  <link>http://www.dimuthu.org</link>
  <url>http://www.dimuthu.org/favicon.ico</url>
  <title>Dimuthu's Blog</title>
</image>
		<item>
		<title>Organic Chemistry &#8211; Reaction Simulator</title>
		<link>http://www.dimuthu.org/blog/2008/11/22/organic-chemistry-reaction-simulator/</link>
		<comments>http://www.dimuthu.org/blog/2008/11/22/organic-chemistry-reaction-simulator/#comments</comments>
		<pubDate>Sat, 22 Nov 2008 05:24:24 +0000</pubDate>
		<dc:creator>dimuthu</dc:creator>
				<category><![CDATA[Organic Chemistry]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[personal]]></category>
		<category><![CDATA[screenshots]]></category>
		<category><![CDATA[xml]]></category>
		<category><![CDATA[G.C.E. A/L]]></category>
		<category><![CDATA[Reaction Simulator]]></category>

		<guid isPermaLink="false">http://www.dimuthu.org/?p=683</guid>
		<description><![CDATA[Organic Reaction Simulator It is a tool that simulates the organic reactions and automatically generate the IUPAC names for the organic compounds drawn by you. This covers most of the syllabus of the Organic Chemistry for G.C.E. A/L examination. Features Nice Canvas to draw your Organic compounds. Generates IUPAC names for the drawn component as [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Organic Reaction Simulator</strong></p>
<p>It is a tool that simulates the organic reactions and automatically generate the IUPAC names for the organic compounds drawn by you. This covers most of the syllabus of the Organic Chemistry for G.C.E. A/L examination.</p>
<p><strong>Features</strong></p>
<ul>
<li> Nice Canvas to draw your Organic compounds.</li>
<li>Generates IUPAC names for the drawn component as possible. (It generates IUPAC names for almost all the compounds which G.C.E A/L examination expect students to know)</li>
<li>You can simulate reaction for organic compounds with preferred inorganic/organic compounds and the selected conditions. (Currently supporting 168 reactions)</li>
<li>Extensibility of adding IUPAC naming rules without the need of recompiling the code.</li>
<li>Extensibility of adding custom reactions, again without compiling a single line of code.</li>
</ul>
<p>Here is a screen shot of the main panel when &#8216;Aldole reaction&#8217; is simulated for some simple reactants.</p>
<div id="attachment_690" class="wp-caption aligncenter" style="width: 510px"><a href="http://www.dimuthu.org/wp-content/uploads/2008/11/aldole.jpg"><img class="size-full wp-image-690" title="Aldole Reaction - Reaction Simulator" src="http://www.dimuthu.org/wp-content/uploads/2008/11/aldole.jpg" alt="Aldole Reaction - Reaction Simulator" width="500" height="392" /></a><p class="wp-caption-text">Aldole Reaction - Reaction Simulator</p></div>
<p><strong>The Drawing Panel</strong></p>
<p>The &#8216;Reaction Simulator&#8217; app provides 2 views for the user. One is the main panel which shows and manages reactions which is shown in the above figure. The other view is the drawing panel. It will be not much different from your favorite drawing application.</p>
<div id="attachment_692" class="wp-caption aligncenter" style="width: 510px"><a href="http://www.dimuthu.org/wp-content/uploads/2008/11/drawing_panel.jpg"><img class="size-full wp-image-692" title="Drawing Panel - Reaction Simulator" src="http://www.dimuthu.org/wp-content/uploads/2008/11/drawing_panel.jpg" alt="Drawing Panel - Reaction Simulator" width="500" height="392" /></a><p class="wp-caption-text">Drawing Panel - Reaction Simulator</p></div>
<p>Here you can select elements and bond types to draw your organic compound. Additionally It has tools to move or delete selected parts of your drawing.</p>
<p>The other thing you may have already noticed, you don&#8217;t need to complete all the bonds for a particular element. Rather if you keep one side of a single bond unconnected, it will be automatically connected to a &#8216;H&#8217;(Hydrogen) at the rendering, similarly for double bond the default connecting element would be &#8216;O&#8217;(Oxygen), and for triple bond it is &#8216;N&#8217; (Nytrogen). And you don&#8217;t even need to bother about connecting bonds with an element, as if you connected only one bond to &#8216;C&#8217; all the other 3 possible bonds will be considered as connected with &#8216;H&#8217;s.</p>
<p>Just check the drawing panel on your own and find its user friendliness. Now after you finish designing your compound, just press &#8220;RETURN&#8221;.</p>
<div id="attachment_694" class="wp-caption aligncenter" style="width: 510px"><a href="http://www.dimuthu.org/wp-content/uploads/2008/11/iupac.jpg"><img class="size-full wp-image-694" title="IUPAC Name Generator - Reaction Simulator" src="http://www.dimuthu.org/wp-content/uploads/2008/11/iupac.jpg" alt="IUPAC Name Generator - Reaction Simulator" width="500" height="392" /></a><p class="wp-caption-text">IUPAC Name Generator - Reaction Simulator</p></div>
<p>Yea it will nicely render our compound, and look at the bottom, it has generated the IUPAC name for the compound.</p>
<p><strong>The Main Panel</strong></p>
<p>This contain a canvas that you can add, edit and delete organic compounds (so you will be directed to the &#8216;Drawing Panel&#8217;), some sidebar panels to select Inorganic compounds and additional conditions requires for the reactions and a set of buttons to start reactions and manage the result set. At the end of the sidebar there is a &#8216;help&#8217; button that directed you to the user guide.</p>
<p><strong>Extending IUPAC Naming Rules</strong></p>
<p>If you go to the &#8220;Data/IUPAC/&#8221; directory (you can view svn from here, <a href="http://svn.dimuthu.org/organic_chemistry/Organic_Reaction_Simulator/Data/IUPAC/">http://svn.dimuthu.org/organic_chemistry/Organic_Reaction_Simulator/Data/IUPAC/</a>) you can find there are set of .txt files (infact xmls) that defines the IUPAC rules.</p>
<p>If you open one of them (we will take al.txt), it would be something like this</p>
<pre class="xml"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;IUPAC</span> <span style="color: #000066;">basename</span>=<span style="color: #ff0000;">"al"</span> <span style="color: #000066;">subname</span>=<span style="color: #ff0000;">"oxo"</span> <span style="color: #000066;">level</span>=<span style="color: #ff0000;">"6"</span> <span style="color: #000066;">nonumber</span>=<span style="color: #ff0000;">"true"</span> <span style="color: #000066;">affectto</span>=<span style="color: #ff0000;">"1"</span><span style="font-weight: bold; color: black;">&gt;</span></span>
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;bond</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">"2"</span><span style="font-weight: bold; color: black;">&gt;</span></span>
		<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;element</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">"7"</span><span style="font-weight: bold; color: black;">&gt;</span></span>
		<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/element<span style="font-weight: bold; color: black;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/bond<span style="font-weight: bold; color: black;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;bond</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">"1"</span><span style="font-weight: bold; color: black;">&gt;</span></span>
		<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;element</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">"1"</span><span style="font-weight: bold; color: black;">&gt;</span></span>
		<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/element<span style="font-weight: bold; color: black;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/bond<span style="font-weight: bold; color: black;">&gt;</span></span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/IUPAC<span style="font-weight: bold; color: black;">&gt;</span></span></span></pre>
<p>It is the naming rules for aldehyde compounds. (H-C=O). First it identify the the aldehyde by checking double bond to &#8216;O&#8217; (which is the element type &#8217;7&#8242;), and a single bond to &#8216;H&#8217; (which is the element type &#8217;1&#8242;). If the element group found, it will give the name &#8216;al&#8217; or &#8216;oxo&#8217; depending on whether it defines the main group of elements of the compound or not.</p>
<p>If you take a look at all these rules, you can have a good idea what each of these syntax mean. And may be you can add your own rules, if you find something missing or incorrect there.</p>
<p><strong>Extending the Reactions</strong></p>
<p>The reaction rules are stored in the &#8220;Data/Reactions/&#8221; directory ( <a href="http://svn.dimuthu.org/organic_chemistry/Organic_Reaction_Simulator/Data/Reactions/">http://svn.dimuthu.org/organic_chemistry/Organic_Reaction_Simulator/Data/Reactions/</a>).</p>
<p>I will take the first reaction we studied in the &#8216;Organic Chemistry&#8217; Class.</p>
<p>CH4 + Cl2 + hv  (dim light) &#8212;&#8212;&#8212;&#8212;&#8212;-&gt; CCl4 + H2</p>
<p>Here is the rule defining that reaction. (cl2hv.txt)</p>
<pre class="xml"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;reaction</span> <span style="color: #000066;">inorganics</span>=<span style="color: #ff0000;">"Cl2"</span> <span style="color: #000066;">conditions</span>=<span style="color: #ff0000;">"hv"</span><span style="font-weight: bold; color: black;">&gt;</span></span>
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;check<span style="font-weight: bold; color: black;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;bond</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">"1"</span><span style="font-weight: bold; color: black;">&gt;</span></span>
			<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;element</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">"H"</span><span style="font-weight: bold; color: black;">&gt;</span></span>
			<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/element<span style="font-weight: bold; color: black;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/bond<span style="font-weight: bold; color: black;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/check<span style="font-weight: bold; color: black;">&gt;</span></span></span>

	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;reordering</span> <span style="color: #000066;">activity</span>=<span style="color: #ff0000;">"remain"</span><span style="font-weight: bold; color: black;">&gt;</span></span>
		<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;bond</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">"1"</span><span style="font-weight: bold; color: black;">&gt;</span></span>
			<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;check<span style="font-weight: bold; color: black;">&gt;</span></span></span>
				<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;element</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">"H"</span><span style="font-weight: bold; color: black;">&gt;</span></span>
				<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/element<span style="font-weight: bold; color: black;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/check<span style="font-weight: bold; color: black;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;reordering</span> <span style="color: #000066;">activity</span>=<span style="color: #ff0000;">"remain"</span><span style="font-weight: bold; color: black;">&gt;</span></span>
				<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;element</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">"H"</span><span style="font-weight: bold; color: black;">&gt;</span></span>
					<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;reordering</span> <span style="color: #000066;">activity</span>=<span style="color: #ff0000;">"replace"</span> <span style="color: #000066;">to</span>=<span style="color: #ff0000;">"Cl"</span><span style="font-weight: bold; color: black;">&gt;</span></span>
					<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/reordering<span style="font-weight: bold; color: black;">&gt;</span></span></span>
				<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/element<span style="font-weight: bold; color: black;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/reordering<span style="font-weight: bold; color: black;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/bond<span style="font-weight: bold; color: black;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/reordering<span style="font-weight: bold; color: black;">&gt;</span></span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/reaction<span style="font-weight: bold; color: black;">&gt;</span></span></span></pre>
<p>If you take a close look at, what it says is if there exist inorganic &#8216;Cl2&#8242; and condition &#8216;hv&#8217; (reactions inorganics=&#8221;Cl2&#8243; conditions=&#8221;hv&#8221;), check for a &#8216;H&#8217; (element type=&#8221;H&#8221;) elements that is connected to a &#8216;C&#8217; element (which is the root of the XML) through a single bond (bond type=&#8221;1&#8243;), then don&#8217;t replace the &#8216;C&#8217; (reordering activity=&#8221;remain&#8221;) and don&#8217;t replace the bond type (reordering activity=&#8221;remain&#8221;), just replace the &#8216;H&#8217; with &#8216;Cl&#8217; (reordering activity=&#8221;replace&#8221; to=&#8221;Cl&#8221;). It is so simple as that.</p>
<p>These rules are applied to all the C elements in reactants. Look at the following figure for this rule in application.</p>
<div id="attachment_698" class="wp-caption aligncenter" style="width: 510px"><a href="http://www.dimuthu.org/wp-content/uploads/2008/11/cl2hv.jpg"><img class="size-full wp-image-698" title="Alkane + Cl2 Reaction - Reaction Simulator" src="http://www.dimuthu.org/wp-content/uploads/2008/11/cl2hv.jpg" alt="Alkane + Cl2 Reaction - Reaction Simulator" width="500" height="392" /></a><p class="wp-caption-text">Alkane + Cl2 Reaction - Reaction Simulator</p></div>
<p>These reaction rules can be as complex as you want. Specially when two or more reactants are involved in a reaction, rules defining that reaction will be little complex. Look at how aldole reaction (which is applied in the first figure of this post) is written in <a href="http://svn.dimuthu.org/organic_chemistry/Organic_Reaction_Simulator/Data/Reactions/aldole.txt">aldole.txt</a>.</p>
<p>There are all together 168 reactions currently defined in this way. If you found some reaction is missing, feel free to add a another rule file defining that reaction. And if you want to share that with others, just let me know <img src='http://www.dimuthu.org/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> , so I can integrate it in to the distribution.</p>
<p><strong>Download</strong></p>
<p>This software application is not yet in a official release. I just thought use this post to do a pre release of the &#8216;Reaction Simulator&#8217;. You can download the windows binary of the pre release version (343KB) from <a href="http://downloads.dimuthu.org/bins/chemistry/reaction_simulator_pre_release/reaction_simulator_pre_release.zip">http://downloads.dimuthu.org/bins/chemistry/reaction_simulator_pre_release/reaction_simulator_pre_release.zip</a></p>
<p><strong>Source Code</strong></p>
<p>SVN location for the source code of &#8216;Reaction Simulator&#8217; is <a href="http://svn.dimuthu.org/organic_chemistry/Organic_Reaction_Simulator/">http://svn.dimuthu.org/organic_chemistry/Organic_Reaction_Simulator/</a></p>
<p><strong>Known Limitations &#8211; Possible Improvements<br />
</strong></p>
<ul>
<li>Generation of IUPAC names and simulations of reactions are not supported for compounds which involves Benzene ring.</li>
<li>IUPAC names generation is not supported for cyclic compounds which are anyway not part of the G.C.E. A/L syllabus.</li>
<li>The set of elements, available in drawing compounds, set of inorganic compounds and conditions, available in reactions are fixed and cannot be extended without changing the code and recompiling.</li>
<li>You can&#8217;t start with an IUPAC name and derive the compound. Currently you always have to start with drawing the compound and then generate the IUPAC.</li>
<li>Only for windows!</li>
</ul>
<p><strong>Little Background</strong></p>
<p>4 years ago, When I was a level 2 student in the <a href="http://www.mrt.ac.lk/">University of Moratuwa</a>, I participated to a competition for making educational software tools organized by <a href="http://www.cse.mrt.ac.lk/">C.S.E</a> and <a href="http://www.nie.lk/">N.I.E (National Institute of Education)</a>. I submitted a software that teaches Organic Chemistry. It consisted of interactive tutorials targeting local G.C.E A/L exams with exercises and revisions in both Sinhala(My Mother tongue) and English languages(not in Unicode though). I got the second price for that.</p>
<p>After the award I decided to improve my software application by adding an &#8216;Organic Reaction Simulator&#8217;. In fact in the vacation of 2 weeks for the New Year, I could complete it.</p>
<p>Although the N.I.E supposed to distribute the applications submitted to the competition throughout the country, it didn&#8217;t happened. So I decided to publish at least the &#8216;Reaction Simulator&#8217; application which I actually didn&#8217;t submitted to the competition.</p>
<p><strong>Technologies Used</strong></p>
<p>This is completely written in C++. It hasn&#8217;t use MFC, because I thought MFC is too heavy for such a small application. I used a lightweight, small image library code taken with some custom changes from some windows game programming book.</p>
<p>This is using XML to load data about reactions and IUPAC names which I have described in details in the early part of the blog. It uses a small inbuilt xml parser (just one recursive function) to parse these xmls.</p>
<p>So it has no depenedencies to third party libraries, You can just chekcout the <a href="http://svn.dimuthu.org/organic_chemistry/Organic_Reaction_Simulator/">souce code from the svn</a>, open the visual studio project and compile it (press &#8216;F7&#8242;).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dimuthu.org/blog/2008/11/22/organic-chemistry-reaction-simulator/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Apache Axis2/C RESTful URL Mapping Algorithm</title>
		<link>http://www.dimuthu.org/blog/2008/11/21/apache-axis2c-restful-url-mapping-algorithm/</link>
		<comments>http://www.dimuthu.org/blog/2008/11/21/apache-axis2c-restful-url-mapping-algorithm/#comments</comments>
		<pubDate>Fri, 21 Nov 2008 10:26:27 +0000</pubDate>
		<dc:creator>dimuthu</dc:creator>
				<category><![CDATA[REST]]></category>
		<category><![CDATA[RESTful]]></category>
		<category><![CDATA[SOA]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[axis2/c]]></category>
		<category><![CDATA[web services]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[time complexity]]></category>

		<guid isPermaLink="false">http://www.dimuthu.org/?p=664</guid>
		<description><![CDATA[Few weeks back I wrote a blog post about Writing RESTful Services in C which explain the use of Axis2/C REST API. Basically when you provide a HTTP Method (GET, POST, PUT or DELETE) and a HTTP URL, it is matched with a given HTTP method and a URL pattern in order to identify the [...]]]></description>
			<content:encoded><![CDATA[<p>Few weeks back I wrote a blog post about <a href="http://www.dimuthu.org/blog/2008/10/18/write-restful-services-in-c/">Writing RESTful Services in C</a> which explain the use of <a href="http://ws.apache.org/axis2/c">Axis2/C</a> REST API. Basically when you provide a HTTP Method (GET, POST, PUT or DELETE) and a HTTP URL, it is matched with a given HTTP method and a URL pattern in order to identify the operation and extract out the request parameters. For the example mentioned in the above <a href="http://www.dimuthu.org/blog/2008/10/18/write-restful-services-in-c/">blog</a>, we can summarize the URL mapping like this.</p>
<table border="1">
<tbody>
<tr style="background:#dddddd">
<td>Operation</td>
<td>HTTP Method</td>
<td>URL Pattern</td>
<td>Example Requests</td>
</tr>
<tr>
<td>getSubjects</td>
<td>GET</td>
<td>subjects</td>
<td>GET subjects</td>
</tr>
<tr>
<td>getSubjectInfoPerName</td>
<td>GET</td>
<td>subjects/{name}</td>
<td>GET subjects/maths</td>
</tr>
<tr>
<td>getStudnets</td>
<td>GET</td>
<td>students</td>
<td>GET students</td>
</tr>
<tr>
<td>getStudnetsInfoPerName</td>
<td>GET</td>
<td>students/{name}</td>
<td>GET students/john</td>
</tr>
<tr>
<td>getMarksPerSubjectPerStudent</td>
<td>GET</td>
<td>students/{student}/marks/{subject}</td>
<td>GET students/john/marks/maths</td>
</tr>
</tbody>
</table>
<p>You can watch <a href="http://labs.wso2.org/wsf/php/resource_view.php?url=RESTfulSchool">an application with this URL mapping in live</a>, written using <a href="http://wso2.org/projects/wsf/php">WSF/PHP</a> which in fact run Axis2/C algorithms underneath.</p>
<p>Last week I updated this REST mapping algorithm and started a <a href="http://www.nabble.com/REST-URL-Mapping---The-changes-done-in-pattern-matching-Algorithm-to20594898.html#a20594898">discussion about the changes on Axis2/C Dev list</a>. I thought it would be better explain the idea on by blog too.</p>
<p>What the early algorithm (before my changes) did was, it search each pattern in the order it was declared, and returns when a match is found. Sequential searching for a matching pattern can reduce the performance as the number of operations grows. So my solutions was to keep the url pattern in a multi level (recursive) structure and match the url from one level to another.</p>
<p>Here is the structure of the &#8216;c struct&#8217;. (defined in src/core/util/core_utils.c)</p>
<pre class="c"><span style="font-style: italic; color: #808080;">/* internal structure to keep the rest map in a multi level hash */</span>
<span style="color: #993333;">typedef</span> <span style="color: #993333;">struct</span> <span style="color: #66cc66;">{</span>
    <span style="font-style: italic; color: #808080;">/* the structure will keep as many as following fields */</span>

    <span style="font-style: italic; color: #808080;">/* if the mapped value is directly the operation */</span>
    axis2_op_t *op_desc;

    <span style="font-style: italic; color: #808080;">/* if the mapped value is a constant, this keeps a hash map of
    possible constants =&gt; corrosponding map_internal structure */</span>
    axutil_hash_t *consts_map;

    <span style="font-style: italic; color: #808080;">/* if the mapped value is a param, this keeps a hash map of
    possible param_values =&gt; corrosponding_map_internal structre */</span>
    axutil_hash_t *params_map;

<span style="color: #66cc66;">}</span> axutil_core_utils_map_internal_t;</pre>
<p>Here is how it will looks like when the above URL pattern set (shown in the above table) is kept inside this multi-level (recursive) structure.</p>
<pre>svc-&gt;op_rest_map  (hash)
                |
            "GET:students" --------- axutil_core_utils_map_internal_t (instance)
                |                                            |
                |                                        op_desc (axis2_op_t* for "GET students" op)
                |                                            |
                |                                        consts_map (empty hash)
                |                                            |
                |                                        params_map (hash)
                |                                                         |
                |                                                      "{student_id}" ------------- axutil_core_utils_map_internal_t (instance)
                |                                                                                            |
                |                                                                                op_desc (axis2_op_t* for "GET students/{student_id}" op)
                |                                                                                            |
                |                                                                                parms_map (empty hash)
                |                                                                                            |
                |                                                                                 const_map (hash)
                |                                                                                            |
                |                                                                                        "marks" ------------------- axutil_core_utils_map_internal_t (instance)
                |                                                                                                                            |
                |                                                                                                                    op_desc (NULL)
                |                                                                                                                            |
                |                                                                                                                   consts_map (empty hash)
                |                                                                                                                            |
                |                                                                                                                   params_map (hash)
                |                                                                                                                            |
                |                                                                                                                      "{subject_id}" ----------- axutil_core_utils_map_internal_t (instance)
                |                                                                                                                                                                               |
                |                                                                                                                                       op_desc (axis2_op_t* for "GET students/{student_id}/marks/{subject_id}" op)
                |                                                                                                                                                                               |
                |                                                                                                                                                                 consts_map / params_map (Both NULL)
                |
            "GET:students" --------- axutil_core_utils_map_internal_t (instance)
                                                            |
                                                        op_desc (axis2_op_t* for "GET students" op)
                                                            |
                                                        consts_map (empty hash)
                                                            |
                                                        params_map (hash)
                                                            |
                                                      "{student_id}" ------------- axutil_core_utils_map_internal_t (instance)
                                                                                                          |
                                                                                  op_desc (axis2_op_t* for "GET students/{student_id}" op)
                                                                                                          |
                                                                                             consts_map / params_map (Both NULL)</pre>
<p>This structure is build at the time the server initialize the services. (from the &#8220;axis2_svc_get_rest_op_list_with_method_and_location&#8221; function in src/core/description/svc.c)</p>
<p>As each request hit the service, the request HTTP method and the URL is matched (which we call &#8216;rest dispatching&#8217;) with the above structure using the following algorithm. (defined in the &#8220;axis2_rest_disp_find_op&#8221; function in src/core/engine/rest_disp.c). Note that here we are extracting out the user REST parameters as well, but it is not shown in here.</p>
<ol>
<li>The request URL is spitted in to URL components from &#8216;/&#8217; character. Retrive the instance of axutil_core_utils_map_internal_t  from the svc-&gt;rest_map to the varaible &#8216;mapping_struct&#8217;.</li>
<li>Check the existance of URL components, count(URL components) &gt; 0.</li>
<li>If it doesn&#8217;t exist any URL components, get the value of mapping_struct-&gt;op_desc where the mapping_struct is the current mapping instance of axutil_core_utils_map_internal_t. if the mapping_struct-&gt;op_desc is not NULL, we found the operation. If it is NULL just exit returning NULL.</li>
<li>Else If some URL component(s) exist, check the most former URL component in the mapping_struct-&gt;const_map hash. If mapping_struct-&gt;const_map['former_url_component'] is not NULL, assign the mapping struct-&gt;const_map['former_url_component'] value to mapping_struct and follow the step 2 with the remaining URL components. (note that here hash['key'] syntax is used to take the value for the key from the hash &#8216;hash&#8217;. If that returns TRUE, we found the opeartion, if not countine to step5.</li>
<li>if mapping_struct-&gt;const_map['former_url_component'] is NULL, match the former url component with each key (which is a URL component pattern) in mapping_struct-&gt;param_map hash. (We use the function <span> &#8220;axis2_core_utils_match_url_component_with_pattern&#8221; in src/core/util/core_utils.c to</span> map URL component with the URL component pattern<span>). If matching pattern found assign the mapping_struct-&gt;param_map['key'] to mapping struct and </span>follow the step 2 with the remaining URL components. If that returns TRUE for some key it will be the matching operation.</li>
</ol>
<p>Where as the earlier algorithm can be simplified to,</p>
<ol>
<li>Match the request URL with URL patterns of each operation. This will be like calling the function &#8220;<span>axis2_core_utils_match_url_component_with_pattern&#8221; (mentioned in step5 of the above algorithm) for the complete URL rather than for a URL component<br />
</span></li>
<li>If the pattern is matched, matching operation is the selected operation for the request.</li>
</ol>
<p>I approximately calculated the time complexity of both of these algorithm.</p>
<p>Here is the time complexity of the later described algorithm.</p>
<table border="1">
<tbody>
<tr>
<td>Average time complexity of iterating &#8216;n&#8217; number of operations</td>
<td>n/2 = O(n)</td>
</tr>
<tr>
<td>Time complexity of matching pattern with a URL with the length &#8216;p&#8217; (complexity of the &#8216;axis2_core_utils_match_url_component_with_pattern&#8217; function)</td>
<td>O(p^2)</td>
</tr>
<tr style="background:#eeeeee">
<td>Complete time complexity of the algorithm</td>
<td>O(n*p^2)</td>
</tr>
</tbody>
</table>
<p>Time complexity of the formerly described algorithm. (which is currently in the SVN).</p>
<table border="1">
<tbody>
<tr>
<td>Time Complexity of a Hash Search</td>
<td>O(1)</td>
</tr>
<tr>
<td>Average Number of has searches required. This is the average number of levels in the tree of recursive structures drawn above</td>
<td>long(n)/2 = O(log(n))</td>
</tr>
<tr>
<td>Time complexity of matching pattern with a URL component with the average length &#8216;d&#8217;, d &lt; p (p = the length of the complete URL)</td>
<td>O(d^2)</td>
</tr>
<tr>
<td>Number of time pattern matching is required = number of param components in the URL = k, k &lt; p/d (p = the length of the url, d = average length of the URL component)/</td>
<td>k = O(k)</td>
</tr>
<tr style="background:#eeeeee">
<td>Complete time complexity of the algorithm</td>
<td>O(log(n)*d^2*k)</td>
</tr>
</tbody>
</table>
<p>Considering the facts, O(logn) &lt; O(n),d &lt; p and k &lt; p/d we can safely conclude</p>
<p>O(long(n)*d^2*k) &lt; O(n*p^2)  =&gt; The newer algorithm has better (low) time complexity.</p>
<p>However the time complexity is valid only it takes high values for the parameters. For low values  the actual time taken by the newer algorithm can have high values, considering the constant overhead of the recursions and the hash search. So in order to judge the performance of the algorithm, we have to run some test cases and measure the actual times. Possibly a task for the weekend <img src='http://www.dimuthu.org/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.dimuthu.org/blog/2008/11/21/apache-axis2c-restful-url-mapping-algorithm/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
