Skip to content

Ramblings

Musings of Matt Williams
  • Blog
  • About
  • Chibi
  • Ruby Blender
  • Archives
  • Log in
 
Less
More
Trim
Untrim
« Older
Home
Loading
Newer »
Archive for the 'programming' Category
01Sep09 Fractal Terrain Generation
Uncategorized programming ruby
1 Comment

The original idea behind the generation was in Computer Rendering of Stochastic Models, in the June 1982 issue of  Communications of the ACM.  I’m basing my code on Paul Mertz’s Generating Random Fractal Terrain.

The gist of what I’m doing is to start with a square, like this (I’ve added a grid to make it easier to see the elements):

Our map

Our map

From here, we determine the corners:

Four corners

Four corners

Each location in the grid has a “height”. In my implementation, I assign it a value from (-1 .. 1). From these four points we find the center point, then average the values of the 4 corners and add to it some random value.

The center point

The center point

This random value, h is in a range of (-1 .. 1). So if the 4 corners all had a value of 0, and our random value was .5, then the center would be .5:

0.0 0.0
0.5
0.0 0.0

From here we determine the midpoints on the sides of the square:

Midpoints

Midpoints

Notice that where we once had one (1) square, we now have 4 smaller squares.  From here, we do each square in turn in the same fashion.  However, this time the range of h has been halved — the range can be tweaked.  If you use the formula 2^(-n), to make the terrain smoother, use a n closer to 1.  Likewise, the smaller n is, the more random the terrain.

The smaller squares are each processed, yielding smaller squares.

Finally, you have a grid of values, which range from -1 < n < 1.  From this terrain, you could then assign ranges, such as n <= -.5  for water, -.5 < n <= -.3 for swamps, -.1 < n <= .2 for plains, snd so forth.  From that you could then have something like this (tiles are  from Wesnoth):

Sample Terrain

Sample Terrain

If anyone’s interested, I can post sample code.

Enhanced by Zemanta
08Oct08 Semantic Web
philosophy programming
0 Comments

I have been doing some site scraping of late.  And as a result, I have really come to appreciate the semantic web.  It’d make life infinitely easier for grabbing data.  Of course there are other, better reasons for using the semantic web, but right now, it’d make a difference in my life.

22Sep08 JRuby + jmx4r + rrd4j == Easy reporting on app servers (part I)
howto jruby monitoring programming
2 Comments

This is a how-to for using jmx and rrd4j, a java implementation of rrdtool, to report on app server statistics.

Thanks to Jeff Mesnil(author of jmx4r), Werner Schuster (JMX the Ruby way with jmx4r), sishen (JRobin sucks), and the rrd4j team.

You’ll need the following:

  1. JVM 1.5 or higher — JRE is not enough, you also need the JDK. Remember to set your JAVA_HOME — JRuby needs it.
  2. JRuby — I’m using 1.1.4. Note: You need to be sure to set the JRUBY_HOME and make sure that ${JRUBY_HOME}/bin comes in $PATH prior to any other ruby installation. Otherwise, your results will be indeterminate.
  3. jmx4r — this is a gem and can be installed by
    jruby -S gem install jmx4r
  4. rrd4j — in order to get this, you need to create an account on java.net. Once you’ve done that, you can download the library via subversion:
    svn co https://rrd4j.dev.java.net/svn/rrd4j/trunk rrd4j --username <em>username</em>

    Replace username with your java.net username. You’ll need ant to build it:

    cd rrd4j/rrd4j; ant

    Or you can download the jars: rrd4j jars

  5. An application which responds to jmx queries — this example is using jconsole since it is a standard part of the java distribution in 1.5 and after.

Before we do much else, let’s verify that jconsole is working:

jconsole -J-Dcom.sun.management.jmxremote.port=64850 -J-Dcom.sun.management.jmxremote.authenticate=false -J-Dcom.sun.management.jmxremote.ssl=false

Choose for the port some large nuimber…  Your screen will look something like this for 1.5 (ignore the cutoff bits, I’m writing this on chibi):

Portion of a JConsole screen

Portion of a JConsole screen

Ok, now that we’ve got that going, let’s attempt connecting via jmx4r…

