001: package org.osbl.agent.model.condition;
002:
003: import org.osbl.agent.model.Condition;
004: import org.osbl.agent.model.Rule;
005: import org.osbl.agent.model.RuleContext;
006:
007: /**
008: * This evaluates a condition related to one of the fields, or properties,
009: * of the subject instance (passed in the {@link org.osbl.agent.model.RuleContext runtime-context}).
010: *
011: * Thus, the specified propertyMeta MUST be a valid property of the subject class.
012: *
013: * Users of this condition specify the property via a {@link org.conform.PropertyMeta propertyMeta},
014: * specify the value to compare to, and provide an suitable {@link Operator operator} for the
015: * evaluation of this two operands.
016: *
017: * @author Sebastian Nozzi.
018: */
019: public class PropertyCondition implements Condition {
020:
021: /** The operator. */
022: private Operator operator;
023:
024: /** The value to compare to. */
025: private Object valueToCompareTo;
026:
027: /** The bean meta class name. */
028: private String beanMetaClassName;
029:
030: /** The property meta name. */
031: private String propertyMetaName;
032:
033: /**
034: * Gets the operator.
035: *
036: * @return the operator
037: */
038: public Operator getOperator() {
039: return operator;
040: }
041:
042: /**
043: * Sets the operator.
044: *
045: * @param operator the new operator
046: */
047: public void setOperator(Operator operator) {
048: this .operator = operator;
049: }
050:
051: /**
052: * Gets the bean meta class name.
053: *
054: * @return the bean meta class name
055: */
056: public String getBeanMetaClassName() {
057: return beanMetaClassName;
058: }
059:
060: /**
061: * Sets the bean meta class name.
062: *
063: * @param beanMetaClassName the new bean meta class name
064: */
065: public void setBeanMetaClassName(String beanMetaClassName) {
066: this .beanMetaClassName = beanMetaClassName;
067: }
068:
069: /**
070: * Gets the property meta name.
071: *
072: * @return the property meta name
073: */
074: public String getPropertyMetaName() {
075: return propertyMetaName;
076: }
077:
078: /**
079: * Sets the property meta name.
080: *
081: * @param propertyMetaName the new property meta name
082: */
083: public void setPropertyMetaName(String propertyMetaName) {
084: this .propertyMetaName = propertyMetaName;
085: }
086:
087: /**
088: * Gets the value to compare to in the evaluation (the right operand).
089: *
090: * @return the value to compare to
091: */
092: public Object getValueToCompareTo() {
093: return valueToCompareTo;
094: }
095:
096: /**
097: * Sets the value to compare to in the evaluation (the right operand).
098: *
099: * @param valueToCompareTo the new value to compare to
100: */
101: public void setValueToCompareTo(Object valueToCompareTo) {
102: this .valueToCompareTo = valueToCompareTo;
103: }
104:
105: /**
106: * Uses the specified operator to perform an evaluation using the specified
107: * propertyMeta and the valueToCompareTo as operands, returning the result
108: * of such evaluation.
109: *
110: * If the valueToCompareTo is not set or null, returns false without evaluation.
111: *
112: * @param context the context
113: *
114: * @return true, if evaluate
115: *
116: * @see org.osbl.agent.model.condition.Condition#evaluate(org.osbl.agent.model.RuleContext)
117: */
118: public boolean evaluate(RuleContext context) {
119:
120: Object value = getValueToCompareTo();
121:
122: // Only compare if there is something to compare against
123: if (value != null) {
124:
125: // Let the operator do the evaluation...
126: // The name of the propertyMeta is a property of our subjectInstance.
127: return operator.evaluate(propertyMetaName, (String) value,
128: context);
129:
130: }
131:
132: return false;
133: }
134:
135: /* (non-Javadoc)
136: * @see java.lang.Object#equals(java.lang.Object)
137: */
138: public boolean equals(Object obj) {
139:
140: if (this == obj)
141: return true;
142:
143: if (obj instanceof PropertyCondition == false)
144: return false;
145:
146: PropertyCondition other = (PropertyCondition) obj;
147:
148: if (!Rule.propertyEquals(operator, other.operator))
149: return false;
150:
151: if (!Rule.propertyEquals(beanMetaClassName,
152: other.beanMetaClassName))
153: return false;
154:
155: if (!Rule.propertyEquals(propertyMetaName,
156: other.propertyMetaName))
157: return false;
158:
159: if (!Rule.propertyEquals(valueToCompareTo,
160: other.valueToCompareTo))
161: return false;
162:
163: return true;
164: }
165:
166: }
|