Pattern Matching framework?

夙愿已清 提交于 2019-12-11 03:22:22

问题


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)


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/1955623/pattern-matching-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!