<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>CloudRules</title>
	<atom:link href="http://gmarabout.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://gmarabout.wordpress.com</link>
	<description>A blog by Grégoire Marabout</description>
	<lastBuildDate>Thu, 23 Sep 2010 07:41:13 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='gmarabout.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>CloudRules</title>
		<link>http://gmarabout.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://gmarabout.wordpress.com/osd.xml" title="CloudRules" />
	<atom:link rel='hub' href='http://gmarabout.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Annotation-based event handling, in Java</title>
		<link>http://gmarabout.wordpress.com/2010/09/23/annotation-based-event-handling-in-java/</link>
		<comments>http://gmarabout.wordpress.com/2010/09/23/annotation-based-event-handling-in-java/#comments</comments>
		<pubDate>Thu, 23 Sep 2010 07:40:37 +0000</pubDate>
		<dc:creator>gmarabout</dc:creator>
				<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://gmarabout.wordpress.com/?p=20</guid>
		<description><![CDATA[For a personal project (a 2D scene graph project), I needed to implement some kind of event handling. The usual Java-way to do it is to provide a listener interface, with the appropriate methods. This looks like : public interface MyEventListener { void handleEvent1(MyEvent event); void handleEvent2(MyEvent event); } When you want to react to&#160;&#8230; <a href="http://gmarabout.wordpress.com/2010/09/23/annotation-based-event-handling-in-java/">Read&#160;more</a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gmarabout.wordpress.com&amp;blog=14216340&amp;post=20&amp;subd=gmarabout&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>For a personal project (a 2D scene graph project), I needed to implement some kind of event handling. The usual Java-way to do it is to provide a listener interface, with the appropriate methods. This looks like :</p>
<pre class="brush: java;">
public interface MyEventListener {

  void handleEvent1(MyEvent event);

  void handleEvent2(MyEvent event);

}
</pre>
<p>When you want to react to an event (a so-called <em>event handler</em>), you would just implement this interface, and add it to the listeners of the object you want to &#8216;monitor&#8217;:</p>
<pre class="brush: java;">
public class MyHandler implements MyEventListener {
    public void handleEvent1(MyEvent event){
      //...
    }

    public void handleEvent2(MyEvent event){
      //...
    }
}
</pre>
<p>This is a well known pattern.</p>
<p>But during  development, I realized I had to add more kinds of events, and thus more kinds of listeners. Each listeners had more and more methods (the same event object could be fired in many situations).<br />
Moreover, my <em>event handlers</em> required to implement many of the listeners, but not all of their methods.<br />
In such situations, you have to implement several interfaces, implement all the methods</p>
<pre class="brush: java;">
public class MyEventHandler implements MyListener1, MyListener2 {
   // implement all the methods...
}
</pre>
<p>and add an instance to <em>all</em> listener lists:</p>
<pre class="brush: java;">
  MyEventHandler handler = new MyEventHandler();

  objectToMonitor.addListener1(handler);
  objectToMonitor.addListener2(handler);
</pre>
<p>So, in the process of reinventing the wheel just for fun, I looked for another way to achieve this.</p>
<p>I finally found that an event handler could be easily implemented using Java annotations.</p>
<p>An event handler would be a regular Java object (no listener interface to implement). An annotation would indicate which method should be called (based on the list of event names, but could be more complicated like regular expressions):</p>
<pre class="brush: java;">
public class MyHandler { // No interface to implement!

   // Process 'event1'.
   @HandleEvent(&quot;event1&quot;)
   public void doSomething(MyEvent event) { ... }

   // Process 'event1' and 'event2' in the same method.
   @HandleEvent({&quot;event1&quot;, &quot;event2&quot;})
   public void doSomethingElse(MyEvent event) { ... }
}
</pre>
<p>The nice part is that the handler object can be added using a single method:</p>
<pre class="brush: java;">
  MyHandler handler = new MyHandler();

  objectToMonitor.addHandler(handler);
</pre>
<p>That way, all addXXXListener() methods are centralized into a single addHandler() entry point.</p>
<p>The class of &#8216;objectToMonitor&#8217; must just be provided with the capability to dispatch (named) events to the annotated method of the handler.</p>
<p>Here is the code for such an <em>event dispatcher</em> :</p>
<pre class="brush: java;">
public class EventDispatcher {
    private Collection handlers = new ArrayList();

    /**
     * Adds an event handler.
     */
    public void addHandler(Object handler) {
        this.handlers.add(handler);
    }

    /**
     * Removes an event handler.
     */
    public void removeHandler(Object handler) {
        this.handlers.remove(handler);
    }

    /**
     * Dispatch an event to the registered handlers.
     */
    public void dispatchEvent(Event event) {
        for (Object handler : handlers) {
            dispatchEventTo(event, handler);

        }
    }

    protected void dispatchEventTo(Event event, Object handler) {
        Collection methods = findMatchingEventHandlerMethods(handler, event.getName());
        for (Method method : methods) {
            try {
                // Make sure the method is accessible (JDK bug ?)
                method.setAccessible(true);

                if (method.getParameterTypes().length == 0)
                    method.invoke(handler);
                if (method.getParameterTypes().length == 1)
                    method.invoke(handler, event);
                if (method.getParameterTypes().length == 2)
                    method.invoke(handler, this, event);
            } catch (Exception e) {
                System.err.println(&quot;Could not invoke event handler!&quot;);
                e.printStackTrace(System.err);
            }
        }
    }

    /**
     * Find all methods from the &lt;em&gt;handler&lt;/em&gt; object that must be called, based on the presence
     * of the HandleEvent annotation.
     */
    private Collection findMatchingEventHandlerMethods(Object handler, String eventName) {
        Method[] methods = handler.getClass().getDeclaredMethods();
        Collection result = new ArrayList();
        for (Method method : methods) {
            if (canHandleEvent(method, eventName)) {
                result.add(method);
            }
        }
        return result;
    }

    /**
     * Look for the annotation values.
     */
    private boolean canHandleEvent(Method method, String eventName) {
        HandleEvent handleEventAnnotation = method.getAnnotation(HandleEvent.class);
        if (handleEventAnnotation != null) {
            String[] values = handleEventAnnotation.value();
            return Arrays.asList(values).contains(eventName);
        }
        return false;
    }
}
</pre>
<p>The code for the annotation is trivial:</p>
<pre class="brush: java;">
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface HandleEvent {
    String[] value();
}
</pre>
<p>You can find the complete source code<br />
<a href="https://s3-eu-west-1.amazonaws.com/gmarabout/annotation-event-handling.zip">here</a>.</p>
<p>What do you think of it ?</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/gmarabout.wordpress.com/20/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/gmarabout.wordpress.com/20/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/gmarabout.wordpress.com/20/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/gmarabout.wordpress.com/20/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/gmarabout.wordpress.com/20/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/gmarabout.wordpress.com/20/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/gmarabout.wordpress.com/20/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/gmarabout.wordpress.com/20/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/gmarabout.wordpress.com/20/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/gmarabout.wordpress.com/20/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/gmarabout.wordpress.com/20/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/gmarabout.wordpress.com/20/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/gmarabout.wordpress.com/20/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/gmarabout.wordpress.com/20/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gmarabout.wordpress.com&amp;blog=14216340&amp;post=20&amp;subd=gmarabout&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://gmarabout.wordpress.com/2010/09/23/annotation-based-event-handling-in-java/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/636c843240fc848f011aeff882c8c52d?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">gmarabout</media:title>
		</media:content>
	</item>
		<item>
		<title>A Spring Layout in Processing</title>
		<link>http://gmarabout.wordpress.com/2010/06/18/spring-layout-in-processing/</link>
		<comments>http://gmarabout.wordpress.com/2010/06/18/spring-layout-in-processing/#comments</comments>
		<pubDate>Fri, 18 Jun 2010 11:08:03 +0000</pubDate>
		<dc:creator>gmarabout</dc:creator>
				<category><![CDATA[Processing]]></category>

		<guid isPermaLink="false">http://gmarabout.wordpress.com/?p=3</guid>
		<description><![CDATA[Processing is a wonderful tool for visualization prototyping. I developped a small applet to implement a &#8216;Spring layout&#8217; graph algorithm. This layout is basically a combination of spring forces (Hook&#8217;s law) and electrostatic repulsion forces (Coulomb&#8217;s law). Enjoy!<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gmarabout.wordpress.com&amp;blog=14216340&amp;post=3&amp;subd=gmarabout&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><a href="http://processing.org">Processing</a> is a wonderful tool for visualization prototyping.</p>
<p>I developped a small <a title="Spring Layout in Processing" href="https://s3-eu-west-1.amazonaws.com/gmarabout/springlayout_applet/index.html">applet</a> to implement a &#8216;Spring layout&#8217; graph algorithm.</p>
<p>This layout is basically a combination of spring forces (<a href="http://en.wikipedia.org/wiki/Hooke%27s_law">Hook&#8217;s law</a>) and electrostatic repulsion forces (<a href="http://en.wikipedia.org/wiki/Coulomb%27s_law">Coulomb&#8217;s law</a>).</p>
<p>Enjoy!</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/gmarabout.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/gmarabout.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/gmarabout.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/gmarabout.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/gmarabout.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/gmarabout.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/gmarabout.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/gmarabout.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/gmarabout.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/gmarabout.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/gmarabout.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/gmarabout.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/gmarabout.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/gmarabout.wordpress.com/3/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gmarabout.wordpress.com&amp;blog=14216340&amp;post=3&amp;subd=gmarabout&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://gmarabout.wordpress.com/2010/06/18/spring-layout-in-processing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/636c843240fc848f011aeff882c8c52d?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">gmarabout</media:title>
		</media:content>
	</item>
		<item>
		<title>When and when not to use a rule engine in your project</title>
		<link>http://gmarabout.wordpress.com/2010/06/15/when-and-when-not-to-use-a-rule-engine-in-your-project/</link>
		<comments>http://gmarabout.wordpress.com/2010/06/15/when-and-when-not-to-use-a-rule-engine-in-your-project/#comments</comments>
		<pubDate>Tue, 15 Jun 2010 19:52:32 +0000</pubDate>
		<dc:creator>gmarabout</dc:creator>
				<category><![CDATA[Rule Engine]]></category>

		<guid isPermaLink="false">http://gmarabout.wordpress.com/?p=5</guid>
		<description><![CDATA[Basically, a rule engine will provide more agility to your project : rules are declarative and more expressive than nested IF/THEN/ELSE of your favorite programming language. rules are more maintainable : adding/removing or modifying a rule is very easy and does not require to change the overall ruleset. rule engines are powerful : rule languages&#160;&#8230; <a href="http://gmarabout.wordpress.com/2010/06/15/when-and-when-not-to-use-a-rule-engine-in-your-project/">Read&#160;more</a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gmarabout.wordpress.com&amp;blog=14216340&amp;post=5&amp;subd=gmarabout&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Basically, a rule engine will provide more agility to your project :</p>
<ul>
<li>rules are declarative and more expressive than nested IF/THEN/ELSE of  your favorite programming language.</li>
<li> rules are more maintainable :  adding/removing or modifying a rule is very easy and does not require to  change the overall ruleset.</li>
<li>rule engines are powerful : rule  languages provide powerful constructs and features that cannot be  expressed easily in a normal programming language.</li>
<li>rule engines  are very efficient : even if they do not have native code performance,  rule engines provide optimizations so that performances are usually not  an issue.</li>
</ul>
<p>So, as a general rule, a rule engine is a very good way  to add decision logic automation to your project if :</p>
<ul>
<li>rules will  change &#8216;often&#8217; (more often than normal patch releases of your  application).</li>
<li>you will have to deal with a lot of rules.</li>
<li>you  can afford a rule engine specialist or a consultant in your dev-team.</li>
</ul>
<p>In  the contrary, I would not recommend to adopt rule engines you answer  &#8220;no&#8221; to at least two of the items above.</p>
<ul>
<li>rules will never  change or change very rarely: hard coded decision logic could do the  job.</li>
<li> you have very few rules : no need for a rule engine, you  programming language should be enough.</li>
<li>You cannot afford a rule  engine specialist : don&#8217;t risk to lose control over your project and  avoid modules that no one can maintain.</li>
</ul>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/gmarabout.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/gmarabout.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/gmarabout.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/gmarabout.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/gmarabout.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/gmarabout.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/gmarabout.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/gmarabout.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/gmarabout.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/gmarabout.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/gmarabout.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/gmarabout.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/gmarabout.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/gmarabout.wordpress.com/5/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gmarabout.wordpress.com&amp;blog=14216340&amp;post=5&amp;subd=gmarabout&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://gmarabout.wordpress.com/2010/06/15/when-and-when-not-to-use-a-rule-engine-in-your-project/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/636c843240fc848f011aeff882c8c52d?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">gmarabout</media:title>
		</media:content>
	</item>
	</channel>
</rss>
