01: package org.osbl.agent.model.condition;
02:
03: import ognl.Ognl;
04: import ognl.OgnlException;
05:
06: import org.osbl.agent.model.Condition;
07: import org.osbl.agent.model.Rule;
08: import org.osbl.agent.model.RuleContext;
09:
10: /**
11: * This class evaluates an OGNL expression, where the root object is the subjectInstance
12: * of the runtime context.
13: *
14: * @see http://www.ognl.org/
15: * @author Sebastian Nozzi.
16: */
17: public class OgnlCondition implements Condition {
18:
19: /** The OGNL expression. */
20: private String expression;
21:
22: /**
23: * Instantiates a new OgnlCondition with the given expression.
24: *
25: * @param expression the expression
26: */
27: public OgnlCondition(String expression) {
28: this .expression = expression;
29: }
30:
31: /**
32: * Instantiates a new OgnlCondition.
33: */
34: public OgnlCondition() {
35: }
36:
37: /**
38: * Gets the OGNL expression.
39: *
40: * @return the expression
41: */
42: public String getExpression() {
43: return expression;
44: }
45:
46: /**
47: * Sets the OGNL expression.
48: *
49: * @param expression the new expression
50: */
51: public void setExpression(String expression) {
52: this .expression = expression;
53: }
54:
55: /**
56: * Evaluates the specified OGNL expression, using the subjectInstance of the
57: * runtime context as the root object.
58: *
59: * @param context the context
60: *
61: * @return true, if evaluate
62: *
63: * @see org.osbl.agent.model.condition.Condition#evaluate(org.osbl.agent.model.RuleContext)
64: */
65: public boolean evaluate(RuleContext context) {
66: // If there is an expression to evaluate...
67: if (expression != null && expression.equals("") == false) {
68: try {
69: // ...evaluate it via OGNL, using the subject instance of the
70: // evaluation context as a root object (which is used when the
71: // expression references fields or methods) and return a boolean
72: // as a result
73: return ((Boolean) Ognl.getValue(expression, context,
74: context.getTargetObject())).booleanValue();
75: } catch (OgnlException e) {
76: // TODO Auto-generated catch block
77: e.printStackTrace();
78: }
79: }
80: return false;
81: }
82:
83: /* (non-Javadoc)
84: * @see java.lang.Object#equals(java.lang.Object)
85: */
86: public boolean equals(Object obj) {
87:
88: if (this == obj)
89: return true;
90:
91: if (obj instanceof OgnlCondition == false)
92: return false;
93:
94: OgnlCondition other = (OgnlCondition) obj;
95:
96: return Rule.propertyEquals(expression, other.expression);
97: }
98:
99: }
|