001: package org.osbl.agent.model.condition;
002:
003: import org.concern.Controller;
004: import org.concern.ControllerException;
005: import org.concern.ControllerLookup;
006: import org.concern.UnknownSubjectException;
007: import org.osbl.agent.model.Condition;
008: import org.osbl.agent.model.Rule;
009: import org.osbl.agent.model.RuleContext;
010:
011: /**
012: * This condition class allows the evaulation of the
013: * {@link org.concern.model.Condition Conditions} defined for the Process of the
014: * subject instance.
015: *
016: * @author Sebastian Nozzi.
017: */
018: public class SubjectCondition implements Condition {
019:
020: /** The con:cern condition name. */
021: private String concernConditionName;
022:
023: /** The process name. */
024: private String processName;
025:
026: /** The operator, which should be one of those defined by this class. */
027: private Operator operator;
028:
029: /**
030: * Instantiates a new subject condition.
031: */
032: public SubjectCondition() {
033:
034: }
035:
036: /**
037: * Gets the con:cern condition name.
038: *
039: * @return the concern condition name
040: */
041: public String getConcernConditionName() {
042: return concernConditionName;
043: }
044:
045: /**
046: * Sets the con:cern condition name.
047: *
048: * @param concernConditionName the new concern condition name
049: */
050: public void setConcernConditionName(String concernConditionName) {
051: this .concernConditionName = concernConditionName;
052: }
053:
054: /**
055: * Gets the con:cern process name.
056: *
057: * @return the process name
058: */
059: public String getProcessName() {
060: return processName;
061: }
062:
063: /**
064: * Sets the con:cern process name.
065: *
066: * @param processName the new process name
067: */
068: public void setProcessName(String processName) {
069: this .processName = processName;
070: }
071:
072: /**
073: * Gets the operator.
074: *
075: * @return the operator
076: */
077: public Operator getOperator() {
078: return operator;
079: }
080:
081: /**
082: * Sets the operator, which should be one of those defined by this class.
083: *
084: * @param operator the new operator
085: */
086: public void setOperator(Operator operator) {
087: this .operator = operator;
088: }
089:
090: /**
091: * Communicates with the corresponding concern controller and evaluates the
092: * specified concern condition, returning the result.
093: *
094: * @param context the context
095: *
096: * @return true, if evaluate
097: *
098: * @see org.osbl.agent.model.condition.Condition#evaluate(org.osbl.agent.model.RuleContext)
099: */
100: public boolean evaluate(RuleContext context) {
101:
102: boolean concernResult = false;
103:
104: String processName = (String) context
105: .get(RuleContext.PROCESS_NAME_KEY);
106: Controller controller = ControllerLookup.getInstance()
107: .getController(processName);
108:
109: try {
110: Boolean result = controller.matchCondition((String) context
111: .get(context.SUBJECT_ID), concernConditionName);
112: concernResult = result.booleanValue();
113: } catch (UnknownSubjectException e) {
114: // TODO Auto-generated catch block
115: e.printStackTrace();
116: } catch (ControllerException e) {
117: // TODO Auto-generated catch block
118: e.printStackTrace();
119: }
120:
121: // Dummy context in which the subjectInstance is a boolean, used
122: // for the evaluation of the operator, which will use the subjectInstance
123: // as its root-object (for OGNL evaluation).
124: RuleContext dummyContext = new RuleContext();
125: dummyContext.setTargetObject(new Boolean(concernResult));
126:
127: return operator
128: .evaluate("booleanValue()", "true", dummyContext);
129:
130: }
131:
132: /* (non-Javadoc)
133: * @see java.lang.Object#equals(java.lang.Object)
134: */
135: public boolean equals(Object obj) {
136:
137: if (this == obj)
138: return true;
139:
140: if (obj instanceof SubjectCondition == false)
141: return false;
142:
143: SubjectCondition other = (SubjectCondition) obj;
144:
145: if (!Rule.propertyEquals(operator, other.operator))
146: return false;
147:
148: if (!Rule.propertyEquals(concernConditionName,
149: other.concernConditionName))
150: return false;
151:
152: return true;
153: }
154:
155: }
|