$ jruby -S jirb
irb(main):001:0&gt; require 'rubygems'
=&gt; true
irb(main):002:0&gt; require 'jmx4r'
=&gt; true
irb(main):003:0&gt; require 'java'
=&gt; false
irb(main):004:0&gt; JMX::MBean.establish_connection :host =&gt; "localhost", :port =&gt; 64850
=&gt; #&lt;#:0x1bd06a0 @java_object=javax.management.remote.rmi.RMIConnector$RemoteMBeanServerConnection@12f9ee1&gt;
irb(main):005:0&gt; memory =JMX::MBean.find_by_name "java.lang:type=Memory"
=&gt; #"NonHeapMemoryUsage", "verbose"=&gt;"Verbose", "object_pending_finalization_count"=&gt;"ObjectPendingFinalizationCount", "heap_memory_usage"=&gt;"HeapMemoryUsage"}, @mbsc=#&lt;#:0x1bd06a0 @java_object=javax.management.remote.rmi.RMIConnector$RemoteMBeanServerConnection@12f9ee1&gt;, @operations={"gc"=&gt;["gc", []]}, @object_name=#&gt;
irb(main):006:0&gt; memory.heap_memory_usage
=&gt; #
irb(main):012:0&gt; memory.heap_memory_usage.keys
=&gt; #&lt;#:0x129dcc0 @java_object=[committed, init, max, used]&gt;
irb(main):015:0&gt; memory.heap_memory_usage.to_s
=&gt; "javax.management.openmbean.CompositeDataSupport(compositeType=javax.management.openmbean.CompositeType(name=java.lang.management.MemoryUsage,items=((itemName=committed,itemType=javax.management.openmbean.SimpleType(name=java.lang.Long)),(itemName=init,itemType=javax.management.openmbean.SimpleType(name=java.lang.Long)),(itemName=max,itemType=javax.management.openmbean.SimpleType(name=java.lang.Long)),(itemName=used,itemType=javax.management.openmbean.SimpleType(name=java.lang.Long)))),contents={committed=7569408, init=0, max=66650112, used=3952968})"
irb(main):020:0&gt; memory.heap_memory_usage["committed"]
=&gt; 7569408
irb(main):021:0&gt; #trigger a gc
irb(main):022:0* memory.gc
=&gt; nil
irb(main):023:0&gt; memory =JMX::MBean.find_by_name "java.lang:type=Memory"
=&gt; #"NonHeapMemoryUsage", "verbose"=&gt;"Verbose", "object_pending_finalization_count"=&gt;"ObjectPendingFinalizationCount", "heap_memory_usage"=&gt;"HeapMemoryUsage"}, @mbsc=#&lt;#:0x1bd06a0 @java_object=javax.management.remote.rmi.RMIConnector$RemoteMBeanServerConnection@12f9ee1&gt;, @operations={"gc"=&gt;["gc", []]}, @object_name=#&gt;
irb(main):024:0&gt; memory.heap_memory_usage["committed"]
=&gt; 7569408
irb(main):026:0&gt; memory.heap_memory_usage["max"]
=&gt; 66650112
irb(main):027:0&gt; memory.heap_memory_usage["used"]
=&gt; 4210824
irb(main):028:0&gt; exit
$

Cool! now we’re making progress!

22Sep08 JRuby to check connectivity…
jruby programming
0 Comments

I had an issue reported by a developer where their jboss connection pool wasn’t working properly. It looked good to me, so I decided to verify that everything worked in so far as connectivity from the box. So, I used the following jruby script to help:

require 'rubygems'
require 'jdbc'
require 'java'
Java::oracle.jdbc.driver.OracleDriver
url = "jdbc:oracle:thin:@SERVER:1521:DB"
user = "user"
pass = "pass"
conn = java.sql.DriverManager.get_connection(url,user,pass)
stmt = conn.create_statement
query = "select 1 from dual"
rss = stmt.execute_query(query)
puts rss.next?                  # did we get anything?
12Sep08 Class is Class, and Instance, Instance, and never the twain shall meet
Uncategorized eating crow gotchas programming ruby
2 Comments

More about methods, it’s inspired/spurred by a comment on methods, public_methods, and private_methods by Pit Captain. It also corrects some misconceptions I had (and may have (wrongly) given others).

I’ve added a new category, “eating crow” for this and any other postings where I step back and re-evaluate my posts. This is to keep me honest, and, if y’all would, please feel free to tell me when it’s time to “eat crow”.
Continue reading ‘Class is Class, and Instance, Instance, and never the twain shall meet’

