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)

Community
  • 1
  • 1
jldupont
  • 93,734
  • 56
  • 203
  • 318
  • Pattern matching is usually to do with Regular Expressions. I think what you want is more of just a "messaging" system for inter-object interaction? – Nick Bedford Dec 23 '09 at 22:21
  • 1
    sounds vaggggggguely like Scala case classes... maybe look at that? (sorry I'm not more familiar with it) – Jason S Dec 23 '09 at 22:22
  • scala... hmmm... I know nothing of Scala... @Jason: you should write an answer ( I am gentle with contributors as my profile shows clearly ). – jldupont Dec 23 '09 at 22:24
  • Do you have a concrete example of a message and how it would mathch an action that you can show us? Like Nick, I also immediately thought of regular expressions, but I assume you're familiar with those since they have them in Erlang and most other languages too. – Bill the Lizard Dec 23 '09 at 22:28
  • 1
    Pattern matching is a feature of functional programming languages, it's nothing to do with regular expressions. Scala supports pattern matching, but I don't think the GWT compiler supports scala. It only compiles from pure Java with a subset of the core Java libraries – Ben James Dec 23 '09 at 22:29
  • Can you elaborate on what you mean by message passing architecture? If you are talking about message objects then this can be easily accomplished through inheritance and subtyping. If you are talking about passing messages between architectural layers (from one system to another) you can use XML or even message serialization. Perhaps I misunderstood your requirements. – GrayWizardx Dec 23 '09 at 22:32

2 Answers2

2

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
  • 268,207
  • 37
  • 334
  • 440
0

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
  • 29,022
  • 11
  • 55
  • 82
  • 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 Dec 23 '09 at 22:51
  • Events *are* messages, however the form of the message is important so that you can utilize it correctly and fastly. – Esko Dec 24 '09 at 08:12