<?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>JDT&#039;s Blog</title>
	<atom:link href="http://jdt.toron.be/blog/feed/" rel="self" type="application/rss+xml" />
	<link>http://jdt.toron.be/blog</link>
	<description>JDT on computers, programming and pie</description>
	<lastBuildDate>Tue, 03 Nov 2009 17:52:03 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Creating custom Statements</title>
		<link>http://jdt.toron.be/blog/2009/11/creating-custom-statements/</link>
		<comments>http://jdt.toron.be/blog/2009/11/creating-custom-statements/#comments</comments>
		<pubDate>Tue, 03 Nov 2009 17:45:19 +0000</pubDate>
		<dc:creator>JDT</dc:creator>
				<category><![CDATA[Various]]></category>
		<category><![CDATA[Calidus]]></category>
		<category><![CDATA[Statements]]></category>

		<guid isPermaLink="false">http://jdt.toron.be/blog/?p=58</guid>
		<description><![CDATA[Abstract
In which the author outlines the method used in Calidus to parse a set of tokens into a new type of statement.
Concrete
Creating a new statement in Calidus is simple. A lot of utilities, methods and classes are available to make creating a statement a breeze.
Creating the basic classes
The first thing to do is to make [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Abstract</strong></p>
<p>In which the author outlines the method used in Calidus to parse a set of tokens into a new type of statement.</p>
<p><strong>Concrete</strong><br />
Creating a new statement in Calidus is simple. A lot of utilities, methods and classes are available to make creating a statement a breeze.</p>
<h2><strong>Creating the basic classes</strong></h2>
<p>The first thing to do is to make sure that all parts are in place. The statement class itself must be declared. This class is derived from the <em>AccessModifierStatement</em> because an indexer can have an access modifier, and the base <em>AccessModifierStatement</em> class provides a few methods to work with these access modifiers.</p>
<div class="codesnip-container" >
<div class="csharp codesnip" style="font-family:monospace;"><span class="co1">/// &lt;summary&gt;</span><br />
<span class="co1">/// This class represents an indexer statement</span><br />
<span class="co1">/// &lt;/summary&gt;</span><br />
<span class="kw1">public</span> <span class="kw4">class</span> IndexerStatement <span class="sy0">:</span> AccessModifierStatement<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="co1">/// &lt;summary&gt;</span><br />
&nbsp; &nbsp; <span class="co1">/// Create a new instance of this class</span><br />
&nbsp; &nbsp; <span class="co1">/// &lt;/summary&gt;</span><br />
&nbsp; &nbsp; <span class="co1">/// &lt;param name=&quot;tokens&quot;&gt;The list of tokens in the statement&lt;/param&gt;</span><br />
&nbsp; &nbsp; <span class="kw1">public</span> IndexerStatement<span class="br0">&#40;</span>IEnumerable<span class="sy0">&lt;</span>TokenBase<span class="sy0">&gt;</span> tokens<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sy0">:</span> <span class="kw1">base</span><span class="br0">&#40;</span>tokens<span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
</div>
<p>The basic code for the statement factory class is also needed. In order to make this parsing as easy as possible the factory is derived from the <em>FluentStatementFactory</em> class to gain access to statement expressions. These statement expressions provide a fluent interface to declare the requirements for a statement.</p>
<div class="codesnip-container" >
<div class="csharp codesnip" style="font-family:monospace;"><span class="co1">/// &lt;summary&gt;</span><br />
<span class="co1">/// This class creates indexer statements</span><br />
<span class="co1">/// &lt;/summary&gt;</span><br />
<span class="kw1">public</span> <span class="kw4">class</span> IndexerStatementFactory <span class="sy0">:</span> FluentStatementFactory<span class="sy0">&lt;</span>IndexerStatement<span class="sy0">&gt;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">protected</span> <span class="kw1">override</span> IndexerStatement BuildStatement<span class="br0">&#40;</span>IEnumerable<span class="sy0">&lt;</span>TokenBase<span class="sy0">&gt;</span> input<span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span class="kw3">new</span></a> IndexerStatement<span class="br0">&#40;</span>input<span class="br0">&#41;</span><span class="sy0">;</span><br />
&nbsp; &nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp; <span class="kw1">protected</span> <span class="kw1">override</span> <span class="kw4">bool</span> IsValidContext<span class="br0">&#40;</span>IStatementContext context<span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> false<span class="sy0">;</span><br />
&nbsp; &nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp; <span class="kw1">protected</span> <span class="kw1">override</span> IStatementExpression Expression<br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; get <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; StatementExpression expression <span class="sy0">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span class="kw3">new</span></a> StatementExpression<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> expression<span class="sy0">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
</div>
<p>Since Calidus is test-driven, a set of tests are needed to indicate that the statement is parsed correctly. For a factory, define a base test class that derives from <em>CalidusTestBase. </em>This class provides a set of utility methods and classes such as a TokenCreator and a StatementCreator that can be used by Calidus tests.</p>
<p>The TDD aficionado&#8217;s will notice that this is technically not a valid test: an additional unit test should be created to validate the context checking separately. However, this test already checks that the context is called through the mock repository in the VerifyAll-method. An extra test provides no additional advantage here.</p>
<div class="codesnip-container" >
<div class="csharp codesnip" style="font-family:monospace;"><span class="br0">&#91;</span>TestFixture<span class="br0">&#93;</span><br />
<span class="kw1">public</span> <span class="kw4">class</span> IndexerStatementFactoryTest <span class="sy0">:</span> CalidusTestBase<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="br0">&#91;</span>SetUp<span class="br0">&#93;</span><br />
&nbsp; &nbsp; <span class="kw1">public</span> <span class="kw1">override</span> <span class="kw1">void</span> SetUp<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">base</span>.<span class="me1">SetUp</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span><br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
</div>
<p>Next, define a series of tests to indicate what is needed, and in this case a single test will suffice. Some additional information is needed to make this test work, so the basic frame of the test class must be expanded to suit the requirements for mocking.</p>
<div class="codesnip-container" >
<div class="csharp codesnip" style="font-family:monospace;"><span class="br0">&#91;</span>TestFixture<span class="br0">&#93;</span><br />
<span class="kw1">public</span> <span class="kw4">class</span> IndexerStatementFactoryTest <span class="sy0">:</span> CalidusTestBase<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">private</span> IndexerStatementFactory _factory<span class="sy0">;</span><br />
&nbsp; &nbsp; <span class="kw1">private</span> IStatementContext _context<span class="sy0">;</span><br />
&nbsp; &nbsp; <span class="kw1">private</span> MockRepository _mocker<span class="sy0">;</span></p>
<p>&nbsp; &nbsp; <span class="br0">&#91;</span>SetUp<span class="br0">&#93;</span><br />
&nbsp; &nbsp; <span class="kw1">public</span> <span class="kw1">override</span> <span class="kw1">void</span> SetUp<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">base</span>.<span class="me1">SetUp</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; _factory <span class="sy0">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span class="kw3">new</span></a> IndexerStatementFactory<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; _mocker <span class="sy0">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span class="kw3">new</span></a> MockRepository<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; _context <span class="sy0">=</span> _mocker.<span class="me1">DynamicMock</span><span class="sy0">&lt;</span>IStatementContext<span class="sy0">&gt;</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span><br />
&nbsp; &nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp; <span class="br0">&#91;</span>Test<span class="br0">&#93;</span><br />
&nbsp; &nbsp; <span class="kw1">public</span> <span class="kw1">void</span> FactoryShouldCreateStatementFromThisFollowedBySquareBracketInClass<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Expect.<span class="me1">Call</span><span class="br0">&#40;</span>_context.<span class="me1">Parents</span><span class="br0">&#41;</span>.<span class="kw1">Return</span><span class="br0">&#40;</span><a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span class="kw3">new</span></a><span class="br0">&#91;</span><span class="br0">&#93;</span> <span class="br0">&#123;</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span class="kw3">new</span></a> StatementParent<span class="br0">&#40;</span>StatementCreator.<span class="me1">CreateClassStatement</span><span class="br0">&#40;</span><span class="br0">&#41;</span>, StatementCreator.<span class="me1">CreateOpenBlockStatement</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#125;</span><span class="br0">&#41;</span>.<span class="me1">Repeat</span>.<span class="me1">Once</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; IList<span class="sy0">&lt;</span>TokenBase<span class="sy0">&gt;</span> input <span class="sy0">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span class="kw3">new</span></a> List<span class="sy0">&lt;</span>TokenBase<span class="sy0">&gt;</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; input.<span class="me1">Add</span><span class="br0">&#40;</span>TokenCreator.<span class="me1">Create</span><span class="sy0">&lt;</span>PublicModifierToken<span class="sy0">&gt;</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; input.<span class="me1">Add</span><span class="br0">&#40;</span>TokenCreator.<span class="me1">Create</span><span class="sy0">&lt;</span>SpaceToken<span class="sy0">&gt;</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; input.<span class="me1">Add</span><span class="br0">&#40;</span>TokenCreator.<span class="me1">Create</span><span class="sy0">&lt;</span>IdentifierToken<span class="sy0">&gt;</span><span class="br0">&#40;</span><span class="st0">&quot;String&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; input.<span class="me1">Add</span><span class="br0">&#40;</span>TokenCreator.<span class="me1">Create</span><span class="sy0">&lt;</span>SpaceToken<span class="sy0">&gt;</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; input.<span class="me1">Add</span><span class="br0">&#40;</span>TokenCreator.<span class="me1">Create</span><span class="sy0">&lt;</span>ThisToken<span class="sy0">&gt;</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; input.<span class="me1">Add</span><span class="br0">&#40;</span>TokenCreator.<span class="me1">Create</span><span class="sy0">&lt;</span>OpenSquareBracketToken<span class="sy0">&gt;</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; input.<span class="me1">Add</span><span class="br0">&#40;</span>TokenCreator.<span class="me1">Create</span><span class="sy0">&lt;</span>IntToken<span class="sy0">&gt;</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; input.<span class="me1">Add</span><span class="br0">&#40;</span>TokenCreator.<span class="me1">Create</span><span class="sy0">&lt;</span>SpaceToken<span class="sy0">&gt;</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; input.<span class="me1">Add</span><span class="br0">&#40;</span>TokenCreator.<span class="me1">Create</span><span class="sy0">&lt;</span>IdentifierToken<span class="sy0">&gt;</span><span class="br0">&#40;</span><span class="st0">&quot;index&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; input.<span class="me1">Add</span><span class="br0">&#40;</span>TokenCreator.<span class="me1">Create</span><span class="sy0">&lt;</span>CloseSquareBracketToken<span class="sy0">&gt;</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; _mocker.<span class="me1">ReplayAll</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Assert.<span class="me1">IsTrue</span><span class="br0">&#40;</span>_factory.<span class="me1">CanCreateStatementFrom</span><span class="br0">&#40;</span>input, _context<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; _mocker.<span class="me1">VerifyAll</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span><br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
</div>
<p>This test code defines a mock statement context that indicates that the list of tokens to be parsed by the factory was found inside a class. The token list to parse is also created here.</p>
<h2><strong>Making it work</strong></h2>
<p>The following code is fairly simple as there are only two requirements for an indexer statement. First of all, it is part of a class: it can only be defined where the parent of the statement is the scope of a class. Second, an indexer statement consists of the This-keyword followed by a square bracket open.  Both requirements can be expressed in code.</p>
<div class="codesnip-container" >
<div class="csharp codesnip" style="font-family:monospace;"><span class="kw1">protected</span> <span class="kw1">override</span> <span class="kw4">bool</span> IsValidContext<span class="br0">&#40;</span>IStatementContext context<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">return</span> context.<span class="me1">Parents</span>.<span class="me1">FirstParentIsOfType</span><span class="sy0">&lt;</span>ClassStatement<span class="sy0">&gt;</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span><br />
<span class="br0">&#125;</span></p>
<p><span class="kw1">protected</span> <span class="kw1">override</span> IStatementExpression Expression<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; get <br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; StatementExpression expression <span class="sy0">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span class="kw3">new</span></a> StatementExpression<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; expression.<span class="me1">Contains</span><span class="sy0">&lt;</span>ThisToken<span class="sy0">&gt;</span><span class="br0">&#40;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.<span class="me1">FollowedBy</span><span class="sy0">&lt;</span>OpenSquareBracketToken<span class="sy0">&gt;</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> expression<span class="sy0">;</span><br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
</div>
<p>Combining these two pieces of codes validates both the context and the tokens, and running the unit tests with this code now yields a nice green bar.</p>
]]></content:encoded>
			<wfw:commentRss>http://jdt.toron.be/blog/2009/11/creating-custom-statements/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Introducing&#8230;</title>
		<link>http://jdt.toron.be/blog/2009/10/introducing/</link>
		<comments>http://jdt.toron.be/blog/2009/10/introducing/#comments</comments>
		<pubDate>Mon, 26 Oct 2009 01:02:20 +0000</pubDate>
		<dc:creator>JDT</dc:creator>
				<category><![CDATA[Various]]></category>
		<category><![CDATA[Calidus]]></category>

		<guid isPermaLink="false">http://jdt.toron.be/blog/?p=51</guid>
		<description><![CDATA[The Calidus Logo!

]]></description>
			<content:encoded><![CDATA[<p>The Calidus Logo!</p>
<p><img class="size-full wp-image-52 alignleft" title="Calidus Logo" src="http://jdt.toron.be/blog/wp-content/uploads/2009/10/calidus_logo.jpg" alt="The Calidus Logo" width="200" height="200" /></p>
]]></content:encoded>
			<wfw:commentRss>http://jdt.toron.be/blog/2009/10/introducing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Calidus Model</title>
		<link>http://jdt.toron.be/blog/2009/10/the-calidus-model/</link>
		<comments>http://jdt.toron.be/blog/2009/10/the-calidus-model/#comments</comments>
		<pubDate>Mon, 19 Oct 2009 21:24:29 +0000</pubDate>
		<dc:creator>JDT</dc:creator>
				<category><![CDATA[Various]]></category>
		<category><![CDATA[Blocks]]></category>
		<category><![CDATA[Calidus]]></category>
		<category><![CDATA[Lines]]></category>
		<category><![CDATA[Rules]]></category>
		<category><![CDATA[Statements]]></category>
		<category><![CDATA[Tokens]]></category>

		<guid isPermaLink="false">http://jdt.toron.be/blog/?p=42</guid>
		<description><![CDATA[Abstract
In which the author outlines the basics of Calidus, the most important concepts and classes and how to use them.
Concrete
Calidus is not a terribly complex application. There are no zillion classes to get to know, there are no hard words or hidden meanings and most things do exactly what they sound like. In order to [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Abstract</strong></p>
<p>In which the author outlines the basics of Calidus, the most important concepts and classes and how to use them.</p>
<p><strong>Concrete</strong></p>
<p>Calidus is not a terribly complex application. There are no zillion classes to get to know, there are no hard words or hidden meanings and most things do exactly what they sound like. In order to take full advantage of Calidus and develop custom parts such as rules it is important to know some of the inner workings of Calidus. This part is intended for advanced users and is a must-read for developers.</p>
<h2>Characters</h2>
<p>Technically, the smallest part of Calidus is the character, provided by the builtin character class. The concept of a character is not explicitly present in Calidus because no rule validation is performed on characters, but since they are the base element on which a token is built they deserve an honorable mention.</p>
<h2>Tokens</h2>
<p>A token is the smallest usefull part of the Calidus model. A token can be described as a collection containing at least one character, grouped together in the builtin String class. A token is either:</p>
<ul>
<li>A single-character token, such as a <em>SpaceToken </em>or a <em>TabToken</em></li>
<li>A single string token, such as an <em>IdentifierToken </em>or a <em>NameSpaceToken</em></li>
<li>A multiple string token, such as a <em>LineCommentToken</em></li>
</ul>
<p>Calidus uses <a href="http://www.ssw.uni-linz.ac.at/coco/" target="_blank">Coco/R</a> to parse source code into tokens. This means that the list of tokens recognized by Calidus is almost exactly the same as the list of tokens recognized by Coco/R. There are some exceptions to this rule: whitespace is not normally part of the Coco/R parser&#8217;s returned token types and is therefore provided by an additional parser.</p>
<p>Tokens that cannot be parsed as a more specific token are <em>GenericTokens</em>.</p>
<h2>Statements</h2>
<p>A statement is a collection containing at least one token. What exactly constitutes a statement is somewhat determined by the C# language: in most cases the end of a statement is a semicolon. For <em>if</em>, <em>for</em>, <em>while</em> constructs and the likes the end of a statement is the closing round bracket &#8216;)&#8217;, and for attributes the end of a statement is the closing square bracket &#8216;]&#8217;.</p>
<p>The statement parser groups tokens together by splitting them at certain well-defined places, the most common example of this is when a semicolon was encountered. There are other ways to group statements, which include using brackets, newlines and other special token types.<span style="color: #ff0000;"></span></p>
<p>Parsing token groups to see if they match a statement definition is a complex operation, and the definition of what constitutes a statement can differ greatly.  One thing that creates a lot of complexity is the amount of whitespace separating tokens, which may or may not be relevant depending on the context. To make token parsing easy Calidus provides a fluent interface that allows a declarative statement definition.</p>
<p>Statements that cannot be parsed as a more specific statement are <em>GenericStatements</em>.</p>
<h2>Lines</h2>
<p>Lines are probably the easiest elements in the Calidus model. A line is just a line: it is a collection of tokens that reside on the same line. Lines are the only elements that can have rules associated with them but do not have any native support for pluggable definitions and extensions: what constitutes a line can never change and is fixed by Calidus.</p>
<h2>Blocks</h2>
<p>Blocks are logical groupings of at least one statement. Blocks have very wide definitions: a series of using statements in a file are grouped into a block because they are related, but so are all elements in the source file since they compose the <em>FileBlock</em>.  The entire contents of a class constitutes a <em>ClassBlock</em>, and blocks are also the place where an <em>if</em>, <em>while</em>, <em>for</em>, <em>do while, foreach&#8230;</em> statement is present along with the code that is run as part of it.</p>
<h2>Rules</h2>
<p>Calidus is a source style validation tool, and the constraints placed on the source are coded into rules. Rules can be put on top of the following Calidus elements:</p>
<ul>
<li>Statements, based off of <em>StatementRuleBase</em></li>
<li>Lines, based off of <em>LineRuleBase</em></li>
<li>Blocks, based off of <em>BlockRuleBase</em></li>
</ul>
<p>Tokens do not have rules associated with them as a token itself cannot be invalid. For example, a line comment is parsed into a <em>LineCommentToken</em>, but validating this token must be done through the statement that contains this token which is the <em>LineCommentStatement</em>.</p>
<p>Calidus rules are automatically configured by the class reponsible for creating them, allowing a user to use a pre-built rule but apply some customization to it. A good example of this principle is the <em>MemberNameMatchesPatternRule</em>, where a class member&#8217;s name is validated against a regex pattern. The pattern itself has a default setting, which can be adjusted to meet the developers needs.  This is unlike Microsoft&#8217;s StyleCop tool, which has several rules used to validate member names, each of which must be enabled or disabled and might not even meet the requirements when a naming pattern is used that is not built into the application. Rule configuration can be done through the main Calidus GUI.</p>
]]></content:encoded>
			<wfw:commentRss>http://jdt.toron.be/blog/2009/10/the-calidus-model/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>I did it</title>
		<link>http://jdt.toron.be/blog/2009/10/i-did-it/</link>
		<comments>http://jdt.toron.be/blog/2009/10/i-did-it/#comments</comments>
		<pubDate>Mon, 19 Oct 2009 21:17:37 +0000</pubDate>
		<dc:creator>JDT</dc:creator>
				<category><![CDATA[Various]]></category>
		<category><![CDATA[Calidus]]></category>

		<guid isPermaLink="false">http://jdt.toron.be/blog/?p=47</guid>
		<description><![CDATA[Calidus is now published at http://code.google.com/p/calidus/
]]></description>
			<content:encoded><![CDATA[<p>Calidus is now published at <a href="http://code.google.com/p/calidus/" target="_blank">http://code.google.com/p/calidus/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://jdt.toron.be/blog/2009/10/i-did-it/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Release early, dammit!</title>
		<link>http://jdt.toron.be/blog/2009/10/release-early-dammit/</link>
		<comments>http://jdt.toron.be/blog/2009/10/release-early-dammit/#comments</comments>
		<pubDate>Tue, 13 Oct 2009 17:05:08 +0000</pubDate>
		<dc:creator>JDT</dc:creator>
				<category><![CDATA[Various]]></category>

		<guid isPermaLink="false">http://jdt.toron.be/blog/?p=39</guid>
		<description><![CDATA[7. Release early.  Release often.  And listen to your customers.
- Eric Raymond
For some time now I have been working on a project. It originally started out as a little tool I wanted to use for myself, but then decided that it might be worth to publish the project. Ever since the first parts [...]]]></description>
			<content:encoded><![CDATA[<blockquote><p>7. Release early.  Release often.  And listen to your customers.</p>
<p>- <a href="http://catb.org/esr/writings/cathedral-bazaar/cathedral-bazaar/ar01s04.html" target="_blank">Eric Raymond</a></p></blockquote>
<p>For some time now I have been working on a project. It originally started out as a little tool I wanted to use for myself, but then decided that it might be worth to publish the project. Ever since the first parts were ready I&#8217;ve had several moments where I thought &#8216;Hey, just a little more and I can publish&#8217;. But I never did.</p>
<p>The reason for this is simple: every time you round up a feature there is always one more to add, one more thing to refactor, one more part to clean up. If you are ever going to start a project with the intention of making it public &#8211; open source or not &#8211; you might as well publish it now, because you&#8217;ll always be <em>just</em> one step away.</p>
<p>I am therefore making a promise: I will take two weeks to wrap up the features I am now working on (there are currently two left). I will <strong>publish the project by october 31st. </strong></p>
<p>If I don&#8217;t, I&#8217;m buying you all a beer.</p>
]]></content:encoded>
			<wfw:commentRss>http://jdt.toron.be/blog/2009/10/release-early-dammit/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ubiquitous language: what is so blindingly obvious you can&#8217;t see it?</title>
		<link>http://jdt.toron.be/blog/2009/08/ubiquitous-language-what-is-so-blindingly-obvious-you-cant-see-it/</link>
		<comments>http://jdt.toron.be/blog/2009/08/ubiquitous-language-what-is-so-blindingly-obvious-you-cant-see-it/#comments</comments>
		<pubDate>Fri, 07 Aug 2009 20:59:26 +0000</pubDate>
		<dc:creator>JDT</dc:creator>
				<category><![CDATA[Various]]></category>
		<category><![CDATA[DDD]]></category>

		<guid isPermaLink="false">http://jdt.toron.be/blog/?p=19</guid>
		<description><![CDATA[Ubiquitous Language is the term Eric Evans uses in Domain Driven 	Design for the practice of building up a common, rigorous language 	between developers and users. This language should be based on the 	Domain Model used in the software &#8211; hence the need for it to be 	rigorous, since software doesn&#8217;t cope well with ambiguity.
&#8211; [...]]]></description>
			<content:encoded><![CDATA[<blockquote><p><em>Ubiquitous Language is the term Eric Evans uses in <a href="http://www.amazon.com/gp/product/0321125215">Domain Driven 	Design</a> for the practice of building up a common, rigorous language 	between developers and users. This language should be based on the 	<a href="http://martinfowler.com/eaaCatalog/domainModel.html">Domain Model</a> used in the software &#8211; hence the need for it to be 	rigorous, since software doesn&#8217;t cope well with ambiguity.<br />
&#8211; <a href="http://www.martinfowler.com/bliki/UbiquitousLanguage.html" target="_blank">Martin Fowler</a></em></p></blockquote>
<p><strong>Abstract</strong></p>
<p>In which the author outlines the basics of the &#8216;Ledger Pattern&#8217;, an addition to the ubiquitous language and domain model to help facilitate a more natural approach to DDD and OO. It is specifically designed to deal with issues of validation and object creation that occur during object instantiation.</p>
<p><strong>Concrete</strong></p>
<p>Some things are obvious.  Some things are very obvious. And some things are so obvious that you actually need to take a step back in order to see them.  <a href="http://www.udidahan.com" target="_blank">Udi Dahan</a> helped me take that step back some time ago with his article &#8216;<a href="http://www.udidahan.com/2009/06/29/dont-create-aggregate-roots/" target="_blank">Don&#8217;t create aggregate roots</a>&#8216;.  While I agree with him on most of what he details in his post, I feel that something is missing.</p>
<p>What Udi points out is that we should never create instances of domain objects from the service layer (or command handler layer, whatever the layer is that coördinates calls to domain objects). Instead, it is the responsibility of an aggregate root to create a new entity that belongs to his aggregate.  In some cases, this is obvious:</p>
<pre>
<div class="codesnip-container" >
<div class="csharp codesnip" style="font-family:monospace;"><span class="kw1">public</span> <span class="kw4">class</span> Order
<span class="br0">&#123;</span>
&nbsp; <span class="kw1">public</span> <span class="kw1">void</span> AddOrderLine<span class="br0">&#40;</span>OrderLine line<span class="br0">&#41;</span>
&nbsp; <span class="br0">&#123;</span>
&nbsp; &nbsp; orderLines.<span class="me1">Add</span><span class="br0">&#40;</span>line<span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; <span class="br0">&#125;</span>

&nbsp; <span class="kw1">public</span> <span class="kw1">void</span> AddOrderLine<span class="br0">&#40;</span>Product product, <span class="kw4">int</span> quantity<span class="br0">&#41;</span>
&nbsp; <span class="br0">&#123;</span>
&nbsp; &nbsp; orderLines.<span class="me1">Add</span><span class="br0">&#40;</span><a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span class="kw3">new</span></a> OrderLine<span class="br0">&#40;</span>product, quantity<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; <span class="br0">&#125;</span>
<span class="br0">&#125;</span></div>
</div>
</pre>
<p>The latter method is actually the better: it is very easy to perform validation on the parameters passed before the orderline object is constructed.  This is closely linked to something I blogged on before: domain object validation is done on a context basis.</p>
<p>And even if you believe that validation can be done, consider the example where we create a customer</p>
<pre>
<div class="codesnip-container" >
<div class="csharp codesnip" style="font-family:monospace;"><span class="kw1">public</span> <span class="kw1">void</span> Handle<span class="br0">&#40;</span>CreateCustomerCommand cmd<span class="br0">&#41;</span>
<span class="br0">&#123;</span>
&nbsp; ICustomerRepository repo <span class="sy0">=</span> RepositoryFactory.<span class="me1">Get</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; Customer customer <span class="sy0">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span class="kw3">new</span></a> Customer<span class="br0">&#40;</span>cmd.<span class="me1">FirstName</span>, cmd.<span class="me1">LastName</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; repo.<span class="me1">Save</span><span class="br0">&#40;</span>customer<span class="br0">&#41;</span><span class="sy0">;</span>
<span class="br0">&#125;</span></div>
</div>
</pre>
<p>Where is the validation code now? Is it in the constructor? If so, what if we need different validation logic? What if our application goes from being an application used in a store to a web application? Now a customer comes through your website and you need to ensure that an email-address is supplied. We have to face facts: this solution is not adequate.</p>
<p>What we need is a domain concept that fits with the &#8216;this object can add customers&#8217;. So we use our common sense: in the store, the Employee-object can add a user. But who adds employees? Well, that would be the store owner. But where does he come from?</p>
<p>Back up a minute. In the past few sentences I&#8217;ve introduced a whole slew of new terms into the ubiquitous language. All of these need to be modeled, coded, tested and used. And what have we really achieved? Nothing much. No matter how far up the chain you push things, at some point you will run into a simple truth: you need an object that is not created and stored in the normal run of the system but that has been present from the start. Therefore I propose the following guideline:</p>
<blockquote><p><em>For every aggregate root that is not the entity of any other aggregate root that can act as its creator, define an artificial aggregate root.<br />
</em></p></blockquote>
<p>I call this artificial root a ledger, because it performs a function similar to an  order or purchase <a href="http://en.wikipedia.org/wiki/Ledger" target="_blank">ledger</a>.  Our customers are prime candidates for this: unless the ubiquitous language explicitly calls for some other object to create customers our CustomerLedger takes care of creating and deleting customers.  Note that all these ledgers to is provide a place to create and delete instances, updates are still processed by the actual domain objects.</p>
<p>Ledgers are also a prime place for validation: if a customer must have a first and last name, the ledger validates this. If our customers are now registering through the website the ledger can be extended to handle the adding of customers with the associated required validation of email addresses.</p>
<p>The odd thing about legders is that they are aggregate roots (and therefore entities) that have no identity*. All instances of a ledger class are equal to each other (disregarding lazy-loading and the likes) because they always contain the same data.</p>
<p>Here is a code example detailing what a ledger looks like.  This uses my implementation of the <a href="http://martinfowler.com/eaaDev/Notification.html" target="_blank">Notification Pattern</a> to let the repository check if the object is in a valid state and to allow it to report this to the presentation layer.</p>
<pre>
<div class="codesnip-container" >
<div class="csharp codesnip" style="font-family:monospace;"><span class="kw1">public</span> <span class="kw4">class</span> CustomerLedger
<span class="br0">&#123;</span>
&nbsp; <span class="kw1">private</span> IList errorList<span class="sy0">;</span>

&nbsp; <span class="kw1">public</span> CustomerLedger<span class="br0">&#40;</span><span class="br0">&#41;</span>
&nbsp; <span class="br0">&#123;</span>
&nbsp; &nbsp; errorList <span class="sy0">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span class="kw3">new</span></a> List<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; <span class="br0">&#125;</span>

&nbsp; <span class="kw1">public</span> IList Customers <span class="br0">&#123;</span> get<span class="sy0">;</span> set<span class="sy0">;</span> <span class="br0">&#125;</span>

&nbsp; <span class="kw1">public</span> ICommandResult AddCustomer<span class="br0">&#40;</span><span class="kw4">String</span> firstName, <span class="kw4">String</span> lastName<span class="br0">&#41;</span>
&nbsp; <span class="br0">&#123;</span>
&nbsp; &nbsp; Validate.<span class="me1">IsNotNullOrEmpty</span><span class="br0">&#40;</span>firstName, <span class="st0">&quot;First name null or empty&quot;</span>, errorList<span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; Validate.<span class="me1">IsNotNullOrEmpty</span><span class="br0">&#40;</span>lastName, <span class="st0">&quot;Last name null or empty&quot;</span>, errorList<span class="br0">&#41;</span><span class="sy0">;</span>

&nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>errorList.<span class="me1">Count</span> <span class="sy0">==</span> 0<span class="br0">&#41;</span>
&nbsp; &nbsp; &nbsp; Customers.<span class="me1">Add</span><span class="br0">&#40;</span><a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span class="kw3">new</span></a> Customer<span class="br0">&#40;</span>firstName, lastName<span class="br0">&#41;</span><span class="sy0">;</span>

&nbsp; &nbsp; <span class="kw1">return</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span class="kw3">new</span></a> CommandResult<span class="br0">&#40;</span>errorList<span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; <span class="br0">&#125;</span>
<span class="br0">&#125;</span></div>
</div>
</pre>
<p>This code is seemingly not very different from regular aggregate root code, and rightly so!  The only actual difference is the way the object is treated from the repository.</p>
<pre>
<div class="codesnip-container" >
<div class="csharp codesnip" style="font-family:monospace;"><span class="kw1">public</span> <span class="kw4">interface</span> ICustomerLedgerRepository
<span class="br0">&#123;</span>
&nbsp; CustomerLedger Get<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; ICommandResult Save<span class="br0">&#40;</span>CustomerLedger ledger<span class="br0">&#41;</span><span class="sy0">;</span>
<span class="br0">&#125;</span></div>
</div>
</pre>
<p>No id&#8217;s, no fancy lookups: there is no difference between the instances*.</p>
<p><strong>Conclusion</strong></p>
<p>The ledger concept is quite confusing at first, and it is a certain risk to implement: it can be confusing to users, domain experts and programmers alike. To introduce the concept into the ubiquitous language is to soil this language with something that is inherently a technical concern, but at the same time a valid domain problem. Everything that enters your system has an origin, and you need to model this origin up to a certain point.If you talk in terms of roles rather than objects it is a lot easier to introduce: the concept of a &#8216;CustomerAdder&#8217; makes sense: your system needs something that adds customers.</p>
<p>Technically, this can not be considered a design pattern. But I&#8217;m going to call it the &#8216;<strong>Ledger Pattern</strong>&#8216; anyway. It&#8217;s either that or calling it &#8216;the concept formerly known as the artifical aggregate root for aggregate roots that lack natural aggregate roots in the ubiquitous language&#8217;. Let&#8217;s see you type that ten times in a heated mailing list discussion.</p>
<p><em>* All my ledgers actually do have an identity to facilitate ORM mapping and retrieval. This means that every aggregate root that is managed by a ledger also has a foreign key that points to this ledger. My database contains a ledger-table which contains an id and the name of the legder. </em></p>
]]></content:encoded>
			<wfw:commentRss>http://jdt.toron.be/blog/2009/08/ubiquitous-language-what-is-so-blindingly-obvious-you-cant-see-it/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Bring on the default constructors</title>
		<link>http://jdt.toron.be/blog/2009/08/bring-on-the-default-constructors/</link>
		<comments>http://jdt.toron.be/blog/2009/08/bring-on-the-default-constructors/#comments</comments>
		<pubDate>Fri, 07 Aug 2009 17:05:51 +0000</pubDate>
		<dc:creator>JDT</dc:creator>
				<category><![CDATA[Various]]></category>
		<category><![CDATA[DDD]]></category>

		<guid isPermaLink="false">http://jdt.toron.be/blog/?p=16</guid>
		<description><![CDATA[The term “default constructor” refers to a constructor that is automatically generated in the absence of explicit constructors (and perhaps under other circumstances); this automatically provide constructor is usually a nullary constructor. In specification or discussion of some languages, “default constructor” may additionally refer to any constructor that may be called without arguments, either because [...]]]></description>
			<content:encoded><![CDATA[<blockquote><p><em>The term “<strong>default constructor</strong>” refers to a <a title="Constructor (computer science)" href="http://en.wikipedia.org/wiki/Constructor_%28computer_science%29">constructor</a> that is automatically generated in the absence of explicit constructors (and perhaps under other circumstances); this automatically provide constructor is usually a <a title="Nullary constructor" href="http://en.wikipedia.org/wiki/Nullary_constructor">nullary constructor</a>. In specification or discussion of some languages, “<strong>default constructor</strong>” may additionally refer to any constructor that may be called without <a title="Argument (computer science)" href="http://en.wikipedia.org/wiki/Argument_%28computer_science%29">arguments</a>, either because it is a nullary constructor or because all of its <a title="Parameter (computer science)" href="http://en.wikipedia.org/wiki/Parameter_%28computer_science%29">parameters</a> have default values.<br />
&#8211; <a href="http://en.wikipedia.org/wiki/Default_constructor" target="_blank">Wikipedia</a><br />
</em></p></blockquote>
<p><strong>Abstract</strong></p>
<p>In which the author outlines why default constructors on domain objects are not a &#8216;code smell&#8217; for NHibernate and are actually required in most situations.</p>
<p><strong>Concrete</strong></p>
<p>I remember when I first started working with (N)Hibernate. It seemed like  a great tool: no more writing SQL queries, no more writing mapping code, but there was that <em>expletive</em> requirement to have a default no-arguments constructor. So I made that private as to not allow the constructor to be abused, but it still seemed bad: the domain object needs to conform to what an infrastructure framework wants.  It is only now, years after my first encounter with NHibernate, that I see that having a default constructor is not a bad thing. If anything, default constructors on domain objects are <em>good things</em>.</p>
<p>Let&#8217;s look at the reason why we need constructors. The <em>raison d&#8217;être</em> for a constructor is to force code that news up an instance of a class to provide other objects that said class needs.  In the context of domain layer and domain objects we tend to think of this as &#8216;creating the object in a state that is valid&#8217;. But what is a valid state?  When talking with programmers, analysts or domain experts it&#8217;s very easy to define what is a valid state.  In a classical order system with customers valid state will probably include a customer having a first name, last name and valid address details.  But is this really such a big requirement that we have to validate this in the constructor?</p>
<p>Weather or not a domain object is valid is in some cases not up to the object itself, and this is especially true for new instances.  Fact of the matter is that the only thing that determines if a domain object is valid is <em>context</em>.  Why does a customer need a first name, last name and an address? Because we need to be able to ship orders to him.</p>
<p>Read that again. <em>We need to ship orders to him</em>. If we are not shipping orders we do not need to validate anything. This brings the concept of validation into a totally different area. We no longer need to think about validating domain objects after creation, we are now validating pre-conditions for object creation.  This validation is always external to the object being created, allowing you to use the same domain object for different use-cases, without the need to write, test and maintain a gazillion different constructors, or even worse &#8211; deal with constructors that have the same parameters but different validation requirements. (The question of where the actual validation occurs for certain domain objects &#8211; such as aggregate roots &#8211; is a matter I&#8217;l talk about in a different post.)</p>
<p><strong>Conclusion</strong></p>
<p>And that, ladies and gentlemen, is why I like default constructors. Because I really don&#8217;t care if my domain objects are valid or not. All I care about is that they are valid in the <em>context </em>I&#8217;m using them in. And validating that can be done with re-useable code, tried and tested through unittests and talks with domain experts. Full of DDD and OO goodness, just the way I like it.</p>
]]></content:encoded>
			<wfw:commentRss>http://jdt.toron.be/blog/2009/08/bring-on-the-default-constructors/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>On the Art of blogging</title>
		<link>http://jdt.toron.be/blog/2009/07/on-the-art-of-blogging/</link>
		<comments>http://jdt.toron.be/blog/2009/07/on-the-art-of-blogging/#comments</comments>
		<pubDate>Sat, 18 Jul 2009 14:42:36 +0000</pubDate>
		<dc:creator>JDT</dc:creator>
				<category><![CDATA[Various]]></category>

		<guid isPermaLink="false">http://jdt.toron.be/blog/?p=3</guid>
		<description><![CDATA[Blogging is tough. While it is an unmistakable part of what marketing types like to call &#8216;Web 2.0&#8242;, there are very few blogs that actually offer decent information. Which is why the title of this first post has a capital &#8216;A&#8217;. And which is why I don&#8217;t pretend to be any good at it. 
]]></description>
			<content:encoded><![CDATA[<p>Blogging is tough. While it is an unmistakable part of what marketing types like to call &#8216;Web 2.0&#8242;, there are very few blogs that actually offer decent information. Which is why the title of this first post has a capital &#8216;A&#8217;. And which is why I don&#8217;t pretend to be any good at it. </p>
]]></content:encoded>
			<wfw:commentRss>http://jdt.toron.be/blog/2009/07/on-the-art-of-blogging/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
