<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>jvoorhis comments on Genetic Madlibs</title>
    <link>http://www.jvoorhis.com/</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>jvoorhis comments</description>
    <item>
      <title>"Genetic Madlibs": comment by JV</title>
      <description>&lt;p&gt;Take a peek at &lt;a href="http://www.jvoorhis.com/articles/2007/04/09/module-method_added"&gt;Module#method_added&lt;/a&gt; for a good look at how this works.&lt;/p&gt;</description>
      <pubDate>Mon,  9 Apr 2007 10:42:13 NZST</pubDate>
      <guid>http://www.jvoorhis.com/articles/2007/04/07/genetic-madlibs#comment-479</guid>
      <link>http://www.jvoorhis.com/articles/2007/04/07/genetic-madlibs#comment-479</link>
    </item>
    <item>
      <title>"Genetic Madlibs": comment by JV</title>
      <description>&lt;p&gt;Good question. &lt;span class="caps"&gt;DRP&lt;/span&gt; uses the exotic and undocumented &lt;code&gt;Module#method_added&lt;/code&gt; callback to alias the method with a unique name and change its access to private. This behavior takes effect between the &lt;code&gt;begin_rules&lt;/code&gt; and &lt;code&gt;end_rules&lt;/code&gt; invocations. When &lt;code&gt;end_rules&lt;/code&gt; is called, methods are defined that forward the call to &lt;span class="caps"&gt;DRP&lt;/span&gt;&amp;#8217;s dispatching system, which in turn dispatches to one of the original methods.&lt;/p&gt;


	&lt;p&gt;Here is a closer look at what&amp;#8217;s going on in my DrpGenerator class.&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
irb(main):038:0&amp;gt; gen.private_methods.grep(/drp/)
=&amp;gt; ["__drp__somebody__1", "__drp__example__1", "__drp__exp__0",
"__drp__ex1__0", "__drp__ex2__0", "__drp__main__0",
"__drp__exp__1", "__drp__call__method", "__drp__adj1__0",
"__drp__ex2__1", "__drp__hello__0", "__drp__exp__2",
"__drp__adj1__1", "__drp__ex2__2", "__drp__somebody__0",
"__drp__example__0", "__drp__choose__method", "__drp__ex2__3"]
&lt;/code&gt;&lt;/pre&gt;</description>
      <pubDate>Mon,  9 Apr 2007 09:45:01 NZST</pubDate>
      <guid>http://www.jvoorhis.com/articles/2007/04/07/genetic-madlibs#comment-478</guid>
      <link>http://www.jvoorhis.com/articles/2007/04/07/genetic-madlibs#comment-478</link>
    </item>
    <item>
      <title>"Genetic Madlibs": comment by RSL</title>
      <description>&lt;p&gt;Since you mention it&amp;#8230; What &lt;em&gt;does&lt;/em&gt; the multiple method definitions do? Besides confuse most of us. ;) I would have thought that only the last definition would be &amp;#8220;available&amp;#8221; but your examples show that&amp;#8217;s not the case.&lt;/p&gt;</description>
      <pubDate>Sun,  8 Apr 2007 00:28:01 NZST</pubDate>
      <guid>http://www.jvoorhis.com/articles/2007/04/07/genetic-madlibs#comment-477</guid>
      <link>http://www.jvoorhis.com/articles/2007/04/07/genetic-madlibs#comment-477</link>
    </item>
    <item>
      <title>"Genetic Madlibs" by jvoorhis</title>
      <description>&lt;p&gt;Inspired by the &lt;a href="http://eigenclass.org/hiki/language-generator"&gt;eigenclass language generator&lt;/a&gt;, I&amp;#8217;ve thrown together an example of how it would be implemented with the &lt;a href="http://drp.rubyforge.org/"&gt;Directed Ruby Programming&lt;/a&gt; library. Directed programming is an interesting hybrid of &lt;a href="http://en.wikipedia.org/wiki/Genetic_programming"&gt;genetic programming&lt;/a&gt; and the recently discovered &lt;a href="http://en.wikipedia.org/wiki/Grammatical_evolution"&gt;grammatical evolution&lt;/a&gt; technique which generates a program tree according to a grammar.&lt;/p&gt;


	&lt;p&gt;This example may not be very interesting for those familiar with evolutionary programming techniques, but it created a fun diversion, highlights how the &lt;span class="caps"&gt;DRP&lt;/span&gt; library is to be used, and shows a different solution for a language generator.&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
class DrpGenerator
  extend DRP::RuleEngine
  begin_rules
    def main() "#{hello}\n#{ex1}\n#{ex2}" end

    def hello() "Hello, #{somebody}!" end

    def somebody() "matz" end
    def somebody() "world" end

    def ex1() "This is a more #{adj1} #{example}." end

    def adj1() "complex" end
    def adj1() "elaborate" end

    def example() "example" end
    def example() "test" end

    def ex2() "Some simple sentence." end
    def ex2() "Another, involving harder stuff." end
    def ex2() "Another, involving a more complex #{exp}." end
    def ex2() "Yet another possibility; each one is chosen with an evolutionary algorithm." end

    def exp() "expression" end
    def exp() "disjunction" end
    def exp() example end
  end_rules
end

g = DrpGenerator.new
3.times { puts g.main }

Hello, matz!
This is a more elaborate example.
Another, involving a more complex disjunction.

Hello, world!
This is a more elaborate test.
Another, involving harder stuff.

Hello, world!
This is a more complex example.
Yet another possibility; each one is chosen with an evolutionary algorithm.
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;My DrpGenerator isn&amp;#8217;t as readable or attractive as the eigenclass &lt;span class="caps"&gt;DSL&lt;/span&gt;, and multiple definitions of the same method will likely confuse most Rubyists at first glance, but makes it easy to see the underlying grammar.&lt;/p&gt;

</description>
      <pubDate>Sat,  7 Apr 2007 08:47:00 NZST</pubDate>
      <guid>&lt;a href="/articles/2007/04/07/genetic-madlibs"&gt;Genetic Madlibs&lt;/a&gt;</guid>
      <link>&lt;a href="/articles/2007/04/07/genetic-madlibs"&gt;Genetic Madlibs&lt;/a&gt;</link>
    </item>
  </channel>
</rss>