11Sep08 methods, public_methods, and private_methods
programming ruby tips
3 Comments

Ruby’s Object has a method, methods. You can use it to see the methods which an object has. Sort of. In this post I’m examining methods, public_methods, and private_methods as well as some of their implications.
Continue reading ‘methods, public_methods, and private_methods’

11Sep08 universal cat redux
programming refactoring ruby utilities
2 Comments

I have a neglectful relationship with inject. That is, I neglect to remember that it exists, having worked for so long with other languages which are “unfamiliar with the concept”. Amos King’s blog entry on Inject & Me – BFFs got me to thinking that ucat (see cat on steroids (or cat on a hot ruby roof)) could use inject as opposed to the each_byte. So, instead of

def non_printing(line)
  proc = ""
  line.each_byte do |c|
    proc += case c
      when (0 .. 8): "^#{(c + 64).chr}"
      when (10 .. 11): "^#{(c + 64).chr}"
      when (13 .. 26): "^#{(c + 64).chr}"
      when (27 .. 31): "^#{%w([ \\ ] ^ _)[c - 27]}"
      when 127: "^?"
      when ((c &amp; 128) == 128): "M-0#{c.to_s(8)}"
      else c.chr
    end
  end
  proc
end

I can do:

def non_printing(line)
  line.split("").map{|c|c[0]}.inject("") do |s,c|
    s += case c
      when (0 .. 8): "^#{(c + 64).chr}"
      when (10 .. 11): "^#{(c + 64).chr}"
      when (13 .. 26): "^#{(c + 64).chr}"
      when (27 .. 31): "^#{%w([ \\ ] ^ _)[c - 27]}"
      when 127: "^?"
      when ((c &amp; 128) == 128): "M-0#{c.to_s(8)}"
      else c.chr
    end
  end
end

I still think there needs to be a better way — going from string to an array of strings mapped to an array of bytes so that I can process it via inject seems to be awkward. So, I do some searching and find Object#enum_for (let me plug gotAPI — it’s a great tool for searching a large number of API’s) and come up with:

  def non_printing(line)
    line.enum_for(:each_byte).inject("") do |s,c|
      s += case c
              when (0 .. 8): "^#{(c + 64).chr}"
              when (10 .. 11): "^#{(c + 64).chr}"
              when (13 .. 26): "^#{(c + 64).chr}"
              when (27 .. 31): "^#{%w([ \\ ] ^ _)[c - 27]}"
              when 127: "^?"
              when ((c &amp; 128) == 128): "M-0#{c.to_s(8)}"
              else c.chr
           end
    end
  end

That seems cleaner to me. One of the things I love about ruby is that there’s usually more way than one to do something. And it’s often quicker, like in Unix, to go with what you know rather than making it more elegant. However, I also like that it’s easy to write elegant code.

And elegant code is a thing of beauty.

You can download the updated version of ucat.rb (you may need to rename it to ucat.rb)

10Sep08 cat on steroids (or cat on a hot ruby roof)
programming ruby utilities
1 Comment

I got to thinking about SuperIO and how it could be used as a swiss army chainsaw to open files, whereever they might be on the net.  From there, my fevered mind got to thinking about cat and how the two could be used together.  That said, I present ucat — a universal cat, if you will, which does not need to be herded, but rather will do as you ask.  It’s expecting to be able to find SuperIO, so you’ll need to make it available.

Continue reading ‘cat on steroids (or cat on a hot ruby roof)’

02Sep08 A language a year
philosophy programming
3 Comments

The Pragmatic Programmers have proposed that developers learn a language a year.  I agree that it’s very important to keep abreast of changes in IT and that by learning new languages we can keep our minds sharp and bring new ways of doing things into old languages.  However, I have to wonder if this learning a language gets in the way of knowing a language.

I’ve always been a generalist, when I was younger I wanted to be a renaissance man.  But sometimes I think that getting caught up in being a generalist is as large a trap as specializing in one thing and not not being willing to learn anything else.

When I was 19, I taught C at a local community college’s Continuing Ed programme.  I’d been asked to do so six weeks prior to teaching the course.  I followed K&R, and I know I was able to get the gist across to my students.  I’d learned C, but I didn’t know C.  It took at least 6-12 months of programming C day-in, day out to know the language.

And then there’s unix.  I recently celebrated my 20th aniversary of using it.  I can claim to having learned it, even perhaps understanding it.  But knowing?  I’m still discovering new aspects of it.

