1 | // Copyright 2004-2007 Jean-Francois Poilpret |
2 | // |
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | // you may not use this file except in compliance with the License. |
5 | // You may obtain a copy of the License at |
6 | // |
7 | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | // |
9 | // Unless required by applicable law or agreed to in writing, software |
10 | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | // See the License for the specific language governing permissions and |
13 | // limitations under the License. |
14 | |
15 | package net.sourceforge.hiveevents; |
16 | |
17 | import java.util.HashMap; |
18 | import java.util.Map; |
19 | |
20 | /** |
21 | * Event filter that is based on a constraint expressed in a specific language, |
22 | * OCL (Object Constraint Language). |
23 | * Allows to easily define filters that have complex constraints. |
24 | * <p> |
25 | * The OCL is a simple language that allows evaluation of a boolean expression |
26 | * based on various operators (comparison, boolean arithmetic). It also can |
27 | * reflect any property of the vent to be evaluated (for further checks). |
28 | * <p> |
29 | * Example: |
30 | * <pre> |
31 | * event.id != 0 && (event.type == 1 || event.type == 2) && event.priority > 2 |
32 | * </pre> |
33 | * In that example <code>event</code> is the variable name that can be used to |
34 | * represent the event to be evaluated by the filter. |
35 | * |
36 | * @author Jean-Francois Poilpret |
37 | */ |
38 | public class ConstraintFilter<T> implements Filter<T> |
39 | { |
40 | static final public String EVENT_NAME = "event"; |
41 | |
42 | /** |
43 | * Build a new ConstraintFilter. |
44 | * @param constraint the expression to evaluate for each event |
45 | * @param eventClass class of the event (used for event reflection control) |
46 | */ |
47 | public ConstraintFilter(String constraint, Class eventClass) |
48 | { |
49 | Map<String, Class> classes = new HashMap<String, Class>(); |
50 | classes.put(EVENT_NAME, eventClass); |
51 | init(constraint, classes, new HashMap<String, Object>()); |
52 | } |
53 | |
54 | /** |
55 | * Build a new ConstraintFilter with one additional variable in the |
56 | * expression. |
57 | * <p> |
58 | * This is seldom used. |
59 | * |
60 | * @param constraint the expression to evaluate for each event |
61 | * @param eventClass class of the event (used for event reflection control) |
62 | * @param argName the name of the additional variable (as used in the |
63 | * constraint expression) |
64 | * @param argClass the class of the additional variable (used for reflection |
65 | * control) |
66 | * @param argValue the value of the additional variable |
67 | */ |
68 | public ConstraintFilter(String constraint, |
69 | Class eventClass, |
70 | String argName, |
71 | Class argClass, |
72 | Object argValue) |
73 | { |
74 | Map<String, Class> classes = new HashMap<String, Class>(); |
75 | classes.put(EVENT_NAME, eventClass); |
76 | classes.put(argName, argClass); |
77 | Map<String, Object> variables = new HashMap<String, Object>(); |
78 | variables.put(argName, argValue); |
79 | init(constraint, classes, variables); |
80 | } |
81 | |
82 | /** |
83 | * Build a new ConstraintFilter with additional variables in the |
84 | * expression. |
85 | * <p> |
86 | * This is seldom used. |
87 | * |
88 | * @param constraint the expression to evaluate for each event |
89 | * @param classes the classes of additional variables (used for reflection |
90 | * control) indexed by name (must contain "event") |
91 | * @param values the values of additional variables indexed by name (must |
92 | * contain "event") |
93 | */ |
94 | public ConstraintFilter(String constraint, |
95 | Map<String, Class> classes, |
96 | Map<String, Object> values) |
97 | { |
98 | init(constraint, classes, values); |
99 | } |
100 | |
101 | private void init( String constraint, |
102 | Map<String, Class> classes, |
103 | Map<String, Object> values) |
104 | { |
105 | _evaluator = new ConstraintEvaluator(constraint, classes); |
106 | _variables = values; |
107 | } |
108 | |
109 | public boolean passEvent(T event) |
110 | { |
111 | Map<String, Object> variables = new HashMap<String, Object>(); |
112 | variables.putAll(_variables); |
113 | variables.put(EVENT_NAME, event); |
114 | return _evaluator.evaluate(variables); |
115 | } |
116 | |
117 | private ConstraintEvaluator _evaluator; |
118 | private Map<String, Object> _variables; |
119 | } |