views:

177

answers:

2

I will be tackling a Java (GWT) project soon (related question). Maybe I am trying to stretch things here but I was wondering if there is any "pattern matching framework" (don't really know if there is a term for this) written in Java? (maybe it is my prolonged exposure to Erlang that twists my thoughts around patterns all the time :-)

I will be using a "message passing" architecture to communicate between my Java components and I'd like to efficiently "match" messages to actions.

Maybe I should just stick with localized state-machines or is there anything else?

Updated: a "message" will be an instance-object carrying "data only". I am not currently planning on using inheritance for conveying semantics to the said messages but rather simple properties.

Update2: after taping the collective wisdom of SO (see here), it seems that Scala is out-of-scope also.

(NOTE: Java novice here... please be gentle)

A: 

What I think you want for pattern matching is regular expressions and by your description you're going to have an event-driven program where the messages are the events.

Don't do that. Instead use actual object as messages, this way you don't have to parse anything (*that's slow*) but just match types and check related directives (*that's fast*) for actual actions. Making a state machine out of all this isn't a bad choice either.

Esko
Yes reception of "messages" can be considered "events" but I haven't mentioned regular expressions nor will I do in the case at hand here: these are clearly (at least in my mind, sorry ;-) out-of-scope.
jldupont
Events *are* messages, however the form of the message is important so that you can utilize it correctly and fastly.
Esko
+2  A: 

What you may be looking for are Javaspaces (a Java implementation of tuple-spaces) and matching objects based on their attributes (called 'entries' in the Javaspace world).

Spaces store objects with particular attributes or entries (e.g. an associated currency, city, user, whatever). You can then select objects from the space by specifying 0 or more such entries, and thus get back 0 or more objects. As such, it's a useful pattern for messaging and producer/consumer scenarios in particular.

So you can store your objects (messages) with particular attributes (e.g. message type, consumer type etc.) and your consumers will select these objects based on a set of 0 or more attributes. Note that this doesn't require modification of the underlying object that you're storing. You can run a space in-process (in one JVM) - it's not just a networked storage pattern.

Brian Agnew
+1: I already am liking the looks of what I am reading... thanks!
jldupont