I agree that it’s important to learn new things, but sometimes you need to just know a thing.

27Aug08 Programming philosophy in the oddest of places
philosophy programming
2 Comments

Today’s thought for the day from A Word A Day is:

Not being able to govern events, I govern myself.

-Michel de Montaigne, essayist (1533-1592)

This struck me as being very much the idea behind defensive programming.  Because we don’t know what sort of events life will throw at a programme, we need for it to be robust and able to respond to most anything, barring, of course, Fear, Fire, and Foes.

So how do we go about doing so?  For starters, there’s testing, whether BDD or TDD.  Boundary conditions and fencepost errors frequently cause grief.  And any time the programme interracts with the “outside” there’s room for error — whether it be bad data or an I/O error.

Assume Nothing! Trust no one! Paranoia is good! (in moderation)

Assume Nothing! Trust no one! Paranoia is good! (in moderation)

 
Browse Archives »
  • administrivia (6)
  • books (1)
  • Computers (2)
  • css (3)
  • eating crow (1)
  • games (1)
  • glassfish (1)
  • gotchas (16)
  • howto (2)
  • idiocy (3)
  • javascript (4)
  • jboss (4)
  • jruby (6)
  • Just Enough Programming (7)
  • life hacking (2)
  • mini sagas (1)
  • miscellany (1)
  • monitoring (1)
  • mysql (1)
  • philosophy (4)
  • php (1)
  • programming (17)
  • rails (6)
  • rants (2)
  • refactoring (1)
  • ruby (14)
  • tips (9)
  • tools (2)
  • Uncategorized (9)
  • utilities (3)
  • web (5)
  • web design (3)
 

Latest

  • Rails & JRuby in a Jar
  • Fractal Terrain Generation
  • Quick thought on programming and distractions
  • Using jnp as a JBoss heartbeat
  • z-index and events
  • JBoss port confusion
  • SSL Joys
  • After long silence
  • New Ruby Blog
  • Javascript with CSS Sprites Animation

Flickr

layout_newm3headerTerrain Testa

Blogroll

  • Development Blog
  • Documentation
  • Plugins
  • Suggest Ideas
  • Support Forum
  • Themes
  • WordPress Planet

Search

Browse by Category

  • administrivia (6)
  • books (1)
  • Computers (2)
  • css (3)
  • eating crow (1)
  • games (1)
  • glassfish (1)
  • gotchas (16)
  • howto (2)
  • idiocy (3)
  • javascript (4)
  • jboss (4)
  • jruby (6)
  • Just Enough Programming (7)
  • life hacking (2)
  • mini sagas (1)
  • miscellany (1)
  • monitoring (1)
  • mysql (1)
  • philosophy (4)
  • php (1)
  • programming (17)
  • rails (6)
  • rants (2)
  • refactoring (1)
  • ruby (14)
  • tips (9)
  • tools (2)
  • Uncategorized (9)
  • utilities (3)
  • web (5)
  • web design (3)

Browse by Tag

  • 1.2.6
  • 2.1
  • administrivia
  • autotest
  • books
  • controller
  • css
  • feed-normalizer
  • feeds
  • gotchas
  • idiocy
  • irb
  • Java
  • javascript
  • jboss
  • jruby
  • just enough programming
  • mini sagas
  • open-uri
  • philosophy
  • php
  • pragmatism
  • programming
  • quotations
  • rails
  • rants
  • reading
  • restful_authentication
  • rspec
  • rss
  • ruby
  • rubygems
  • scriptaculous
  • setup
  • simplicity
  • sprites
  • statemachine
  • tips
  • treetop
  • utilities
  • web
  • web design
  • websense
  • yaml
  • zentest

Browse by Month

  • March 2010 (1)
  • September 2009 (1)
  • August 2009 (2)
  • July 2009 (2)
  • May 2009 (1)
  • April 2009 (1)
  • February 2009 (4)
  • January 2009 (2)
  • December 2008 (2)
  • November 2008 (5)
  • October 2008 (3)
  • September 2008 (12)
  • August 2008 (28)
 
 
  • Blog
  • About
  • Chibi
  • Ruby Blender
  • Archives
  • Log in
 


Theme Design by Jay Kwong | Powered by WordPress and K2

 

Home Top Archives Entries FeedComments Feed