April 2024 - This site, and Kamaelia are being updated. There is significant work needed, and PRs are welcome.

Kamaelia.Util.Comparator

Comparing two data sources

The Comparator component tests two incoming streams to see if the items they contain match (pass an equality test).

Example Usage

Compares contents of two files and prints "MISMATCH!" whenever one is found:

class DetectFalse(component):
    def main(self):
        while 1:
            yield 1
            if self.dataReady("inbox"):
                if not self.recv("inbox"):
                    print "MISMATCH!"

Graphline( file1   = RateControlledFileReader(filename="file 1", ...),
           file2   = RateControlledFileReader(filename="file 2", ...),
           compare = Comparator(),
           fdetect = DetectFalse(),
           output  = ConsoleEchoer(),
           linkages = {
               ("file1","outbox") : ("compare","inA"),
               ("file2","outbox") : ("compare","inB"),
               ("compare", "outbox") : ("fdetect", "inbox"),
               ("fdetect", "outbox") : ("output", "inbox"),
           },
         ).run()

How does it work?

The component simply waits until there is data ready on both its "inA" and "inB" inboxes, then takes an item from each and compares them. The result of the comparison is sent to the "outbox" outbox.

If data is available at neither, or only one, of the two inboxes, then the component will wait indefinitely until data is available on both.

If a producerFinished or shutdownMicroprocess message is received on the "control" inbox, then a producerFinished message is sent out of the "signal" outbox and the component terminates.

The comparison is done by the combine() method. This method returns the result of a simple equality test of the two arguments.

You could always subclass this component and reimplement the combine() method to perform different functions (for example, an 'adder').


Kamaelia.Util.Comparator.Comparator

class Comparator(Axon.Component.component)

Comparator() -> new Comparator component.

Compares items received on "inA" inbox with items received on "inB" inbox. For each pair, outputs True if items compare equal, otherwise False.

Inboxes

  • control : NOT USED
  • inbox : NOT USED
  • inB : Source 'B' of items to compare
  • inA : Source 'A' of items to compare

Outboxes

  • outbox : Result of comparison
  • signal : NOT USED

Methods defined here

Warning!

You should be using the inbox/outbox interface, not these methods (except construction). This documentation is designed as a roadmap as to their functionalilty for maintainers and new component developers.

combine(self, valA, valB)

Returns result of (valA == valB)

Reimplement this method to change the type of comparison from equality testing.

mainBody(self)

Main loop body.

Feedback

Got a problem with the documentation? Something unclear that could be clearer? Want to help improve it? Constructive criticism is very welcome - especially if you can suggest a better rewording!

Please leave you feedback here in reply to the documentation thread in the Kamaelia blog.

-- Automatic documentation generator, 05 Jun 2009 at 03:01:38 UTC/GMT