    001    package com.hammurapi.eventbus;
    002
    003    import java.util.logging.Logger;
    004
    005    /**
    006     * Discards events with length of inference chain greater than specified threshold, issues warning.
    007     * @author Pavel Vlasov
    008     */
    009    public class InferenceChainLengthFilter<E, P extends Comparable<P>, C, K, H extends EventBus.Handle<E,P,C>, S extends EventStore<E,P,C,H,S>> implements InferenceFilter<E,P,C,K,H,S> {
    010
    011            private static final Logger logger = Logger.getLogger(InferenceChainLengthFilter.class.getCanonicalName());
    012
    013            private int threshold;
    014
    015            public InferenceChainLengthFilter(int threshold) {
    016                    this.threshold = threshold;
    017            }
    018
    019            public boolean accept(InferenceCommand<E,P,C,K,H,S> inferenceCommand, EventBus<E,P,C,K,H,S> bus) {
    020                    if (inferenceCommand.getInferenceContext().getInferenceChainLength()>threshold) {
    021                            logger.warning("Inference chain is too long ("+inferenceCommand.getInferenceContext().getInferenceChainLength()+") for inference command "+inferenceCommand+", command discarded");
    022                            return false;
    023                    }
    024                    return true;
    025            }
    026    }