NAME Message::SmartMerge - Enforce downstream transformations on message streams SYNOPSIS use Message::SmartMerge; my $merge = Message::SmartMerge->new(); $merge->config({ merge_instance => 'instance', }); $merge->message({ instance => 'i1', x => 'y', this => 'whatever', }); #no merges, so pass through: #emit sends { instance => 'i1', x => 'y', this => 'whatever' } $merge->add_merge({ merge_id => 'm1', match => {x => 'y'}, transform => {this => 'that'}, }); #so we've already passed through a message instance i1, and this new #merge matches that, so the module will send a transformed message: { instance => 'i1', x => 'y', this => 'that', } #Now send another message through: $merge->message({ instance => 'i1', x => 'y', this => 'not that', something => 'else', }); #merge matches x => 'y', so transforms this => 'that': #emit sends: { instance => 'i1', x => 'y', this => 'that', something => 'else' } $merge->remove_merge('m1'); #even though we didn't send a message in, removing a merge will trigger #an emit to reflect that a change has occurred, specifically that the #previously activated transform is no longer in force. It sends the #last message received, without the transform #emit sends: { instance => 'i1', x => 'y', this => 'not that', something => 'else' } #Here's a way the message stream can clear a merge: $merge->add_merge({ merge_id => 'm2', match => { x => 'y', }, transform => { foo => 'bar', }, toggle_fields => ['something'], }); #Since m2 also matches x => 'y', we emit: { instance => 'i1', x => 'y', this => 'not that', something => 'else', foo => 'bar', } $merge->message({ instance => 'i1', x => 'y', foo => 'not bar', something => 'else', another => 'thing', }); #the value of the single defined toggle field ('something') did not #change from the first value we saw in it ('else'). So m2 stands: { instance => 'i1', x => 'y', something => 'else', foo => 'bar', another => 'thing', } #even though we passed 'not bar' in with foo, it was transformed to 'bar' #Now let's hit the toggle: $merge->message({ instance => 'i1', x => 'y', foo => 'not bar', something => 'other', another => 'thing', }); #this will 'permanently' remove the merge m2 for i1; the message passes #through untransformed: { instance => 'i1', x => 'y', foo => 'not bar', something => 'other', another => 'thing', } #Here's another way the message stream can clear a merge: $merge->add_merge({ merge_id => 'm3', match => { i => 'j', }, transform => { a => 'b', }, remove_match => { remove => 'match', }, }); #This causes nothing to emit, because there are no instances that match #i => 'j' $merge->message({ instance => 'i2', x => 'y', i => 'j', foo => 'not bar', a => 'not b', something => 'here', }); #this is fun because it matches both m2 and m3. it would have matched #m1 had we not removed it #i2 has never been seen before, and m2 is a toggle. The toggle #deallocates itself for an instance if the toggle field changes #from the previous to the current message. Since there was no #previous message for i2, the toggle merge deallocates itself for i2 #before it can take any action. { instance => 'i2', x => 'y', i => 'j', foo => 'not bar', something => 'here', a => 'b', #rather than 'not b' } #and now to deallocate m3: $merge->message({ instance => 'i2', x => 'y', i => 'j', a => 'not b', remove => 'match', }); #which emits: { instance => 'i2', x => 'y', i => 'j', a => 'not b', #no longer transformed remove => 'match', } DESCRIPTION In message based programming, we think in terms of streams of messages flowing from one way-point to another. Each way-point does only one thing to messages flowing through it, independent from various other way-points. The contract between these are required fields in the messages. This module is designed to modify the state of a stream of messages in a powerful and configurable way. Conceptually, it will enforce certain transformations on certain message streams. If the nature of the transformation changes, (for instance, if it expires, or is deallocated some other way), the module will send a 'corrective' message. We call these configurations 'merges'. Part of a merge is a transformation. For example, when a new merge is configured, all of the matching message instances will be re-sent with the new transform in force. And when the merge is removed or expires, all of the matching message instances are re-sent with their last received values. This effectively causes the downstream receiver to be aware of stateful changes, but in a fully message-oriented fashion. Merges can be added and removed explicitly with add_merge and remove_merge. They can also expire, with expire and expire_at. More interestingly, merges can be deallocated for a given message stream using one or two configurations: remove_match and toggle_fields. remove_match is simplest: if a message instance is under the influence of a given merge that contains a remove_match config, and that message matches the remove_match, then the merge is, for that instance, deallocated. The message passes through that merge unchanged. toggle_fields is more tricky: it is an array of fields in the message to consider. The toggle_fields configured merge will continue to be in force as long as the value of all of the fields in toggle_fields is un-changed. As soon as any of those values changes, the merge is, for that instance, deallocated. This is pretty abstract stuff; more concrete examples will be forthcoming in subsequent releases. SUBROUTINES/METHODS new my $merge = Message::SmartMerge->new(state => $previous_state); * state (optional) The hashref returned by a previous invocation of "get_state" get_state my $state_to_save = $merge->get_state(); This method takes no arguments; it returns a hashref to all of the data necessary to re-create the current behaviour of this library. Simply put, before your process exits, gather the return value of get_state, and save it somewhere. When your process comes up, take that information and pass it into the state key in the constructor. The library will continue functioning as before. emit $merge->emit(%args) This method is designed to be over-ridden; the default implementation simply adds the passed message to the package global @Message::SmartMerge::return_messages and returns all of the arguments * message The message being sent out, which is a HASHref. * matching_merge_ids A HASHref whose keys are the merge IDs that were applied, and values are 1. * other: things (TODO) config $merge->config({ merge_instance => 'instance_key', }); * config_def (positional, required) HASHref of configuration * merge_instance (required) This is a scalar must exist as a key to every incoming message. The value of this key must also be a scalar, and represent the 'instance' of a message stream. That is, all messages of the same instance are considered a unified stream. add_merge $merge->add_merge({ merge_id => 'm1', match => {x => 'y'}, transform => {this => 'to that'}, expire => 120, #expire in two minutes expire_at => 1465173300, #expire in June 2016 (TODO) }); * merge_def (first positional, required) * merge_id (required) Unique scalar identifying this merge * match (required) Message::Match object (HASHref); defines messages this merge applies to * transform (required) Message::Transform object (HASHref); what changes to make NOTE: considering not making transform required * expire (optional) How many seconds (integer) before this merge expires * expire_at (optional) (TODO) Epoch time (integer) this merge will expire exceptions * must have at least one argument, a HASH reference * passed merge must have a scalar merge_id * passed merge_id '$merge_id' is already defined message $merge->message({ instance_key => 'instance1', x => 'y', }); Coupled with the above defined merge, this message method will call the emit method thusly: (Assuming it's still before the merge expired) ( message => { instance_key => 'instance1', x => 'y', this => 'to that', }, matching_merge_ids => { m1 => 1, }, ) * message (first positional, required) exceptions * must have at least one argument, a HASH reference * passed message did not have instance field remove_merge $merge->remove_merge('m1'); * merge_id (first positional, required) The merge_id to be removed. exceptions * passed merge_id does not reference an existing merge * must have at least one argument, a scalar AUTHOR Dana M. Diederich, BUGS Please report any bugs or feature requests to "bug-message-smartmerge at rt.cpan.org", or through the web interface at . I will be notified, and then you'll automatically be notified of progress on your bug as I make changes. SEE ALSO http://c2.com/cgi/wiki?AlanKayOnMessaging http://spin.atomicobject.com/2012/11/15/message-oriented-programming/ SUPPORT You can find documentation for this module with the perldoc command. perldoc Message::SmartMerge You can also look for information at: * Report bugs and feature requests here * AnnoCPAN: Annotated CPAN documentation * CPAN Ratings * Search CPAN ACKNOWLEDGEMENTS LICENSE AND COPYRIGHT Copyright 2013 Dana M. Diederich. This program is free software; you can redistribute it and/or modify it under the terms of the the Artistic License (2.0). You may obtain a copy of the full license at: Any use, modification, and distribution of the Standard or Modified Versions is governed by this Artistic License. By using, modifying or distributing the Package, you accept this license. Do not use, modify, or distribute the Package, if you do not accept this license. If your Modified Version has been derived from a Modified Version made by someone other than you, you are nevertheless required to ensure that your Modified Version complies with the requirements of this license. This license does not grant you the right to use any trademark, service mark, tradename, or logo of the Copyright Holder. This license includes the non-exclusive, worldwide, free-of-charge patent license to make, have made, use, offer to sell, sell, import and otherwise transfer the Package with respect to any patent claims licensable by the Copyright Holder that are necessarily infringed by the Package. If you institute patent litigation (including a cross-claim or counterclaim) against any party alleging that the Package constitutes direct or contributory patent infringement, then this Artistic License to you shall terminate on the date that such litigation is filed. Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.