<?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>Eugene Hutorny</title>
	<atom:link href="http://hutorny.in.ua/feed" rel="self" type="application/rss+xml" />
	<link>http://hutorny.in.ua</link>
	<description>Programming in a small</description>
	<lastBuildDate>Tue, 16 Mar 2010 07:25:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.5</generator>
		<item>
		<title>Model-Document-Renderer Design Pattern</title>
		<link>http://hutorny.in.ua/java/model-document-renderer-design-pattern</link>
		<comments>http://hutorny.in.ua/java/model-document-renderer-design-pattern#comments</comments>
		<pubDate>Tue, 16 Mar 2010 07:25:38 +0000</pubDate>
		<dc:creator>Eugene</dc:creator>
				<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://hutorny.in.ua/?p=248</guid>
		<description><![CDATA[Model-Document-Renderer is an evolution of Passive-View design pattern, suggested by Martin Fowler [1]. The difference is mainly in the terminology and in the suggested way of decomposing the application. In this design pattern a web application is decomposed on Model, Document and Renderer (see Figure 1). It differs from &#8216;the talk of the town&#8217; MVC [...]]]></description>
			<content:encoded><![CDATA[<p>Model-Document-Renderer is an evolution of Passive-View design pattern, suggested by Martin Fowler [<a href="http://www.martinfowler.com/eaaDev/PassiveScreen.html">1</a>]. The difference is mainly in the terminology and in the suggested way of decomposing the application. <span id="more-248"></span><br />
In this design pattern a web application is decomposed on <strong>Model</strong>, <strong>Document</strong> and <strong>Renderer</strong> (see <a href="http://hutorny.in.ua/wp-content/uploads/2010/03/MDR.png" rel="thumbnail">Figure 1</a>). It differs from &#8216;the talk of the town&#8217; MVC (see <a href="http://hutorny.in.ua/wp-content/uploads/2010/03/MVC.png" rel="thumbnail">Figure 2</a>) by the fact that the View (1) degrades to simple renderer, loses (2) control and (3) dependency on the Model and thus becomes (4) application invariant. On the contrary, the Document (5) combines model-depended parts of the View and the Controller, implements (6) application-specific behaviour and (7) control on data flow.</p>
<table style="padding-left: 10px;" align="bottom" border="0" cellpadding="0" cellspacing="0" width="160">
<tbody>
<tr>
<td><a href="http://hutorny.in.ua/wp-content/uploads/2010/03/MDR.png" rel="thumbnail"><img src="http://hutorny.in.ua/wp-content/uploads/2010/03/MDR-330x100.png" alt="MDR" title="MDR" width="331" height="101" class="alignnone size-thumbnail wp-image-257" /></a></td>
<td><a href="http://hutorny.in.ua/wp-content/uploads/2010/03/MVC.png" rel="thumbnail"><img src="http://hutorny.in.ua/wp-content/uploads/2010/03/MVC-330x100.png" alt="MVC" title="MVC" width="331" height="101" class="alignnone size-thumbnail wp-image-258" /></a></td>
</tr>
<tr>
<td align="center"><b>Figure 1</b></td>
<td align="center"><b>Figure 2</b></td>
</tr>
</tbody>
</table>
<h3>Model</h3>
<p>The Model in MDR is the same as in MVC: it provides application domain objects and implements their persistence.</p>
<h3>Document</h3>
<p>The Document provides access to a concrete part of the model and implements certain use cases. It is expected to be application-specific and output-format-irrelevant. Also, it should not depend on the container (e.g. application server), leaving these aspects to the Application. Document is responsible for:</p>
<ol>
<li>getting proper business objects from the Model</li>
<li>applying proper template and renderer to the document data</li>
<li>presenting the rendered view to the user</li>
<li>capturing user&#8217;s action and data entered by the user</li>
<li>inferring the immediate document state from the data supplied by the user</li>
<li>updating the model when user selects appropriate action</li>
</ol>
<h3>Application</h3>
<p>The Application (not shown on the diagrams) is what actually deployed to and called by the application server. The Application may include several documents, which Application distinguishes by the URI and selects for processing a request. The Application should not directly embed any business logic, model references or rendering functionality. It is responsible for selecting an appropriate document and cross-document navigation.</p>
<h3>Renderer</h3>
<p>Renderer is responsible for:</p>
<ol>
<li>generating appropriate stream of tokens (HTML, XML, CSV, others)</li>
<li>properly decorating the data (escaping, quoting, etc)</li>
<li>localizing labels and captions</li>
<li>formatting the data according to the format patterns, specific for the selected template and/or the currently selected language</li>
</ol>
<p>A passive template engine is one of possible implementations of the Renderer. A good theory applicable to template engines is given in [<a href="http://www.cs.usfca.edu/%7Eparrt/papers/mvc.templates.pdf">2</a>]. It describes motivations for separating code and HTML and rules for strict separation. According to this theory, a template engine should implement four predicates: (1) attribute reference, (2) conditional inclusion, (3) recursive reference and (4) template application.</p>
<h3>References</h3>
<p>1. Passive View, Martin Fowler<br />
<a href="http://www.martinfowler.com/eaaDev/PassiveScreen.html">http://www.martinfowler.com/eaaDev/PassiveScreen.html</a></p>
<p>2. Enforcing Strict Model-View Separation in Template Engines.<br />
<a href="http://www.cs.usfca.edu/%7Eparrt/papers/mvc.templates.pdf">http://www.cs.usfca.edu/~parrt/papers/mvc.templates.pdf</a></p>
<p>3. Web Presentation Patterns, Jonathan Snook<br />
<a href="http://www.digital-web.com/articles/web_presentation_patterns/">http://www.digital-web.com/articles/web_presentation_patterns/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://hutorny.in.ua/java/model-document-renderer-design-pattern/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Case study on web frameworks</title>
		<link>http://hutorny.in.ua/java/case-study-on-web-frameworks</link>
		<comments>http://hutorny.in.ua/java/case-study-on-web-frameworks#comments</comments>
		<pubDate>Fri, 12 Mar 2010 15:06:31 +0000</pubDate>
		<dc:creator>Eugene</dc:creator>
				<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://hutorny.in.ua/?p=231</guid>
		<description><![CDATA[Goals The goal of this case-study is to make a short overview of the development tools for developing web-based business applications. The tools must (should) satisfy the following criteria: Best suits for developing server-side stateful applications with complex UI Good support for localization/internationalization Full isolation of applications – it should be possible (and simple) to [...]]]></description>
			<content:encoded><![CDATA[<h1 class="Heading1">Goals</h1>
<p>The goal of this case-study is to make a short overview of the development tools for developing web-based business applications. The tools must (should) satisfy the following criteria:<br />
<span id="more-231"></span></p>
<ol type="a">
<li>Best suits for developing server-side stateful applications with complex UI</li>
<li>Good support for localization/internationalization</li>
<li>Full isolation of applications – it should be possible (and simple) to deploy an application without (risk of) breaking other applications which run on the same production server.</li>
<li>Provide a mechanism for simple switching of web page appearance to &#8220;printer-friendly&#8221; or &#8220;mobile-device-friendly&#8221; look.</li>
<li>Use page templates in HTML so that designers can use WYSIWYG editors for making changes to the templates</li>
<li>Complete separation of code and HTML tags, so that changes in the application logic does not break the design</li>
</ol>
<h1 class="Heading1">Overview</h1>
<p>Software frameworks available for web-application are split onto three categories: (1) <span style="text-decoration: underline;"><a href="http://en.wikipedia.org/wiki/Web_portal">portals</a></span>, (2) <span style="text-decoration: underline;"><a href="http://en.wikipedia.org/wiki/Web_application_framework">web application frameworks</a></span> and (3) <span style="text-decoration: underline;"><a href="http://en.wikipedia.org/wiki/Template_engine_%28web%29">template engines</a></span> (for definitions please follow the references to Wiki).</p>
<h2>Portals</h2>
<p>Portals are designed to provide good integration of applications within an enterprise. This is going against our goal to have subscriber&#8217;s applications as much isolated as possible. Although, in general, this goal is achievable, but, as shows our experience with JBoss Portal, such isolation takes extra human and hardware resources.<br />
Portals are usually build on top of a web-development framework.</p>
<h2>Application frameworks</h2>
<p>Frameworks are designed to provide reusable functionality, design patterns, and offers highly-configurable environment.</p>
<h3>Reusable functionality</h3>
<p>To be truly useful, this functionality has to be learned by developers. Rich-on-functionality frameworks provide a lot more functionality but on the other hand requires more time to learn than simple ones. Open source frameworks usually poorly documented, which makes this learning curve much longer. As any software, frameworks contain bugs, and rich frameworks have even more bugs because it is made of bigger amount of source code. Accompanied with poor open source documentation, a simple issue may become a real obstacle just because it is hard to distinguish between a bug in framework and its misusage – system behaves in the way developers do not expect and no one can say whether this behaviour is correct or incorrect. Therefore experience is gained on trial-and-error path. This path makes the learning curve driving away from the application goals.</p>
<h3>Design patterns</h3>
<p>A typical framework is bound to few design patterns. These patterns were chosen by the vendor/developer of the framework and the reason for that choice is never or very rarely disclosed. Indeed, design patters have high impact on the product and the development process. Too concrete design patterns may not fit needs of a particular application. Too abstract design patterns give more chances to fail on mapping application. Proper design pattern will speedup the development process, improper – will slowdown.<br />
Most of the available web-development frameworks are based on Model-View-Controller (MVC) (see definition in <span style="text-decoration: underline;"><a href="http://en.wikipedia.org/wiki/Model_View_Controller">Wiki</a></span>, good description can be found on [<a href="http://www.digital-web.com/articles/web_presentation_patterns/">1</a>]). It is not clear why this three-letter-design-pattern become so popular. Besides serious drawbacks of this pattern, every vendor has they own understanding of what is view and what is controller [<a href="http://c2.com/cgi/wiki?WhatsaControllerAnyway">2</a>], which leads to various flavours of MVC, misimplementation and misusage of this design pattern.</p>
<h3>Drawbacks of MVC</h3>
<p>Paper [<a href="http://www.phpwact.org/pattern/model_view_controller#drawbacks_of_mvc">3</a>] identifies the following drawbacks of MVC:</p>
<ol type="a">
<li>Increased complexity</li>
<li>Close coupling of views and controllers to model</li>
<li>Potential for excessive updates</li>
<li>Close coupling between view and controller</li>
</ol>
<h3>Framework comparison</h3>
<p>Wiki lists [<a href="http://en.wikipedia.org/wiki/List_of_web_application_frameworks">4</a>] two dozens of web application frameworks. It does not seem possible to evaluate each of those, therefore we will relay on papers providing such comparisons: [<a href="https://appfuse-light.dev.java.net/framework-comparison/JavaWebFrameworkSweetSpots.pdf">5</a>], [<a href="http://bdn1.borland.com/article/borcon/files/6000/paper/6000.html">6</a>], [<a href="http://static.raibledesigns.com/repository/presentations/ComparingJavaWebFrameworks.pdf">7</a>], [<a href="http://en.wikipedia.org/wiki/Comparison_of_web_application_frameworks">8</a>]. As it appears, very few available frameworks provide good separation of concerns (code vs. HTML). This seems to be the consequence of using MVC as the design pattern and JSP as implementation of View. And even those few which claim good separation – Wicket and Trapestry forces to the component model, which will be an obstacle for producing printer-friendly version of pages.</p>
<h2>Template Engines</h2>
<p>List of Java template engines is given in [<a href="http://java-source.net/open-source/template-engines">9</a>]. Three of them that match our needs the best are <span style="text-decoration: underline;"><a href="http://velocity.apache.org/">Velocity</a></span>, <span style="text-decoration: underline;"><a href="http://www.stringtemplate.org/">StringTemplate</a></span> and <span style="text-decoration: underline;"><a href="http://www2.caret.cam.ac.uk/rsfwiki/Wiki.jsp?page=IKAT">IKAT</a></span>. All of them use similar approach – use of special programming language for templates. With Velocity we had some experience and it has shown that debugging such templates is a difficult task. This experience we may propagate on the other engines that use a programming language in templates.</p>
<h1 class="Heading1">Conclusion</h1>
<p>Existing frameworks and template engines, while giving advantage in some aspects of web programming, sacrifice the others, which makes the development process longer than expected, sometime even too long. It does not worth spending time on learning a framework which does not give obvious benefits in developing or maintaining the software.<br />
As the experience has shown, implementing a home-grown Java template engine takes efforts comparable to learning a new framework.</p>
<h1 class="Heading1">References</h1>
<p>1. Web Presentation Patterns, Jonathan Snook<br />
<a href="http://www.digital-web.com/articles/web_presentation_patterns/">http://www.digital-web.com/articles/web_presentation_patterns/</a></p>
<p>2. Whatsa Controller Anyway, KyleBrown,<br />
<a href="http://c2.com/cgi/wiki?WhatsaControllerAnyway">http://c2.com/cgi/wiki?WhatsaControllerAnyway</a></p>
<p>3. Model View Controller<br />
<a href="http://www.phpwact.org/pattern/model_view_controller">http://www.phpwact.org/pattern/model_view_controller</a></p>
<p>4. List of web application frameworks<br />
<a href="http://en.wikipedia.org/wiki/List_of_web_application_frameworks">http://en.wikipedia.org/wiki/List_of_web_application_frameworks</a></p>
<p>5. Java Web Framework Sweet Spots<br />
<a href="https://appfuse-light.dev.java.net/framework-comparison/JavaWebFrameworkSweetSpots.pdf">https://appfuse-light.dev.java.net/framework-comparison/JavaWebFrameworkSweetSpots.pdf</a></p>
<p>6. Comparison of Java Web Frameworks, Neal Ford<br />
<a href="http://bdn1.borland.com/article/borcon/files/6000/paper/6000.html">http://bdn1.borland.com/article/borcon/files/6000/paper/6000.html</a></p>
<p>7. Comparing Web Frameworks, Matt Raible<br />
<a href="http://static.raibledesigns.com/repository/presentations/ComparingJavaWebFrameworks.pdf">http://static.raibledesigns.com/repository/presentations/ComparingJavaWebFrameworks.pdf</a></p>
<p>8. Comparison of web application frameworks<br />
<a href="http://en.wikipedia.org/wiki/Comparison_of_web_application_frameworks">http://en.wikipedia.org/wiki/Comparison_of_web_application_frameworks</a></p>
<p>9. Open Source Template Engines in Java<br />
<a href="http://java-source.net/open-source/template-engines">http://java-source.net/open-source/template-engines</a></p>
<p>10. Enforcing Strict Model-View Separation in Template Engines.<br />
<a href="http://www.cs.usfca.edu/%7Eparrt/papers/mvc.templates.pdf">http://www.cs.usfca.edu/~parrt/papers/mvc.templates.pdf</a></p>
<p>11. Passive View, Martin Fowler<br />
<a href="http://www.martinfowler.com/eaaDev/PassiveScreen.html">http://www.martinfowler.com/eaaDev/PassiveScreen.html</a></p>
<p style="text-align:right"><sub>October 2007</sub></p>
]]></content:encoded>
			<wfw:commentRss>http://hutorny.in.ua/java/case-study-on-web-frameworks/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Exercise on implementing collaborations with ObjectTeams/Java</title>
		<link>http://hutorny.in.ua/java/exercise-on-implementing-collaborations-with-objectteams-java</link>
		<comments>http://hutorny.in.ua/java/exercise-on-implementing-collaborations-with-objectteams-java#comments</comments>
		<pubDate>Tue, 29 Dec 2009 15:41:10 +0000</pubDate>
		<dc:creator>Eugene</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[account]]></category>
		<category><![CDATA[cash]]></category>
		<category><![CDATA[collaboration]]></category>
		<category><![CDATA[objectteams]]></category>
		<category><![CDATA[role]]></category>
		<category><![CDATA[withdraw]]></category>

		<guid isPermaLink="false">http://hutorny.in.ua/?p=189</guid>
		<description><![CDATA[Introduction ObjectTeams/Java (OT/J) is an extension to Java programming language that facilitates roles and collaborations as the first class language constructs. This article is an exercise on using (OT/J) for implementing collaborations. Subject area This exercise considers two bank operations – money transfer and fund cashing. The course of events for these two operations is [...]]]></description>
			<content:encoded><![CDATA[<h3>Introduction</h3>
<p><a href="http://www.objectteams.org/">ObjectTeams/Java</a> (OT/J) is an extension to Java programming language that facilitates roles and collaborations as the first class language constructs. This article is an exercise on using (OT/J) for implementing collaborations.</p>
<h3>Subject area</h3>
<p>This exercise considers two bank operations – money transfer and fund cashing. The course of events for these two operations is very similar in basic and differs in details. Considering this, it is implemented as an abstract withdraw routine leaving detail differences to concrete implementations.<br />
<span id="more-189"></span></p>
<h3>An abstract withdraw routine</h3>
<table border="0" cellspacing="0" cellpadding="0" width="160" align="right" style="padding-left:10px;">
<tbody>
<tr>
<td><a href="http://hutorny.in.ua/wp-content/uploads/2009/12/CollaborationDiagram.png" rel="thumbnail"><img src="http://hutorny.in.ua/wp-content/uploads/2009/12/CollaborationDiagram-150x150.png" alt="Figure 1" title="Figure 1" width="150" height="150" class="alignnone size-thumbnail wp-image-192" /></a></td>
</tr>
<tr>
<td align="center"><b>Figure 1</b></td>
</tr>
</tbody>
</table>
<p>A withdraw routine executed in a bank allows to move funds from one account to another. The bank, which executes this operation, charges some fee, applied to the credit account and debited onto the bank&#8217;s operational account. When the currency of the operation is not the same as the bank&#8217;s native currency, the bank converts the fee to the native currency.<br />
A collaboration diagram for this operation is given on <a href="http://hutorny.in.ua/wp-content/uploads/2009/12/CollaborationDiagram.png" rel="thumbnail">Figure 1</a>. The collaboration steps and reiterated below:</p>
<ol>
<li>Calculate a fee for this operation</li>
<li>Charge the amount+fee from the credit account</li>
<li>Put the money on the debit account, which is      expected to accept any currency</li>
<li>If the currency of the operation differs from      the currency of the banks&#8217; operational account conversion is applied to the fee</li>
<li>Put the fee is on the operational account</li>
</ol>
<p>A function that implements this code is given below (for a complete class please refer to the <a href="http://hutorny.in.ua/wp-content/uploads/2009/12/exer1/src/ua/in/hutorny/otj/exer1/accounting/WithdrawCollaboration.java">source</a>):</p>
<pre class="brush: java; title: ;">
	protected final void withdraw(BigDecimal amount, CreditAccount creditAccount, DebitAccount debitAccount,
			OperationalAccount operationalAccount, CurrencyConverter currencyConvertor) {
		BigDecimal fee =  calculateFee(amount);
		creditAccount.credit(amount.add(fee));
		debitAccount.debit(amount,creditAccount.currency());
		if( creditAccount.currency() != operationalAccount.currency())
			fee = currencyConvertor.convert(creditAccount.currency(), operationalAccount.currency(), fee);
		operationalAccount.debit(fee);
	}
</pre>
<p>In this exercise, this method is implemented in scope of a team class Withdraw Collaboration and the parameter types are defined as role classes in the team. This class is pretty isolated and depends only on Currency class, so it can be reused in other applications. Details of the account implementations are hidden behind the roles.</p>
<h3>Concrete withdrawal routines</h3>
<p>A bank that implements concrete withdrawal routines, gives its clients two interfaces &#8211; the Cashier interface that allows people to get some cash from they accounts, and the Wire Operator interface that allows them to transfer funds from the account they have in this bank to any other account. In scope of this exercise, these two interfaces are implemented by two collaboration, both extending the Withdraw Collaboration. Each collaboration is instantiated in the bank for a given account and then returned to the client. Once the interface is provided, the client may execute more than one operation with the current account.<br />
Since there could be a rather long thinking time, the bank can not provide an operational account at the time when the interface is acquired, but instead the operation account should be determined at the moment of transaction execution. To achieve this, these collaborations are extended with an additional role – Operator which provides access to the available operational account at the moment of transaction execution.<br />
Additional complications with implementing the Cashier that there is not debit account available, since the money are given to a person and the person cannot accept a decimal value, he/she can only take cash, so the digits should be converted to cash. Code snippet for this operation is given below (for a complete class please refer to the <a href="http://hutorny.in.ua/wp-content/uploads/2009/12/exer1/src/ua/in/hutorny/otj/exer1/bank/GetCashFromBankCollaboration.java">source</a>):</p>
<pre class="brush: java; title: ;">
	public void giveCashTo(Person person, double amount) {
		BigDecimal value = new BigDecimal(amount);
		reserved = bank.acquireCash(value, account.currency());
		try {
			cashOperation(new BigDecimal(amount), account, person, bank, bank);
		}
		finally {
			if( reserved.value().signum() != 0 ) {
			// Recipient did not take the money
				bank.returnCash(reserved);
			}
		}
	}
</pre>
<p>To match the Person entity with the DebitAccount interface, a new role is defined as below:</p>
<pre class="brush: java; title: ;">
	protected class CashRecipient extends DebitAccount playedBy Person {

		void takes(Cash cash) -&gt; void takes(Cash cash);

		protected void debit(BigDecimal amount, Currency currency) {
			assert reserved.value() == amount &amp;&amp; reserved.currency() == currency;
			takes(reserved);
		}
	}
</pre>
<h3>Bank</h3>
<p>In this exercise a bank has an internal operational account, and a safe, where all cash is kept. It provides methods for opening accounts (credit and current) and access to two already mentioned interfaces Cashier and Wire Operator. BankAccount is an protected inner class of the Bank to ensure that no one can create a bank account outside of a bank (for a complete class please refer to the <a href="http://www.hutorny.in.ua/wp-content/uploads/2009/12/exer1/src/ua/in/hutorny/otj/exer1/bank/Bank.java">source</a>).</p>
<h3>Cash</h3>
<p>If everyone would be able to create cash in their own, there would be no need for banks and accounts <img src='http://hutorny.in.ua/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> . Also, when cash is being added or subtracted, its behavior differs very much from adding two numbers. To model these features of the cash, a dedicated class is provided. The only public constructor it provides creates zero cash. The add operation accepts Cash as the addendum and clears it after adding. The subtract operation accepts a number and creates a new cash for the amount of subtrahend. Also it provides an instruction to the garbage collector to report any occurrence of valuable cash in the garbage (for a complete class please refer to the <a href="http://hutorny.in.ua/wp-content/uploads/2009/12/exer1/src/ua/in/hutorny/otj/exer1/money/Cash.java">source</a>).</p>
<h3>Modeling the plan</h3>
<p>With the mentioned above classes (and some additional) we can now model Tom and Mary’s plan:<br />
Entities that are assumed to exist:</p>
<pre class="brush: java; title: ;">
	Bank spicer  = new Bank(&quot;JVM-Spicer,MN &quot;, Currency.USD);
	Bank willmar = new Bank(&quot;OTJ-Willmar,MN&quot;, Currency.EUR);
	Bank bahama  = new Bank(&quot;Java-Bahamas  &quot;, Currency.BSD);

	Person tom  = new Person(&quot;Tom &quot;);
	Person mary = new Person(&quot;Mary&quot;);
</pre>
<p>The plan:</p>
<pre class="brush: java; title: ;">
		Account credit = spicer.openCreditAccount(1000000, Currency.USD);
		Cashier cashier = spicer.cashier(credit);
		cashier.giveCashTo(tom, 970000);

		mary.takes(	tom.gives(965000, Currency.USD) );

		Account deposit = willmar.openAccount(mary.gives(960000,Currency.USD));

		Account bahamamama = bahama.openAccount(mary.gives(1000,Currency.USD));

		WireTransfer wire = willmar.wireOperator(deposit);
		wire.wireTo(bahamamama, 950000);

		cashier = bahama.cashier(bahamamama);
		cashier.giveCashTo(mary, 923000);

		tom.takes( mary.gives(461000, Currency.USD) );
</pre>
<p>All sources are available for downloading as a single <a href='http://hutorny.in.ua/wp-content/uploads/2009/12/exer1-src.zip'>archive</a> under The GNU Lesser General Public License (LGPLv3).</p>
<h3>Conclusion</h3>
<p>Collaboration design pattern is a useful abstraction for designing the application. Implementing collaborations with a new, promising Java language extension facilitates using of the first class language constructs (teams) and significantly improves the separation of concerns letting the developer to concentrate on one task at a time.</p>
]]></content:encoded>
			<wfw:commentRss>http://hutorny.in.ua/java/exercise-on-implementing-collaborations-with-objectteams-java/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using Java langauge constructs to define static graphs and trees</title>
		<link>http://hutorny.in.ua/java/using-java-langauge-constructs-to-define-static-graphs-and-trees</link>
		<comments>http://hutorny.in.ua/java/using-java-langauge-constructs-to-define-static-graphs-and-trees#comments</comments>
		<pubDate>Wed, 09 Dec 2009 10:13:01 +0000</pubDate>
		<dc:creator>Eugene</dc:creator>
				<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://hutorny.in.ua/?p=120</guid>
		<description><![CDATA[This article suggests an approach on adapting the Java programming language for defining static graphs and trees, gives an exercise on defining a microwave finite state automaton (FSA) using this approach and provides code sample for processing graph definitions]]></description>
			<content:encoded><![CDATA[<p>Graphs, networks and trees are commonly used to model application domains. In some cases these structures are highly dynamic and can not be defined at design time. Yet there are also cases when they are rather static and do not change during the application life cycle. An example of such static structure is a finite state automaton – defined as a part of design, it remains unchanged at the run-time.<br />
<span id="more-120"></span><br />
However, programming languages do not provide facilities that would make creation of graphs (trees) an easy task. Most common approach is to use XML with a custom schema and encode a graph (a tree) with XML tags. Unfortunately, development tools does not provide the same degree of support for editing an XML file as they do for many wide spread programming languages – detecting typos as  you type, reference navigation with one mouse click, etc. Additionally, using an XML requires a utility to process the XML and build a model corresponding to the definition. Another approach to define a graph &#8211; instantiating objects and establishing references among them “by hands”. The code created with this approach is burdened with poor readability and maintainability. An ideal solution would be using a domain specific language for these purposes, however cost of creating such a language is tremendous at present time.<br />
This article suggests an approach on adapting the Java programming language for defining static graphs, trees.</p>
<h3>The idea</h3>
<p>If we look at Java classes, interfaces and references among them at the high level of abstraction, their appear to be a linked graph where classes/interfaces can be thought as nodes and references – as links between them. These thoughts, being turned inside out, lead to the following rationales: a graph can be represented as a set of interfaces where an interface represents a node and a reference to another interface represents a link. Such references, for instance, could be return types of methods defined in the interface.</p>
<h3>Rationales</h3>
<p>The following table lists rationales on graph representation.</p>
<table border="1" cellspacing="0" cellpadding="0" width="100%" style="border-collapse:collapse;">
<tbody>
<tr>
<td width="199" valign="top"><em>FSA primitive</em></td>
<td width="458" valign="top"><em>Represented as</em></td>
</tr>
<tr>
<td width="199" valign="top"><strong>State chart</strong></td>
<td width="458" valign="top">A Java class that includes definition of all its states</td>
</tr>
<tr>
<td width="199" valign="top"><strong>State</strong></td>
<td width="458" valign="top">An interface extending <strong>State</strong> interface</td>
</tr>
<tr>
<td width="199" valign="top">Set of <strong>On Enter</strong> actions</td>
<td width="458" valign="top">One or more methods returning <strong>void</strong> and annotated with <strong>@OnEnter</strong></td>
</tr>
<tr>
<td width="199" valign="top"><strong>On Enter Action</strong></td>
<td width="458" valign="top">A parameter type extending <strong>Action</strong> of a method mentioned above</td>
</tr>
<tr>
<td width="199" valign="top">Set of <strong>On Leave</strong> actions</td>
<td width="458" valign="top">One or   more methods returning <strong>void</strong> and annotated with <strong>@OnLeave</strong></td>
</tr>
<tr>
<td width="199" valign="top"><strong>On Leave Action</strong></td>
<td width="458" valign="top">A parameter type extending <strong>Action</strong> of a method mentioned above</td>
</tr>
<tr>
<td width="199" valign="top"><strong>Transition</strong></td>
<td width="458" valign="top">A method returning interface corresponding to the “<strong>To</strong>” state</td>
</tr>
<tr>
<td width="199" valign="top"><strong>Condition</strong></td>
<td width="458" valign="top">A   parameter type extending <strong>Condition</strong> of a method mentioned above</td>
</tr>
<tr>
<td width="199" valign="top"><strong>Action</strong></td>
<td width="458" valign="top">A   parameter type extending <strong>Action</strong> of   a method mentioned above</td>
</tr>
</tbody>
</table>
<p></p>
<table border="0" cellspacing="0" cellpadding="0" width="160" align="right" style="padding-left:10px;">
<tbody>
<tr>
<td>
<a href="http://hutorny.in.ua/wp-content/uploads/2009/12/microwave.png" rel="thumbnail"><img src="http://hutorny.in.ua/wp-content/uploads/2009/12/microwave-150x150.png" alt="Microwave Automaton" title="microwave" width="150" height="150" class="size-thumbnail wp-image-127" /></a></td>
</tr>
<tr>
<td align="center"><b>Figure 1</b></td>
</tr>
</tbody>
</table>
<h3>Microwave automaton</h3>
<p>Lets consider a simple microwave with one push-button and a knob timer. This microwave starts heating when the user pushes the button and the door is closed and the timer is set. Pushing button again stops heating, opening the door suspends heating until the door is closed again. A state chart for this microwave is shown on <a href="http://hutorny.in.ua/wp-content/uploads/2009/12/microwave.png" rel="thumbnail"><b>Figure 1</b></a>. The following code snippet shows how this state chart can be represented with the rationales given above.</p>
<table border="0" cellspacing="0" cellpadding="0" width="100%" >
<tbody>
<tr>
<td>&nbsp;</td>
</tr>
</tbody>
</table>
<pre class="brush: java; title: ;">
public class MicrowaveStateMachine {
  /**
   * In Door Open State MW waits until the door is closed
   */
  interface DoorOpenState extends State {
	  @OnEnter void enter	(DoDing a);                            	// on entering Door Open State issue a Ding action
	  WaitingState  idle   	(OnDoorClose e, IfTimerIsSet a);  		// on Door Close Event transit to Waiting State
	  HeatingState  resume 	(OnDoorClose e, IfTimerIsNotSet a); 	// on Door Close Event transit to Waiting State
	  DoorOpenState ignore 	(OnPushButton e, DoBeep a);          	// on Push Button Event issue a Beep and remain in DoorOpenState
	  @OnLeave void leave	(DoDong a);								// on entering Door Close State issue a Dong
  }
  /**
   * In Waiting State MW waits until a button is pushed
   * If timer is set, it starts heating, otherwise it beeps
   */
  interface WaitingState extends State {
	  HeatingState  start   (OnPushButton e, IfTimerIsSet c);		// on Push Button Event if timer is set transit to heating
	  WaitingState  beep    (OnPushButton e, IfTimerIsNotSet c, DoBeep a); // otherwise issue a Beep and remain in waiting
	  DoorOpenState open    (OnDoorOpen e);							// on Door Open Event transit to Door Open State
  }
  /**
   * In Heating state it actually heats.
   * On entering this state MW starts the heater and the cook timer;
   * On leaving this state MW stops the heater and suspends the cook timer
   */
  interface HeatingState extends State {
	  @OnEnter void enter	(DoStartHeater a, DoStartCookTimer t); 	// on entering HeatingState State issue Start Heater Action
	  WaitingState  timer	(OnCookTimerExpired e);					// on Timer Expired Event transit to Waiting State
	  WaitingState  stop	(OnPushButton e);						// on Push Button Event transit to WaitingState State
	  DoorOpenState open	(OnDoorOpen e);							// on Door Open Event transit to Door Open State
	  @OnLeave void leave	(DoStopHeater a, DoSuspendCookTimer b);	// on leave issue DoStopHeater
  }
}
</pre>
<h3>Constructing run-time artifacts</h3>
<p>Obviously, this is only definition and it requires, as well as an XML definition, some code to turn it into a run-time artifact. Java Reflection API provides all necessary means for examining the definition but producing a valuable output is an application specific task. In other words, the task is split on two concerns &#8211; (1) examining the definition and (2) generating the output. The source code sample <a href="#attachments">attached</a> to this article provides a utility class that implements first concern (<code>Walker</code>) and an application class (<code>MicrowaveSample</code>) that generates microwave automaton assembler code for a PICmicro MCU (see sample output below). <code>Walker</code> class exposes one method <code>processAutomaton</code> accepting a state chart definition class and communicates with <code>MicrowaveSample</code> via <code>WalkerCallback</code> interface implemented by <code>MicrowaveSample</code>.</p>
<p>[picmicro collapse="true"]<br />
;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br />
DoorOpenState<br />
    call DoDing<br />
DoorOpenState_begin<br />
    call sleepAndWaitEvents<br />
DoorOpenState_0<br />
    btfss OnDoorClose    ; resume<br />
    bra DoorOpenState_1<br />
    btfss IfTimerIsNotSet<br />
    bra DoorOpenState_1<br />
    call DoorOpenState_leave<br />
    bra HeatingState<br />
DoorOpenState_1<br />
    btfss OnDoorClose    ; idle<br />
    bra DoorOpenState_2<br />
    btfss IfTimerIsSet<br />
    bra DoorOpenState_2<br />
    call DoorOpenState_leave<br />
    bra WaitingState<br />
DoorOpenState_2<br />
    btfss OnPushButton    ; ignore<br />
    bra DoorOpenState_3<br />
    call DoBeep<br />
    call DoorOpenState_leave<br />
    bra DoorOpenState<br />
DoorOpenState_3<br />
DoorOpenState_end<br />
    bra DoorOpenState_begin<br />
DoorOpenState_leave<br />
    call DoDong<br />
    return<br />
HeatingState<br />
    call DoStartHeater<br />
    call DoStartCookTimer<br />
HeatingState_begin<br />
    call sleepAndWaitEvents<br />
HeatingState_0<br />
    btfss OnPushButton    ; stop<br />
    bra HeatingState_1<br />
    call HeatingState_leave<br />
    bra WaitingState<br />
HeatingState_1<br />
    btfss OnDoorOpen    ; open<br />
    bra HeatingState_2<br />
    call HeatingState_leave<br />
    bra DoorOpenState<br />
HeatingState_2<br />
    btfss OnCookTimerExpired    ; timer<br />
    bra HeatingState_3<br />
    call HeatingState_leave<br />
    bra WaitingState<br />
HeatingState_3<br />
HeatingState_end<br />
    bra HeatingState_begin<br />
HeatingState_leave<br />
    call DoStopHeater<br />
    call DoSuspendCookTimer<br />
    return<br />
WaitingState<br />
WaitingState_begin<br />
    call sleepAndWaitEvents<br />
WaitingState_0<br />
    btfss OnPushButton    ; start<br />
    bra WaitingState_1<br />
    btfss IfTimerIsSet<br />
    bra WaitingState_1<br />
    bra HeatingState<br />
WaitingState_1<br />
    btfss OnDoorOpen    ; open<br />
    bra WaitingState_2<br />
    bra DoorOpenState<br />
WaitingState_2<br />
    btfss OnPushButton    ; beep<br />
    bra WaitingState_3<br />
    btfss IfTimerIsNotSet<br />
    bra WaitingState_3<br />
    call DoBeep<br />
    bra WaitingState<br />
WaitingState_3<br />
WaitingState_end<br />
    bra WaitingState_begin<br />
[/picmicro]</p>
<table border="0" cellspacing="0" cellpadding="0" width="160" align="right" style="padding-left:10px;">
<tbody>
<tr>
<td><a href="http://hutorny.in.ua/wp-content/uploads/2009/12/microwave-new.png" rel="thumbnail"><img src="http://hutorny.in.ua/wp-content/uploads/2009/12/microwave-new-150x150.png" alt="microwave-new" title="microwave-new" width="150" height="150" class="alignnone size-thumbnail wp-image-149" /></a></td>
</tr>
<tr>
<td align="center"><b>Figure 2</b></td>
</tr>
</tbody>
</table>
<h3>Extending state charts</h3>
<p>Now, lets image that we have this microwave automaton in full production, and R&#038;D department came up with a innovative and conceptual design of a microwave with <b>two</b> buttons <img src='http://hutorny.in.ua/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  and sales department insisted on adding a &#8216;polite reminder&#8217; feature. State chart for the new microwave automaton is given on <a href="http://hutorny.in.ua/wp-content/uploads/2009/12/microwave-new.png" rel="thumbnail"><b>Figure 2</b></a> where blue color indicates new state and transitions and red cross denotes transition no longer needed. A big part of the state chart remains the same as in previous version one and, thus, can be reused in a new state chart. With the approach that this article suggests, a state chart can be extended by means of Java inheritance, e.g. new state chart <code>extends</code> the previous. With very little efforts the new automaton is defined as given below. </p>
<table border="0" cellspacing="0" cellpadding="0" width="100%" >
<tbody>
<tr>
<td>&nbsp;</td>
</tr>
</tbody>
</table>
<pre class="brush: java; title: ;">
/**
 * State machine of a microwave oven with two buttons and polite reminder feature:
 * when cooked and door was not open - beep every N seconds during M minutes
 */
public class ModernMicrowaveStateMachine extends MicrowaveStateMachine {
	  /**
	   * This machine introduces several new events and actions
	   */
	  interface OnPushStopButton extends Event {}
	  interface OnPoliteBeepTimer extends Event {}
	  interface OnPoliteOffTimer extends Event {}
	  interface DoStartPoliteBeepTimer extends Action {}
	  interface DoStopPoliteBeepTimer extends Action {}
	  interface DoStartPoliteOffTimer extends Action {}
	  interface DoStopPoliteOffTimer extends Action {}
	  interface DoAddOneMinuteCookTimer extends Action {}

	  /**
	   * Alter the inherited Heating State
	   */
	  interface HeatingState extends MicrowaveStateMachine.HeatingState {
		  HeatingState	add		(OnPushButton e, DoAddOneMinuteCookTimer a);  // add one minute to the timer when push button is pressed
		  @Disable
		  WaitingState	timer	(OnCookTimerExpired e);					// disable this transition inherited from the old MW
		  CookedState   cooked	(OnCookTimerExpired e);					// introduce new transition on Timer Expired Event transit to CookedState State
		  WaitingState  stop	(OnPushStopButton e);					// on Push Stop Button Event transit to WaitingState State
		  @Disable
		  WaitingState  stop	(OnPushButton e);						// disable this transition inherited from the old MW
	  }
	  /**
	   * Add new state to provide polite beep reminder
	   */
	  interface CookedState extends State {
		  @OnEnter void onEnter	(DoStartPoliteBeepTimer a, DoStartPoliteOffTimer b ); // on enter start timers Polite Beep and Polite Off
		  DoorOpenState stop	(OnDoorOpen e);							// on Door Open Event transit to Door Open State
		  HeatingState  start1	(OnPushButton e, IfTimerIsNotSet i, DoAddOneMinuteCookTimer t); // on Push Button Event transit to WaitingState State
		  HeatingState  start	(OnPushButton e, IfTimerIsSet i);  		// on Push Button Event transit to WaitingState State
		  WaitingState  stop	(OnPushStopButton e); 					// on Push Stop Button Event transit to WaitingState State
		  WaitingState  idle	(OnPoliteOffTimer e); 					// stop beeping after M minutes
		  CookedState   beep	(OnPoliteBeepTimer e); 					// beep every N seconds
		  @OnLeave void onleave	(DoStopPoliteBeepTimer a, DoStopPoliteOffTimer b ); 	// on leave stop timers
	  }
}
</pre>
<p>There is one new annotation added: <code>@Disabled</code>. It is used to indicate a deletion of unused links that were inherited from the original chart. Output for this automaton is following</p>
<p>[picmicro collapse="true"]<br />
;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br />
CookedState<br />
    call DoStartPoliteBeepTimer<br />
    call DoStartPoliteOffTimer<br />
CookedState_begin<br />
    call sleepAndWaitEvents<br />
CookedState_0<br />
    btfss OnPushButton    ; start<br />
    bra CookedState_1<br />
    btfss IfTimerIsSet<br />
    bra CookedState_1<br />
    call CookedState_leave<br />
    bra HeatingState<br />
CookedState_1<br />
    btfss OnPushStopButton    ; stop<br />
    bra CookedState_2<br />
    call CookedState_leave<br />
    bra WaitingState<br />
CookedState_2<br />
    btfss OnDoorOpen    ; stop<br />
    bra CookedState_3<br />
    call CookedState_leave<br />
    bra DoorOpenState<br />
CookedState_3<br />
    btfss OnPoliteOffTimer    ; idle<br />
    bra CookedState_4<br />
    call CookedState_leave<br />
    bra WaitingState<br />
CookedState_4<br />
    btfss OnPoliteBeepTimer    ; beep<br />
    bra CookedState_5<br />
    call CookedState_leave<br />
    bra CookedState<br />
CookedState_5<br />
    btfss OnPushButton    ; start1<br />
    bra CookedState_6<br />
    btfss IfTimerIsNotSet<br />
    bra CookedState_6<br />
    call DoAddOneMinuteCookTimer<br />
    call CookedState_leave<br />
    bra HeatingState<br />
CookedState_6<br />
CookedState_end<br />
    bra CookedState_begin<br />
CookedState_leave<br />
    call DoStopPoliteBeepTimer<br />
    call DoStopPoliteOffTimer<br />
    return<br />
HeatingState<br />
    call DoStartHeater<br />
    call DoStartCookTimer<br />
HeatingState_begin<br />
    call sleepAndWaitEvents<br />
HeatingState_0<br />
    btfss OnPushButton    ; add<br />
    bra HeatingState_1<br />
    call DoAddOneMinuteCookTimer<br />
    call HeatingState_leave<br />
    bra HeatingState<br />
HeatingState_1<br />
    btfss OnPushStopButton    ; stop<br />
    bra HeatingState_2<br />
    call HeatingState_leave<br />
    bra WaitingState<br />
HeatingState_2<br />
    btfss OnCookTimerExpired    ; cooked<br />
    bra HeatingState_3<br />
    call HeatingState_leave<br />
    bra CookedState<br />
HeatingState_3<br />
HeatingState_end<br />
    bra HeatingState_begin<br />
HeatingState_leave<br />
    call DoStopHeater<br />
    call DoSuspendCookTimer<br />
    return<br />
DoorOpenState<br />
    call DoDing<br />
DoorOpenState_begin<br />
    call sleepAndWaitEvents<br />
DoorOpenState_0<br />
    btfss OnDoorClose    ; resume<br />
    bra DoorOpenState_1<br />
    btfss IfTimerIsNotSet<br />
    bra DoorOpenState_1<br />
    call DoorOpenState_leave<br />
    bra HeatingState<br />
DoorOpenState_1<br />
    btfss OnDoorClose    ; idle<br />
    bra DoorOpenState_2<br />
    btfss IfTimerIsSet<br />
    bra DoorOpenState_2<br />
    call DoorOpenState_leave<br />
    bra WaitingState<br />
DoorOpenState_2<br />
    btfss OnPushButton    ; ignore<br />
    bra DoorOpenState_3<br />
    call DoBeep<br />
    call DoorOpenState_leave<br />
    bra DoorOpenState<br />
DoorOpenState_3<br />
DoorOpenState_end<br />
    bra DoorOpenState_begin<br />
DoorOpenState_leave<br />
    call DoDong<br />
    return<br />
WaitingState<br />
WaitingState_begin<br />
    call sleepAndWaitEvents<br />
WaitingState_0<br />
    btfss OnPushButton    ; start<br />
    bra WaitingState_1<br />
    btfss IfTimerIsSet<br />
    bra WaitingState_1<br />
    bra HeatingState<br />
WaitingState_1<br />
    btfss OnDoorOpen    ; open<br />
    bra WaitingState_2<br />
    bra DoorOpenState<br />
WaitingState_2<br />
    btfss OnPushButton    ; beep<br />
    bra WaitingState_3<br />
    btfss IfTimerIsNotSet<br />
    bra WaitingState_3<br />
    call DoBeep<br />
    bra WaitingState<br />
WaitingState_3<br />
WaitingState_end<br />
    bra WaitingState_begin<br />
[/picmicro]</p>
<h3>Representing other graph and tree-alike structures</h3>
<p>Although, this article is focused on finite state automatons, similar approach can be applied for defining other kind of graphs and trees. The author has a proof of concept for using nested classes as a representation of a web-application URI structure and mapping HTTP requests issued by very complex but strictly structured HTML forms.</p>
<p><a id="attachments"></a></p>
<h3>Attachments</h3>
<p><a href='http://hutorny.in.ua/wp-content/uploads/2009/12/FSA-Sources.zip'>FSA-Sources</a></p>
<table border="0" cellspacing="0" cellpadding="0" width="100%" >
<tbody>
<tr>
<td>&nbsp;</td>
</tr>
</tbody>
</table>
]]></content:encoded>
			<wfw:commentRss>http://hutorny.in.ua/java/using-java-langauge-constructs-to-define-static-graphs-and-trees/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A convenient approach on keeping SQL in Java</title>
		<link>http://hutorny.in.ua/java/a-convenient-approach-on-keeping-sql-in-java</link>
		<comments>http://hutorny.in.ua/java/a-convenient-approach-on-keeping-sql-in-java#comments</comments>
		<pubDate>Tue, 24 Nov 2009 12:00:45 +0000</pubDate>
		<dc:creator>Eugene</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[comments]]></category>
		<category><![CDATA[queries]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://hutorny.in.ua/?p=109</guid>
		<description><![CDATA[Where to keep SQL statements? This question arises almost for any more-less sophisticated Java application that accesses DBMS. Two most common approaches are: (1) as string constants in Java and (2) as text in external files. Both these approaches have significant drawbacks. Java strings get complicated when a string is too long to fit in [...]]]></description>
			<content:encoded><![CDATA[<p>Where to keep SQL statements? This question arises almost for any more-less sophisticated Java application that accesses DBMS. Two most common approaches are: (1) as string constants in Java and (2) as text in external files. Both these approaches have significant drawbacks. Java strings get complicated when a string is too long to fit in one line, leading to a difficult to read and hard to maintain SQL code. SQL in external files are not easy to lookup and require some mapping technique to associate statement in the external file with a place in Java where it is needed. Also, as development goes, the text file adheres with more and more orphan SQL statements and there is no easy way to clean them up. This article suggests an approach that combines two mentioned above and inherits their strong sides.<br />
<span id="more-109"></span>The idea is very simple – keep SQL statements in the source Java file as comments following the definition of a string member, load them at run-time and assign to the corresponding member. The following example gives a grasp on this technique.</p>
<pre class="brush: java; title: ;">
public class SQLpeople {
  public static final String sqlSELECTall = Comments.load();
  /*--
    SELECT * FROM people;
    --*/
}
</pre>
<p>To make it available at run-time, this source file should be packaged together with the class file. That&#8217;s everything you need to make SQL statements loaded&#8230;<br />
Well, almost everything <img src='http://hutorny.in.ua/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> . One minor thing left is an implementation of Comments.load(). You may implement it in your own or download from <a href="http://sourceforge.net/projects/commentsload/files/">SourceForge</a> an open source implementation (under <a href="http://www.opensource.org/licenses/lgpl-3.0.html">LGPLv3 license</a>).</p>
<h3>Dialectism</h3>
<p>If your application is designed to run with different DBMS engines, maintaining SQL in different dialects may become a challenge. The Comments class proposes a solution as illustrated below:</p>
<pre class="brush: java; title: ; wrap-lines: false;">
public class SQLpeople {
  public static final String sqlSELECTtopN = Comments.load(getCurrentDBMS());
  /*--
    --: pgsql
    SELECT * FROM people LIMIT ?;
    --: mssql
    SELECT TOP ? * FROM people;
    --*/
}
</pre>
<p>Where getCurrentDBMS() is your static function that returns name of the current DBMS, and <code>--:</code> pgsql and <code>--:</code> mssql are discriminator values designating statements appropriate for different DBMS.</p>
<h3>How it works</h3>
<p>The <em>Comments</em> class just a friendly decorator created for the sake of convenience. Its method load() uses StackFrame to gather information about invocation point and passes name of the source file, line number (<em>N</em>), and class name to the CommentRetriever.retrieve(), which opens the source file as a resource stream, skips <em>N</em> lines, reads content until it finds <strong><code>--*/</code></strong> in the line and then uses regex to extract content between <strong><code>/*--</code></strong> and <strong><code>--*/</code></strong>. If the <em>discriminator</em> parameter is null, <em>retrieve</em> returns extracted content. Otherwise it finds designators <strong><code>--:</code> &lt;discriminator&gt;</strong> and tests its equity against the parameter. If they are equal method returns content of the section between the current discriminator and the next one or end of the block. If block contains no discriminator equal to the parameter, it returns content before the first discriminator, unless default values are disabled by <i>NODEFAULTS</i> option.<br />
That’s all you need to start using this class, for advanced topics please refer to <a href="http://hutorny.in.ua/common/commentsload/javadoc/">Java docs</a>.</p>
<h3>Guidelines on authoring comments</h3>
<p>There should be no characters between the end of the load() line and sequence <code>/*--</code> that opens the block, except, of course, white spaces (space, tab, new line).</p>
<pre class="brush: java; title: ; wrap-lines: false;">
public class SQLpeople {
  /*
   * allowed comment
   */

  public static final String sqlSELECTall = Comments.load(); -- allowed comment

  /* disallowed comment */
  /*--
    SELECT * FROM people;
    --*/

}
</pre>
<p>Discriminator takes entire line, there should be no other text in this line</p>
<h3>Errors</h3>
<p>By default error messages are returned as output value and prepended with <code>"--"</code>, are returned as the result of load() method. Although, this behavior can be opted (see <a href="http://hutorny.in.ua/common/commentsload/javadoc/">Java docs</a> for details).</p>
]]></content:encoded>
			<wfw:commentRss>http://hutorny.in.ua/java/a-convenient-approach-on-keeping-sql-in-java/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Theoretical aspects of dimming an incandescent lamp</title>
		<link>http://hutorny.in.ua/research/theoretical-aspects-of-dimming-an-incandescent-lamp</link>
		<comments>http://hutorny.in.ua/research/theoretical-aspects-of-dimming-an-incandescent-lamp#comments</comments>
		<pubDate>Thu, 23 Jul 2009 12:49:26 +0000</pubDate>
		<dc:creator>Eugene</dc:creator>
				<category><![CDATA[Research]]></category>

		<guid isPermaLink="false">http://hutorny.in.ua/research/theoretical-aspects-of-dimming-an-incandescent-lamp</guid>
		<description><![CDATA[Abstract A dimmer is an electronic device that controls alternate voltage applied to a lamp through delivering a selected portion of the mains sinusoid. Engineer, designing a dimmer needs to estimate how big this portion should be to get a desired luminance level. This article uses a model of incandescent lamp, tungsten resistivity, and human [...]]]></description>
			<content:encoded><![CDATA[<h1 class="Heading1">Abstract</h1>
<p>A dimmer is an electronic device that controls alternate voltage applied to a lamp through delivering a selected portion of the mains sinusoid. Engineer, designing a dimmer needs to estimate how big this portion should be to get a desired luminance level. This article uses a model of incandescent lamp, tungsten resistivity, and human eye spectral efficiency to derive dependency of produced luminous flux over the voltage, gives a simple analytical function that describes this dependency with good accuracy (&plusmn;2% comparing to the model).</p>
<p><span id="more-105"></span></p>
<p align="right"><small><a href="http://hutorny.in.ua/ta/Theoretical-aspects-of-dimming-an-incandescent-lamp.pdf">Download PDF version of this article</a></small></p>
<h1 class="Heading1">Structured Abstract</h1>
<table border=1 cellspacing=0 cellpadding=0 style='border-collapse:collapse;border:solid windowtext 1.0pt'>
<tr>
<td width=235 valign=top style='width:176.4pt;border:solid windowtext 1.0pt; border-top:solid windowtext 1.0pt;padding:0cm 2pt 0cm 2pt'>
<p>Purpose</p>
</td>
<td width=482 valign=top style='width:361.35pt;border-top:solid windowtext 1.0pt;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 2pt 0cm 2pt'>
<p>To discover a dependency of luminous flux produced by an incandescent lamp on the applied voltage</p>
</td>
</tr>
<tr>
<td width=235 valign=top style='width:176.4pt;border:solid windowtext 1.0pt; border-top:none;padding:0cm 2pt 0cm 2pt'>
<p>Methodology/Approach</p>
</td>
<td width=482 valign=top style='width:361.35pt;border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 2pt 0cm 2pt'>
<p>Numerical analysis on a theoretical model of an incandescent lamp</p>
</td>
</tr>
<tr>
<td width=235 valign=top style='width:176.4pt;border:solid windowtext 1.0pt; border-top:none;padding:0cm 2pt 0cm 2pt'>
<p>Findings</p>
</td>
<td width=482 valign=top style='width:361.35pt;border-top:none;border-left: none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt; padding:0cm 2pt 0cm 2pt'>
<p>Analytical function for approximate estimating of luminous flux for the applied voltage with &plusmn;2% accuracy</p>
</td>
</tr>
<tr>
<td width=235 valign=top style='width:176.4pt;border:solid windowtext 1.0pt; border-top:none;padding:0cm 2pt 0cm 2pt'>
<p>Research limitations/implications</p>
</td>
<td width=482 valign=top style='width:361.35pt;border-top:none;border-left: none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt; padding:0cm 2pt 0cm 2pt'>
<p>Model does not encounter thermal conduction and convection in the lamp.</p>
</td>
</tr>
<tr>
<td width=235 valign=top style='width:176.4pt;border:solid windowtext 1.0pt; border-top:none;padding:0cm 2pt 0cm 2pt'>
<p>Practical implications (if applicable)</p>
</td>
<td width=482 valign=top style='width:361.35pt;border-top:none;border-left: none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt; padding:0cm 2pt 0cm 2pt'>
<p>Provided function has some degree of dependency on the lamp&#8217;s design (namely, nominal filament temperature)</p>
</td>
</tr>
</table>
<p></p>
<h1 class="Heading1">Definitions</h1>
<p>Process of controlling some output value (such as voltage <i><code>U</code></i> or luminous flux <i><code>L</code></i>) we will denote as a control function <img src="/ta/image001.gif" width=104 height=20 align="absmiddle"/>, defined on the control parameter&nbsp;<img src="/ta/image002.gif" width=76 height=20 align="absmiddle"/>:</p>
<table width="100%" border="0" cellspacing="0" cellpadding="0" class=".TableHidden">
<tr>
<td width="90%" align="center"><img src="/ta/image003.gif"/>&nbsp;</td>
<td width="10%" align="right"><span class=FormulaNum style='font-size:12.0pt'>(1)</span></td>
</tr>
</table>
<p>Each of <img src="/ta/image004.gif" width=29 height=20 align="absmiddle"/>&nbsp;monotonously increase on the defined range. Control functions defined on a different (other than <img src="/ta/image005.gif" width=13 height=16 align="absmiddle"/>) argument we will denote in this article as <img src="/ta/image006.gif" width=28 height=23 align="absmiddle"/>.</p>
<p>Commonly used are linear (<img src="/ta/image007.gif" width=141 height=21 align="absmiddle"/>) and logarithmic (<img src="/ta/image008.gif" width=157 height=21 align="absmiddle"/>) control functions. To implement a desired control function <img src="/ta/image009.gif" width=39 height=23 align="absmiddle"/>, we needs to know what is <img src="/ta/image010.gif" width=45 height=24 align="absmiddle"/>&nbsp;dependency.&nbsp;<br />
The following chapters give an attempt to derive <img src="/ta/image011.gif" width=44 height=24 align="absmiddle"/>&nbsp;using a model suggested in (Agraval 1996).</p>
<p></p>
<h1 class="Heading1">Incandescent Lamp Model</h1>
<p>An incandescent lamp is characterized with few nominal values: <span class=Formula>P<sub>N</sub></span> &#8211; power consumption, <span class=Formula>U<sub>N</sub></span> &#8211; nominal voltage, <span class=Formula>L<sub>N</sub></span> &#8211; nominal luminous flux, produced by the lamp at nominal voltage. The flux is produced by filament, incandesced to nominal working temperature <span class=Formula>T<sub>N</sub></span>.<br />
When lower voltage <span class=Formula>U&lt;U<sub>N</sub></span> is applied, operating temperature <span class=Formula>T</span> of filament is lower <span class=Formula>T<sub>N</sub></span> and therefore it produces less luminosity <span class=Formula>L</span>.</p>
<h2 class="Heading2">Filament Resistance</h2>
<p>Power, consumed by a lamp, is expressed with Ohm low: </p>
<table width="100%" border="0" cellspacing="0" cellpadding="0" class=".TableHidden">
<tr>
<td width="90%" align="center"><img width=168 height=48 src="/ta/image012.gif"/>&nbsp;</td>
<td width="10%" align="right"><a name=e2><span class=FormulaNum style='font-size:12.0pt'>(2)</span></a></td>
</tr>
</table>
<p>where <img src="/ta/image013.gif" width=131 height=24 align="absmiddle"/>&nbsp;defines dependency of filament resistance over the temperature relatively to its nominal resistance <span class=Formula>R<sub>N</sub></span>, which can be evaluated via <span class=Formula>P<sub>N</sub></span> and <span class=Formula>U<sub>N</sub></span>:</p>
<table width="100%" border="0" cellspacing="0" cellpadding="0" class=".TableHidden">
<tr>
<td width="90%" align="center"><img width=64 height=48 src="/ta/image014.gif"/>&nbsp;</td>
<td width="10%" align="right"><span class=FormulaNum style='font-size:12.0pt'>(3)</span></td>
</tr>
</table>
<p>Dependency <img src="/ta/image015.gif" width=31 height=20 align="absmiddle"/>&nbsp;for tungsten is not linear in the working range of temperatures (1000-2500K). For this model we will use polynomial approximation of tungsten resistance <i><span style='font-size:12.0pt;font-family:"Times New Roman"'>&#961;</span></i>, [<a href="#ref_Harang_2003"><span style='color:windowtext; text-decoration:none'>Harang 2003</span></a>]:</p>
<table width="100%" border="0" cellspacing="0" cellpadding="0" class=".TableHidden">
<tr>
<td width="90%" align="center"><img border=0 width=161 height=27 src="/ta/image016.gif"/>&nbsp;</td>
<td width="10%" align="right"><span class=FormulaNum style='font-size:12.0pt'>(4)</span></td>
</tr>
</table>
<p class=FormulaP><img src="/ta/image015.gif" width=31 height=20 border=0 align="absmiddle"/>&nbsp;can be expressed via <img src="/ta/image017.gif" width=32 height=20 border=0 align="absmiddle"/>&nbsp;as the following:</p>
<table width="100%" border="0" cellspacing="0" cellpadding="0" class=".TableHidden">
<tr>
<td width="90%" align="center"><img border=0 width=92 height=45 src="/ta/image018.gif"/>&nbsp;</td>
<td width="10%" align="right"><span class=FormulaNum style='font-size:12.0pt'>(5)</span></td>
</tr>
</table>
<h2 class="Heading2">Filament Temperature</h2>
<p>During operation, filament radiates electromagnetic energy and dissipates heat via conduction and convection. To estimate radiation, filament is modeled [<a href="#ref_Agraval_1996"><span style='color:windowtext;text-decoration:none'>Agraval 1996</span></a>] as a simple non-ideal blackbody that obeys Plank&#8217;s radiation law:</p>
<table width="100%" border="0" cellspacing="0" cellpadding="0" class=".TableHidden">
<tr>
<td width="90%" align="center"><img border=0 width=261 height=57 src="/ta/image019.gif"/>&nbsp;</td>
<td width="10%" align="right"><span class=FormulaNum style='font-size:12.0pt'>(6)</span></td>
</tr>
</table>
<p>where <img src="/ta/image020.gif" width=72 height=20 border=0 align="absmiddle"/>&nbsp;is power radiated between wavelength&nbsp;<img src="/ta/image021.gif" width=15 height=19 border=0 align="absmiddle"/>&nbsp;and&nbsp;<img src="/ta/image022.gif" width=48 height=19 border=0 align="absmiddle"/>, <img src="/ta/image023.gif" width=49 height=21 border=0 align="absmiddle"/>&nbsp;- tungsten emittance, and <span class=Formula>A</span> is filament area. The total power emitted over all wavelengths is:</p>
<table width="100%" border="0" cellspacing="0" cellpadding="0" class=".TableHidden">
<tr>
<td width="90%" align="center"><img border=0 width=116 height=24 src="/ta/image024.gif"/>&nbsp;</td>
<td width="10%" align="right"><span class=FormulaNum style='font-size:12.0pt'>(7)</span></td>
</tr>
</table>
<p>where <b><i><code>&#963;</code></i></b> is the Stefan-Boltzman constant and <img src="/ta/image025.gif" width=29 height=20 border=0 align="absmiddle"/>&nbsp;is average emittance over all wavelengths, which is approximated as a second order polynomial (Harang 2003):</p>
<table width="100%" border="0" cellspacing="0" cellpadding="0" class=".TableHidden">
<tr>
<td width="90%" align="center"><img border=0 width=160 height=25 src="/ta/image026.gif"/>&nbsp;</td>
<td width="10%" align="right"><a name=e8><span class=FormulaNum style='font-size:12.0pt'>(8)</span></a></td>
</tr>
</table>
<p>In a steady-state operation, power applied to the lamp is in balance with power radiated and dissipated to outside ambient. Dissipation has two constituents &#8211; conduction and convection. Agraval in [<a href="#ref_Agraval_1996"><span style='color:windowtext;text-decoration: none'>Agraval 1996</span></a>] has suggested a reasonable way of accounting dissipation as a factor of input power:</p>
<table width="100%" border="0" cellspacing="0" cellpadding="0" class=".TableHidden">
<tr>
<td width="90%" align="center"><img border=0 width=95 height=21 src="/ta/image027.gif"/>&nbsp;</td>
<td width="10%" align="right"><span class=FormulaNum style='font-size:12.0pt'>(9)</span></td>
</tr>
</table>
<p>Solving&nbsp;<span class=FormulaRef>(8)&nbsp;</span>for <span class=Formula>P</span>:</p>
<table width="100%" border="0" cellspacing="0" cellpadding="0" class=".TableHidden">
<tr>
<td width="90%" align="center"><img border=0 width=105 height=44 src="/ta/image028.gif"/>&nbsp;</td>
<td width="10%" align="right"><a name=e10><span class=FormulaNum style='font-size:12.0pt'>(10)</span></a></td>
</tr>
</table>
<p>Substituting left side of <span class=FormulaRef></span><span class=FormulaNum style='font-size:12.0pt'>(10)</span>&nbsp;with right side of <span class=FormulaRef></span><span class=FormulaNum style='font-size:12.0pt'>(2)</span>:</p>
<table width="100%" border="0" cellspacing="0" cellpadding="0" class=".TableHidden">
<tr>
<td width="90%" align="center"><img border=0 width=321 height=48 src="/ta/image029.gif"/>&nbsp;</td>
<td width="10%" align="right"><a name=e11><span class=FormulaNum style='font-size:12.0pt'>(11)</span></a></td>
</tr>
</table>
<p>Using <span class=FormulaNum style='font-size:12.0pt'>(11)</span>, we may derive <img src="/ta/image030.gif" width=39 height=25 border=0 align="absmiddle"/>:</p>
<table width="100%" border="0" cellspacing="0" cellpadding="0" class=".TableHidden">
<tr>
<td width="90%" align="center"><img border=0 width=192 height=55 src="/ta/image031.gif"/>&nbsp;</td>
<td width="10%" align="right"><span class=FormulaNum style='font-size:12.0pt'>(12)</span></td>
</tr>
</table>
<p class=MsoNormal style='page-break-after:avoid'>Figure 1 illustrates dependency <img src="/ta/image032.gif" width=39 height=25 border=0 align="absmiddle"/></p>
<p class=MsoNormal align=center style='margin-top:12.0pt;text-align:center; page-break-after:avoid'><img border=0 width=366 height=336 src="/ta/image033.gif"/></p>
<p class=MsoCaption align=center style='text-align:center'><a name="_Ref164682915">Figure 1</a>. Dependency of <span class=Formula></span><span style='font-weight:normal;font-style:normal'>T</span> on fraction of applied voltage <span class=Formula></span><span style='font-weight:normal;font-style:normal'>&#958;<sub>U</sub></span> for different <span class=Formula></span><span style='font-weight:normal;font-style:normal'>T<sub>N</sub></span></p>
<h2 class="Heading2">Luminance</h2>
<p class=MsoNormal style='margin-top:12.0pt'>Not all power radiated by filament has its effect on luminous flux. Some of the energy is absorbed by bulb glass and dissipated as heat. Although, this absorption depends on <span class=Formula>&#955;</span>, we will simplify this absorption to a constant factor <span style='font-family:"Times New Roman"'>&#951;&lt;1</span>.</p>
<p>Major part of the emitted energy is not visible to human eye. This is described as spectral efficiency function <span class=Formula>S(&#955;)</span>, approximated as the following [<a href="#ref_Agraval_1996"><span style='color:windowtext;text-decoration:none'>Agraval 1996</span></a>]:</p>
<table width="100%" border="0" cellspacing="0" cellpadding="0" class=".TableHidden">
<tr>
<td width="90%" align="center"><img border=0 width=172 height=27 src="/ta/image034.gif"/>&nbsp;</td>
<td width="10%" align="right"><span class=FormulaNum style='font-size:12.0pt'>(13)</span></td>
</tr>
</table>
<p class=MsoNormal style='margin-top:12.0pt;page-break-after:avoid'>Thus, total luminous flux produced by a lamp can be defined as:</p>
<table width="100%" border="0" cellspacing="0" cellpadding="0" class=".TableHidden">
<tr>
<td width="90%" align="center"><img border=0 width=444 height=60 src="/ta/image035.gif"/>&nbsp;</td>
<td width="10%" align="right"><span class=FormulaNum style='font-size:12.0pt'>(14)</span></td>
</tr>
</table>
<p>where</p>
<table width="100%" border="0" cellspacing="0" cellpadding="0" class=".TableHidden">
<tr>
<td width="90%" align="center"><img border=0 width=161 height=60 src="/ta/image036.gif"/>&nbsp;</td>
<td width="10%" align="right"><span class=FormulaNum style='font-size:12.0pt'>(15)</span></td>
</tr>
</table>
<p>Thereby, control function <img src="/ta/image037.gif" width=19 height=23 border=0 align="absmiddle"/>&nbsp;can be expressed as a function of <span class=Formula>T</span>:</p>
<table width="100%" border="0" cellspacing="0" cellpadding="0" class=".TableHidden">
<tr>
<td width="90%" align="center"><img border=0 width=149 height=45 src="/ta/image038.gif"/>&nbsp;</td>
<td width="10%" align="right"><a name=e15><span class=FormulaNum style='font-size:12.0pt'>(16)</span></a></td>
</tr>
</table>
<p>For working range of temperatures and visible area, emissivity <span class=Formula>&#1108;(&#955;,T)</span> can be approximated as the following [<a href="#ref_Larrabee_1957"><span style='color:windowtext;text-decoration:none'>Larrabee&nbsp;1957</span></a>]:</p>
<table width="100%" border="0" cellspacing="0" cellpadding="0" class=".TableHidden">
<tr>
<td width="90%" align="center"><img border=0 width=445 height=66 src="/ta/image039.gif"/>&nbsp;</td>
<td width="10%" align="right"><a name=e16><span class=FormulaNum style='font-size:12.0pt'>(17)</span></a></td>
</tr>
</table>
<p class=MsoNormal style='page-break-after:avoid'>where <span class=Formula>T</span> is in &deg;K and <span class=Formula>&#955;</span> in nm. Substituting <span class=FormulaRef>(17)</span> in <span class=FormulaRef>(16)</span> and integrating numerically <span class=FormulaRef>(16)</span> we get dependency, shown on Figure 2:</p>
<p class=MsoNormal align=center style='margin-top:12.0pt;text-align:center;page-break-after:avoid'><img border=0 width=366 height=336 src="/ta/image040.gif"/></p>
<p class=MsoCaption align=center style='text-align:center'><a name="_Ref164327531">Figure 2</a>. Dependency <span class=Formula></span><span style='font-weight:normal;font-style:normal'>&#958;<sub>L</sub></span><span class=Formula></span><span style='font-weight:normal'>(</span><span class=Formula></span><span style='font-weight:normal;font-style:normal'>T</span><span class=Formula></span><span style='font-weight:normal'>)</span> for different <span class=Formula></span><span style='font-weight:normal'>T<sub>N</sub></span></p>
<p></p>
<h1 class="Heading1">Solving Luminance vs. Voltage</h1>
<p class=MsoNormal style='page-break-after:avoid'>Using <span class=Formula>T</span> as a parametric variable, we may numerically compute <span class=FormulaRef>(11)</span>, <span class=FormulaRef>(15)</span> and graphically solve dependency <span class=Formula>&#958;<sub>L</sub>(&#958;<sub>U</sub>)</span>, as shown on Figure 3:</p>
<p class=MsoNormal align=center style='margin-top:12.0pt;text-align:center; page-break-after:avoid'><img border=0 width=366 height=335 src="/ta/image041.gif"/></p>
<p class=MsoCaption align=center style='text-align:center'><a name="_Ref164328881">Figure </a>3. Dependency <span class=Formula></span><span style='font-weight:normal;font-style:normal'>&#958;<sub>L</sub></span><span class=Formula></span><span style='font-weight:normal'>(</span><span class=Formula></span><span style='font-weight:normal;font-style:normal'>&#958;<sub>U</sub></span><span class=Formula></span><span style='font-weight:normal'>) </span>for different <span class=Formula></span><span style='font-weight:normal;font-style:normal'>T<sub>N</sub></span></p>
<p>As Figure 3 indicates, <span class=Formula></span><span style='font-style:normal'>&#958;<sub>L</sub></span>(<span class=Formula></span><span style='font-style:normal'>&#958;<sub>U</sub></span>) is affected by a design factor &#8211; nominal temperature <span class=Formula>T<sub>N</sub></span>.</p>
<p></p>
<h1 class="Heading1">Approximation</h1>
<p class=MsoCaption align=center style='text-align:center;page-break-after: avoid'><img border=0 width=366 height=345 src="/ta/image042.gif"/></p>
<p class=MsoCaption align=center style='text-align:center'><a name="_Ref164331865">Figure </a>4. Dependency <span class=Formula></span><span style='font-weight:normal'>&#958;<sub>L</sub>(&#958;<sub>U</sub><sup>3</sup>)</span></p>
<p>As it appears (see Figure 4), dependency <span class=Formula>&#958;<sub>L</sub></span>(<span class=Formula>&#958;<sub>U</sub></span><span class=Formula><sup>3</sup>) </span>looks quite linear for <span class=Formula>&#958;<sub>U</sub></span><span class=Formula><sup>3</sup>&gt;0.2</span> and has some higher-order dependency for <span class=Formula>&#958;<sub>U</sub></span><span class=Formula><sup>3</sup>&lt;0.2</span>. Therefore we will try approximating <span class=Formula>&#958;<sub>L</sub></span>(<span class=Formula>&#958;<sub>U</sub></span>) as two polynoms:</p>
<table width="100%" border="0" cellspacing="0" cellpadding="0" class=".TableHidden">
<tr>
<td width="90%" align="center"><img border=0 width=160 height=51 src="/ta/image043.gif"/>&nbsp;</td>
<td width="10%" align="right"><span class=FormulaNum style='font-size:12.0pt'>(18)</span></td>
</tr>
</table>
<p>Value <span class=Formula></span><span style='font-style:normal'>&#958;<sub>L</sub></span>at <code>x=1</code> is equal to <code>1</code> by nature of <span class=Formula></span><span style='font-style:normal'>&#958;</span>, therefore <code>c</code>&nbsp;can be expressed via <code>k</code>:</p>
<table width="100%" border="0" cellspacing="0" cellpadding="0" class=".TableHidden">
<tr>
<td width="90%" align="center"><img border=0 width=205 height=25 src="/ta/image044.gif"/>&nbsp;</td>
<td width="10%" align="right"><span class=FormulaNum style='font-size:12.0pt'>(19)</span></td>
</tr>
</table>
<p>To solve <code>a</code> and <code>x</code><sub>0</sub>&nbsp;we will require continuality of <img src="/ta/image045.gif" width=17 height=23 border=0 align="absmiddle"/>&nbsp;and its derived function <img src="/ta/image046.gif" width=17 height=23 border=0 align="absmiddle"/>, this gives us two equations with three unknowns:</p>
<table width="100%" border="0" cellspacing="0" cellpadding="0" class=".TableHidden">
<tr>
<td width="90%" align="center"><img border=0 width=115 height=53 src="/ta/image047.gif"/>&nbsp;</td>
<td width="10%" align="right"><a name=e19><span class=FormulaNum style='font-size:12.0pt'>(20)</span></a></td>
</tr>
</table>
<p class=MsoNormal style='margin-top:12.0pt;page-break-after:avoid'>Solving <span class=FormulaRef>(20)</span> for <code>a</code>&nbsp;and <code>x</code><sub>0</sub> we get them expressed via <code>k</code>:</p>
<table width="100%" border="0" cellspacing="0" cellpadding="0" class=".TableHidden">
<tr>
<td width="90%" align="center"><img border=0 width=123 height=99 src="/ta/image048.gif"/>&nbsp;</td>
<td width="10%" align="right"><span class=FormulaNum style='font-size:12.0pt'>(21)</span></td>
</tr>
</table>
<p>Thereby, <sub><img src="/ta/image045.gif" width=17 height=23 border=0 align="absmiddle"/></sub>&nbsp;depends on a single constant <span class=Formula>k</span>, which is then wiggled around to find a value giving lowest RMS deviation <img src="/ta/image049.gif" width=99 height=25 border=0 align="absmiddle"/>. Table 1 lists selected values for few most common nominal temperatures <span class=Formula>T<sub>N</sub></span>, Figure 5 illustrates this dependency, and Figure 6 shows modeled curves and values, approximated with <span class=FormulaRef>(20)</span>.</p>
<p class=MsoCaption align=center style='text-align:center;page-break-after: avoid'><a name="_Ref164336487">Table 1</a>. <span class=Formula>k</span> values for some <span class=Formula></span><span style='font-weight:normal'>T<sub>N</sub></span></p>
<div align=center>
<table border=1 cellspacing=0 cellpadding=0 style='border-collapse:collapse;border:none;padding:1pt 1.5pt 1pt 1.5pt'>
<tr style='height:4.0pt'>
<td width=50 valign=top style='width:37.5pt;border:solid windowtext 1.0pt; padding:1pt 1.5pt 1pt 1.5pt;height:4.0pt'>
<p class=MsoNormal align=center style='text-align:center;text-autospace:none'><span style='font-family:"Courier New";color:black'>T<sub>N</sub></span></p>
</td>
<td width=60 valign=top style='width:45.0pt;border:solid windowtext 1.0pt; border-left:none;padding:1pt 1.5pt 1pt 1.5pt;height:4.0pt'>
<p class=MsoNormal align=center style='text-align:center;text-autospace:none'><span style='font-family:"Courier New";color:black'>k</span></p>
</td>
</tr>
<tr style='height:4.0pt'>
<td width=50 valign=top style='width:37.5pt;border:solid windowtext 1.0pt; border-top:none;padding:1pt 1.5pt 1pt 1.5pt;height:4.0pt'>
<p class=MsoNormal align=right style='text-align:right;text-autospace:none'><span style='color:black'>2800</span></p>
</td>
<td width=60 valign=top style='width:45.0pt;border-top:none;border-left:none; border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt; padding:1pt 1.5pt 1pt 1.5pt;height:4.0pt'>
<p class=MsoNormal align=right style='text-align:right;text-autospace:none'><span style='color:black'>1.067</span></p>
</td>
</tr>
<tr style='height:3.6pt'>
<td width=50 valign=top style='width:37.5pt;border:solid windowtext 1.0pt; border-top:none;padding:1pt 1.5pt 1pt 1.5pt;height:3.6pt'>
<p class=MsoNormal align=right style='text-align:right;text-autospace:none'><span style='color:black'>2900</span></p>
</td>
<td width=60 valign=top style='width:45.0pt;border-top:none;border-left:none; border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt; padding:1pt 1.5pt 1pt 1.5pt;height:3.6pt'>
<p class=MsoNormal align=right style='text-align:right;text-autospace:none'><span style='color:black'>1.054</span></p>
</td>
</tr>
<tr style='height:3.4pt'>
<td width=50 valign=top style='width:37.5pt;border:solid windowtext 1.0pt; border-top:none;padding:1pt 1.5pt 1pt 1.5pt;height:3.4pt'>
<p class=MsoNormal align=right style='text-align:right;text-autospace:none'><span style='color:black'>3000</span></p>
</td>
<td width=60 valign=top style='width:45.0pt;border-top:none;border-left:none; border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt; padding:1pt 1.5pt 1pt 1.5pt;height:3.4pt'>
<p class=MsoNormal align=right style='text-align:right;text-autospace:none'><span style='color:black'>1.043</span></p>
</td>
</tr>
<tr style='height:3.4pt'>
<td width=50 valign=top style='width:37.5pt;border:solid windowtext 1.0pt; border-top:none;padding:1pt 1.5pt 1pt 1.5pt;height:3.4pt'>
<p class=MsoNormal align=right style='text-align:right;text-autospace:none'><span style='color:black'>3100</span></p>
</td>
<td width=60 valign=top style='width:45.0pt;border-top:none;border-left:none; border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt; padding:1pt 1.5pt 1pt 1.5pt;height:3.4pt'>
<p class=MsoNormal align=right style='text-align:right;text-autospace:none'><span style='color:black'>1.034</span></p>
</td>
</tr>
</table>
</div>
<p class=MsoNormal align=center style='text-align:center;page-break-after:avoid'><img border=0 width=233 height=218 src="/ta/image050.gif"/></p>
<p class=MsoCaption align=center style='text-align:center'><a name="_Ref236117574">Figure 5</a>. <span style='font-family: "Courier New";font-weight:normal'>k</span> variations over <span class=Formula></span><span style='font-weight:normal'>T<sub>N</sub></span></p>
<p class=MsoNormal align=center style='text-align:center;page-break-after:avoid'><img border=0 width=366 height=304 src="/ta/image051.gif"/></p>
<p class=MsoCaption align=center style='text-align:center'><a name="_Ref164337059">Figure 6</a>. Modeled curves and approximated values (marks)</p>
<p>Since designer of a dimmer may not exactly know nominal temperature of the dimmed lamp, <code>k</code>can be selected for an average nominal temperature. Suggested value <code>k=1.05</code> gives &plusmn;2% error (against the model) for <span class=Formula>T<sub>N</sub></span> in range 2800-3100 &deg;K. For this <code>k</code>, equation <span class=FormulaRef>(20)</span> becomes practically concrete:</p>
<table width="100%" border="0" cellspacing="0" cellpadding="0" class=".TableHidden">
<tr>
<td width="90%" align="center"><img border=0 width=257 height=51 src="/ta/image052.gif"/>&nbsp;</td>
<td width="10%" align="right"><a name=e21><span class=FormulaNum style='font-size:12.0pt'>(22)</span></a></td>
</tr>
</table>
<p></p>
<h1 class="Heading1">Sine Wave Dimmer</h1>
<p>A sine wave dimmer implemented with pulse-width modulation controls applied voltage with a cycle duty, which we will denote as control function <span class=Formula></span><span style='font-style:normal'>&#958;<sub>t</sub></span>. Modulation frequency is usually chosen much higher than mains frequency. Therefore, RMS of output voltage is proportional to cycle duty, and control function <span class=Formula></span><span style='font-style:normal'>&#958;<sub>U</sub></span> as simple as:</p>
<table width="100%" border="0" cellspacing="0" cellpadding="0" class=".TableHidden">
<tr>
<td width="90%" align="center"><img border=0 width=123 height=23 src="/ta/image053.gif"/>&nbsp;</td>
<td width="10%" align="right"><a name=e22><span class=FormulaNum style='font-size:12.0pt'>(23)</span></a></td>
</tr>
</table>
<p class=MsoNormal style='margin-top:12.0pt'>To make <span class=Formula></span><span style='font-style:normal'>&#958;<sub>L</sub>(p)</span>, linear on parameter <span class=Formula></span><span style='font-style:normal'>p</span>, function <span class=Formula></span><span style='font-style:normal'>&#958;<sub>t</sub>(p)</span><span class=Formula></span><span style='font-family:"Trebuchet MS"'> should be defined as the following:</span></p>
<table width="100%" border="0" cellspacing="0" cellpadding="0" class=".TableHidden">
<tr>
<td width="90%" align="center"><img border=0 width=501 height=44 src="/ta/image054.gif"/>&nbsp;</td>
<td width="10%" align="right"><a name="e23_1"><span class=FormulaNum style='font-size:12.0pt'>(24)</span></a></td>
</tr>
</table>
<p>Applying <span class=FormulaNum style='font-size:12.0pt'>(24)</span> to equation <span class=FormulaNum style='font-size:12.0pt'>(22)</span> we get </p>
<table width="100%" border="0" cellspacing="0" cellpadding="0" class=".TableHidden">
<tr>
<td width="90%" align="center"><img border=0 width=259 height=53 src="/ta/image055.gif"/>&nbsp;</td>
<td width="10%" align="right"><span class=FormulaNum style='font-size:12.0pt'>(25)</span></td>
</tr>
</table>
<p></p>
<h1 class="Heading1">Phase Control Dimmer</h1>
<p>When implementing an AC dimmer based on phase control technique, one needs to tabulate cycle duty values <code>t<sub>i</sub></code> as a function of desired luminance level <code>p<sub>i</sub></code>.&nbsp;<br />
Following the same approach, we introduce control function <span class=Formula></span><span style='font-style:normal'>&#958;<sub>t</sub></span>:</p>
<table width="100%" border="0" cellspacing="0" cellpadding="0" class=".TableHidden">
<tr>
<td width="90%" align="center"><img border=0 width=117 height=45 src="/ta/image056.gif"/>&nbsp;</td>
<td width="10%" align="right"><span class=FormulaNum style='font-size:12.0pt'>(26)</span></td>
</tr>
</table>
<p>where <i><code>t</code></i> is cycle duty (time when the switch is on) and <i><code>t<sub>M</sub></code></i> is the mains half period, and <i><code>f</code></i>&nbsp;is the mains frequency. Average power <span class=Formula><sub><img border=0 width=15 height=16 src="/ta/image057.gif"/></sub></span>&nbsp;applied to the lamp, can be evaluated as the following:</p>
<table width="100%" border="0" cellspacing="0" cellpadding="0" class=".TableHidden">
<tr>
<td width="90%" align="center"><img border=0 width=360 height=51 src="/ta/image058.gif"/>&nbsp;</td>
<td width="10%" align="right"><a name=e23><span class=FormulaNum style='font-size:12.0pt'>(27)</span></a></td>
</tr>
</table>
<p>where <i><code>U<sub>A</sub></code></i> is voltage amplitude. Integrating <span class=FormulaRef>(27)</span> we can define <span class=Formula></span><span style='font-style:normal'>&#958;<sub>P</sub></span> as the following</p>
<table width="100%" border="0" cellspacing="0" cellpadding="0" class=".TableHidden">
<tr>
<td width="90%" align="center"><img border=0 width=321 height=48 src="/ta/image059.gif"/>&nbsp;</td>
<td width="10%" align="right"><a name=e24><span class=FormulaNum style='font-size:12.0pt'>(28)</span></a></td>
</tr>
</table>
<p class=MsoNormal style='margin-top:12.0pt'>Considering that <span class=Formula></span><span style='font-style:normal'><img src="/ta/image060.gif" width=48 height=25 border=0 align="absmiddle"/></span>, and substituting <span class=FormulaRef>(28)</span> to <span class=FormulaRef>(22)</span> we may estimate <img src="/ta/image061.gif" width=40 height=24 border=0 align="absmiddle"/>. </p>
<p class=MsoNormal align=center style='text-align:center;page-break-after:avoid'><img border=0 width=438 height=381 src="/ta/image062.gif"/></p>
<p class=MsoCaption align=center style='text-align:center'>Figure 7. Dimmer control functions <span class=Formula></span><span style='font-weight:normal'>&#958;<sub>L</sub></span> and <span class=Formula></span><span style='font-weight:normal'>&#958;<sub>U</sub></span></p>
<p>Applying approach used in <span class=FormulaRef>(24)</span> function <span class=Formula></span><span style='font-style:normal'>&#958;<sub>t</sub>(p)</span> is defined as following:</p>
<table width="100%" border="0" cellspacing="0" cellpadding="0" class=".TableHidden">
<tr>
<td width="90%" align="center"><img border=0 width=291 height=25 src="/ta/image063.gif"/>&nbsp;</td>
<td width="10%" align="right"><span class=FormulaNum style='font-size:12.0pt'>(29)</span></td>
</tr>
</table>
<p>Although, reverse function of <span class=FormulaRef>(28)</span> is not solvable analytically, it can be solved numerically.</p>
<p></p>
<h1 class="Heading1">Conclusions</h1>
<p>As a result of numerical modeling, the following approximation functions are suggested:</p>
<ul>
<li>Estimation of produced luminous flux over the voltage:</li>
</ul>
<p style='text-indent:36.0pt'><code>L = L0 * ((U/U0 &lt; 0.575) ? 1.369 * (U/U0) ^ 4 : (1.05 * (U/U0) ^ 3 - 0.05);</code></p>
<ul>
<li>Tabulation of cycle duty over the control parameter p</li>
</ul>
<p style='text-indent:36.0pt'><code>t = (p &lt; 0.1496) ? (0.925 * (p) ^ -4) : (0.984 * (p + 0.05) ^ -3);</code></p>
<p></p>
<h1 class="Heading1">References</h1>
<p class=MsoNormal align=left style='margin-left:28.0pt;text-align:left; text-indent:-28.0pt'><a name="ref_Agraval_1996"></a>Agraval D.C., Leff H.S., Monon V.J. (1996)<br />
&quot;Efficiency and efficacy of incandescent lamps&quot;<br />
<i>American Journal of Physics</i>, May 1996, Volume 64, Issue 5, pp. 649-654<br />
<a href="http://scitation.aip.org/getabs/servlet/GetabsServlet?prog=normal&amp;id=AJPIAS000064000005000649000001&amp;idtype=cvips&amp;gifs=yes">http://scitation.aip.org/getabs/servlet/GetabsServlet?prog=normal&amp;id=AJPIAS000064000005000649000001&amp;idtype=cvips&amp;gifs=yes</a></p>
<p class=MsoNormal align=left style='margin-left:28.0pt;text-align:left; text-indent:-28.0pt'><a name="ref_Harang_2003"></a>Harang O., Kosch M. J. (2003) &quot;Absolute Optical Calibration Using a Simple Tungsten<br />
Bulb&quot;<br />
<i>Sodankyl&auml; Geophysical Observatory Publications</i>, 2003, pp. 92:121-123<br />
<a href="http://spaceweb.oulu.fi/28AM/proc_papers/27_harang_et_al_calibration_with_tungsten_bulb.pdf">http://spaceweb.oulu.fi/28AM/proc_papers/27_harang_et_al_calibration_with_tungsten_bulb.pdf</a></p>
<p class=MsoNormal align=left style='margin-left:28.0pt;text-align:left; text-indent:-28.0pt'><a name="ref_Larrabee_1957"></a>Larrabee R.D. (1957) &quot;The spectral emissivity and optical properties of tungsten&quot;<br /> <br />
  Research Laboratory Of Electronics, Massachusetts Institute of Technology <br />
<a href="http://dspace.mit.edu/bitstream/1721.1/4755/1/RLE-TR-328-04734719.pdf">http://dspace.mit.edu/bitstream/1721.1/4755/1/RLE-TR-328-04734719.pdf</a></p>
]]></content:encoded>
			<wfw:commentRss>http://hutorny.in.ua/research/theoretical-aspects-of-dimming-an-incandescent-lamp/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Customizing ErWin templates for PostgreSQL</title>
		<link>http://hutorny.in.ua/misc/customizing-erwin-templates-for-postgresql</link>
		<comments>http://hutorny.in.ua/misc/customizing-erwin-templates-for-postgresql#comments</comments>
		<pubDate>Wed, 22 Jul 2009 16:20:06 +0000</pubDate>
		<dc:creator>Eugene</dc:creator>
				<category><![CDATA[Misc]]></category>

		<guid isPermaLink="false">http://hutorny.in.ua/misc/customizing-erwin-templates-for-postgresql</guid>
		<description><![CDATA[ErWin 7.2 does not provide support for PostgreSQL, indeed with few tricks it still can be used to model database and generate valid SQL code. (Note: In ErWin 7.3 templates look very different from 7.2, but nevertheless, I believe, similar approach still can be applied). Trick 1. Use Oracle 10.x as the target DBMS PostgreSQL [...]]]></description>
			<content:encoded><![CDATA[<p>ErWin 7.2 does not provide  support for PostgreSQL, indeed with few tricks it still can be used to model  database and generate valid SQL code.</p>
<p><span id="more-99"></span>(Note: <em>In ErWin 7.3 templates  look very different from 7.2, but nevertheless, I believe, similar approach  still can be applied</em>).</p>
<p><strong>Trick 1. Use Oracle 10.x as the target DBMS</strong></p>
<p>PostgreSQL has common  ideology with Oracle and, in my opinion, is the best choice for PostgreSQL needs.</p>
<p><strong>Trick 2. Fix syntax differences</strong><br />
I have encoundered two syntax  differences &#8211; in the order designator in the column specifier of <code>CREATE INDEX</code> statement and extra parenthesis in <code>ALTER TABLE ADD CONSTRAINT</code></p>
<p>Following samples illustrate what causes PostgreSQL to fail with generated code:</p>
<p><code>CREATE INDEX Publisher_xIF1 ON Publisher (PublisherID <strong><del>ASC</del></strong>);</code></p>
<p><code>ALTER TABLE Publisher<br />
ADD <strong><del>(</del></strong>CONSTRAINT Publisher_fObject FOREIGN KEY (ObjectID) REFERENCES Object(ObjectID) ON DELETE CASCADE<strong><del>)</del></strong>;</code></p>
<p>This can be fixed by altering ErWin FE template as the following:</p>
<p>2.1. Copy and rename <code>Oracle.erwin_fe_template</code> to <code>Postgre.erwin_fe_template</code> and then open it in a text editor.</p>
<table width="100%" cellspacing="0" cellpadding="0" border="0">
<tr>
<td>
<p>2.2. Find<br />
		<code>SPItemBegin = KeyGroupMembers</code></p>
<p>2.3. Below that find and remove line containing <code>[" " PropertyValueX("Key Group Sort  Order")]</code></p>
</td>
<td valign="top" style="width: 130px"><a title="Key Group Sort Order" class="imagelink" href="http://hutorny.in.ua/wp-content/uploads/2009/07/postgress_fe1.PNG" rel="thumbnail"><img alt="Key Group Sort Order" id="image100" src="http://hutorny.in.ua/wp-content/uploads/2009/07/postgress_fe1.thumbnail.PNG" /></a></td>
</tr>
<tr>
<td>
<p>2.4. Find<code>"ADD "  "("  IsNotNullX(ExecuteX("FKConstraint"),"") ")"</code></p>
<p>2.5. Remove <code>"("  ")"</code>, so it looks like the following:<code>"ADD "  IsNotNullX(ExecuteX("FKConstraint"),"")</code></p>
<p>2.6. Switch you project to  use this template<br />
(Tools>Forward  Engineer>Scema Generation; Database template &#8211; Browse; select  Postgre.erwin_fe_template)</p>
<p>These two tricks are  essential to produce valid SQL code from an ErWin model that contain  no Oracle specific features, like table partitions, logging, validation, etc. If you already have an Oracle model which you prefer to keep intact and still be able to generate SQL for Postgress, you will need to void all parts of FE template responsible for Oracle specific features.</p>
</td>
<td valign="top"><a class="imagelink" title="Add constraint" href="http://hutorny.in.ua/wp-content/uploads/2009/07/postgress_fe2.PNG" rel="thumbnail"><img id="image101" alt="Add constraint" src="http://hutorny.in.ua/wp-content/uploads/2009/07/postgress_fe2.thumbnail.PNG" /></a></td>
</tr>
<tr>
<td>
<p><strong>Trick 3. Implement use of <code>INHERITS</code> for subtype relationships</strong><br />
This part is optional and  necessary if you really need to use table inheritance.</p>
<p>3.1. Find first occurrence of <code>[ExecuteX("EndOfStatementX")]</code> after <code>SPItemBegin = Create Entity</code></p>
</td>
<td></td>
</tr>
<tr>
<td>
<p>3.2. Add <code>[ExecuteX("Extra  Properties")]</code> before <code>[ExecuteX("EndOfStatementX")]</code></p>
<p><code>[ExecuteX("Table  Properties")]<br />
<ins>[ExecuteX("Extra Properties")]</ins><br />
[ExecuteX("EndOfStatementX")]<br />
</code></p>
</td>
<td valign="top"><a class="imagelink" title="Extra Properties" href="http://hutorny.in.ua/wp-content/uploads/2009/07/postgress_fe3.PNG" rel="thumbnail"><img id="image102" alt="Extra Properties" src="http://hutorny.in.ua/wp-content/uploads/2009/07/postgress_fe3.thumbnail.PNG" /></a></td>
</tr>
<tr>
<td>
<p>3.3. At the end of the file add</p>
</td>
<td></td>
</tr>
<tr>
<td>
<pre>SPItemBegin = Extra Properties
  10000:
  {#
  ForEachVectorReferenceX("Child Relations Ref")
  {
    [ IsPropertyEqual("Type","9")
        " INHERITS("

          [
              PushReferenceX("Parent Entity Ref")
              [ [OwnerX"."]PhysicalNameX ]
              PopX
          ]
       ")"
     ]
   }
   #}
   SPItemEnd
</pre>
</td>
<td valign="top"><a class="imagelink" title="Extra Properties Implemetation" href="http://hutorny.in.ua/wp-content/uploads/2009/07/postgress_fe4.PNG" rel="thumbnail"><img id="image103" alt="Extra Properties Implemetation" src="http://hutorny.in.ua/wp-content/uploads/2009/07/postgress_fe4.thumbnail.PNG" /></a></td>
</tr>
</table>
]]></content:encoded>
			<wfw:commentRss>http://hutorny.in.ua/misc/customizing-erwin-templates-for-postgresql/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Simplescope v1.1 released</title>
		<link>http://hutorny.in.ua/projects/w32/sisan/simplescope-v11-released</link>
		<comments>http://hutorny.in.ua/projects/w32/sisan/simplescope-v11-released#comments</comments>
		<pubDate>Wed, 14 Mar 2007 09:08:09 +0000</pubDate>
		<dc:creator>Eugene</dc:creator>
				<category><![CDATA[SISAN]]></category>

		<guid isPermaLink="false">http://hutorny.in.ua/projects/w32/sisan/simplescope-v11-released</guid>
		<description><![CDATA[This is a maintenance release of the product that includes the following fixes: 1. SISAN v 1.1 &#160;&#160;1.1.&#160;Added SISAM88 hardware schematic &#160;&#160;1.2.&#160;Fixed TX/RX typo on SISAM690 schematic &#160;&#160;1.3.&#160;Fixed issue with beam pattern affecting ruler pattern 2. SISAM1 v 1.1 &#160;&#160;2.1.&#160;Fixed diagnostic failure, which made product unusable if high baudrate cannot be set &#160;&#160;2.2.&#160;Fixed issue with [...]]]></description>
			<content:encoded><![CDATA[<p>This is a maintenance release of the product that includes the following fixes:<br />
<strong>1. SISAN v 1.1</strong><br />
&nbsp;&nbsp;1.1.&nbsp;Added SISAM88 hardware schematic<br />
&nbsp;&nbsp;1.2.&nbsp;Fixed TX/RX typo on SISAM690 schematic<br />
&nbsp;&nbsp;1.3.&nbsp;Fixed issue with beam pattern affecting ruler pattern<br />
<strong>2. SISAM1 v 1.1</strong><br />
&nbsp;&nbsp;2.1.&nbsp;Fixed diagnostic failure, which made product unusable if high baudrate cannot be set<br />
&nbsp;&nbsp;2.2.&nbsp;Fixed issue with BREAK is not handled properly<br />
<a id=p97 href="http://hutorny.in.ua/wp-content/uploads/2007/03/Simplescope.v1.1.zip">Download Simplescope v1.1.</a></p>
<p><span id="more-96"></span><br />
Read more about <a href="http://hutorny.in.ua/projects/pic/sisam/sisam-overview">SISAM &#8211; SImple SAMpler</a> and <a href="http://hutorny.in.ua/projects/w32/sisan-overview">SISAN &#8211; SImlpe Signal Analyse</a></p>
]]></content:encoded>
			<wfw:commentRss>http://hutorny.in.ua/projects/w32/sisan/simplescope-v11-released/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>MIHA675 &#8211; Multi Input Hall Automaton</title>
		<link>http://hutorny.in.ua/projects/pic/miha675-multi-input-hall-automaton</link>
		<comments>http://hutorny.in.ua/projects/pic/miha675-multi-input-hall-automaton#comments</comments>
		<pubDate>Thu, 07 Dec 2006 15:59:25 +0000</pubDate>
		<dc:creator>Eugene</dc:creator>
				<category><![CDATA[PIC]]></category>
		<category><![CDATA[VELOOS]]></category>

		<guid isPermaLink="false">http://hutorny.in.ua/projects/pic/miha675-multi-input-hall-automaton</guid>
		<description><![CDATA[Previous examples illustrated VELOOS programming aspects. This post provides an example of using object oriented paradigm for designing VELOOS application. Unlike the previous examples which were purely programming exercises, this example is dedicated to a utilitarian home appliance application and provides a &#34;reference design&#34; for VELOOS applications. Example3 &#8211; Designing MIHA &#8211; Multi Input Hall [...]]]></description>
			<content:encoded><![CDATA[<p>Previous examples illustrated VELOOS  programming aspects. This post provides an example of using object oriented  paradigm for designing VELOOS application. Unlike the previous examples which  were purely programming exercises, this example is dedicated to a utilitarian  home appliance application and provides a &quot;reference design&quot; for  VELOOS applications.</p>
<p><span id="more-87"></span></p>
<h2>Example3 &ndash; Designing MIHA &ndash;  Multi Input Hall Automaton</h2>
<p></p>
<h3>Introduction</h3>
<p>There are two common approaches for light  switching in halls and staircases &ndash; off-timers and intermediate switching  schemes (please excuse me if terminology is wrong). Off timers turn light on when user pushes a button and turn off when specified time elapsed. Intermediate  switching schemes toggle lights when user toggles any switch (of two). Each  approach has its own advantages and disadvantages, however, both approaches  require special switches, which could be an undesired constraint for interior  design. See, for example, <a href="http://www.o2m8.com/modules.php?name=News&amp;file=article&amp;sid=32">Intermediate lighting switch</a>,&nbsp;<a href="http://www.tlc-direct.co.uk/Manufacturers/Contactum_Distribution/CMD_Controls_3/index.html">Stair case timer</a>,&nbsp;<a href="http://www.amidata.es/componentes-electronicos-es/circuitos-electricos/424356720-STAIRCASE-TIMER.html">Multifunction staircase timer</a></p>
<h3>Requirements (user&#8217;s needs)</h3>
<p>Implement a light-control appliance that:</p>
<ul>
<li>combines off-timer and  intermediate switchers</li>
<li>provides support for at least 3  switches</li>
<li>supports regular light switches  as well as push-buttons</li>
<li>provides settable 0.5-10 min  timer range with a simple user interface for entering value</li>
</ul>
<h3>MIHA Design Description</h3>
<p><u>Key Design Decisions:</u></p>
<ul>
<li>Implement the appliance (named  MIHA) as a VELOOS application running on a PIC microcontroller</li>
<li>Store application  configuration in EEPROM during device programming</li>
<li>Use potentiometer for  specifying timeout</li>
</ul>
<p><u>Architecture</u><br />
  MIHA consists of the following units (see  architecture diagram on Figure 1):</p>
<table border="1" cellspacing="0" cellpadding="0" style="border:solid gray .5pt;border-collapse:collapse;">
<tr>
<td width="196" valign="top" style="border:solid gray .5pt;padding:0cm 4pt 0cm 4pt;">
      Multi-input Switch Debouncer </td>
<td width="463" valign="top" style="border:solid gray .5pt;padding:0cm 4pt 0cm 4pt;">Reads and debounces switches, sends a    message on switch state changing. <br />
      Featuring triple port read with majority    logic and software implementation of RC-integration and Schmidt trigger.</td>
</tr>
<tr>
<td width="196" valign="top" style="border:solid gray .5pt;padding:0cm 4pt 0cm 4pt;">Hall Light Automaton</td>
<td width="463" valign="top" style="border:solid gray .5pt;padding:0cm 4pt 0cm 4pt;">Implements automaton logic, provides    supports for switches/push-buttons, send &#8216;alt&#8217; message to actuator and &#8216;off&#8217;    message to the delay block</td>
</tr>
<tr>
<td width="196" valign="top" style="border:solid gray .5pt;padding:0cm 4pt 0cm 4pt;">Delay Time Input</td>
<td width="463" valign="top" style="border:solid gray .5pt;padding:0cm 4pt 0cm 4pt;">Reads potentiometer value and sends the    value read to the delay block<br />
      Featuring support for multiple channels.    ADC access is arbitrated via time quantization</td>
</tr>
<tr>
<td width="196" valign="top" style="border:solid gray .5pt;padding:0cm 4pt 0cm 4pt;">Message Delay Block</td>
<td width="463" valign="top" style="border:solid gray .5pt;padding:0cm 4pt 0cm 4pt;">Provides delay for the &#8216;off&#8217; message.</td>
</tr>
<tr>
<td width="196" valign="top" style="border:solid gray .5pt;padding:0cm 4pt 0cm 4pt;">Discrete Actuator</td>
<td width="463" valign="top" style="border:solid gray .5pt;padding:0cm 4pt 0cm 4pt;">Turns on or off an output pin depending    on the message received</td>
</tr>
</table>
<table>
<tr>
<td rowspan="2">
<p><u>Mapping Architecture to Implementation.</u><br />
  Mapping from architectural units to  implementation units is straightforward &ndash; all MIHA units are directly mapped to  operating system objects, inter-unit communications are mapped to messaging. <br />
  All MIHA objects are configurable with  EEPROM data. To make design flexible, object configuration embeds acceptor  handle. The latter two decisions establish a generality which is captured as  two abstract classes:<br />
  PDO &ndash; a configurable Process Data Object<br />
  PDD &ndash; a dynamically bound PDO<br />
  (See class diagram on Figure 2)</p>
</td>
<td rowspan="2">&nbsp;</td>
<td height=57><a href="http://hutorny.in.ua/wp-content/uploads/2006/12/MIHA-Architecture.png" target=_blank rel="thumbnail"><img src="http://hutorny.in.ua/wp-content/uploads/2006/12/MIHA-Architecture.thumbnail.png" alt="MIHA Architecture" height=57 border="0" id="MIHAArchitecture"/></a><br />Figure 1.</td>
</tr>
<tr>
<td><a href="http://hutorny.in.ua/wp-content/uploads/2006/12/MIHA-ClassDiagram.png" target=_blank rel="thumbnail"><img src="http://hutorny.in.ua/wp-content/uploads/2006/12/MIHA-ClassDiagram.thumbnail.png" alt="MIHA Class Diagram" name="image89" height=80 border="0" id="image89" /></a><br />Figure 2.</td>
</tr>
</table>
<h3>Moduling</h3>
<p>Major advantage provided by VELOOS is  ability to design and implement loosely coupled objects. The following practice  can be applied to get full advantage of loose coupling: </p>
<ul>
<li>A unit should be made up of (at  least) two files: (1) .inc containing interface and (2) .asm &ndash; implementation</li>
<li>Each unit should implement one  class or a hierarchy of closely related classes.</li>
<li>A unit should not instantiate  objects of reenterable or configurable classes. Instead it should publish class  name in the interface.</li>
<li>A unit may instantiate object  of a non-reenterable class. In this case it should publish object name and  encapsulate class name.</li>
</ul>
<h3>Sharing RAM among objects</h3>
<p>Decoupling objects via messaging mechanism  provides a very explicit and straightforward watershed for scoping variables &ndash; everything  that should survive between two messages is a part of object state and should  be allocated in udata section, everything that may be discarded after handling  the message is a temporary variable and can be allocated in udata_ovr section.</p>
<h3>Using Timer for Resource Access Arbitration</h3>
<p>Class Potentiometer Parameter Input (PPI)  implements time-quantified approach for sharing ADC among several instances &ndash; time  is sliced on N quants, each instance may access hardware only if TIME mod N  equals to channel number. To simplify computing TIME mod N, N can be chosen as  power of 2. In this example N = 16.</p>
<table>
<tr>
<td rowspan="4">
<h3>Multi-input Debouncer</h3>
<p>On timer event Debouncer  polls the port three times in a row and applies majority logic to determine current  switch state. This approach eliminates sporadic spikes than may be present in a  noisy environment. For each assigned pin it maintains a counter. This counter  is incremented whenever pin level is high and decremented if low. Counter is  maintained in range 0..16, e.g. when it reaches the boundary it is not changed.  When counter reaches upper threshold (12) logical output state is changed to 1  and to 0 when it reaches lower threshold. This is a software model of an RC-integration  connected to a Schmidt trigger.<br />Few samples of  switch bouncing are shown on figures on right. The switches are regular light switches  manufactured by VIKO.</p>
<h3>Hardware Schematic</h3>
<p>MIHA schematic is shown on Figure 6. Relay, loaded on PIC12, should have actuating current up to 25 mA. Resistors R1-R4 are used for pull-up and can be roughly 1 kOhm. In an environment with strong EMI their can be reduced to avoid pickup.</p>
<h3>Package Content</h3>
<p>The package includes three projects to build this application for three different devices &ndash; PIC12F675, PIC16F690, PIC18F248. It is licensed to public under the <a href="http://www.opensource.org/licenses/osl-3.0.php">Open Software   License<br />
</a><a id=p94 href="http://hutorny.in.ua/wp-content/uploads/2006/12/miha.v.1.0.zip">Download MIHA Sources v.1.0</a></p>
</td>
<td rowspan="4">&nbsp;</td>
<td><a href="http://hutorny.in.ua/wp-content/uploads/2006/12/ON-Bouncing.png" target="_blank" rel="thumbnail"><img src="http://hutorny.in.ua/wp-content/uploads/2006/12/ON-Bouncing.thumbnail.png" alt=ON-Bouncing.png height=39 border="0" id="image91" /></a><br />Figure 3.</td>
</tr>
<tr>
<td><a href="http://hutorny.in.ua/wp-content/uploads/2006/12/OFF-Bouncing.png" target="_blank" rel="thumbnail"><img src="http://hutorny.in.ua/wp-content/uploads/2006/12/OFF-Bouncing.thumbnail.png" alt=OFF-Bouncing.png height=39 border="0" id="image92" /></a><br />Figure 4.</td>
</tr>
<tr>
<td><a href="http://hutorny.in.ua/wp-content/uploads/2006/12/Debounced.png" target="_blank" rel="thumbnail"><img src="http://hutorny.in.ua/wp-content/uploads/2006/12/Debounced.thumbnail.png" alt=Debounced.png height=47 border="0" id="image93" /></a><br />Figure 5.</td>
</tr>
<tr>
<td><a href="http://hutorny.in.ua/wp-content/uploads/2006/12/MIHA675.png" target="_blank" rel="thumbnail"><img src="http://hutorny.in.ua/wp-content/uploads/2006/12/MIHA675.thumbnail.png" alt="MIHA Hardware Schematics" height=92 border="0" id="image90" /></a><br />Figure 6.</td>
</tr>
</table>
]]></content:encoded>
			<wfw:commentRss>http://hutorny.in.ua/projects/pic/miha675-multi-input-hall-automaton/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>VELOOS Example 3</title>
		<link>http://hutorny.in.ua/projects/pic/veloos/veloos-example-3</link>
		<comments>http://hutorny.in.ua/projects/pic/veloos/veloos-example-3#comments</comments>
		<pubDate>Wed, 06 Dec 2006 21:38:00 +0000</pubDate>
		<dc:creator>Eugene</dc:creator>
				<category><![CDATA[VELOOS]]></category>

		<guid isPermaLink="false">http://hutorny.in.ua/projects/pic/veloos/veloos-example-3</guid>
		<description><![CDATA[This example for PIC12F675 implements set of &#8220;actuators&#8221; and &#8220;generators&#8221; controlled via USART. Actuators produce one 300 ms pulse when it receives command &#8216;ON&#8217; and three 100 ms pulses delimited with 100 ms spaces (pauses) on command &#8216;OFF&#8217;. Generators, when turned on, produce pulses and spaces of the specified duration (1..255). Actuators and Generators are [...]]]></description>
			<content:encoded><![CDATA[<p>This example for PIC12F675 implements set of &#8220;actuators&#8221; and &#8220;generators&#8221; controlled via USART. Actuators produce one 300 ms pulse when it receives command &#8216;ON&#8217; and three 100 ms pulses delimited with 100 ms spaces (pauses) on command &#8216;OFF&#8217;. Generators, when turned on, produce pulses and spaces of the specified duration (1..255). </p>
<p><span id="more-84"></span></p>
<p>Actuators and Generators are implemented as instances of Actuator and Generator classes. Yet another class in this example   protocol driver   parses the incoming via software USART commands and forwards it to a corresponding object. Command format is: <strong>command</strong>[<strong>data</strong>,[<strong>data</strong>]]; COM port settings: 2400, 8bit no parity, 1 stop bits.<br />
USART is implemented in software and uses INT0 and TMR1 interrupts for timing bits.<br />
Command language consist of 8 commands (H..O)</p>
<ul>
<li>H,I,J   control an actuator, </li>
<li>K,L   control a generator, </li>
<li>N   all actuators do OFF, susped all generators</li>
<li>O   all actuators do ON, resume all generators.</li>
</ul>
<p>H,I,J commands accepts &#8217;0&#8242; and &#8217;1&#8242; as a parameter to execute OFF or ON command  respectively. K,L commands should be supplied with pulse and pause duration</p>
<p>For example:</p>
<ul>
<li>H0   Actuator 1 do OFF</li>
<li>LUA   Turn on generator 1 with 85 ms pulse and 65 ms space</li>
</ul>
<p><a id=p86 href="http://hutorny.in.ua/wp-content/uploads/2006/12/Example3.zip">Example 3 Sources</a></p>
<p>Hardware Scematics. Connect loads to GP0,GP1,GP4,GP5 (not shown on the schematic)</p>
<p><img id="image85" alt="Example 3 Hardware Schematics" src="http://hutorny.in.ua/wp-content/uploads/2006/12/Example3.png" /></p>
<p>Simulation Results</p>
<p><img id="image95" alt="Simulation Results" src="http://hutorny.in.ua/wp-content/uploads/2006/12/SIM-Example3.png" /></p>
]]></content:encoded>
			<wfw:commentRss>http://hutorny.in.ua/projects/pic/veloos/veloos-example-3/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

