Skip to Main Content U.S. Department of Energy
 

Using Filters

Filters are used to accept certain messages based on criteria you define. Messages which do not meet this criteria are discarded. This is a good way to reduce the number of messages passing through the pipeline or to route certain types of messages in one direction and other types in another.

To use a filter in MIF, you extend the class AbstractMifFilter and implement a single method to determine if the incoming message should be passed on:

public abstract boolean shouldAccept(Object obj);

When implementing this method, if the object received should be passed on to the next step in the pipeline, return true, otherwise return false. The resulting filter object is attached to an endpoint on a module, so when a message passes through the endpoint, MIF calls shouldAccept. If this method returns true, the message is passed on to the module, if not it is discarded.

Filter Example

The following example is from the Decision Support Control 2007 demonstration. This filter was implemented to remove duplicate UDP messages received from a network sensor.

Implementing the Custom Filter

The following code shows how the duplicate-removing filter DupeFilter was implemented. First, the abstract filter class is extended. We create a field called prev to keep track of the previous object we received.

public class CategoryDupeFilter extends AbstractMifFilter {
  private byte[] prev = null;

Then we implement the the abstract method. The first thing we do is make sure we are receiving the expected type which in this case is a byte array. If the type is not expected, we cannot accept the message.

  public boolean shouldAccept(Object obj) {
    if (obj == null || !(obj instanceof byte[])) {
      log.error("Incoming object not byte array");
      return false;
    }

If this is the first message we received, accept it.

    byte[] curr = (byte[]) obj;
	
    if (prev == null) {
      prev = curr;
      return true;
    }

Return false (ie, reject the message) only if the current message is identical to the previous one.

    for (int i=0; i < curr.length && i < prev.length; i++) {
      if (curr[i] != prev[i]) {
        prev = curr;
        return true;
      }
    }	
    
    return false;
  }
}

Using the Filter

To use the filter in a pipeline, we instantiate it, add it to a MIF pipeline in the configure() method of a component, and attach it to the inbound endpoint of the module we wish to spare the trouble of receiving duplicates.

Here is an excerpt from configure() where we are pulling out our inbound endpoint from the endpoint mapping.

MifEndpoint catIn = endpoints.get("bytes-in");

Then we instantiate the filter, add it to the pipeline and set it on the endpoint.

DupeFilter dupeFilter = new DupeFilter("DupeFilter");
catIn.setFilter( pipeline.addMifFilter(dupeFilter));

Later on, this endpoint is added to the inbound data path on a processing module.

 
filters.txt · Last modified: 2007/10/18 16:12 by adamw