1 | package com.hammurapi.eventbus; |
2 | |
3 | import java.util.logging.Logger; |
4 | |
5 | /** |
6 | * Discards events with length of inference chain greater than specified threshold, issues warning. |
7 | * @author Pavel Vlasov |
8 | */ |
9 | 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> { |
10 | |
11 | private static final Logger logger = Logger.getLogger(InferenceChainLengthFilter.class.getCanonicalName()); |
12 | |
13 | private int threshold; |
14 | |
15 | public InferenceChainLengthFilter(int threshold) { |
16 | this.threshold = threshold; |
17 | } |
18 | |
19 | public boolean accept(InferenceCommand<E,P,C,K,H,S> inferenceCommand, EventBus<E,P,C,K,H,S> bus) { |
20 | if (inferenceCommand.getInferenceContext().getInferenceChainLength()>threshold) { |
21 | logger.warning("Inference chain is too long ("+inferenceCommand.getInferenceContext().getInferenceChainLength()+") for inference command "+inferenceCommand+", command discarded"); |
22 | return false; |
23 | } |
24 | return true; |
25 | } |
26 | } |